mirror of https://github.com/qt/qtbase.git
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 Pick-to: 5.15 6.0 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
parent
238f466d49
commit
be83ff65c4
|
@ -4273,13 +4273,13 @@ qsizetype QString::lastIndexOf(const QRegularExpression &re, qsizetype from, QRe
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
qsizetype endpos = (from < 0) ? (size() + from + 1) : (from + 1);
|
qsizetype endpos = (from < 0) ? (size() + from + 1) : (from);
|
||||||
QRegularExpressionMatchIterator iterator = re.globalMatch(*this);
|
QRegularExpressionMatchIterator iterator = re.globalMatch(*this);
|
||||||
qsizetype lastIndex = -1;
|
qsizetype lastIndex = -1;
|
||||||
while (iterator.hasNext()) {
|
while (iterator.hasNext()) {
|
||||||
QRegularExpressionMatch match = iterator.next();
|
QRegularExpressionMatch match = iterator.next();
|
||||||
qsizetype start = match.capturedStart();
|
qsizetype start = match.capturedStart();
|
||||||
if (start < endpos) {
|
if (start <= endpos) {
|
||||||
lastIndex = start;
|
lastIndex = start;
|
||||||
if (rmatch)
|
if (rmatch)
|
||||||
*rmatch = std::move(match);
|
*rmatch = std::move(match);
|
||||||
|
|
|
@ -1645,7 +1645,7 @@ void tst_QString::lastIndexOf()
|
||||||
QCOMPARE(haystack.lastIndexOf(needle.toLatin1(), from, cs), expected);
|
QCOMPARE(haystack.lastIndexOf(needle.toLatin1(), from, cs), expected);
|
||||||
QCOMPARE(haystack.lastIndexOf(needle.toLatin1().data(), 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 QRegularExpression don't have the same out of bound semantics
|
// unfortunately, QString and QRegularExpression don't have the same out of bound semantics
|
||||||
// I think QString is wrong -- See file log for contact information.
|
// I think QString is wrong -- See file log for contact information.
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue