String Views: add slice() methods

Methods with preconditions can't be noexcept (from Marc in code
review).

[ChangeLog][QtCore][String Views] Added slice() methods to
Q{String,ByteArray,Latin1String,Utf8String,AnyString}View which work
like sliced() but modify the view they are called on.

Found in API review.

Task-number: QTBUG-99218
Change-Id: Ic8ef3085f7cfac86b8404fb28d491ca38ad121eb
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
(cherry picked from commit 8f68bd9e6353a42d3b71d893b27fec6bedced501)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Ahmad Samir 2023-07-02 02:39:48 +03:00 committed by Qt Cherry-pick Bot
parent c97e58bb61
commit e1f4f2ef47
11 changed files with 206 additions and 70 deletions

View File

@ -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 <typename Visitor> decltype(auto) QAnyStringView::visit(Visitor &&v) const

View File

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

View File

@ -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); }

View File

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

View File

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

View File

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

View File

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

View File

@ -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 <typename Needle, typename...Flags>
[[nodiscard]] constexpr inline auto tokenize(Needle &&needle, Flags...flags) const
noexcept(noexcept(qTokenize(std::declval<const QStringView&>(), std::forward<Needle>(needle), flags...)))

View File

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

View File

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

View File

@ -741,9 +741,9 @@ private:
void right_data();
template <typename String> void right_impl();
// Tests both sliced() and slice()
void sliced_data();
template <typename String> void sliced_impl();
template <typename String> void slice_impl();
void first_data();
template <typename String> void first_impl();
@ -832,11 +832,6 @@ private Q_SLOTS:
void sliced_QByteArrayView_data() { sliced_data(); }
void sliced_QByteArrayView() { sliced_impl<QByteArrayView>(); }
void slice_QString_data() { sliced_data(); }
void slice_QString() { slice_impl<QString>(); }
void slice_QByteArray_data() { sliced_data(); }
void slice_QByteArray() { slice_impl<QByteArray>(); }
void first_truncate_QString_data() { first_data(); }
void first_truncate_QString() { first_impl<QString>(); }
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 <typename String>
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<String>(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()