Revert "QString::replace: fix a performance regression"

This reverts commit 315210de916d060c044c01e53ff249d676122b1b.

The change was not correct. If the newly-inserted reserve() call
actually grows the string, and `after` aliased a part of the old
string, it will now reference deleted data, as the q_points_into_range
check now comes too late.

Change-Id: I2e016b8b90f74126dfc7126800b7b7fde96a091e
Pick-to: 6.8 6.7
Task-number: QTBUG-127549
Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
(cherry picked from commit d1539331dd813c83916a323f6cdc7fd6aacb0043)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2025-02-26 09:57:48 +00:00 committed by Qt Cherry-pick Bot
parent f08721bb39
commit 887c7a4b89

View File

@ -3755,13 +3755,11 @@ static void replace_helper(QString &str, QSpan<size_t> indices, qsizetype blen,
const qsizetype oldSize = str.data_ptr().size;
const qsizetype adjust = indices.size() * (after.size() - blen);
const qsizetype newSize = oldSize + adjust;
if (str.data_ptr().needsDetach()) {
if (str.data_ptr().needsDetach() || needsReallocate(str, newSize)) {
replace_with_copy(str, indices, blen, after);
return;
}
str.reserve(newSize);
if (QtPrivate::q_points_into_range(after.begin(), str))
// Copy after if it lies inside our own d.b area (which we could
// possibly invalidate via a realloc or modify by replacement)