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