diff options
author | Ahmad Samir <a.samirh78@gmail.com> | 2022-11-23 08:21:37 +0200 |
---|---|---|
committer | Ahmad Samir <a.samirh78@gmail.com> | 2023-02-09 22:52:01 +0200 |
commit | eb60e940202857dc155f1a0e499364962faad7f6 (patch) | |
tree | bd5869f4e3d0d3e2dec0a6bedc31ce0335f07e87 /tests/auto | |
parent | fc3a9ee60159b2391cba2320cfdeeddb94d781f8 (diff) |
QString: don't detach in replace_helper()
I.e. don't detach in the replace() overloads that delegate to
replace_helper() if this string is shared, instead create a new string
and copy characters from this string to it, along with the "after"
string, then swap it with this.
Do the same thing if "before" is shorter than "after" and there isn't
enough capacity to do the replacement without reallocating.
Use std::copy* and std::move*, which will both fallback to
memmove/memcpy, but they have C++ API, which is more readable.
[ChangeLog][QtCore][QString] Using replace() on a currently shared
QString is now done more efficiently
Task-number: QTBUG-106184
Change-Id: If74ffa1ed47636dc23d543d6dc123d8f2b21d537
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'tests/auto')
-rw-r--r-- | tests/auto/corelib/text/qstring/tst_qstring.cpp | 47 |
1 files changed, 41 insertions, 6 deletions
diff --git a/tests/auto/corelib/text/qstring/tst_qstring.cpp b/tests/auto/corelib/text/qstring/tst_qstring.cpp index ca20e2fe5a..987b3edbf5 100644 --- a/tests/auto/corelib/text/qstring/tst_qstring.cpp +++ b/tests/auto/corelib/text/qstring/tst_qstring.cpp @@ -827,7 +827,14 @@ void tst_QString::replace_qchar_qstring() QFETCH(Qt::CaseSensitivity, cs); QFETCH(QString, expected); - QCOMPARE(src.replace(before, after, cs), expected); + // Test when string needs detach + QString s = src; + QCOMPARE(s.replace(before, after, cs), expected); + + // Test when it's not shared + s = src; + s.detach(); + QCOMPARE(s.replace(before, after, cs), expected); } void tst_QString::replace_uint_uint_data() @@ -3498,18 +3505,46 @@ void tst_QString::replace_uint_uint() QFETCH( int, len ); QFETCH( QString, after ); + // Test when the string is shared QString s1 = string; s1.replace( (uint) index, (int) len, after ); QTEST( s1, "result" ); + // Test when it's not shared + s1 = string; + s1.detach(); + s1.replace((uint)index, (int)len, after); + QTEST(s1, "result"); + // Test when the string is shared QString s2 = string; - s2.replace( (uint) index, (uint) len, after.unicode(), after.size() ); - QTEST( s2, "result" ); + s2.replace((uint)index, (uint)len, after.unicode(), after.size()); + QTEST(s2, "result"); + // Test when it's not shared + s2 = string; + s2.detach(); + s2.replace((uint)index, (uint)len, after.unicode(), after.size()); + QTEST(s2, "result"); - if ( after.size() == 1 ) { + if (after.size() == 1) { + // Test when the string is shared QString s3 = string; - s3.replace( (uint) index, (uint) len, QChar(after[0]) ); - QTEST( s3, "result" ); + s3.replace((uint)index, (uint)len, QChar(after[0])); + QTEST(s3, "result"); + // Test when it's not shared + s3 = string; + s3.detach(); + s3.replace((uint)index, (uint)len, QChar(after[0])); + QTEST(s3, "result"); + + // Test when the string is shared + QString s4 = string; + s4.replace((uint)index, (uint)len, QChar(after[0]).toLatin1()); + QTEST(s4, "result"); + // Test when it's not shared + s4 = string; + s4.detach(); + s4.replace((uint)index, (uint)len, QChar(after[0]).toLatin1()); + QTEST(s4, "result"); } } |