Add a verify() method to all sequential containers

A helper method encasuplating the asserts related to index into the
container and length, modelled after the QVLA::verify().

`pos <= size` is OK because if pos == size, the e.g. sliced()'ed
container is just going to be empty.

Normalize how verify() is used, the first arg is an index and the second
a length.

This method is constexpr even in QString/QByteArray merely for
consistency with similar methods in other string classes (this
necessitates using `d.size` in verify() in QString/QBA because size()
isn't constexpr).

Change-Id: I90e3c56d76c802259297a06d11d46ee342a1daf2
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Ahmad Samir 2023-07-06 15:05:08 +03:00
parent 016addc201
commit 2fd9735e7a
8 changed files with 105 additions and 73 deletions

View File

@ -250,20 +250,20 @@ public:
}
[[nodiscard]] constexpr QAnyStringView sliced(qsizetype pos) const
{ verify(pos); auto r = *this; r.advanceData(pos); r.setSize(size() - pos); return r; }
{ verify(pos, 0); auto r = *this; r.advanceData(pos); r.setSize(size() - pos); return r; }
[[nodiscard]] constexpr QAnyStringView sliced(qsizetype pos, qsizetype n) const
{ verify(pos, n); auto r = *this; r.advanceData(pos); r.setSize(n); return r; }
[[nodiscard]] constexpr QAnyStringView first(qsizetype n) const
{ verify(n); return sliced(0, n); }
{ verify(0, n); return sliced(0, n); }
[[nodiscard]] constexpr QAnyStringView last(qsizetype n) const
{ verify(n); return sliced(size() - n, n); }
{ verify(0, n); return sliced(size() - n, n); }
[[nodiscard]] constexpr QAnyStringView chopped(qsizetype n) const
{ verify(n); return sliced(0, size() - n); }
{ verify(0, n); return sliced(0, size() - n); }
constexpr void truncate(qsizetype n)
{ verify(n); setSize(n); }
{ verify(0, n); setSize(n); }
constexpr void chop(qsizetype n)
{ verify(n); setSize(size() - n); }
{ verify(0, n); setSize(size() - n); }
[[nodiscard]] inline QString toString() const; // defined in qstring.h
@ -329,7 +329,8 @@ private:
constexpr void setSize(qsizetype sz) noexcept { m_size = size_t(sz) | tag(); }
constexpr void advanceData(qsizetype delta) noexcept
{ m_data_utf8 += delta * charSize(); }
Q_ALWAYS_INLINE constexpr void verify(qsizetype pos, qsizetype n = 0) const
Q_ALWAYS_INLINE constexpr void verify([[maybe_unused]] qsizetype pos = 0,
[[maybe_unused]] qsizetype n = 1) const
{
Q_ASSERT(pos >= 0);
Q_ASSERT(pos <= size());

View File

@ -157,15 +157,15 @@ public:
[[nodiscard]] QByteArray mid(qsizetype index, qsizetype len = -1) const;
[[nodiscard]] QByteArray first(qsizetype n) const
{ Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); return QByteArray(data(), n); }
{ verify(0, n); return QByteArray(data(), n); }
[[nodiscard]] QByteArray last(qsizetype n) const
{ Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); return QByteArray(data() + size() - n, n); }
{ verify(0, n); return QByteArray(data() + size() - n, n); }
[[nodiscard]] QByteArray sliced(qsizetype pos) const
{ Q_ASSERT(pos >= 0); Q_ASSERT(pos <= size()); return QByteArray(data() + pos, size() - pos); }
{ verify(pos, 0); return QByteArray(data() + pos, size() - pos); }
[[nodiscard]] QByteArray sliced(qsizetype pos, qsizetype n) const
{ Q_ASSERT(pos >= 0); Q_ASSERT(n >= 0); Q_ASSERT(size_t(pos) + size_t(n) <= size_t(size())); return QByteArray(data() + pos, n); }
{ verify(pos, n); return QByteArray(data() + pos, n); }
[[nodiscard]] QByteArray chopped(qsizetype len) const
{ Q_ASSERT(len >= 0); Q_ASSERT(len <= size()); return first(size() - len); }
{ verify(0, len); return first(size() - len); }
bool startsWith(QByteArrayView bv) const
{ return QtPrivate::startsWith(qToByteArrayViewIgnoringNull(*this), bv); }
@ -491,6 +491,15 @@ private:
void reallocGrowData(qsizetype n);
void expand(qsizetype i);
Q_ALWAYS_INLINE constexpr void verify([[maybe_unused]] qsizetype pos = 0,
[[maybe_unused]] qsizetype n = 1) const
{
Q_ASSERT(pos >= 0);
Q_ASSERT(pos <= d.size);
Q_ASSERT(n >= 0);
Q_ASSERT(n <= d.size - pos);
}
static QByteArray toLower_helper(const QByteArray &a);
static QByteArray toLower_helper(QByteArray &a);
static QByteArray toUpper_helper(const QByteArray &a);
@ -521,9 +530,9 @@ inline constexpr QByteArray::QByteArray() noexcept {}
inline QByteArray::~QByteArray() {}
inline char QByteArray::at(qsizetype i) const
{ Q_ASSERT(size_t(i) < size_t(size())); return d.data()[i]; }
{ verify(i, 1); return d.data()[i]; }
inline char QByteArray::operator[](qsizetype i) const
{ Q_ASSERT(size_t(i) < size_t(size())); return d.data()[i]; }
{ verify(i, 1); return d.data()[i]; }
#ifndef QT_NO_CAST_FROM_BYTEARRAY
inline QByteArray::operator const char *() const
@ -573,7 +582,7 @@ inline void QByteArray::squeeze()
}
inline char &QByteArray::operator[](qsizetype i)
{ Q_ASSERT(i >= 0 && i < size()); return data()[i]; }
{ verify(i, 1); return data()[i]; }
inline char &QByteArray::front() { return operator[](0); }
inline char &QByteArray::back() { return operator[](size() - 1); }
inline QByteArray &QByteArray::append(qsizetype n, char ch)

View File

@ -180,7 +180,7 @@ public:
[[nodiscard]] constexpr const_pointer constData() const noexcept { return data(); }
[[nodiscard]] constexpr char operator[](qsizetype n) const
{ Q_ASSERT(n >= 0); Q_ASSERT(n < size()); return m_data[n]; }
{ verify(n, 1); return m_data[n]; }
//
// QByteArray API
@ -188,15 +188,15 @@ public:
[[nodiscard]] constexpr char at(qsizetype n) const { return (*this)[n]; }
[[nodiscard]] constexpr QByteArrayView first(qsizetype n) const
{ Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); return QByteArrayView(data(), n); }
{ verify(0, n); return QByteArrayView(data(), n); }
[[nodiscard]] constexpr QByteArrayView last(qsizetype n) const
{ Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); return QByteArrayView(data() + size() - n, n); }
{ verify(0, n); return QByteArrayView(data() + size() - n, n); }
[[nodiscard]] constexpr QByteArrayView sliced(qsizetype pos) const
{ Q_ASSERT(pos >= 0); Q_ASSERT(pos <= size()); return QByteArrayView(data() + pos, size() - pos); }
{ verify(pos, 0); return QByteArrayView(data() + pos, size() - pos); }
[[nodiscard]] constexpr QByteArrayView sliced(qsizetype pos, qsizetype n) const
{ Q_ASSERT(pos >= 0); Q_ASSERT(n >= 0); Q_ASSERT(size_t(pos) + size_t(n) <= size_t(size())); return QByteArrayView(data() + pos, n); }
{ verify(pos, n); return QByteArrayView(data() + pos, n); }
[[nodiscard]] constexpr QByteArrayView chopped(qsizetype len) const
{ Q_ASSERT(len >= 0); Q_ASSERT(len <= size()); return first(size() - len); }
{ verify(0, len); return first(size() - len); }
[[nodiscard]] constexpr QByteArrayView left(qsizetype n) const
{ if (n < 0 || n > size()) n = size(); return QByteArrayView(data(), n); }
@ -211,9 +211,9 @@ public:
}
constexpr void truncate(qsizetype n)
{ Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); m_size = n; }
{ verify(0, n); m_size = n; }
constexpr void chop(qsizetype n)
{ Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); m_size -= n; }
{ verify(0, n); m_size -= n; }
// Defined in qbytearray.cpp:
[[nodiscard]] QByteArrayView trimmed() const noexcept
@ -328,6 +328,15 @@ public:
{ return !(lhs < rhs); }
private:
Q_ALWAYS_INLINE constexpr void verify([[maybe_unused]] qsizetype pos = 0,
[[maybe_unused]] qsizetype n = 1) const
{
Q_ASSERT(pos >= 0);
Q_ASSERT(pos <= size());
Q_ASSERT(n >= 0);
Q_ASSERT(n <= size() - pos);
}
qsizetype m_size;
const storage_type *m_data;
};

View File

@ -223,20 +223,20 @@ public:
}
[[nodiscard]] constexpr QLatin1StringView sliced(qsizetype pos) const
{ verify(pos); return {m_data + pos, m_size - pos}; }
{ verify(pos, 0); return {m_data + pos, m_size - pos}; }
[[nodiscard]] constexpr QLatin1StringView sliced(qsizetype pos, qsizetype n) const
{ verify(pos, n); return {m_data + pos, n}; }
[[nodiscard]] constexpr QLatin1StringView first(qsizetype n) const
{ verify(n); return {m_data, n}; }
{ verify(0, n); return {m_data, n}; }
[[nodiscard]] constexpr QLatin1StringView last(qsizetype n) const
{ verify(n); return {m_data + size() - n, n}; }
{ verify(0, n); return {m_data + size() - n, n}; }
[[nodiscard]] constexpr QLatin1StringView chopped(qsizetype n) const
{ verify(n); return {m_data, size() - n}; }
{ verify(0, n); return {m_data, size() - n}; }
constexpr void chop(qsizetype n)
{ verify(n); m_size -= n; }
{ verify(0, n); m_size -= n; }
constexpr void truncate(qsizetype n)
{ verify(n); m_size = n; }
{ verify(0, n); m_size = n; }
[[nodiscard]] QLatin1StringView trimmed() const noexcept { return QtPrivate::trimmed(*this); }
@ -321,7 +321,8 @@ public:
#endif // !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
private:
Q_ALWAYS_INLINE constexpr void verify(qsizetype pos, qsizetype n = 0) const
Q_ALWAYS_INLINE constexpr void verify([[maybe_unused]] qsizetype pos,
[[maybe_unused]] qsizetype n = 1) const
{
Q_ASSERT(pos >= 0);
Q_ASSERT(pos <= size());

View File

@ -336,16 +336,15 @@ public:
[[nodiscard]] QString mid(qsizetype position, qsizetype n = -1) const;
[[nodiscard]] QString first(qsizetype n) const
{ Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); return QString(data(), n); }
{ verify(0, n); return QString(data(), n); }
[[nodiscard]] QString last(qsizetype n) const
{ Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); return QString(data() + size() - n, n); }
{ verify(0, n); return QString(data() + size() - n, n); }
[[nodiscard]] QString sliced(qsizetype pos) const
{ Q_ASSERT(pos >= 0); Q_ASSERT(pos <= size()); return QString(data() + pos, size() - pos); }
{ verify(pos, 0); return QString(data() + pos, size() - pos); }
[[nodiscard]] QString sliced(qsizetype pos, qsizetype n) const
{ Q_ASSERT(pos >= 0); Q_ASSERT(n >= 0); Q_ASSERT(size_t(pos) + size_t(n) <= size_t(size())); return QString(data() + pos, n); }
{ verify(pos, n); return QString(data() + pos, n); }
[[nodiscard]] QString chopped(qsizetype n) const
{ Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); return first(size() - n); }
{ verify(0, n); return first(size() - n); }
bool startsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
[[nodiscard]] bool startsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
@ -1003,6 +1002,15 @@ private:
return T(val);
}
Q_ALWAYS_INLINE constexpr void verify([[maybe_unused]] qsizetype pos = 0,
[[maybe_unused]] qsizetype n = 1) const
{
Q_ASSERT(pos >= 0);
Q_ASSERT(pos <= d.size);
Q_ASSERT(n >= 0);
Q_ASSERT(n <= d.size - pos);
}
public:
inline DataPointer &data_ptr() { return d; }
inline const DataPointer &data_ptr() const { return d; }
@ -1098,9 +1106,9 @@ QString QAnyStringView::toString() const
QString::QString(QLatin1StringView latin1)
{ *this = QString::fromLatin1(latin1.data(), latin1.size()); }
const QChar QString::at(qsizetype i) const
{ Q_ASSERT(size_t(i) < size_t(size())); return QChar(d.data()[i]); }
{ verify(i, 1); return QChar(d.data()[i]); }
const QChar QString::operator[](qsizetype i) const
{ Q_ASSERT(size_t(i) < size_t(size())); return QChar(d.data()[i]); }
{ verify(i, 1); return QChar(d.data()[i]); }
const QChar *QString::unicode() const
{ return data(); }
const QChar *QString::data() const
@ -1210,7 +1218,7 @@ void QString::squeeze()
QString &QString::setUtf16(const ushort *autf16, qsizetype asize)
{ return setUnicode(reinterpret_cast<const QChar *>(autf16), asize); }
QChar &QString::operator[](qsizetype i)
{ Q_ASSERT(i >= 0 && i < size()); return data()[i]; }
{ verify(i, 1); return data()[i]; }
QChar &QString::front() { return operator[](0); }
QChar &QString::back() { return operator[](size() - 1); }
QString::iterator QString::begin()

View File

@ -184,7 +184,7 @@ public:
[[nodiscard]] constexpr const storage_type *utf16() const noexcept { return m_data; }
[[nodiscard]] constexpr QChar operator[](qsizetype n) const
{ return Q_ASSERT(n >= 0), Q_ASSERT(n < size()), QChar(m_data[n]); }
{ verify(n, 1); return QChar(m_data[n]); }
//
// QString API
@ -220,20 +220,20 @@ public:
}
[[nodiscard]] constexpr QStringView first(qsizetype n) const noexcept
{ Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); return QStringView(m_data, n); }
{ verify(0, n); return QStringView(m_data, n); }
[[nodiscard]] constexpr QStringView last(qsizetype n) const noexcept
{ Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); return QStringView(m_data + size() - n, n); }
{ verify(0, n); return QStringView(m_data + size() - n, n); }
[[nodiscard]] constexpr QStringView sliced(qsizetype pos) const noexcept
{ Q_ASSERT(pos >= 0); Q_ASSERT(pos <= size()); return QStringView(m_data + pos, size() - pos); }
{ verify(pos, 0); return QStringView(m_data + pos, size() - pos); }
[[nodiscard]] constexpr QStringView sliced(qsizetype pos, qsizetype n) const noexcept
{ Q_ASSERT(pos >= 0); Q_ASSERT(n >= 0); Q_ASSERT(size_t(pos) + size_t(n) <= size_t(size())); return QStringView(m_data + pos, n); }
{ verify(pos, n); return QStringView(m_data + pos, n); }
[[nodiscard]] constexpr QStringView chopped(qsizetype n) const noexcept
{ return Q_ASSERT(n >= 0), Q_ASSERT(n <= size()), QStringView(m_data, m_size - n); }
{ verify(0, n); return QStringView(m_data, m_size - n); }
constexpr void truncate(qsizetype n) noexcept
{ Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); m_size = n; }
{ verify(0, n); ; m_size = n; }
constexpr void chop(qsizetype n) noexcept
{ Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); m_size -= n; }
{ verify(0, n); m_size -= n; }
[[nodiscard]] QStringView trimmed() const noexcept { return QtPrivate::trimmed(*this); }
@ -423,6 +423,15 @@ private:
const storage_type *m_data = nullptr;
#endif
Q_ALWAYS_INLINE constexpr void verify([[maybe_unused]] qsizetype pos = 0,
[[maybe_unused]] qsizetype n = 1) const
{
Q_ASSERT(pos >= 0);
Q_ASSERT(pos <= size());
Q_ASSERT(n >= 0);
Q_ASSERT(n <= size() - pos);
}
constexpr int compare_single_char_helper(int diff) const noexcept
{ return diff ? diff : size() > 1 ? 1 : 0; }
};

View File

@ -207,7 +207,7 @@ public:
#endif
[[nodiscard]] constexpr storage_type operator[](qsizetype n) const
{ return Q_ASSERT(n >= 0), Q_ASSERT(n < size()), m_data[n]; }
{ verify(n, 1); return m_data[n]; }
//
// QString API
@ -238,20 +238,20 @@ public:
}
[[nodiscard]] constexpr QBasicUtf8StringView sliced(qsizetype pos) const
{ verify(pos); return QBasicUtf8StringView{m_data + pos, m_size - pos}; }
{ verify(pos, 0); return QBasicUtf8StringView{m_data + pos, m_size - pos}; }
[[nodiscard]] constexpr QBasicUtf8StringView sliced(qsizetype pos, qsizetype n) const
{ verify(pos, n); return QBasicUtf8StringView(m_data + pos, n); }
[[nodiscard]] constexpr QBasicUtf8StringView first(qsizetype n) const
{ verify(n); return QBasicUtf8StringView(m_data, n); }
{ verify(0, n); return QBasicUtf8StringView(m_data, n); }
[[nodiscard]] constexpr QBasicUtf8StringView last(qsizetype n) const
{ verify(n); return QBasicUtf8StringView(m_data + m_size - n, n); }
{ verify(0, n); return QBasicUtf8StringView(m_data + m_size - n, n); }
[[nodiscard]] constexpr QBasicUtf8StringView chopped(qsizetype n) const
{ verify(n); return QBasicUtf8StringView(m_data, m_size - n); }
{ verify(0, n); return QBasicUtf8StringView(m_data, m_size - n); }
constexpr void truncate(qsizetype n)
{ verify(n); m_size = n; }
{ verify(0, n); m_size = n; }
constexpr void chop(qsizetype n)
{ verify(n); m_size -= n; }
{ verify(0, n); m_size -= n; }
[[nodiscard]] inline bool isValidUtf8() const noexcept
{
@ -326,7 +326,8 @@ private:
{ return QBasicUtf8StringView::compare(lhs, rhs) > 0; }
#endif
Q_ALWAYS_INLINE constexpr void verify(qsizetype pos, qsizetype n = 0) const
Q_ALWAYS_INLINE constexpr void verify([[maybe_unused]] qsizetype pos = 0,
[[maybe_unused]] qsizetype n = 1) const
{
Q_ASSERT(pos >= 0);
Q_ASSERT(pos <= size());

View File

@ -259,6 +259,14 @@ private:
const std::less<const T*> less = {};
return !less(d->end(), i.i) && !less(i.i, d->begin());
}
void verify([[maybe_unused]] qsizetype pos = 0, [[maybe_unused]] qsizetype n = 1) const
{
Q_ASSERT(pos >= 0);
Q_ASSERT(pos <= size());
Q_ASSERT(n >= 0);
Q_ASSERT(n <= size() - pos);
}
public:
QList(DataPointer dd) noexcept
: d(dd)
@ -633,27 +641,13 @@ public:
QList<T> mid(qsizetype pos, qsizetype len = -1) const;
QList<T> first(qsizetype n) const
{
Q_ASSERT(size_t(n) <= size_t(size()));
return QList<T>(begin(), begin() + n);
}
{ verify(0, n); return QList<T>(begin(), begin() + n); }
QList<T> last(qsizetype n) const
{
Q_ASSERT(size_t(n) <= size_t(size()));
return QList<T>(end() - n, end());
}
{ verify(0, n); return QList<T>(end() - n, end()); }
QList<T> sliced(qsizetype pos) const
{
Q_ASSERT(size_t(pos) <= size_t(size()));
return QList<T>(begin() + pos, end());
}
{ verify(pos, 0); return QList<T>(begin() + pos, end()); }
QList<T> sliced(qsizetype pos, qsizetype n) const
{
Q_ASSERT(size_t(pos) <= size_t(size()));
Q_ASSERT(n >= 0);
Q_ASSERT(pos + n <= size());
return QList<T>(begin() + pos, begin() + pos + n);
}
{ verify(pos, n); return QList<T>(begin() + pos, begin() + pos + n); }
T value(qsizetype i) const { return value(i, T()); }
T value(qsizetype i, parameter_type defaultValue) const;