Skip to content

Commit

Permalink
Add q_points_into_range to container utilities
Browse files Browse the repository at this point in the history
We already used it in QString and QBA. And implicitly in QADP (see
parent commit). Might as well move to a common location and reuse

Pick-to: dev 6.0 6.1
Change-Id: I694f0f1dbd109f17c134f64b3f3dc28d19556c88
Reviewed-by: Lars Knoll <[email protected]>
  • Loading branch information
andrey-golubev committed Apr 26, 2021
1 parent d119ccb commit 10b46e7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
14 changes: 4 additions & 10 deletions src/corelib/text/qbytearray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,6 @@

QT_BEGIN_NAMESPACE

template <typename T, typename Cmp = std::less<>>
static constexpr bool points_into_range(const T *p, const T *b, const T *e, Cmp less = {}) noexcept
{
return !less(p, b) && less(p, e);
}

const char QByteArray::_empty = '\0';

// ASCII case system, used by QByteArray::to{Upper,Lower}() and qstr(n)icmp():
Expand Down Expand Up @@ -2030,7 +2024,7 @@ QByteArray &QByteArray::insert(qsizetype i, QByteArrayView data)
return *this;
}

if (!d->needsDetach() && points_into_range(str, d.data(), d.data() + d.size)) {
if (!d->needsDetach() && QtPrivate::q_points_into_range(str, d.data(), d.data() + d.size)) {
QVarLengthArray a(str, str + size);
return insert(i, a);
}
Expand Down Expand Up @@ -2169,7 +2163,7 @@ QByteArray &QByteArray::remove(qsizetype pos, qsizetype len)

QByteArray &QByteArray::replace(qsizetype pos, qsizetype len, QByteArrayView after)
{
if (points_into_range(after.data(), d.data(), d.data() + d.size)) {
if (QtPrivate::q_points_into_range(after.data(), d.data(), d.data() + d.size)) {
QVarLengthArray copy(after.data(), after.data() + after.size());
return replace(pos, len, QByteArrayView{copy});
}
Expand Down Expand Up @@ -2226,11 +2220,11 @@ QByteArray &QByteArray::replace(QByteArrayView before, QByteArrayView after)
return *this;

// protect against before or after being part of this
if (points_into_range(a, d.data(), d.data() + d.size)) {
if (QtPrivate::q_points_into_range(a, d.data(), d.data() + d.size)) {
QVarLengthArray copy(a, a + asize);
return replace(before, QByteArrayView{copy});
}
if (points_into_range(b, d.data(), d.data() + d.size)) {
if (QtPrivate::q_points_into_range(b, d.data(), d.data() + d.size)) {
QVarLengthArray copy(b, b + bsize);
return replace(QByteArrayView{copy}, after);
}
Expand Down
10 changes: 2 additions & 8 deletions src/corelib/text/qstring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,6 @@

QT_BEGIN_NAMESPACE

template <typename T, typename Cmp = std::less<>>
static constexpr bool points_into_range(const T *p, const T *b, const T *e, Cmp less = {}) noexcept
{
return !less(p, b) && less(p, e);
}

const char16_t QString::_empty = 0;

/*
Expand Down Expand Up @@ -2824,7 +2818,7 @@ QString& QString::insert(qsizetype i, const QChar *unicode, qsizetype size)
return *this;
}

if (!d->needsDetach() && points_into_range(s, d.data(), d.data() + d.size))
if (!d->needsDetach() && QtPrivate::q_points_into_range(s, d.data(), d.data() + d.size))
return insert(i, QStringView{QVarLengthArray(s, s + size)});

d->insert(i, s, size);
Expand Down Expand Up @@ -3118,7 +3112,7 @@ static void removeStringImpl(QString &s, const T &needle, Qt::CaseSensitivity cs
QString &QString::remove(const QString &str, Qt::CaseSensitivity cs)
{
const auto s = str.d.data();
if (points_into_range(s, d.data(), d.data() + d.size))
if (QtPrivate::q_points_into_range(s, d.data(), d.data() + d.size))
removeStringImpl(*this, QStringView{QVarLengthArray(s, s + str.size())}, cs);
else
removeStringImpl(*this, qToStringViewIgnoringNull(str), cs);
Expand Down
13 changes: 13 additions & 0 deletions src/corelib/tools/qcontainertools_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ QT_BEGIN_NAMESPACE
namespace QtPrivate
{

/*!
\internal
Returns whether \a p is within a range [b, e). In simplest form equivalent to:
b <= p < e.
*/
template<typename T, typename Cmp = std::less<>>
static constexpr bool q_points_into_range(const T *p, const T *b, const T *e,
Cmp less = {}) noexcept
{
return !less(p, b) && less(p, e);
}

template <typename T, typename N>
void q_uninitialized_relocate_n(T* first, N n, T* out)
{
Expand Down

0 comments on commit 10b46e7

Please sign in to comment.