diff options
author | João Abecasis <joao@trolltech.com> | 2010-01-25 17:27:47 (GMT) |
---|---|---|
committer | Samuli Piippo <samuli.piippo@digia.com> | 2011-06-09 10:05:00 (GMT) |
commit | 02bf486331dc9cdcef46efaed0791c350111a3b3 (patch) | |
tree | e8ffcef680291f7a3553cfbce2768cfb1db77b74 | |
parent | 5cb86915503ff7b97bce1a09db27bea152269ed5 (diff) | |
download | Qt-02bf486331dc9cdcef46efaed0791c350111a3b3.zip Qt-02bf486331dc9cdcef46efaed0791c350111a3b3.tar.gz Qt-02bf486331dc9cdcef46efaed0791c350111a3b3.tar.bz2 |
QRegExp::pos() should return -1 for empty/non-matching captures
Instead, we were returning 0, even if this index did not belong to the
match.
Task-number: QTBUG-7049
Reviewed-by: Volker Hilsheimer
Reviewed-by: Olivier Goffart
(cherry picked from commit dadb99ea2c59d7d0f7a83134b7df5aaaaf80a995)
-rw-r--r-- | src/corelib/tools/qregexp.cpp | 11 | ||||
-rw-r--r-- | tests/auto/qregexp/tst_qregexp.cpp | 93 |
2 files changed, 101 insertions, 3 deletions
diff --git a/src/corelib/tools/qregexp.cpp b/src/corelib/tools/qregexp.cpp index f3374ab..d69719d 100644 --- a/src/corelib/tools/qregexp.cpp +++ b/src/corelib/tools/qregexp.cpp @@ -1466,9 +1466,14 @@ void QRegExpMatchState::match(const QChar *str0, int len0, int pos0, #ifndef QT_NO_REGEXP_CAPTURE for (int i = 0; i < numCaptures; ++i) { int j = eng->captureForOfficialCapture.at(i); - int len = capEnd[j] - capBegin[j]; - *c++ = (len > 0) ? pos + capBegin[j] : 0; - *c++ = len; + if (capBegin[j] != EmptyCapture) { + int len = capEnd[j] - capBegin[j]; + *c++ = (len > 0) ? pos + capBegin[j] : 0; + *c++ = len; + } else { + *c++ = -1; + *c++ = -1; + } } #endif } else { diff --git a/tests/auto/qregexp/tst_qregexp.cpp b/tests/auto/qregexp/tst_qregexp.cpp index 5ffee81..2d75cef 100644 --- a/tests/auto/qregexp/tst_qregexp.cpp +++ b/tests/auto/qregexp/tst_qregexp.cpp @@ -102,6 +102,9 @@ private slots: void reentrancy(); void threadsafeEngineCache(); + + void QTBUG_7049_data(); + void QTBUG_7049(); }; // Testing get/set functions @@ -1333,5 +1336,95 @@ void tst_QRegExp::operator_eq() } } +void tst_QRegExp::QTBUG_7049_data() +{ + QTest::addColumn<QString>("reStr"); + QTest::addColumn<QString>("text"); + QTest::addColumn<int>("matchIndex"); + + QTest::addColumn<int>("pos0"); + QTest::addColumn<int>("pos1"); + QTest::addColumn<int>("pos2"); + + QTest::addColumn<QString>("cap0"); + QTest::addColumn<QString>("cap1"); + QTest::addColumn<QString>("cap2"); + + QTest::newRow("no match") + << QString("(a) (b)") << QString("b a") << -1 + << -1 << -1 << -1 << QString() << QString() << QString(); + + QTest::newRow("both captures match") + << QString("(a) (b)") << QString("a b") << 0 + << 0 << 0 << 2 << QString("a b") << QString("a") << QString("b"); + + QTest::newRow("first capture matches @0") + << QString("(a*)|(b*)") << QString("axx") << 0 + << 0 << 0 << -1 << QString("a") << QString("a") << QString(); + QTest::newRow("second capture matches @0") + << QString("(a*)|(b*)") << QString("bxx") << 0 + << 0 << -1 << 0 << QString("b") << QString() << QString("b"); + QTest::newRow("first capture empty match @0") + << QString("(a*)|(b*)") << QString("xx") << 0 + << 0 << -1 << -1 << QString("") << QString() << QString(); + QTest::newRow("second capture empty match @0") + << QString("(a)|(b*)") << QString("xx") << 0 + << 0 << -1 << -1 << QString("") << QString() << QString(); + + QTest::newRow("first capture matches @1") + << QString("x(?:(a*)|(b*))") << QString("-xa") << 1 + << 1 << 2 << -1 << QString("xa") << QString("a") << QString(); + QTest::newRow("second capture matches @1") + << QString("x(?:(a*)|(b*))") << QString("-xb") << 1 + << 1 << -1 << 2 << QString("xb") << QString() << QString("b"); + QTest::newRow("first capture empty match @1") + << QString("x(?:(a*)|(b*))") << QString("-xx") << 1 + << 1 << -1 << -1 << QString("x") << QString() << QString(); + QTest::newRow("second capture empty match @1") + << QString("x(?:(a)|(b*))") << QString("-xx") << 1 + << 1 << -1 << -1 << QString("x") << QString() << QString(); + + QTest::newRow("first capture matches @2") + << QString("(a)|(b)") << QString("xxa") << 2 + << 2 << 2 << -1 << QString("a") << QString("a") << QString(); + QTest::newRow("second capture matches @2") + << QString("(a)|(b)") << QString("xxb") << 2 + << 2 << -1 << 2 << QString("b") << QString() << QString("b"); + QTest::newRow("no match - with options") + << QString("(a)|(b)") << QString("xx") << -1 + << -1 << -1 << -1 << QString() << QString() << QString(); + +} + +void tst_QRegExp::QTBUG_7049() +{ + QFETCH( QString, reStr ); + QFETCH( QString, text ); + QFETCH( int, matchIndex ); + QFETCH( int, pos0 ); + QFETCH( int, pos1 ); + QFETCH( int, pos2 ); + QFETCH( QString, cap0 ); + QFETCH( QString, cap1 ); + QFETCH( QString, cap2 ); + + QRegExp re(reStr); + QCOMPARE(re.numCaptures(), 2); + QCOMPARE(re.capturedTexts().size(), 3); + + QCOMPARE(re.indexIn(text), matchIndex); + + QCOMPARE( re.pos(0), pos0 ); + QCOMPARE( re.pos(1), pos1 ); + QCOMPARE( re.pos(2), pos2 ); + + QCOMPARE( re.cap(0).isNull(), cap0.isNull() ); + QCOMPARE( re.cap(0), cap0 ); + QCOMPARE( re.cap(1).isNull(), cap1.isNull() ); + QCOMPARE( re.cap(1), cap1 ); + QCOMPARE( re.cap(2).isNull(), cap2.isNull() ); + QCOMPARE( re.cap(2), cap2 ); +} + QTEST_APPLESS_MAIN(tst_QRegExp) #include "tst_qregexp.moc" |