From 4c975fd564e3b1f5c398f6ffe6b4b87ff6f95379 Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Tue, 25 Oct 2022 14:46:52 +0200 Subject: [PATCH] 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 --- src/corelib/text/qstring.cpp | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index 707b13082a..fe5f8ae5f1 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -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)); } /*!