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 <thiago.macieira@intel.com>
This commit is contained in:
Ahmad Samir 2023-10-04 12:29:33 +03:00
parent 90532e0b01
commit 341854a4f3
3 changed files with 14 additions and 0 deletions

View File

@ -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.

View File

@ -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:

View File

@ -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);
}