QString::contains overload that returns the match results

This convenience overload allows one to write

    QRegularExpression re1, re2, ...;
    QRegularExpressionMatch match;
    QString subject;

    if (subject.contains(re1, &match)) {
        // ...
    } else if (subject.contains(re2, &match)) {
        // ...
    } // ..

One can then inspect the results of a successful match in each block
(as well as extracting the captured substrings, etc.).

Change-Id: I0fb8be8b577656e8db994198f8105c26c4fe67b0
Reviewed-by: Lars Knoll <lars.knoll@digia.com>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Giuseppe D'Angelo 2012-10-14 17:10:43 +01:00 committed by The Qt Project
parent 65fba49d63
commit 9110d4f1ed
3 changed files with 78 additions and 0 deletions

View File

@ -3339,6 +3339,33 @@ bool QString::contains(const QRegularExpression &re) const
return match.hasMatch();
}
/*!
\overload contains()
\since 5.1
Returns true if the regular expression \a re matches somewhere in this
string; otherwise returns false.
If the match is successful and \a match is not a null pointer, it also
writes the results of the match into the QRegularExpressionMatch object
pointed by \a match.
\sa QRegularExpression::match()
*/
bool QString::contains(const QRegularExpression &re, QRegularExpressionMatch *match) const
{
if (!re.isValid()) {
qWarning("QString::contains: invalid QRegularExpresssion object");
return false;
}
QRegularExpressionMatch m = re.match(*this);
bool hasMatch = m.hasMatch();
if (hasMatch && match)
*match = m;
return hasMatch;
}
/*!
\overload count()
\since 5.0

View File

@ -72,6 +72,7 @@ QT_BEGIN_NAMESPACE
class QCharRef;
class QRegExp;
class QRegularExpression;
class QRegularExpressionMatch;
class QString;
class QStringList;
class QTextCodec;
@ -339,6 +340,7 @@ public:
int indexOf(const QRegularExpression &re, int from = 0) const;
int lastIndexOf(const QRegularExpression &re, int from = -1) const;
bool contains(const QRegularExpression &re) const;
bool contains(const QRegularExpression &re, QRegularExpressionMatch *match) const;
int count(const QRegularExpression &re) const;
#endif

View File

@ -1425,6 +1425,55 @@ void tst_QString::contains()
QVERIFY(a.contains(QRegularExpression("[FG][HI]")));
QVERIFY(a.contains(QRegularExpression("[G][HE]")));
{
QRegularExpressionMatch match;
QVERIFY(!match.hasMatch());
QVERIFY(a.contains(QRegularExpression("[FG][HI]"), &match));
QVERIFY(match.hasMatch());
QCOMPARE(match.capturedStart(), 6);
QCOMPARE(match.capturedEnd(), 8);
QCOMPARE(match.captured(), QStringLiteral("GH"));
QVERIFY(a.contains(QRegularExpression("[G][HE]"), &match));
QVERIFY(match.hasMatch());
QCOMPARE(match.capturedStart(), 6);
QCOMPARE(match.capturedEnd(), 8);
QCOMPARE(match.captured(), QStringLiteral("GH"));
QVERIFY(a.contains(QRegularExpression("[f](.*)[FG]"), &match));
QVERIFY(match.hasMatch());
QCOMPARE(match.capturedStart(), 10);
QCOMPARE(match.capturedEnd(), 15);
QCOMPARE(match.captured(), QString("fGEFG"));
QCOMPARE(match.capturedStart(1), 11);
QCOMPARE(match.capturedEnd(1), 14);
QCOMPARE(match.captured(1), QStringLiteral("GEF"));
QVERIFY(a.contains(QRegularExpression("[f](.*)[F]"), &match));
QVERIFY(match.hasMatch());
QCOMPARE(match.capturedStart(), 10);
QCOMPARE(match.capturedEnd(), 14);
QCOMPARE(match.captured(), QString("fGEF"));
QCOMPARE(match.capturedStart(1), 11);
QCOMPARE(match.capturedEnd(1), 13);
QCOMPARE(match.captured(1), QStringLiteral("GE"));
QVERIFY(!a.contains(QRegularExpression("ZZZ"), &match));
// doesn't match, but ensure match didn't change
QVERIFY(match.hasMatch());
QCOMPARE(match.capturedStart(), 10);
QCOMPARE(match.capturedEnd(), 14);
QCOMPARE(match.captured(), QStringLiteral("fGEF"));
QCOMPARE(match.capturedStart(1), 11);
QCOMPARE(match.capturedEnd(1), 13);
QCOMPARE(match.captured(1), QStringLiteral("GE"));
// don't crash with a null pointer
QVERIFY(a.contains(QRegularExpression("[FG][HI]"), 0));
QVERIFY(!a.contains(QRegularExpression("ZZZ"), 0));
}
CREATE_REF(QLatin1String("FG"));
QVERIFY(a.contains(ref));
QVERIFY(a.contains(ref, Qt::CaseInsensitive));