From 0f52ec8d5265e23f563f91a290a60e7d8e714166 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 17 Feb 2022 08:55:20 +0100 Subject: [PATCH] QByteArray: Extract Function QtMiscUtils::caseCompareAscii() Change-Id: Id9c385f4df4f33792738cd134fabb9cc03cab809 Reviewed-by: Qt CI Bot Reviewed-by: Thiago Macieira --- src/corelib/text/qbytearray.cpp | 8 ++++---- src/corelib/tools/qtools_p.h | 9 +++++++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/corelib/text/qbytearray.cpp b/src/corelib/text/qbytearray.cpp index c587e9c254..9f768484b6 100644 --- a/src/corelib/text/qbytearray.cpp +++ b/src/corelib/text/qbytearray.cpp @@ -272,7 +272,7 @@ int qstricmp(const char *str1, const char *str2) max += offset; do { uchar c = s1[offset]; - if (int res = asciiLower(c) - asciiLower(s2[offset])) + if (int res = QtMiscUtils::caseCompareAscii(c, s2[offset])) return res; if (!c) return 0; @@ -355,7 +355,7 @@ int qstrnicmp(const char *str1, const char *str2, size_t len) return s1 ? 1 : (s2 ? -1 : 0); for (; len--; ++s1, ++s2) { const uchar c = *s1; - if (int res = asciiLower(c) - asciiLower(*s2)) + if (int res = QtMiscUtils::caseCompareAscii(c, *s2)) return res; if (!c) // strings are equal break; @@ -396,7 +396,7 @@ int qstrnicmp(const char *str1, qsizetype len1, const char *str2, qsizetype len2 if (!c) return 1; - if (int res = asciiLower(s1[i]) - asciiLower(c)) + if (int res = QtMiscUtils::caseCompareAscii(s1[i], c)) return res; } return s2[i] ? -1 : 0; @@ -404,7 +404,7 @@ int qstrnicmp(const char *str1, qsizetype len1, const char *str2, qsizetype len2 // not null-terminated const qsizetype len = qMin(len1, len2); for (qsizetype i = 0; i < len; ++i) { - if (int res = asciiLower(s1[i]) - asciiLower(s2[i])) + if (int res = QtMiscUtils::caseCompareAscii(s1[i], s2[i])) return res; } if (len1 == len2) diff --git a/src/corelib/tools/qtools_p.h b/src/corelib/tools/qtools_p.h index 9d4a3ffe60..e952297831 100644 --- a/src/corelib/tools/qtools_p.h +++ b/src/corelib/tools/qtools_p.h @@ -89,6 +89,15 @@ constexpr inline char toAsciiLower(char ch) noexcept { return (ch >= 'A' && ch <= 'Z') ? ch - 'A' + 'a' : ch; } + +constexpr inline int caseCompareAscii(char lhs, char rhs) noexcept +{ + const char lhsLower = QtMiscUtils::toAsciiLower(lhs); + const char rhsLower = QtMiscUtils::toAsciiLower(rhs); + return int(uchar(lhsLower)) - int(uchar(rhsLower)); +} + + } // We typically need an extra bit for qNextPowerOfTwo when determining the next allocation size.