summaryrefslogtreecommitdiffstats
path: root/src/corelib/text/qstringview.h
diff options
context:
space:
mode:
authorLars Knoll <lars.knoll@qt.io>2020-06-03 21:59:19 +0200
committerLars Knoll <lars.knoll@qt.io>2020-06-08 13:17:32 +0200
commit38096a3d7040edac4f769270d2402ff4e39d7694 (patch)
tree159f8de28301b992190e5a25553dd5b9c5991eb0 /src/corelib/text/qstringview.h
parent6ec41bd550703aa06ed772fb0f58b7395db40fd3 (diff)
Implement first/last/from and slice() for string-like classes
These methods are scheduled as a replacement for left/right/mid() in Qt 6 with a consistent, narrow contract that does not allow out of bounds indices, and therefore does permit faster implementations. Change-Id: Iabf22e8d4f3fef3c5e69a17f103e6cddebe420b1 Reviewed-by: Alex Blasche <alexander.blasche@qt.io> Reviewed-by: Edward Welbourne <edward.welbourne@qt.io> Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Diffstat (limited to 'src/corelib/text/qstringview.h')
-rw-r--r--src/corelib/text/qstringview.h9
1 files changed, 9 insertions, 0 deletions
diff --git a/src/corelib/text/qstringview.h b/src/corelib/text/qstringview.h
index 028bf3a544..2b085c769d 100644
--- a/src/corelib/text/qstringview.h
+++ b/src/corelib/text/qstringview.h
@@ -265,6 +265,15 @@ public:
{ return Q_ASSERT(n >= 0), Q_ASSERT(n <= size()), QStringView(m_data, n); }
Q_REQUIRED_RESULT Q_DECL_CONSTEXPR QStringView right(qsizetype n) const
{ return Q_ASSERT(n >= 0), Q_ASSERT(n <= size()), QStringView(m_data + m_size - n, n); }
+
+ Q_REQUIRED_RESULT constexpr QStringView first(qsizetype n) const
+ { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); return QStringView(m_data, int(n)); }
+ Q_REQUIRED_RESULT constexpr QStringView last(qsizetype n) const
+ { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); return QStringView(m_data + size() - n, int(n)); }
+ Q_REQUIRED_RESULT constexpr QStringView from(qsizetype pos) const
+ { Q_ASSERT(pos >= 0); Q_ASSERT(pos <= size()); return QStringView(m_data + pos, size() - int(pos)); }
+ Q_REQUIRED_RESULT constexpr QStringView slice(qsizetype pos, qsizetype n) const
+ { Q_ASSERT(pos >= 0); Q_ASSERT(n >= 0); Q_ASSERT(size_t(pos) + size_t(n) <= size_t(size())); return QStringView(m_data + pos, int(n)); }
Q_REQUIRED_RESULT Q_DECL_CONSTEXPR QStringView chopped(qsizetype n) const
{ return Q_ASSERT(n >= 0), Q_ASSERT(n <= size()), QStringView(m_data, m_size - n); }