1/****************************************************************************
2**
3** Copyright (C) 2020 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39#ifndef QBYTEARRAYVIEW_H
40#define QBYTEARRAYVIEW_H
41
42#include <QtCore/qbytearrayalgorithms.h>
43
44#include <string>
45
46QT_BEGIN_NAMESPACE
47
48class QByteArray;
49class QLatin1String;
50
51namespace QtPrivate {
52
53template <typename Byte>
54struct IsCompatibleByteTypeHelper
55 : std::integral_constant<bool,
56 std::is_same_v<Byte, char> ||
57 std::is_same_v<Byte, uchar> ||
58 std::is_same_v<Byte, signed char> ||
59 std::is_same_v<Byte, std::byte>> {};
60
61template <typename Byte>
62struct IsCompatibleByteType
63 : IsCompatibleByteTypeHelper<
64 typename std::remove_cv_t<typename std::remove_reference_t<Byte>>> {};
65
66template <typename Pointer>
67struct IsCompatibleByteArrayPointerHelper : std::false_type {};
68template <typename Byte>
69struct IsCompatibleByteArrayPointerHelper<Byte *>
70 : IsCompatibleByteType<Byte> {};
71template<typename Pointer>
72struct IsCompatibleByteArrayPointer
73 : IsCompatibleByteArrayPointerHelper<
74 typename std::remove_cv_t<typename std::remove_reference_t<Pointer>>> {};
75
76template <typename T, typename Enable = void>
77struct IsContainerCompatibleWithQByteArrayView : std::false_type {};
78
79template <typename T>
80struct IsContainerCompatibleWithQByteArrayView<T, std::enable_if_t<
81 std::conjunction_v<
82 // lacking concepts and ranges, we accept any T whose std::data yields a suitable
83 // pointer ...
84 IsCompatibleByteArrayPointer<decltype(std::data(std::declval<const T &>()))>,
85 // ... and that has a suitable size ...
86 std::is_convertible<decltype(std::size(std::declval<const T &>())), qsizetype>,
87 // ... and it's a range as it defines an iterator-like API
88 IsCompatibleByteType<typename std::iterator_traits<decltype(
89 std::begin(std::declval<const T &>()))>::value_type>,
90 std::is_convertible<decltype(std::begin(std::declval<const T &>())
91 != std::end(std::declval<const T &>())),
92 bool>,
93
94 // This needs to be treated specially due to the empty vs null distinction
95 std::negation<std::is_same<std::decay_t<T>, QByteArray>>,
96
97 // We handle array literals specially for source compat reasons
98 std::negation<std::is_array<T>>,
99
100 // Don't make an accidental copy constructor
101 std::negation<std::is_same<std::decay_t<T>, QByteArrayView>>>>> : std::true_type {};
102
103} // namespace QtPrivate
104
105class Q_CORE_EXPORT QByteArrayView
106{
107public:
108 typedef char storage_type;
109 typedef const char value_type;
110 typedef qptrdiff difference_type;
111 typedef qsizetype size_type;
112 typedef value_type &reference;
113 typedef value_type &const_reference;
114 typedef value_type *pointer;
115 typedef value_type *const_pointer;
116
117 typedef pointer iterator;
118 typedef const_pointer const_iterator;
119 typedef std::reverse_iterator<iterator> reverse_iterator;
120 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
121
122private:
123 template <typename Byte>
124 using if_compatible_byte =
125 typename std::enable_if_t<QtPrivate::IsCompatibleByteType<Byte>::value, bool>;
126
127 template <typename Pointer>
128 using if_compatible_pointer =
129 typename std::enable_if_t<QtPrivate::IsCompatibleByteArrayPointer<Pointer>::value,
130 bool>;
131
132 template <typename T>
133 using if_compatible_qbytearray_like =
134 typename std::enable_if_t<std::is_same_v<T, QByteArray>, bool>;
135
136 template <typename T>
137 using if_compatible_container =
138 typename std::enable_if_t<QtPrivate::IsContainerCompatibleWithQByteArrayView<T>::value,
139 bool>;
140
141 template <typename Char>
142 static constexpr qsizetype lengthHelperPointer(const Char *data) noexcept
143 {
144 return qsizetype(std::char_traits<Char>::length(data));
145 }
146
147 template <typename Container>
148 static constexpr qsizetype lengthHelperContainer(const Container &c) noexcept
149 {
150 return qsizetype(std::size(c));
151 }
152
153 static constexpr qsizetype lengthHelperCharArray(const char *data, size_t size) noexcept
154 {
155 const auto it = std::char_traits<char>::find(data, size, '\0');
156 const auto end = it ? it : std::next(data, size);
157 return qsizetype(std::distance(data, end));
158 }
159
160 template <typename Byte>
161 static const storage_type *castHelper(const Byte *data) noexcept
162 { return reinterpret_cast<const storage_type*>(data); }
163 static constexpr const storage_type *castHelper(const storage_type *data) noexcept
164 { return data; }
165
166public:
167 constexpr QByteArrayView() noexcept
168 : m_size(0), m_data(nullptr) {}
169 constexpr QByteArrayView(std::nullptr_t) noexcept
170 : QByteArrayView() {}
171
172 template <typename Byte, if_compatible_byte<Byte> = true>
173 constexpr QByteArrayView(const Byte *data, qsizetype len)
174 : m_size((Q_ASSERT(len >= 0), Q_ASSERT(data || !len), len)),
175 m_data(castHelper(data)) {}
176
177 template <typename Byte, if_compatible_byte<Byte> = true>
178 constexpr QByteArrayView(const Byte *first, const Byte *last)
179 : QByteArrayView(first, last - first) {}
180
181#ifdef Q_QDOC
182 template <typename Byte>
183 constexpr QByteArrayView(const Byte *data) noexcept;
184#else
185 template <typename Pointer, if_compatible_pointer<Pointer> = true>
186 constexpr QByteArrayView(const Pointer &data) noexcept
187 : QByteArrayView(
188 data, data ? lengthHelperPointer(data) : 0) {}
189#endif
190
191#ifdef Q_QDOC
192 QByteArrayView(const QByteArray &data) noexcept;
193#else
194 template <typename ByteArray, if_compatible_qbytearray_like<ByteArray> = true>
195 QByteArrayView(const ByteArray &ba) noexcept
196 : QByteArrayView(ba.isNull() ? nullptr : ba.data(), qsizetype(ba.size())) {}
197#endif
198
199 template <typename Container, if_compatible_container<Container> = true>
200 constexpr QByteArrayView(const Container &c) noexcept
201 : QByteArrayView(std::data(c), lengthHelperContainer(c)) {}
202 template <size_t Size>
203 constexpr QByteArrayView(const char (&data)[Size]) noexcept
204 : QByteArrayView(data, lengthHelperCharArray(data, Size)) {}
205
206#ifdef Q_QDOC
207 template <typename Byte, size_t Size>
208#else
209 template <typename Byte, size_t Size, if_compatible_byte<Byte> = true>
210#endif
211 [[nodiscard]] constexpr static QByteArrayView fromArray(const Byte (&data)[Size]) noexcept
212 { return QByteArrayView(data, Size); }
213 [[nodiscard]] inline QByteArray toByteArray() const; // defined in qbytearray.h
214
215 [[nodiscard]] constexpr qsizetype size() const noexcept { return m_size; }
216 [[nodiscard]] constexpr const_pointer data() const noexcept { return m_data; }
217 [[nodiscard]] constexpr const_pointer constData() const noexcept { return data(); }
218
219 [[nodiscard]] constexpr char operator[](qsizetype n) const
220 { Q_ASSERT(n >= 0); Q_ASSERT(n < size()); return m_data[n]; }
221
222 //
223 // QByteArray API
224 //
225 [[nodiscard]] constexpr char at(qsizetype n) const { return (*this)[n]; }
226
227 [[nodiscard]] constexpr QByteArrayView first(qsizetype n) const
228 { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); return QByteArrayView(data(), n); }
229 [[nodiscard]] constexpr QByteArrayView last(qsizetype n) const
230 { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); return QByteArrayView(data() + size() - n, n); }
231 [[nodiscard]] constexpr QByteArrayView sliced(qsizetype pos) const
232 { Q_ASSERT(pos >= 0); Q_ASSERT(pos <= size()); return QByteArrayView(data() + pos, size() - pos); }
233 [[nodiscard]] constexpr QByteArrayView sliced(qsizetype pos, qsizetype n) const
234 { Q_ASSERT(pos >= 0); Q_ASSERT(n >= 0); Q_ASSERT(size_t(pos) + size_t(n) <= size_t(size())); return QByteArrayView(data() + pos, n); }
235 [[nodiscard]] constexpr QByteArrayView chopped(qsizetype len) const
236 { Q_ASSERT(len >= 0); Q_ASSERT(len <= size()); return first(size() - len); }
237
238 constexpr void truncate(qsizetype n)
239 { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); m_size = n; }
240 constexpr void chop(qsizetype n)
241 { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); m_size -= n; }
242
243 [[nodiscard]] bool startsWith(QByteArrayView other) const noexcept
244 { return QtPrivate::startsWith(*this, other); }
245 [[nodiscard]] bool startsWith(char c) const noexcept
246 { return !empty() && front() == c; }
247
248 [[nodiscard]] bool endsWith(QByteArrayView other) const noexcept
249 { return QtPrivate::endsWith(*this, other); }
250 [[nodiscard]] bool endsWith(char c) const noexcept
251 { return !empty() && back() == c; }
252
253 [[nodiscard]] qsizetype indexOf(QByteArrayView a, qsizetype from = 0) const noexcept
254 { return QtPrivate::findByteArray(*this, from, a); }
255 [[nodiscard]] qsizetype indexOf(char ch, qsizetype from = 0) const noexcept
256 { return QtPrivate::findByteArray(*this, from, QByteArrayView(&ch, 1)); }
257
258 [[nodiscard]] bool contains(QByteArrayView a) const noexcept
259 { return indexOf(a) != qsizetype(-1); }
260 [[nodiscard]] bool contains(char c) const noexcept
261 { return indexOf(c) != qsizetype(-1); }
262
263 [[nodiscard]] qsizetype lastIndexOf(QByteArrayView a) const noexcept
264 { return lastIndexOf(a, size()); }
265 [[nodiscard]] qsizetype lastIndexOf(QByteArrayView a, qsizetype from) const noexcept
266 { return QtPrivate::lastIndexOf(*this, from, a); }
267 [[nodiscard]] qsizetype lastIndexOf(char ch, qsizetype from = -1) const noexcept
268 { return QtPrivate::lastIndexOf(*this, from, QByteArrayView(&ch, 1)); }
269
270 [[nodiscard]] qsizetype count(QByteArrayView a) const noexcept
271 { return QtPrivate::count(*this, a); }
272 [[nodiscard]] qsizetype count(char ch) const noexcept
273 { return QtPrivate::count(*this, QByteArrayView(&ch, 1)); }
274
275 inline int compare(QByteArrayView a, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
276
277 //
278 // STL compatibility API:
279 //
280 [[nodiscard]] constexpr const_iterator begin() const noexcept { return data(); }
281 [[nodiscard]] constexpr const_iterator end() const noexcept { return data() + size(); }
282 [[nodiscard]] constexpr const_iterator cbegin() const noexcept { return begin(); }
283 [[nodiscard]] constexpr const_iterator cend() const noexcept { return end(); }
284 [[nodiscard]] constexpr const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); }
285 [[nodiscard]] constexpr const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); }
286 [[nodiscard]] constexpr const_reverse_iterator crbegin() const noexcept { return rbegin(); }
287 [[nodiscard]] constexpr const_reverse_iterator crend() const noexcept { return rend(); }
288
289 [[nodiscard]] constexpr bool empty() const noexcept { return size() == 0; }
290 [[nodiscard]] constexpr char front() const { Q_ASSERT(!empty()); return m_data[0]; }
291 [[nodiscard]] constexpr char back() const { Q_ASSERT(!empty()); return m_data[m_size - 1]; }
292
293 //
294 // Qt compatibility API:
295 //
296 [[nodiscard]] constexpr bool isNull() const noexcept { return !m_data; }
297 [[nodiscard]] constexpr bool isEmpty() const noexcept { return empty(); }
298 [[nodiscard]] constexpr qsizetype length() const noexcept
299 { return size(); }
300 [[nodiscard]] constexpr char first() const { return front(); }
301 [[nodiscard]] constexpr char last() const { return back(); }
302
303 friend inline bool operator==(QByteArrayView lhs, QByteArrayView rhs) noexcept
304 { return lhs.size() == rhs.size() && QtPrivate::compareMemory(lhs, rhs) == 0; }
305 friend inline bool operator!=(QByteArrayView lhs, QByteArrayView rhs) noexcept
306 { return !(lhs == rhs); }
307 friend inline bool operator< (QByteArrayView lhs, QByteArrayView rhs) noexcept
308 { return QtPrivate::compareMemory(lhs, rhs) < 0; }
309 friend inline bool operator<=(QByteArrayView lhs, QByteArrayView rhs) noexcept
310 { return QtPrivate::compareMemory(lhs, rhs) <= 0; }
311 friend inline bool operator> (QByteArrayView lhs, QByteArrayView rhs) noexcept
312 { return !(lhs <= rhs); }
313 friend inline bool operator>=(QByteArrayView lhs, QByteArrayView rhs) noexcept
314 { return !(lhs < rhs); }
315
316private:
317 qsizetype m_size;
318 const storage_type *m_data;
319};
320Q_DECLARE_TYPEINFO(QByteArrayView, Q_PRIMITIVE_TYPE);
321
322template<typename QByteArrayLike,
323 std::enable_if_t<std::is_same_v<QByteArrayLike, QByteArray>, bool> = true>
324[[nodiscard]] inline QByteArrayView qToByteArrayViewIgnoringNull(const QByteArrayLike &b) noexcept
325{ return QByteArrayView(b.data(), b.size()); }
326
327inline int QByteArrayView::compare(QByteArrayView a, Qt::CaseSensitivity cs) const noexcept
328{
329 return cs == Qt::CaseSensitive ? QtPrivate::compareMemory(*this, a) :
330 qstrnicmp(data(), size(), a.data(), a.size());
331}
332
333#if QT_DEPRECATED_SINCE(6, 0)
334QT_DEPRECATED_VERSION_X_6_0("Use the QByteArrayView overload.")
335inline quint16 qChecksum(const char *s, qsizetype len,
336 Qt::ChecksumType standard = Qt::ChecksumIso3309)
337{ return qChecksum(QByteArrayView(s, len), standard); }
338#endif
339
340QT_END_NAMESPACE
341
342#endif // QBYTEARRAYVIEW_H
343

source code of qtbase/src/corelib/text/qbytearrayview.h