diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2022-01-09 20:35:59 -0800 |
---|---|---|
committer | Thiago Macieira <thiago.macieira@intel.com> | 2022-01-18 21:38:12 -0800 |
commit | 2932c3f942b4f52f113a52fbeb261a14c4e14cd4 (patch) | |
tree | a139361abf89c58fde67543bff01b769333cc532 /src/corelib | |
parent | b3863464687aa406f1acf90ab2ff9a1cb29d868c (diff) |
QString: introduce ucstreq() to optimize equalStrings()
If the lengths aren't equal, the strings can't be equal either, so we
can skip the entire comparison. Some of the front-end functions that
call these entry points already check for this, actually.
Change-Id: Ib42b3adc93bf4d43bd55fffd16c8ceb9594512f2
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
Diffstat (limited to 'src/corelib')
-rw-r--r-- | src/corelib/text/qstring.cpp | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index 7c6d39d540..594b0d2298 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -1314,6 +1314,19 @@ constexpr int lencmp(qsizetype lhs, qsizetype rhs) noexcept /* else */ -1 ; } +// Unicode case-sensitive equality +template <typename Char2> +static bool ucstreq(const QChar *a, size_t alen, const Char2 *b, size_t blen) +{ + if (alen != blen) + return false; + if constexpr (std::is_same_v<decltype(a), decltype(b)>) { + if (a == b) + return true; + } + return ucstrncmp(a, b, alen) == 0; +} + // Unicode case-sensitive comparison static int ucstrcmp(const QChar *a, size_t alen, const QChar *b, size_t blen) { @@ -1371,12 +1384,12 @@ static int latin1nicmp(const char *lhsChar, qsizetype lSize, const char *rhsChar } bool QtPrivate::equalStrings(QStringView lhs, QStringView rhs) noexcept { - return ucstrcmp(lhs.begin(), lhs.size(), rhs.begin(), rhs.size()) == 0; + return ucstreq(lhs.begin(), lhs.size(), rhs.begin(), rhs.size()); } bool QtPrivate::equalStrings(QStringView lhs, QLatin1String rhs) noexcept { - return ucstrcmp(lhs.begin(), lhs.size(), rhs.begin(), rhs.size()) == 0; + return ucstreq(lhs.begin(), lhs.size(), rhs.begin(), rhs.size()); } bool QtPrivate::equalStrings(QLatin1String lhs, QStringView rhs) noexcept |