QRegularExpression: minor fix to captureIndexForName
Although passing a null pointer to pcre16_get_stringnumber for the compiled pattern should simply make it error out, it's actually an undocumented behaviour, so let's stay safe and add an explicit check. Tests for this codepath are added. Change-Id: Ifd9c87874f6812ba487104ec1a5bbc83c3b16761 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
parent
b543141992
commit
824cc94921
@ -1106,6 +1106,9 @@ int QRegularExpressionPrivate::captureIndexForName(const QString &name) const
|
||||
{
|
||||
Q_ASSERT(!name.isEmpty());
|
||||
|
||||
if (!compiledPattern)
|
||||
return -1;
|
||||
|
||||
int index = pcre16_get_stringnumber(compiledPattern, name.utf16());
|
||||
if (index >= 0)
|
||||
return index;
|
||||
|
@ -95,8 +95,13 @@ bool operator==(const QRegularExpressionMatch &rem, const Match &m)
|
||||
}
|
||||
|
||||
Q_FOREACH (const QString &name, m.namedCaptured.keys()) {
|
||||
if (rem.captured(name) != m.namedCaptured.value(name))
|
||||
QString remCaptured = rem.captured(name);
|
||||
QString mCaptured = m.namedCaptured.value(name);
|
||||
if (remCaptured != mCaptured
|
||||
|| remCaptured.isNull() != mCaptured.isNull()
|
||||
|| remCaptured.isEmpty() != mCaptured.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -571,6 +576,32 @@ void tst_QRegularExpression::normalMatch_data()
|
||||
<< QRegularExpression::MatchOptions(QRegularExpression::NoMatchOption)
|
||||
<< m;
|
||||
|
||||
// non existing names for capturing groups
|
||||
m.clear();
|
||||
m.isValid = true; m.hasMatch = true;
|
||||
m.captured << "a string" << "a" << "string";
|
||||
m.namedCaptured["article"] = "a";
|
||||
m.namedCaptured["noun"] = "string";
|
||||
m.namedCaptured["nonexisting1"] = QString();
|
||||
m.namedCaptured["nonexisting2"] = QString();
|
||||
m.namedCaptured["nonexisting3"] = QString();
|
||||
QTest::newRow("match10") << QRegularExpression("(?<article>\\w+) (?<noun>\\w+)")
|
||||
<< "a string"
|
||||
<< 0
|
||||
<< QRegularExpression::MatchOptions(QRegularExpression::NoMatchOption)
|
||||
<< m;
|
||||
|
||||
m.clear();
|
||||
m.isValid = true; m.hasMatch = true;
|
||||
m.captured << "" << "";
|
||||
m.namedCaptured["digits"] = ""; // empty VS null
|
||||
m.namedCaptured["nonexisting"] = QString();
|
||||
QTest::newRow("match11") << QRegularExpression("(?<digits>\\d*)")
|
||||
<< "abcde"
|
||||
<< 0
|
||||
<< QRegularExpression::MatchOptions(QRegularExpression::NoMatchOption)
|
||||
<< m;
|
||||
|
||||
// ***
|
||||
|
||||
m.clear();
|
||||
@ -1223,3 +1254,28 @@ void tst_QRegularExpression::pcreJitStackUsage()
|
||||
consistencyCheck(match);
|
||||
}
|
||||
}
|
||||
|
||||
void tst_QRegularExpression::regularExpressionMatch_data()
|
||||
{
|
||||
QTest::addColumn<QString>("pattern");
|
||||
QTest::addColumn<QString>("subject");
|
||||
|
||||
QTest::newRow("validity01") << "(?<digits>\\d+)" << "1234 abcd";
|
||||
QTest::newRow("validity02") << "(?<digits>\\d+) (?<alpha>\\w+)" << "1234 abcd";
|
||||
}
|
||||
|
||||
void tst_QRegularExpression::regularExpressionMatch()
|
||||
{
|
||||
QFETCH(QString, pattern);
|
||||
QFETCH(QString, subject);
|
||||
|
||||
QRegularExpression re(pattern);
|
||||
QVERIFY(re.isValid());
|
||||
QRegularExpressionMatch match = re.match(subject);
|
||||
consistencyCheck(match);
|
||||
QCOMPARE(match.captured("non-existing").isNull(), true);
|
||||
QTest::ignoreMessage(QtWarningMsg, "QRegularExpressionMatch::captured: empty capturing group name passed");
|
||||
QCOMPARE(match.captured("").isNull(), true);
|
||||
QTest::ignoreMessage(QtWarningMsg, "QRegularExpressionMatch::captured: empty capturing group name passed");
|
||||
QCOMPARE(match.captured(QString()).isNull(), true);
|
||||
}
|
||||
|
@ -73,6 +73,8 @@ private slots:
|
||||
void captureCount();
|
||||
void pcreJitStackUsage_data();
|
||||
void pcreJitStackUsage();
|
||||
void regularExpressionMatch_data();
|
||||
void regularExpressionMatch();
|
||||
|
||||
private:
|
||||
void provideRegularExpressions();
|
||||
|
Loading…
Reference in New Issue
Block a user