diff options
author | Marc Mutz <[email protected]> | 2022-11-24 10:51:46 +0100 |
---|---|---|
committer | Marc Mutz <[email protected]> | 2022-12-08 18:00:16 +0000 |
commit | 41a994db063e4dc7796c0a33d3f1baf2c30366f6 (patch) | |
tree | eee589a860b3d8f7fccdbcf53fd0836972423eef /src | |
parent | 270a2f7ee2b1f02a240dfc7be4fc893d4d432d3e (diff) |
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 <[email protected]>
Reviewed-by: Mate Barany <[email protected]>
Reviewed-by: Ivan Solovev <[email protected]>
Reviewed-by: Thiago Macieira <[email protected]>
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/text/qanystringview.h | 42 | ||||
-rw-r--r-- | src/corelib/text/qanystringview.qdoc | 136 |
2 files changed, 178 insertions, 0 deletions
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 <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); 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 <typename Visitor> decltype(auto) QAnyStringView::visit(Visitor &&v) const Calls \a v with either a QUtf8StringView, QLatin1String, or QStringView, depending |