Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-15 09:21:09

0001 // Copyright (C) 2021 The Qt Company Ltd.
0002 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
0003 // Qt-Security score:critical reason:data-parser
0004 #ifndef QBYTEARRAYVIEW_H
0005 #define QBYTEARRAYVIEW_H
0006 
0007 #include <QtCore/qbytearrayalgorithms.h>
0008 #include <QtCore/qcompare.h>
0009 #include <QtCore/qcontainerfwd.h>
0010 #include <QtCore/qstringfwd.h>
0011 #include <QtCore/qarraydata.h>
0012 
0013 #include <string>
0014 #include <string_view>
0015 #include <QtCore/q20type_traits.h>
0016 
0017 QT_BEGIN_NAMESPACE
0018 
0019 namespace QtPrivate {
0020 
0021 template <typename Byte>
0022 struct IsCompatibleByteTypeHelper : std::false_type {};
0023 template <> struct IsCompatibleByteTypeHelper<char> : std::true_type {};
0024 template <> struct IsCompatibleByteTypeHelper<signed char> : std::true_type {};
0025 template <> struct IsCompatibleByteTypeHelper<unsigned char> : std::true_type {};
0026 template <> struct IsCompatibleByteTypeHelper<std::byte> : std::true_type {};
0027 
0028 template <typename Byte>
0029 struct IsCompatibleByteType
0030     : IsCompatibleByteTypeHelper<q20::remove_cvref_t<Byte>> {};
0031 
0032 template <typename Pointer>
0033 struct IsCompatibleByteArrayPointerHelper : std::false_type {};
0034 template <typename Byte>
0035 struct IsCompatibleByteArrayPointerHelper<Byte *>
0036     : IsCompatibleByteType<Byte> {};
0037 template<typename Pointer>
0038 struct IsCompatibleByteArrayPointer
0039     : IsCompatibleByteArrayPointerHelper<q20::remove_cvref_t<Pointer>> {};
0040 
0041 template <typename T, typename Enable = void>
0042 struct IsContainerCompatibleWithQByteArrayView : std::false_type {};
0043 
0044 template <typename T>
0045 struct IsContainerCompatibleWithQByteArrayView<T, std::enable_if_t<
0046         std::conjunction_v<
0047                 // lacking concepts and ranges, we accept any T whose std::data yields a suitable
0048                 // pointer ...
0049                 IsCompatibleByteArrayPointer<decltype(std::data(std::declval<const T &>()))>,
0050                 // ... and that has a suitable size ...
0051                 std::is_convertible<decltype(std::size(std::declval<const T &>())), qsizetype>,
0052                 // ... and it's a range as it defines an iterator-like API
0053                 IsCompatibleByteType<typename std::iterator_traits<decltype(
0054                         std::begin(std::declval<const T &>()))>::value_type>,
0055                 std::is_convertible<decltype(std::begin(std::declval<const T &>())
0056                                              != std::end(std::declval<const T &>())),
0057                                     bool>,
0058 
0059                 // This needs to be treated specially due to the empty vs null distinction
0060                 std::negation<std::is_same<std::decay_t<T>, QByteArray>>,
0061 
0062                 // We handle array literals specially for source compat reasons
0063                 std::negation<std::is_array<T>>,
0064 
0065                 // Don't make an accidental copy constructor
0066                 std::negation<std::is_same<std::decay_t<T>, QByteArrayView>>>>> : std::true_type {};
0067 
0068 // Used by QLatin1StringView too
0069 template <typename Char>
0070 static constexpr qsizetype lengthHelperPointer(const Char *data) noexcept
0071 {
0072     // std::char_traits can only be used with one of the regular char types
0073     // (char, char16_t, wchar_t, but not uchar or QChar), so we roll the loop
0074     // out by ourselves.
0075     qsizetype i = 0;
0076     if (!data)
0077         return i;
0078     while (data[i] != Char(0))
0079         ++i;
0080     return i;
0081 }
0082 
0083 } // namespace QtPrivate
0084 
0085 class Q_CORE_EXPORT QByteArrayView
0086 {
0087 public:
0088     typedef char storage_type;
0089     typedef const char value_type;
0090     typedef qptrdiff difference_type;
0091     typedef qsizetype size_type;
0092     typedef value_type &reference;
0093     typedef value_type &const_reference;
0094     typedef value_type *pointer;
0095     typedef value_type *const_pointer;
0096 
0097     typedef pointer iterator;
0098     typedef const_pointer const_iterator;
0099     typedef std::reverse_iterator<iterator> reverse_iterator;
0100     typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
0101 
0102 private:
0103     template <typename Byte>
0104     using if_compatible_byte =
0105             typename std::enable_if_t<QtPrivate::IsCompatibleByteType<Byte>::value, bool>;
0106 
0107     template <typename Pointer>
0108     using if_compatible_pointer =
0109             typename std::enable_if_t<QtPrivate::IsCompatibleByteArrayPointer<Pointer>::value,
0110                                       bool>;
0111 
0112     template <typename T>
0113     using if_compatible_qbytearray_like =
0114             typename std::enable_if_t<std::is_same_v<T, QByteArray>, bool>;
0115 
0116     template <typename T>
0117     using if_compatible_container =
0118             typename std::enable_if_t<QtPrivate::IsContainerCompatibleWithQByteArrayView<T>::value,
0119                                       bool>;
0120 
0121     template <typename Container>
0122     static constexpr qsizetype lengthHelperContainer(const Container &c) noexcept
0123     {
0124         return qsizetype(std::size(c));
0125     }
0126 
0127     static constexpr qsizetype lengthHelperCharArray(const char *data, size_t size) noexcept
0128     {
0129         const auto it = std::char_traits<char>::find(data, size, '\0');
0130         const auto end = it ? it : std::next(data, size);
0131         return qsizetype(std::distance(data, end));
0132     }
0133 
0134     template <typename Byte>
0135     static const storage_type *castHelper(const Byte *data) noexcept
0136     { return reinterpret_cast<const storage_type*>(data); }
0137     static constexpr const storage_type *castHelper(const storage_type *data) noexcept
0138     { return data; }
0139 
0140 public:
0141     constexpr QByteArrayView() = default;
0142     constexpr QByteArrayView(std::nullptr_t) noexcept
0143         : QByteArrayView() {}
0144 
0145     template <typename Byte, if_compatible_byte<Byte> = true>
0146     constexpr QByteArrayView(const Byte *data, qsizetype len)
0147 #if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0) || defined(QT_BOOTSTRAPPED)
0148         : m_data(castHelper(data)),
0149           m_size((Q_ASSERT(len >= 0), Q_ASSERT(data || !len), len)) {}
0150 #else
0151         : m_size((Q_ASSERT(len >= 0), Q_ASSERT(data || !len), len)),
0152           m_data(castHelper(data)) {}
0153 #endif
0154 
0155     template <typename Byte, if_compatible_byte<Byte> = true>
0156     constexpr QByteArrayView(const Byte *first, const Byte *last)
0157         : QByteArrayView(first, last - first) {}
0158 
0159 #ifdef Q_QDOC
0160     template <typename Byte>
0161     constexpr QByteArrayView(const Byte *data) noexcept;
0162 #else
0163     template <typename Pointer, if_compatible_pointer<Pointer> = true>
0164     constexpr QByteArrayView(const Pointer &data) noexcept
0165         : QByteArrayView(
0166               data, data ? QtPrivate::lengthHelperPointer(data) : 0) {}
0167 #endif
0168 
0169 #ifdef Q_QDOC
0170     QByteArrayView(const QByteArray &data) noexcept;
0171 #else
0172     template <typename ByteArray, if_compatible_qbytearray_like<ByteArray> = true>
0173     QByteArrayView(const ByteArray &ba) noexcept
0174         : QByteArrayView{ba.begin(), ba.size()} {}
0175 #endif
0176 
0177     template <typename Container, if_compatible_container<Container> = true>
0178     constexpr QByteArrayView(const Container &c) noexcept
0179         : QByteArrayView(std::data(c), lengthHelperContainer(c)) {}
0180     template <size_t Size>
0181     constexpr QByteArrayView(const char (&data)[Size]) noexcept
0182         : QByteArrayView(data, lengthHelperCharArray(data, Size)) {}
0183 
0184     template <typename Byte, if_compatible_byte<Byte> = true>
0185     constexpr QByteArrayView(const Byte (&data)[]) noexcept
0186         : QByteArrayView(&*data) {} // decay to pointer
0187 
0188 #ifdef Q_QDOC
0189     template <typename Byte, size_t Size>
0190 #else
0191     template <typename Byte, size_t Size, if_compatible_byte<Byte> = true>
0192 #endif
0193     [[nodiscard]] constexpr static QByteArrayView fromArray(const Byte (&data)[Size]) noexcept
0194     { return QByteArrayView(data, Size); }
0195     [[nodiscard]] inline QByteArray toByteArray() const; // defined in qbytearray.h
0196 
0197     [[nodiscard]] constexpr qsizetype size() const noexcept { return m_size; }
0198     [[nodiscard]] constexpr const_pointer data() const noexcept { return m_data; }
0199     [[nodiscard]] constexpr const_pointer constData() const noexcept { return data(); }
0200 
0201     [[nodiscard]] constexpr char operator[](qsizetype n) const
0202     { verify(n, 1); return m_data[n]; }
0203 
0204     //
0205     // QByteArray API
0206     //
0207     [[nodiscard]] constexpr char at(qsizetype n) const { return (*this)[n]; }
0208 
0209     [[nodiscard]] constexpr QByteArrayView first(qsizetype n) const
0210     { verify(0, n); return sliced(0, n); }
0211     [[nodiscard]] constexpr QByteArrayView last(qsizetype n) const
0212     { verify(0, n); return sliced(size() - n, n); }
0213     [[nodiscard]] constexpr QByteArrayView sliced(qsizetype pos) const
0214     { verify(pos, 0); return QByteArrayView(data() + pos, size() - pos); }
0215     [[nodiscard]] constexpr QByteArrayView sliced(qsizetype pos, qsizetype n) const
0216     { verify(pos, n); return QByteArrayView(data() + pos, n); }
0217 
0218      constexpr QByteArrayView &slice(qsizetype pos)
0219     { *this = sliced(pos); return *this; }
0220      constexpr QByteArrayView &slice(qsizetype pos, qsizetype n)
0221     { *this = sliced(pos, n); return *this; }
0222 
0223     [[nodiscard]] constexpr QByteArrayView chopped(qsizetype len) const
0224     { verify(0, len); return sliced(0, size() - len); }
0225 
0226     [[nodiscard]] constexpr QByteArrayView left(qsizetype n) const
0227     { if (n < 0 || n > size()) n = size(); return QByteArrayView(data(), n); }
0228     [[nodiscard]] constexpr QByteArrayView right(qsizetype n) const
0229     { if (n < 0 || n > size()) n = size(); if (n < 0) n = 0; return QByteArrayView(data() + size() - n, n); }
0230     [[nodiscard]] constexpr QByteArrayView mid(qsizetype pos, qsizetype n = -1) const
0231     {
0232         using namespace QtPrivate;
0233         auto result = QContainerImplHelper::mid(size(), &pos, &n);
0234         return result == QContainerImplHelper::Null ? QByteArrayView()
0235                                                     : QByteArrayView(m_data + pos, n);
0236     }
0237 
0238     constexpr void truncate(qsizetype n)
0239     { verify(0, n); m_size = n; }
0240     constexpr void chop(qsizetype n)
0241     { verify(0, n); m_size -= n; }
0242 
0243     // Defined in qbytearray.cpp:
0244     [[nodiscard]] QByteArrayView trimmed() const noexcept
0245     { return QtPrivate::trimmed(*this); }
0246     [[nodiscard]] short toShort(bool *ok = nullptr, int base = 10) const
0247     { return QtPrivate::toIntegral<short>(*this, ok, base); }
0248     [[nodiscard]] ushort toUShort(bool *ok = nullptr, int base = 10) const
0249     { return QtPrivate::toIntegral<ushort>(*this, ok, base); }
0250     [[nodiscard]] int toInt(bool *ok = nullptr, int base = 10) const
0251     { return QtPrivate::toIntegral<int>(*this, ok, base); }
0252     [[nodiscard]] uint toUInt(bool *ok = nullptr, int base = 10) const
0253     { return QtPrivate::toIntegral<uint>(*this, ok, base); }
0254     [[nodiscard]] long toLong(bool *ok = nullptr, int base = 10) const
0255     { return QtPrivate::toIntegral<long>(*this, ok, base); }
0256     [[nodiscard]] ulong toULong(bool *ok = nullptr, int base = 10) const
0257     { return QtPrivate::toIntegral<ulong>(*this, ok, base); }
0258     [[nodiscard]] qlonglong toLongLong(bool *ok = nullptr, int base = 10) const
0259     { return QtPrivate::toIntegral<qlonglong>(*this, ok, base); }
0260     [[nodiscard]] qulonglong toULongLong(bool *ok = nullptr, int base = 10) const
0261     { return QtPrivate::toIntegral<qulonglong>(*this, ok, base); }
0262     [[nodiscard]] float toFloat(bool *ok = nullptr) const
0263     {
0264         const auto r = QtPrivate::toFloat(*this);
0265         if (ok)
0266             *ok = bool(r);
0267         return r.value_or(0.0f);
0268     }
0269     [[nodiscard]] double toDouble(bool *ok = nullptr) const
0270     {
0271         const auto r = QtPrivate::toDouble(*this);
0272         if (ok)
0273             *ok = bool(r);
0274         return r.value_or(0.0);
0275     }
0276 
0277     [[nodiscard]] bool startsWith(QByteArrayView other) const noexcept
0278     { return QtPrivate::startsWith(*this, other); }
0279     [[nodiscard]] constexpr bool startsWith(char c) const noexcept
0280     { return !empty() && front() == c; }
0281 
0282     [[nodiscard]] bool endsWith(QByteArrayView other) const noexcept
0283     { return QtPrivate::endsWith(*this, other); }
0284     [[nodiscard]] constexpr bool endsWith(char c) const noexcept
0285     { return !empty() && back() == c; }
0286 
0287     [[nodiscard]] qsizetype indexOf(QByteArrayView a, qsizetype from = 0) const noexcept
0288     { return QtPrivate::findByteArray(*this, from, a); }
0289     [[nodiscard]] qsizetype indexOf(char ch, qsizetype from = 0) const noexcept
0290     { return QtPrivate::findByteArray(*this, from, ch); }
0291 
0292     [[nodiscard]] bool contains(QByteArrayView a) const noexcept
0293     { return indexOf(a) != qsizetype(-1); }
0294     [[nodiscard]] bool contains(char c) const noexcept
0295     { return indexOf(c) != qsizetype(-1); }
0296 
0297     [[nodiscard]] qsizetype lastIndexOf(QByteArrayView a) const noexcept
0298     { return lastIndexOf(a, size()); }
0299     [[nodiscard]] qsizetype lastIndexOf(QByteArrayView a, qsizetype from) const noexcept
0300     { return QtPrivate::lastIndexOf(*this, from, a); }
0301     [[nodiscard]] qsizetype lastIndexOf(char ch, qsizetype from = -1) const noexcept
0302     { return QtPrivate::lastIndexOf(*this, from, ch); }
0303 
0304     [[nodiscard]] qsizetype count(QByteArrayView a) const noexcept
0305     { return QtPrivate::count(*this, a); }
0306     [[nodiscard]] qsizetype count(char ch) const noexcept
0307     { return QtPrivate::count(*this, QByteArrayView(&ch, 1)); }
0308 
0309     inline int compare(QByteArrayView a, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
0310 
0311     [[nodiscard]] inline bool isValidUtf8() const noexcept { return QtPrivate::isValidUtf8(*this); }
0312 
0313     //
0314     // STL compatibility API:
0315     //
0316     [[nodiscard]] constexpr const_iterator begin()   const noexcept { return data(); }
0317     [[nodiscard]] constexpr const_iterator end()     const noexcept { return data() + size(); }
0318     [[nodiscard]] constexpr const_iterator cbegin()  const noexcept { return begin(); }
0319     [[nodiscard]] constexpr const_iterator cend()    const noexcept { return end(); }
0320     [[nodiscard]] constexpr const_reverse_iterator rbegin()  const noexcept { return const_reverse_iterator(end()); }
0321     [[nodiscard]] constexpr const_reverse_iterator rend()    const noexcept { return const_reverse_iterator(begin()); }
0322     [[nodiscard]] constexpr const_reverse_iterator crbegin() const noexcept { return rbegin(); }
0323     [[nodiscard]] constexpr const_reverse_iterator crend()   const noexcept { return rend(); }
0324 
0325     [[nodiscard]] constexpr bool empty() const noexcept { return size() == 0; }
0326     [[nodiscard]] constexpr char front() const { Q_ASSERT(!empty()); return m_data[0]; }
0327     [[nodiscard]] constexpr char back()  const { Q_ASSERT(!empty()); return m_data[m_size - 1]; }
0328 
0329     [[nodiscard]] constexpr Q_IMPLICIT operator std::string_view() const noexcept
0330     { return std::string_view(m_data, size_t(m_size)); }
0331 
0332     [[nodiscard]] constexpr qsizetype max_size() const noexcept { return maxSize(); }
0333 
0334     //
0335     // Qt compatibility API:
0336     //
0337     [[nodiscard]] constexpr bool isNull() const noexcept { return !m_data; }
0338     [[nodiscard]] constexpr bool isEmpty() const noexcept { return empty(); }
0339     [[nodiscard]] constexpr qsizetype length() const noexcept
0340     { return size(); }
0341     [[nodiscard]] constexpr char first() const { return front(); }
0342     [[nodiscard]] constexpr char last()  const { return back(); }
0343 
0344     [[nodiscard]] static constexpr qsizetype maxSize() noexcept
0345     {
0346         // -1 to deal with the pointer one-past-the-end;
0347         return QtPrivate::MaxAllocSize - 1;
0348     }
0349 
0350 private:
0351     Q_ALWAYS_INLINE constexpr void verify([[maybe_unused]] qsizetype pos = 0,
0352                                           [[maybe_unused]] qsizetype n = 1) const
0353     {
0354         Q_ASSERT(pos >= 0);
0355         Q_ASSERT(pos <= size());
0356         Q_ASSERT(n >= 0);
0357         Q_ASSERT(n <= size() - pos);
0358     }
0359 
0360     friend bool
0361     comparesEqual(const QByteArrayView &lhs, const QByteArrayView &rhs) noexcept
0362     {
0363         return lhs.size() == rhs.size()
0364                 && (!lhs.size() || memcmp(lhs.data(), rhs.data(), lhs.size()) == 0);
0365     }
0366     friend Qt::strong_ordering
0367     compareThreeWay(const QByteArrayView &lhs, const QByteArrayView &rhs) noexcept
0368     {
0369         const int res = QtPrivate::compareMemory(lhs, rhs);
0370         return Qt::compareThreeWay(res, 0);
0371     }
0372     Q_DECLARE_STRONGLY_ORDERED(QByteArrayView)
0373 
0374     // defined in qstring.cpp
0375     friend Q_CORE_EXPORT bool
0376     comparesEqual(const QByteArrayView &lhs, const QChar &rhs) noexcept;
0377     friend Q_CORE_EXPORT Qt::strong_ordering
0378     compareThreeWay(const QByteArrayView &lhs, const QChar &rhs) noexcept;
0379     friend Q_CORE_EXPORT bool
0380     comparesEqual(const QByteArrayView &lhs, char16_t rhs) noexcept;
0381     friend Q_CORE_EXPORT Qt::strong_ordering
0382     compareThreeWay(const QByteArrayView &lhs, char16_t rhs) noexcept;
0383 #if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
0384     Q_DECLARE_STRONGLY_ORDERED(QByteArrayView, QChar, QT_ASCII_CAST_WARN)
0385     Q_DECLARE_STRONGLY_ORDERED(QByteArrayView, char16_t, QT_ASCII_CAST_WARN)
0386 #endif // !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
0387 
0388 #if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0) || defined(QT_BOOTSTRAPPED)
0389     const storage_type *m_data = nullptr;
0390     qsizetype m_size = 0;
0391 #else
0392     qsizetype m_size = 0;
0393     const storage_type *m_data = nullptr;
0394 #endif
0395 };
0396 Q_DECLARE_TYPEINFO(QByteArrayView, Q_PRIMITIVE_TYPE);
0397 
0398 template<typename QByteArrayLike,
0399          std::enable_if_t<std::is_same_v<QByteArrayLike, QByteArray>, bool> = true>
0400 [[nodiscard]] inline QByteArrayView qToByteArrayViewIgnoringNull(const QByteArrayLike &b) noexcept
0401 { return QByteArrayView(b.begin(), b.size()); }
0402 
0403 inline int QByteArrayView::compare(QByteArrayView a, Qt::CaseSensitivity cs) const noexcept
0404 {
0405     return cs == Qt::CaseSensitive ? QtPrivate::compareMemory(*this, a) :
0406                                      qstrnicmp(data(), size(), a.data(), a.size());
0407 }
0408 
0409 #if QT_DEPRECATED_SINCE(6, 0)
0410 QT_DEPRECATED_VERSION_X_6_0("Use the QByteArrayView overload.")
0411 inline quint16 qChecksum(const char *s, qsizetype len,
0412                          Qt::ChecksumType standard = Qt::ChecksumIso3309)
0413 { return qChecksum(QByteArrayView(s, len), standard); }
0414 #endif
0415 
0416 qsizetype QtPrivate::findByteArray(QByteArrayView haystack, qsizetype from, char needle) noexcept
0417 {
0418     if (from < -haystack.size()) // from < 0 && abs(from) > haystack.size(), avoiding overflow
0419         return -1;
0420     if (from < 0)
0421         from = from + haystack.size();
0422     if (from < haystack.size()) {
0423         const char *const b = haystack.data();
0424         if (const auto n = static_cast<const char *>(
0425                     memchr(b + from, needle, static_cast<size_t>(haystack.size() - from)))) {
0426             return n - b;
0427         }
0428     }
0429     return -1;
0430 }
0431 
0432 qsizetype QtPrivate::lastIndexOf(QByteArrayView haystack, qsizetype from, uchar needle) noexcept
0433 {
0434     if (from < 0)
0435         from = qMax(from + haystack.size(), qsizetype(0));
0436     else
0437         from = qMin(from, haystack.size() - 1);
0438 
0439     const char *const b = haystack.data();
0440     const void *n = b ? qmemrchr(b, needle, from + 1) : nullptr;
0441     return n ? static_cast<const char *>(n) - b : -1;
0442 }
0443 
0444 QT_END_NAMESPACE
0445 
0446 #endif // QBYTEARRAYVIEW_H