diff options
author | Ahmad Samir <a.samirh78@gmail.com> | 2022-10-27 11:34:05 +0200 |
---|---|---|
committer | Thiago Macieira <thiago.macieira@intel.com> | 2022-11-17 02:15:28 +0000 |
commit | d9637d07816af472bd40bb8e6e9f63f1c48085fe (patch) | |
tree | fc3fc7a05fcd452043666522f4b0f458a7d6dc35 /tests/auto | |
parent | d27360818d78adc412c4fb7012c8d5c0b2f38d98 (diff) |
QString: don't detach in removeStringImpl()
- If this string is not shared, modify it directly
- If this string is shared, instead of detaching copy the characters
from this string, except the ones that are going to be removed, to a
new string and swap it. This is more efficient than detaching, which
would copy the whole string including the characters that are going
to be removed.
This affects:
remove(const QString &str, Qt::CaseSensitivity cs)
remove(QLatin1StringView str, Qt::CaseSensitivity cs)
Adjust the unittests to test both code paths.
[ChangeLog][QtCore][QString] Improved the performance of
QString::remove() by avoiding unnecessary data copying. Now, if this
string is (implicitly) shared with another, instead of copying
everything and then removing what we don't want, the characters from
this string are copied to the destination, except the ones that need to
be removed.
Task-number: QTBUG-106181
Change-Id: Id8eba59a44bab641cc8aa662eb45063faf201183
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'tests/auto')
-rw-r--r-- | tests/auto/corelib/text/qstring/tst_qstring.cpp | 20 |
1 files changed, 17 insertions, 3 deletions
diff --git a/tests/auto/corelib/text/qstring/tst_qstring.cpp b/tests/auto/corelib/text/qstring/tst_qstring.cpp index 6a40d511e6..600b97f1bf 100644 --- a/tests/auto/corelib/text/qstring/tst_qstring.cpp +++ b/tests/auto/corelib/text/qstring/tst_qstring.cpp @@ -3381,10 +3381,16 @@ void tst_QString::remove_string() } } + // Test when needsDetach() is true QString s3 = string; s3.remove( before, cs ); QCOMPARE(s3, result); + QString s5 = string; + s5.begin(); // Detach so needsDetach() is false + s5.remove( before, cs ); + QCOMPARE(s5, result); + if (QtPrivate::isLatin1(before)) { QString s6 = string; s6.remove( QLatin1String(before.toLatin1()), cs ); @@ -3440,9 +3446,17 @@ void tst_QString::remove_regexp() void tst_QString::remove_extra() { { - QString s = "The quick brown fox jumps over the lazy dog. " - "The lazy dog jumps over the quick brown fox."; - s.remove(s); + QString quickFox = "The quick brown fox jumps over the lazy dog. " + "The lazy dog jumps over the quick brown fox."; + QString s1 = quickFox; + QVERIFY(s1.data_ptr().needsDetach()); + s1.remove(s1); + QVERIFY(s1.isEmpty()); + QVERIFY(!quickFox.isEmpty()); + + QVERIFY(!quickFox.data_ptr().needsDetach()); + quickFox.remove(quickFox); + QVERIFY(quickFox.isEmpty()); } { |