diff options
author | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2020-08-24 11:45:23 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2020-08-26 05:11:58 +0000 |
commit | b470abaca9938a6c36efa86de9c8c27cd86b4836 (patch) | |
tree | 81d6476be68d8ac19b216d7236b769050f0716b6 /tests/auto/corelib | |
parent | f7857d7f867ae5be0f910f802daaf4cec69c8a58 (diff) |
QRegularExpression: do not assume QStringViews are NUL terminated
The convenience API used to look up the index of a named capturing
group expects NUL terminated strings. Therefore, we can't just
use it together with QStringViews, which may be not. Use the
non-convenience API instead.
Change-Id: I25ca14de49b13ee1764525f8b19f2550c30c1afa
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
(cherry picked from commit 049d8892eaa18d71d6edb10752418ad33305f310)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'tests/auto/corelib')
-rw-r--r-- | tests/auto/corelib/text/qregularexpression/tst_qregularexpression.cpp | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/tests/auto/corelib/text/qregularexpression/tst_qregularexpression.cpp b/tests/auto/corelib/text/qregularexpression/tst_qregularexpression.cpp index 83f9c17a42..88f8ea0bbb 100644 --- a/tests/auto/corelib/text/qregularexpression/tst_qregularexpression.cpp +++ b/tests/auto/corelib/text/qregularexpression/tst_qregularexpression.cpp @@ -69,6 +69,7 @@ private slots: void captureCount(); void captureNames_data(); void captureNames(); + void captureNamesNul(); void pcreJitStackUsage_data(); void pcreJitStackUsage(); void regularExpressionMatch_data(); @@ -1623,6 +1624,36 @@ void tst_QRegularExpression::captureNames() } +void tst_QRegularExpression::captureNamesNul() +{ + QRegularExpression re("a(\\d+)b(?<name>\\d+)c(?<anotherName>\\d+)d(\\d+)e$"); + QVERIFY(re.isValid()); + + QCOMPARE(re.captureCount(), 4); + + QStringList namedCaptureGroups = re.namedCaptureGroups(); + QCOMPARE(namedCaptureGroups[0], QString()); + QCOMPARE(namedCaptureGroups[1], QString()); + QCOMPARE(namedCaptureGroups[2], "name"); + QCOMPARE(namedCaptureGroups[3], "anotherName"); + QCOMPARE(namedCaptureGroups[4], QString()); + + QRegularExpressionMatch m = re.match("a12b456c789d0e"); + QVERIFY(m.hasMatch()); + + QString captureName("name"); + QCOMPARE(m.captured(captureName), "456"); + QCOMPARE(m.captured(QStringView(captureName)), "456"); + QCOMPARE(m.captured(qToStringViewIgnoringNull(captureName)), "456"); + QCOMPARE(m.captured(u"name"), "456"); + + captureName = "anotherName"; + QCOMPARE(m.captured(captureName), "789"); + QCOMPARE(m.captured(QStringView(captureName)), "789"); + QCOMPARE(m.captured(qToStringViewIgnoringNull(captureName)), "789"); + QCOMPARE(m.captured(u"anotherName"), "789"); +} + void tst_QRegularExpression::pcreJitStackUsage_data() { QTest::addColumn<QString>("pattern"); |