QString: refactor removeStringImpl()

Use std::copy() instead of memmove(), cppreference.com docs mentions that
it will use memmove() internally, I confirmed that by following the code in
gdb, I did see that it uses:.
__builtin_memmove(__result, __first, sizeof(_Tp) * _Num);

in /usr/include/c++/12/bits/stl_algobase.h

std::copy is more readable / easier-to-use API than memmove.

Change-Id: Iccb2fa1dc9897fd6a922ef96bc25308493d39eac
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Ahmad Samir 2022-10-25 14:46:52 +02:00
parent e5f777638b
commit 4c975fd564

View File

@ -3325,21 +3325,19 @@ static void removeStringImpl(QString &s, const T &needle, Qt::CaseSensitivity cs
if (i < 0)
return;
const auto beg = s.begin(); // detaches
auto beg = s.begin(); // detaches
auto dst = beg + i;
auto src = beg + i + needleSize;
const auto end = s.end();
auto end = s.end();
// loop invariant: [beg, dst[ is partial result
// [src, end[ still to be checked for needles
while (src < end) {
const auto i = s.indexOf(needle, src - beg, cs);
const auto hit = i == -1 ? end : beg + i;
const auto skipped = hit - src;
memmove(dst, src, skipped * sizeof(QChar));
dst += skipped;
i = s.indexOf(needle, std::distance(beg, src), cs);
auto hit = i == -1 ? end : beg + i;
dst = std::copy(src, hit, dst);
src = hit + needleSize;
}
s.truncate(dst - beg);
s.truncate(std::distance(beg, dst));
}
/*!