diff options
author | Ahmad Samir <a.samirh78@gmail.com> | 2023-07-06 15:05:08 +0300 |
---|---|---|
committer | Ahmad Samir <a.samirh78@gmail.com> | 2023-09-28 23:45:24 +0300 |
commit | 2fd9735e7aee9f6c6c014c3e4d33b420f7ef5375 (patch) | |
tree | c6dd34ead9a2a9ee8d5520c28e09fccf92d4e660 /src/corelib/text/qbytearrayview.h | |
parent | 016addc201b29696077b8d397c988c3817eaa429 (diff) |
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>
Diffstat (limited to 'src/corelib/text/qbytearrayview.h')
-rw-r--r-- | src/corelib/text/qbytearrayview.h | 25 |
1 files changed, 17 insertions, 8 deletions
diff --git a/src/corelib/text/qbytearrayview.h b/src/corelib/text/qbytearrayview.h index a056c7836e..2fe358b619 100644 --- a/src/corelib/text/qbytearrayview.h +++ b/src/corelib/text/qbytearrayview.h @@ -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; }; |