String classes: make first/last/chopped() delegate to sliced()

De-duplicates code.

Change-Id: Id29511e7e571ed14f9e3cfd4355b901d81ea2562
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
bb10
Ahmad Samir 2023-07-15 21:10:47 +03:00
parent fdd2fc2c71
commit 00c1c04bd5
6 changed files with 18 additions and 18 deletions

View File

@ -157,15 +157,15 @@ public:
[[nodiscard]] QByteArray mid(qsizetype index, qsizetype len = -1) const;
[[nodiscard]] QByteArray first(qsizetype n) const
{ verify(0, n); return QByteArray(data(), n); }
{ verify(0, n); return sliced(0, n); }
[[nodiscard]] QByteArray last(qsizetype n) const
{ verify(0, n); return QByteArray(data() + size() - n, n); }
{ verify(0, n); return sliced(size() - n, n); }
[[nodiscard]] QByteArray sliced(qsizetype pos) const
{ verify(pos, 0); return QByteArray(data() + pos, size() - pos); }
[[nodiscard]] QByteArray sliced(qsizetype pos, qsizetype n) const
{ verify(pos, n); return QByteArray(data() + pos, n); }
[[nodiscard]] QByteArray chopped(qsizetype len) const
{ verify(0, len); return first(size() - len); }
{ verify(0, len); return sliced(0, size() - len); }
bool startsWith(QByteArrayView bv) const
{ return QtPrivate::startsWith(qToByteArrayViewIgnoringNull(*this), bv); }

View File

@ -188,15 +188,15 @@ public:
[[nodiscard]] constexpr char at(qsizetype n) const { return (*this)[n]; }
[[nodiscard]] constexpr QByteArrayView first(qsizetype n) const
{ verify(0, n); return QByteArrayView(data(), n); }
{ verify(0, n); return sliced(0, n); }
[[nodiscard]] constexpr QByteArrayView last(qsizetype n) const
{ verify(0, n); return QByteArrayView(data() + size() - n, n); }
{ verify(0, n); return sliced(size() - n, n); }
[[nodiscard]] constexpr QByteArrayView sliced(qsizetype pos) const
{ verify(pos, 0); return QByteArrayView(data() + pos, size() - pos); }
[[nodiscard]] constexpr QByteArrayView sliced(qsizetype pos, qsizetype n) const
{ verify(pos, n); return QByteArrayView(data() + pos, n); }
[[nodiscard]] constexpr QByteArrayView chopped(qsizetype len) const
{ verify(0, len); return first(size() - len); }
{ verify(0, len); return sliced(0, size() - len); }
[[nodiscard]] constexpr QByteArrayView left(qsizetype n) const
{ if (n < 0 || n > size()) n = size(); return QByteArrayView(data(), n); }

View File

@ -227,11 +227,11 @@ public:
[[nodiscard]] constexpr QLatin1StringView sliced(qsizetype pos, qsizetype n) const
{ verify(pos, n); return {m_data + pos, n}; }
[[nodiscard]] constexpr QLatin1StringView first(qsizetype n) const
{ verify(0, n); return {m_data, n}; }
{ verify(0, n); return sliced(0, n); }
[[nodiscard]] constexpr QLatin1StringView last(qsizetype n) const
{ verify(0, n); return {m_data + size() - n, n}; }
{ verify(0, n); return sliced(size() - n, n); }
[[nodiscard]] constexpr QLatin1StringView chopped(qsizetype n) const
{ verify(0, n); return {m_data, size() - n}; }
{ verify(0, n); return sliced(0, size() - n); }
constexpr void chop(qsizetype n)
{ verify(0, n); m_size -= n; }

View File

@ -336,15 +336,15 @@ public:
[[nodiscard]] QString mid(qsizetype position, qsizetype n = -1) const;
[[nodiscard]] QString first(qsizetype n) const
{ verify(0, n); return QString(data(), n); }
{ verify(0, n); return sliced(0, n); }
[[nodiscard]] QString last(qsizetype n) const
{ verify(0, n); return QString(data() + size() - n, n); }
{ verify(0, n); return sliced(size() - n, n); }
[[nodiscard]] QString sliced(qsizetype pos) const
{ verify(pos, 0); return QString(data() + pos, size() - pos); }
[[nodiscard]] QString sliced(qsizetype pos, qsizetype n) const
{ verify(pos, n); return QString(data() + pos, n); }
[[nodiscard]] QString chopped(qsizetype n) const
{ verify(0, n); return first(size() - n); }
{ verify(0, n); return sliced(0, size() - n); }
bool startsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
[[nodiscard]] bool startsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept

View File

@ -220,15 +220,15 @@ public:
}
[[nodiscard]] constexpr QStringView first(qsizetype n) const noexcept
{ verify(0, n); return QStringView(m_data, n); }
{ verify(0, n); return sliced(0, n); }
[[nodiscard]] constexpr QStringView last(qsizetype n) const noexcept
{ verify(0, n); return QStringView(m_data + size() - n, n); }
{ verify(0, n); return sliced(size() - n, n); }
[[nodiscard]] constexpr QStringView sliced(qsizetype pos) const noexcept
{ verify(pos, 0); return QStringView(m_data + pos, size() - pos); }
[[nodiscard]] constexpr QStringView sliced(qsizetype pos, qsizetype n) const noexcept
{ verify(pos, n); return QStringView(m_data + pos, n); }
[[nodiscard]] constexpr QStringView chopped(qsizetype n) const noexcept
{ verify(0, n); return QStringView(m_data, m_size - n); }
{ verify(0, n); return sliced(0, m_size - n); }
constexpr void truncate(qsizetype n) noexcept
{ verify(0, n); ; m_size = n; }

View File

@ -242,11 +242,11 @@ public:
[[nodiscard]] constexpr QBasicUtf8StringView sliced(qsizetype pos, qsizetype n) const
{ verify(pos, n); return QBasicUtf8StringView(m_data + pos, n); }
[[nodiscard]] constexpr QBasicUtf8StringView first(qsizetype n) const
{ verify(0, n); return QBasicUtf8StringView(m_data, n); }
{ verify(0, n); return sliced(0, n); }
[[nodiscard]] constexpr QBasicUtf8StringView last(qsizetype n) const
{ verify(0, n); return QBasicUtf8StringView(m_data + m_size - n, n); }
{ verify(0, n); return sliced(m_size - n, n); }
[[nodiscard]] constexpr QBasicUtf8StringView chopped(qsizetype n) const
{ verify(0, n); return QBasicUtf8StringView(m_data, m_size - n); }
{ verify(0, n); return sliced(0, m_size - n); }
constexpr void truncate(qsizetype n)
{ verify(0, n); m_size = n; }