QStringView: add chopped(), chop(), and truncate()

Change-Id: I33925f5b2b3e0904f47f18f3cbab480d7f844734
Reviewed-by: Anton Kudryavtsev <antkudr@mail.ru>
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
This commit is contained in:
Marc Mutz 2017-04-05 08:55:19 +02:00
parent d176808eef
commit fc8cc2573c
3 changed files with 52 additions and 4 deletions

View File

@ -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()
*/
/*!

View File

@ -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:

View File

@ -236,6 +236,8 @@ private Q_SLOTS:
void chop_QString() { chop_impl<QString>(); }
void chop_QStringRef_data() { chop_data(); }
void chop_QStringRef() { chop_impl<QStringRef>(); }
void chop_QStringView_data() { chop_data(); }
void chop_QStringView() { chop_impl<QStringView>(); }
void chop_QByteArray_data() { chop_data(); }
void chop_QByteArray() { chop_impl<QByteArray>(); }
@ -243,6 +245,8 @@ private Q_SLOTS:
void truncate_QString() { truncate_impl<QString>(); }
void truncate_QStringRef_data() { truncate_data(); }
void truncate_QStringRef() { truncate_impl<QStringRef>(); }
void truncate_QStringView_data() { truncate_data(); }
void truncate_QStringView() { truncate_impl<QStringView>(); }
void truncate_QByteArray_data() { truncate_data(); }
void truncate_QByteArray() { truncate_impl<QByteArray>(); }