1// Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@kdab.com>
2// Copyright (C) 2019 Mail.ru Group.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4#ifndef QSTRINGVIEW_H
5#define QSTRINGVIEW_H
6
7#include <QtCore/qchar.h>
8#include <QtCore/qbytearray.h>
9#include <QtCore/qstringliteral.h>
10#include <QtCore/qstringalgorithms.h>
11
12#include <string>
13
14#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
15Q_FORWARD_DECLARE_CF_TYPE(CFString);
16Q_FORWARD_DECLARE_OBJC_CLASS(NSString);
17#endif
18
19QT_BEGIN_NAMESPACE
20
21class QString;
22class QStringView;
23class QRegularExpression;
24class QRegularExpressionMatch;
25
26namespace QtPrivate {
27template <typename Char>
28struct IsCompatibleCharTypeHelper
29 : std::integral_constant<bool,
30 std::is_same<Char, QChar>::value ||
31 std::is_same<Char, ushort>::value ||
32 std::is_same<Char, char16_t>::value ||
33 (std::is_same<Char, wchar_t>::value && sizeof(wchar_t) == sizeof(QChar))> {};
34template <typename Char>
35struct IsCompatibleCharType
36 : IsCompatibleCharTypeHelper<typename std::remove_cv<typename std::remove_reference<Char>::type>::type> {};
37
38template <typename Pointer>
39struct IsCompatiblePointerHelper : std::false_type {};
40template <typename Char>
41struct IsCompatiblePointerHelper<Char*>
42 : IsCompatibleCharType<Char> {};
43template <typename Pointer>
44struct IsCompatiblePointer
45 : IsCompatiblePointerHelper<typename std::remove_cv<typename std::remove_reference<Pointer>::type>::type> {};
46
47template <typename T, typename Enable = void>
48struct IsContainerCompatibleWithQStringView : std::false_type {};
49
50template <typename T>
51struct IsContainerCompatibleWithQStringView<T, std::enable_if_t<std::conjunction_v<
52 // lacking concepts and ranges, we accept any T whose std::data yields a suitable pointer ...
53 IsCompatiblePointer<decltype( std::data(std::declval<const T &>()) )>,
54 // ... and that has a suitable size ...
55 std::is_convertible<decltype( std::size(std::declval<const T &>()) ), qsizetype>,
56 // ... and it's a range as it defines an iterator-like API
57 IsCompatibleCharType<typename std::iterator_traits<decltype( std::begin(std::declval<const T &>()) )>::value_type>,
58 std::is_convertible<
59 decltype( std::begin(std::declval<const T &>()) != std::end(std::declval<const T &>()) ),
60 bool>,
61
62 // These need to be treated specially due to the empty vs null distinction
63 std::negation<std::is_same<std::decay_t<T>, QString>>,
64
65 // Don't make an accidental copy constructor
66 std::negation<std::is_same<std::decay_t<T>, QStringView>>
67 >>> : std::true_type {};
68
69} // namespace QtPrivate
70
71class QStringView
72{
73public:
74 typedef char16_t storage_type;
75 typedef const QChar value_type;
76 typedef std::ptrdiff_t difference_type;
77 typedef qsizetype size_type;
78 typedef value_type &reference;
79 typedef value_type &const_reference;
80 typedef value_type *pointer;
81 typedef value_type *const_pointer;
82
83 typedef pointer iterator;
84 typedef const_pointer const_iterator;
85 typedef std::reverse_iterator<iterator> reverse_iterator;
86 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
87
88private:
89 template <typename Char>
90 using if_compatible_char = typename std::enable_if<QtPrivate::IsCompatibleCharType<Char>::value, bool>::type;
91
92 template <typename Pointer>
93 using if_compatible_pointer = typename std::enable_if<QtPrivate::IsCompatiblePointer<Pointer>::value, bool>::type;
94
95 template <typename T>
96 using if_compatible_qstring_like = typename std::enable_if<std::is_same<T, QString>::value, bool>::type;
97
98 template <typename T>
99 using if_compatible_container = typename std::enable_if<QtPrivate::IsContainerCompatibleWithQStringView<T>::value, bool>::type;
100
101 template <typename Char>
102 static constexpr qsizetype lengthHelperPointer(const Char *str) noexcept
103 {
104#if defined(__cpp_lib_is_constant_evaluated)
105 if (std::is_constant_evaluated())
106 return std::char_traits<Char>::length(str);
107#elif defined(Q_CC_GNU) && !defined(Q_CC_CLANG)
108 if (__builtin_constant_p(*str))
109 return std::char_traits<Char>::length(str);
110#endif
111 return QtPrivate::qustrlen(str: reinterpret_cast<const char16_t *>(str));
112 }
113 static qsizetype lengthHelperPointer(const QChar *str) noexcept
114 {
115 return QtPrivate::qustrlen(str: reinterpret_cast<const char16_t *>(str));
116 }
117
118 template <typename Container>
119 static constexpr qsizetype lengthHelperContainer(const Container &c) noexcept
120 {
121 return qsizetype(std::size(c));
122 }
123
124 template <typename Char, size_t N>
125 static constexpr qsizetype lengthHelperContainer(const Char (&str)[N]) noexcept
126 {
127 const auto it = std::char_traits<Char>::find(str, N, Char(0));
128 const auto end = it ? it : std::end(str);
129 return qsizetype(std::distance(str, end));
130 }
131
132 template <typename Char>
133 static const storage_type *castHelper(const Char *str) noexcept
134 { return reinterpret_cast<const storage_type*>(str); }
135 static constexpr const storage_type *castHelper(const storage_type *str) noexcept
136 { return str; }
137
138public:
139 constexpr QStringView() noexcept
140 : m_size(0), m_data(nullptr) {}
141 constexpr QStringView(std::nullptr_t) noexcept
142 : QStringView() {}
143
144 template <typename Char, if_compatible_char<Char> = true>
145 constexpr QStringView(const Char *str, qsizetype len)
146 : m_size((Q_ASSERT(len >= 0), Q_ASSERT(str || !len), len)),
147 m_data(castHelper(str)) {}
148
149 template <typename Char, if_compatible_char<Char> = true>
150 constexpr QStringView(const Char *f, const Char *l)
151 : QStringView(f, l - f) {}
152
153#ifdef Q_CLANG_QDOC
154 template <typename Char, size_t N>
155 constexpr QStringView(const Char (&array)[N]) noexcept;
156
157 template <typename Char>
158 constexpr QStringView(const Char *str) noexcept;
159#else
160
161 template <typename Pointer, if_compatible_pointer<Pointer> = true>
162 constexpr QStringView(const Pointer &str) noexcept
163 : QStringView(str, str ? lengthHelperPointer(str) : 0) {}
164#endif
165
166#ifdef Q_CLANG_QDOC
167 QStringView(const QString &str) noexcept;
168#else
169 template <typename String, if_compatible_qstring_like<String> = true>
170 QStringView(const String &str) noexcept
171 : QStringView(str.isNull() ? nullptr : str.data(), qsizetype(str.size())) {}
172#endif
173
174 template <typename Container, if_compatible_container<Container> = true>
175 constexpr QStringView(const Container &c) noexcept
176 : QStringView(std::data(c), lengthHelperContainer(c)) {}
177
178 template <typename Char, size_t Size, if_compatible_char<Char> = true>
179 [[nodiscard]] constexpr static QStringView fromArray(const Char (&string)[Size]) noexcept
180 { return QStringView(string, Size); }
181
182 [[nodiscard]] inline QString toString() const; // defined in qstring.h
183#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
184 // defined in qcore_foundation.mm
185 [[nodiscard]] Q_CORE_EXPORT CFStringRef toCFString() const Q_DECL_CF_RETURNS_RETAINED;
186 [[nodiscard]] Q_CORE_EXPORT NSString *toNSString() const Q_DECL_NS_RETURNS_AUTORELEASED;
187#endif
188
189 [[nodiscard]] constexpr qsizetype size() const noexcept { return m_size; }
190 [[nodiscard]] const_pointer data() const noexcept { return reinterpret_cast<const_pointer>(m_data); }
191 [[nodiscard]] const_pointer constData() const noexcept { return data(); }
192 [[nodiscard]] constexpr const storage_type *utf16() const noexcept { return m_data; }
193
194 [[nodiscard]] constexpr QChar operator[](qsizetype n) const
195 { return Q_ASSERT(n >= 0), Q_ASSERT(n < size()), QChar(m_data[n]); }
196
197 //
198 // QString API
199 //
200
201 template <typename...Args>
202 [[nodiscard]] inline QString arg(Args &&...args) const; // defined in qstring.h
203
204 [[nodiscard]] QByteArray toLatin1() const { return QtPrivate::convertToLatin1(str: *this); }
205 [[nodiscard]] QByteArray toUtf8() const { return QtPrivate::convertToUtf8(str: *this); }
206 [[nodiscard]] QByteArray toLocal8Bit() const { return QtPrivate::convertToLocal8Bit(str: *this); }
207 [[nodiscard]] inline QList<uint> toUcs4() const; // defined in qlist.h ### Qt 7 char32_t
208
209 [[nodiscard]] constexpr QChar at(qsizetype n) const noexcept { return (*this)[n]; }
210
211 [[nodiscard]] constexpr QStringView mid(qsizetype pos, qsizetype n = -1) const noexcept
212 {
213 using namespace QtPrivate;
214 auto result = QContainerImplHelper::mid(originalLength: size(), position: &pos, length: &n);
215 return result == QContainerImplHelper::Null ? QStringView() : QStringView(m_data + pos, n);
216 }
217 [[nodiscard]] constexpr QStringView left(qsizetype n) const noexcept
218 {
219 if (size_t(n) >= size_t(size()))
220 n = size();
221 return QStringView(m_data, n);
222 }
223 [[nodiscard]] constexpr QStringView right(qsizetype n) const noexcept
224 {
225 if (size_t(n) >= size_t(size()))
226 n = size();
227 return QStringView(m_data + m_size - n, n);
228 }
229
230 [[nodiscard]] constexpr QStringView first(qsizetype n) const noexcept
231 { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); return QStringView(m_data, n); }
232 [[nodiscard]] constexpr QStringView last(qsizetype n) const noexcept
233 { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); return QStringView(m_data + size() - n, n); }
234 [[nodiscard]] constexpr QStringView sliced(qsizetype pos) const noexcept
235 { Q_ASSERT(pos >= 0); Q_ASSERT(pos <= size()); return QStringView(m_data + pos, size() - pos); }
236 [[nodiscard]] constexpr QStringView sliced(qsizetype pos, qsizetype n) const noexcept
237 { Q_ASSERT(pos >= 0); Q_ASSERT(n >= 0); Q_ASSERT(size_t(pos) + size_t(n) <= size_t(size())); return QStringView(m_data + pos, n); }
238 [[nodiscard]] constexpr QStringView chopped(qsizetype n) const noexcept
239 { return Q_ASSERT(n >= 0), Q_ASSERT(n <= size()), QStringView(m_data, m_size - n); }
240
241 constexpr void truncate(qsizetype n) noexcept
242 { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); m_size = n; }
243 constexpr void chop(qsizetype n) noexcept
244 { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); m_size -= n; }
245
246 [[nodiscard]] QStringView trimmed() const noexcept { return QtPrivate::trimmed(s: *this); }
247
248 template <typename Needle, typename...Flags>
249 [[nodiscard]] constexpr inline auto tokenize(Needle &&needle, Flags...flags) const
250 noexcept(noexcept(qTokenize(std::declval<const QStringView&>(), std::forward<Needle>(needle), flags...)))
251 -> decltype(qTokenize(*this, std::forward<Needle>(needle), flags...))
252 { return qTokenize(*this, std::forward<Needle>(needle), flags...); }
253
254 [[nodiscard]] int compare(QStringView other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
255 { return QtPrivate::compareStrings(lhs: *this, rhs: other, cs); }
256 [[nodiscard]] inline int compare(QLatin1StringView other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
257 [[nodiscard]] constexpr int compare(QChar c) const noexcept
258 { return size() >= 1 ? compare_single_char_helper(diff: *utf16() - c.unicode()) : -1; }
259 [[nodiscard]] int compare(QChar c, Qt::CaseSensitivity cs) const noexcept
260 { return QtPrivate::compareStrings(lhs: *this, rhs: QStringView(&c, 1), cs); }
261
262 [[nodiscard]] inline int localeAwareCompare(QStringView other) const;
263
264 [[nodiscard]] bool startsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
265 { return QtPrivate::startsWith(haystack: *this, needle: s, cs); }
266 [[nodiscard]] inline bool startsWith(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
267 [[nodiscard]] bool startsWith(QChar c) const noexcept
268 { return !empty() && front() == c; }
269 [[nodiscard]] bool startsWith(QChar c, Qt::CaseSensitivity cs) const noexcept
270 { return QtPrivate::startsWith(haystack: *this, needle: QStringView(&c, 1), cs); }
271
272 [[nodiscard]] bool endsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
273 { return QtPrivate::endsWith(haystack: *this, needle: s, cs); }
274 [[nodiscard]] inline bool endsWith(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
275 [[nodiscard]] bool endsWith(QChar c) const noexcept
276 { return !empty() && back() == c; }
277 [[nodiscard]] bool endsWith(QChar c, Qt::CaseSensitivity cs) const noexcept
278 { return QtPrivate::endsWith(haystack: *this, needle: QStringView(&c, 1), cs); }
279
280 [[nodiscard]] qsizetype indexOf(QChar c, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
281 { return QtPrivate::findString(haystack: *this, from, needle: QStringView(&c, 1), cs); }
282 [[nodiscard]] qsizetype indexOf(QStringView s, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
283 { return QtPrivate::findString(haystack: *this, from, needle: s, cs); }
284 [[nodiscard]] inline qsizetype indexOf(QLatin1StringView s, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
285
286 [[nodiscard]] bool contains(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
287 { return indexOf(s: QStringView(&c, 1), from: 0, cs) != qsizetype(-1); }
288 [[nodiscard]] bool contains(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
289 { return indexOf(s, from: 0, cs) != qsizetype(-1); }
290 [[nodiscard]] inline bool contains(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
291
292 [[nodiscard]] qsizetype count(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
293 { return QtPrivate::count(haystack: *this, needle: c, cs); }
294 [[nodiscard]] qsizetype count(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
295 { return QtPrivate::count(haystack: *this, needle: s, cs); }
296 [[nodiscard]] inline qsizetype count(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
297
298 [[nodiscard]] qsizetype lastIndexOf(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
299 { return lastIndexOf(c, from: -1, cs); }
300 [[nodiscard]] qsizetype lastIndexOf(QChar c, qsizetype from, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
301 { return QtPrivate::lastIndexOf(haystack: *this, from, needle: QStringView(&c, 1), cs); }
302 [[nodiscard]] qsizetype lastIndexOf(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
303 { return lastIndexOf(s, from: size(), cs); }
304 [[nodiscard]] qsizetype lastIndexOf(QStringView s, qsizetype from, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
305 { return QtPrivate::lastIndexOf(haystack: *this, from, needle: s, cs); }
306 [[nodiscard]] inline qsizetype lastIndexOf(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
307 [[nodiscard]] inline qsizetype lastIndexOf(QLatin1StringView s, qsizetype from, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
308
309#if QT_CONFIG(regularexpression)
310 [[nodiscard]] qsizetype indexOf(const QRegularExpression &re, qsizetype from = 0, QRegularExpressionMatch *rmatch = nullptr) const
311 {
312 return QtPrivate::indexOf(haystack: *this, re, from, rmatch);
313 }
314#ifdef Q_QDOC
315 [[nodiscard]] qsizetype lastIndexOf(const QRegularExpression &re, QRegularExpressionMatch *rmatch = nullptr) const;
316#else
317 // prevent an ambiguity when called like this: lastIndexOf(re, 0)
318 template <typename T = QRegularExpressionMatch, std::enable_if_t<std::is_same_v<T, QRegularExpressionMatch>, bool> = false>
319 [[nodiscard]] qsizetype lastIndexOf(const QRegularExpression &re, T *rmatch = nullptr) const
320 {
321 return QtPrivate::lastIndexOf(*this, re, size(), rmatch);
322 }
323#endif
324 [[nodiscard]] qsizetype lastIndexOf(const QRegularExpression &re, qsizetype from, QRegularExpressionMatch *rmatch = nullptr) const
325 {
326 return QtPrivate::lastIndexOf(haystack: *this, re, from, rmatch);
327 }
328 [[nodiscard]] bool contains(const QRegularExpression &re, QRegularExpressionMatch *rmatch = nullptr) const
329 {
330 return QtPrivate::contains(haystack: *this, re, rmatch);
331 }
332 [[nodiscard]] qsizetype count(const QRegularExpression &re) const
333 {
334 return QtPrivate::count(haystack: *this, re);
335 }
336#endif
337
338 [[nodiscard]] bool isRightToLeft() const noexcept
339 { return QtPrivate::isRightToLeft(string: *this); }
340 [[nodiscard]] bool isValidUtf16() const noexcept
341 { return QtPrivate::isValidUtf16(s: *this); }
342
343 [[nodiscard]] inline short toShort(bool *ok = nullptr, int base = 10) const;
344 [[nodiscard]] inline ushort toUShort(bool *ok = nullptr, int base = 10) const;
345 [[nodiscard]] inline int toInt(bool *ok = nullptr, int base = 10) const;
346 [[nodiscard]] inline uint toUInt(bool *ok = nullptr, int base = 10) const;
347 [[nodiscard]] inline long toLong(bool *ok = nullptr, int base = 10) const;
348 [[nodiscard]] inline ulong toULong(bool *ok = nullptr, int base = 10) const;
349 [[nodiscard]] inline qlonglong toLongLong(bool *ok = nullptr, int base = 10) const;
350 [[nodiscard]] inline qulonglong toULongLong(bool *ok = nullptr, int base = 10) const;
351 [[nodiscard]] Q_CORE_EXPORT float toFloat(bool *ok = nullptr) const;
352 [[nodiscard]] Q_CORE_EXPORT double toDouble(bool *ok = nullptr) const;
353
354 [[nodiscard]] inline qsizetype toWCharArray(wchar_t *array) const; // defined in qstring.h
355
356
357 [[nodiscard]] Q_CORE_EXPORT
358 QList<QStringView> split(QStringView sep,
359 Qt::SplitBehavior behavior = Qt::KeepEmptyParts,
360 Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
361 [[nodiscard]] Q_CORE_EXPORT
362 QList<QStringView> split(QChar sep, Qt::SplitBehavior behavior = Qt::KeepEmptyParts,
363 Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
364
365#if QT_CONFIG(regularexpression)
366 [[nodiscard]] Q_CORE_EXPORT
367 QList<QStringView> split(const QRegularExpression &sep,
368 Qt::SplitBehavior behavior = Qt::KeepEmptyParts) const;
369#endif
370
371 // QStringView <> QStringView
372 friend bool operator==(QStringView lhs, QStringView rhs) noexcept { return lhs.size() == rhs.size() && QtPrivate::equalStrings(lhs, rhs); }
373 friend bool operator!=(QStringView lhs, QStringView rhs) noexcept { return !(lhs == rhs); }
374 friend bool operator< (QStringView lhs, QStringView rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) < 0; }
375 friend bool operator<=(QStringView lhs, QStringView rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) <= 0; }
376 friend bool operator> (QStringView lhs, QStringView rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) > 0; }
377 friend bool operator>=(QStringView lhs, QStringView rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) >= 0; }
378
379 // QStringView <> QChar
380 friend bool operator==(QStringView lhs, QChar rhs) noexcept { return lhs == QStringView(&rhs, 1); }
381 friend bool operator!=(QStringView lhs, QChar rhs) noexcept { return lhs != QStringView(&rhs, 1); }
382 friend bool operator< (QStringView lhs, QChar rhs) noexcept { return lhs < QStringView(&rhs, 1); }
383 friend bool operator<=(QStringView lhs, QChar rhs) noexcept { return lhs <= QStringView(&rhs, 1); }
384 friend bool operator> (QStringView lhs, QChar rhs) noexcept { return lhs > QStringView(&rhs, 1); }
385 friend bool operator>=(QStringView lhs, QChar rhs) noexcept { return lhs >= QStringView(&rhs, 1); }
386
387 friend bool operator==(QChar lhs, QStringView rhs) noexcept { return QStringView(&lhs, 1) == rhs; }
388 friend bool operator!=(QChar lhs, QStringView rhs) noexcept { return QStringView(&lhs, 1) != rhs; }
389 friend bool operator< (QChar lhs, QStringView rhs) noexcept { return QStringView(&lhs, 1) < rhs; }
390 friend bool operator<=(QChar lhs, QStringView rhs) noexcept { return QStringView(&lhs, 1) <= rhs; }
391 friend bool operator> (QChar lhs, QStringView rhs) noexcept { return QStringView(&lhs, 1) > rhs; }
392 friend bool operator>=(QChar lhs, QStringView rhs) noexcept { return QStringView(&lhs, 1) >= rhs; }
393
394 //
395 // STL compatibility API:
396 //
397 [[nodiscard]] const_iterator begin() const noexcept { return data(); }
398 [[nodiscard]] const_iterator end() const noexcept { return data() + size(); }
399 [[nodiscard]] const_iterator cbegin() const noexcept { return begin(); }
400 [[nodiscard]] const_iterator cend() const noexcept { return end(); }
401 [[nodiscard]] const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); }
402 [[nodiscard]] const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); }
403 [[nodiscard]] const_reverse_iterator crbegin() const noexcept { return rbegin(); }
404 [[nodiscard]] const_reverse_iterator crend() const noexcept { return rend(); }
405
406 [[nodiscard]] constexpr bool empty() const noexcept { return size() == 0; }
407 [[nodiscard]] constexpr QChar front() const { return Q_ASSERT(!empty()), QChar(m_data[0]); }
408 [[nodiscard]] constexpr QChar back() const { return Q_ASSERT(!empty()), QChar(m_data[m_size - 1]); }
409
410 //
411 // Qt compatibility API:
412 //
413 [[nodiscard]] const_iterator constBegin() const noexcept { return begin(); }
414 [[nodiscard]] const_iterator constEnd() const noexcept { return end(); }
415 [[nodiscard]] constexpr bool isNull() const noexcept { return !m_data; }
416 [[nodiscard]] constexpr bool isEmpty() const noexcept { return empty(); }
417 [[nodiscard]] constexpr qsizetype length() const noexcept
418 { return size(); }
419 [[nodiscard]] constexpr QChar first() const { return front(); }
420 [[nodiscard]] constexpr QChar last() const { return back(); }
421private:
422 qsizetype m_size;
423 const storage_type *m_data;
424
425 constexpr int compare_single_char_helper(int diff) const noexcept
426 { return diff ? diff : size() > 1 ? 1 : 0; }
427};
428Q_DECLARE_TYPEINFO(QStringView, Q_PRIMITIVE_TYPE);
429
430template <typename QStringLike, typename std::enable_if<
431 std::is_same<QStringLike, QString>::value,
432 bool>::type = true>
433inline QStringView qToStringViewIgnoringNull(const QStringLike &s) noexcept
434{ return QStringView(s.data(), s.size()); }
435
436// QChar inline functions:
437
438[[nodiscard]] constexpr auto QChar::fromUcs4(char32_t c) noexcept
439{
440 struct R {
441 char16_t chars[2];
442 [[nodiscard]] constexpr operator QStringView() const noexcept { return {begin(), end()}; }
443 [[nodiscard]] constexpr qsizetype size() const noexcept { return chars[1] ? 2 : 1; }
444 [[nodiscard]] constexpr const char16_t *begin() const noexcept { return chars; }
445 [[nodiscard]] constexpr const char16_t *end() const noexcept { return begin() + size(); }
446 };
447 return requiresSurrogates(ucs4: c) ? R{.chars: {QChar::highSurrogate(ucs4: c),
448 QChar::lowSurrogate(ucs4: c)}} :
449 R{.chars: {char16_t(c), u'\0'}} ;
450}
451
452QT_END_NAMESPACE
453
454#endif /* QSTRINGVIEW_H */
455

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