diff --git a/src/corelib/text/qanystringview.cpp b/src/corelib/text/qanystringview.cpp index 4129257c02d..14e07bd4f17 100644 --- a/src/corelib/text/qanystringview.cpp +++ b/src/corelib/text/qanystringview.cpp @@ -383,7 +383,7 @@ QT_BEGIN_NAMESPACE \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} + \sa first(), last(), sliced(), chopped(), chop(), truncate(), slice(), {Sizes and Sub-Strings} */ /*! @@ -398,7 +398,7 @@ QT_BEGIN_NAMESPACE 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} + \sa first(), last(), sliced(), chopped(), chop(), truncate(), slice(), {Sizes and Sub-Strings} */ /*! @@ -413,7 +413,7 @@ QT_BEGIN_NAMESPACE 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} + \sa first(), last(), sliced(), chopped(), chop(), truncate(), slice(), {Sizes and Sub-Strings} */ /*! @@ -425,7 +425,7 @@ QT_BEGIN_NAMESPACE \note The behavior is undefined when \a n < 0 or \a n > size(). - \sa last(), sliced(), chopped(), chop(), truncate(), {Sizes and Sub-Strings} + \sa last(), sliced(), chopped(), chop(), truncate(), slice(), {Sizes and Sub-Strings} */ /*! @@ -436,7 +436,7 @@ QT_BEGIN_NAMESPACE \note The behavior is undefined when \a n < 0 or \a n > size(). - \sa first(), sliced(), chopped(), chop(), truncate(), {Sizes and Sub-Strings} + \sa first(), sliced(), chopped(), chop(), truncate(), slice(), {Sizes and Sub-Strings} */ /*! @@ -451,7 +451,7 @@ QT_BEGIN_NAMESPACE or \a pos + \a n > size(). //! [UB-sliced-index-length] - \sa first(), last(), chopped(), chop(), truncate(), {Sizes and Sub-Strings} + \sa first(), last(), chopped(), chop(), truncate(), slice(), {Sizes and Sub-Strings} */ /*! @@ -465,7 +465,32 @@ QT_BEGIN_NAMESPACE \note The behavior is undefined when \a pos < 0 or \a pos > size(). //! [UB-sliced-index-only] - \sa first(), last(), chopped(), chop(), truncate(), {Sizes and Sub-Strings} + \sa first(), last(), chopped(), chop(), truncate(), slice(), {Sizes and Sub-Strings} +*/ + +/*! + \fn QAnyStringView &QAnyStringView::slice(qsizetype pos, qsizetype n) + \since 6.8 + + Modifies this string view to start at position \a pos, extending for + \a n code points. + + \include qanystringview.cpp UB-sliced-index-length + + \sa sliced(), first(), last(), chopped(), chop(), truncate(), {Sizes and Sub-Strings} +*/ + +/*! + \fn QAnyStringView &QAnyStringView::slice(qsizetype pos) + \since 6.8 + \overload + + Modifies this string view to start at position \a pos, extending to + its end. + + \include qanystringview.cpp UB-sliced-index-only + + \sa sliced(), first(), last(), chopped(), chop(), truncate(), {Sizes and Sub-Strings} */ /*! @@ -479,7 +504,7 @@ QT_BEGIN_NAMESPACE \note The behavior is undefined when \a n < 0 or \a n > size(). - \sa sliced(), first(), last(), chop(), truncate(), {Sizes and Sub-Strings} + \sa sliced(), first(), last(), chop(), truncate(), slice(), {Sizes and Sub-Strings} */ /*! @@ -505,7 +530,7 @@ QT_BEGIN_NAMESPACE \note The behavior is undefined when \a n < 0 or \a n > size(). - \sa sliced(), first(), last(), chopped(), truncate(), {Sizes and Sub-Strings} + \sa sliced(), first(), last(), chopped(), truncate(), slice(), {Sizes and Sub-Strings} */ /*! \fn template decltype(auto) QAnyStringView::visit(Visitor &&v) const diff --git a/src/corelib/text/qanystringview.h b/src/corelib/text/qanystringview.h index b855c826d30..af241ce3ed9 100644 --- a/src/corelib/text/qanystringview.h +++ b/src/corelib/text/qanystringview.h @@ -259,6 +259,11 @@ public: [[nodiscard]] constexpr QAnyStringView chopped(qsizetype n) const { verify(0, n); return sliced(0, size() - n); } + constexpr QAnyStringView &slice(qsizetype pos) + { *this = sliced(pos); return *this; } + constexpr QAnyStringView &slice(qsizetype pos, qsizetype n) + { *this = sliced(pos, n); return *this; } + constexpr void truncate(qsizetype n) { verify(0, n); setSize(n); } constexpr void chop(qsizetype n) diff --git a/src/corelib/text/qbytearrayview.h b/src/corelib/text/qbytearrayview.h index 34e38a36ffb..d55902537c4 100644 --- a/src/corelib/text/qbytearrayview.h +++ b/src/corelib/text/qbytearrayview.h @@ -205,6 +205,12 @@ public: { verify(pos, 0); return QByteArrayView(data() + pos, size() - pos); } [[nodiscard]] constexpr QByteArrayView sliced(qsizetype pos, qsizetype n) const { verify(pos, n); return QByteArrayView(data() + pos, n); } + + constexpr QByteArrayView &slice(qsizetype pos) + { *this = sliced(pos); return *this; } + constexpr QByteArrayView &slice(qsizetype pos, qsizetype n) + { *this = sliced(pos, n); return *this; } + [[nodiscard]] constexpr QByteArrayView chopped(qsizetype len) const { verify(0, len); return sliced(0, size() - len); } diff --git a/src/corelib/text/qbytearrayview.qdoc b/src/corelib/text/qbytearrayview.qdoc index eb890917eb0..a80db68a498 100644 --- a/src/corelib/text/qbytearrayview.qdoc +++ b/src/corelib/text/qbytearrayview.qdoc @@ -551,7 +551,7 @@ \note The behavior is undefined when \a n < 0 or \a n > size(). - \sa last(), startsWith(), chopped(), chop(), truncate() + \sa last(), startsWith(), chopped(), chop(), truncate(), sliced(), slice() */ /*! @@ -562,7 +562,7 @@ \note The behavior is undefined when \a n < 0 or \a n > size(). - \sa first(), endsWith(), chopped(), chop(), truncate() + \sa first(), endsWith(), chopped(), chop(), truncate(), sliced(), slice() */ /*! @@ -576,7 +576,7 @@ or \a pos + \a n > size(). //! [UB-sliced-index-length] - \sa first(), last(), chopped(), chop(), truncate() + \sa first(), last(), chopped(), chop(), truncate(), slice() */ /*! @@ -589,7 +589,32 @@ \note The behavior is undefined when \a pos < 0 or \a pos > size(). //! [UB-sliced-index-only] - \sa first(), last(), chopped(), chop(), truncate() + \sa first(), last(), chopped(), chop(), truncate(), slice() +*/ + +/*! + \fn QByteArrayView &QByteArrayView::slice(qsizetype pos, qsizetype n) + \since 6.8 + + Modifies this byte array view to start at position \a pos, extending for + \a n bytes. + + \include qbytearrayview.qdoc UB-sliced-index-length + + \sa sliced(), first(), last(), chopped(), chop(), truncate() +*/ + +/*! + \fn QByteArrayView &QByteArrayView::slice(qsizetype pos) + \since 6.8 + \overload + + Modifies this byte array view to start at position \a pos, extending + to its end. + + \include qbytearrayview.qdoc UB-sliced-index-only + + \sa sliced(), first(), last(), chopped(), chop(), truncate() */ /*! @@ -603,7 +628,7 @@ \note The behavior is undefined when \a length < 0 or \a length > size(). - \sa first(), last(), sliced(), chop(), truncate() + \sa first(), last(), sliced(), chop(), truncate(), slice() */ /*! @@ -627,7 +652,7 @@ \note The behavior is undefined when \a length < 0 or \a length > size(). - \sa sliced(), first(), last(), chopped(), truncate() + \sa sliced(), first(), last(), chopped(), truncate(), slice() */ /*! @@ -645,7 +670,7 @@ \a length is negative (default), the function returns all characters that are available from \a start. - \sa first(), last(), sliced(), chopped(), chop(), truncate() + \sa first(), last(), sliced(), chopped(), chop(), truncate(), slice() */ /*! @@ -660,7 +685,7 @@ 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() + \sa first(), last(), sliced(), startsWith(), chopped(), chop(), truncate(), slice() */ /*! @@ -675,7 +700,7 @@ 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() + \sa first(), last(), sliced(), endsWith(), chopped(), chop(), truncate(), slice() */ /*! diff --git a/src/corelib/text/qlatin1stringview.h b/src/corelib/text/qlatin1stringview.h index c3872aaba0a..0606238a5cf 100644 --- a/src/corelib/text/qlatin1stringview.h +++ b/src/corelib/text/qlatin1stringview.h @@ -236,6 +236,11 @@ public: [[nodiscard]] constexpr QLatin1StringView chopped(qsizetype n) const { verify(0, n); return sliced(0, size() - n); } + constexpr QLatin1StringView &slice(qsizetype pos) + { *this = sliced(pos); return *this; } + constexpr QLatin1StringView &slice(qsizetype pos, qsizetype n) + { *this = sliced(pos, n); return *this; } + constexpr void chop(qsizetype n) { verify(0, n); m_size -= n; } constexpr void truncate(qsizetype n) diff --git a/src/corelib/text/qlatin1stringview.qdoc b/src/corelib/text/qlatin1stringview.qdoc index 711057767b1..c6a148abe0f 100644 --- a/src/corelib/text/qlatin1stringview.qdoc +++ b/src/corelib/text/qlatin1stringview.qdoc @@ -708,7 +708,7 @@ (default), the function returns all characters that are available from \a start. - \sa first(), last(), sliced(), chopped(), chop(), truncate() + \sa first(), last(), sliced(), chopped(), chop(), truncate(), slice() */ /*! @@ -724,7 +724,7 @@ The entire Latin-1 string 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() + \sa first(), last(), sliced(), startsWith(), chopped(), chop(), truncate(), slice() */ /*! @@ -740,7 +740,7 @@ The entire Latin-1 string 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() + \sa first(), last(), sliced(), endsWith(), chopped(), chop(), truncate(), slice() */ /*! @@ -752,7 +752,7 @@ \note The behavior is undefined when \a n < 0 or \a n > size(). - \sa last(), startsWith(), chopped(), chop(), truncate() + \sa last(), startsWith(), chopped(), chop(), truncate(), slice() */ /*! @@ -764,7 +764,7 @@ \note The behavior is undefined when \a n < 0 or \a n > size(). - \sa first(), endsWith(), chopped(), chop(), truncate() + \sa first(), endsWith(), chopped(), chop(), truncate(), slice() */ /*! @@ -779,7 +779,7 @@ or \c{pos + n > size()}. //! [UB-sliced-index-length] - \sa first(), last(), chopped(), chop(), truncate() + \sa first(), last(), chopped(), chop(), truncate(), slice() */ /*! @@ -793,7 +793,32 @@ \note The behavior is undefined when \a pos < 0 or \a pos > size(). //! [UB-sliced-index-only] - \sa first(), last(), chopped(), chop(), truncate() + \sa first(), last(), chopped(), chop(), truncate(), slice() +*/ + +/*! + \fn QLatin1StringView &QLatin1StringView::slice(qsizetype pos, qsizetype n) + \since 6.8 + \overload + + Modifies this Latin-1 string view to start at position \a pos, + extending for \a n characters. + + \include qlatin1stringview.qdoc UB-sliced-index-length + + \sa sliced(), first(), last(), chopped(), chop(), truncate() +*/ + +/*! + \fn QLatin1StringView &QLatin1StringView::slice(qsizetype pos) + \since 6.8 + + Modifies this Latin-1 string view to start at position \a pos, extending + to its end. + + \include qlatin1stringview.qdoc UB-sliced-index-only + + \sa sliced(), first(), last(), chopped(), chop(), truncate() */ /*! @@ -807,7 +832,7 @@ \note The behavior is undefined when \a length < 0 or \a length > size(). - \sa sliced(), first(), last(), chop(), truncate() + \sa sliced(), first(), last(), chop(), truncate(), slice() */ /*! @@ -833,7 +858,7 @@ \note The behavior is undefined when \a length < 0 or \a length > size(). - \sa sliced(), first(), last(), chopped(), truncate() + \sa sliced(), first(), last(), chopped(), truncate(), slice() */ /*! diff --git a/src/corelib/text/qstringview.cpp b/src/corelib/text/qstringview.cpp index 29b83ffe8fa..3eb26cdd832 100644 --- a/src/corelib/text/qstringview.cpp +++ b/src/corelib/text/qstringview.cpp @@ -592,7 +592,7 @@ QT_BEGIN_NAMESPACE \a length is negative (default), the function returns all characters that are available from \a start. - \sa first(), last(), sliced(), chopped(), chop(), truncate() + \sa first(), last(), sliced(), chopped(), chop(), truncate(), slice() */ /*! @@ -606,7 +606,7 @@ QT_BEGIN_NAMESPACE The entire string 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() + \sa first(), last(), sliced(), startsWith(), chopped(), chop(), truncate(), slice() */ /*! @@ -620,7 +620,7 @@ QT_BEGIN_NAMESPACE The entire string 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() + \sa first(), last(), sliced(), endsWith(), chopped(), chop(), truncate(), slice() */ /*! @@ -632,7 +632,7 @@ QT_BEGIN_NAMESPACE \note The behavior is undefined when \a n < 0 or \a n > size(). - \sa last(), sliced(), startsWith(), chopped(), chop(), truncate() + \sa last(), sliced(), startsWith(), chopped(), chop(), truncate(), slice() */ /*! @@ -644,7 +644,7 @@ QT_BEGIN_NAMESPACE \note The behavior is undefined when \a n < 0 or \a n > size(). - \sa first(), sliced(), endsWith(), chopped(), chop(), truncate() + \sa first(), sliced(), endsWith(), chopped(), chop(), truncate(), slice() */ /*! @@ -659,7 +659,7 @@ QT_BEGIN_NAMESPACE or \a pos + \a n > size(). //! [UB-sliced-index-length] - \sa first(), last(), chopped(), chop(), truncate() + \sa first(), last(), chopped(), chop(), truncate(), slice() */ /*! @@ -674,7 +674,32 @@ QT_BEGIN_NAMESPACE \note The behavior is undefined when \a pos < 0 or \a pos > size(). //! [UB-sliced-index-only] - \sa first(), last(), chopped(), chop(), truncate() + \sa first(), last(), chopped(), chop(), truncate(), slice() +*/ + +/*! + \fn QStringView &QStringView::slice(qsizetype pos, qsizetype n) + \since 6.8 + + Modifies this string view to start from position \a pos, extending + for \a n code points. + + \include qstringview.cpp UB-sliced-index-length + + \sa sliced(), first(), last(), chopped(), chop(), truncate() +*/ + +/*! + \fn QStringView &QStringView::slice(qsizetype pos) + \since 6.8 + \overload + + Modifies this string view to start from position \a pos, extending + to its end. + + \include qstringview.cpp UB-sliced-index-only + + \sa sliced(), first(), last(), chopped(), chop(), truncate() */ /*! @@ -687,7 +712,7 @@ QT_BEGIN_NAMESPACE \note The behavior is undefined when \a length < 0 or \a length > size(). - \sa mid(), left(), right(), chop(), truncate() + \sa mid(), left(), right(), chop(), truncate(), slice() */ /*! @@ -711,7 +736,7 @@ QT_BEGIN_NAMESPACE \note The behavior is undefined when \a length < 0 or \a length > size(). - \sa mid(), left(), right(), chopped(), truncate() + \sa mid(), left(), right(), chopped(), truncate(), slice() */ /*! diff --git a/src/corelib/text/qstringview.h b/src/corelib/text/qstringview.h index 9578363806f..730134fa5f0 100644 --- a/src/corelib/text/qstringview.h +++ b/src/corelib/text/qstringview.h @@ -237,6 +237,11 @@ public: [[nodiscard]] QStringView trimmed() const noexcept { return QtPrivate::trimmed(*this); } + constexpr QStringView &slice(qsizetype pos) + { *this = sliced(pos); return *this; } + constexpr QStringView &slice(qsizetype pos, qsizetype n) + { *this = sliced(pos, n); return *this; } + template [[nodiscard]] constexpr inline auto tokenize(Needle &&needle, Flags...flags) const noexcept(noexcept(qTokenize(std::declval(), std::forward(needle), flags...))) diff --git a/src/corelib/text/qutf8stringview.h b/src/corelib/text/qutf8stringview.h index 872ff759cc4..6ff5adb5f14 100644 --- a/src/corelib/text/qutf8stringview.h +++ b/src/corelib/text/qutf8stringview.h @@ -247,6 +247,11 @@ public: [[nodiscard]] constexpr QBasicUtf8StringView chopped(qsizetype n) const { verify(0, n); return sliced(0, m_size - n); } + constexpr QBasicUtf8StringView &slice(qsizetype pos) + { *this = sliced(pos); return *this; } + constexpr QBasicUtf8StringView &slice(qsizetype pos, qsizetype n) + { *this = sliced(pos, n); return *this; } + constexpr void truncate(qsizetype n) { verify(0, n); m_size = n; } constexpr void chop(qsizetype n) diff --git a/src/corelib/text/qutf8stringview.qdoc b/src/corelib/text/qutf8stringview.qdoc index b433e5b9956..436165cd360 100644 --- a/src/corelib/text/qutf8stringview.qdoc +++ b/src/corelib/text/qutf8stringview.qdoc @@ -555,7 +555,7 @@ \a n is negative (default), the function returns all code points that are available from \a pos. - \sa first(), last(), sliced(), chopped(), chop(), truncate() + \sa first(), last(), sliced(), chopped(), chop(), truncate(), slice() */ /*! @@ -569,7 +569,7 @@ 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() + \sa first(), last(), sliced(), chopped(), chop(), truncate(), slice() */ /*! @@ -583,7 +583,7 @@ 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() + \sa first(), last(), sliced(), chopped(), chop(), truncate(), slice() */ /*! @@ -594,7 +594,7 @@ \note The behavior is undefined when \a n < 0 or \a n > size(). - \sa last(), sliced(), chopped(), chop(), truncate() + \sa last(), sliced(), chopped(), chop(), truncate(), slice() */ /*! @@ -604,7 +604,7 @@ \note The behavior is undefined when \a n < 0 or \a n > size(). - \sa first(), sliced(), chopped(), chop(), truncate() + \sa first(), sliced(), chopped(), chop(), truncate(), slice() */ /*! @@ -618,7 +618,7 @@ or \a pos + \a n > size(). //! [UB-sliced-index-length] - \sa first(), last(), chopped(), chop(), truncate() + \sa first(), last(), chopped(), chop(), truncate(), slice() */ /*! @@ -631,7 +631,32 @@ \note The behavior is undefined when \a pos < 0 or \a pos > size(). //! [UB-sliced-index-only] - \sa first(), last(), chopped(), chop(), truncate() + \sa first(), last(), chopped(), chop(), truncate(), slice() +*/ + +/*! + \fn QUtf8StringView &QUtf8StringView::slice(qsizetype pos, qsizetype n) + \since 6.8 + + Modifies this string view to start at position \a pos, extending for + \a n code points. + + \include qutf8stringview.qdoc UB-sliced-index-length + + \sa sliced(), first(), last(), chopped(), chop(), truncate() +*/ + +/*! + \fn QUtf8StringView &QUtf8StringView::slice(qsizetype pos) + \since 6.8 + \overload + + Modifies this string view to start at position \a pos, extending + to its end. + + \include qutf8stringview.qdoc UB-sliced-index-only + + \sa sliced(), first(), last(), chopped(), chop(), truncate() */ /*! @@ -644,7 +669,7 @@ \note The behavior is undefined when \a n < 0 or \a n > size(). - \sa sliced(), first(), last(), chop(), truncate() + \sa sliced(), first(), last(), chop(), truncate(), slice() */ /*! diff --git a/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp b/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp index 06ef1aede0d..d281162f88d 100644 --- a/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp +++ b/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp @@ -741,9 +741,9 @@ private: void right_data(); template void right_impl(); + // Tests both sliced() and slice() void sliced_data(); template void sliced_impl(); - template void slice_impl(); void first_data(); template void first_impl(); @@ -832,11 +832,6 @@ private Q_SLOTS: void sliced_QByteArrayView_data() { sliced_data(); } void sliced_QByteArrayView() { sliced_impl(); } - void slice_QString_data() { sliced_data(); } - void slice_QString() { slice_impl(); } - void slice_QByteArray_data() { sliced_data(); } - void slice_QByteArray() { slice_impl(); } - void first_truncate_QString_data() { first_data(); } void first_truncate_QString() { first_impl(); } void first_truncate_QStringView_data() { first_data(); } @@ -2564,27 +2559,17 @@ void tst_QStringApiSymmetry::sliced_impl() QCOMPARE_EQ(sliced.isNull(), result2.isNull()); QCOMPARE_EQ(sliced.isEmpty(), result2.isEmpty()); } -} -template -void tst_QStringApiSymmetry::slice_impl() -{ - QFETCH(const QStringView, unicode); - QFETCH(const QLatin1String, latin1); - QFETCH(const int, pos); - QFETCH(const int, n); - QFETCH(const QAnyStringView, result); - QFETCH(const QAnyStringView, result2); - - const auto str = make(unicode, latin1, unicode.toUtf8()); - - auto s = str; - s.slice(pos); - QCOMPARE_EQ(s, result); - - s = str; - s.slice(pos, n); - QCOMPARE_EQ(s, result2); + { + auto str = s; + str.slice(pos); + QCOMPARE_EQ(str, result); + } + { + auto str = s; + str.slice(pos, n); + QCOMPARE_EQ(str, result2); + } } void tst_QStringApiSymmetry::first_data()