From c878a51509d22d4ef774ebb81b90e385ded6f885 Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Mon, 23 Oct 2023 22:47:09 +0300 Subject: QLatin1StringMatcher: add indexIn(QStringView) overload MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Drive-by changes, remove a redundant typedef. [ChangeLog][QtCore][QLatin1StringMatcher] Added indexIn(QStringView) overload. Task-number: QTBUG-117054 Change-Id: I5a8426cb0f9d9111f086015902ffe2185a267c86 Reviewed-by: Thiago Macieira Reviewed-by: Øystein Heskestad --- src/corelib/text/qlatin1stringmatcher.cpp | 42 +++++++++++++++++++++++++++---- src/corelib/text/qlatin1stringmatcher.h | 9 +++++++ 2 files changed, 46 insertions(+), 5 deletions(-) (limited to 'src/corelib/text') diff --git a/src/corelib/text/qlatin1stringmatcher.cpp b/src/corelib/text/qlatin1stringmatcher.cpp index 68bf97db5c..9036048fff 100644 --- a/src/corelib/text/qlatin1stringmatcher.cpp +++ b/src/corelib/text/qlatin1stringmatcher.cpp @@ -160,6 +160,31 @@ Qt::CaseSensitivity QLatin1StringMatcher::caseSensitivity() const noexcept */ qsizetype QLatin1StringMatcher::indexIn(QLatin1StringView haystack, qsizetype from) const noexcept { + return indexIn_helper(haystack, from); +} + +/*! + \since 6.8 + \overload + + Searches for the pattern in the given \a haystack starting from index + position \a from. + + \sa caseSensitivity(), pattern() +*/ +qsizetype QLatin1StringMatcher::indexIn(QStringView haystack, qsizetype from) const noexcept +{ + return indexIn_helper(haystack, from); +} + +/*! + \internal +*/ +template +qsizetype QLatin1StringMatcher::indexIn_helper(String haystack, qsizetype from) const noexcept +{ + static_assert(QtPrivate::isLatin1OrUtf16View); + if (m_pattern.isEmpty() && from == haystack.size()) return from; if (from < 0) // Historical behavior (see QString::indexOf and co.) @@ -167,8 +192,15 @@ qsizetype QLatin1StringMatcher::indexIn(QLatin1StringView haystack, qsizetype fr if (from >= haystack.size()) return -1; - auto begin = haystack.begin() + from; - auto end = haystack.end(); + const auto start = [haystack] { + if constexpr (std::is_same_v) + return haystack.utf16(); + else + return haystack.begin(); + }(); + + auto begin = start + from; + auto end = start + haystack.size(); auto found = begin; if (m_cs == Qt::CaseSensitive) { found = m_caseSensitiveSearcher(begin, end, m_pattern.begin(), m_pattern.end()).begin; @@ -178,7 +210,7 @@ qsizetype QLatin1StringMatcher::indexIn(QLatin1StringView haystack, qsizetype fr const qsizetype bufferSize = std::min(m_pattern.size(), qsizetype(sizeof m_foldBuffer)); const QLatin1StringView restNeedle = m_pattern.sliced(bufferSize); const bool needleLongerThanBuffer = restNeedle.size() > 0; - QLatin1StringView restHaystack = haystack; + String restHaystack = haystack; do { found = m_caseInsensitiveSearcher(found, end, m_foldBuffer, &m_foldBuffer[bufferSize]) .begin; @@ -189,13 +221,13 @@ qsizetype QLatin1StringMatcher::indexIn(QLatin1StringView haystack, qsizetype fr } restHaystack = haystack.sliced( qMin(haystack.size(), - bufferSize + qsizetype(std::distance(haystack.begin(), found)))); + bufferSize + qsizetype(std::distance(start, found)))); if (restHaystack.startsWith(restNeedle, Qt::CaseInsensitive)) break; ++found; } while (true); } - return std::distance(haystack.begin(), found); + return std::distance(start, found); } QT_END_NAMESPACE diff --git a/src/corelib/text/qlatin1stringmatcher.h b/src/corelib/text/qlatin1stringmatcher.h index 3b8c24fc92..dd3414fc6d 100644 --- a/src/corelib/text/qlatin1stringmatcher.h +++ b/src/corelib/text/qlatin1stringmatcher.h @@ -14,6 +14,10 @@ QT_BEGIN_NAMESPACE namespace QtPrivate { +template +constexpr inline bool isLatin1OrUtf16View = + std::disjunction_v, std::is_same>; + template::value_type>, class BinaryPredicate = std::equal_to<>> @@ -147,6 +151,7 @@ public: Q_CORE_EXPORT Qt::CaseSensitivity caseSensitivity() const noexcept; Q_CORE_EXPORT qsizetype indexIn(QLatin1StringView haystack, qsizetype from = 0) const noexcept; + Q_CORE_EXPORT qsizetype indexIn(QStringView haystack, qsizetype from = 0) const noexcept; private: void setSearcher() noexcept; @@ -164,6 +169,10 @@ private: CaseSensitiveSearcher m_caseSensitiveSearcher; CaseInsensitiveSearcher m_caseInsensitiveSearcher; }; + + template + qsizetype indexIn_helper(String haystack, qsizetype from) const noexcept; + char m_foldBuffer[256]; }; -- cgit v1.2.3