summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorAhmad Samir <a.samirh78@gmail.com>2023-07-02 02:39:48 +0300
committerMarc Mutz <marc.mutz@qt.io>2024-08-28 15:32:19 +0000
commit8f68bd9e6353a42d3b71d893b27fec6bedced501 (patch)
tree1d30b833668214c8e36dedcb7d39bc50f7f0a688 /src
parent8e63cf282cb248ddd7fbc908766091659b3a0b22 (diff)
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. Pick-to: 6.8 Task-number: QTBUG-99218 Change-Id: Ic8ef3085f7cfac86b8404fb28d491ca38ad121eb Reviewed-by: Marc Mutz <marc.mutz@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/corelib/text/qanystringview.cpp43
-rw-r--r--src/corelib/text/qanystringview.h5
-rw-r--r--src/corelib/text/qbytearrayview.h6
-rw-r--r--src/corelib/text/qbytearrayview.qdoc43
-rw-r--r--src/corelib/text/qlatin1stringview.h5
-rw-r--r--src/corelib/text/qlatin1stringview.qdoc43
-rw-r--r--src/corelib/text/qstringview.cpp43
-rw-r--r--src/corelib/text/qstringview.h5
-rw-r--r--src/corelib/text/qutf8stringview.h5
-rw-r--r--src/corelib/text/qutf8stringview.qdoc41
10 files changed, 195 insertions, 44 deletions
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 <typename Visitor> decltype(auto) QAnyStringView::visit(Visitor &&v) const
diff --git a/src/corelib/text/qanystringview.h b/src/corelib/text/qanystringview.h
index d5f0d92b998..fa3591ac38a 100644
--- a/src/corelib/text/qanystringview.h
+++ b/src/corelib/text/qanystringview.h
@@ -268,6 +268,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 <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...)))
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()
*/
/*!