summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorAhmad Samir <a.samirh78@gmail.com>2022-11-26 12:50:00 +0200
committerAhmad Samir <a.samirh78@gmail.com>2022-12-11 15:44:26 +0200
commit2fe3b0e5640df34d5512e06ecbc5739c2d4df21e (patch)
tree600624fcc281e5b5d1ade2a153ee6c325c594a12 /src
parentOptimize QXmlStreamWriterPrivate::doWriteToDevice(QStringView) (diff)
downloadqtbase-2fe3b0e5640df34d5512e06ecbc5739c2d4df21e.tar.xz
qtbase-2fe3b0e5640df34d5512e06ecbc5739c2d4df21e.zip
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>
Diffstat (limited to 'src')
-rw-r--r--src/corelib/text/qbytearray.cpp8
-rw-r--r--src/corelib/text/qlocale.cpp2
-rw-r--r--src/corelib/tools/qarraydataops.h5
-rw-r--r--src/corelib/tools/qarraydatapointer.h2
-rw-r--r--src/corelib/tools/qcontainertools_impl.h17
5 files changed, 25 insertions, 9 deletions
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 <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)
{