diff options
author | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2020-12-07 23:42:33 +0100 |
---|---|---|
committer | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2020-12-10 23:38:20 +0000 |
commit | 3a273ac47f20e82a1f2f63411b210025ca0f4495 (patch) | |
tree | f80a1c4767753bb6616e619bd7ac96a2d1437615 | |
parent | be0301b42fcfa7ae21fef54f88995f2b0b1c573f (diff) |
QString::lastIndexOf: fix off-by-one for zero length matches
Otherwise, it would report that lastIndexOf of an empty pattern
in an empty string doesn't exist. Next commit adds extensive autotests;
for now, disable a broken autotest (which already features a comment
about why it's broken).
Change-Id: I9a0e5c0142007f81f5cf93e356c8bd82f00066f7
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
(cherry picked from commit be83ff65c424cff1036e7da19d6175826d9f7ed9)
-rw-r--r-- | src/corelib/text/qstring.cpp | 4 | ||||
-rw-r--r-- | tests/auto/corelib/text/qstring/tst_qstring.cpp | 2 |
2 files changed, 3 insertions, 3 deletions
diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index e6e7cbaad4..02a9fe3a30 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -4558,13 +4558,13 @@ int QString::lastIndexOf(const QRegularExpression &re, int from, QRegularExpress return -1; } - int endpos = (from < 0) ? (size() + from + 1) : (from + 1); + int endpos = (from < 0) ? (size() + from + 1) : (from); QRegularExpressionMatchIterator iterator = re.globalMatch(*this); int lastIndex = -1; while (iterator.hasNext()) { QRegularExpressionMatch match = iterator.next(); int start = match.capturedStart(); - if (start < endpos) { + if (start <= endpos) { lastIndex = start; if (rmatch) *rmatch = std::move(match); diff --git a/tests/auto/corelib/text/qstring/tst_qstring.cpp b/tests/auto/corelib/text/qstring/tst_qstring.cpp index 4c4a8f0416..8f53824050 100644 --- a/tests/auto/corelib/text/qstring/tst_qstring.cpp +++ b/tests/auto/corelib/text/qstring/tst_qstring.cpp @@ -1674,7 +1674,7 @@ void tst_QString::lastIndexOf() QCOMPARE(haystack.lastIndexOf(needle.toLatin1(), from, cs), expected); QCOMPARE(haystack.lastIndexOf(needle.toLatin1().data(), from, cs), expected); - if (from >= -1 && from < haystack.size()) { + if (from >= -1 && from < haystack.size() && needle.size() > 0) { // unfortunately, QString and QRegExp don't have the same out of bound semantics // I think QString is wrong -- See file log for contact information. { |