File indexing completed on 2026-05-17 08:20:07
0001
0002
0003
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
0048
0049 IsCompatibleByteArrayPointer<decltype(std::data(std::declval<const T &>()))>,
0050
0051 std::is_convertible<decltype(std::size(std::declval<const T &>())), qsizetype>,
0052
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
0060 std::negation<std::is_same<std::decay_t<T>, QByteArray>>,
0061
0062
0063 std::negation<std::is_array<T>>,
0064
0065
0066 std::negation<std::is_same<std::decay_t<T>, QByteArrayView>>>>> : std::true_type {};
0067
0068
0069 template <typename Char>
0070 static constexpr qsizetype lengthHelperPointer(const Char *data) noexcept
0071 {
0072
0073
0074
0075 qsizetype i = 0;
0076 if (!data)
0077 return i;
0078 while (data[i] != Char(0))
0079 ++i;
0080 return i;
0081 }
0082
0083 }
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() noexcept
0142 : m_size(0), m_data(nullptr) {}
0143 constexpr QByteArrayView(std::nullptr_t) noexcept
0144 : QByteArrayView() {}
0145
0146 template <typename Byte, if_compatible_byte<Byte> = true>
0147 constexpr QByteArrayView(const Byte *data, qsizetype len)
0148 : m_size((Q_ASSERT(len >= 0), Q_ASSERT(data || !len), len)),
0149 m_data(castHelper(data)) {}
0150
0151 template <typename Byte, if_compatible_byte<Byte> = true>
0152 constexpr QByteArrayView(const Byte *first, const Byte *last)
0153 : QByteArrayView(first, last - first) {}
0154
0155 #ifdef Q_QDOC
0156 template <typename Byte>
0157 constexpr QByteArrayView(const Byte *data) noexcept;
0158 #else
0159 template <typename Pointer, if_compatible_pointer<Pointer> = true>
0160 constexpr QByteArrayView(const Pointer &data) noexcept
0161 : QByteArrayView(
0162 data, data ? QtPrivate::lengthHelperPointer(data) : 0) {}
0163 #endif
0164
0165 #ifdef Q_QDOC
0166 QByteArrayView(const QByteArray &data) noexcept;
0167 #else
0168 template <typename ByteArray, if_compatible_qbytearray_like<ByteArray> = true>
0169 QByteArrayView(const ByteArray &ba) noexcept
0170 : QByteArrayView{ba.begin(), ba.size()} {}
0171 #endif
0172
0173 template <typename Container, if_compatible_container<Container> = true>
0174 constexpr QByteArrayView(const Container &c) noexcept
0175 : QByteArrayView(std::data(c), lengthHelperContainer(c)) {}
0176 template <size_t Size>
0177 constexpr QByteArrayView(const char (&data)[Size]) noexcept
0178 : QByteArrayView(data, lengthHelperCharArray(data, Size)) {}
0179
0180 template <typename Byte, if_compatible_byte<Byte> = true>
0181 constexpr QByteArrayView(const Byte (&data)[]) noexcept
0182 : QByteArrayView(&*data) {}
0183
0184 #ifdef Q_QDOC
0185 template <typename Byte, size_t Size>
0186 #else
0187 template <typename Byte, size_t Size, if_compatible_byte<Byte> = true>
0188 #endif
0189 [[nodiscard]] constexpr static QByteArrayView fromArray(const Byte (&data)[Size]) noexcept
0190 { return QByteArrayView(data, Size); }
0191 [[nodiscard]] inline QByteArray toByteArray() const;
0192
0193 [[nodiscard]] constexpr qsizetype size() const noexcept { return m_size; }
0194 [[nodiscard]] constexpr const_pointer data() const noexcept { return m_data; }
0195 [[nodiscard]] constexpr const_pointer constData() const noexcept { return data(); }
0196
0197 [[nodiscard]] constexpr char operator[](qsizetype n) const
0198 { verify(n, 1); return m_data[n]; }
0199
0200
0201
0202
0203 [[nodiscard]] constexpr char at(qsizetype n) const { return (*this)[n]; }
0204
0205 [[nodiscard]] constexpr QByteArrayView first(qsizetype n) const
0206 { verify(0, n); return sliced(0, n); }
0207 [[nodiscard]] constexpr QByteArrayView last(qsizetype n) const
0208 { verify(0, n); return sliced(size() - n, n); }
0209 [[nodiscard]] constexpr QByteArrayView sliced(qsizetype pos) const
0210 { verify(pos, 0); return QByteArrayView(data() + pos, size() - pos); }
0211 [[nodiscard]] constexpr QByteArrayView sliced(qsizetype pos, qsizetype n) const
0212 { verify(pos, n); return QByteArrayView(data() + pos, n); }
0213
0214 constexpr QByteArrayView &slice(qsizetype pos)
0215 { *this = sliced(pos); return *this; }
0216 constexpr QByteArrayView &slice(qsizetype pos, qsizetype n)
0217 { *this = sliced(pos, n); return *this; }
0218
0219 [[nodiscard]] constexpr QByteArrayView chopped(qsizetype len) const
0220 { verify(0, len); return sliced(0, size() - len); }
0221
0222 [[nodiscard]] constexpr QByteArrayView left(qsizetype n) const
0223 { if (n < 0 || n > size()) n = size(); return QByteArrayView(data(), n); }
0224 [[nodiscard]] constexpr QByteArrayView right(qsizetype n) const
0225 { if (n < 0 || n > size()) n = size(); if (n < 0) n = 0; return QByteArrayView(data() + size() - n, n); }
0226 [[nodiscard]] constexpr QByteArrayView mid(qsizetype pos, qsizetype n = -1) const
0227 {
0228 using namespace QtPrivate;
0229 auto result = QContainerImplHelper::mid(size(), &pos, &n);
0230 return result == QContainerImplHelper::Null ? QByteArrayView()
0231 : QByteArrayView(m_data + pos, n);
0232 }
0233
0234 constexpr void truncate(qsizetype n)
0235 { verify(0, n); m_size = n; }
0236 constexpr void chop(qsizetype n)
0237 { verify(0, n); m_size -= n; }
0238
0239
0240 [[nodiscard]] QByteArrayView trimmed() const noexcept
0241 { return QtPrivate::trimmed(*this); }
0242 [[nodiscard]] short toShort(bool *ok = nullptr, int base = 10) const
0243 { return QtPrivate::toIntegral<short>(*this, ok, base); }
0244 [[nodiscard]] ushort toUShort(bool *ok = nullptr, int base = 10) const
0245 { return QtPrivate::toIntegral<ushort>(*this, ok, base); }
0246 [[nodiscard]] int toInt(bool *ok = nullptr, int base = 10) const
0247 { return QtPrivate::toIntegral<int>(*this, ok, base); }
0248 [[nodiscard]] uint toUInt(bool *ok = nullptr, int base = 10) const
0249 { return QtPrivate::toIntegral<uint>(*this, ok, base); }
0250 [[nodiscard]] long toLong(bool *ok = nullptr, int base = 10) const
0251 { return QtPrivate::toIntegral<long>(*this, ok, base); }
0252 [[nodiscard]] ulong toULong(bool *ok = nullptr, int base = 10) const
0253 { return QtPrivate::toIntegral<ulong>(*this, ok, base); }
0254 [[nodiscard]] qlonglong toLongLong(bool *ok = nullptr, int base = 10) const
0255 { return QtPrivate::toIntegral<qlonglong>(*this, ok, base); }
0256 [[nodiscard]] qulonglong toULongLong(bool *ok = nullptr, int base = 10) const
0257 { return QtPrivate::toIntegral<qulonglong>(*this, ok, base); }
0258 [[nodiscard]] float toFloat(bool *ok = nullptr) const
0259 {
0260 const auto r = QtPrivate::toFloat(*this);
0261 if (ok)
0262 *ok = bool(r);
0263 return r.value_or(0.0f);
0264 }
0265 [[nodiscard]] double toDouble(bool *ok = nullptr) const
0266 {
0267 const auto r = QtPrivate::toDouble(*this);
0268 if (ok)
0269 *ok = bool(r);
0270 return r.value_or(0.0);
0271 }
0272
0273 [[nodiscard]] bool startsWith(QByteArrayView other) const noexcept
0274 { return QtPrivate::startsWith(*this, other); }
0275 [[nodiscard]] constexpr bool startsWith(char c) const noexcept
0276 { return !empty() && front() == c; }
0277
0278 [[nodiscard]] bool endsWith(QByteArrayView other) const noexcept
0279 { return QtPrivate::endsWith(*this, other); }
0280 [[nodiscard]] constexpr bool endsWith(char c) const noexcept
0281 { return !empty() && back() == c; }
0282
0283 [[nodiscard]] qsizetype indexOf(QByteArrayView a, qsizetype from = 0) const noexcept
0284 { return QtPrivate::findByteArray(*this, from, a); }
0285 [[nodiscard]] qsizetype indexOf(char ch, qsizetype from = 0) const noexcept
0286 { return QtPrivate::findByteArray(*this, from, ch); }
0287
0288 [[nodiscard]] bool contains(QByteArrayView a) const noexcept
0289 { return indexOf(a) != qsizetype(-1); }
0290 [[nodiscard]] bool contains(char c) const noexcept
0291 { return indexOf(c) != qsizetype(-1); }
0292
0293 [[nodiscard]] qsizetype lastIndexOf(QByteArrayView a) const noexcept
0294 { return lastIndexOf(a, size()); }
0295 [[nodiscard]] qsizetype lastIndexOf(QByteArrayView a, qsizetype from) const noexcept
0296 { return QtPrivate::lastIndexOf(*this, from, a); }
0297 [[nodiscard]] qsizetype lastIndexOf(char ch, qsizetype from = -1) const noexcept
0298 { return QtPrivate::lastIndexOf(*this, from, ch); }
0299
0300 [[nodiscard]] qsizetype count(QByteArrayView a) const noexcept
0301 { return QtPrivate::count(*this, a); }
0302 [[nodiscard]] qsizetype count(char ch) const noexcept
0303 { return QtPrivate::count(*this, QByteArrayView(&ch, 1)); }
0304
0305 inline int compare(QByteArrayView a, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
0306
0307 [[nodiscard]] inline bool isValidUtf8() const noexcept { return QtPrivate::isValidUtf8(*this); }
0308
0309
0310
0311
0312 [[nodiscard]] constexpr const_iterator begin() const noexcept { return data(); }
0313 [[nodiscard]] constexpr const_iterator end() const noexcept { return data() + size(); }
0314 [[nodiscard]] constexpr const_iterator cbegin() const noexcept { return begin(); }
0315 [[nodiscard]] constexpr const_iterator cend() const noexcept { return end(); }
0316 [[nodiscard]] constexpr const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); }
0317 [[nodiscard]] constexpr const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); }
0318 [[nodiscard]] constexpr const_reverse_iterator crbegin() const noexcept { return rbegin(); }
0319 [[nodiscard]] constexpr const_reverse_iterator crend() const noexcept { return rend(); }
0320
0321 [[nodiscard]] constexpr bool empty() const noexcept { return size() == 0; }
0322 [[nodiscard]] constexpr char front() const { Q_ASSERT(!empty()); return m_data[0]; }
0323 [[nodiscard]] constexpr char back() const { Q_ASSERT(!empty()); return m_data[m_size - 1]; }
0324
0325 [[nodiscard]] constexpr Q_IMPLICIT operator std::string_view() const noexcept
0326 { return std::string_view(m_data, size_t(m_size)); }
0327
0328 [[nodiscard]] constexpr qsizetype max_size() const noexcept { return maxSize(); }
0329
0330
0331
0332
0333 [[nodiscard]] constexpr bool isNull() const noexcept { return !m_data; }
0334 [[nodiscard]] constexpr bool isEmpty() const noexcept { return empty(); }
0335 [[nodiscard]] constexpr qsizetype length() const noexcept
0336 { return size(); }
0337 [[nodiscard]] constexpr char first() const { return front(); }
0338 [[nodiscard]] constexpr char last() const { return back(); }
0339
0340 [[nodiscard]] static constexpr qsizetype maxSize() noexcept
0341 {
0342
0343 return QtPrivate::MaxAllocSize - 1;
0344 }
0345
0346 private:
0347 Q_ALWAYS_INLINE constexpr void verify([[maybe_unused]] qsizetype pos = 0,
0348 [[maybe_unused]] qsizetype n = 1) const
0349 {
0350 Q_ASSERT(pos >= 0);
0351 Q_ASSERT(pos <= size());
0352 Q_ASSERT(n >= 0);
0353 Q_ASSERT(n <= size() - pos);
0354 }
0355
0356 friend bool
0357 comparesEqual(const QByteArrayView &lhs, const QByteArrayView &rhs) noexcept
0358 {
0359 return lhs.size() == rhs.size()
0360 && (!lhs.size() || memcmp(lhs.data(), rhs.data(), lhs.size()) == 0);
0361 }
0362 friend Qt::strong_ordering
0363 compareThreeWay(const QByteArrayView &lhs, const QByteArrayView &rhs) noexcept
0364 {
0365 const int res = QtPrivate::compareMemory(lhs, rhs);
0366 return Qt::compareThreeWay(res, 0);
0367 }
0368 Q_DECLARE_STRONGLY_ORDERED(QByteArrayView)
0369
0370
0371 friend Q_CORE_EXPORT bool
0372 comparesEqual(const QByteArrayView &lhs, const QChar &rhs) noexcept;
0373 friend Q_CORE_EXPORT Qt::strong_ordering
0374 compareThreeWay(const QByteArrayView &lhs, const QChar &rhs) noexcept;
0375 friend Q_CORE_EXPORT bool
0376 comparesEqual(const QByteArrayView &lhs, char16_t rhs) noexcept;
0377 friend Q_CORE_EXPORT Qt::strong_ordering
0378 compareThreeWay(const QByteArrayView &lhs, char16_t rhs) noexcept;
0379 #if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
0380 Q_DECLARE_STRONGLY_ORDERED(QByteArrayView, QChar, QT_ASCII_CAST_WARN)
0381 Q_DECLARE_STRONGLY_ORDERED(QByteArrayView, char16_t, QT_ASCII_CAST_WARN)
0382 #endif
0383
0384 qsizetype m_size;
0385 const storage_type *m_data;
0386 };
0387 Q_DECLARE_TYPEINFO(QByteArrayView, Q_PRIMITIVE_TYPE);
0388
0389 template<typename QByteArrayLike,
0390 std::enable_if_t<std::is_same_v<QByteArrayLike, QByteArray>, bool> = true>
0391 [[nodiscard]] inline QByteArrayView qToByteArrayViewIgnoringNull(const QByteArrayLike &b) noexcept
0392 { return QByteArrayView(b.begin(), b.size()); }
0393
0394 inline int QByteArrayView::compare(QByteArrayView a, Qt::CaseSensitivity cs) const noexcept
0395 {
0396 return cs == Qt::CaseSensitive ? QtPrivate::compareMemory(*this, a) :
0397 qstrnicmp(data(), size(), a.data(), a.size());
0398 }
0399
0400 #if QT_DEPRECATED_SINCE(6, 0)
0401 QT_DEPRECATED_VERSION_X_6_0("Use the QByteArrayView overload.")
0402 inline quint16 qChecksum(const char *s, qsizetype len,
0403 Qt::ChecksumType standard = Qt::ChecksumIso3309)
0404 { return qChecksum(QByteArrayView(s, len), standard); }
0405 #endif
0406
0407 qsizetype QtPrivate::findByteArray(QByteArrayView haystack, qsizetype from, char needle) noexcept
0408 {
0409 if (from < -haystack.size())
0410 return -1;
0411 if (from < 0)
0412 from = from + haystack.size();
0413 if (from < haystack.size()) {
0414 const char *const b = haystack.data();
0415 if (const auto n = static_cast<const char *>(
0416 memchr(b + from, needle, static_cast<size_t>(haystack.size() - from)))) {
0417 return n - b;
0418 }
0419 }
0420 return -1;
0421 }
0422
0423 qsizetype QtPrivate::lastIndexOf(QByteArrayView haystack, qsizetype from, uchar needle) noexcept
0424 {
0425 if (from < 0)
0426 from = qMax(from + haystack.size(), qsizetype(0));
0427 else
0428 from = qMin(from, haystack.size() - 1);
0429
0430 const char *const b = haystack.data();
0431 const void *n = b ? qmemrchr(b, needle, from + 1) : nullptr;
0432 return n ? static_cast<const char *>(n) - b : -1;
0433 }
0434
0435 QT_END_NAMESPACE
0436
0437 #endif