summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/text
diff options
context:
space:
mode:
authorGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2020-08-24 11:45:23 +0200
committerGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2020-08-25 23:09:09 +0000
commit049d8892eaa18d71d6edb10752418ad33305f310 (patch)
tree3cb104959f45385919d3fd79e83b739188502b56 /tests/auto/corelib/text
parentacbf9a858b6b389103b7f43f4f4892a142ec56c6 (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. Pick-to: 5.15 Change-Id: I25ca14de49b13ee1764525f8b19f2550c30c1afa Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'tests/auto/corelib/text')
-rw-r--r--tests/auto/corelib/text/qregularexpression/tst_qregularexpression.cpp31
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 ab93373d44..dbe77be1f5 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();
@@ -1620,6 +1621,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");