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// Qt-Security score:critical reason:data-parser
5#ifndef QSTRINGVIEW_H
6#define QSTRINGVIEW_H
7
8#include <QtCore/qchar.h>
9#include <QtCore/qcompare.h>
10#include <QtCore/qcontainerfwd.h>
11#include <QtCore/qbytearray.h>
12#include <QtCore/qstringfwd.h>
13#include <QtCore/qstringliteral.h>
14#include <QtCore/qstringalgorithms.h>
15
16#include <string>
17#include <string_view>
18#include <QtCore/q20type_traits.h>
19
20#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
21Q_FORWARD_DECLARE_CF_TYPE(CFString);
22Q_FORWARD_DECLARE_OBJC_CLASS(NSString);
23#endif
24
25QT_BEGIN_NAMESPACE
26
27class QRegularExpression;
28class QRegularExpressionMatch;
29
30namespace QtPrivate {
31template <typename Char>
32struct IsCompatibleCharTypeHelper
33 : std::integral_constant<bool,
34 std::is_same<Char, QChar>::value ||
35 std::is_same<Char, ushort>::value ||
36 std::is_same<Char, char16_t>::value ||
37 (std::is_same<Char, wchar_t>::value && sizeof(wchar_t) == sizeof(QChar))> {};
38template <typename Char>
39struct IsCompatibleCharType
40 : IsCompatibleCharTypeHelper<q20::remove_cvref_t<Char>> {};
41
42template <typename Pointer>
43struct IsCompatiblePointerHelper : std::false_type {};
44template <typename Char>
45struct IsCompatiblePointerHelper<Char*>
46 : IsCompatibleCharType<Char> {};
47template <typename Pointer>
48struct IsCompatiblePointer
49 : IsCompatiblePointerHelper<q20::remove_cvref_t<Pointer>> {};
50
51template <typename T, typename Enable = void>
52struct IsContainerCompatibleWithQStringView : std::false_type {};
53
54template <typename T>
55struct IsContainerCompatibleWithQStringView<T, std::enable_if_t<std::conjunction_v<
56 // lacking concepts and ranges, we accept any T whose std::data yields a suitable pointer ...
57 IsCompatiblePointer<decltype( std::data(array: array: array: array: array: array: array: array: array: array: array: cont: array: array: array: array: array: array: array: array: array: array: cont: cont: cont: cont: cont: cont: std::declval<const T &>()) )>,
58 // ... and that has a suitable size ...
59 std::is_convertible<decltype( std::size(cont: cont: cont: cont: cont: cont: cont: std::declval<const T &>()) ), qsizetype>,
60 // ... and it's a range as it defines an iterator-like API
61 IsCompatibleCharType<typename std::iterator_traits<decltype( std::begin(arr: arr: arr: arr: arr: arr: arr: arr: arr: arr: arr: cont: arr: arr: arr: arr: arr: arr: arr: arr: arr: arr: cont: cont: cont: cont: cont: cont: std::declval<const T &>()) )>::value_type>,
62 std::is_convertible<
63 decltype( std::begin(arr: arr: arr: arr: arr: arr: arr: arr: arr: arr: arr: cont: arr: arr: arr: arr: arr: arr: arr: arr: arr: arr: cont: cont: cont: cont: cont: cont: std::declval<const T &>()) != std::end(arr: arr: arr: arr: arr: arr: arr: arr: arr: arr: arr: cont: arr: arr: arr: arr: arr: arr: arr: arr: arr: arr: cont: cont: cont: cont: cont: cont: std::declval<const T &>()) ),
64 bool>,
65
66 // These need to be treated specially due to the empty vs null distinction
67 std::negation<std::is_same<std::decay_t<T>, QString>>,
68#define QSTRINGVIEW_REFUSES_QSTRINGREF 1
69 std::negation<std::is_same<q20::remove_cvref_t<T>, QStringRef>>, // QStringRef::op QStringView()
70
71 // Don't make an accidental copy constructor
72 std::negation<std::is_same<std::decay_t<T>, QStringView>>
73 >>> : std::true_type {};
74
75} // namespace QtPrivate
76
77class QStringView
78{
79public:
80 typedef char16_t storage_type;
81 typedef const QChar value_type;
82 typedef std::ptrdiff_t difference_type;
83 typedef qsizetype size_type;
84 typedef value_type &reference;
85 typedef value_type &const_reference;
86 typedef value_type *pointer;
87 typedef value_type *const_pointer;
88
89 typedef pointer iterator;
90 typedef const_pointer const_iterator;
91 typedef std::reverse_iterator<iterator> reverse_iterator;
92 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
93
94private:
95 template <typename Char>
96 using if_compatible_char = typename std::enable_if<QtPrivate::IsCompatibleCharType<Char>::value, bool>::type;
97
98 template <typename Pointer>
99 using if_compatible_pointer = typename std::enable_if<QtPrivate::IsCompatiblePointer<Pointer>::value, bool>::type;
100
101 template <typename T>
102 using if_compatible_qstring_like = typename std::enable_if<std::is_same<T, QString>::value, bool>::type;
103
104 template <typename T>
105 using if_compatible_container = typename std::enable_if<QtPrivate::IsContainerCompatibleWithQStringView<T>::value, bool>::type;
106
107 template <typename Char>
108 static constexpr qsizetype lengthHelperPointer(const Char *str) noexcept
109 {
110 if (q20::is_constant_evaluated())
111 return QtPrivate::lengthHelperPointer(str);
112 return QtPrivate::qustrlen(str: reinterpret_cast<const char16_t *>(str));
113 }
114 static qsizetype lengthHelperPointer(const QChar *str) noexcept
115 {
116 return QtPrivate::qustrlen(str: reinterpret_cast<const char16_t *>(str));
117 }
118
119 template <typename Char>
120 static const storage_type *castHelper(const Char *str) noexcept
121 { return reinterpret_cast<const storage_type*>(str); }
122 static constexpr const storage_type *castHelper(const storage_type *str) noexcept
123 { return str; }
124
125public:
126 constexpr QStringView() noexcept {}
127 constexpr QStringView(std::nullptr_t) noexcept
128 : QStringView() {}
129
130 template <typename Char, if_compatible_char<Char> = true>
131 constexpr QStringView(const Char *str, qsizetype len)
132#if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0) || defined(QT_BOOTSTRAPPED)
133 : m_data(castHelper(str)),
134 m_size((Q_PRE(len >= 0), Q_PRE(str || !len), len))
135#else
136 : m_size((Q_PRE(len >= 0), Q_PRE(str || !len), len)),
137 m_data(castHelper(str))
138#endif
139 {}
140
141 template <typename Char, if_compatible_char<Char> = true>
142 constexpr QStringView(const Char *f, const Char *l)
143 : QStringView(f, l - f) {}
144
145#ifdef Q_QDOC
146 template <typename Char, size_t N>
147 constexpr QStringView(const Char (&array)[N]) noexcept;
148
149 template <typename Char>
150 constexpr QStringView(const Char *str) noexcept;
151#else
152 template <typename Pointer, if_compatible_pointer<Pointer> = true>
153 constexpr QStringView(const Pointer &str) noexcept
154 : QStringView(str, str ? lengthHelperPointer(str) : 0) {}
155
156 template <typename Char, if_compatible_char<Char> = true>
157 constexpr QStringView(const Char (&str)[]) noexcept // array of unknown bounds
158 : QStringView{&*str} {} // decay to pointer
159#endif
160
161#ifdef Q_QDOC
162 QStringView(const QString &str) noexcept;
163#else
164 template <typename String, if_compatible_qstring_like<String> = true>
165 QStringView(const String &str) noexcept
166 : QStringView{str.begin(), str.size()} {}
167#endif
168
169 template <typename Container, if_compatible_container<Container> = true>
170 Q_ALWAYS_INLINE constexpr QStringView(const Container &c) noexcept
171 : QStringView(std::data(c), QtPrivate::lengthHelperContainer(c)) {}
172
173 template <typename Char, size_t Size, if_compatible_char<Char> = true>
174 [[nodiscard]] constexpr static QStringView fromArray(const Char (&string)[Size]) noexcept
175 { return QStringView(string, Size); }
176
177 [[nodiscard]] inline QString toString() const; // defined in qstring.h
178#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
179 // defined in qcore_foundation.mm
180 [[nodiscard]] Q_CORE_EXPORT CFStringRef toCFString() const Q_DECL_CF_RETURNS_RETAINED;
181 [[nodiscard]] Q_CORE_EXPORT NSString *toNSString() const Q_DECL_NS_RETURNS_AUTORELEASED;
182#endif
183
184 [[nodiscard]] constexpr qsizetype size() const noexcept { return m_size; }
185 [[nodiscard]] const_pointer data() const noexcept { return reinterpret_cast<const_pointer>(m_data); }
186 [[nodiscard]] const_pointer constData() const noexcept { return data(); }
187 [[nodiscard]] constexpr const storage_type *utf16() const noexcept { return m_data; }
188
189 [[nodiscard]] constexpr QChar operator[](qsizetype n) const
190 { verify(pos: n, n: 1); return QChar(m_data[n]); }
191
192 //
193 // QString API
194 //
195
196 template <typename...Args>
197 [[nodiscard]] inline QString arg(Args &&...args) const; // defined in qstring.h
198
199 [[nodiscard]] QByteArray toLatin1() const { return QtPrivate::convertToLatin1(str: *this); }
200 [[nodiscard]] QByteArray toUtf8() const { return QtPrivate::convertToUtf8(str: *this); }
201 [[nodiscard]] QByteArray toLocal8Bit() const { return QtPrivate::convertToLocal8Bit(str: *this); }
202 [[nodiscard]] inline QList<uint> toUcs4() const; // defined in qlist.h ### Qt 7 char32_t
203
204 [[nodiscard]] constexpr QChar at(qsizetype n) const noexcept { return (*this)[n]; }
205
206 [[nodiscard]] constexpr QStringView mid(qsizetype pos, qsizetype n = -1) const noexcept
207 {
208 using namespace QtPrivate;
209 auto result = QContainerImplHelper::mid(originalLength: size(), position: &pos, length: &n);
210 return result == QContainerImplHelper::Null ? QStringView() : QStringView(m_data + pos, n);
211 }
212 [[nodiscard]] constexpr QStringView left(qsizetype n) const noexcept
213 {
214 if (size_t(n) >= size_t(size()))
215 n = size();
216 return QStringView(m_data, n);
217 }
218 [[nodiscard]] constexpr QStringView right(qsizetype n) const noexcept
219 {
220 if (size_t(n) >= size_t(size()))
221 n = size();
222 return QStringView(m_data + m_size - n, n);
223 }
224
225 [[nodiscard]] constexpr QStringView first(qsizetype n) const noexcept
226 { verify(pos: 0, n); return sliced(pos: 0, n); }
227 [[nodiscard]] constexpr QStringView last(qsizetype n) const noexcept
228 { verify(pos: 0, n); return sliced(pos: size() - n, n); }
229 [[nodiscard]] constexpr QStringView sliced(qsizetype pos) const noexcept
230 { verify(pos, n: 0); return QStringView(m_data + pos, size() - pos); }
231 [[nodiscard]] constexpr QStringView sliced(qsizetype pos, qsizetype n) const noexcept
232 { verify(pos, n); return QStringView(m_data + pos, n); }
233 [[nodiscard]] constexpr QStringView chopped(qsizetype n) const noexcept
234 { verify(pos: 0, n); return sliced(pos: 0, n: m_size - n); }
235
236 constexpr void truncate(qsizetype n) noexcept
237 { verify(pos: 0, n); ; m_size = n; }
238 constexpr void chop(qsizetype n) noexcept
239 { verify(pos: 0, n); m_size -= n; }
240
241 [[nodiscard]] QStringView trimmed() const noexcept { return QtPrivate::trimmed(s: *this); }
242
243 constexpr QStringView &slice(qsizetype pos)
244 { *this = sliced(pos); return *this; }
245 constexpr QStringView &slice(qsizetype pos, qsizetype n)
246 { *this = sliced(pos, n); return *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]] inline int compare(QUtf8StringView other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
258 [[nodiscard]] constexpr int compare(QChar c) const noexcept
259 { return size() >= 1 ? compare_single_char_helper(diff: *utf16() - c.unicode()) : -1; }
260 [[nodiscard]] int compare(QChar c, Qt::CaseSensitivity cs) const noexcept
261 { return QtPrivate::compareStrings(lhs: *this, rhs: QStringView(&c, 1), cs); }
262
263 [[nodiscard]] inline int localeAwareCompare(QStringView other) const;
264
265 [[nodiscard]] bool startsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
266 { return QtPrivate::startsWith(haystack: *this, needle: s, cs); }
267 [[nodiscard]] inline bool startsWith(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
268 [[nodiscard]] bool startsWith(QChar c) const noexcept
269 { return !empty() && front() == c; }
270 [[nodiscard]] bool startsWith(QChar c, Qt::CaseSensitivity cs) const noexcept
271 { return QtPrivate::startsWith(haystack: *this, needle: QStringView(&c, 1), cs); }
272
273 [[nodiscard]] bool endsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
274 { return QtPrivate::endsWith(haystack: *this, needle: s, cs); }
275 [[nodiscard]] inline bool endsWith(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
276 [[nodiscard]] bool endsWith(QChar c) const noexcept
277 { return !empty() && back() == c; }
278 [[nodiscard]] bool endsWith(QChar c, Qt::CaseSensitivity cs) const noexcept
279 { return QtPrivate::endsWith(haystack: *this, needle: QStringView(&c, 1), cs); }
280
281 [[nodiscard]] qsizetype indexOf(QChar c, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
282 { return QtPrivate::findString(str: *this, from, needle: c.unicode(), cs); }
283 [[nodiscard]] qsizetype indexOf(QStringView s, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
284 { return QtPrivate::findString(haystack: *this, from, needle: s, cs); }
285 [[nodiscard]] inline qsizetype indexOf(QLatin1StringView s, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
286
287 [[nodiscard]] bool contains(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
288 { return indexOf(s: QStringView(&c, 1), from: 0, cs) != qsizetype(-1); }
289 [[nodiscard]] bool contains(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
290 { return indexOf(s, from: 0, cs) != qsizetype(-1); }
291 [[nodiscard]] inline bool contains(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
292
293 [[nodiscard]] qsizetype count(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
294 { return QtPrivate::count(haystack: *this, needle: c, cs); }
295 [[nodiscard]] qsizetype count(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
296 { return QtPrivate::count(haystack: *this, needle: s, cs); }
297 [[nodiscard]] inline qsizetype count(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
298
299 [[nodiscard]] qsizetype lastIndexOf(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
300 { return lastIndexOf(c, from: -1, cs); }
301 [[nodiscard]] qsizetype lastIndexOf(QChar c, qsizetype from, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
302 { return QtPrivate::lastIndexOf(haystack: *this, from, needle: c.unicode(), cs); }
303 [[nodiscard]] qsizetype lastIndexOf(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
304 { return lastIndexOf(s, from: size(), cs); }
305 [[nodiscard]] qsizetype lastIndexOf(QStringView s, qsizetype from, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
306 { return QtPrivate::lastIndexOf(haystack: *this, from, needle: s, cs); }
307 [[nodiscard]] inline qsizetype lastIndexOf(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
308 [[nodiscard]] inline qsizetype lastIndexOf(QLatin1StringView s, qsizetype from, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
309
310#if QT_CONFIG(regularexpression)
311 [[nodiscard]] qsizetype indexOf(const QRegularExpression &re, qsizetype from = 0, QRegularExpressionMatch *rmatch = nullptr) const
312 {
313 return QtPrivate::indexOf(haystack: *this, re, from, rmatch);
314 }
315#ifdef Q_QDOC
316 [[nodiscard]] qsizetype lastIndexOf(const QRegularExpression &re, QRegularExpressionMatch *rmatch = nullptr) const;
317#else
318 // prevent an ambiguity when called like this: lastIndexOf(re, 0)
319 template <typename T = QRegularExpressionMatch, std::enable_if_t<std::is_same_v<T, QRegularExpressionMatch>, bool> = false>
320 [[nodiscard]] qsizetype lastIndexOf(const QRegularExpression &re, T *rmatch = nullptr) const
321 {
322 return QtPrivate::lastIndexOf(*this, re, size(), rmatch);
323 }
324#endif
325 [[nodiscard]] qsizetype lastIndexOf(const QRegularExpression &re, qsizetype from, QRegularExpressionMatch *rmatch = nullptr) const
326 {
327 return QtPrivate::lastIndexOf(haystack: *this, re, from, rmatch);
328 }
329 [[nodiscard]] bool contains(const QRegularExpression &re, QRegularExpressionMatch *rmatch = nullptr) const
330 {
331 return QtPrivate::contains(haystack: *this, re, rmatch);
332 }
333 [[nodiscard]] qsizetype count(const QRegularExpression &re) const
334 {
335 return QtPrivate::count(haystack: *this, re);
336 }
337#endif
338
339 [[nodiscard]] bool isRightToLeft() const noexcept
340 { return QtPrivate::isRightToLeft(string: *this); }
341 [[nodiscard]] bool isValidUtf16() const noexcept
342 { return QtPrivate::isValidUtf16(s: *this); }
343
344 [[nodiscard]] bool isUpper() const noexcept
345 { return QtPrivate::isUpper(s: *this); }
346 [[nodiscard]] bool isLower() const noexcept
347 { return QtPrivate::isLower(s: *this); }
348
349 [[nodiscard]] inline short toShort(bool *ok = nullptr, int base = 10) const;
350 [[nodiscard]] inline ushort toUShort(bool *ok = nullptr, int base = 10) const;
351 [[nodiscard]] inline int toInt(bool *ok = nullptr, int base = 10) const;
352 [[nodiscard]] inline uint toUInt(bool *ok = nullptr, int base = 10) const;
353 [[nodiscard]] inline long toLong(bool *ok = nullptr, int base = 10) const;
354 [[nodiscard]] inline ulong toULong(bool *ok = nullptr, int base = 10) const;
355 [[nodiscard]] inline qlonglong toLongLong(bool *ok = nullptr, int base = 10) const;
356 [[nodiscard]] inline qulonglong toULongLong(bool *ok = nullptr, int base = 10) const;
357 [[nodiscard]] Q_CORE_EXPORT float toFloat(bool *ok = nullptr) const;
358 [[nodiscard]] Q_CORE_EXPORT double toDouble(bool *ok = nullptr) const;
359
360 [[nodiscard]] inline qsizetype toWCharArray(wchar_t *array) const; // defined in qstring.h
361
362
363 [[nodiscard]] Q_CORE_EXPORT
364 QList<QStringView> split(QStringView sep,
365 Qt::SplitBehavior behavior = Qt::KeepEmptyParts,
366 Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
367 [[nodiscard]] Q_CORE_EXPORT
368 QList<QStringView> split(QChar sep, Qt::SplitBehavior behavior = Qt::KeepEmptyParts,
369 Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
370
371#if QT_CONFIG(regularexpression)
372 [[nodiscard]] Q_CORE_EXPORT
373 QList<QStringView> split(const QRegularExpression &sep,
374 Qt::SplitBehavior behavior = Qt::KeepEmptyParts) const;
375#endif
376
377 // QStringView <> QStringView
378 friend bool comparesEqual(const QStringView &lhs, const QStringView &rhs) noexcept
379 { return lhs.size() == rhs.size() && QtPrivate::equalStrings(lhs, rhs); }
380 friend Qt::strong_ordering
381 compareThreeWay(const QStringView &lhs, const QStringView &rhs) noexcept
382 {
383 const int res = QtPrivate::compareStrings(lhs, rhs);
384 return Qt::compareThreeWay(lhs: res, rhs: 0);
385 }
386 Q_DECLARE_STRONGLY_ORDERED(QStringView)
387
388 // QStringView <> QChar
389 friend bool comparesEqual(const QStringView &lhs, QChar rhs) noexcept
390 { return lhs.size() == 1 && lhs[0] == rhs; }
391 friend Qt::strong_ordering compareThreeWay(const QStringView &lhs, QChar rhs) noexcept
392 { return compareThreeWay(lhs, rhs: QStringView(&rhs, 1)); }
393 Q_DECLARE_STRONGLY_ORDERED(QStringView, QChar)
394
395 //
396 // STL compatibility API:
397 //
398 [[nodiscard]] const_iterator begin() const noexcept { return data(); }
399 [[nodiscard]] const_iterator end() const noexcept { return data() + size(); }
400 [[nodiscard]] const_iterator cbegin() const noexcept { return begin(); }
401 [[nodiscard]] const_iterator cend() const noexcept { return end(); }
402 [[nodiscard]] const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); }
403 [[nodiscard]] const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); }
404 [[nodiscard]] const_reverse_iterator crbegin() const noexcept { return rbegin(); }
405 [[nodiscard]] const_reverse_iterator crend() const noexcept { return rend(); }
406
407 [[nodiscard]] constexpr bool empty() const noexcept { return size() == 0; }
408 [[nodiscard]] constexpr QChar front() const { return Q_PRE(!empty()), QChar(m_data[0]); }
409 [[nodiscard]] constexpr QChar back() const { return Q_PRE(!empty()), QChar(m_data[m_size - 1]); }
410
411 [[nodiscard]] Q_IMPLICIT operator std::u16string_view() const noexcept
412 { return std::u16string_view(m_data, size_t(m_size)); }
413
414 [[nodiscard]] constexpr qsizetype max_size() const noexcept { return maxSize(); }
415
416 //
417 // Qt compatibility API:
418 //
419 [[nodiscard]] const_iterator constBegin() const noexcept { return begin(); }
420 [[nodiscard]] const_iterator constEnd() const noexcept { return end(); }
421 [[nodiscard]] constexpr bool isNull() const noexcept { return !m_data; }
422 [[nodiscard]] constexpr bool isEmpty() const noexcept { return empty(); }
423 [[nodiscard]] constexpr qsizetype length() const noexcept
424 { return size(); }
425 [[nodiscard]] constexpr QChar first() const { return front(); }
426 [[nodiscard]] constexpr QChar last() const { return back(); }
427
428 [[nodiscard]] static constexpr qsizetype maxSize() noexcept
429 {
430 // -1 to deal with the pointer one-past-the-end;
431 return QtPrivate::MaxAllocSize / sizeof(storage_type) - 1;
432 }
433private:
434#if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0) || defined(QT_BOOTSTRAPPED)
435 const storage_type *m_data = nullptr;
436 qsizetype m_size = 0;
437#else
438 qsizetype m_size = 0;
439 const storage_type *m_data = nullptr;
440#endif
441
442 Q_ALWAYS_INLINE constexpr void verify([[maybe_unused]] qsizetype pos = 0,
443 [[maybe_unused]] qsizetype n = 1) const
444 {
445 Q_PRE(pos >= 0);
446 Q_PRE(pos <= size());
447 Q_PRE(n >= 0);
448 Q_PRE(n <= size() - pos);
449 }
450
451 constexpr int compare_single_char_helper(int diff) const noexcept
452 { return diff ? diff : size() > 1 ? 1 : 0; }
453
454 Q_CORE_EXPORT static bool equal_helper(QStringView sv, const char *data, qsizetype len);
455 Q_CORE_EXPORT static int compare_helper(QStringView sv, const char *data, qsizetype len);
456
457#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
458 friend bool comparesEqual(const QStringView &lhs, const QByteArrayView &rhs) noexcept
459 { return equal_helper(sv: lhs, data: rhs.data(), len: rhs.size()); }
460 friend Qt::strong_ordering
461 compareThreeWay(const QStringView &lhs, const QByteArrayView &rhs) noexcept
462 {
463 const int res = compare_helper(sv: lhs, data: rhs.data(), len: rhs.size());
464 return Qt::compareThreeWay(lhs: res, rhs: 0);
465 }
466 Q_DECLARE_STRONGLY_ORDERED(QStringView, QByteArrayView, QT_ASCII_CAST_WARN)
467 Q_DECLARE_STRONGLY_ORDERED(QStringView, QByteArray, QT_ASCII_CAST_WARN)
468 Q_DECLARE_STRONGLY_ORDERED(QStringView, const char *, QT_ASCII_CAST_WARN)
469#endif // !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
470};
471Q_DECLARE_TYPEINFO(QStringView, Q_PRIMITIVE_TYPE);
472
473template <typename QStringLike, typename std::enable_if<
474 std::is_same<QStringLike, QString>::value,
475 bool>::type = true>
476inline QStringView qToStringViewIgnoringNull(const QStringLike &s) noexcept
477{ return QStringView(s.begin(), s.size()); }
478
479// QChar inline functions:
480
481[[nodiscard]] constexpr auto QChar::fromUcs4(char32_t c) noexcept
482{
483 struct R {
484 char16_t chars[2];
485 [[nodiscard]] constexpr operator QStringView() const noexcept { return {begin(), end()}; }
486 [[nodiscard]] constexpr qsizetype size() const noexcept { return chars[1] ? 2 : 1; }
487 [[nodiscard]] constexpr const char16_t *begin() const noexcept { return chars; }
488 [[nodiscard]] constexpr const char16_t *end() const noexcept { return begin() + size(); }
489 };
490 return requiresSurrogates(ucs4: c) ? R{.chars: {QChar::highSurrogate(ucs4: c),
491 QChar::lowSurrogate(ucs4: c)}} :
492 R{.chars: {char16_t(c), u'\0'}} ;
493}
494
495qsizetype QtPrivate::findString(QStringView str, qsizetype from, QChar ch, Qt::CaseSensitivity cs) noexcept
496{
497 if (from < -str.size()) // from < 0 && abs(from) > str.size(), avoiding overflow
498 return -1;
499 if (from < 0)
500 from = qMax(a: from + str.size(), b: qsizetype(0));
501 if (from < str.size()) {
502 const char16_t *s = str.utf16();
503 char16_t c = ch.unicode();
504 const char16_t *n = s + from;
505 const char16_t *e = s + str.size();
506 if (cs == Qt::CaseSensitive)
507 n = qustrchr(str: QStringView(n, e), ch: c);
508 else
509 n = qustrcasechr(str: QStringView(n, e), ch: c);
510 if (n != e)
511 return n - s;
512 }
513 return -1;
514}
515
516namespace Qt {
517inline namespace Literals {
518inline namespace StringLiterals {
519constexpr QStringView operator""_sv(const char16_t *str, size_t size) noexcept
520{
521 return QStringView(str, qsizetype(size));
522}
523} // StringLiterals
524} // Literals
525} // Qt
526
527QT_END_NAMESPACE
528
529#endif /* QSTRINGVIEW_H */
530

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