QAnyStringView: add substringing operations
Add the full set of substringing operations: - mid/left/right (old-style) - sliced/first/last (new style) - chop/chopped/truncate The implementation is copied from QUtf8StringView, adjusted to use sliced() instead of the (ptr, n) ctor, so we need to deal with the tag twiddling only once, in sliced(). The documentation is also copied from QUtf8StringView. [ChangeLog][QtCore][QAnyStringView] Added substring functions sliced(), first(), last(), chop()/chopped(), truncate(). Change-Id: Ief454e9694519e97d9146fa84bc05dda1dded046 Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io> Reviewed-by: Mate Barany <mate.barany@qt.io> Reviewed-by: Ivan Solovev <ivan.solovev@qt.io> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
parent
270a2f7ee2
commit
41a994db06
@ -249,6 +249,45 @@ public:
|
||||
template <typename Visitor>
|
||||
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);
|
||||
|
@ -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 <typename Visitor> decltype(auto) QAnyStringView::visit(Visitor &&v) const
|
||||
|
||||
Calls \a v with either a QUtf8StringView, QLatin1String, or QStringView, depending
|
||||
|
@ -728,6 +728,12 @@ private Q_SLOTS:
|
||||
void mid_QUtf8StringView() { mid_impl<QUtf8StringView>(); }
|
||||
void mid_QLatin1String_data() { mid_data(); }
|
||||
void mid_QLatin1String() { mid_impl<QLatin1String>(); }
|
||||
void mid_QAnyStringViewUsingL1_data() { mid_data(); }
|
||||
void mid_QAnyStringViewUsingL1() { mid_impl<QAnyStringViewUsingL1>(); }
|
||||
void mid_QAnyStringViewUsingU8_data() { mid_data(); }
|
||||
void mid_QAnyStringViewUsingU8() { mid_impl<QAnyStringViewUsingU8>(); }
|
||||
void mid_QAnyStringViewUsingU16_data() { mid_data(); }
|
||||
void mid_QAnyStringViewUsingU16() { mid_impl<QAnyStringViewUsingU16>(); }
|
||||
void mid_QByteArray_data() { mid_data(); }
|
||||
void mid_QByteArray() { mid_impl<QByteArray>(); }
|
||||
void mid_QByteArrayView_data() { mid_data(); }
|
||||
@ -741,6 +747,12 @@ private Q_SLOTS:
|
||||
void left_QUtf8StringView() { left_impl<QUtf8StringView>(); }
|
||||
void left_QLatin1String_data() { left_data(); }
|
||||
void left_QLatin1String() { left_impl<QLatin1String>(); }
|
||||
void left_QAnyStringViewUsingL1_data() { left_data(); }
|
||||
void left_QAnyStringViewUsingL1() { left_impl<QAnyStringViewUsingL1>(); }
|
||||
void left_QAnyStringViewUsingU8_data() { left_data(); }
|
||||
void left_QAnyStringViewUsingU8() { left_impl<QAnyStringViewUsingU8>(); }
|
||||
void left_QAnyStringViewUsingU16_data() { left_data(); }
|
||||
void left_QAnyStringViewUsingU16() { left_impl<QAnyStringViewUsingU16>(); }
|
||||
void left_QByteArray_data();
|
||||
void left_QByteArray() { left_impl<QByteArray>(); }
|
||||
void left_QByteArrayView_data() { left_data(); }
|
||||
@ -754,6 +766,12 @@ private Q_SLOTS:
|
||||
void right_QUtf8StringView() { right_impl<QUtf8StringView>(); }
|
||||
void right_QLatin1String_data() { right_data(); }
|
||||
void right_QLatin1String() { right_impl<QLatin1String>(); }
|
||||
void right_QAnyStringViewUsingL1_data() { right_data(); }
|
||||
void right_QAnyStringViewUsingL1() { right_impl<QAnyStringViewUsingL1>(); }
|
||||
void right_QAnyStringViewUsingU8_data() { right_data(); }
|
||||
void right_QAnyStringViewUsingU8() { right_impl<QAnyStringViewUsingU8>(); }
|
||||
void right_QAnyStringViewUsingU16_data() { right_data(); }
|
||||
void right_QAnyStringViewUsingU16() { right_impl<QAnyStringViewUsingU16>(); }
|
||||
void right_QByteArray_data();
|
||||
void right_QByteArray() { right_impl<QByteArray>(); }
|
||||
void right_QByteArrayView_data() { right_data(); }
|
||||
@ -767,6 +785,12 @@ private Q_SLOTS:
|
||||
void sliced_QLatin1String() { sliced_impl<QLatin1String>(); }
|
||||
void sliced_QUtf8StringView_data() { sliced_data(); }
|
||||
void sliced_QUtf8StringView() { sliced_impl<QUtf8StringView>(); }
|
||||
void sliced_QAnyStringViewUsingL1_data() { sliced_data(); }
|
||||
void sliced_QAnyStringViewUsingL1() { sliced_impl<QAnyStringViewUsingL1>(); }
|
||||
void sliced_QAnyStringViewUsingU8_data() { sliced_data(); }
|
||||
void sliced_QAnyStringViewUsingU8() { sliced_impl<QAnyStringViewUsingU8>(); }
|
||||
void sliced_QAnyStringViewUsingU16_data() { sliced_data(); }
|
||||
void sliced_QAnyStringViewUsingU16() { sliced_impl<QAnyStringViewUsingU16>(); }
|
||||
void sliced_QByteArray_data() { sliced_data(); }
|
||||
void sliced_QByteArray() { sliced_impl<QByteArray>(); }
|
||||
void sliced_QByteArrayView_data() { sliced_data(); }
|
||||
@ -780,6 +804,12 @@ private Q_SLOTS:
|
||||
void first_truncate_QLatin1String() { first_impl<QLatin1String>(); }
|
||||
void first_truncate_QUtf8StringView_data() { first_data(); }
|
||||
void first_truncate_QUtf8StringView() { first_impl<QUtf8StringView>(); }
|
||||
void first_truncate_QAnyStringViewUsingL1_data() { first_data(); }
|
||||
void first_truncate_QAnyStringViewUsingL1() { first_impl<QAnyStringViewUsingL1>(); }
|
||||
void first_truncate_QAnyStringViewUsingU8_data() { first_data(); }
|
||||
void first_truncate_QAnyStringViewUsingU8() { first_impl<QAnyStringViewUsingU8>(); }
|
||||
void first_truncate_QAnyStringViewUsingU16_data() { first_data(); }
|
||||
void first_truncate_QAnyStringViewUsingU16() { first_impl<QAnyStringViewUsingU16>(); }
|
||||
void first_truncate_QByteArray_data() { first_data(); }
|
||||
void first_truncate_QByteArray() { first_impl<QByteArray>(); }
|
||||
void first_truncate_QByteArrayView_data() { first_data(); }
|
||||
@ -793,6 +823,12 @@ private Q_SLOTS:
|
||||
void last_QLatin1String() { last_impl<QLatin1String>(); }
|
||||
void last_QUtf8StringView_data() { last_data(); }
|
||||
void last_QUtf8StringView() { last_impl<QUtf8StringView>(); }
|
||||
void last_QAnyStringViewUsingL1_data() { last_data(); }
|
||||
void last_QAnyStringViewUsingL1() { last_impl<QAnyStringViewUsingL1>(); }
|
||||
void last_QAnyStringViewUsingU8_data() { last_data(); }
|
||||
void last_QAnyStringViewUsingU8() { last_impl<QAnyStringViewUsingU8>(); }
|
||||
void last_QAnyStringViewUsingU16_data() { last_data(); }
|
||||
void last_QAnyStringViewUsingU16() { last_impl<QAnyStringViewUsingU16>(); }
|
||||
void last_QByteArray_data() { last_data(); }
|
||||
void last_QByteArray() { last_impl<QByteArray>(); }
|
||||
void last_QByteArrayView_data() { last_data(); }
|
||||
@ -802,10 +838,16 @@ private Q_SLOTS:
|
||||
void chop_QString() { chop_impl<QString>(); }
|
||||
void chop_QStringView_data() { chop_data(); }
|
||||
void chop_QStringView() { chop_impl<QStringView>(); }
|
||||
void chop_QUtf8StringView_data() { chop_data(); }
|
||||
void chop_QUtf8StringView() { chop_impl<QUtf8StringView>(); }
|
||||
void chop_QLatin1String_data() { chop_data(); }
|
||||
void chop_QLatin1String() { chop_impl<QLatin1String>(); }
|
||||
void chop_QUtf8StringView_data() { chop_data(); }
|
||||
void chop_QUtf8StringView() { chop_impl<QUtf8StringView>(); }
|
||||
void chop_QAnyStringViewUsingL1_data() { chop_data(); }
|
||||
void chop_QAnyStringViewUsingL1() { chop_impl<QAnyStringViewUsingL1>(); }
|
||||
void chop_QAnyStringViewUsingU8_data() { chop_data(); }
|
||||
void chop_QAnyStringViewUsingU8() { chop_impl<QAnyStringViewUsingU8>(); }
|
||||
void chop_QAnyStringViewUsingU16_data() { chop_data(); }
|
||||
void chop_QAnyStringViewUsingU16() { chop_impl<QAnyStringViewUsingU16>(); }
|
||||
void chop_QByteArray_data() { chop_data(); }
|
||||
void chop_QByteArray() { chop_impl<QByteArray>(); }
|
||||
void chop_QByteArrayView_data() { chop_data(); }
|
||||
|
Loading…
x
Reference in New Issue
Block a user