From 0b9fb15b1a9bd9b5c8658e38c753343ff7ecd390 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 31 Jan 2017 00:36:54 +0100 Subject: QStringView: add mid(), left(), right() Change-Id: If1d2cf175d51b3c02881e21937b0a2d33b78aadd Reviewed-by: Anton Kudryavtsev Reviewed-by: Ville Voutilainen Reviewed-by: Lars Knoll Reviewed-by: Edward Welbourne --- src/corelib/tools/qstringview.cpp | 46 ++++++++++++++++++++++ src/corelib/tools/qstringview.h | 9 +++++ .../qstringapisymmetry/tst_qstringapisymmetry.cpp | 6 +++ 3 files changed, 61 insertions(+) diff --git a/src/corelib/tools/qstringview.cpp b/src/corelib/tools/qstringview.cpp index fe00965a5c..ec77e11740 100644 --- a/src/corelib/tools/qstringview.cpp +++ b/src/corelib/tools/qstringview.cpp @@ -548,4 +548,50 @@ QT_BEGIN_NAMESPACE \sa back(), front(), first() */ +/*! + \fn QStringView QStringView::mid(size_type start) const + + Returns the substring starting at position \a start in this object, + and extending to the end of the string. + + \note The behavior is undefined when \a start < 0 or \a start > size(). + + \sa left(), right() +*/ + +/*! + \fn QStringView QStringView::mid(size_type start, size_type length) const + \overload + + Returns the substring of length \a length starting at position + \a start in this object. + + \note The behavior is undefined when \a start < 0, \a length < 0, + or \a start + \a length > size(). + + \sa left(), right() +*/ + +/*! + \fn QStringView QStringView::left(size_type length) const + + Returns the substring of length \a length starting at position + 0 in this object. + + \note The behavior is undefined when \a length < 0 or \a length > size(). + + \sa mid(), right() +*/ + +/*! + \fn QStringView QStringView::right(size_type length) const + + Returns the substring of length \a length starting at position + size() - \a length in this object. + + \note The behavior is undefined when \a length < 0 or \a length > size(). + + \sa mid(), left() +*/ + QT_END_NAMESPACE diff --git a/src/corelib/tools/qstringview.h b/src/corelib/tools/qstringview.h index b1722e4657..1eb267f78e 100644 --- a/src/corelib/tools/qstringview.h +++ b/src/corelib/tools/qstringview.h @@ -183,6 +183,15 @@ public: Q_DECL_CONSTEXPR QChar at(size_type n) const { return (*this)[n]; } + Q_DECL_CONSTEXPR QStringView mid(size_type pos) const + { return Q_ASSERT(pos >= 0), Q_ASSERT(pos <= size()), QStringView(m_data + pos, m_size - pos); } + Q_DECL_CONSTEXPR QStringView mid(size_type pos, size_type n) const + { return Q_ASSERT(pos >= 0), Q_ASSERT(n >= 0), Q_ASSERT(pos + n <= size()), QStringView(m_data + pos, n); } + Q_DECL_CONSTEXPR QStringView left(size_type n) const + { return Q_ASSERT(n >= 0), Q_ASSERT(n <= size()), QStringView(m_data, n); } + Q_DECL_CONSTEXPR QStringView right(size_type n) const + { return Q_ASSERT(n >= 0), Q_ASSERT(n <= size()), QStringView(m_data + m_size - n, n); } + // // STL compatibility API: // diff --git a/tests/auto/corelib/tools/qstringapisymmetry/tst_qstringapisymmetry.cpp b/tests/auto/corelib/tools/qstringapisymmetry/tst_qstringapisymmetry.cpp index d5c22be029..dd8cbbfbcd 100644 --- a/tests/auto/corelib/tools/qstringapisymmetry/tst_qstringapisymmetry.cpp +++ b/tests/auto/corelib/tools/qstringapisymmetry/tst_qstringapisymmetry.cpp @@ -197,6 +197,8 @@ private Q_SLOTS: void mid_QString() { mid_impl(); } void mid_QStringRef_data() { mid_data(); } void mid_QStringRef() { mid_impl(); } + void mid_QStringView_data() { mid_data(); } + void mid_QStringView() { mid_impl(); } void mid_QLatin1String_data() { mid_data(); } void mid_QLatin1String() { mid_impl(); } void mid_QByteArray_data() { mid_data(); } @@ -206,6 +208,8 @@ private Q_SLOTS: void left_QString() { left_impl(); } void left_QStringRef_data() { left_data(); } void left_QStringRef() { left_impl(); } + void left_QStringView_data() { left_data(); } + void left_QStringView() { left_impl(); } void left_QLatin1String_data() { left_data(); } void left_QLatin1String() { left_impl(); } void left_QByteArray_data() { left_data(); } @@ -215,6 +219,8 @@ private Q_SLOTS: void right_QString() { right_impl(); } void right_QStringRef_data() { right_data(); } void right_QStringRef() { right_impl(); } + void right_QStringView_data() { right_data(); } + void right_QStringView() { right_impl(); } void right_QLatin1String_data() { right_data(); } void right_QLatin1String() { right_impl(); } void right_QByteArray_data() { right_data(); } -- cgit v1.2.3