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 <marc.mutz@qt.io>
(cherry picked from commit 2382bfb5b03f6f03a31981dc416121cf556b962a)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Ivan Solovev 2024-08-28 17:05:54 +02:00 committed by Qt Cherry-pick Bot
parent 60ed64e1d6
commit f5a2d34da8
10 changed files with 130 additions and 0 deletions

View File

@ -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

View File

@ -5,6 +5,7 @@
#define QANYSTRINGVIEW_H
#include <QtCore/qcompare.h>
#include <QtCore/qcontainerfwd.h>
#include <QtCore/qlatin1stringview.h>
#include <QtCore/qstringview.h>
#include <QtCore/qutf8stringview.h>
@ -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:
//

View File

@ -5,6 +5,7 @@
#include <QtCore/qbytearrayalgorithms.h>
#include <QtCore/qcompare.h>
#include <QtCore/qcontainerfwd.h>
#include <QtCore/qstringfwd.h>
#include <QtCore/qarraydata.h>
@ -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

View File

@ -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().
*/

View File

@ -9,6 +9,7 @@
#include <QtCore/qchar.h>
#include <QtCore/qcompare.h>
#include <QtCore/qcontainerfwd.h>
#include <QtCore/qnamespace.h>
#include <QtCore/qtversionchecks.h>
#include <QtCore/qstringview.h>
@ -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;

View File

@ -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().
*/

View File

@ -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

View File

@ -6,6 +6,7 @@
#include <QtCore/qchar.h>
#include <QtCore/qcompare.h>
#include <QtCore/qcontainerfwd.h>
#include <QtCore/qbytearray.h>
#include <QtCore/qstringliteral.h>
#include <QtCore/qstringalgorithms.h>
@ -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;

View File

@ -12,6 +12,7 @@
#include <QtCore/qarraydata.h> // for QContainerImplHelper
#include <QtCore/qbytearrayview.h>
#include <QtCore/qcompare.h>
#include <QtCore/qcontainerfwd.h>
#include <string>
#include <string_view>
@ -281,6 +282,8 @@ public:
[[nodiscard]] Q_IMPLICIT operator std::basic_string_view<storage_type>() const noexcept
{ return std::basic_string_view<storage_type>(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
{

View File

@ -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().
*/