summaryrefslogtreecommitdiffstats
path: root/src/corelib/text/qstringview.h
diff options
context:
space:
mode:
authorAhmad Samir <a.samirh78@gmail.com>2023-07-06 15:05:08 +0300
committerAhmad Samir <a.samirh78@gmail.com>2023-09-28 23:45:24 +0300
commit2fd9735e7aee9f6c6c014c3e4d33b420f7ef5375 (patch)
treec6dd34ead9a2a9ee8d5520c28e09fccf92d4e660 /src/corelib/text/qstringview.h
parent016addc201b29696077b8d397c988c3817eaa429 (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/qstringview.h')
-rw-r--r--src/corelib/text/qstringview.h25
1 files changed, 17 insertions, 8 deletions
diff --git a/src/corelib/text/qstringview.h b/src/corelib/text/qstringview.h
index 69694db0c8..6a1806d602 100644
--- a/src/corelib/text/qstringview.h
+++ b/src/corelib/text/qstringview.h
@@ -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; }
};