From f5a2d34da83585784b8924d95f42b69cd26c057f Mon Sep 17 00:00:00 2001 From: Ivan Solovev Date: Wed, 28 Aug 2024 17:05:54 +0200 Subject: [PATCH] Add max_size() and maxSize() to view types That requires including qcontainerfwd.h to the headers, so that we could get access to QtPrivate::MaxAllocSize. The max_size() methods are added for compatibility with stl. The logic for most of the views is similar. The only exception is QAnyStringView, because its character size may vary, depending on the contained data. The maxSize() methods are the static equivalents of max_size(). QASV does not have it for the reasons mentioned above. [ChangeLog][QtCore] Added max_size() to all string-view types. Amends 7ce6920aacfcba485cd8017e01c6aeb324292e75. Found in 6.8 API review. Change-Id: I467c7d64ec3d92eb424eb5b94a39ec3d45d63d1f Reviewed-by: Marc Mutz (cherry picked from commit 2382bfb5b03f6f03a31981dc416121cf556b962a) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/text/qanystringview.cpp | 15 +++++++++++++++ src/corelib/text/qanystringview.h | 7 +++++++ src/corelib/text/qbytearrayview.h | 9 +++++++++ src/corelib/text/qbytearrayview.qdoc | 18 ++++++++++++++++++ src/corelib/text/qlatin1stringview.h | 9 +++++++++ src/corelib/text/qlatin1stringview.qdoc | 18 ++++++++++++++++++ src/corelib/text/qstringview.cpp | 18 ++++++++++++++++++ src/corelib/text/qstringview.h | 9 +++++++++ src/corelib/text/qutf8stringview.h | 9 +++++++++ src/corelib/text/qutf8stringview.qdoc | 18 ++++++++++++++++++ 10 files changed, 130 insertions(+) diff --git a/src/corelib/text/qanystringview.cpp b/src/corelib/text/qanystringview.cpp index 14e07bd4f17..ff699ce44c7 100644 --- a/src/corelib/text/qanystringview.cpp +++ b/src/corelib/text/qanystringview.cpp @@ -640,6 +640,21 @@ QT_BEGIN_NAMESPACE \sa QString::isNull(), QAnyStringView */ +/*! + \fn QAnyStringView::max_size() const + \since 6.8 + + This function is provided for STL compatibility. + + It returns the maximum number of elements that the string view can + theoretically represent. In practice, the number can be much smaller, + limited by the amount of memory available to the system. + + \note The returned value is calculated based on the currently used character + type, so calling this function on two different views may return different + results. +*/ + /*! \fn QAnyStringView::operator<<(QDebug d, QAnyStringView s) \since 6.7 diff --git a/src/corelib/text/qanystringview.h b/src/corelib/text/qanystringview.h index af241ce3ed9..822a53bb7c5 100644 --- a/src/corelib/text/qanystringview.h +++ b/src/corelib/text/qanystringview.h @@ -5,6 +5,7 @@ #define QANYSTRINGVIEW_H #include +#include #include #include #include @@ -296,6 +297,12 @@ public: [[nodiscard]] constexpr qsizetype size_bytes() const noexcept { return size() * charSize(); } + [[nodiscard]] constexpr qsizetype max_size() const noexcept + { + // -1 to deal with the pointer one-past-the-end; + return QtPrivate::MaxAllocSize / charSize() - 1; + } + // // Qt compatibility API: // diff --git a/src/corelib/text/qbytearrayview.h b/src/corelib/text/qbytearrayview.h index d55902537c4..09dd70aa6f2 100644 --- a/src/corelib/text/qbytearrayview.h +++ b/src/corelib/text/qbytearrayview.h @@ -5,6 +5,7 @@ #include #include +#include #include #include @@ -320,6 +321,8 @@ public: [[nodiscard]] constexpr Q_IMPLICIT operator std::string_view() const noexcept { return std::string_view(m_data, size_t(m_size)); } + [[nodiscard]] constexpr qsizetype max_size() const noexcept { return maxSize(); } + // // Qt compatibility API: // @@ -330,6 +333,12 @@ public: [[nodiscard]] constexpr char first() const { return front(); } [[nodiscard]] constexpr char last() const { return back(); } + [[nodiscard]] static constexpr qsizetype maxSize() noexcept + { + // -1 to deal with the pointer one-past-the-end; + return QtPrivate::MaxAllocSize - 1; + } + private: Q_ALWAYS_INLINE constexpr void verify([[maybe_unused]] qsizetype pos = 0, [[maybe_unused]] qsizetype n = 1) const diff --git a/src/corelib/text/qbytearrayview.qdoc b/src/corelib/text/qbytearrayview.qdoc index a80db68a498..bc9df292769 100644 --- a/src/corelib/text/qbytearrayview.qdoc +++ b/src/corelib/text/qbytearrayview.qdoc @@ -1079,3 +1079,21 @@ The returned view will have the same data pointer and length of this view. */ + +/*! + \fn QByteArrayView::maxSize() + \since 6.8 + + It returns the maximum number of elements that the view can + theoretically represent. In practice, the number can be much smaller, + limited by the amount of memory available to the system. +*/ + +/*! + \fn QByteArrayView::max_size() const + \since 6.8 + + This function is provided for STL compatibility. + + Returns maxSize(). +*/ diff --git a/src/corelib/text/qlatin1stringview.h b/src/corelib/text/qlatin1stringview.h index 0606238a5cf..bfe760a61ae 100644 --- a/src/corelib/text/qlatin1stringview.h +++ b/src/corelib/text/qlatin1stringview.h @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -205,6 +206,14 @@ public: const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); } const_reverse_iterator crend() const noexcept { return const_reverse_iterator(begin()); } + [[nodiscard]] constexpr qsizetype max_size() const noexcept { return maxSize(); } + + [[nodiscard]] static constexpr qsizetype maxSize() noexcept + { + // -1 to deal with the pointer one-past-the-end; + return QtPrivate::MaxAllocSize - 1; + } + [[nodiscard]] constexpr QLatin1StringView mid(qsizetype pos, qsizetype n = -1) const { using namespace QtPrivate; diff --git a/src/corelib/text/qlatin1stringview.qdoc b/src/corelib/text/qlatin1stringview.qdoc index c6a148abe0f..60ac381c786 100644 --- a/src/corelib/text/qlatin1stringview.qdoc +++ b/src/corelib/text/qlatin1stringview.qdoc @@ -1318,3 +1318,21 @@ \sa Qt::Literals::StringLiterals */ + +/*! + \fn QLatin1StringView::maxSize() + \since 6.8 + + It returns the maximum number of elements that the string view can + theoretically represent. In practice, the number can be much smaller, + limited by the amount of memory available to the system. +*/ + +/*! + \fn QLatin1StringView::max_size() const + \since 6.8 + + This function is provided for STL compatibility. + + Returns maxSize(). +*/ diff --git a/src/corelib/text/qstringview.cpp b/src/corelib/text/qstringview.cpp index 3eb26cdd832..65df78dfda1 100644 --- a/src/corelib/text/qstringview.cpp +++ b/src/corelib/text/qstringview.cpp @@ -1478,4 +1478,22 @@ or the character \a ch this view. */ +/*! + \fn QStringView::maxSize() + \since 6.8 + + It returns the maximum number of elements that the view can + theoretically represent. In practice, the number can be much smaller, + limited by the amount of memory available to the system. +*/ + +/*! + \fn QStringView::max_size() const + \since 6.8 + + This function is provided for STL compatibility. + + Returns maxSize(). +*/ + QT_END_NAMESPACE diff --git a/src/corelib/text/qstringview.h b/src/corelib/text/qstringview.h index 730134fa5f0..dfcd0e1c1fa 100644 --- a/src/corelib/text/qstringview.h +++ b/src/corelib/text/qstringview.h @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -408,6 +409,8 @@ public: [[nodiscard]] Q_IMPLICIT operator std::u16string_view() const noexcept { return std::u16string_view(m_data, size_t(m_size)); } + [[nodiscard]] constexpr qsizetype max_size() const noexcept { return maxSize(); } + // // Qt compatibility API: // @@ -419,6 +422,12 @@ public: { return size(); } [[nodiscard]] constexpr QChar first() const { return front(); } [[nodiscard]] constexpr QChar last() const { return back(); } + + [[nodiscard]] static constexpr qsizetype maxSize() noexcept + { + // -1 to deal with the pointer one-past-the-end; + return QtPrivate::MaxAllocSize / sizeof(storage_type) - 1; + } private: #if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0) || defined(QT_BOOTSTRAPPED) const storage_type *m_data = nullptr; diff --git a/src/corelib/text/qutf8stringview.h b/src/corelib/text/qutf8stringview.h index 6ff5adb5f14..6f0894e2922 100644 --- a/src/corelib/text/qutf8stringview.h +++ b/src/corelib/text/qutf8stringview.h @@ -12,6 +12,7 @@ #include // for QContainerImplHelper #include #include +#include #include #include @@ -281,6 +282,8 @@ public: [[nodiscard]] Q_IMPLICIT operator std::basic_string_view() const noexcept { return std::basic_string_view(data(), size_t(size())); } + [[nodiscard]] constexpr qsizetype max_size() const noexcept { return maxSize(); } + // // Qt compatibility API: // @@ -309,6 +312,12 @@ public: [[nodiscard]] bool equal(QLatin1StringView other) const noexcept; [[nodiscard]] bool equal(const QByteArray &other) const noexcept; + [[nodiscard]] static constexpr qsizetype maxSize() noexcept + { + // -1 to deal with the pointer one-past-the-end; + return QtPrivate::MaxAllocSize - 1; + } + private: [[nodiscard]] static inline int compare(QBasicUtf8StringView lhs, QBasicUtf8StringView rhs) noexcept { diff --git a/src/corelib/text/qutf8stringview.qdoc b/src/corelib/text/qutf8stringview.qdoc index 436165cd360..303425caa49 100644 --- a/src/corelib/text/qutf8stringview.qdoc +++ b/src/corelib/text/qutf8stringview.qdoc @@ -743,3 +743,21 @@ same data pointer and length of this view. The character type of the returned view will be \c{storage_type}. */ + +/*! + \fn QUtf8StringView::maxSize() + \since 6.8 + + It returns the maximum number of elements that the view can + theoretically represent. In practice, the number can be much smaller, + limited by the amount of memory available to the system. +*/ + +/*! + \fn QUtf8StringView::max_size() const + \since 6.8 + + This function is provided for STL compatibility. + + Returns maxSize(). +*/