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>
25using IsCompatibleChar8TypeHelper = std::disjunction<
26#ifdef __cpp_char8_t
27 std::is_same<Char, char8_t>,
28#endif
29 std::is_same<Char, char>,
30 std::is_same<Char, uchar>,
31 std::is_same<Char, signed char>
32 >;
33template <typename Char>
34using IsCompatibleChar8Type
35 = IsCompatibleChar8TypeHelper<q20::remove_cvref_t<Char>>;
36
37template <typename Pointer>
38struct IsCompatiblePointer8Helper : std::false_type {};
39template <typename Char>
40struct IsCompatiblePointer8Helper<Char*>
41 : IsCompatibleChar8Type<Char> {};
42template <typename Pointer>
43using IsCompatiblePointer8
44 = IsCompatiblePointer8Helper<q20::remove_cvref_t<Pointer>>;
45
46template <typename T, typename Enable = void>
47struct IsContainerCompatibleWithQUtf8StringView : std::false_type {};
48
49template <typename T>
50struct IsContainerCompatibleWithQUtf8StringView<T, std::enable_if_t<std::conjunction_v<
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 ...
54 std::is_convertible<
55 decltype(std::size(std::declval<const T &>())),
56 qsizetype
57 >,
58 // ... and it's a range as it defines an iterator-like API
59 IsCompatibleChar8Type<typename std::iterator_traits<
60 decltype(std::begin(std::declval<const T &>()))>::value_type
61 >,
62 std::is_convertible<
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
68 std::negation<std::is_same<std::decay_t<T>, QByteArray>>,
69
70 // This has a compatible value_type, but explicitly a different encoding
71 std::negation<std::is_same<std::decay_t<T>, QLatin1StringView>>,
72
73 // Don't make an accidental copy constructor
74 std::negation<std::disjunction<
75 std::is_same<std::decay_t<T>, QBasicUtf8StringView<true>>,
76 std::is_same<std::decay_t<T>, QBasicUtf8StringView<false>>
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;
107 typedef qptrdiff difference_type;
108 typedef qsizetype size_type;
109 typedef value_type &reference;
110 typedef value_type &const_reference;
111 typedef value_type *pointer;
112 typedef value_type *const_pointer;
113
114 typedef pointer iterator;
115 typedef const_pointer const_iterator;
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)
160 : m_data(castHelper(str)),
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
176 : QBasicUtf8StringView(str, QtPrivate::lengthHelperPointer(str)) {}
177
178 template <typename Char, if_compatible_char<Char> = true>
179 constexpr QBasicUtf8StringView(const Char (&str)[]) noexcept
180 : QBasicUtf8StringView(&*str) {} // decay to pointer
181#endif
182
183#ifdef Q_QDOC
184 QBasicUtf8StringView(const QByteArray &str) noexcept;
185 constexpr QBasicUtf8StringView(const storage_type *d, qsizetype n) noexcept {};
186#else
187 template <typename String, if_compatible_qstring_like<String> = true>
188 QBasicUtf8StringView(const String &str) noexcept
189 : QBasicUtf8StringView{str.begin(), str.size()} {}
190#endif
191
192 template <typename Container, if_compatible_container<Container> = true>
193 constexpr QBasicUtf8StringView(const Container &c) noexcept
194 : QBasicUtf8StringView(std::data(c), lengthHelperContainer(c)) {}
195
196#if defined(__cpp_char8_t) && !defined(Q_QDOC)
197 constexpr QBasicUtf8StringView(QBasicUtf8StringView<!UseChar8T> other)
198 : QBasicUtf8StringView(other.data(), other.size()) {}
199#endif
200
201 template <typename Char, size_t Size, if_compatible_char<Char> = true>
202 [[nodiscard]] constexpr static QBasicUtf8StringView fromArray(const Char (&string)[Size]) noexcept
203 { return QBasicUtf8StringView(string, Size); }
204
205 [[nodiscard]] inline QString toString() const; // defined in qstring.h
206
207 [[nodiscard]] constexpr qsizetype size() const noexcept { return m_size; }
208 [[nodiscard]] constexpr const_pointer data() const noexcept { return m_data; }
209#ifdef __cpp_char8_t
210 [[nodiscard]] const char8_t *utf8() const noexcept { return reinterpret_cast<const char8_t*>(m_data); }
211#endif
212
213 [[nodiscard]] constexpr storage_type operator[](qsizetype n) const
214 { verify(pos: n, n: 1); return m_data[n]; }
215
216 //
217 // QString API
218 //
219
220 [[nodiscard]] constexpr storage_type at(qsizetype n) const { return (*this)[n]; }
221
222 [[nodiscard]]
223 constexpr QBasicUtf8StringView mid(qsizetype pos, qsizetype n = -1) const
224 {
225 using namespace QtPrivate;
226 auto result = QContainerImplHelper::mid(originalLength: size(), position: &pos, length: &n);
227 return result == QContainerImplHelper::Null ? QBasicUtf8StringView() : QBasicUtf8StringView(m_data + pos, n);
228 }
229 [[nodiscard]]
230 constexpr QBasicUtf8StringView left(qsizetype n) const
231 {
232 if (size_t(n) >= size_t(size()))
233 n = size();
234 return QBasicUtf8StringView(m_data, n);
235 }
236 [[nodiscard]]
237 constexpr QBasicUtf8StringView right(qsizetype n) const
238 {
239 if (size_t(n) >= size_t(size()))
240 n = size();
241 return QBasicUtf8StringView(m_data + m_size - n, n);
242 }
243
244 [[nodiscard]] constexpr QBasicUtf8StringView sliced(qsizetype pos) const
245 { verify(pos, n: 0); return QBasicUtf8StringView{m_data + pos, m_size - pos}; }
246 [[nodiscard]] constexpr QBasicUtf8StringView sliced(qsizetype pos, qsizetype n) const
247 { verify(pos, n); return QBasicUtf8StringView(m_data + pos, n); }
248 [[nodiscard]] constexpr QBasicUtf8StringView first(qsizetype n) const
249 { verify(pos: 0, n); return sliced(0, n); }
250 [[nodiscard]] constexpr QBasicUtf8StringView last(qsizetype n) const
251 { verify(pos: 0, n); return sliced(m_size - n, n); }
252 [[nodiscard]] constexpr QBasicUtf8StringView chopped(qsizetype n) const
253 { verify(pos: 0, n); return sliced(0, m_size - n); }
254
255 constexpr QBasicUtf8StringView &slice(qsizetype pos)
256 { *this = sliced(pos); return *this; }
257 constexpr QBasicUtf8StringView &slice(qsizetype pos, qsizetype n)
258 { *this = sliced(pos, n); return *this; }
259
260 constexpr void truncate(qsizetype n)
261 { verify(pos: 0, n); m_size = n; }
262 constexpr void chop(qsizetype n)
263 { verify(pos: 0, n); m_size -= n; }
264
265 [[nodiscard]] inline bool isValidUtf8() const noexcept
266 {
267 return QByteArrayView(reinterpret_cast<const char *>(data()), size()).isValidUtf8();
268 }
269
270 //
271 // STL compatibility API:
272 //
273 [[nodiscard]] const_iterator begin() const noexcept { return data(); }
274 [[nodiscard]] const_iterator end() const noexcept { return data() + size(); }
275 [[nodiscard]] const_iterator cbegin() const noexcept { return begin(); }
276 [[nodiscard]] const_iterator cend() const noexcept { return end(); }
277 [[nodiscard]] const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); }
278 [[nodiscard]] const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); }
279 [[nodiscard]] const_reverse_iterator crbegin() const noexcept { return rbegin(); }
280 [[nodiscard]] const_reverse_iterator crend() const noexcept { return rend(); }
281
282 [[nodiscard]] constexpr bool empty() const noexcept { return size() == 0; }
283 [[nodiscard]] constexpr storage_type front() const { return Q_ASSERT(!empty()), m_data[0]; }
284 [[nodiscard]] constexpr storage_type back() const { return Q_ASSERT(!empty()), m_data[m_size - 1]; }
285
286 [[nodiscard]] Q_IMPLICIT operator std::basic_string_view<storage_type>() const noexcept
287 { return std::basic_string_view<storage_type>(data(), size_t(size())); }
288
289 [[nodiscard]] constexpr qsizetype max_size() const noexcept { return maxSize(); }
290
291 //
292 // Qt compatibility API:
293 //
294 [[nodiscard]] constexpr bool isNull() const noexcept { return !m_data; }
295 [[nodiscard]] constexpr bool isEmpty() const noexcept { return empty(); }
296 [[nodiscard]] constexpr qsizetype length() const noexcept
297 { return size(); }
298
299 [[nodiscard]] int compare(QBasicUtf8StringView other,
300 Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
301 {
302 return QtPrivate::compareStrings(*this, other, cs);
303 }
304
305 // all defined in qstring.h
306 [[nodiscard]] inline int compare(QChar other,
307 Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
308 [[nodiscard]] inline int compare(QStringView other,
309 Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
310 [[nodiscard]] inline int compare(QLatin1StringView other,
311 Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
312 [[nodiscard]] inline int compare(const QByteArray &other,
313 Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
314
315 [[nodiscard]] inline bool equal(QChar other) const noexcept;
316 [[nodiscard]] inline bool equal(QStringView other) const noexcept;
317 [[nodiscard]] inline bool equal(QLatin1StringView other) const noexcept;
318 [[nodiscard]] inline bool equal(const QByteArray &other) const noexcept;
319 // end defined in qstring.h
320
321 [[nodiscard]] static constexpr qsizetype maxSize() noexcept
322 {
323 // -1 to deal with the pointer one-past-the-end;
324 return QtPrivate::MaxAllocSize - 1;
325 }
326
327private:
328 [[nodiscard]] static inline int compare(QBasicUtf8StringView lhs, QBasicUtf8StringView rhs) noexcept
329 {
330 return QtPrivate::compareStrings(lhs: QBasicUtf8StringView<false>(lhs.data(), lhs.size()),
331 rhs: QBasicUtf8StringView<false>(rhs.data(), rhs.size()));
332 }
333
334 friend bool
335 comparesEqual(const QBasicUtf8StringView &lhs, const QBasicUtf8StringView &rhs) noexcept
336 {
337 return lhs.size() == rhs.size()
338 && QtPrivate::equalStrings(lhs: QBasicUtf8StringView<false>(lhs.data(), lhs.size()),
339 rhs: QBasicUtf8StringView<false>(rhs.data(), rhs.size()));
340 }
341 friend Qt::strong_ordering
342 compareThreeWay(const QBasicUtf8StringView &lhs, const QBasicUtf8StringView &rhs) noexcept
343 {
344 const int res = QBasicUtf8StringView::compare(lhs, rhs);
345 return Qt::compareThreeWay(lhs: res, rhs: 0);
346 }
347 Q_DECLARE_STRONGLY_ORDERED(QBasicUtf8StringView)
348
349 friend bool
350 comparesEqual(const QBasicUtf8StringView &lhs, const QLatin1StringView &rhs) noexcept
351 {
352 return lhs.equal(rhs);
353 }
354 friend Qt::strong_ordering
355 compareThreeWay(const QBasicUtf8StringView &lhs, const QLatin1StringView &rhs) noexcept
356 {
357 const int res = lhs.compare(rhs);
358 return Qt::compareThreeWay(lhs: res, rhs: 0);
359 }
360 Q_DECLARE_STRONGLY_ORDERED(QBasicUtf8StringView, QLatin1StringView)
361
362 friend bool
363 comparesEqual(const QBasicUtf8StringView &lhs, const QStringView &rhs) noexcept
364 { return lhs.equal(rhs); }
365 friend Qt::strong_ordering
366 compareThreeWay(const QBasicUtf8StringView &lhs, const QStringView &rhs) noexcept
367 {
368 const int res = lhs.compare(rhs);
369 return Qt::compareThreeWay(lhs: res, rhs: 0);
370 }
371 Q_DECLARE_STRONGLY_ORDERED(QBasicUtf8StringView, QStringView)
372
373 friend bool comparesEqual(const QBasicUtf8StringView &lhs, const QChar &rhs) noexcept
374 { return lhs.equal(rhs); }
375 friend Qt::strong_ordering
376 compareThreeWay(const QBasicUtf8StringView &lhs, const QChar &rhs) noexcept
377 {
378 const int res = lhs.compare(rhs);
379 return Qt::compareThreeWay(lhs: res, rhs: 0);
380 }
381 Q_DECLARE_STRONGLY_ORDERED(QBasicUtf8StringView, QChar)
382 Q_DECLARE_STRONGLY_ORDERED(QBasicUtf8StringView, char16_t)
383
384#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
385 friend bool
386 comparesEqual(const QBasicUtf8StringView &lhs, const QByteArrayView &rhs) noexcept
387 {
388 return lhs.size() == rhs.size()
389 && QtPrivate::equalStrings(lhs: QBasicUtf8StringView<false>(lhs.data(), lhs.size()),
390 rhs: QBasicUtf8StringView<false>(rhs.data(), rhs.size()));
391 }
392 friend Qt::strong_ordering
393 compareThreeWay(const QBasicUtf8StringView &lhs, const QByteArrayView &rhs) noexcept
394 {
395 const int res = QtPrivate::compareStrings(lhs: QBasicUtf8StringView<false>(lhs.data(), lhs.size()),
396 rhs: QBasicUtf8StringView<false>(rhs.data(), rhs.size()));
397 return Qt::compareThreeWay(lhs: res, rhs: 0);
398 }
399 Q_DECLARE_STRONGLY_ORDERED(QBasicUtf8StringView, QByteArrayView, QT_ASCII_CAST_WARN)
400
401 friend bool
402 comparesEqual(const QBasicUtf8StringView &lhs, const QByteArray &rhs) noexcept
403 {
404 return lhs.equal(rhs);
405 }
406 friend Qt::strong_ordering
407 compareThreeWay(const QBasicUtf8StringView &lhs, const QByteArray &rhs) noexcept
408 {
409 const int res = lhs.compare(rhs);
410 return Qt::compareThreeWay(lhs: res, rhs: 0);
411 }
412 Q_DECLARE_STRONGLY_ORDERED(QBasicUtf8StringView, QByteArray, QT_ASCII_CAST_WARN)
413
414 friend bool comparesEqual(const QBasicUtf8StringView &lhs, const char *rhs) noexcept
415 { return comparesEqual(lhs, QByteArrayView(rhs)); }
416 friend Qt::strong_ordering
417 compareThreeWay(const QBasicUtf8StringView &lhs, const char *rhs) noexcept
418 { return compareThreeWay(lhs, QByteArrayView(rhs)); }
419 Q_DECLARE_STRONGLY_ORDERED(QBasicUtf8StringView, const char *, QT_ASCII_CAST_WARN)
420#endif // !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
421
422 Q_ALWAYS_INLINE constexpr void verify([[maybe_unused]] qsizetype pos = 0,
423 [[maybe_unused]] qsizetype n = 1) const
424 {
425 Q_ASSERT(pos >= 0);
426 Q_ASSERT(pos <= size());
427 Q_ASSERT(n >= 0);
428 Q_ASSERT(n <= size() - pos);
429 }
430 const storage_type *m_data;
431 qsizetype m_size;
432};
433
434#ifdef Q_QDOC
435#undef QBasicUtf8StringView
436#else
437template <bool UseChar8T>
438Q_DECLARE_TYPEINFO_BODY(QBasicUtf8StringView<UseChar8T>, Q_PRIMITIVE_TYPE);
439
440template <typename QStringLike, std::enable_if_t<std::is_same_v<QStringLike, QByteArray>, bool> = true>
441[[nodiscard]] inline q_no_char8_t::QUtf8StringView qToUtf8StringViewIgnoringNull(const QStringLike &s) noexcept
442{ return q_no_char8_t::QUtf8StringView(s.begin(), s.size()); }
443#endif // Q_QDOC
444
445QT_END_NAMESPACE
446
447#endif /* QUTF8STRINGVIEW_H */
448

Provided by KDAB

Privacy Policy
Start learning QML with our Intro Training
Find out more

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