From 341854a4f36dd7e4afd80c7049a782b39423ff20 Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Wed, 4 Oct 2023 12:29:33 +0300 Subject: [PATCH] QStringMatcher: add a method that returns a string view of the pattern The existing pattern() method always returns a QString, which means that if the matcher was constructed using a QStringView, pattern() would uncoditionally convert it to a QString. This is useful to check if a match is exact: auto pattern = matcher.patternView(); if (pattern.size() == needle.size() && matcher.indexIn(needle) == 0) .... This may be needed for a later change in QStringList::contains(); regardless of that, this change makes sense on its own. Change-Id: I49018551dd22a8f88cf6b9f878a5166902a26f58 Reviewed-by: Thiago Macieira --- src/corelib/text/qstringmatcher.cpp | 9 +++++++++ src/corelib/text/qstringmatcher.h | 3 +++ .../corelib/text/qstringmatcher/tst_qstringmatcher.cpp | 2 ++ 3 files changed, 14 insertions(+) diff --git a/src/corelib/text/qstringmatcher.cpp b/src/corelib/text/qstringmatcher.cpp index ac66ec802e..379d555e54 100644 --- a/src/corelib/text/qstringmatcher.cpp +++ b/src/corelib/text/qstringmatcher.cpp @@ -242,6 +242,15 @@ QString QStringMatcher::pattern() const return q_sv.toString(); } +/*! + \fn QStringView QStringMatcher::patternView() const noexcept + \since 6.7 + + Returns a string view of the pattern that this string matcher will search for. + + \sa setPattern() +*/ + /*! Sets the case sensitivity setting of this string matcher to \a cs. diff --git a/src/corelib/text/qstringmatcher.h b/src/corelib/text/qstringmatcher.h index 581d8931e4..937f17df0a 100644 --- a/src/corelib/text/qstringmatcher.h +++ b/src/corelib/text/qstringmatcher.h @@ -40,6 +40,9 @@ public: { return indexIn(QStringView(str, length), from); } qsizetype indexIn(QStringView str, qsizetype from = 0) const; QString pattern() const; + QStringView patternView() const noexcept + { return q_sv; } + inline Qt::CaseSensitivity caseSensitivity() const { return q_cs; } private: diff --git a/tests/auto/corelib/text/qstringmatcher/tst_qstringmatcher.cpp b/tests/auto/corelib/text/qstringmatcher/tst_qstringmatcher.cpp index 6378ed8f5a..4488e5e2c5 100644 --- a/tests/auto/corelib/text/qstringmatcher/tst_qstringmatcher.cpp +++ b/tests/auto/corelib/text/qstringmatcher/tst_qstringmatcher.cpp @@ -24,6 +24,7 @@ void tst_QStringMatcher::qstringmatcher() QCOMPARE(matcher.caseSensitivity(), Qt::CaseSensitive); QCOMPARE(matcher.indexIn("foo", 1), 1); QCOMPARE(matcher.pattern(), QString()); + QCOMPARE(matcher.patternView(), QStringView()); } // public Qt::CaseSensitivity caseSensitivity() const @@ -143,6 +144,7 @@ void tst_QStringMatcher::assignOperator() QStringMatcher m2 = m1; QCOMPARE(m2.pattern(), needle); + QCOMPARE(m2.patternView(), needle); QCOMPARE(m2.indexIn(hayStack), 3); }