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 <lars.knoll@digia.com>
This commit is contained in:
Giuseppe D'Angelo 2012-12-16 15:05:19 +01:00 committed by The Qt Project
parent bc5a2336ab
commit 998899cf3a
3 changed files with 87 additions and 22 deletions

View File

@ -1,6 +1,7 @@
/**************************************************************************** /****************************************************************************
** **
** Copyright (C) 2012 Giuseppe D'Angelo <dangelog@gmail.com>. ** Copyright (C) 2012 Giuseppe D'Angelo <dangelog@gmail.com>.
** Copyright (C) 2012 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal ** 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 that (in this text) there are other characters beyond the end of the
subject string. This can lead to surprising results; see the discussion subject string. This can lead to surprising results; see the discussion
in the \l{partial matching} section for more details. 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); 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, QRegularExpressionMatchPrivate *priv = new QRegularExpressionMatchPrivate(re, subject,
matchType, matchOptions, matchType, matchOptions,
capturingCount); capturingCount);

View File

@ -1,6 +1,7 @@
/**************************************************************************** /****************************************************************************
** **
** Copyright (C) 2012 Giuseppe D'Angelo <dangelog@gmail.com>. ** Copyright (C) 2012 Giuseppe D'Angelo <dangelog@gmail.com>.
** Copyright (C) 2012 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
** Contact: http://www.qt-project.org/legal ** Contact: http://www.qt-project.org/legal
** **
** This file is part of the QtCore module of the Qt Toolkit. ** This file is part of the QtCore module of the Qt Toolkit.
@ -99,7 +100,8 @@ public:
enum MatchType { enum MatchType {
NormalMatch = 0, NormalMatch = 0,
PartialPreferCompleteMatch, PartialPreferCompleteMatch,
PartialPreferFirstMatch PartialPreferFirstMatch,
NoMatch
}; };
enum MatchOption { enum MatchOption {

View File

@ -127,8 +127,6 @@ bool operator!=(const Match &m, const QRegularExpressionMatch &rem)
bool operator==(const QRegularExpressionMatchIterator &iterator, const QList<Match> &expectedMatchList) bool operator==(const QRegularExpressionMatchIterator &iterator, const QList<Match> &expectedMatchList)
{ {
QRegularExpressionMatchIterator i = iterator; QRegularExpressionMatchIterator i = iterator;
if (i.isValid() != (!expectedMatchList.isEmpty()))
return false;
foreach (const Match &expectedMatch, expectedMatchList) foreach (const Match &expectedMatch, expectedMatchList)
{ {
@ -694,14 +692,30 @@ void tst_QRegularExpression::normalMatch()
QFETCH(QRegularExpression::MatchOptions, matchOptions); QFETCH(QRegularExpression::MatchOptions, matchOptions);
QFETCH(Match, match); QFETCH(Match, match);
QRegularExpressionMatch m = regexp.match(subject, offset, QRegularExpression::NormalMatch, matchOptions); {
consistencyCheck(m); QRegularExpressionMatch m = regexp.match(subject, offset, QRegularExpression::NormalMatch, matchOptions);
QVERIFY(m == match); consistencyCheck(m);
QCOMPARE(m.regularExpression(), regexp); QVERIFY(m == match);
QCOMPARE(m.matchType(), QRegularExpression::NormalMatch); QCOMPARE(m.regularExpression(), regexp);
QCOMPARE(m.matchOptions(), matchOptions); 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() void tst_QRegularExpression::partialMatch_data()
{ {
@ -956,12 +970,29 @@ void tst_QRegularExpression::partialMatch()
QFETCH(QRegularExpression::MatchOptions, matchOptions); QFETCH(QRegularExpression::MatchOptions, matchOptions);
QFETCH(Match, match); QFETCH(Match, match);
QRegularExpressionMatch m = regexp.match(subject, offset, matchType, matchOptions); {
consistencyCheck(m); QRegularExpressionMatch m = regexp.match(subject, offset, matchType, matchOptions);
QVERIFY(m == match); consistencyCheck(m);
QCOMPARE(m.regularExpression(), regexp); QVERIFY(m == match);
QCOMPARE(m.matchType(), matchType); QCOMPARE(m.regularExpression(), regexp);
QCOMPARE(m.matchOptions(), matchOptions); 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() void tst_QRegularExpression::globalMatch_data()
@ -1230,13 +1261,28 @@ void tst_QRegularExpression::globalMatch()
QFETCH(QRegularExpression::MatchType, matchType); QFETCH(QRegularExpression::MatchType, matchType);
QFETCH(QRegularExpression::MatchOptions, matchOptions); QFETCH(QRegularExpression::MatchOptions, matchOptions);
QFETCH(QList<Match>, matchList); QFETCH(QList<Match>, 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<Match> 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() void tst_QRegularExpression::serialize_data()