diff --git a/src/corelib/text/qbytearrayview.h b/src/corelib/text/qbytearrayview.h index e0e318249c..ad3bfbdaeb 100644 --- a/src/corelib/text/qbytearrayview.h +++ b/src/corelib/text/qbytearrayview.h @@ -5,6 +5,7 @@ #include #include +#include #include @@ -198,6 +199,18 @@ public: [[nodiscard]] constexpr QByteArrayView chopped(qsizetype len) const { Q_ASSERT(len >= 0); Q_ASSERT(len <= size()); return first(size() - len); } + [[nodiscard]] constexpr QByteArrayView left(qsizetype n) const + { if (n < 0 || n > size()) n = size(); return QByteArrayView(data(), n); } + [[nodiscard]] constexpr QByteArrayView right(qsizetype n) const + { if (n < 0 || n > size()) n = size(); if (n < 0) n = 0; return QByteArrayView(data() + size() - n, n); } + [[nodiscard]] constexpr QByteArrayView mid(qsizetype pos, qsizetype n = -1) const + { + using namespace QtPrivate; + auto result = QContainerImplHelper::mid(size(), &pos, &n); + return result == QContainerImplHelper::Null ? QByteArrayView() + : QByteArrayView(m_data + pos, n); + } + constexpr void truncate(qsizetype n) { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); m_size = n; } constexpr void chop(qsizetype n) diff --git a/src/corelib/text/qbytearrayview.qdoc b/src/corelib/text/qbytearrayview.qdoc index 10cc636fd4..79cf9a2fa4 100644 --- a/src/corelib/text/qbytearrayview.qdoc +++ b/src/corelib/text/qbytearrayview.qdoc @@ -617,6 +617,54 @@ \sa sliced(), first(), last(), chopped(), truncate() */ +/*! + \fn QByteArrayView QByteArrayView::mid(qsizetype start, qsizetype length) const + \since 6.5 + + \deprecated Use sliced() instead in new code. + + Returns the subarray of length \a length starting at position + \a start in this object. + + Returns an empty byte array view if \a start exceeds the + length of the byte array view. If there are less than \a length characters + available in the byte array view starting at \a start, or if + \a length is negative (default), the function returns all characters that + are available from \a start. + + \sa first(), last(), sliced(), chopped(), chop(), truncate() +*/ + +/*! + \fn QByteArrayView QByteArrayView::left(qsizetype length) const + \since 6.5 + + \deprecated Use first() instead in new code. + + Returns the subarray of length \a length starting at position + 0 in this object. + + The entire byte array view is returned if \a length is greater than or equal + to size(), or less than zero. + + \sa first(), last(), sliced(), startsWith(), chopped(), chop(), truncate() +*/ + +/*! + \fn QByteArrayView QByteArrayView::right(qsizetype length) const + \since 6.5 + + \deprecated Use last() instead in new code. + + Returns the subarray of length \a length starting at position + size() - \a length in this object. + + The entire byte array view is returned if \a length is greater than or equal + to size(), or less than zero. + + \sa first(), last(), sliced(), endsWith(), chopped(), chop(), truncate() +*/ + /*! \fn QByteArrayView QByteArrayView::trimmed() const noexcept \since 6.3 diff --git a/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp b/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp index bee958e57d..74c62cb339 100644 --- a/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp +++ b/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp @@ -730,6 +730,8 @@ private Q_SLOTS: void mid_QLatin1String() { mid_impl(); } void mid_QByteArray_data() { mid_data(); } void mid_QByteArray() { mid_impl(); } + void mid_QByteArrayView_data() { mid_data(); } + void mid_QByteArrayView() { mid_impl(); } void left_QString_data() { left_data(); } void left_QString() { left_impl(); } @@ -741,6 +743,8 @@ private Q_SLOTS: void left_QLatin1String() { left_impl(); } void left_QByteArray_data(); void left_QByteArray() { left_impl(); } + void left_QByteArrayView_data() { left_data(); } + void left_QByteArrayView() { left_impl(); } void right_QString_data() { right_data(); } void right_QString() { right_impl(); } @@ -752,6 +756,8 @@ private Q_SLOTS: void right_QLatin1String() { right_impl(); } void right_QByteArray_data(); void right_QByteArray() { right_impl(); } + void right_QByteArrayView_data() { right_data(); } + void right_QByteArrayView() { right_impl(); } void sliced_QString_data() { sliced_data(); } void sliced_QString() { sliced_impl(); }