From 8db16bfa7e2ebfcb710d8d9e885c2d54d7b29b4f Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Thu, 16 Dec 2021 15:27:34 -0300 Subject: [PATCH] QString: use char16_t in isAscii() instead of QChar Drive-by simple clarification of the code that needed to be touched anyway. Pick-to: 6.3 6.2 Change-Id: Ib42b3adc93bf4d43bd55fffd16c14f984b0fb592 Reviewed-by: Marc Mutz --- src/corelib/text/qstring.cpp | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index 02b7895645b..7f844db04ff 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -542,19 +542,19 @@ bool QtPrivate::isAscii(QLatin1String s) noexcept return qt_is_ascii(ptr, end); } -static bool isAscii(const QChar *&ptr, const QChar *end) +static bool isAscii_helper(const char16_t *&ptr, const char16_t *end) { #ifdef __SSE2__ const char *ptr8 = reinterpret_cast(ptr); const char *end8 = reinterpret_cast(end); bool ok = simdTestMask(ptr8, end8, 0xff80ff80); - ptr = reinterpret_cast(ptr8); + ptr = reinterpret_cast(ptr8); if (!ok) return false; #endif while (ptr != end) { - if (ptr->unicode() & 0xff80) + if (*ptr & 0xff80) return false; ++ptr; } @@ -563,27 +563,27 @@ static bool isAscii(const QChar *&ptr, const QChar *end) bool QtPrivate::isAscii(QStringView s) noexcept { - const QChar *ptr = s.begin(); - const QChar *end = s.end(); + const char16_t *ptr = s.utf16(); + const char16_t *end = ptr + s.size(); - return isAscii(ptr, end); + return isAscii_helper(ptr, end); } bool QtPrivate::isLatin1(QStringView s) noexcept { - const QChar *ptr = s.begin(); - const QChar *end = s.end(); + const char16_t *ptr = s.utf16(); + const char16_t *end = ptr + s.size(); #ifdef __SSE2__ const char *ptr8 = reinterpret_cast(ptr); const char *end8 = reinterpret_cast(end); if (!simdTestMask(ptr8, end8, 0xff00ff00)) return false; - ptr = reinterpret_cast(ptr8); + ptr = reinterpret_cast(ptr8); #endif while (ptr != end) { - if ((*ptr++).unicode() > 0xff) + if (*ptr++ > 0xff) return false; } return true; @@ -7702,11 +7702,15 @@ QString QString::repeated(qsizetype times) const void qt_string_normalize(QString *data, QString::NormalizationForm mode, QChar::UnicodeVersion version, qsizetype from) { - const QChar *p = data->constData() + from; - if (isAscii(p, p + data->length() - from)) - return; - if (p > data->constData() + from) - from = p - data->constData() - 1; // need one before the non-ASCII to perform NFC + { + // check if it's fully ASCII first, because then we have no work + auto start = reinterpret_cast(data->constData()); + const char16_t *p = start + from; + if (isAscii_helper(p, p + data->length() - from)) + return; + if (p > start + from) + from = p - start - 1; // need one before the non-ASCII to perform NFC + } if (version == QChar::Unicode_Unassigned) { version = QChar::currentUnicodeVersion();