diff options
Diffstat (limited to 'src/corelib/text/qbytearrayview.h')
-rw-r--r-- | src/corelib/text/qbytearrayview.h | 125 |
1 files changed, 95 insertions, 30 deletions
diff --git a/src/corelib/text/qbytearrayview.h b/src/corelib/text/qbytearrayview.h index e0e318249c..45ebc812cd 100644 --- a/src/corelib/text/qbytearrayview.h +++ b/src/corelib/text/qbytearrayview.h @@ -4,9 +4,13 @@ #define QBYTEARRAYVIEW_H #include <QtCore/qbytearrayalgorithms.h> +#include <QtCore/qcompare.h> #include <QtCore/qstringfwd.h> +#include <QtCore/qarraydata.h> #include <string> +#include <string_view> +#include <QtCore/q20type_traits.h> QT_BEGIN_NAMESPACE @@ -22,8 +26,7 @@ struct IsCompatibleByteTypeHelper template <typename Byte> struct IsCompatibleByteType - : IsCompatibleByteTypeHelper< - typename std::remove_cv_t<typename std::remove_reference_t<Byte>>> {}; + : IsCompatibleByteTypeHelper<q20::remove_cvref_t<Byte>> {}; template <typename Pointer> struct IsCompatibleByteArrayPointerHelper : std::false_type {}; @@ -32,8 +35,7 @@ struct IsCompatibleByteArrayPointerHelper<Byte *> : IsCompatibleByteType<Byte> {}; template<typename Pointer> struct IsCompatibleByteArrayPointer - : IsCompatibleByteArrayPointerHelper< - typename std::remove_cv_t<typename std::remove_reference_t<Pointer>>> {}; + : IsCompatibleByteArrayPointerHelper<q20::remove_cvref_t<Pointer>> {}; template <typename T, typename Enable = void> struct IsContainerCompatibleWithQByteArrayView : std::false_type {}; @@ -166,6 +168,9 @@ public: constexpr QByteArrayView(const char (&data)[Size]) noexcept : QByteArrayView(data, lengthHelperCharArray(data, Size)) {} + constexpr QByteArrayView(QLatin1StringView v) noexcept; // defined in qlatin1stringview.h + constexpr QByteArrayView(QUtf8StringView v) noexcept; // defined in qutf8stringview.h + #ifdef Q_QDOC template <typename Byte, size_t Size> #else @@ -180,7 +185,7 @@ public: [[nodiscard]] constexpr const_pointer constData() const noexcept { return data(); } [[nodiscard]] constexpr char operator[](qsizetype n) const - { Q_ASSERT(n >= 0); Q_ASSERT(n < size()); return m_data[n]; } + { verify(n, 1); return m_data[n]; } // // QByteArray API @@ -188,20 +193,32 @@ public: [[nodiscard]] constexpr char at(qsizetype n) const { return (*this)[n]; } [[nodiscard]] constexpr QByteArrayView first(qsizetype n) const - { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); return QByteArrayView(data(), n); } + { verify(0, n); return sliced(0, n); } [[nodiscard]] constexpr QByteArrayView last(qsizetype n) const - { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); return QByteArrayView(data() + size() - n, n); } + { verify(0, n); return sliced(size() - n, n); } [[nodiscard]] constexpr QByteArrayView sliced(qsizetype pos) const - { Q_ASSERT(pos >= 0); Q_ASSERT(pos <= size()); return QByteArrayView(data() + pos, size() - pos); } + { verify(pos, 0); return QByteArrayView(data() + pos, size() - pos); } [[nodiscard]] constexpr QByteArrayView sliced(qsizetype pos, qsizetype n) const - { Q_ASSERT(pos >= 0); Q_ASSERT(n >= 0); Q_ASSERT(size_t(pos) + size_t(n) <= size_t(size())); return QByteArrayView(data() + pos, n); } + { verify(pos, n); return QByteArrayView(data() + pos, n); } [[nodiscard]] constexpr QByteArrayView chopped(qsizetype len) const - { Q_ASSERT(len >= 0); Q_ASSERT(len <= size()); 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); } + [[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; } + { verify(0, n); m_size = n; } constexpr void chop(qsizetype n) - { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); m_size -= n; } + { verify(0, n); m_size -= n; } // Defined in qbytearray.cpp: [[nodiscard]] QByteArrayView trimmed() const noexcept @@ -239,18 +256,18 @@ public: [[nodiscard]] bool startsWith(QByteArrayView other) const noexcept { return QtPrivate::startsWith(*this, other); } - [[nodiscard]] bool startsWith(char c) const noexcept + [[nodiscard]] constexpr bool startsWith(char c) const noexcept { return !empty() && front() == c; } [[nodiscard]] bool endsWith(QByteArrayView other) const noexcept { return QtPrivate::endsWith(*this, other); } - [[nodiscard]] bool endsWith(char c) const noexcept + [[nodiscard]] constexpr bool endsWith(char c) const noexcept { return !empty() && back() == c; } [[nodiscard]] qsizetype indexOf(QByteArrayView a, qsizetype from = 0) const noexcept { return QtPrivate::findByteArray(*this, from, a); } [[nodiscard]] qsizetype indexOf(char ch, qsizetype from = 0) const noexcept - { return QtPrivate::findByteArray(*this, from, QByteArrayView(&ch, 1)); } + { return QtPrivate::findByteArray(*this, from, ch); } [[nodiscard]] bool contains(QByteArrayView a) const noexcept { return indexOf(a) != qsizetype(-1); } @@ -262,7 +279,7 @@ public: [[nodiscard]] qsizetype lastIndexOf(QByteArrayView a, qsizetype from) const noexcept { return QtPrivate::lastIndexOf(*this, from, a); } [[nodiscard]] qsizetype lastIndexOf(char ch, qsizetype from = -1) const noexcept - { return QtPrivate::lastIndexOf(*this, from, QByteArrayView(&ch, 1)); } + { return QtPrivate::lastIndexOf(*this, from, ch); } [[nodiscard]] qsizetype count(QByteArrayView a) const noexcept { return QtPrivate::count(*this, a); } @@ -289,6 +306,9 @@ public: [[nodiscard]] constexpr char front() const { Q_ASSERT(!empty()); return m_data[0]; } [[nodiscard]] constexpr char back() const { Q_ASSERT(!empty()); return m_data[m_size - 1]; } + [[nodiscard]] constexpr Q_IMPLICIT operator std::string_view() const noexcept + { return std::string_view(m_data, size_t(m_size)); } + // // Qt compatibility API: // @@ -299,20 +319,51 @@ public: [[nodiscard]] constexpr char first() const { return front(); } [[nodiscard]] constexpr char last() const { return back(); } - friend inline bool operator==(QByteArrayView lhs, QByteArrayView rhs) noexcept - { return lhs.size() == rhs.size() && (!lhs.size() || memcmp(lhs.data(), rhs.data(), lhs.size()) == 0); } - friend inline bool operator!=(QByteArrayView lhs, QByteArrayView rhs) noexcept - { return !(lhs == rhs); } - friend inline bool operator< (QByteArrayView lhs, QByteArrayView rhs) noexcept - { return QtPrivate::compareMemory(lhs, rhs) < 0; } - friend inline bool operator<=(QByteArrayView lhs, QByteArrayView rhs) noexcept - { return QtPrivate::compareMemory(lhs, rhs) <= 0; } - friend inline bool operator> (QByteArrayView lhs, QByteArrayView rhs) noexcept - { return !(lhs <= rhs); } - friend inline bool operator>=(QByteArrayView lhs, QByteArrayView rhs) noexcept - { return !(lhs < rhs); } - private: + Q_ALWAYS_INLINE constexpr void verify([[maybe_unused]] qsizetype pos = 0, + [[maybe_unused]] qsizetype n = 1) const + { + Q_ASSERT(pos >= 0); + Q_ASSERT(pos <= size()); + Q_ASSERT(n >= 0); + Q_ASSERT(n <= size() - pos); + } + + friend bool + comparesEqual(const QByteArrayView &lhs, const QByteArrayView &rhs) noexcept + { + return lhs.size() == rhs.size() + && (!lhs.size() || memcmp(lhs.data(), rhs.data(), lhs.size()) == 0); + } + friend Qt::strong_ordering + compareThreeWay(const QByteArrayView &lhs, const QByteArrayView &rhs) noexcept + { + const int res = QtPrivate::compareMemory(lhs, rhs); + return Qt::compareThreeWay(res, 0); + } + Q_DECLARE_STRONGLY_ORDERED(QByteArrayView) + + friend bool comparesEqual(const QByteArrayView &lhs, const char *rhs) noexcept + { return comparesEqual(lhs, QByteArrayView(rhs)); } + friend Qt::strong_ordering + compareThreeWay(const QByteArrayView &lhs, const char *rhs) noexcept + { return compareThreeWay(lhs, QByteArrayView(rhs)); } + Q_DECLARE_STRONGLY_ORDERED(QByteArrayView, const char *) + + // defined in qstring.cpp + friend Q_CORE_EXPORT bool + comparesEqual(const QByteArrayView &lhs, const QChar &rhs) noexcept; + friend Q_CORE_EXPORT Qt::strong_ordering + compareThreeWay(const QByteArrayView &lhs, const QChar &rhs) noexcept; + friend Q_CORE_EXPORT bool + comparesEqual(const QByteArrayView &lhs, char16_t rhs) noexcept; + friend Q_CORE_EXPORT Qt::strong_ordering + compareThreeWay(const QByteArrayView &lhs, char16_t rhs) noexcept; +#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) + Q_DECLARE_STRONGLY_ORDERED(QByteArrayView, QChar, QT_ASCII_CAST_WARN) + Q_DECLARE_STRONGLY_ORDERED(QByteArrayView, char16_t, QT_ASCII_CAST_WARN) +#endif // !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) + qsizetype m_size; const storage_type *m_data; }; @@ -321,7 +372,7 @@ Q_DECLARE_TYPEINFO(QByteArrayView, Q_PRIMITIVE_TYPE); template<typename QByteArrayLike, std::enable_if_t<std::is_same_v<QByteArrayLike, QByteArray>, bool> = true> [[nodiscard]] inline QByteArrayView qToByteArrayViewIgnoringNull(const QByteArrayLike &b) noexcept -{ return QByteArrayView(b.data(), b.size()); } +{ return QByteArrayView(b.begin(), b.size()); } inline int QByteArrayView::compare(QByteArrayView a, Qt::CaseSensitivity cs) const noexcept { @@ -336,6 +387,20 @@ inline quint16 qChecksum(const char *s, qsizetype len, { return qChecksum(QByteArrayView(s, len), standard); } #endif +qsizetype QtPrivate::findByteArray(QByteArrayView haystack, qsizetype from, char needle) noexcept +{ + if (from < 0) + from = qMax(from + haystack.size(), qsizetype(0)); + if (from < haystack.size()) { + const char *const b = haystack.data(); + if (const auto n = static_cast<const char *>( + memchr(b + from, needle, static_cast<size_t>(haystack.size() - from)))) { + return n - b; + } + } + return -1; +} + QT_END_NAMESPACE #endif // QBYTEARRAYVIEW_H |