diff options
Diffstat (limited to 'src/corelib/text/qbytearrayview.h')
-rw-r--r-- | src/corelib/text/qbytearrayview.h | 224 |
1 files changed, 136 insertions, 88 deletions
diff --git a/src/corelib/text/qbytearrayview.h b/src/corelib/text/qbytearrayview.h index 11db03c62f..45ebc812cd 100644 --- a/src/corelib/text/qbytearrayview.h +++ b/src/corelib/text/qbytearrayview.h @@ -1,53 +1,19 @@ -/**************************************************************************** -** -** Copyright (C) 2021 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtCore module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #ifndef QBYTEARRAYVIEW_H #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 -class QByteArray; -class QLatin1String; - namespace QtPrivate { template <typename Byte> @@ -60,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 {}; @@ -70,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 {}; @@ -100,6 +64,13 @@ struct IsContainerCompatibleWithQByteArrayView<T, std::enable_if_t< // Don't make an accidental copy constructor std::negation<std::is_same<std::decay_t<T>, QByteArrayView>>>>> : std::true_type {}; +// Used by QLatin1StringView too +template <typename Char> +static constexpr qsizetype lengthHelperPointer(const Char *data) noexcept +{ + return qsizetype(std::char_traits<Char>::length(data)); +} + } // namespace QtPrivate class Q_CORE_EXPORT QByteArrayView @@ -138,12 +109,6 @@ private: typename std::enable_if_t<QtPrivate::IsContainerCompatibleWithQByteArrayView<T>::value, bool>; - template <typename Char> - static constexpr qsizetype lengthHelperPointer(const Char *data) noexcept - { - return qsizetype(std::char_traits<Char>::length(data)); - } - template <typename Container> static constexpr qsizetype lengthHelperContainer(const Container &c) noexcept { @@ -185,7 +150,7 @@ public: template <typename Pointer, if_compatible_pointer<Pointer> = true> constexpr QByteArrayView(const Pointer &data) noexcept : QByteArrayView( - data, data ? lengthHelperPointer(data) : 0) {} + data, data ? QtPrivate::lengthHelperPointer(data) : 0) {} #endif #ifdef Q_QDOC @@ -203,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 @@ -217,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 @@ -225,49 +193,81 @@ 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 { return QtPrivate::trimmed(*this); } - [[nodiscard]] short toShort(bool *ok = nullptr, int base = 10) const; - [[nodiscard]] ushort toUShort(bool *ok = nullptr, int base = 10) const; - [[nodiscard]] int toInt(bool *ok = nullptr, int base = 10) const; - [[nodiscard]] uint toUInt(bool *ok = nullptr, int base = 10) const; - [[nodiscard]] long toLong(bool *ok = nullptr, int base = 10) const; - [[nodiscard]] ulong toULong(bool *ok = nullptr, int base = 10) const; - [[nodiscard]] qlonglong toLongLong(bool *ok = nullptr, int base = 10) const; - [[nodiscard]] qulonglong toULongLong(bool *ok = nullptr, int base = 10) const; - [[nodiscard]] float toFloat(bool *ok = nullptr) const; - [[nodiscard]] double toDouble(bool *ok = nullptr) const; + [[nodiscard]] short toShort(bool *ok = nullptr, int base = 10) const + { return QtPrivate::toIntegral<short>(*this, ok, base); } + [[nodiscard]] ushort toUShort(bool *ok = nullptr, int base = 10) const + { return QtPrivate::toIntegral<ushort>(*this, ok, base); } + [[nodiscard]] int toInt(bool *ok = nullptr, int base = 10) const + { return QtPrivate::toIntegral<int>(*this, ok, base); } + [[nodiscard]] uint toUInt(bool *ok = nullptr, int base = 10) const + { return QtPrivate::toIntegral<uint>(*this, ok, base); } + [[nodiscard]] long toLong(bool *ok = nullptr, int base = 10) const + { return QtPrivate::toIntegral<long>(*this, ok, base); } + [[nodiscard]] ulong toULong(bool *ok = nullptr, int base = 10) const + { return QtPrivate::toIntegral<ulong>(*this, ok, base); } + [[nodiscard]] qlonglong toLongLong(bool *ok = nullptr, int base = 10) const + { return QtPrivate::toIntegral<qlonglong>(*this, ok, base); } + [[nodiscard]] qulonglong toULongLong(bool *ok = nullptr, int base = 10) const + { return QtPrivate::toIntegral<qulonglong>(*this, ok, base); } + [[nodiscard]] float toFloat(bool *ok = nullptr) const + { + const auto r = QtPrivate::toFloat(*this); + if (ok) + *ok = bool(r); + return r.value_or(0.0f); + } + [[nodiscard]] double toDouble(bool *ok = nullptr) const + { + const auto r = QtPrivate::toDouble(*this); + if (ok) + *ok = bool(r); + return r.value_or(0.0); + } [[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); } @@ -279,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); } @@ -306,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: // @@ -316,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() && 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 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; }; @@ -338,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 { @@ -353,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 |