From fc8cc2573c75b2cb81493dba67b2a9e24699c1ec Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 5 Apr 2017 08:55:19 +0200 Subject: [PATCH] QStringView: add chopped(), chop(), and truncate() Change-Id: I33925f5b2b3e0904f47f18f3cbab480d7f844734 Reviewed-by: Anton Kudryavtsev Reviewed-by: Lars Knoll --- src/corelib/tools/qstringview.cpp | 45 +++++++++++++++++-- src/corelib/tools/qstringview.h | 7 +++ .../tst_qstringapisymmetry.cpp | 4 ++ 3 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/corelib/tools/qstringview.cpp b/src/corelib/tools/qstringview.cpp index 65f76965f6..1dc01e321b 100644 --- a/src/corelib/tools/qstringview.cpp +++ b/src/corelib/tools/qstringview.cpp @@ -578,7 +578,7 @@ QT_BEGIN_NAMESPACE \note The behavior is undefined when \a start < 0 or \a start > size(). - \sa left(), right() + \sa left(), right(), chopped(), chop(), truncate() */ /*! @@ -591,7 +591,7 @@ QT_BEGIN_NAMESPACE \note The behavior is undefined when \a start < 0, \a length < 0, or \a start + \a length > size(). - \sa left(), right() + \sa left(), right(), chopped(), chop(), truncate() */ /*! @@ -602,7 +602,7 @@ QT_BEGIN_NAMESPACE \note The behavior is undefined when \a length < 0 or \a length > size(). - \sa mid(), right() + \sa mid(), right(), chopped(), chop(), truncate() */ /*! @@ -613,7 +613,44 @@ QT_BEGIN_NAMESPACE \note The behavior is undefined when \a length < 0 or \a length > size(). - \sa mid(), left() + \sa mid(), left(), chopped(), chop(), truncate() +*/ + +/*! + \fn QStringView QStringView::chopped(qssize_t length) const + + Returns the substring of length size() - \a length starting at the + beginning of this object. + + Same as \c{left(size() - length)}. + + \note The behavior is undefined when \a length < 0 or \a length > size(). + + \sa mid(), left(), right(), chop(), truncate() +*/ + +/*! + \fn void QStringView::truncate(qssize_t length) + + Truncates this string view to length \a length. + + Same as \c{*this = left(length)}. + + \note The behavior is undefined when \a length < 0 or \a length > size(). + + \sa mid(), left(), right(), chopped(), chop() +*/ + +/*! + \fn void QStringView::chop(qssize_t length) + + Truncates this string view by \a length characters. + + Same as \c{*this = left(size() - length)}. + + \note The behavior is undefined when \a length < 0 or \a length > size(). + + \sa mid(), left(), right(), chopped(), truncate() */ /*! diff --git a/src/corelib/tools/qstringview.h b/src/corelib/tools/qstringview.h index bd59d4ee2c..3318d2f7ef 100644 --- a/src/corelib/tools/qstringview.h +++ b/src/corelib/tools/qstringview.h @@ -233,6 +233,13 @@ public: { return Q_ASSERT(n >= 0), Q_ASSERT(n <= size()), QStringView(m_data, n); } Q_DECL_CONSTEXPR QStringView right(qssize_t n) const { return Q_ASSERT(n >= 0), Q_ASSERT(n <= size()), QStringView(m_data + m_size - n, n); } + Q_DECL_CONSTEXPR QStringView chopped(qssize_t n) const Q_REQUIRED_RESULT + { return Q_ASSERT(n >= 0), Q_ASSERT(n <= size()), QStringView(m_data, m_size - n); } + + Q_DECL_RELAXED_CONSTEXPR void truncate(qssize_t n) + { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); m_size = n; } + Q_DECL_RELAXED_CONSTEXPR void chop(qssize_t n) + { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); m_size -= 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 14e84d80b6..0339ab040f 100644 --- a/tests/auto/corelib/tools/qstringapisymmetry/tst_qstringapisymmetry.cpp +++ b/tests/auto/corelib/tools/qstringapisymmetry/tst_qstringapisymmetry.cpp @@ -236,6 +236,8 @@ private Q_SLOTS: void chop_QString() { chop_impl(); } void chop_QStringRef_data() { chop_data(); } void chop_QStringRef() { chop_impl(); } + void chop_QStringView_data() { chop_data(); } + void chop_QStringView() { chop_impl(); } void chop_QByteArray_data() { chop_data(); } void chop_QByteArray() { chop_impl(); } @@ -243,6 +245,8 @@ private Q_SLOTS: void truncate_QString() { truncate_impl(); } void truncate_QStringRef_data() { truncate_data(); } void truncate_QStringRef() { truncate_impl(); } + void truncate_QStringView_data() { truncate_data(); } + void truncate_QStringView() { truncate_impl(); } void truncate_QByteArray_data() { truncate_data(); } void truncate_QByteArray() { truncate_impl(); }