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
qutf8stringview.h
Go to the documentation of this file.
1// Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@kdab.com>
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 QUTF8STRINGVIEW_H
5#define QUTF8STRINGVIEW_H
6
7#if 0
8#pragma qt_class(QUtf8StringView)
9#endif
10
11#include <QtCore/qstringalgorithms.h>
12#include <QtCore/qstringfwd.h>
13#include <QtCore/qarraydata.h> // for QContainerImplHelper
14#include <QtCore/qbytearrayview.h>
15#include <QtCore/qcompare.h>
16#include <QtCore/qcontainerfwd.h>
17
18#include <string>
19#include <string_view>
20#include <QtCore/q20type_traits.h>
21
22QT_BEGIN_NAMESPACE
23
24namespace QtPrivate {
25template <typename Char>
27#ifdef __cpp_char8_t
29#endif
30 std::is_same<Char, char>,
32 std::is_same<Char, signed char>
33 >;
34template <typename Char>
37
38template <typename Pointer>
39struct IsCompatiblePointer8Helper : std::false_type {};
40template <typename Char>
41struct IsCompatiblePointer8Helper<Char*>
42 : IsCompatibleChar8Type<Char> {};
43template <typename Pointer>
47template <typename T, typename Enable = void>
48struct IsContainerCompatibleWithQUtf8StringView : std::false_type {};
50template <typename T>
52 // lacking concepts and ranges, we accept any T whose std::data yields a suitable pointer ...
53 IsCompatiblePointer8<decltype(std::data(std::declval<const T &>()))>,
54 // ... and that has a suitable size ...
56 decltype(std::size(std::declval<const T &>())),
58 >,
59 // ... and it's a range as it defines an iterator-like API
61 decltype(std::begin(std::declval<const T &>()))>::value_type
62 >,
64 decltype( std::begin(std::declval<const T &>()) != std::end(std::declval<const T &>()) ),
65 bool
66 >,
67
68 // This needs to be treated specially due to the empty vs null distinction
70
71 // This has a compatible value_type, but explicitly a different encoding
73
74 // Don't make an accidental copy constructor
78 >>
79 >>> : std::true_type {};
80
81struct hide_char8_t {
82#ifdef __cpp_char8_t
83 using type = char8_t;
84#endif
85};
86
87struct wrap_char { using type = char; };
88
89} // namespace QtPrivate
90
91#ifdef Q_QDOC
92#define QBasicUtf8StringView QUtf8StringView
93#else
94template <bool UseChar8T>
95#endif
97{
98public:
99#ifndef Q_QDOC
100 using storage_type = typename std::conditional<UseChar8T,
101 QtPrivate::hide_char8_t,
102 QtPrivate::wrap_char
103 >::type::type;
104#else
105 using storage_type = typename QtPrivate::hide_char8_t;
106#endif
114
117 typedef std::reverse_iterator<iterator> reverse_iterator;
118 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
119
120private:
121 template <typename Char>
123
124 template <typename Pointer>
126
127 template <typename T>
129
130 template <typename T>
132
133 template <typename Container>
134 static constexpr qsizetype lengthHelperContainer(const Container &c) noexcept
135 {
136 return qsizetype(std::size(c));
137 }
138
139 // Note: Do not replace with std::size(const Char (&)[N]), because the result
140 // will be of by one.
141 template <typename Char, size_t N>
142 static constexpr qsizetype lengthHelperContainer(const Char (&str)[N]) noexcept
143 {
144 return QtPrivate::lengthHelperContainer(str);
145 }
146
147 template <typename Char>
148 static const storage_type *castHelper(const Char *str) noexcept
149 { return reinterpret_cast<const storage_type*>(str); }
150 static constexpr const storage_type *castHelper(const storage_type *str) noexcept
151 { return str; }
152
153public:
154 constexpr QBasicUtf8StringView() noexcept
155 : m_data(nullptr), m_size(0) {}
156 constexpr QBasicUtf8StringView(std::nullptr_t) noexcept
158
159 template <typename Char, if_compatible_char<Char> = true>
160 constexpr QBasicUtf8StringView(const Char *str, qsizetype len)
162 m_size((Q_ASSERT(len >= 0), Q_ASSERT(str || !len), len)) {}
163
164 template <typename Char, if_compatible_char<Char> = true>
165 constexpr QBasicUtf8StringView(const Char *f, const Char *l)
166 : QBasicUtf8StringView(f, l - f) {}
167
168#ifdef Q_QDOC
169 template <typename Char, size_t N>
170 constexpr QBasicUtf8StringView(const Char (&array)[N]) noexcept;
171
172 template <typename Char>
173 constexpr QBasicUtf8StringView(const Char *str) noexcept;
174#else
175 template <typename Pointer, if_compatible_pointer<Pointer> = true>
176 constexpr QBasicUtf8StringView(const Pointer &str) noexcept
178
179 template <typename Char, if_compatible_char<Char> = true>
180 constexpr QBasicUtf8StringView(const Char (&str)[]) noexcept
181 : QBasicUtf8StringView(&*str) {} // decay to pointer
182#endif
183
184#ifdef Q_QDOC
185 QBasicUtf8StringView(const QByteArray &str) noexcept;
186 constexpr QBasicUtf8StringView(const storage_type *d, qsizetype n) noexcept {};
187#else
188 template <typename String, if_compatible_qstring_like<String> = true>
189 QBasicUtf8StringView(const String &str) noexcept
190 : QBasicUtf8StringView{str.begin(), str.size()} {}
191#endif
192
193 template <typename Container, if_compatible_container<Container> = true>
194 constexpr QBasicUtf8StringView(const Container &c) noexcept
195 : QBasicUtf8StringView(std::data(c), lengthHelperContainer(c)) {}
196
197#if defined(__cpp_char8_t) && !defined(Q_QDOC)
200#endif
201
202 template <typename Char, size_t Size, if_compatible_char<Char> = true>
203 [[nodiscard]] constexpr static QBasicUtf8StringView fromArray(const Char (&string)[Size]) noexcept
204 { return QBasicUtf8StringView(string, Size); }
205
206 [[nodiscard]] inline QString toString() const; // defined in qstring.h
207
208 [[nodiscard]] constexpr qsizetype size() const noexcept { return m_size; }
209 [[nodiscard]] constexpr const_pointer data() const noexcept { return m_data; }
210#ifdef __cpp_char8_t
211 [[nodiscard]] const char8_t *utf8() const noexcept { return reinterpret_cast<const char8_t*>(m_data); }
212#endif
213
214 [[nodiscard]] constexpr storage_type operator[](qsizetype n) const
215 { verify(n, 1); return m_data[n]; }
216
217 //
218 // QString API
219 //
220
221 [[nodiscard]] constexpr storage_type at(qsizetype n) const { return (*this)[n]; }
222
223 template <typename...Args>
224 [[nodiscard]] inline QString arg(Args &&...args) const;
225
226 [[nodiscard]]
227 constexpr QBasicUtf8StringView 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 ? QBasicUtf8StringView() : QBasicUtf8StringView(m_data + pos, n);
232 }
233 [[nodiscard]]
234 constexpr QBasicUtf8StringView left(qsizetype n) const
235 {
236 if (size_t(n) >= size_t(size()))
237 n = size();
238 return QBasicUtf8StringView(m_data, n);
239 }
240 [[nodiscard]]
241 constexpr QBasicUtf8StringView right(qsizetype n) const
242 {
243 if (size_t(n) >= size_t(size()))
244 n = size();
245 return QBasicUtf8StringView(m_data + m_size - n, n);
246 }
247
248 [[nodiscard]] constexpr QBasicUtf8StringView sliced(qsizetype pos) const
249 { verify(pos, 0); return QBasicUtf8StringView{m_data + pos, m_size - pos}; }
250 [[nodiscard]] constexpr QBasicUtf8StringView sliced(qsizetype pos, qsizetype n) const
251 { verify(pos, n); return QBasicUtf8StringView(m_data + pos, n); }
252 [[nodiscard]] constexpr QBasicUtf8StringView first(qsizetype n) const
253 { verify(0, n); return sliced(0, n); }
254 [[nodiscard]] constexpr QBasicUtf8StringView last(qsizetype n) const
255 { verify(0, n); return sliced(m_size - n, n); }
256 [[nodiscard]] constexpr QBasicUtf8StringView chopped(qsizetype n) const
257 { verify(0, n); return sliced(0, m_size - n); }
258
259 constexpr QBasicUtf8StringView &slice(qsizetype pos)
260 { *this = sliced(pos); return *this; }
261 constexpr QBasicUtf8StringView &slice(qsizetype pos, qsizetype n)
262 { *this = sliced(pos, n); return *this; }
263
264 constexpr void truncate(qsizetype n)
265 { verify(0, n); m_size = n; }
266 constexpr void chop(qsizetype n)
267 { verify(0, n); m_size -= n; }
268
269 [[nodiscard]] inline bool isValidUtf8() const noexcept
270 {
271 return QByteArrayView(reinterpret_cast<const char *>(data()), size()).isValidUtf8();
272 }
273
274 //
275 // STL compatibility API:
276 //
277 [[nodiscard]] const_iterator begin() const noexcept { return data(); }
278 [[nodiscard]] const_iterator end() const noexcept { return data() + size(); }
279 [[nodiscard]] const_iterator cbegin() const noexcept { return begin(); }
280 [[nodiscard]] const_iterator cend() const noexcept { return end(); }
283 [[nodiscard]] const_reverse_iterator crbegin() const noexcept { return rbegin(); }
284 [[nodiscard]] const_reverse_iterator crend() const noexcept { return rend(); }
285
286 [[nodiscard]] constexpr bool empty() const noexcept { return size() == 0; }
287 [[nodiscard]] constexpr storage_type front() const { return Q_ASSERT(!empty()), m_data[0]; }
288 [[nodiscard]] constexpr storage_type back() const { return Q_ASSERT(!empty()), m_data[m_size - 1]; }
289
291 { return std::string_view{reinterpret_cast<const char*>(data()), size_t(size())}; }
292
293#ifdef __cpp_lib_char8_t
294 [[nodiscard]] Q_IMPLICIT operator std::u8string_view() const noexcept
295 { return std::u8string_view{utf8(), size_t(size())}; }
296#endif
297
298 [[nodiscard]] constexpr qsizetype max_size() const noexcept { return maxSize(); }
299
300 //
301 // Qt compatibility API:
302 //
303 [[nodiscard]] constexpr bool isNull() const noexcept { return !m_data; }
304 [[nodiscard]] constexpr bool isEmpty() const noexcept { return empty(); }
305 [[nodiscard]] constexpr qsizetype length() const noexcept
306 { return size(); }
307
309 Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
310 {
311 return QtPrivate::compareStrings(*this, other, cs);
312 }
313
314 // all defined in qstring.h
315 [[nodiscard]] inline int compare(QChar other,
316 Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
317 [[nodiscard]] inline int compare(QStringView other,
318 Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
319 [[nodiscard]] inline int compare(QLatin1StringView other,
320 Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
321 [[nodiscard]] inline int compare(const QByteArray &other,
322 Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
323
324 [[nodiscard]] inline bool equal(QChar other) const noexcept;
325 [[nodiscard]] inline bool equal(QStringView other) const noexcept;
326 [[nodiscard]] inline bool equal(QLatin1StringView other) const noexcept;
327 [[nodiscard]] inline bool equal(const QByteArray &other) const noexcept;
328 // end defined in qstring.h
329
330 [[nodiscard]] static constexpr qsizetype maxSize() noexcept
331 {
332 // -1 to deal with the pointer one-past-the-end;
333 return QtPrivate::MaxAllocSize - 1;
334 }
335
336private:
337 [[nodiscard]] static inline int compare(QBasicUtf8StringView lhs, QBasicUtf8StringView rhs) noexcept
338 {
339 return QtPrivate::compareStrings(QBasicUtf8StringView<false>(lhs.data(), lhs.size()),
340 QBasicUtf8StringView<false>(rhs.data(), rhs.size()));
341 }
342
343 friend bool
345 {
346 return lhs.size() == rhs.size()
347 && QtPrivate::equalStrings(QBasicUtf8StringView<false>(lhs.data(), lhs.size()),
348 QBasicUtf8StringView<false>(rhs.data(), rhs.size()));
349 }
350 friend Qt::strong_ordering
352 {
353 const int res = QBasicUtf8StringView::compare(lhs, rhs);
354 return Qt::compareThreeWay(res, 0);
355 }
357
358 friend bool
360 {
361 return lhs.equal(rhs);
362 }
363 friend Qt::strong_ordering
365 {
366 const int res = lhs.compare(rhs);
367 return Qt::compareThreeWay(res, 0);
368 }
370
371 friend bool
373 { return lhs.equal(rhs); }
374 friend Qt::strong_ordering
376 {
377 const int res = lhs.compare(rhs);
378 return Qt::compareThreeWay(res, 0);
379 }
381
382 friend bool comparesEqual(const QBasicUtf8StringView &lhs, const QChar &rhs) noexcept
383 { return lhs.equal(rhs); }
384 friend Qt::strong_ordering
386 {
387 const int res = lhs.compare(rhs);
388 return Qt::compareThreeWay(res, 0);
389 }
392
393#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
394 friend bool
396 {
397 return lhs.size() == rhs.size()
399 QBasicUtf8StringView<false>(rhs.data(), rhs.size()));
400 }
401 friend Qt::strong_ordering
403 {
405 QBasicUtf8StringView<false>(rhs.data(), rhs.size()));
406 return Qt::compareThreeWay(res, 0);
407 }
409
410 friend bool
412 {
413 return lhs.equal(rhs);
414 }
415 friend Qt::strong_ordering
417 {
418 const int res = lhs.compare(rhs);
419 return Qt::compareThreeWay(res, 0);
420 }
422
423 friend bool comparesEqual(const QBasicUtf8StringView &lhs, const char *rhs) noexcept
424 { return comparesEqual(lhs, QByteArrayView(rhs)); }
425 friend Qt::strong_ordering
426 compareThreeWay(const QBasicUtf8StringView &lhs, const char *rhs) noexcept
429#endif // !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
430
431 Q_ALWAYS_INLINE constexpr void verify([[maybe_unused]] qsizetype pos = 0,
432 [[maybe_unused]] qsizetype n = 1) const
433 {
434 Q_ASSERT(pos >= 0);
435 Q_ASSERT(pos <= size());
436 Q_ASSERT(n >= 0);
437 Q_ASSERT(n <= size() - pos);
438 }
439 const storage_type *m_data;
440 qsizetype m_size;
441};
442
443#ifdef Q_QDOC
444#undef QBasicUtf8StringView
445#else
446template <bool UseChar8T>
448
449template <typename QStringLike, std::enable_if_t<std::is_same_v<QStringLike, QByteArray>, bool> = true>
450[[nodiscard]] inline q_no_char8_t::QUtf8StringView qToUtf8StringViewIgnoringNull(const QStringLike &s) noexcept
451{ return q_no_char8_t::QUtf8StringView(s.begin(), s.size()); }
452#endif // Q_QDOC
453
454QT_END_NAMESPACE
455
456#endif /* QUTF8STRINGVIEW_H */
constexpr QBasicUtf8StringView mid(qsizetype pos, qsizetype n=-1) const
const_reverse_iterator crend() const noexcept
constexpr QBasicUtf8StringView & slice(qsizetype pos, qsizetype n)
constexpr QBasicUtf8StringView first(qsizetype n) const
constexpr bool empty() const noexcept
constexpr void truncate(qsizetype n)
value_type & const_reference
constexpr QBasicUtf8StringView(const Char *f, const Char *l)
QString toString() const
Definition qstring.h:1313
constexpr QBasicUtf8StringView(const Pointer &str) noexcept
constexpr qsizetype size() const noexcept
constexpr QBasicUtf8StringView(const Char *str, qsizetype len)
const_reverse_iterator rbegin() const noexcept
constexpr QBasicUtf8StringView sliced(qsizetype pos) const
typename std::conditional< UseChar8T, QtPrivate::hide_char8_t, QtPrivate::wrap_char >::type::type storage_type
bool isValidUtf8() const noexcept
bool equal(QChar other) const noexcept
Definition qstring.h:1297
const_iterator begin() const noexcept
QString arg(Args &&...args) const
Definition qstring.h:1762
static constexpr QBasicUtf8StringView fromArray(const Char(&string)[Size]) noexcept
constexpr QBasicUtf8StringView & slice(qsizetype pos)
constexpr storage_type operator[](qsizetype n) const
friend Qt::strong_ordering compareThreeWay(const QBasicUtf8StringView &lhs, const QBasicUtf8StringView &rhs) noexcept
bool equal(const QByteArray &other) const noexcept
Definition qstring.h:1341
friend bool comparesEqual(const QBasicUtf8StringView &lhs, const QBasicUtf8StringView &rhs) noexcept
const_iterator cend() const noexcept
constexpr storage_type back() const
constexpr QBasicUtf8StringView sliced(qsizetype pos, qsizetype n) const
const_reverse_iterator rend() const noexcept
int compare(const QByteArray &other, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
Definition qstring.h:1326
std::reverse_iterator< const_iterator > const_reverse_iterator
const_pointer const_iterator
int compare(QStringView other, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
Definition qstring.h:1291
constexpr QBasicUtf8StringView last(qsizetype n) const
static constexpr qsizetype maxSize() noexcept
constexpr QBasicUtf8StringView(std::nullptr_t) noexcept
constexpr storage_type at(qsizetype n) const
const storage_type value_type
constexpr QBasicUtf8StringView(const Char(&str)[]) noexcept
constexpr storage_type front() const
constexpr QBasicUtf8StringView left(qsizetype n) const
constexpr QBasicUtf8StringView() noexcept
const_iterator end() const noexcept
constexpr void chop(qsizetype n)
const_reverse_iterator crbegin() const noexcept
const_iterator cbegin() const noexcept
constexpr const_pointer data() const noexcept
constexpr QBasicUtf8StringView right(qsizetype n) const
constexpr QBasicUtf8StringView chopped(qsizetype n) const
q_no_char8_t::QUtf8StringView qToUtf8StringViewIgnoringNull(const QStringLike &s) noexcept
Q_DECLARE_TYPEINFO_BODY(QBasicUtf8StringView< UseChar8T >, Q_PRIMITIVE_TYPE)