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