From 2932c3f942b4f52f113a52fbeb261a14c4e14cd4 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Sun, 9 Jan 2022 20:35:59 -0800 Subject: 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 --- src/corelib/text/qstring.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'src/corelib') 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 +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) { + 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 -- cgit v1.2.3