Skip to content

Commit

Permalink
Fix signature of QArrayDataOps::erase()
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-by: Thiago Macieira <[email protected]>
  • Loading branch information
laknoll committed Nov 17, 2020
1 parent c0e1a38 commit 996255b
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 13 deletions.
4 changes: 3 additions & 1 deletion src/corelib/text/qbytearray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/corelib/text/qstring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 10 additions & 6 deletions src/corelib/tools/qarraydataops.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,9 @@ struct QPodArrayOps
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());
Expand All @@ -231,7 +232,7 @@ struct QPodArrayOps
this->ptr = e;
else if (e != this->end())
::memmove(static_cast<void *>(b), static_cast<void *>(e), (static_cast<T *>(this->end()) - e) * sizeof(T));
this->size -= (e - b);
this->size -= n;
}

void eraseFirst() noexcept
Expand Down Expand Up @@ -592,8 +593,9 @@ struct QGenericArrayOps
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());
Expand All @@ -616,7 +618,7 @@ struct QGenericArrayOps
++e;
}
}
this->size -= (e - b);
this->size -= n;
std::destroy(b, e);
}

Expand Down Expand Up @@ -818,8 +820,10 @@ struct QMovableArrayOps
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());
Expand All @@ -836,7 +840,7 @@ struct QMovableArrayOps
} else if (e != this->end()) {
memmove(static_cast<void *>(b), static_cast<const void *>(e), (static_cast<const T *>(this->end()) - e)*sizeof(T));
}
this->size -= (e - b);
this->size -= n;
}

void reallocate(qsizetype alloc, QArrayData::AllocationOption option)
Expand Down
6 changes: 3 additions & 3 deletions src/corelib/tools/qlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ class QList
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)
{
Expand Down Expand Up @@ -448,7 +448,7 @@ class QList
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 <typename AT = T>
Expand Down Expand Up @@ -661,7 +661,7 @@ inline void QList<T>::remove(qsizetype i, qsizetype n)
return;

d.detach();
d->erase(d->begin() + i, d->begin() + i + n);
d->erase(d->begin() + i, n);
}

template <typename T>
Expand Down
2 changes: 1 addition & 1 deletion tests/auto/corelib/tools/qarraydata/simplevector.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ struct SimpleVector
if (last == end)
d->truncate(end - first);
else
d->erase(first, last);
d->erase(first, last - first);
}

void swap(SimpleVector &other)
Expand Down
2 changes: 1 addition & 1 deletion tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 996255b

Please sign in to comment.