QRegularExpression: add overloads matching over a QStringRef

[ChangeLog][QtCore][QRegularExpression] Support for matching
using QStringRef as the subject's string type has been added.

Change-Id: Idb956bbbdf4213f9ebe035db32cd37cf3370c6bc
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
bb10
Giuseppe D'Angelo 2014-08-26 19:01:06 +02:00
parent c714f58085
commit f137618baf
3 changed files with 126 additions and 14 deletions

View File

@ -1721,6 +1721,32 @@ QRegularExpressionMatch QRegularExpression::match(const QString &subject,
return QRegularExpressionMatch(*priv);
}
/*!
\since 5.5
\overload
Attempts to match the regular expression against the given \a subjectRef
string reference, starting at the position \a offset inside the subject, using a
match of type \a matchType and honoring the given \a matchOptions.
The returned QRegularExpressionMatch object contains the results of the
match.
\sa QRegularExpressionMatch, {normal matching}
*/
QRegularExpressionMatch QRegularExpression::match(const QStringRef &subjectRef,
int offset,
MatchType matchType,
MatchOptions matchOptions) const
{
d.data()->compilePattern();
const QString subject = subjectRef.string() ? *subjectRef.string() : QString();
QRegularExpressionMatchPrivate *priv = d->doMatch(subject, subjectRef.position(), subjectRef.length(), offset, matchType, matchOptions);
return QRegularExpressionMatch(*priv);
}
/*!
Attempts to perform a global match of the regular expression against the
given \a subject string, starting at the position \a offset inside the
@ -1746,6 +1772,34 @@ QRegularExpressionMatchIterator QRegularExpression::globalMatch(const QString &s
return QRegularExpressionMatchIterator(*priv);
}
/*!
\since 5.5
\overload
Attempts to perform a global match of the regular expression against the
given \a subjectRef string reference, starting at the position \a offset inside the
subject, using a match of type \a matchType and honoring the given \a
matchOptions.
The returned QRegularExpressionMatchIterator is positioned before the
first match result (if any).
\sa QRegularExpressionMatchIterator, {global matching}
*/
QRegularExpressionMatchIterator QRegularExpression::globalMatch(const QStringRef &subjectRef,
int offset,
MatchType matchType,
MatchOptions matchOptions) const
{
QRegularExpressionMatchIteratorPrivate *priv =
new QRegularExpressionMatchIteratorPrivate(*this,
matchType,
matchOptions,
match(subjectRef, offset, matchType, matchOptions));
return QRegularExpressionMatchIterator(*priv);
}
/*!
\since 5.4

View File

@ -120,11 +120,21 @@ public:
MatchType matchType = NormalMatch,
MatchOptions matchOptions = NoMatchOption) const;
QRegularExpressionMatch match(const QStringRef &subjectRef,
int offset = 0,
MatchType matchType = NormalMatch,
MatchOptions matchOptions = NoMatchOption) const;
QRegularExpressionMatchIterator globalMatch(const QString &subject,
int offset = 0,
MatchType matchType = NormalMatch,
MatchOptions matchOptions = NoMatchOption) const;
QRegularExpressionMatchIterator globalMatch(const QStringRef &subjectRef,
int offset = 0,
MatchType matchType = NormalMatch,
MatchOptions matchOptions = NoMatchOption) const;
void optimize() const;
static QString escape(const QString &str);

View File

@ -257,18 +257,15 @@ static void prepareResultForNoMatchType(Match *m, const Match &orig)
m->isValid = orig.isValid;
}
template<typename QREMatch, typename QREMatchFunc, typename Result>
static void testMatch(const QRegularExpression &regexp,
QREMatchFunc matchingMethod,
const QString &subject,
int offset,
QRegularExpression::MatchType matchType,
QRegularExpression::MatchOptions matchOptions,
const Result &result)
template<typename QREMatch, typename QREMatchFunc, typename Subject, typename Result>
static void testMatchImpl(const QRegularExpression &regexp,
QREMatchFunc matchingMethod,
const Subject &subject,
int offset,
QRegularExpression::MatchType matchType,
QRegularExpression::MatchOptions matchOptions,
const Result &result)
{
if (forceOptimize)
regexp.optimize();
{
const QREMatch m = (regexp.*matchingMethod)(subject, offset, matchType, matchOptions);
consistencyCheck(m);
@ -293,6 +290,36 @@ static void testMatch(const QRegularExpression &regexp,
}
}
template<typename QREMatch, typename QREMatchFuncForString, typename QREMatchFuncForStringRef, typename Result>
static void testMatch(const QRegularExpression &regexp,
QREMatchFuncForString matchingMethodForString,
QREMatchFuncForStringRef matchingMethodForStringRef,
const QString &subject,
int offset,
QRegularExpression::MatchType matchType,
QRegularExpression::MatchOptions matchOptions,
const Result &result)
{
if (forceOptimize)
regexp.optimize();
// test with QString as subject type
testMatchImpl<QREMatch>(regexp, matchingMethodForString, subject, offset, matchType, matchOptions, result);
// test with QStringRef as subject type
testMatchImpl<QREMatch>(regexp,
matchingMethodForStringRef,
QStringRef(&subject, 0, subject.length()),
offset,
matchType,
matchOptions,
result);
}
typedef QRegularExpressionMatch (QRegularExpression::*QREMatchStringPMF)(const QString &, int, QRegularExpression::MatchType, QRegularExpression::MatchOptions) const;
typedef QRegularExpressionMatch (QRegularExpression::*QREMatchStringRefPMF)(const QStringRef &, int, QRegularExpression::MatchType, QRegularExpression::MatchOptions) const;
typedef QRegularExpressionMatchIterator (QRegularExpression::*QREGlobalMatchStringPMF)(const QString &, int, QRegularExpression::MatchType, QRegularExpression::MatchOptions) const;
typedef QRegularExpressionMatchIterator (QRegularExpression::*QREGlobalMatchStringRefPMF)(const QStringRef &, int, QRegularExpression::MatchType, QRegularExpression::MatchOptions) const;
void tst_QRegularExpression::provideRegularExpressions()
{
@ -785,7 +812,14 @@ void tst_QRegularExpression::normalMatch()
QFETCH(QRegularExpression::MatchOptions, matchOptions);
QFETCH(Match, match);
testMatch<QRegularExpressionMatch>(regexp, &QRegularExpression::match, subject, offset, QRegularExpression::NormalMatch, matchOptions, match);
testMatch<QRegularExpressionMatch>(regexp,
static_cast<QREMatchStringPMF>(&QRegularExpression::match),
static_cast<QREMatchStringRefPMF>(&QRegularExpression::match),
subject,
offset,
QRegularExpression::NormalMatch,
matchOptions,
match);
}
void tst_QRegularExpression::partialMatch_data()
@ -1041,7 +1075,14 @@ void tst_QRegularExpression::partialMatch()
QFETCH(QRegularExpression::MatchOptions, matchOptions);
QFETCH(Match, match);
testMatch<QRegularExpressionMatch>(regexp, &QRegularExpression::match, subject, offset, matchType, matchOptions, match);
testMatch<QRegularExpressionMatch>(regexp,
static_cast<QREMatchStringPMF>(&QRegularExpression::match),
static_cast<QREMatchStringRefPMF>(&QRegularExpression::match),
subject,
offset,
matchType,
matchOptions,
match);
}
void tst_QRegularExpression::globalMatch_data()
@ -1311,7 +1352,14 @@ void tst_QRegularExpression::globalMatch()
QFETCH(QRegularExpression::MatchOptions, matchOptions);
QFETCH(QList<Match>, matchList);
testMatch<QRegularExpressionMatchIterator>(regexp, &QRegularExpression::globalMatch, subject, offset, matchType, matchOptions, matchList);
testMatch<QRegularExpressionMatchIterator>(regexp,
static_cast<QREGlobalMatchStringPMF>(&QRegularExpression::globalMatch),
static_cast<QREGlobalMatchStringRefPMF>(&QRegularExpression::globalMatch),
subject,
offset,
matchType,
matchOptions,
matchList);
}
void tst_QRegularExpression::serialize_data()