QByteArray: optimize replace(char, char)
Write every byte only once. Amends 83c2c5055405f09043c35b93cd5ba9b9969f3174 (as well as the start of the public history). Pick-to: 6.8 Task-number: QTBUG-106185 Change-Id: I883b2f00c754806882131a09dc3cbc5613420151 Reviewed-by: Anton Kudryavtsev <antkudr@mail.ru> Reviewed-by: Edward Welbourne <edward.welbourne@qt.io> (cherry picked from commit 2753924fcbeb29fa5b34782bd6ad6ed4fe4b51f1) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
parent
0d36941a78
commit
a5bafadf8e
@ -2626,8 +2626,18 @@ QByteArray &QByteArray::replace(char before, char after)
|
||||
{
|
||||
if (before != after) {
|
||||
if (const auto pos = indexOf(before); pos >= 0) {
|
||||
const auto detachedData = data();
|
||||
std::replace(detachedData + pos, detachedData + size(), before, after);
|
||||
if (d.needsDetach()) {
|
||||
QByteArray tmp(size(), Qt::Uninitialized);
|
||||
auto dst = tmp.d.data();
|
||||
dst = std::copy(d.data(), d.data() + pos, dst);
|
||||
*dst++ = after;
|
||||
std::replace_copy(d.data() + pos + 1, d.end(), dst, before, after);
|
||||
swap(tmp);
|
||||
} else {
|
||||
// in-place
|
||||
d.data()[pos] = after;
|
||||
std::replace(d.data() + pos + 1, d.end(), before, after);
|
||||
}
|
||||
}
|
||||
}
|
||||
return *this;
|
||||
|
Loading…
x
Reference in New Issue
Block a user