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/qstring.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/qstring.h')
-rw-r--r-- | src/corelib/text/qstring.h | 26 |
1 files changed, 17 insertions, 9 deletions
diff --git a/src/corelib/text/qstring.h b/src/corelib/text/qstring.h index 078abda361..cd4af57e13 100644 --- a/src/corelib/text/qstring.h +++ b/src/corelib/text/qstring.h @@ -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() |