diff --git a/src/corelib/text/qanystringview.h b/src/corelib/text/qanystringview.h index f8660b4d2b2..ff9d0f9d8f6 100644 --- a/src/corelib/text/qanystringview.h +++ b/src/corelib/text/qanystringview.h @@ -249,6 +249,45 @@ public: template inline constexpr decltype(auto) visit(Visitor &&v) const; + [[nodiscard]] + constexpr QAnyStringView mid(qsizetype pos, qsizetype n = -1) const + { + using namespace QtPrivate; + auto result = QContainerImplHelper::mid(size(), &pos, &n); + return result == QContainerImplHelper::Null ? QAnyStringView() : sliced(pos, n); + } + [[nodiscard]] + constexpr QAnyStringView left(qsizetype n) const + { + if (size_t(n) >= size_t(size())) + n = size(); + return sliced(0, n); + } + [[nodiscard]] + constexpr QAnyStringView right(qsizetype n) const + { + if (size_t(n) >= size_t(size())) + n = size(); + return sliced(size() - n, n); + } + + [[nodiscard]] constexpr QAnyStringView sliced(qsizetype pos) const + { verify(pos); auto r = *this; r.advanceData(pos); r.setSize(size() - pos); return r; } + [[nodiscard]] constexpr QAnyStringView sliced(qsizetype pos, qsizetype n) const + { verify(pos, n); auto r = *this; r.advanceData(pos); r.setSize(n); return r; } + [[nodiscard]] constexpr QAnyStringView first(qsizetype n) const + { verify(n); return sliced(0, n); } + [[nodiscard]] constexpr QAnyStringView last(qsizetype n) const + { verify(n); return sliced(size() - n, n); } + [[nodiscard]] constexpr QAnyStringView chopped(qsizetype n) const + { verify(n); return sliced(0, size() - n); } + + constexpr void truncate(qsizetype n) + { verify(n); setSize(n); } + constexpr void chop(qsizetype n) + { verify(n); setSize(size() - n); } + + [[nodiscard]] inline QString toString() const; // defined in qstring.h [[nodiscard]] constexpr qsizetype size() const noexcept @@ -312,6 +351,9 @@ private: { return Q_ASSERT(isUtf8()), q_no_char8_t::QUtf8StringView{m_data_utf8, size()}; } [[nodiscard]] inline constexpr QLatin1StringView asLatin1StringView() const; [[nodiscard]] constexpr size_t charSize() const noexcept { return isUtf16() ? 2 : 1; } + constexpr void setSize(qsizetype sz) noexcept { m_size = size_t(sz) | tag(); } + constexpr void advanceData(qsizetype delta) noexcept + { m_data_utf8 += delta * charSize(); } Q_ALWAYS_INLINE constexpr void verify(qsizetype pos, qsizetype n = 0) const { Q_ASSERT(pos >= 0); diff --git a/src/corelib/text/qanystringview.qdoc b/src/corelib/text/qanystringview.qdoc index 796202e2e1b..09cd002f391 100644 --- a/src/corelib/text/qanystringview.qdoc +++ b/src/corelib/text/qanystringview.qdoc @@ -339,6 +339,142 @@ \sa front(), {Sizes and Sub-Strings} */ +/*! + \fn QAnyStringView::mid(qsizetype pos, qsizetype n) const + \since 6.5 + + Returns the substring of length \a n starting at position + \a pos in this object. + + \deprecated Use sliced() instead in new code. + + Returns an empty string view if \a n exceeds the + length of the string view. If there are less than \a n code points + available in the string view starting at \a pos, or if + \a n is negative (default), the function returns all code points that + are available from \a pos. + + \sa first(), last(), sliced(), chopped(), chop(), truncate(), {Sizes and Sub-Strings} +*/ + +/*! + \fn QAnyStringView::left(qsizetype n) const + \since 6.5 + + \deprecated Use first() instead in new code. + + Returns the substring of length \a n starting at position + 0 in this object. + + The entire string view is returned if \a n is greater than or equal + to size(), or less than zero. + + \sa first(), last(), sliced(), chopped(), chop(), truncate(), {Sizes and Sub-Strings} +*/ + +/*! + \fn QAnyStringView::right(qsizetype n) const + \since 6.5 + + \deprecated Use last() instead in new code. + + Returns the substring of length \a n starting at position + size() - \a n in this object. + + The entire string view is returned if \a n is greater than or equal + to size(), or less than zero. + + \sa first(), last(), sliced(), chopped(), chop(), truncate(), {Sizes and Sub-Strings} +*/ + +/*! + \fn QAnyStringView::first(qsizetype n) const + \since 6.5 + + Returns a string view that contains the first \a n code points + of this string view. + + \note The behavior is undefined when \a n < 0 or \a n > size(). + + \sa last(), sliced(), chopped(), chop(), truncate(), {Sizes and Sub-Strings} +*/ + +/*! + \fn QAnyStringView::last(qsizetype n) const + \since 6.5 + + Returns a string view that contains the last \a n code points of this string view. + + \note The behavior is undefined when \a n < 0 or \a n > size(). + + \sa first(), sliced(), chopped(), chop(), truncate(), {Sizes and Sub-Strings} +*/ + +/*! + \fn QAnyStringView::sliced(qsizetype pos, qsizetype n) const + \since 6.5 + + Returns a string view containing \a n code points of this string view, + starting at position \a pos. + + \note The behavior is undefined when \a pos < 0, \a n < 0, + or \a pos + \a n > size(). + + \sa first(), last(), chopped(), chop(), truncate(), {Sizes and Sub-Strings} +*/ + +/*! + \fn QAnyStringView::sliced(qsizetype pos) const + \since 6.5 + + Returns a string view starting at position \a pos in this object, + and extending to its end. + + \note The behavior is undefined when \a pos < 0 or \a pos > size(). + + \sa first(), last(), chopped(), chop(), truncate(), {Sizes and Sub-Strings} +*/ + +/*! + \fn QAnyStringView::chopped(qsizetype n) const + \since 6.5 + + Returns the substring of length size() - \a n starting at the + beginning of this object. + + Same as \c{first(size() - n)}. + + \note The behavior is undefined when \a n < 0 or \a n > size(). + + \sa sliced(), first(), last(), chop(), truncate(), {Sizes and Sub-Strings} +*/ + +/*! + \fn QAnyStringView::truncate(qsizetype n) + \since 6.5 + + Truncates this string view to \a n code points. + + Same as \c{*this = first(n)}. + + \note The behavior is undefined when \a n < 0 or \a n > size(). + + \sa sliced(), first(), last(), chopped(), chop(), {Sizes and Sub-Strings} +*/ + +/*! + \fn QAnyStringView::chop(qsizetype n) + \since 6.5 + + Truncates this string view by \a n code points. + + Same as \c{*this = first(size() - n)}. + + \note The behavior is undefined when \a n < 0 or \a n > size(). + + \sa sliced(), first(), last(), chopped(), truncate(), {Sizes and Sub-Strings} +*/ + /*! \fn template decltype(auto) QAnyStringView::visit(Visitor &&v) const Calls \a v with either a QUtf8StringView, QLatin1String, or QStringView, depending diff --git a/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp b/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp index 74c62cb339c..caba0be0e7b 100644 --- a/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp +++ b/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp @@ -728,6 +728,12 @@ private Q_SLOTS: void mid_QUtf8StringView() { mid_impl(); } void mid_QLatin1String_data() { mid_data(); } void mid_QLatin1String() { mid_impl(); } + void mid_QAnyStringViewUsingL1_data() { mid_data(); } + void mid_QAnyStringViewUsingL1() { mid_impl(); } + void mid_QAnyStringViewUsingU8_data() { mid_data(); } + void mid_QAnyStringViewUsingU8() { mid_impl(); } + void mid_QAnyStringViewUsingU16_data() { mid_data(); } + void mid_QAnyStringViewUsingU16() { mid_impl(); } void mid_QByteArray_data() { mid_data(); } void mid_QByteArray() { mid_impl(); } void mid_QByteArrayView_data() { mid_data(); } @@ -741,6 +747,12 @@ private Q_SLOTS: void left_QUtf8StringView() { left_impl(); } void left_QLatin1String_data() { left_data(); } void left_QLatin1String() { left_impl(); } + void left_QAnyStringViewUsingL1_data() { left_data(); } + void left_QAnyStringViewUsingL1() { left_impl(); } + void left_QAnyStringViewUsingU8_data() { left_data(); } + void left_QAnyStringViewUsingU8() { left_impl(); } + void left_QAnyStringViewUsingU16_data() { left_data(); } + void left_QAnyStringViewUsingU16() { left_impl(); } void left_QByteArray_data(); void left_QByteArray() { left_impl(); } void left_QByteArrayView_data() { left_data(); } @@ -754,6 +766,12 @@ private Q_SLOTS: void right_QUtf8StringView() { right_impl(); } void right_QLatin1String_data() { right_data(); } void right_QLatin1String() { right_impl(); } + void right_QAnyStringViewUsingL1_data() { right_data(); } + void right_QAnyStringViewUsingL1() { right_impl(); } + void right_QAnyStringViewUsingU8_data() { right_data(); } + void right_QAnyStringViewUsingU8() { right_impl(); } + void right_QAnyStringViewUsingU16_data() { right_data(); } + void right_QAnyStringViewUsingU16() { right_impl(); } void right_QByteArray_data(); void right_QByteArray() { right_impl(); } void right_QByteArrayView_data() { right_data(); } @@ -767,6 +785,12 @@ private Q_SLOTS: void sliced_QLatin1String() { sliced_impl(); } void sliced_QUtf8StringView_data() { sliced_data(); } void sliced_QUtf8StringView() { sliced_impl(); } + void sliced_QAnyStringViewUsingL1_data() { sliced_data(); } + void sliced_QAnyStringViewUsingL1() { sliced_impl(); } + void sliced_QAnyStringViewUsingU8_data() { sliced_data(); } + void sliced_QAnyStringViewUsingU8() { sliced_impl(); } + void sliced_QAnyStringViewUsingU16_data() { sliced_data(); } + void sliced_QAnyStringViewUsingU16() { sliced_impl(); } void sliced_QByteArray_data() { sliced_data(); } void sliced_QByteArray() { sliced_impl(); } void sliced_QByteArrayView_data() { sliced_data(); } @@ -780,6 +804,12 @@ private Q_SLOTS: void first_truncate_QLatin1String() { first_impl(); } void first_truncate_QUtf8StringView_data() { first_data(); } void first_truncate_QUtf8StringView() { first_impl(); } + void first_truncate_QAnyStringViewUsingL1_data() { first_data(); } + void first_truncate_QAnyStringViewUsingL1() { first_impl(); } + void first_truncate_QAnyStringViewUsingU8_data() { first_data(); } + void first_truncate_QAnyStringViewUsingU8() { first_impl(); } + void first_truncate_QAnyStringViewUsingU16_data() { first_data(); } + void first_truncate_QAnyStringViewUsingU16() { first_impl(); } void first_truncate_QByteArray_data() { first_data(); } void first_truncate_QByteArray() { first_impl(); } void first_truncate_QByteArrayView_data() { first_data(); } @@ -793,6 +823,12 @@ private Q_SLOTS: void last_QLatin1String() { last_impl(); } void last_QUtf8StringView_data() { last_data(); } void last_QUtf8StringView() { last_impl(); } + void last_QAnyStringViewUsingL1_data() { last_data(); } + void last_QAnyStringViewUsingL1() { last_impl(); } + void last_QAnyStringViewUsingU8_data() { last_data(); } + void last_QAnyStringViewUsingU8() { last_impl(); } + void last_QAnyStringViewUsingU16_data() { last_data(); } + void last_QAnyStringViewUsingU16() { last_impl(); } void last_QByteArray_data() { last_data(); } void last_QByteArray() { last_impl(); } void last_QByteArrayView_data() { last_data(); } @@ -802,10 +838,16 @@ private Q_SLOTS: void chop_QString() { chop_impl(); } void chop_QStringView_data() { chop_data(); } void chop_QStringView() { chop_impl(); } - void chop_QUtf8StringView_data() { chop_data(); } - void chop_QUtf8StringView() { chop_impl(); } void chop_QLatin1String_data() { chop_data(); } void chop_QLatin1String() { chop_impl(); } + void chop_QUtf8StringView_data() { chop_data(); } + void chop_QUtf8StringView() { chop_impl(); } + void chop_QAnyStringViewUsingL1_data() { chop_data(); } + void chop_QAnyStringViewUsingL1() { chop_impl(); } + void chop_QAnyStringViewUsingU8_data() { chop_data(); } + void chop_QAnyStringViewUsingU8() { chop_impl(); } + void chop_QAnyStringViewUsingU16_data() { chop_data(); } + void chop_QAnyStringViewUsingU16() { chop_impl(); } void chop_QByteArray_data() { chop_data(); } void chop_QByteArray() { chop_impl(); } void chop_QByteArrayView_data() { chop_data(); }