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() noexcept
143 : m_size(0), m_data(nullptr) {}
144 constexpr QByteArrayView(std::nullptr_t) noexcept
145 : QByteArrayView() {}
146
147 template <typename Byte, if_compatible_byte<Byte> = true>
148 constexpr QByteArrayView(const Byte *data, qsizetype len)
149 : m_size((Q_ASSERT(len >= 0), Q_ASSERT(data || !len), len)),
150 m_data(castHelper(data)) {}
151
152 template <typename Byte, if_compatible_byte<Byte> = true>
153 constexpr QByteArrayView(const Byte *first, const Byte *last)
154 : QByteArrayView(first, last - first) {}
155
156#ifdef Q_QDOC
157 template <typename Byte>
158 constexpr QByteArrayView(const Byte *data) noexcept;
159#else
160 template <typename Pointer, if_compatible_pointer<Pointer> = true>
161 constexpr QByteArrayView(const Pointer &data) noexcept
162 : QByteArrayView(
163 data, data ? QtPrivate::lengthHelperPointer(data) : 0) {}
164#endif
165
166#ifdef Q_QDOC
167 QByteArrayView(const QByteArray &data) noexcept;
168#else
169 template <typename ByteArray, if_compatible_qbytearray_like<ByteArray> = true>
170 QByteArrayView(const ByteArray &ba) noexcept
171 : QByteArrayView{ba.begin(), ba.size()} {}
172#endif
173
174 template <typename Container, if_compatible_container<Container> = true>
175 constexpr QByteArrayView(const Container &c) noexcept
176 : QByteArrayView(std::data(c), lengthHelperContainer(c)) {}
177 template <size_t Size>
178 constexpr QByteArrayView(const char (&data)[Size]) noexcept
179 : QByteArrayView(data, lengthHelperCharArray(data, Size)) {}
180
181 template <typename Byte, if_compatible_byte<Byte> = true>
182 constexpr QByteArrayView(const Byte (&data)[]) noexcept
183 : QByteArrayView(&*data) {} // decay to pointer
184
185#ifdef Q_QDOC
186 template <typename Byte, size_t Size>
187#else
188 template <typename Byte, size_t Size, if_compatible_byte<Byte> = true>
189#endif
190 [[nodiscard]] constexpr static QByteArrayView fromArray(const Byte (&data)[Size]) noexcept
191 { return QByteArrayView(data, Size); }
192 [[nodiscard]] inline QByteArray toByteArray() const; // defined in qbytearray.h
193
194 [[nodiscard]] constexpr qsizetype size() const noexcept { return m_size; }
195 [[nodiscard]] constexpr const_pointer data() const noexcept { return m_data; }
196 [[nodiscard]] constexpr const_pointer constData() const noexcept { return data(); }
197
198 [[nodiscard]] constexpr char operator[](qsizetype n) const
199 { verify(n, 1); return m_data[n]; }
200
201 //
202 // QByteArray API
203 //
204 [[nodiscard]] constexpr char at(qsizetype n) const { return (*this)[n]; }
205
206 [[nodiscard]] constexpr QByteArrayView first(qsizetype n) const
207 { verify(0, n); return sliced(0, n); }
208 [[nodiscard]] constexpr QByteArrayView last(qsizetype n) const
209 { verify(0, n); return sliced(size() - n, n); }
210 [[nodiscard]] constexpr QByteArrayView sliced(qsizetype pos) const
211 { verify(pos, 0); return QByteArrayView(data() + pos, size() - pos); }
212 [[nodiscard]] constexpr QByteArrayView sliced(qsizetype pos, qsizetype n) const
213 { verify(pos, n); return QByteArrayView(data() + pos, n); }
214
215 constexpr QByteArrayView &slice(qsizetype pos)
216 { *this = sliced(pos); return *this; }
217 constexpr QByteArrayView &slice(qsizetype pos, qsizetype n)
218 { *this = sliced(pos, n); return *this; }
219
220 [[nodiscard]] constexpr QByteArrayView chopped(qsizetype len) const
221 { verify(0, len); return sliced(0, size() - len); }
222
223 [[nodiscard]] constexpr QByteArrayView left(qsizetype n) const
224 { if (n < 0 || n > size()) n = size(); return QByteArrayView(data(), n); }
225 [[nodiscard]] constexpr QByteArrayView right(qsizetype n) const
226 { if (n < 0 || n > size()) n = size(); if (n < 0) n = 0; return QByteArrayView(data() + size() - n, n); }
227 [[nodiscard]] constexpr QByteArrayView mid(qsizetype pos, qsizetype n = -1) const
228 {
229 using namespace QtPrivate;
230 auto result = QContainerImplHelper::mid(size(), &pos, &n);
231 return result == QContainerImplHelper::Null ? QByteArrayView()
232 : QByteArrayView(m_data + pos, n);
233 }
234
235 constexpr void truncate(qsizetype n)
236 { verify(0, n); m_size = n; }
237 constexpr void chop(qsizetype n)
238 { verify(0, n); m_size -= n; }
239
240 // Defined in qbytearray.cpp:
241 [[nodiscard]] QByteArrayView trimmed() const noexcept
242 { return QtPrivate::trimmed(*this); }
243 [[nodiscard]] short toShort(bool *ok = nullptr, int base = 10) const
244 { return QtPrivate::toIntegral<short>(*this, ok, base); }
245 [[nodiscard]] ushort toUShort(bool *ok = nullptr, int base = 10) const
246 { return QtPrivate::toIntegral<ushort>(*this, ok, base); }
247 [[nodiscard]] int toInt(bool *ok = nullptr, int base = 10) const
248 { return QtPrivate::toIntegral<int>(*this, ok, base); }
249 [[nodiscard]] uint toUInt(bool *ok = nullptr, int base = 10) const
250 { return QtPrivate::toIntegral<uint>(*this, ok, base); }
251 [[nodiscard]] long toLong(bool *ok = nullptr, int base = 10) const
252 { return QtPrivate::toIntegral<long>(*this, ok, base); }
253 [[nodiscard]] ulong toULong(bool *ok = nullptr, int base = 10) const
254 { return QtPrivate::toIntegral<ulong>(*this, ok, base); }
255 [[nodiscard]] qlonglong toLongLong(bool *ok = nullptr, int base = 10) const
256 { return QtPrivate::toIntegral<qlonglong>(*this, ok, base); }
257 [[nodiscard]] qulonglong toULongLong(bool *ok = nullptr, int base = 10) const
258 { return QtPrivate::toIntegral<qulonglong>(*this, ok, base); }
259 [[nodiscard]] float toFloat(bool *ok = nullptr) const
260 {
261 const auto r = QtPrivate::toFloat(*this);
262 if (ok)
263 *ok = bool(r);
264 return r.value_or(0.0f);
265 }
266 [[nodiscard]] double toDouble(bool *ok = nullptr) const
267 {
268 const auto r = QtPrivate::toDouble(*this);
269 if (ok)
270 *ok = bool(r);
271 return r.value_or(0.0);
272 }
273
274 [[nodiscard]] bool startsWith(QByteArrayView other) const noexcept
275 { return QtPrivate::startsWith(*this, other); }
276 [[nodiscard]] constexpr bool startsWith(char c) const noexcept
277 { return !empty() && front() == c; }
278
279 [[nodiscard]] bool endsWith(QByteArrayView other) const noexcept
280 { return QtPrivate::endsWith(*this, other); }
281 [[nodiscard]] constexpr bool endsWith(char c) const noexcept
282 { return !empty() && back() == c; }
283
284 [[nodiscard]] qsizetype indexOf(QByteArrayView a, qsizetype from = 0) const noexcept
285 { return QtPrivate::findByteArray(*this, from, a); }
286 [[nodiscard]] qsizetype indexOf(char ch, qsizetype from = 0) const noexcept
287 { return QtPrivate::findByteArray(*this, from, ch); }
288
289 [[nodiscard]] bool contains(QByteArrayView a) const noexcept
290 { return indexOf(a) != qsizetype(-1); }
291 [[nodiscard]] bool contains(char c) const noexcept
292 { return indexOf(c) != qsizetype(-1); }
293
294 [[nodiscard]] qsizetype lastIndexOf(QByteArrayView a) const noexcept
295 { return lastIndexOf(a, size()); }
296 [[nodiscard]] qsizetype lastIndexOf(QByteArrayView a, qsizetype from) const noexcept
297 { return QtPrivate::lastIndexOf(*this, from, a); }
298 [[nodiscard]] qsizetype lastIndexOf(char ch, qsizetype from = -1) const noexcept
299 { return QtPrivate::lastIndexOf(*this, from, ch); }
300
301 [[nodiscard]] qsizetype count(QByteArrayView a) const noexcept
302 { return QtPrivate::count(*this, a); }
303 [[nodiscard]] qsizetype count(char ch) const noexcept
304 { return QtPrivate::count(*this, QByteArrayView(&ch, 1)); }
305
306 inline int compare(QByteArrayView a, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
307
308 [[nodiscard]] inline bool isValidUtf8() const noexcept { return QtPrivate::isValidUtf8(*this); }
309
310 //
311 // STL compatibility API:
312 //
313 [[nodiscard]] constexpr const_iterator begin() const noexcept { return data(); }
314 [[nodiscard]] constexpr const_iterator end() const noexcept { return data() + size(); }
315 [[nodiscard]] constexpr const_iterator cbegin() const noexcept { return begin(); }
316 [[nodiscard]] constexpr const_iterator cend() const noexcept { return end(); }
317 [[nodiscard]] constexpr const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); }
318 [[nodiscard]] constexpr const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); }
319 [[nodiscard]] constexpr const_reverse_iterator crbegin() const noexcept { return rbegin(); }
320 [[nodiscard]] constexpr const_reverse_iterator crend() const noexcept { return rend(); }
321
322 [[nodiscard]] constexpr bool empty() const noexcept { return size() == 0; }
323 [[nodiscard]] constexpr char front() const { Q_ASSERT(!empty()); return m_data[0]; }
324 [[nodiscard]] constexpr char back() const { Q_ASSERT(!empty()); return m_data[m_size - 1]; }
325
326 [[nodiscard]] constexpr Q_IMPLICIT operator std::string_view() const noexcept
327 { return std::string_view(m_data, size_t(m_size)); }
328
329 [[nodiscard]] constexpr qsizetype max_size() const noexcept { return maxSize(); }
330
331 //
332 // Qt compatibility API:
333 //
334 [[nodiscard]] constexpr bool isNull() const noexcept { return !m_data; }
335 [[nodiscard]] constexpr bool isEmpty() const noexcept { return empty(); }
336 [[nodiscard]] constexpr qsizetype length() const noexcept
337 { return size(); }
338 [[nodiscard]] constexpr char first() const { return front(); }
339 [[nodiscard]] constexpr char last() const { return back(); }
340
341 [[nodiscard]] static constexpr qsizetype maxSize() noexcept
342 {
343 // -1 to deal with the pointer one-past-the-end;
344 return QtPrivate::MaxAllocSize - 1;
345 }
346
347private:
348 Q_ALWAYS_INLINE constexpr void verify([[maybe_unused]] qsizetype pos = 0,
349 [[maybe_unused]] qsizetype n = 1) const
350 {
351 Q_ASSERT(pos >= 0);
352 Q_ASSERT(pos <= size());
353 Q_ASSERT(n >= 0);
354 Q_ASSERT(n <= size() - pos);
355 }
356
357 friend bool
358 comparesEqual(const QByteArrayView &lhs, const QByteArrayView &rhs) noexcept
359 {
360 return lhs.size() == rhs.size()
361 && (!lhs.size() || memcmp(lhs.data(), rhs.data(), lhs.size()) == 0);
362 }
363 friend Qt::strong_ordering
364 compareThreeWay(const QByteArrayView &lhs, const QByteArrayView &rhs) noexcept
365 {
366 const int res = QtPrivate::compareMemory(lhs, rhs);
367 return Qt::compareThreeWay(res, 0);
368 }
369 Q_DECLARE_STRONGLY_ORDERED(QByteArrayView)
370
371 // defined in qstring.cpp
372 friend Q_CORE_EXPORT bool
373 comparesEqual(const QByteArrayView &lhs, const QChar &rhs) noexcept;
374 friend Q_CORE_EXPORT Qt::strong_ordering
375 compareThreeWay(const QByteArrayView &lhs, const QChar &rhs) noexcept;
376 friend Q_CORE_EXPORT bool
377 comparesEqual(const QByteArrayView &lhs, char16_t rhs) noexcept;
378 friend Q_CORE_EXPORT Qt::strong_ordering
379 compareThreeWay(const QByteArrayView &lhs, char16_t rhs) noexcept;
380#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
381 Q_DECLARE_STRONGLY_ORDERED(QByteArrayView, QChar, QT_ASCII_CAST_WARN)
382 Q_DECLARE_STRONGLY_ORDERED(QByteArrayView, char16_t, QT_ASCII_CAST_WARN)
383#endif // !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
384
385 qsizetype m_size;
386 const storage_type *m_data;
387};
388Q_DECLARE_TYPEINFO(QByteArrayView, Q_PRIMITIVE_TYPE);
389
390template<typename QByteArrayLike,
391 std::enable_if_t<std::is_same_v<QByteArrayLike, QByteArray>, bool> = true>
392[[nodiscard]] inline QByteArrayView qToByteArrayViewIgnoringNull(const QByteArrayLike &b) noexcept
393{ return QByteArrayView(b.begin(), b.size()); }
394
395inline int QByteArrayView::compare(QByteArrayView a, Qt::CaseSensitivity cs) const noexcept
396{
397 return cs == Qt::CaseSensitive ? QtPrivate::compareMemory(*this, a) :
398 qstrnicmp(data(), size(), a.data(), a.size());
399}
400
401#if QT_DEPRECATED_SINCE(6, 0)
402QT_DEPRECATED_VERSION_X_6_0("Use the QByteArrayView overload.")
403inline quint16 qChecksum(const char *s, qsizetype len,
404 Qt::ChecksumType standard = Qt::ChecksumIso3309)
405{ return qChecksum(QByteArrayView(s, len), standard); }
406#endif
407
408qsizetype QtPrivate::findByteArray(QByteArrayView haystack, qsizetype from, char needle) noexcept
409{
410 if (from < -haystack.size()) // from < 0 && abs(from) > haystack.size(), avoiding overflow
411 return -1;
412 if (from < 0)
413 from = from + haystack.size();
414 if (from < haystack.size()) {
415 const char *const b = haystack.data();
416 if (const auto n = static_cast<const char *>(
417 memchr(b + from, needle, static_cast<size_t>(haystack.size() - from)))) {
418 return n - b;
419 }
420 }
421 return -1;
422}
423
424qsizetype QtPrivate::lastIndexOf(QByteArrayView haystack, qsizetype from, uchar needle) noexcept
425{
426 if (from < 0)
427 from = qMax(from + haystack.size(), qsizetype(0));
428 else
429 from = qMin(from, haystack.size() - 1);
430
431 const char *const b = haystack.data();
432 const void *n = b ? qmemrchr(b, needle, from + 1) : nullptr;
433 return n ? static_cast<const char *>(n) - b : -1;
434}
435
436QT_END_NAMESPACE
437
438#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:110
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:99
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:128
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