Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qbytearrayview.h
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:critical reason:data-parser
4#ifndef QBYTEARRAYVIEW_H
5#define QBYTEARRAYVIEW_H
6
7#include <QtCore/qbytearrayalgorithms.h>
8#include <QtCore/qchar.h>
9#include <QtCore/qcompare.h>
10#include <QtCore/qcontainerfwd.h>
11#include <QtCore/qstringfwd.h>
12#include <QtCore/qarraydata.h>
13
14#include <string>
15#include <string_view>
16#include <QtCore/q20type_traits.h>
17
19
20namespace QtPrivate {
21
22template <typename Byte>
24template <> struct IsCompatibleByteTypeHelper<char> : std::true_type {};
25template <> struct IsCompatibleByteTypeHelper<signed char> : std::true_type {};
26template <> struct IsCompatibleByteTypeHelper<unsigned char> : std::true_type {};
27template <> struct IsCompatibleByteTypeHelper<std::byte> : std::true_type {};
28
29template <typename Byte>
32
33template <typename Pointer>
35template <typename Byte>
38template<typename Pointer>
41
42template <typename T, typename Enable = void>
44
45template <typename T>
48 // lacking concepts and ranges, we accept any T whose std::data yields a suitable
49 // pointer ...
50 IsCompatibleByteArrayPointer<decltype(std::data(std::declval<const T &>()))>,
51 // ... and that has a suitable size ...
52 std::is_convertible<decltype(std::size(std::declval<const T &>())), qsizetype>,
53 // ... and it's a range as it defines an iterator-like API
54 IsCompatibleByteType<typename std::iterator_traits<decltype(
55 std::begin(std::declval<const T &>()))>::value_type>,
56 std::is_convertible<decltype(std::begin(std::declval<const T &>())
57 != std::end(std::declval<const T &>())),
58 bool>,
59
60 // This needs to be treated specially due to the empty vs null distinction
62
63 // We handle array literals specially for source compat reasons
65
66 // Don't make an accidental copy constructor
68
69// Used by QLatin1StringView too
70template <typename Char>
71static constexpr qsizetype lengthHelperPointer(const Char *data) noexcept
72{
73 // std::char_traits can only be used with one of the regular char types
74 // (char, char16_t, wchar_t, but not uchar or QChar), so we roll the loop
75 // out by ourselves.
76 qsizetype i = 0;
77 if (!data)
78 return i;
79 while (data[i] != Char(0))
80 ++i;
81 return i;
82}
83
84} // namespace QtPrivate
85
86class Q_CORE_EXPORT QByteArrayView
87{
88public:
89 typedef char storage_type;
90 typedef const char value_type;
91 typedef qptrdiff difference_type;
92 typedef qsizetype size_type;
93 typedef value_type &reference;
94 typedef value_type &const_reference;
95 typedef value_type *pointer;
96 typedef value_type *const_pointer;
97
98 typedef pointer iterator;
99 typedef const_pointer const_iterator;
100 typedef std::reverse_iterator<iterator> reverse_iterator;
101 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
102
103private:
104 template <typename Byte>
105 using if_compatible_byte =
106 typename std::enable_if_t<QtPrivate::IsCompatibleByteType<Byte>::value, bool>;
107
108 template <typename Pointer>
109 using if_compatible_pointer =
110 typename std::enable_if_t<QtPrivate::IsCompatibleByteArrayPointer<Pointer>::value,
111 bool>;
112
113 template <typename T>
114 using if_compatible_qbytearray_like =
115 typename std::enable_if_t<std::is_same_v<T, QByteArray>, bool>;
116
117 template <typename T>
118 using if_compatible_container =
119 typename std::enable_if_t<QtPrivate::IsContainerCompatibleWithQByteArrayView<T>::value,
120 bool>;
121
122 template <typename Container>
123 static constexpr qsizetype lengthHelperContainer(const Container &c) noexcept
124 {
125 return qsizetype(std::size(c));
126 }
127
128 static constexpr qsizetype lengthHelperCharArray(const char *data, size_t size) noexcept
129 {
130 const auto it = std::char_traits<char>::find(data, size, '\0');
131 const auto end = it ? it : std::next(data, size);
132 return qsizetype(std::distance(data, end));
133 }
134
135 template <typename Byte>
136 static const storage_type *castHelper(const Byte *data) noexcept
137 { return reinterpret_cast<const storage_type*>(data); }
138 static constexpr const storage_type *castHelper(const storage_type *data) noexcept
139 { return data; }
140
141public:
142 constexpr QByteArrayView() = default;
143 constexpr QByteArrayView(std::nullptr_t) noexcept
144 : QByteArrayView() {}
145
146 template <typename Byte, if_compatible_byte<Byte> = true>
147 constexpr QByteArrayView(const Byte *data, qsizetype len)
148#if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0) || defined(QT_BOOTSTRAPPED)
149 : m_data(castHelper(data)),
150 m_size((Q_ASSERT(len >= 0), Q_ASSERT(data || !len), len)) {}
151#else
152 : m_size((Q_ASSERT(len >= 0), Q_ASSERT(data || !len), len)),
153 m_data(castHelper(data)) {}
154#endif
155
156 template <typename Byte, if_compatible_byte<Byte> = true>
157 constexpr QByteArrayView(const Byte *first, const Byte *last)
158 : QByteArrayView(first, last - first) {}
159
160#ifdef Q_QDOC
161 template <typename Byte>
162 constexpr QByteArrayView(const Byte *data) noexcept;
163#else
164 template <typename Pointer, if_compatible_pointer<Pointer> = true>
165 constexpr QByteArrayView(const Pointer &data) noexcept
166 : QByteArrayView(
167 data, data ? QtPrivate::lengthHelperPointer(data) : 0) {}
168#endif
169
170#ifdef Q_QDOC
171 QByteArrayView(const QByteArray &data) noexcept;
172#else
173 template <typename ByteArray, if_compatible_qbytearray_like<ByteArray> = true>
174 QByteArrayView(const ByteArray &ba) noexcept
175 : QByteArrayView{ba.begin(), ba.size()} {}
176#endif
177
178 template <typename Container, if_compatible_container<Container> = true>
179 constexpr QByteArrayView(const Container &c) noexcept
180 : QByteArrayView(std::data(c), lengthHelperContainer(c)) {}
181 template <size_t Size>
182 constexpr QByteArrayView(const char (&data)[Size]) noexcept
183 : QByteArrayView(data, lengthHelperCharArray(data, Size)) {}
184
185 template <typename Byte, if_compatible_byte<Byte> = true>
186 constexpr QByteArrayView(const Byte (&data)[]) noexcept
187 : QByteArrayView(&*data) {} // decay to pointer
188
189#ifdef Q_QDOC
190 template <typename Byte, size_t Size>
191#else
192 template <typename Byte, size_t Size, if_compatible_byte<Byte> = true>
193#endif
194 [[nodiscard]] constexpr static QByteArrayView fromArray(const Byte (&data)[Size]) noexcept
195 { return QByteArrayView(data, Size); }
196 [[nodiscard]] inline QByteArray toByteArray() const; // defined in qbytearray.h
197
198 [[nodiscard]] constexpr qsizetype size() const noexcept { return m_size; }
199 [[nodiscard]] constexpr const_pointer data() const noexcept { return m_data; }
200 [[nodiscard]] constexpr const_pointer constData() const noexcept { return data(); }
201
202 [[nodiscard]] constexpr char operator[](qsizetype n) const
203 { verify(n, 1); return m_data[n]; }
204
205 //
206 // QByteArray API
207 //
208 [[nodiscard]] constexpr char at(qsizetype n) const { return (*this)[n]; }
209
210 [[nodiscard]] constexpr QByteArrayView first(qsizetype n) const
211 { verify(0, n); return sliced(0, n); }
212 [[nodiscard]] constexpr QByteArrayView last(qsizetype n) const
213 { verify(0, n); return sliced(size() - n, n); }
214 [[nodiscard]] constexpr QByteArrayView sliced(qsizetype pos) const
215 { verify(pos, 0); return QByteArrayView(data() + pos, size() - pos); }
216 [[nodiscard]] constexpr QByteArrayView sliced(qsizetype pos, qsizetype n) const
217 { verify(pos, n); return QByteArrayView(data() + pos, n); }
218
219 constexpr QByteArrayView &slice(qsizetype pos)
220 { *this = sliced(pos); return *this; }
221 constexpr QByteArrayView &slice(qsizetype pos, qsizetype n)
222 { *this = sliced(pos, n); return *this; }
223
224 [[nodiscard]] constexpr QByteArrayView chopped(qsizetype len) const
225 { verify(0, len); return sliced(0, size() - len); }
226
227 [[nodiscard]] constexpr QByteArrayView left(qsizetype n) const
228 { if (n < 0 || n > size()) n = size(); return QByteArrayView(data(), n); }
229 [[nodiscard]] constexpr QByteArrayView right(qsizetype n) const
230 { if (n < 0 || n > size()) n = size(); if (n < 0) n = 0; return QByteArrayView(data() + size() - n, n); }
231 [[nodiscard]] constexpr QByteArrayView mid(qsizetype pos, qsizetype n = -1) const
232 {
233 using namespace QtPrivate;
234 auto result = QContainerImplHelper::mid(size(), &pos, &n);
235 return result == QContainerImplHelper::Null ? QByteArrayView()
236 : QByteArrayView(m_data + pos, n);
237 }
238
239 constexpr void truncate(qsizetype n)
240 { verify(0, n); m_size = n; }
241 constexpr void chop(qsizetype n)
242 { verify(0, n); m_size -= n; }
243
244 // Defined in qbytearray.cpp:
245 [[nodiscard]] QByteArrayView trimmed() const noexcept
246 { return QtPrivate::trimmed(*this); }
247 [[nodiscard]] short toShort(bool *ok = nullptr, int base = 10) const
248 { return QtPrivate::toIntegral<short>(*this, ok, base); }
249 [[nodiscard]] ushort toUShort(bool *ok = nullptr, int base = 10) const
250 { return QtPrivate::toIntegral<ushort>(*this, ok, base); }
251 [[nodiscard]] int toInt(bool *ok = nullptr, int base = 10) const
252 { return QtPrivate::toIntegral<int>(*this, ok, base); }
253 [[nodiscard]] uint toUInt(bool *ok = nullptr, int base = 10) const
254 { return QtPrivate::toIntegral<uint>(*this, ok, base); }
255 [[nodiscard]] long toLong(bool *ok = nullptr, int base = 10) const
256 { return QtPrivate::toIntegral<long>(*this, ok, base); }
257 [[nodiscard]] ulong toULong(bool *ok = nullptr, int base = 10) const
258 { return QtPrivate::toIntegral<ulong>(*this, ok, base); }
259 [[nodiscard]] qlonglong toLongLong(bool *ok = nullptr, int base = 10) const
260 { return QtPrivate::toIntegral<qlonglong>(*this, ok, base); }
261 [[nodiscard]] qulonglong toULongLong(bool *ok = nullptr, int base = 10) const
262 { return QtPrivate::toIntegral<qulonglong>(*this, ok, base); }
263 [[nodiscard]] float toFloat(bool *ok = nullptr) const
264 {
265 const auto r = QtPrivate::toFloat(*this);
266 if (ok)
267 *ok = bool(r);
268 return r.value_or(0.0f);
269 }
270 [[nodiscard]] double toDouble(bool *ok = nullptr) const
271 {
272 const auto r = QtPrivate::toDouble(*this);
273 if (ok)
274 *ok = bool(r);
275 return r.value_or(0.0);
276 }
277
278 [[nodiscard]] bool startsWith(QByteArrayView other) const noexcept
279 { return QtPrivate::startsWith(*this, other); }
280 [[nodiscard]] constexpr bool startsWith(char c) const noexcept
281 { return !empty() && front() == c; }
282
283 [[nodiscard]] bool endsWith(QByteArrayView other) const noexcept
284 { return QtPrivate::endsWith(*this, other); }
285 [[nodiscard]] constexpr bool endsWith(char c) const noexcept
286 { return !empty() && back() == c; }
287
288 [[nodiscard]] qsizetype indexOf(QByteArrayView a, qsizetype from = 0) const noexcept
289 { return QtPrivate::findByteArray(*this, from, a); }
290 [[nodiscard]] qsizetype indexOf(char ch, qsizetype from = 0) const noexcept
291 { return QtPrivate::findByteArray(*this, from, ch); }
292
293 [[nodiscard]] bool contains(QByteArrayView a) const noexcept
294 { return indexOf(a) != qsizetype(-1); }
295 [[nodiscard]] bool contains(char c) const noexcept
296 { return indexOf(c) != qsizetype(-1); }
297
298 [[nodiscard]] qsizetype lastIndexOf(QByteArrayView a) const noexcept
299 { return lastIndexOf(a, size()); }
300 [[nodiscard]] qsizetype lastIndexOf(QByteArrayView a, qsizetype from) const noexcept
301 { return QtPrivate::lastIndexOf(*this, from, a); }
302 [[nodiscard]] qsizetype lastIndexOf(char ch, qsizetype from = -1) const noexcept
303 { return QtPrivate::lastIndexOf(*this, from, ch); }
304
305 [[nodiscard]] qsizetype count(QByteArrayView a) const noexcept
306 { return QtPrivate::count(*this, a); }
307 [[nodiscard]] qsizetype count(char ch) const noexcept
308 { return QtPrivate::count(*this, QByteArrayView(&ch, 1)); }
309
310 inline int compare(QByteArrayView a, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
311
312 [[nodiscard]] inline bool isValidUtf8() const noexcept { return QtPrivate::isValidUtf8(*this); }
313
314 //
315 // STL compatibility API:
316 //
317 [[nodiscard]] constexpr const_iterator begin() const noexcept { return data(); }
318 [[nodiscard]] constexpr const_iterator end() const noexcept { return data() + size(); }
319 [[nodiscard]] constexpr const_iterator cbegin() const noexcept { return begin(); }
320 [[nodiscard]] constexpr const_iterator cend() const noexcept { return end(); }
321 [[nodiscard]] constexpr const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); }
322 [[nodiscard]] constexpr const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); }
323 [[nodiscard]] constexpr const_reverse_iterator crbegin() const noexcept { return rbegin(); }
324 [[nodiscard]] constexpr const_reverse_iterator crend() const noexcept { return rend(); }
325
326 [[nodiscard]] constexpr bool empty() const noexcept { return size() == 0; }
327 [[nodiscard]] constexpr char front() const { Q_ASSERT(!empty()); return m_data[0]; }
328 [[nodiscard]] constexpr char back() const { Q_ASSERT(!empty()); return m_data[m_size - 1]; }
329
330 [[nodiscard]] constexpr Q_IMPLICIT operator std::string_view() const noexcept
331 { return std::string_view(m_data, size_t(m_size)); }
332
333 [[nodiscard]] constexpr qsizetype max_size() const noexcept { return maxSize(); }
334
335 //
336 // Qt compatibility API:
337 //
338 [[nodiscard]] constexpr bool isNull() const noexcept { return !m_data; }
339 [[nodiscard]] constexpr bool isEmpty() const noexcept { return empty(); }
340 [[nodiscard]] constexpr qsizetype length() const noexcept
341 { return size(); }
342 [[nodiscard]] constexpr char first() const { return front(); }
343 [[nodiscard]] constexpr char last() const { return back(); }
344
345 [[nodiscard]] static constexpr qsizetype maxSize() noexcept
346 {
347 // -1 to deal with the pointer one-past-the-end;
348 return QtPrivate::MaxAllocSize - 1;
349 }
350
351private:
352 Q_ALWAYS_INLINE constexpr void verify([[maybe_unused]] qsizetype pos = 0,
353 [[maybe_unused]] qsizetype n = 1) const
354 {
355 Q_ASSERT(pos >= 0);
356 Q_ASSERT(pos <= size());
357 Q_ASSERT(n >= 0);
358 Q_ASSERT(n <= size() - pos);
359 }
360
361 friend bool
362 comparesEqual(const QByteArrayView &lhs, const QByteArrayView &rhs) noexcept
363 {
364 return lhs.size() == rhs.size()
365 && (!lhs.size() || memcmp(lhs.data(), rhs.data(), lhs.size()) == 0);
366 }
367 friend Qt::strong_ordering
368 compareThreeWay(const QByteArrayView &lhs, const QByteArrayView &rhs) noexcept
369 {
370 const int res = QtPrivate::compareMemory(lhs, rhs);
371 return Qt::compareThreeWay(res, 0);
372 }
373 Q_DECLARE_STRONGLY_ORDERED(QByteArrayView)
374
375 // defined in qstring.cpp
376 friend Q_CORE_EXPORT bool
377 comparesEqual(const QByteArrayView &lhs, const QChar &rhs) noexcept;
378 friend Q_CORE_EXPORT Qt::strong_ordering
379 compareThreeWay(const QByteArrayView &lhs, const QChar &rhs) noexcept;
380 friend Q_CORE_EXPORT bool
381 comparesEqual(const QByteArrayView &lhs, char16_t rhs) noexcept;
382 friend Q_CORE_EXPORT Qt::strong_ordering
383 compareThreeWay(const QByteArrayView &lhs, char16_t rhs) noexcept;
384#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
385 Q_DECLARE_STRONGLY_ORDERED(QByteArrayView, QChar, QT_ASCII_CAST_WARN)
386 Q_DECLARE_STRONGLY_ORDERED(QByteArrayView, char16_t, QT_ASCII_CAST_WARN)
387#endif // !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
388
389#if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0) || defined(QT_BOOTSTRAPPED)
390 const storage_type *m_data = nullptr;
391 qsizetype m_size = 0;
392#else
393 qsizetype m_size = 0;
394 const storage_type *m_data = nullptr;
395#endif
396};
397Q_DECLARE_TYPEINFO(QByteArrayView, Q_PRIMITIVE_TYPE);
398
399template<typename QByteArrayLike,
400 std::enable_if_t<std::is_same_v<QByteArrayLike, QByteArray>, bool> = true>
401[[nodiscard]] inline QByteArrayView qToByteArrayViewIgnoringNull(const QByteArrayLike &b) noexcept
402{ return QByteArrayView(b.begin(), b.size()); }
403
404inline int QByteArrayView::compare(QByteArrayView a, Qt::CaseSensitivity cs) const noexcept
405{
406 return cs == Qt::CaseSensitive ? QtPrivate::compareMemory(*this, a) :
407 qstrnicmp(data(), size(), a.data(), a.size());
408}
409
410#if QT_DEPRECATED_SINCE(6, 0)
411QT_DEPRECATED_VERSION_X_6_0("Use the QByteArrayView overload.")
412inline quint16 qChecksum(const char *s, qsizetype len,
413 Qt::ChecksumType standard = Qt::ChecksumIso3309)
414{ return qChecksum(QByteArrayView(s, len), standard); }
415#endif
416
417qsizetype QtPrivate::findByteArray(QByteArrayView haystack, qsizetype from, char needle) noexcept
418{
419 if (from < -haystack.size()) // from < 0 && abs(from) > haystack.size(), avoiding overflow
420 return -1;
421 if (from < 0)
422 from = from + haystack.size();
423 if (from < haystack.size()) {
424 const char *const b = haystack.data();
425 if (const auto n = static_cast<const char *>(
426 memchr(b + from, needle, static_cast<size_t>(haystack.size() - from)))) {
427 return n - b;
428 }
429 }
430 return -1;
431}
432
433qsizetype QtPrivate::lastIndexOf(QByteArrayView haystack, qsizetype from, uchar needle) noexcept
434{
435 if (from < 0)
436 from = qMax(from + haystack.size(), qsizetype(0));
437 else
438 from = qMin(from, haystack.size() - 1);
439
440 const char *const b = haystack.data();
441 const void *n = b ? qmemrchr(b, needle, from + 1) : nullptr;
442 return n ? static_cast<const char *>(n) - b : -1;
443}
444
445QT_END_NAMESPACE
446
447#endif // QBYTEARRAYVIEW_H
\inmodule QtCore\reentrant
Definition qdatastream.h:50
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:177
Combined button and popup list for selecting options.
QString && asString(QString &&s)
Definition qstring.h:1679
constexpr QAnyStringArg qStringLikeToArg(QAnyStringView s) noexcept
Definition qstring.h:1741
const QString & asString(const QString &s)
Definition qstring.h:1678
static constexpr qsizetype lengthHelperPointer(const Char *data) noexcept
std::is_same< Char, char32_t > IsCompatibleChar32TypeHelper
Definition qstring.h:57
constexpr bool isLatin1(QLatin1StringView s) noexcept
Definition qstring.h:77
QString operator""_s(const char16_t *str, size_t size) noexcept
Definition qstring.h:1787
Definition qcompare.h:111
QByteArrayView qToByteArrayViewIgnoringNull(const QByteArrayLike &b) noexcept
Q_DECLARE_TYPEINFO(QByteArrayView, Q_PRIMITIVE_TYPE)
#define __has_builtin(x)
Q_LOGGING_CATEGORY(lcEventDispatcher, "qt.eventdispatcher")
Q_CORE_EXPORT Q_DECL_CONST_FUNCTION unsigned int qt_int_sqrt(unsigned int n)
\inmodule QtCore \title Global Qt Declarations
Definition qglobal.cpp:100
QString operator+(const QString &s1, QChar s2)
Definition qstring.h:1560
QString operator+(QString &&lhs, const QString &rhs)
Definition qstring.h:1558
qsizetype erase_if(QString &s, Predicate pred)
Definition qstring.h:1779
#define QT_UNICODE_LITERAL(str)
Definition qstring.h:1813
QString operator+(QString &&lhs, QChar rhs)
Definition qstring.h:1562
QString operator+(QChar s1, const QString &s2)
Definition qstring.h:1564
QString operator+(const QString &s1, const QString &s2)
Definition qstring.h:1556
qsizetype erase(QString &s, const T &t)
Definition qstring.h:1773
QList< QList< qInternalCallback > > callbacks
Definition qglobal.cpp:129
constexpr QAnyStringArg(QAnyStringView v) noexcept
Definition qstring.h:1725
QAnyStringView string
Definition qstring.h:1723
QLatin1StringView string
Definition qstring.h:1717
constexpr QLatin1StringArg(QLatin1StringView v) noexcept
Definition qstring.h:1719
constexpr QStringViewArg(QStringView v) noexcept
Definition qstring.h:1713