Port qt_string_count() to QStringView

It was not using QStringView API, but immediately dropped to bare
metal operations (reinterpret_cast, ushort), subjecting itself to
ushort -> char16_t issues.

The new formulation avoids low-level primitives.

Change-Id: I8e75c7ea7409b133ff43bf5c829aa1f8f7503f11
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
This commit is contained in:
Marc Mutz 2020-05-06 13:58:12 +02:00
parent 1eaf7fd544
commit d124eeb00c

View File

@ -11743,19 +11743,18 @@ static inline qsizetype qt_string_count(QStringView haystack, QStringView needle
static inline qsizetype qt_string_count(QStringView haystack, QChar ch,
Qt::CaseSensitivity cs)
{
ushort c = ch.unicode();
qsizetype num = 0;
const ushort *b = reinterpret_cast<const ushort*>(haystack.data());
const ushort *i = b + haystack.size();
if (cs == Qt::CaseSensitive) {
while (i != b)
if (*--i == c)
for (QChar c : haystack) {
if (c == ch)
++num;
}
} else {
c = foldCase(c);
while (i != b)
if (foldCase(*(--i)) == c)
ch = foldCase(ch);
for (QChar c : haystack) {
if (foldCase(c) == ch)
++num;
}
}
return num;
}