diff --git a/src/corelib/text/qbytearray.cpp b/src/corelib/text/qbytearray.cpp index 9eabf22600..f4b4523b68 100644 --- a/src/corelib/text/qbytearray.cpp +++ b/src/corelib/text/qbytearray.cpp @@ -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); } diff --git a/src/corelib/text/qlocale.cpp b/src/corelib/text/qlocale.cpp index 23534cbb58..cf79ed901f 100644 --- a/src/corelib/text/qlocale.cpp +++ b/src/corelib/text/qlocale.cpp @@ -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; } diff --git a/src/corelib/tools/qarraydataops.h b/src/corelib/tools/qarraydataops.h index 13d5572c03..d50c74f2c2 100644 --- a/src/corelib/tools/qarraydataops.h +++ b/src/corelib/tools/qarraydataops.h @@ -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); diff --git a/src/corelib/tools/qarraydatapointer.h b/src/corelib/tools/qarraydatapointer.h index 3e1c2c11e4..f0b689b499 100644 --- a/src/corelib/tools/qarraydatapointer.h +++ b/src/corelib/tools/qarraydatapointer.h @@ -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; } diff --git a/src/corelib/tools/qcontainertools_impl.h b/src/corelib/tools/qcontainertools_impl.h index 9b0bd2ad6b..22f5cdf336 100644 --- a/src/corelib/tools/qcontainertools_impl.h +++ b/src/corelib/tools/qcontainertools_impl.h @@ -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 +static constexpr bool q_points_into_range(const T &p, const C &c) noexcept +{ + static_assert(std::is_same_v); + + // 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 void q_uninitialized_move_if_noexcept_n(T* first, N n, T* out) {