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
qstringview.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// 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/qstringalgorithms.h>
14
15#include <string>
16#include <string_view>
17#include <QtCore/q20type_traits.h>
18
19#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
20Q_FORWARD_DECLARE_CF_TYPE(CFString);
21Q_FORWARD_DECLARE_OBJC_CLASS(NSString);
22#endif
23
24QT_BEGIN_NAMESPACE
25
26class QRegularExpression;
27class QRegularExpressionMatch;
28
29namespace QtPrivate {
30template <typename Char>
32 : std::integral_constant<bool,
35 std::is_same<Char, char16_t>::value ||
36 (std::is_same<Char, wchar_t>::value && sizeof(wchar_t) == sizeof(QChar))> {};
37template <typename Char>
40
41template <typename Pointer>
43template <typename Char>
46template <typename Pointer>
49
50template <typename T, typename Enable = void>
52
53template <typename T>
55 // lacking concepts and ranges, we accept any T whose std::data yields a suitable pointer ...
56 IsCompatiblePointer<decltype( std::data(std::declval<const T &>()) )>,
57 // ... and that has a suitable size ...
58 std::is_convertible<decltype( std::size(std::declval<const T &>()) ), qsizetype>,
59 // ... and it's a range as it defines an iterator-like API
60 IsCompatibleCharType<typename std::iterator_traits<decltype( std::begin(std::declval<const T &>()) )>::value_type>,
62 decltype( std::begin(std::declval<const T &>()) != std::end(std::declval<const T &>()) ),
63 bool>,
64
65 // These need to be treated specially due to the empty vs null distinction
67#define QSTRINGVIEW_REFUSES_QSTRINGREF 1
68 std::negation<std::is_same<q20::remove_cvref_t<T>, QStringRef>>, // QStringRef::op QStringView()
69
70 // Don't make an accidental copy constructor
72 >>> : std::true_type {};
73
74} // namespace QtPrivate
75
77{
78public:
79 typedef char16_t storage_type;
80 typedef const QChar value_type;
81 typedef std::ptrdiff_t difference_type;
87
90 typedef std::reverse_iterator<iterator> reverse_iterator;
91 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
92
93private:
94 template <typename Char>
95 using if_compatible_char = typename std::enable_if<QtPrivate::IsCompatibleCharType<Char>::value, bool>::type;
96
97 template <typename Pointer>
98 using if_compatible_pointer = typename std::enable_if<QtPrivate::IsCompatiblePointer<Pointer>::value, bool>::type;
99
100 template <typename T>
101 using if_compatible_qstring_like = typename std::enable_if<std::is_same<T, QString>::value, bool>::type;
102
103 template <typename T>
105
106 template <typename Char>
107 static constexpr qsizetype lengthHelperPointer(const Char *str) noexcept
108 {
109 if (q20::is_constant_evaluated())
110 return QtPrivate::lengthHelperPointer(str);
111 return QtPrivate::qustrlen(reinterpret_cast<const char16_t *>(str));
112 }
113 static qsizetype lengthHelperPointer(const QChar *str) noexcept
114 {
115 return QtPrivate::qustrlen(reinterpret_cast<const char16_t *>(str));
116 }
117
118 template <typename Char>
119 static const storage_type *castHelper(const Char *str) noexcept
120 { return reinterpret_cast<const storage_type*>(str); }
121 static constexpr const storage_type *castHelper(const storage_type *str) noexcept
122 { return str; }
123
124public:
125 constexpr QStringView() noexcept {}
126 constexpr QStringView(std::nullptr_t) noexcept
127 : QStringView() {}
128
129 template <typename Char, if_compatible_char<Char> = true>
130 constexpr QStringView(const Char *str, qsizetype len)
131#if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0) || defined(QT_BOOTSTRAPPED)
133 m_size((Q_PRE(len >= 0), Q_PRE(str || !len), len))
134#else
135 : m_size((Q_PRE(len >= 0), Q_PRE(str || !len), len)),
136 m_data(castHelper(str))
137#endif
138 {}
139
140 template <typename Char, if_compatible_char<Char> = true>
141 constexpr QStringView(const Char *f, const Char *l)
142 : QStringView(f, l - f) {}
143
144#ifdef Q_QDOC
145 template <typename Char, size_t N>
146 constexpr QStringView(const Char (&array)[N]) noexcept;
147
148 template <typename Char>
149 constexpr QStringView(const Char *str) noexcept;
150#else
151 template <typename Pointer, if_compatible_pointer<Pointer> = true>
152 constexpr QStringView(const Pointer &str) noexcept
153 : QStringView(str, str ? lengthHelperPointer(str) : 0) {}
154
155 template <typename Char, if_compatible_char<Char> = true>
156 constexpr QStringView(const Char (&str)[]) noexcept // array of unknown bounds
157 : QStringView{&*str} {} // decay to pointer
158#endif
159
160#ifdef Q_QDOC
161 QStringView(const QString &str) noexcept;
162#else
163 template <typename String, if_compatible_qstring_like<String> = true>
164 QStringView(const String &str) noexcept
165 : QStringView{str.begin(), str.size()} {}
166#endif
167
168 template <typename Container, if_compatible_container<Container> = true>
171
172 template <typename Char, size_t Size, if_compatible_char<Char> = true>
173 [[nodiscard]] constexpr static QStringView fromArray(const Char (&string)[Size]) noexcept
174 { return QStringView(string, Size); }
175
176 [[nodiscard]] inline QString toString() const; // defined in qstring.h
177#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
178 // defined in qcore_foundation.mm
181#endif
182
183 [[nodiscard]] static constexpr qsizetype maxSize() noexcept
184 {
185 // -1 to deal with the pointer one-past-the-end;
186 return QtPrivate::MaxAllocSize / sizeof(storage_type) - 1;
187 }
188 [[nodiscard]] constexpr qsizetype size() const noexcept
189 {
190 constexpr size_t MaxSize = maxSize();
191 Q_PRESUME(size_t(m_size) <= MaxSize);
192 return m_size;
193 }
194 [[nodiscard]] const_pointer data() const noexcept { return reinterpret_cast<const_pointer>(m_data); }
195 [[nodiscard]] const_pointer constData() const noexcept { return data(); }
196 [[nodiscard]] constexpr const storage_type *utf16() const noexcept { return m_data; }
197
198 [[nodiscard]] constexpr QChar operator[](qsizetype n) const
199 { verify(n, 1); return QChar(m_data[n]); }
200
201 //
202 // QString API
203 //
204
205 template <typename...Args>
206 [[nodiscard]] inline QString arg(Args &&...args) const; // defined in qstring.h
207
208 [[nodiscard]] QByteArray toLatin1() const { return QtPrivate::convertToLatin1(*this); }
209 [[nodiscard]] QByteArray toUtf8() const { return QtPrivate::convertToUtf8(*this); }
210 [[nodiscard]] QByteArray toLocal8Bit() const { return QtPrivate::convertToLocal8Bit(*this); }
211 [[nodiscard]] inline QList<uint> toUcs4() const; // defined in qlist.h ### Qt 7 char32_t
212
213 [[nodiscard]] constexpr QChar at(qsizetype n) const QT6_ONLY(noexcept) { return (*this)[n]; }
214
215 [[nodiscard]] constexpr QStringView mid(qsizetype pos, qsizetype n = -1) const noexcept
216 {
217 using namespace QtPrivate;
218 auto result = QContainerImplHelper::mid(size(), &pos, &n);
220 }
221 [[nodiscard]] constexpr QStringView left(qsizetype n) const noexcept
222 {
223 if (size_t(n) >= size_t(size()))
224 n = size();
225 return QStringView(m_data, n);
226 }
227 [[nodiscard]] constexpr QStringView right(qsizetype n) const noexcept
228 {
229 if (size_t(n) >= size_t(size()))
230 n = size();
231 return QStringView(m_data + m_size - n, n);
232 }
233
234 [[nodiscard]] constexpr QStringView first(qsizetype n) const QT6_ONLY(noexcept)
235 { verify(0, n); return sliced(0, n); }
236 [[nodiscard]] constexpr QStringView last(qsizetype n) const QT6_ONLY(noexcept)
237 { verify(0, n); return sliced(size() - n, n); }
238 [[nodiscard]] constexpr QStringView sliced(qsizetype pos) const QT6_ONLY(noexcept)
239 { verify(pos, 0); return QStringView(m_data + pos, size() - pos); }
240 [[nodiscard]] constexpr QStringView sliced(qsizetype pos, qsizetype n) const QT6_ONLY(noexcept)
241 { verify(pos, n); return QStringView(m_data + pos, n); }
242 [[nodiscard]] constexpr QStringView chopped(qsizetype n) const QT6_ONLY(noexcept)
243 { verify(0, n); return sliced(0, m_size - n); }
244
245 constexpr void truncate(qsizetype n) QT6_ONLY(noexcept)
246 { verify(0, n); ; m_size = n; }
247 constexpr void chop(qsizetype n) QT6_ONLY(noexcept)
248 { verify(0, n); m_size -= n; }
249
250 [[nodiscard]] QStringView trimmed() const noexcept { return QtPrivate::trimmed(*this); }
251
253 { *this = sliced(pos); return *this; }
255 { *this = sliced(pos, n); return *this; }
256
257 template <typename Needle, typename...Flags>
258 [[nodiscard]] constexpr inline auto tokenize(Needle &&needle, Flags...flags) const
259 noexcept(noexcept(qTokenize(std::declval<const QStringView&>(), std::forward<Needle>(needle), flags...)))
260 -> decltype(qTokenize(*this, std::forward<Needle>(needle), flags...))
261 { return qTokenize(*this, std::forward<Needle>(needle), flags...); }
262
264 {
265#if (!defined(Q_CC_GNU_ONLY) || Q_CC_GNU_ONLY >= 1700) && __has_builtin(__builtin_constant_p)
267 return compare(other.front(), cs);
268#endif
269 return QtPrivate::compareStrings(*this, other, cs);
270 }
271 [[nodiscard]] inline int compare(QLatin1StringView other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
272 [[nodiscard]] inline int compare(QUtf8StringView other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
273 [[nodiscard]] constexpr int compare(QChar c) const noexcept
274 { return size() >= 1 ? compare_single_char_helper(*utf16() - c.unicode()) : -1; }
275 [[nodiscard]] int compare(QChar c, Qt::CaseSensitivity cs) const noexcept
276 {
277#if (!defined(Q_CC_GNU_ONLY) || Q_CC_GNU_ONLY >= 1700) && __has_builtin(__builtin_constant_p)
278 if (__builtin_constant_p(cs) && cs == Qt::CaseSensitive)
279 return compare(c);
280#endif
281 return QtPrivate::compareStrings(*this, QStringView(&c, 1), cs);
282 }
283
284 [[nodiscard]] inline int localeAwareCompare(QStringView other) const;
285
286 [[nodiscard]] bool startsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
287 {
288#if (!defined(Q_CC_GNU_ONLY) || Q_CC_GNU_ONLY >= 1700) && __has_builtin(__builtin_constant_p)
289 if (__builtin_constant_p(s.m_size) && s.size() == 1)
290 return startsWith(s.front(), cs);
291#endif
292 return QtPrivate::startsWith(*this, s, cs);
293 }
294 [[nodiscard]] inline bool startsWith(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
295 [[nodiscard]] bool startsWith(QChar c) const noexcept
296 { return !empty() && front() == c; }
297 [[nodiscard]] bool startsWith(QChar c, Qt::CaseSensitivity cs) const noexcept
298 {
299#if (!defined(Q_CC_GNU_ONLY) || Q_CC_GNU_ONLY >= 1700) && __has_builtin(__builtin_constant_p)
300 if (__builtin_constant_p(cs) && cs == Qt::CaseSensitive)
301 return startsWith(c);
302#endif
303 return QtPrivate::startsWith(*this, QStringView(&c, 1), cs);
304 }
305
306 [[nodiscard]] bool endsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
307 {
308#if (!defined(Q_CC_GNU_ONLY) || Q_CC_GNU_ONLY >= 1700) && __has_builtin(__builtin_constant_p)
309 if (__builtin_constant_p(s.m_size) && s.size() == 1)
310 return endsWith(s.front(), cs);
311#endif
312 return QtPrivate::endsWith(*this, s, cs);
313 }
314 [[nodiscard]] inline bool endsWith(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
315 [[nodiscard]] bool endsWith(QChar c) const noexcept
316 { return !empty() && back() == c; }
317 [[nodiscard]] bool endsWith(QChar c, Qt::CaseSensitivity cs) const noexcept
318 {
319#if (!defined(Q_CC_GNU_ONLY) || Q_CC_GNU_ONLY >= 1700) && __has_builtin(__builtin_constant_p)
320 if (__builtin_constant_p(cs) && cs == Qt::CaseSensitive)
321 return endsWith(c);
322#endif
323 return QtPrivate::endsWith(*this, QStringView(&c, 1), cs);
324 }
325
326 [[nodiscard]] qsizetype indexOf(QChar c, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
327 { return QtPrivate::findString(*this, from, c.unicode(), cs); }
328 [[nodiscard]] qsizetype indexOf(QStringView s, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
329 {
330#if (!defined(Q_CC_GNU_ONLY) || Q_CC_GNU_ONLY >= 1700) && __has_builtin(__builtin_constant_p)
331 if (__builtin_constant_p(s.m_size) && s.size() == 1)
332 return indexOf(s.front(), from, cs);
333#endif
334 return QtPrivate::findString(*this, from, s, cs);
335 }
336 [[nodiscard]] inline qsizetype indexOf(QLatin1StringView s, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
337
338 [[nodiscard]] bool contains(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
339 { return indexOf(c, 0, cs) != qsizetype(-1); }
340 [[nodiscard]] bool contains(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
341 { return indexOf(s, 0, cs) != qsizetype(-1); }
342 [[nodiscard]] inline bool contains(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
343
344 [[nodiscard]] qsizetype count(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
345 { return QtPrivate::count(*this, c, cs); }
346 [[nodiscard]] qsizetype count(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
347 {
348#if (!defined(Q_CC_GNU_ONLY) || Q_CC_GNU_ONLY >= 1700) && __has_builtin(__builtin_constant_p)
349 if (__builtin_constant_p(s.m_size) && s.size() == 1)
350 return count(s.front(), cs);
351#endif
352 return QtPrivate::count(*this, s, cs);
353 }
354 [[nodiscard]] inline qsizetype count(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
355
356 [[nodiscard]] qsizetype lastIndexOf(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
357 { return lastIndexOf(c, -1, cs); }
358 [[nodiscard]] qsizetype lastIndexOf(QChar c, qsizetype from, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
359 { return QtPrivate::lastIndexOf(*this, from, c.unicode(), cs); }
360 [[nodiscard]] qsizetype lastIndexOf(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
361 { return lastIndexOf(s, size(), cs); }
362 [[nodiscard]] qsizetype lastIndexOf(QStringView s, qsizetype from, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
363 {
364#if (!defined(Q_CC_GNU_ONLY) || Q_CC_GNU_ONLY >= 1700) && __has_builtin(__builtin_constant_p)
365 if (__builtin_constant_p(s.m_size) && s.size() == 1)
366 return lastIndexOf(s.front(), from, cs);
367#endif
368 return QtPrivate::lastIndexOf(*this, from, s, cs);
369 }
370 [[nodiscard]] inline qsizetype lastIndexOf(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
371 [[nodiscard]] inline qsizetype lastIndexOf(QLatin1StringView s, qsizetype from, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
372
373#if QT_CONFIG(regularexpression)
375 {
376 return QtPrivate::indexOf(*this, re, from, rmatch);
377 }
378#ifdef Q_QDOC
380#else
381 // prevent an ambiguity when called like this: lastIndexOf(re, 0)
382 template <typename T = QRegularExpressionMatch, std::enable_if_t<std::is_same_v<T, QRegularExpressionMatch>, bool> = false>
383 [[nodiscard]] qsizetype lastIndexOf(const QRegularExpression &re, T *rmatch = nullptr) const
384 {
385 return QtPrivate::lastIndexOf(*this, re, size(), rmatch);
386 }
387#endif
389 {
390 return QtPrivate::lastIndexOf(*this, re, from, rmatch);
391 }
392 [[nodiscard]] bool contains(const QRegularExpression &re, QRegularExpressionMatch *rmatch = nullptr) const
393 {
394 return QtPrivate::contains(*this, re, rmatch);
395 }
396 [[nodiscard]] qsizetype count(const QRegularExpression &re) const; // defined in qregularexpression.h
397#endif
398
399 [[nodiscard]] bool isRightToLeft() const noexcept
400 { return QtPrivate::isRightToLeft(*this); }
401 [[nodiscard]] bool isValidUtf16() const noexcept
402 { return QtPrivate::isValidUtf16(*this); }
403
404 [[nodiscard]] bool isUpper() const noexcept
405 { return QtPrivate::isUpper(*this); }
406 [[nodiscard]] bool isLower() const noexcept
407 { return QtPrivate::isLower(*this); }
408
409 [[nodiscard]] inline short toShort(bool *ok = nullptr, int base = 10) const;
410 [[nodiscard]] inline ushort toUShort(bool *ok = nullptr, int base = 10) const;
411 [[nodiscard]] inline int toInt(bool *ok = nullptr, int base = 10) const;
412 [[nodiscard]] inline uint toUInt(bool *ok = nullptr, int base = 10) const;
413 [[nodiscard]] inline long toLong(bool *ok = nullptr, int base = 10) const;
414 [[nodiscard]] inline ulong toULong(bool *ok = nullptr, int base = 10) const;
415 [[nodiscard]] inline qlonglong toLongLong(bool *ok = nullptr, int base = 10) const;
416 [[nodiscard]] inline qulonglong toULongLong(bool *ok = nullptr, int base = 10) const;
417 [[nodiscard]] Q_CORE_EXPORT float toFloat(bool *ok = nullptr) const;
418 [[nodiscard]] Q_CORE_EXPORT double toDouble(bool *ok = nullptr) const;
419
420 [[nodiscard]] inline qsizetype toWCharArray(wchar_t *array) const; // defined in qstring.h
421
422
423 [[nodiscard]] Q_CORE_EXPORT
430
431#if QT_CONFIG(regularexpression)
435#endif
436
437 // QStringView <> QStringView
438 friend bool comparesEqual(const QStringView &lhs, const QStringView &rhs) noexcept
439 { return lhs.size() == rhs.size() && QtPrivate::equalStrings(lhs, rhs); }
440 friend Qt::strong_ordering
441 compareThreeWay(const QStringView &lhs, const QStringView &rhs) noexcept
442 {
443 const int res = QtPrivate::compareStrings(lhs, rhs);
444 return Qt::compareThreeWay(res, 0);
445 }
447
448 // QStringView <> QChar
449 friend bool comparesEqual(const QStringView &lhs, QChar rhs) noexcept
450 { return lhs.size() == 1 && lhs[0] == rhs; }
451 friend Qt::strong_ordering compareThreeWay(const QStringView &lhs, QChar rhs) noexcept
452 { return compareThreeWay(lhs, QStringView(&rhs, 1)); }
453 Q_DECLARE_STRONGLY_ORDERED(QStringView, QChar)
454
455 //
456 // STL compatibility API:
457 //
458 [[nodiscard]] const_iterator begin() const noexcept { return data(); }
459 [[nodiscard]] const_iterator end() const noexcept { return data() + size(); }
460 [[nodiscard]] const_iterator cbegin() const noexcept { return begin(); }
461 [[nodiscard]] const_iterator cend() const noexcept { return end(); }
464 [[nodiscard]] const_reverse_iterator crbegin() const noexcept { return rbegin(); }
465 [[nodiscard]] const_reverse_iterator crend() const noexcept { return rend(); }
466
467 [[nodiscard]] constexpr bool empty() const noexcept { return size() == 0; }
468 [[nodiscard]] constexpr QChar front() const { return Q_PRE(!empty()), QChar(m_data[0]); }
469 [[nodiscard]] constexpr QChar back() const { return Q_PRE(!empty()), QChar(m_data[m_size - 1]); }
470
471 [[nodiscard]] constexpr Q_IMPLICIT operator std::u16string_view() const noexcept
472 { return std::u16string_view(m_data, size_t(m_size)); }
473
474 [[nodiscard]] constexpr qsizetype max_size() const noexcept { return maxSize(); }
475
476 //
477 // Qt compatibility API:
478 //
479 [[nodiscard]] const_iterator constBegin() const noexcept { return begin(); }
480 [[nodiscard]] const_iterator constEnd() const noexcept { return end(); }
481 [[nodiscard]] constexpr bool isNull() const noexcept { return !m_data; }
482 [[nodiscard]] constexpr bool isEmpty() const noexcept { return empty(); }
483 [[nodiscard]] constexpr qsizetype length() const noexcept
484 { return size(); }
485 [[nodiscard]] constexpr QChar first() const { return front(); }
486 [[nodiscard]] constexpr QChar last() const { return back(); }
487private:
488#if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0) || defined(QT_BOOTSTRAPPED)
489 const storage_type *m_data = nullptr;
490 qsizetype m_size = 0;
491#else
492 qsizetype m_size = 0;
493 const storage_type *m_data = nullptr;
494#endif
495
496 Q_ALWAYS_INLINE constexpr void verify([[maybe_unused]] qsizetype pos = 0,
497 [[maybe_unused]] qsizetype n = 1) const
498 {
499 Q_PRE(pos >= 0);
500 Q_PRE(pos <= size());
501 Q_PRE(n >= 0);
502 Q_PRE(n <= size() - pos);
503 }
504
505 constexpr int compare_single_char_helper(int diff) const noexcept
506 { return diff ? diff : size() > 1 ? 1 : 0; }
507
508 Q_CORE_EXPORT static bool equal_helper(QStringView sv, const char *data, qsizetype len);
509 Q_CORE_EXPORT static int compare_helper(QStringView sv, const char *data, qsizetype len);
510
511#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
512 friend bool comparesEqual(const QStringView &lhs, const QByteArrayView &rhs) noexcept
513 { return equal_helper(lhs, rhs.data(), rhs.size()); }
514 friend Qt::strong_ordering
515 compareThreeWay(const QStringView &lhs, const QByteArrayView &rhs) noexcept
516 {
517 const int res = compare_helper(lhs, rhs.data(), rhs.size());
518 return Qt::compareThreeWay(res, 0);
519 }
520 Q_DECLARE_STRONGLY_ORDERED(QStringView, QByteArrayView, QT_ASCII_CAST_WARN)
523#endif // !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
524};
526
527template <typename QStringLike, typename std::enable_if<
528 std::is_same<QStringLike, QString>::value,
529 bool>::type = true>
530inline QStringView qToStringViewIgnoringNull(const QStringLike &s) noexcept
531{ return QStringView(s.begin(), s.size()); }
532
533qsizetype QtPrivate::findString(QStringView str, qsizetype from, QChar ch, Qt::CaseSensitivity cs) noexcept
534{
535 const qsizetype size = str.size();
536 if (from < 0)
537 from += size;
538 if (size_t(from) < size_t(size)) {
539 const char16_t *s = str.utf16();
540 char16_t c = ch.unicode();
541 const char16_t *n = s + from;
542 const char16_t *e = s + size;
543 if (cs == Qt::CaseSensitive)
544 n = qustrchr(QStringView(n, e), c);
545 else
546 n = qustrcasechr(QStringView(n, e), c);
547 if (n != e) {
548 qsizetype ret = n - s;
549 Q_PRESUME(ret >= 0);
550 return ret;
551 }
552 }
553 return -1;
554}
555
556namespace Qt {
557inline namespace Literals {
558inline namespace StringLiterals {
559constexpr QStringView operator""_sv(const char16_t *str, size_t size) noexcept
560{
561 return QStringView(str, qsizetype(size));
562}
563} // StringLiterals
564} // Literals
565} // Qt
566
567QT_END_NAMESPACE
568
569#endif /* QSTRINGVIEW_H */
\inmodule QtCore
Definition qstringview.h:77
QString arg(Args &&...args) const
bool isLower() const noexcept
bool startsWith(QChar c) const noexcept
bool contains(QChar c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
qsizetype lastIndexOf(QStringView s, qsizetype from, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
Q_CORE_EXPORT double toDouble(bool *ok=nullptr) const
Returns the string view converted to a double value.
Definition qstring.cpp:7910
bool isRightToLeft() const noexcept
constexpr QStringView(std::nullptr_t) noexcept
Constructs a null string view.
Q_CORE_EXPORT float toFloat(bool *ok=nullptr) const
Returns the string view converted to a float value.
Definition qstring.cpp:7956
bool startsWith(QStringView s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
constexpr QStringView() noexcept
Constructs a null string view.
qsizetype count(QChar c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
int localeAwareCompare(QStringView other) const
Definition qstring.h:1656
value_type & reference
Alias for {value_type &}.
Definition qstringview.h:83
std::ptrdiff_t difference_type
Alias for {std::ptrdiff_t}.
Definition qstringview.h:81
static constexpr qsizetype maxSize() noexcept
friend bool comparesEqual(const QStringView &lhs, const QStringView &rhs) noexcept
ushort toUShort(bool *ok=nullptr, int base=10) const
Returns the string view converted to an {unsigned short} using base base, which is 10 by default and ...
Definition qstring.h:1282
constexpr int compare(QChar c) const noexcept
friend Qt::strong_ordering compareThreeWay(const QStringView &lhs, QChar rhs) noexcept
constexpr QStringView(const Char *str, qsizetype len)
constexpr const storage_type * utf16() const noexcept
char16_t storage_type
Alias for {char16_t}.
Definition qstringview.h:79
qsizetype count(QStringView s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
const QChar value_type
Alias for {const QChar}.
Definition qstringview.h:80
const_pointer data() const noexcept
qsizetype lastIndexOf(QChar c, qsizetype from, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
const_pointer constData() const noexcept
static constexpr QStringView fromArray(const Char(&string)[Size]) noexcept
Constructs a string view on the full character string literal string of Size elements,...
QList< uint > toUcs4() const
Returns a UCS-4/UTF-32 representation of the string view as a QList<uint>.
Definition qlist.h:1122
QByteArray toLatin1() const
Returns a Latin-1 representation of the string as a QByteArray.
bool endsWith(QStringView s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
QByteArray toUtf8() const
Returns a UTF-8 representation of the string view as a QByteArray.
QByteArray toLocal8Bit() const
Returns a local 8-bit representation of the string as a QByteArray.
bool endsWith(QChar c) const noexcept
bool contains(QStringView s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
const_pointer const_iterator
This typedef provides an STL-style const iterator for QStringView.
Definition qstringview.h:89
value_type * pointer
Alias for {value_type *}.
Definition qstringview.h:85
QString toString() const
Returns a deep copy of this string view's data as a QString.
Definition qstring.h:1265
int toInt(bool *ok=nullptr, int base=10) const
Returns the string view converted to an int using base base, which is 10 by default and must be betwe...
Definition qstring.h:1276
constexpr QChar at(qsizetype n) const QT6_ONLY(noexcept)
Returns the character at position n in this string view.
qlonglong toLongLong(bool *ok=nullptr, int base=10) const
Returns the string view converted to a {long long} using base base, which is 10 by default and must b...
Definition qstring.h:1268
friend Qt::strong_ordering compareThreeWay(const QStringView &lhs, const QByteArrayView &rhs) noexcept
value_type * const_pointer
Alias for {value_type *}.
Definition qstringview.h:86
ulong toULong(bool *ok=nullptr, int base=10) const
Returns the string view converted to an {unsigned long} using base base, which is 10 by default and m...
Definition qstring.h:1274
bool isValidUtf16() const noexcept
constexpr QChar operator[](qsizetype n) const
Returns the character at position n in this string view.
constexpr qsizetype size() const noexcept
Returns the size of this string view, in UTF-16 code units (that is, surrogate pairs count as two for...
qulonglong toULongLong(bool *ok=nullptr, int base=10) const
Returns the string view converted to an {unsigned long long} using base base, which is 10 by default ...
Definition qstring.h:1270
constexpr QStringView(const Char *f, const Char *l)
Constructs a string view on first with length (last - first).
constexpr QStringView(const Char(&str)[]) noexcept
value_type & const_reference
Alias for {value_type &}.
Definition qstringview.h:84
short toShort(bool *ok=nullptr, int base=10) const
Returns the string view converted to a short using base base, which is 10 by default and must be betw...
Definition qstring.h:1280
int compare(QUtf8StringView other, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
Definition qstring.h:1258
bool endsWith(QLatin1StringView s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
Definition qstring.h:99
friend bool comparesEqual(const QStringView &lhs, QChar rhs) noexcept
qsizetype size_type
Alias for qsizetype.
Definition qstringview.h:82
pointer iterator
This typedef provides an STL-style const iterator for QStringView.
Definition qstringview.h:88
qsizetype indexOf(QStringView s, qsizetype from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
bool isUpper() const noexcept
qsizetype toWCharArray(wchar_t *array) const
Definition qstring.h:1449
bool startsWith(QLatin1StringView s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
Definition qstring.h:91
qsizetype lastIndexOf(QChar c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
friend bool comparesEqual(const QStringView &lhs, const QByteArrayView &rhs) noexcept
uint toUInt(bool *ok=nullptr, int base=10) const
Returns the string view converted to an {unsigned int} using base base, which is 10 by default and mu...
Definition qstring.h:1278
qsizetype lastIndexOf(QStringView s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
qsizetype indexOf(QChar c, qsizetype from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
long toLong(bool *ok=nullptr, int base=10) const
Returns the string view converted to a long using base base, which is 10 by default and must be betwe...
Definition qstring.h:1272
std::reverse_iterator< const_iterator > const_reverse_iterator
This typedef provides an STL-style const reverse iterator for QStringView.
Definition qstringview.h:91
friend Qt::strong_ordering compareThreeWay(const QStringView &lhs, const QStringView &rhs) noexcept
constexpr QStringView(const Pointer &str) noexcept
Combined button and popup list for selecting options.
constexpr QStringView operator""_sv(const char16_t *str, size_t size) noexcept
Definition qcompare.h:111
#define __has_builtin(x)
QStringView qToStringViewIgnoringNull(const QStringLike &s) noexcept
Q_DECLARE_TYPEINFO(QStringView, Q_PRIMITIVE_TYPE)