QString: in remove() use d->erase() for all code paths

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 <thiago.macieira@intel.com>
bb10
Ahmad Samir 2022-10-21 16:53:32 +02:00
parent 6707b881cf
commit 6c7099d27a
1 changed files with 2 additions and 2 deletions

View File

@ -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';