From 6c7099d27a8f944836725ea6048fe472626a1d6a Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Fri, 21 Oct 2022 16:53:32 +0200 Subject: [PATCH] QString: in remove() use d->erase() for all code paths MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use the minimum of "len" and "size() - pos", logically we'll only ever remove characters up-to the end of the array. This eliminates one if branch, and d->erase() can handle all the use-cases. After making this change I looked at QByteArray::remove() and it has similar logic, so it should be correctâ„¢. Change-Id: Ife6c552f8a3f3dda1d5a1da12b80a60790f3bae4 Reviewed-by: Thiago Macieira --- src/corelib/text/qstring.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index c7a601fe5e..d3b72c734d 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -3279,11 +3279,11 @@ QString &QString::remove(qsizetype pos, qsizetype len) { if (pos < 0) // count from end of string pos += size(); + if (size_t(pos) >= size_t(size())) { // range problems - } else if (len >= size() - pos) { - resize(pos); // truncate } else if (len > 0) { + len = std::min(len, size() - pos); detach(); d->erase(d.begin() + pos, len); d.data()[d.size] = u'\0';