QByteArrayView: Add mid/left/right

Because they are too convenient to leave out.

Change-Id: I844cfb794ce0f575c2c65075d9051b0b878a434f
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Mårten Nordheim 2022-11-17 14:04:29 +01:00
parent acb012558b
commit e35cf5ebdc
3 changed files with 67 additions and 0 deletions

View File

@ -5,6 +5,7 @@
#include <QtCore/qbytearrayalgorithms.h>
#include <QtCore/qstringfwd.h>
#include <QtCore/qarraydata.h>
#include <string>
@ -198,6 +199,18 @@ public:
[[nodiscard]] constexpr QByteArrayView chopped(qsizetype len) const
{ Q_ASSERT(len >= 0); Q_ASSERT(len <= size()); return first(size() - len); }
[[nodiscard]] constexpr QByteArrayView left(qsizetype n) const
{ if (n < 0 || n > size()) n = size(); return QByteArrayView(data(), n); }
[[nodiscard]] constexpr QByteArrayView right(qsizetype n) const
{ if (n < 0 || n > size()) n = size(); if (n < 0) n = 0; return QByteArrayView(data() + size() - n, n); }
[[nodiscard]] constexpr QByteArrayView mid(qsizetype pos, qsizetype n = -1) const
{
using namespace QtPrivate;
auto result = QContainerImplHelper::mid(size(), &pos, &n);
return result == QContainerImplHelper::Null ? QByteArrayView()
: QByteArrayView(m_data + pos, n);
}
constexpr void truncate(qsizetype n)
{ Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); m_size = n; }
constexpr void chop(qsizetype n)

View File

@ -617,6 +617,54 @@
\sa sliced(), first(), last(), chopped(), truncate()
*/
/*!
\fn QByteArrayView QByteArrayView::mid(qsizetype start, qsizetype length) const
\since 6.5
\deprecated Use sliced() instead in new code.
Returns the subarray of length \a length starting at position
\a start in this object.
Returns an empty byte array view if \a start exceeds the
length of the byte array view. If there are less than \a length characters
available in the byte array view starting at \a start, or if
\a length is negative (default), the function returns all characters that
are available from \a start.
\sa first(), last(), sliced(), chopped(), chop(), truncate()
*/
/*!
\fn QByteArrayView QByteArrayView::left(qsizetype length) const
\since 6.5
\deprecated Use first() instead in new code.
Returns the subarray of length \a length starting at position
0 in this object.
The entire byte array view is returned if \a length is greater than or equal
to size(), or less than zero.
\sa first(), last(), sliced(), startsWith(), chopped(), chop(), truncate()
*/
/*!
\fn QByteArrayView QByteArrayView::right(qsizetype length) const
\since 6.5
\deprecated Use last() instead in new code.
Returns the subarray of length \a length starting at position
size() - \a length in this object.
The entire byte array view is returned if \a length is greater than or equal
to size(), or less than zero.
\sa first(), last(), sliced(), endsWith(), chopped(), chop(), truncate()
*/
/*!
\fn QByteArrayView QByteArrayView::trimmed() const noexcept
\since 6.3

View File

@ -730,6 +730,8 @@ private Q_SLOTS:
void mid_QLatin1String() { mid_impl<QLatin1String>(); }
void mid_QByteArray_data() { mid_data(); }
void mid_QByteArray() { mid_impl<QByteArray>(); }
void mid_QByteArrayView_data() { mid_data(); }
void mid_QByteArrayView() { mid_impl<QByteArrayView>(); }
void left_QString_data() { left_data(); }
void left_QString() { left_impl<QString>(); }
@ -741,6 +743,8 @@ private Q_SLOTS:
void left_QLatin1String() { left_impl<QLatin1String>(); }
void left_QByteArray_data();
void left_QByteArray() { left_impl<QByteArray>(); }
void left_QByteArrayView_data() { left_data(); }
void left_QByteArrayView() { left_impl<QByteArrayView>(); }
void right_QString_data() { right_data(); }
void right_QString() { right_impl<QString>(); }
@ -752,6 +756,8 @@ private Q_SLOTS:
void right_QLatin1String() { right_impl<QLatin1String>(); }
void right_QByteArray_data();
void right_QByteArray() { right_impl<QByteArray>(); }
void right_QByteArrayView_data() { right_data(); }
void right_QByteArrayView() { right_impl<QByteArrayView>(); }
void sliced_QString_data() { sliced_data(); }
void sliced_QString() { sliced_impl<QString>(); }