From 998899cf3a2501d3bf30ad06ce47a2cf81e1d60b Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Sun, 16 Dec 2012 15:05:19 +0100 Subject: Introduce QRegularExpression::NoMatch match type This match type doesn't do any match at all; it's only necessary to properly introduce default constructors for QRegularExpressionMatch and QRegularExpressionMatchIterator (since they return the match type that created them). Change-Id: Ibfe92459c7fdd23129cf3afe073cd443c461ddeb Reviewed-by: Lars Knoll --- src/corelib/tools/qregularexpression.cpp | 17 +++++ src/corelib/tools/qregularexpression.h | 4 +- .../qregularexpression/tst_qregularexpression.cpp | 88 ++++++++++++++++------ 3 files changed, 87 insertions(+), 22 deletions(-) diff --git a/src/corelib/tools/qregularexpression.cpp b/src/corelib/tools/qregularexpression.cpp index 5c6b3ff044..1585389d22 100644 --- a/src/corelib/tools/qregularexpression.cpp +++ b/src/corelib/tools/qregularexpression.cpp @@ -1,6 +1,7 @@ /**************************************************************************** ** ** Copyright (C) 2012 Giuseppe D'Angelo . +** Copyright (C) 2012 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** @@ -725,6 +726,13 @@ QT_BEGIN_NAMESPACE that (in this text) there are other characters beyond the end of the subject string. This can lead to surprising results; see the discussion in the \l{partial matching} section for more details. + + \value NoMatch + No matching is done. This value is returned as the match type by a + default constructed QRegularExpressionMatch or + QRegularExpressionMatchIterator. Using this match type is not very + useful for the user, as no matching ever happens. This enum value + has been introduced in Qt 5.1. */ /*! @@ -1200,6 +1208,15 @@ QRegularExpressionMatchPrivate *QRegularExpressionPrivate::doMatch(const QString return new QRegularExpressionMatchPrivate(re, subject, matchType, matchOptions, 0); } + // skip optimizing and doing the actual matching if NoMatch type was requested + if (matchType == QRegularExpression::NoMatch) { + QRegularExpressionMatchPrivate *priv = new QRegularExpressionMatchPrivate(re, subject, + matchType, matchOptions, + 0); + priv->isValid = true; + return priv; + } + QRegularExpressionMatchPrivate *priv = new QRegularExpressionMatchPrivate(re, subject, matchType, matchOptions, capturingCount); diff --git a/src/corelib/tools/qregularexpression.h b/src/corelib/tools/qregularexpression.h index e312b10ff1..57d03a34e5 100644 --- a/src/corelib/tools/qregularexpression.h +++ b/src/corelib/tools/qregularexpression.h @@ -1,6 +1,7 @@ /**************************************************************************** ** ** Copyright (C) 2012 Giuseppe D'Angelo . +** Copyright (C) 2012 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo ** Contact: http://www.qt-project.org/legal ** ** This file is part of the QtCore module of the Qt Toolkit. @@ -99,7 +100,8 @@ public: enum MatchType { NormalMatch = 0, PartialPreferCompleteMatch, - PartialPreferFirstMatch + PartialPreferFirstMatch, + NoMatch }; enum MatchOption { diff --git a/tests/auto/corelib/tools/qregularexpression/tst_qregularexpression.cpp b/tests/auto/corelib/tools/qregularexpression/tst_qregularexpression.cpp index 8cb43a677d..8b91d1c35d 100644 --- a/tests/auto/corelib/tools/qregularexpression/tst_qregularexpression.cpp +++ b/tests/auto/corelib/tools/qregularexpression/tst_qregularexpression.cpp @@ -127,8 +127,6 @@ bool operator!=(const Match &m, const QRegularExpressionMatch &rem) bool operator==(const QRegularExpressionMatchIterator &iterator, const QList &expectedMatchList) { QRegularExpressionMatchIterator i = iterator; - if (i.isValid() != (!expectedMatchList.isEmpty())) - return false; foreach (const Match &expectedMatch, expectedMatchList) { @@ -694,15 +692,31 @@ void tst_QRegularExpression::normalMatch() QFETCH(QRegularExpression::MatchOptions, matchOptions); QFETCH(Match, match); - QRegularExpressionMatch m = regexp.match(subject, offset, QRegularExpression::NormalMatch, matchOptions); - consistencyCheck(m); - QVERIFY(m == match); - QCOMPARE(m.regularExpression(), regexp); - QCOMPARE(m.matchType(), QRegularExpression::NormalMatch); - QCOMPARE(m.matchOptions(), matchOptions); + { + QRegularExpressionMatch m = regexp.match(subject, offset, QRegularExpression::NormalMatch, matchOptions); + consistencyCheck(m); + QVERIFY(m == match); + QCOMPARE(m.regularExpression(), regexp); + QCOMPARE(m.matchType(), QRegularExpression::NormalMatch); + QCOMPARE(m.matchOptions(), matchOptions); + } + { + // ignore the expected results provided by the match object -- + // we'll never get any result when testing the NoMatch type. + // Just check the validity of the match here. + Match realMatch; + realMatch.clear(); + realMatch.isValid = match.isValid; + + QRegularExpressionMatch m = regexp.match(subject, offset, QRegularExpression::NoMatch, matchOptions); + consistencyCheck(m); + QVERIFY(m == realMatch); + QCOMPARE(m.regularExpression(), regexp); + QCOMPARE(m.matchType(), QRegularExpression::NoMatch); + QCOMPARE(m.matchOptions(), matchOptions); + } } - void tst_QRegularExpression::partialMatch_data() { QTest::addColumn("regexp"); @@ -956,12 +970,29 @@ void tst_QRegularExpression::partialMatch() QFETCH(QRegularExpression::MatchOptions, matchOptions); QFETCH(Match, match); - QRegularExpressionMatch m = regexp.match(subject, offset, matchType, matchOptions); - consistencyCheck(m); - QVERIFY(m == match); - QCOMPARE(m.regularExpression(), regexp); - QCOMPARE(m.matchType(), matchType); - QCOMPARE(m.matchOptions(), matchOptions); + { + QRegularExpressionMatch m = regexp.match(subject, offset, matchType, matchOptions); + consistencyCheck(m); + QVERIFY(m == match); + QCOMPARE(m.regularExpression(), regexp); + QCOMPARE(m.matchType(), matchType); + QCOMPARE(m.matchOptions(), matchOptions); + } + { + // ignore the expected results provided by the match object -- + // we'll never get any result when testing the NoMatch type. + // Just check the validity of the match here. + Match realMatch; + realMatch.clear(); + realMatch.isValid = match.isValid; + + QRegularExpressionMatch m = regexp.match(subject, offset, QRegularExpression::NoMatch, matchOptions); + consistencyCheck(m); + QVERIFY(m == realMatch); + QCOMPARE(m.regularExpression(), regexp); + QCOMPARE(m.matchType(), QRegularExpression::NoMatch); + QCOMPARE(m.matchOptions(), matchOptions); + } } void tst_QRegularExpression::globalMatch_data() @@ -1230,13 +1261,28 @@ void tst_QRegularExpression::globalMatch() QFETCH(QRegularExpression::MatchType, matchType); QFETCH(QRegularExpression::MatchOptions, matchOptions); QFETCH(QList, matchList); + { + QRegularExpressionMatchIterator iterator = regexp.globalMatch(subject, offset, matchType, matchOptions); + consistencyCheck(iterator); + QVERIFY(iterator == matchList); + QCOMPARE(iterator.regularExpression(), regexp); + QCOMPARE(iterator.matchType(), matchType); + QCOMPARE(iterator.matchOptions(), matchOptions); + } + { + // ignore the expected results provided by the match object -- + // we'll never get any result when testing the NoMatch type. + // Just check the validity of the match here. + QList realMatchList; + + QRegularExpressionMatchIterator iterator = regexp.globalMatch(subject, offset, QRegularExpression::NoMatch, matchOptions); + consistencyCheck(iterator); + QVERIFY(iterator == realMatchList); + QCOMPARE(iterator.regularExpression(), regexp); + QCOMPARE(iterator.matchType(), QRegularExpression::NoMatch); + QCOMPARE(iterator.matchOptions(), matchOptions); + } - QRegularExpressionMatchIterator iterator = regexp.globalMatch(subject, offset, matchType, matchOptions); - consistencyCheck(iterator); - QVERIFY(iterator == matchList); - QCOMPARE(iterator.regularExpression(), regexp); - QCOMPARE(iterator.matchType(), matchType); - QCOMPARE(iterator.matchOptions(), matchOptions); } void tst_QRegularExpression::serialize_data() -- cgit v1.2.3