QContainerTools: add q_points_into_range overload

Looking at the use-cases of the already existing q_points_into_range
overload, all of them can be ported to the new one (i.e. all of them
were using range [begin, end)).

Change-Id: I4bfdd68271512b88a9800a16237ff967a367eaeb
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Ahmad Samir 2022-11-26 12:50:00 +02:00
parent fa2153bd10
commit 2fe3b0e564
5 changed files with 25 additions and 9 deletions

View File

@ -2151,7 +2151,7 @@ QByteArray &QByteArray::insert(qsizetype i, QByteArrayView data)
return *this;
}
if (!d->needsDetach() && QtPrivate::q_points_into_range(str, d.data(), d.data() + d.size)) {
if (!d->needsDetach() && QtPrivate::q_points_into_range(str, d)) {
QVarLengthArray a(str, str + size);
return insert(i, a);
}
@ -2327,7 +2327,7 @@ QByteArray &QByteArray::remove(qsizetype pos, qsizetype len)
QByteArray &QByteArray::replace(qsizetype pos, qsizetype len, QByteArrayView after)
{
if (QtPrivate::q_points_into_range(after.data(), d.data(), d.data() + d.size)) {
if (QtPrivate::q_points_into_range(after.data(), d)) {
QVarLengthArray copy(after.data(), after.data() + after.size());
return replace(pos, len, QByteArrayView{copy});
}
@ -2387,11 +2387,11 @@ QByteArray &QByteArray::replace(QByteArrayView before, QByteArrayView after)
return *this;
// protect against before or after being part of this
if (QtPrivate::q_points_into_range(a, d.data(), d.data() + d.size)) {
if (QtPrivate::q_points_into_range(a, d)) {
QVarLengthArray copy(a, a + asize);
return replace(before, QByteArrayView{copy});
}
if (QtPrivate::q_points_into_range(b, d.data(), d.data() + d.size)) {
if (QtPrivate::q_points_into_range(b, d)) {
QVarLengthArray copy(b, b + bsize);
return replace(QByteArrayView{copy}, after);
}

View File

@ -821,7 +821,7 @@ static qsizetype defaultIndex()
#endif
using QtPrivate::q_points_into_range;
Q_ASSERT(q_points_into_range(data, locale_data, std::end(locale_data)));
Q_ASSERT(q_points_into_range(data, locale_data));
return data - locale_data;
}

View File

@ -916,11 +916,10 @@ public:
DataPointer old;
// points into range:
if (QtPrivate::q_points_into_range(b, this->begin(), this->end())) {
if (QtPrivate::q_points_into_range(b, *this))
this->detachAndGrow(QArrayData::GrowsAtEnd, n, &b, &old);
} else {
else
this->detachAndGrow(QArrayData::GrowsAtEnd, n, nullptr, nullptr);
}
Q_ASSERT(this->freeSpaceAtEnd() >= n);
// b might be updated so use [b, n)
this->copyAppend(b, b + n);

View File

@ -300,7 +300,7 @@ public:
T *res = this->ptr + offset;
QtPrivate::q_relocate_overlap_n(this->ptr, this->size, res);
// first update data pointer, then this->ptr
if (data && QtPrivate::q_points_into_range(*data, this->begin(), this->end()))
if (data && QtPrivate::q_points_into_range(*data, *this))
*data += offset;
this->ptr = res;
}

View File

@ -39,6 +39,23 @@ static constexpr bool q_points_into_range(const T *p, const T *b, const T *e,
return !less(p, b) && less(p, e);
}
/*!
\internal
Returns whether \a p is within container \a c. In its simplest form equivalent to:
c.data() <= p < c.data() + c.size()
*/
template <typename C, typename T>
static constexpr bool q_points_into_range(const T &p, const C &c) noexcept
{
static_assert(std::is_same_v<decltype(std::data(c)), T>);
// std::distance because QArrayDataPointer has a "qsizetype size"
// member but no size() function
return q_points_into_range(p, std::data(c),
std::data(c) + std::distance(std::begin(c), std::end(c)));
}
template <typename T, typename N>
void q_uninitialized_move_if_noexcept_n(T* first, N n, T* out)
{