From 996255baae06310b09892f4376fb8b3227ecbb70 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Thu, 12 Nov 2020 16:41:58 +0100 Subject: [PATCH] Fix signature of QArrayDataOps::erase() Bring it in line with the other methods that also take a pointer and a size. Also use truncate() in removeAll() as that's more efficient for the use case. Change-Id: Ib1073b7c048ceb96fb6391b308ef8feb77896866 Reviewed-by: Andrei Golubev Reviewed-by: Thiago Macieira --- src/corelib/text/qbytearray.cpp | 4 +++- src/corelib/text/qstring.cpp | 2 +- src/corelib/tools/qarraydataops.h | 16 ++++++++++------ src/corelib/tools/qlist.h | 6 +++--- .../auto/corelib/tools/qarraydata/simplevector.h | 2 +- .../corelib/tools/qarraydata/tst_qarraydata.cpp | 2 +- 6 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/corelib/text/qbytearray.cpp b/src/corelib/text/qbytearray.cpp index bc2f73a67d0..e1bf2cdc2b8 100644 --- a/src/corelib/text/qbytearray.cpp +++ b/src/corelib/text/qbytearray.cpp @@ -2044,7 +2044,9 @@ QByteArray &QByteArray::remove(qsizetype pos, qsizetype len) if (len <= 0 || pos < 0 || size_t(pos) >= size_t(size())) return *this; detach(); - d->erase(d.begin() + pos, d.begin() + qMin(pos + len, size())); + if (pos + len > d->size) + len = d->size - pos; + d->erase(d.begin() + pos, len); d.data()[d.size] = '\0'; return *this; } diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index 499d8dcade3..4e220c44537 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -2985,7 +2985,7 @@ QString &QString::remove(qsizetype pos, qsizetype len) resize(pos); // truncate } else if (len > 0) { detach(); - d->erase(d.begin() + pos, d.begin() + pos + len); + d->erase(d.begin() + pos, len); d.data()[d.size] = u'\0'; } return *this; diff --git a/src/corelib/tools/qarraydataops.h b/src/corelib/tools/qarraydataops.h index 1efa94ebd12..950b26d1a2d 100644 --- a/src/corelib/tools/qarraydataops.h +++ b/src/corelib/tools/qarraydataops.h @@ -216,8 +216,9 @@ public: new (where) T(std::move(tmp)); } - void erase(T *b, T *e) + void erase(T *b, qsizetype n) { + T *e = b + n; Q_ASSERT(this->isMutable()); Q_ASSERT(b < e); Q_ASSERT(b >= this->begin() && b < this->end()); @@ -231,7 +232,7 @@ public: this->ptr = e; else if (e != this->end()) ::memmove(static_cast(b), static_cast(e), (static_cast(this->end()) - e) * sizeof(T)); - this->size -= (e - b); + this->size -= n; } void eraseFirst() noexcept @@ -592,8 +593,9 @@ public: Inserter(this, pos).insertOne(i, std::move(tmp)); } - void erase(T *b, T *e) + void erase(T *b, qsizetype n) { + T *e = b + n; Q_ASSERT(this->isMutable()); Q_ASSERT(b < e); Q_ASSERT(b >= this->begin() && b < this->end()); @@ -616,7 +618,7 @@ public: ++e; } } - this->size -= (e - b); + this->size -= n; std::destroy(b, e); } @@ -818,8 +820,10 @@ public: Inserter(this, pos).insertOne(i, std::move(tmp)); } - void erase(T *b, T *e) + void erase(T *b, qsizetype n) { + T *e = b + n; + Q_ASSERT(this->isMutable()); Q_ASSERT(b < e); Q_ASSERT(b >= this->begin() && b < this->end()); @@ -836,7 +840,7 @@ public: } else if (e != this->end()) { memmove(static_cast(b), static_cast(e), (static_cast(this->end()) - e)*sizeof(T)); } - this->size -= (e - b); + this->size -= n; } void reallocate(qsizetype alloc, QArrayData::AllocationOption option) diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h index daf9d43fd94..4511e1282a4 100644 --- a/src/corelib/tools/qlist.h +++ b/src/corelib/tools/qlist.h @@ -341,7 +341,7 @@ public: inline reference emplaceFront(Args&&... args); iterator insert(qsizetype i, parameter_type t) - { return insert(i, 1, t); } + { return emplace(i, t); } iterator insert(qsizetype i, qsizetype n, parameter_type t); iterator insert(const_iterator before, parameter_type t) { @@ -448,7 +448,7 @@ public: const AT &tCopy = CopyProxy(t); const iterator e = end(), it = std::remove(begin() + index, e, tCopy); const qsizetype result = std::distance(it, e); - d->erase(it, e); + d->truncate(d->size - result); return result; } template @@ -661,7 +661,7 @@ inline void QList::remove(qsizetype i, qsizetype n) return; d.detach(); - d->erase(d->begin() + i, d->begin() + i + n); + d->erase(d->begin() + i, n); } template diff --git a/tests/auto/corelib/tools/qarraydata/simplevector.h b/tests/auto/corelib/tools/qarraydata/simplevector.h index 1dc9be765cf..74227dd8d0f 100644 --- a/tests/auto/corelib/tools/qarraydata/simplevector.h +++ b/tests/auto/corelib/tools/qarraydata/simplevector.h @@ -274,7 +274,7 @@ public: if (last == end) d->truncate(end - first); else - d->erase(first, last); + d->erase(first, last - first); } void swap(SimpleVector &other) diff --git a/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp b/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp index 94ed26710ff..3dcaba47597 100644 --- a/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp +++ b/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp @@ -1666,7 +1666,7 @@ void tst_QArrayData::arrayOpsExtra() const size_t pos = std::distance(dataPointer.begin(), first); auto copy = cloneArrayDataPointer(dataPointer, dataPointer.size); - dataPointer->erase(first, last); + dataPointer->erase(first, last - first); QCOMPARE(size_t(dataPointer.size), originalSize - distance); size_t i = 0; for (; i < pos; ++i)