QRegularExpression: print a warning if (?J) is used in a pattern

(?J) inside a pattern string can be used to allow or disallow duplicated
capturing group names in the pattern string itself.
Although PCRE supports duplicated names, in Qt we don't yet.

Change-Id: I21cd0c41273cd7ef42870ced3a0fad6ba7035cbc
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
bb10
Giuseppe D'Angelo 2013-01-30 23:19:08 +00:00 committed by The Qt Project
parent bcd04af4e8
commit d57731b0d7
3 changed files with 42 additions and 0 deletions

View File

@ -1013,6 +1013,14 @@ void QRegularExpressionPrivate::getPatternInfo()
usingCrLfNewlines = (patternNewlineSetting == PCRE_NEWLINE_CRLF) ||
(patternNewlineSetting == PCRE_NEWLINE_ANY) ||
(patternNewlineSetting == PCRE_NEWLINE_ANYCRLF);
int hasJOptionChanged;
pcre16_fullinfo(compiledPattern, 0, PCRE_INFO_JCHANGED, &hasJOptionChanged);
if (hasJOptionChanged) {
qWarning("QRegularExpressionPrivate::getPatternInfo(): the pattern '%s'\n"
" is using the (?J) option; duplicate capturing group names are not supported by Qt",
qPrintable(pattern));
}
}

View File

@ -1550,3 +1550,35 @@ void tst_QRegularExpression::regularExpressionMatch()
QTest::ignoreMessage(QtWarningMsg, "QRegularExpressionMatch::captured: empty capturing group name passed");
QCOMPARE(match.captured(QString()).isNull(), true);
}
void tst_QRegularExpression::JOptionUsage_data()
{
QTest::addColumn<QString>("pattern");
QTest::addColumn<bool>("isValid");
QTest::addColumn<bool>("JOptionUsed");
QTest::newRow("joption-notused-01") << "a.*b" << true << false;
QTest::newRow("joption-notused-02") << "^a(b)(c)$" << true << false;
QTest::newRow("joption-notused-03") << "a(b)(?<c>d)|e" << true << false;
QTest::newRow("joption-notused-04") << "(?<a>.)(?<a>.)" << false << false;
QTest::newRow("joption-used-01") << "(?J)a.*b" << true << true;
QTest::newRow("joption-used-02") << "(?-J)a.*b" << true << true;
QTest::newRow("joption-used-03") << "(?J)(?<a>.)(?<a>.)" << true << true;
QTest::newRow("joption-used-04") << "(?-J)(?<a>.)(?<a>.)" << false << true;
}
void tst_QRegularExpression::JOptionUsage()
{
QFETCH(QString, pattern);
QFETCH(bool, isValid);
QFETCH(bool, JOptionUsed);
const QString warningMessage = QStringLiteral("QRegularExpressionPrivate::getPatternInfo(): the pattern '%1'\n is using the (?J) option; duplicate capturing group names are not supported by Qt");
QRegularExpression re(pattern);
if (isValid && JOptionUsed)
QTest::ignoreMessage(QtWarningMsg, qPrintable(warningMessage.arg(pattern)));
QCOMPARE(re.isValid(), isValid);
}

View File

@ -78,6 +78,8 @@ private slots:
void pcreJitStackUsage();
void regularExpressionMatch_data();
void regularExpressionMatch();
void JOptionUsage_data();
void JOptionUsage();
private:
void provideRegularExpressions();