From af2d9bc3c46818f2b99056e5407493f0830dab3b Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Mon, 24 Jan 2022 09:26:54 +0100 Subject: Optimize finding UTF-16 needles in L1 haystacks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We were converting the full L1 haystack to UTF-16. But, even so, we won't be able to find non-L1 UTF-16 needles in L1 haystacks! Optimize by converting the needle (not the haystack) to L1, after checking it's actually L1. Pick-to: 6.3 Change-Id: I413d7a602d44ecb2d3149dc4fa87c690c40e6aaa Reviewed-by: MÃ¥rten Nordheim --- src/corelib/text/qstring.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index 7a908cfd98..a8482c5fbd 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -10508,9 +10508,12 @@ qsizetype QtPrivate::findString(QLatin1String haystack, qsizetype from, QStringV if (haystack.size() < needle.size()) return -1; - QVarLengthArray s(haystack.size()); - qt_from_latin1(s.data(), haystack.latin1(), haystack.size()); - return QtPrivate::findString(QStringView(reinterpret_cast(s.constData()), s.size()), from, needle, cs); + if (!QtPrivate::isLatin1(needle)) // won't find non-L1 UTF-16 needles in a L1 haystack! + return -1; + + QVarLengthArray s(needle.size()); + qt_to_latin1_unchecked(reinterpret_cast(s.data()), needle.utf16(), needle.size()); + return QtPrivate::findString(haystack, from, QLatin1String(s.data(), s.size()), cs); } qsizetype QtPrivate::findString(QLatin1String haystack, qsizetype from, QLatin1String needle, Qt::CaseSensitivity cs) noexcept -- cgit v1.2.3