summaryrefslogtreecommitdiffstats
path: root/src/corelib/text/qanystringview.h
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@qt.io>2022-11-24 10:51:46 +0100
committerMarc Mutz <marc.mutz@qt.io>2022-12-08 18:00:16 +0000
commit41a994db063e4dc7796c0a33d3f1baf2c30366f6 (patch)
treeeee589a860b3d8f7fccdbcf53fd0836972423eef /src/corelib/text/qanystringview.h
parent270a2f7ee2b1f02a240dfc7be4fc893d4d432d3e (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 <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>
Diffstat (limited to 'src/corelib/text/qanystringview.h')
-rw-r--r--src/corelib/text/qanystringview.h42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/corelib/text/qanystringview.h b/src/corelib/text/qanystringview.h
index f8660b4d2b..ff9d0f9d8f 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);