QString:add (last)indexOf overload with QRegularExpressionMatch output
This patch adds indexOf and lastIndexOf with QRegularExpressionMatch output overloads to QString. This allows to get the match corresponding to the index returned. [ChangeLog][QtCore][QString] Added support for retrieving the QRegularExpressionMatch to indexOf and lastIndexOf. Change-Id: Ia0ae2d3ff78864c7053ffa397874aca1d2b1c35c Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
This commit is contained in:
parent
a8a8a3bfbd
commit
21d9ad8b11
@ -394,6 +394,13 @@ void Widget::firstIndexOfFunction()
|
||||
QString str = "the minimum";
|
||||
str.indexOf(QRegularExpression("m[aeiou]"), 0); // returns 4
|
||||
//! [93]
|
||||
|
||||
//! [97]
|
||||
QString str = "the minimum";
|
||||
QRegularExpressionMatch match;
|
||||
str.indexOf(QRegularExpression("m[aeiou]"), 0, &match); // returns 4
|
||||
// match.captured() == mi
|
||||
//! [97]
|
||||
}
|
||||
|
||||
void Widget::insertFunction()
|
||||
@ -444,6 +451,13 @@ void Widget::lastIndexOfFunction()
|
||||
QString str = "the minimum";
|
||||
str.lastIndexOf(QRegularExpression("m[aeiou]")); // returns 8
|
||||
//! [94]
|
||||
|
||||
//! [98]
|
||||
QString str = "the minimum";
|
||||
QRegularExpressionMatch match;
|
||||
str.lastIndexOf(QRegularExpression("m[aeiou]"), -1, &match); // returns 8
|
||||
// match.captured() == mu
|
||||
//! [98]
|
||||
}
|
||||
|
||||
void Widget::leftFunction()
|
||||
|
@ -3676,6 +3676,27 @@ int QString::count(const QRegExp& rx) const
|
||||
\snippet qstring/main.cpp 93
|
||||
*/
|
||||
int QString::indexOf(const QRegularExpression& re, int from) const
|
||||
{
|
||||
return indexOf(re, from, Q_NULLPTR);
|
||||
}
|
||||
|
||||
/*!
|
||||
\overload
|
||||
\since 5.5
|
||||
|
||||
Returns the index position of the first match of the regular
|
||||
expression \a re in the string, searching forward from index
|
||||
position \a from. Returns -1 if \a re didn't match anywhere.
|
||||
|
||||
If the match is successful and \a rmatch is not a null pointer, it also
|
||||
writes the results of the match into the QRegularExpressionMatch object
|
||||
pointed to by \a rmatch.
|
||||
|
||||
Example:
|
||||
|
||||
\snippet qstring/main.cpp 97
|
||||
*/
|
||||
int QString::indexOf(const QRegularExpression &re, int from, QRegularExpressionMatch *rmatch) const
|
||||
{
|
||||
if (!re.isValid()) {
|
||||
qWarning("QString::indexOf: invalid QRegularExpression object");
|
||||
@ -3683,8 +3704,12 @@ int QString::indexOf(const QRegularExpression& re, int from) const
|
||||
}
|
||||
|
||||
QRegularExpressionMatch match = re.match(*this, from);
|
||||
if (match.hasMatch())
|
||||
return match.capturedStart();
|
||||
if (match.hasMatch()) {
|
||||
const int ret = match.capturedStart();
|
||||
if (rmatch)
|
||||
*rmatch = qMove(match);
|
||||
return ret;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
@ -3702,6 +3727,27 @@ int QString::indexOf(const QRegularExpression& re, int from) const
|
||||
\snippet qstring/main.cpp 94
|
||||
*/
|
||||
int QString::lastIndexOf(const QRegularExpression &re, int from) const
|
||||
{
|
||||
return lastIndexOf(re, from, Q_NULLPTR);
|
||||
}
|
||||
|
||||
/*!
|
||||
\overload
|
||||
\since 5.5
|
||||
|
||||
Returns the index position of the last match of the regular
|
||||
expression \a re in the string, which starts before the index
|
||||
position \a from. Returns -1 if \a re didn't match anywhere.
|
||||
|
||||
If the match is successful and \a rmatch is not a null pointer, it also
|
||||
writes the results of the match into the QRegularExpressionMatch object
|
||||
pointed to by \a rmatch.
|
||||
|
||||
Example:
|
||||
|
||||
\snippet qstring/main.cpp 98
|
||||
*/
|
||||
int QString::lastIndexOf(const QRegularExpression &re, int from, QRegularExpressionMatch *rmatch) const
|
||||
{
|
||||
if (!re.isValid()) {
|
||||
qWarning("QString::lastIndexOf: invalid QRegularExpression object");
|
||||
@ -3709,16 +3755,18 @@ int QString::lastIndexOf(const QRegularExpression &re, int from) const
|
||||
}
|
||||
|
||||
int endpos = (from < 0) ? (size() + from + 1) : (from + 1);
|
||||
|
||||
QRegularExpressionMatchIterator iterator = re.globalMatch(*this);
|
||||
int lastIndex = -1;
|
||||
while (iterator.hasNext()) {
|
||||
QRegularExpressionMatch match = iterator.next();
|
||||
int start = match.capturedStart();
|
||||
if (start < endpos)
|
||||
if (start < endpos) {
|
||||
lastIndex = start;
|
||||
else
|
||||
if (rmatch)
|
||||
*rmatch = qMove(match);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return lastIndex;
|
||||
|
@ -335,7 +335,9 @@ public:
|
||||
|
||||
#ifndef QT_NO_REGULAREXPRESSION
|
||||
int indexOf(const QRegularExpression &re, int from = 0) const;
|
||||
int indexOf(const QRegularExpression &re, int from, QRegularExpressionMatch *rmatch) const; // ### Qt 6: merge overloads
|
||||
int lastIndexOf(const QRegularExpression &re, int from = -1) const;
|
||||
int lastIndexOf(const QRegularExpression &re, int from, QRegularExpressionMatch *rmatch) const; // ### Qt 6: merge overloads
|
||||
bool contains(const QRegularExpression &re) const;
|
||||
bool contains(const QRegularExpression &re, QRegularExpressionMatch *match) const; // ### Qt 6: merge overloads
|
||||
int count(const QRegularExpression &re) const;
|
||||
|
@ -1191,6 +1191,18 @@ void tst_QString::indexOf()
|
||||
|
||||
QRegularExpression re(QRegularExpression::escape(needle), options);
|
||||
QCOMPARE( haystack.indexOf(re, startpos), resultpos );
|
||||
QCOMPARE(haystack.indexOf(re, startpos, Q_NULLPTR), resultpos);
|
||||
|
||||
QRegularExpressionMatch match;
|
||||
QVERIFY(!match.hasMatch());
|
||||
QCOMPARE(haystack.indexOf(re, startpos, &match), resultpos);
|
||||
QCOMPARE(match.hasMatch(), resultpos != -1);
|
||||
if (resultpos > -1 && needleIsLatin) {
|
||||
if (bcs)
|
||||
QVERIFY(match.captured() == needle);
|
||||
else
|
||||
QVERIFY(match.captured().toLower() == needle.toLower());
|
||||
}
|
||||
}
|
||||
|
||||
if (cs == Qt::CaseSensitive) {
|
||||
@ -1303,6 +1315,14 @@ void tst_QString::indexOfInvalidRegex()
|
||||
{
|
||||
QTest::ignoreMessage(QtWarningMsg, "QString::indexOf: invalid QRegularExpression object");
|
||||
QCOMPARE(QString("invalid regex\\").indexOf(QRegularExpression("invalid regex\\")), -1);
|
||||
QTest::ignoreMessage(QtWarningMsg, "QString::indexOf: invalid QRegularExpression object");
|
||||
QCOMPARE(QString("invalid regex\\").indexOf(QRegularExpression("invalid regex\\"), -1, Q_NULLPTR), -1);
|
||||
|
||||
QRegularExpressionMatch match;
|
||||
QVERIFY(!match.hasMatch());
|
||||
QTest::ignoreMessage(QtWarningMsg, "QString::indexOf: invalid QRegularExpression object");
|
||||
QCOMPARE(QString("invalid regex\\").indexOf(QRegularExpression("invalid regex\\"), -1, &match), -1);
|
||||
QVERIFY(!match.hasMatch());
|
||||
}
|
||||
|
||||
void tst_QString::lastIndexOf_data()
|
||||
@ -1394,6 +1414,17 @@ void tst_QString::lastIndexOf()
|
||||
|
||||
QRegularExpression re(QRegularExpression::escape(needle), options);
|
||||
QCOMPARE(haystack.lastIndexOf(re, from), expected);
|
||||
QCOMPARE(haystack.lastIndexOf(re, from, Q_NULLPTR), expected);
|
||||
QRegularExpressionMatch match;
|
||||
QVERIFY(!match.hasMatch());
|
||||
QCOMPARE(haystack.lastIndexOf(re, from, &match), expected);
|
||||
QCOMPARE(match.hasMatch(), expected > -1);
|
||||
if (expected > -1) {
|
||||
if (caseSensitive)
|
||||
QCOMPARE(match.captured(), needle);
|
||||
else
|
||||
QCOMPARE(match.captured().toLower(), needle.toLower());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1419,6 +1450,14 @@ void tst_QString::lastIndexOfInvalidRegex()
|
||||
{
|
||||
QTest::ignoreMessage(QtWarningMsg, "QString::lastIndexOf: invalid QRegularExpression object");
|
||||
QCOMPARE(QString("invalid regex\\").lastIndexOf(QRegularExpression("invalid regex\\"), 0), -1);
|
||||
QTest::ignoreMessage(QtWarningMsg, "QString::lastIndexOf: invalid QRegularExpression object");
|
||||
QCOMPARE(QString("invalid regex\\").lastIndexOf(QRegularExpression("invalid regex\\"), -1, Q_NULLPTR), -1);
|
||||
|
||||
QRegularExpressionMatch match;
|
||||
QVERIFY(!match.hasMatch());
|
||||
QTest::ignoreMessage(QtWarningMsg, "QString::lastIndexOf: invalid QRegularExpression object");
|
||||
QCOMPARE(QString("invalid regex\\").lastIndexOf(QRegularExpression("invalid regex\\"), -1, &match), -1);
|
||||
QVERIFY(!match.hasMatch());
|
||||
}
|
||||
|
||||
void tst_QString::count()
|
||||
|
Loading…
x
Reference in New Issue
Block a user