summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndreas Buhr <andreas.buhr@qt.io>2020-11-19 14:26:18 +0100
committerAndreas Buhr <andreas.buhr@qt.io>2020-11-20 19:14:09 +0100
commitea6a76aa17c0282f079a6e8e69d2416d19daa0fa (patch)
tree22de0620c721c235307ea98ed7e1b5e51b7671f5
parentb0096facab4fb6b04729bf2c95d170cebfaef31b (diff)
Fix logic error in QString::replace(ch, after, cs)
Coverage analysis showed that an if-branch marked "Q_LIKELY" was never taken. It turns out the code was incorrect, but behaved correctly. This patch fixes the logic and adds a unit test. Change-Id: I9b4ba76392b52f07b8e21188496e23f98dba95a9 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> (cherry picked from commit ebaae45ea17efc230209ed90d94596647cf6cb48) Reviewed-by: Sona Kurazyan <sona.kurazyan@qt.io>
-rw-r--r--src/corelib/text/qstring.cpp2
-rw-r--r--tests/auto/corelib/text/qstring/tst_qstring.cpp11
2 files changed, 12 insertions, 1 deletions
diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp
index f9ec7678fe..f119fb920f 100644
--- a/src/corelib/text/qstring.cpp
+++ b/src/corelib/text/qstring.cpp
@@ -3296,7 +3296,7 @@ QString& QString::replace(QChar ch, const QString &after, Qt::CaseSensitivity cs
replace_helper(indices, pos, 1, after.constData(), after.d->size);
- if (Q_LIKELY(index == -1)) // Nothing left to replace
+ if (Q_LIKELY(index == size())) // Nothing left to replace
break;
// The call to replace_helper just moved what index points at:
index += pos*(after.d->size - 1);
diff --git a/tests/auto/corelib/text/qstring/tst_qstring.cpp b/tests/auto/corelib/text/qstring/tst_qstring.cpp
index e8e578ac25..ce091f6688 100644
--- a/tests/auto/corelib/text/qstring/tst_qstring.cpp
+++ b/tests/auto/corelib/text/qstring/tst_qstring.cpp
@@ -2966,6 +2966,17 @@ void tst_QString::replace_extra()
// Also check the full values match, of course:
QCOMPARE(str8.size(), ans8.size());
QCOMPARE(str8, ans8);
+
+ {
+ QString s(QLatin1String("BBB"));
+ QString expected(QLatin1String("BBB"));
+ for (int i = 0; i < 1028; ++i) {
+ s.append("X");
+ expected.append("GXU");
+ }
+ s.replace(QChar('X'), "GXU");
+ QCOMPARE(s, expected);
+ }
}
void tst_QString::replace_string()