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]] constexpr qsizetype size() const noexcept { return m_size; }
184 [[nodiscard]] const_pointer data() const noexcept { return reinterpret_cast<const_pointer>(m_data); }
185 [[nodiscard]] const_pointer constData() const noexcept { return data(); }
186 [[nodiscard]] constexpr const storage_type *utf16() const noexcept { return m_data; }
187
188 [[nodiscard]] constexpr QChar operator[](qsizetype n) const
189 { verify(n, 1); return QChar(m_data[n]); }
190
191 //
192 // QString API
193 //
194
195 template <typename...Args>
196 [[nodiscard]] inline QString arg(Args &&...args) const; // defined in qstring.h
197
198 [[nodiscard]] QByteArray toLatin1() const { return QtPrivate::convertToLatin1(*this); }
199 [[nodiscard]] QByteArray toUtf8() const { return QtPrivate::convertToUtf8(*this); }
200 [[nodiscard]] QByteArray toLocal8Bit() const { return QtPrivate::convertToLocal8Bit(*this); }
201 [[nodiscard]] inline QList<uint> toUcs4() const; // defined in qlist.h ### Qt 7 char32_t
202
203 [[nodiscard]] constexpr QChar at(qsizetype n) const noexcept { return (*this)[n]; }
204
205 [[nodiscard]] constexpr QStringView mid(qsizetype pos, qsizetype n = -1) const noexcept
206 {
207 using namespace QtPrivate;
208 auto result = QContainerImplHelper::mid(size(), &pos, &n);
209 return result == QContainerImplHelper::Null ? QStringView() : QStringView(m_data + pos, n);
210 }
211 [[nodiscard]] constexpr QStringView left(qsizetype n) const noexcept
212 {
213 if (size_t(n) >= size_t(size()))
214 n = size();
215 return QStringView(m_data, n);
216 }
217 [[nodiscard]] constexpr QStringView right(qsizetype n) const noexcept
218 {
219 if (size_t(n) >= size_t(size()))
220 n = size();
221 return QStringView(m_data + m_size - n, n);
222 }
223
224 [[nodiscard]] constexpr QStringView first(qsizetype n) const noexcept
225 { verify(0, n); return sliced(0, n); }
226 [[nodiscard]] constexpr QStringView last(qsizetype n) const noexcept
227 { verify(0, n); return sliced(size() - n, n); }
228 [[nodiscard]] constexpr QStringView sliced(qsizetype pos) const noexcept
229 { verify(pos, 0); return QStringView(m_data + pos, size() - pos); }
230 [[nodiscard]] constexpr QStringView sliced(qsizetype pos, qsizetype n) const noexcept
231 { verify(pos, n); return QStringView(m_data + pos, n); }
232 [[nodiscard]] constexpr QStringView chopped(qsizetype n) const noexcept
233 { verify(0, n); return sliced(0, m_size - n); }
234
235 constexpr void truncate(qsizetype n) noexcept
236 { verify(0, n); ; m_size = n; }
237 constexpr void chop(qsizetype n) noexcept
238 { verify(0, n); m_size -= n; }
239
240 [[nodiscard]] QStringView trimmed() const noexcept { return QtPrivate::trimmed(*this); }
241
242 constexpr QStringView &slice(qsizetype pos)
243 { *this = sliced(pos); return *this; }
244 constexpr QStringView &slice(qsizetype pos, qsizetype n)
245 { *this = sliced(pos, n); return *this; }
246
247 template <typename Needle, typename...Flags>
248 [[nodiscard]] constexpr inline auto tokenize(Needle &&needle, Flags...flags) const
249 noexcept(noexcept(qTokenize(std::declval<const QStringView&>(), std::forward<Needle>(needle), flags...)))
250 -> decltype(qTokenize(*this, std::forward<Needle>(needle), flags...))
251 { return qTokenize(*this, std::forward<Needle>(needle), flags...); }
252
253 [[nodiscard]] int compare(QStringView other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
254 {
255#if defined(Q_CC_GNU) || __has_builtin(__builtin_constant_p)
256 if (__builtin_constant_p(other.m_size) && other.size() == 1)
257 return compare(other.front(), cs);
258#endif
259 return QtPrivate::compareStrings(*this, other, cs);
260 }
261 [[nodiscard]] inline int compare(QLatin1StringView other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
262 [[nodiscard]] inline int compare(QUtf8StringView other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
263 [[nodiscard]] constexpr int compare(QChar c) const noexcept
264 { return size() >= 1 ? compare_single_char_helper(*utf16() - c.unicode()) : -1; }
265 [[nodiscard]] int compare(QChar c, Qt::CaseSensitivity cs) const noexcept
266 {
267#if defined(Q_CC_GNU) || __has_builtin(__builtin_constant_p)
268 if (__builtin_constant_p(cs) && cs == Qt::CaseSensitive)
269 return compare(c);
270#endif
271 return QtPrivate::compareStrings(*this, QStringView(&c, 1), cs);
272 }
273
274 [[nodiscard]] inline int localeAwareCompare(QStringView other) const;
275
276 [[nodiscard]] bool startsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
277 {
278#if defined(Q_CC_GNU) || __has_builtin(__builtin_constant_p)
279 if (__builtin_constant_p(s.m_size) && s.size() == 1)
280 return startsWith(s.front(), cs);
281#endif
282 return QtPrivate::startsWith(*this, s, cs);
283 }
284 [[nodiscard]] inline bool startsWith(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
285 [[nodiscard]] bool startsWith(QChar c) const noexcept
286 { return !empty() && front() == c; }
287 [[nodiscard]] bool startsWith(QChar c, Qt::CaseSensitivity cs) const noexcept
288 {
289#if defined(Q_CC_GNU) || __has_builtin(__builtin_constant_p)
290 if (__builtin_constant_p(cs) && cs == Qt::CaseSensitive)
291 return startsWith(c);
292#endif
293 return QtPrivate::startsWith(*this, QStringView(&c, 1), cs);
294 }
295
296 [[nodiscard]] bool endsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
297 {
298#if defined(Q_CC_GNU) || __has_builtin(__builtin_constant_p)
299 if (__builtin_constant_p(s.m_size) && s.size() == 1)
300 return endsWith(s.front(), cs);
301#endif
302 return QtPrivate::endsWith(*this, s, cs);
303 }
304 [[nodiscard]] inline bool endsWith(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
305 [[nodiscard]] bool endsWith(QChar c) const noexcept
306 { return !empty() && back() == c; }
307 [[nodiscard]] bool endsWith(QChar c, Qt::CaseSensitivity cs) const noexcept
308 {
309#if defined(Q_CC_GNU) || __has_builtin(__builtin_constant_p)
310 if (__builtin_constant_p(cs) && cs == Qt::CaseSensitive)
311 return endsWith(c);
312#endif
313 return QtPrivate::endsWith(*this, QStringView(&c, 1), cs);
314 }
315
316 [[nodiscard]] qsizetype indexOf(QChar c, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
317 { return QtPrivate::findString(*this, from, c.unicode(), cs); }
318 [[nodiscard]] qsizetype indexOf(QStringView s, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
319 {
320#if defined(Q_CC_GNU) || __has_builtin(__builtin_constant_p)
321 if (__builtin_constant_p(s.m_size) && s.size() == 1)
322 return indexOf(s.front(), from, cs);
323#endif
324 return QtPrivate::findString(*this, from, s, cs);
325 }
326 [[nodiscard]] inline qsizetype indexOf(QLatin1StringView s, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
327
328 [[nodiscard]] bool contains(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
329 { return indexOf(c, 0, cs) != qsizetype(-1); }
330 [[nodiscard]] bool contains(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
331 { return indexOf(s, 0, cs) != qsizetype(-1); }
332 [[nodiscard]] inline bool contains(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
333
334 [[nodiscard]] qsizetype count(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
335 { return QtPrivate::count(*this, c, cs); }
336 [[nodiscard]] qsizetype count(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
337 {
338#if defined(Q_CC_GNU) || __has_builtin(__builtin_constant_p)
339 if (__builtin_constant_p(s.m_size) && s.size() == 1)
340 return count(s.front(), cs);
341#endif
342 return QtPrivate::count(*this, s, cs);
343 }
344 [[nodiscard]] inline qsizetype count(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
345
346 [[nodiscard]] qsizetype lastIndexOf(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
347 { return lastIndexOf(c, -1, cs); }
348 [[nodiscard]] qsizetype lastIndexOf(QChar c, qsizetype from, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
349 { return QtPrivate::lastIndexOf(*this, from, c.unicode(), cs); }
350 [[nodiscard]] qsizetype lastIndexOf(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
351 { return lastIndexOf(s, size(), cs); }
352 [[nodiscard]] qsizetype lastIndexOf(QStringView s, qsizetype from, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
353 {
354#if defined(Q_CC_GNU) || __has_builtin(__builtin_constant_p)
355 if (__builtin_constant_p(s.m_size) && s.size() == 1)
356 return lastIndexOf(s.front(), from, cs);
357#endif
358 return QtPrivate::lastIndexOf(*this, from, s, cs);
359 }
360 [[nodiscard]] inline qsizetype lastIndexOf(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
361 [[nodiscard]] inline qsizetype lastIndexOf(QLatin1StringView s, qsizetype from, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
362
363#if QT_CONFIG(regularexpression)
365 {
366 return QtPrivate::indexOf(*this, re, from, rmatch);
367 }
368#ifdef Q_QDOC
370#else
371 // prevent an ambiguity when called like this: lastIndexOf(re, 0)
372 template <typename T = QRegularExpressionMatch, std::enable_if_t<std::is_same_v<T, QRegularExpressionMatch>, bool> = false>
373 [[nodiscard]] qsizetype lastIndexOf(const QRegularExpression &re, T *rmatch = nullptr) const
374 {
375 return QtPrivate::lastIndexOf(*this, re, size(), rmatch);
376 }
377#endif
379 {
380 return QtPrivate::lastIndexOf(*this, re, from, rmatch);
381 }
382 [[nodiscard]] bool contains(const QRegularExpression &re, QRegularExpressionMatch *rmatch = nullptr) const
383 {
384 return QtPrivate::contains(*this, re, rmatch);
385 }
386 [[nodiscard]] qsizetype count(const QRegularExpression &re) const; // defined in qregularexpression.h
387#endif
388
389 [[nodiscard]] bool isRightToLeft() const noexcept
390 { return QtPrivate::isRightToLeft(*this); }
391 [[nodiscard]] bool isValidUtf16() const noexcept
392 { return QtPrivate::isValidUtf16(*this); }
393
394 [[nodiscard]] bool isUpper() const noexcept
395 { return QtPrivate::isUpper(*this); }
396 [[nodiscard]] bool isLower() const noexcept
397 { return QtPrivate::isLower(*this); }
398
399 [[nodiscard]] inline short toShort(bool *ok = nullptr, int base = 10) const;
400 [[nodiscard]] inline ushort toUShort(bool *ok = nullptr, int base = 10) const;
401 [[nodiscard]] inline int toInt(bool *ok = nullptr, int base = 10) const;
402 [[nodiscard]] inline uint toUInt(bool *ok = nullptr, int base = 10) const;
403 [[nodiscard]] inline long toLong(bool *ok = nullptr, int base = 10) const;
404 [[nodiscard]] inline ulong toULong(bool *ok = nullptr, int base = 10) const;
405 [[nodiscard]] inline qlonglong toLongLong(bool *ok = nullptr, int base = 10) const;
406 [[nodiscard]] inline qulonglong toULongLong(bool *ok = nullptr, int base = 10) const;
407 [[nodiscard]] Q_CORE_EXPORT float toFloat(bool *ok = nullptr) const;
408 [[nodiscard]] Q_CORE_EXPORT double toDouble(bool *ok = nullptr) const;
409
410 [[nodiscard]] inline qsizetype toWCharArray(wchar_t *array) const; // defined in qstring.h
411
412
413 [[nodiscard]] Q_CORE_EXPORT
420
421#if QT_CONFIG(regularexpression)
425#endif
426
427 // QStringView <> QStringView
428 friend bool comparesEqual(const QStringView &lhs, const QStringView &rhs) noexcept
429 { return lhs.size() == rhs.size() && QtPrivate::equalStrings(lhs, rhs); }
430 friend Qt::strong_ordering
431 compareThreeWay(const QStringView &lhs, const QStringView &rhs) noexcept
432 {
433 const int res = QtPrivate::compareStrings(lhs, rhs);
434 return Qt::compareThreeWay(res, 0);
435 }
437
438 // QStringView <> QChar
439 friend bool comparesEqual(const QStringView &lhs, QChar rhs) noexcept
440 { return lhs.size() == 1 && lhs[0] == rhs; }
442 { return compareThreeWay(lhs, QStringView(&rhs, 1)); }
444
445 //
446 // STL compatibility API:
447 //
448 [[nodiscard]] const_iterator begin() const noexcept { return data(); }
449 [[nodiscard]] const_iterator end() const noexcept { return data() + size(); }
450 [[nodiscard]] const_iterator cbegin() const noexcept { return begin(); }
451 [[nodiscard]] const_iterator cend() const noexcept { return end(); }
454 [[nodiscard]] const_reverse_iterator crbegin() const noexcept { return rbegin(); }
455 [[nodiscard]] const_reverse_iterator crend() const noexcept { return rend(); }
456
457 [[nodiscard]] constexpr bool empty() const noexcept { return size() == 0; }
458 [[nodiscard]] constexpr QChar front() const { return Q_PRE(!empty()), QChar(m_data[0]); }
459 [[nodiscard]] constexpr QChar back() const { return Q_PRE(!empty()), QChar(m_data[m_size - 1]); }
460
461 [[nodiscard]] constexpr Q_IMPLICIT operator std::u16string_view() const noexcept
462 { return std::u16string_view(m_data, size_t(m_size)); }
463
464 [[nodiscard]] constexpr qsizetype max_size() const noexcept { return maxSize(); }
465
466 //
467 // Qt compatibility API:
468 //
469 [[nodiscard]] const_iterator constBegin() const noexcept { return begin(); }
470 [[nodiscard]] const_iterator constEnd() const noexcept { return end(); }
471 [[nodiscard]] constexpr bool isNull() const noexcept { return !m_data; }
472 [[nodiscard]] constexpr bool isEmpty() const noexcept { return empty(); }
473 [[nodiscard]] constexpr qsizetype length() const noexcept
474 { return size(); }
475 [[nodiscard]] constexpr QChar first() const { return front(); }
476 [[nodiscard]] constexpr QChar last() const { return back(); }
477
478 [[nodiscard]] static constexpr qsizetype maxSize() noexcept
479 {
480 // -1 to deal with the pointer one-past-the-end;
481 return QtPrivate::MaxAllocSize / sizeof(storage_type) - 1;
482 }
483private:
484#if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0) || defined(QT_BOOTSTRAPPED)
485 const storage_type *m_data = nullptr;
486 qsizetype m_size = 0;
487#else
488 qsizetype m_size = 0;
489 const storage_type *m_data = nullptr;
490#endif
491
492 Q_ALWAYS_INLINE constexpr void verify([[maybe_unused]] qsizetype pos = 0,
493 [[maybe_unused]] qsizetype n = 1) const
494 {
495 Q_PRE(pos >= 0);
496 Q_PRE(pos <= size());
497 Q_PRE(n >= 0);
498 Q_PRE(n <= size() - pos);
499 }
500
501 constexpr int compare_single_char_helper(int diff) const noexcept
502 { return diff ? diff : size() > 1 ? 1 : 0; }
503
504 Q_CORE_EXPORT static bool equal_helper(QStringView sv, const char *data, qsizetype len);
505 Q_CORE_EXPORT static int compare_helper(QStringView sv, const char *data, qsizetype len);
506
507#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
508 friend bool comparesEqual(const QStringView &lhs, const QByteArrayView &rhs) noexcept
509 { return equal_helper(lhs, rhs.data(), rhs.size()); }
510 friend Qt::strong_ordering
511 compareThreeWay(const QStringView &lhs, const QByteArrayView &rhs) noexcept
512 {
513 const int res = compare_helper(lhs, rhs.data(), rhs.size());
514 return Qt::compareThreeWay(res, 0);
515 }
516 Q_DECLARE_STRONGLY_ORDERED(QStringView, QByteArrayView, QT_ASCII_CAST_WARN)
519#endif // !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
520};
522
523template <typename QStringLike, typename std::enable_if<
524 std::is_same<QStringLike, QString>::value,
525 bool>::type = true>
526inline QStringView qToStringViewIgnoringNull(const QStringLike &s) noexcept
527{ return QStringView(s.begin(), s.size()); }
528
529qsizetype QtPrivate::findString(QStringView str, qsizetype from, QChar ch, Qt::CaseSensitivity cs) noexcept
530{
531 if (from < -str.size()) // from < 0 && abs(from) > str.size(), avoiding overflow
532 return -1;
533 if (from < 0)
534 from = qMax(from + str.size(), qsizetype(0));
535 if (from < str.size()) {
536 const char16_t *s = str.utf16();
537 char16_t c = ch.unicode();
538 const char16_t *n = s + from;
539 const char16_t *e = s + str.size();
540 if (cs == Qt::CaseSensitive)
541 n = qustrchr(QStringView(n, e), c);
542 else
543 n = qustrcasechr(QStringView(n, e), c);
544 if (n != e)
545 return n - s;
546 }
547 return -1;
548}
549
550namespace Qt {
551inline namespace Literals {
552inline namespace StringLiterals {
553constexpr QStringView operator""_sv(const char16_t *str, size_t size) noexcept
554{
555 return QStringView(str, qsizetype(size));
556}
557} // StringLiterals
558} // Literals
559} // Qt
560
561QT_END_NAMESPACE
562
563#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:7967
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:8013
constexpr QStringView & slice(qsizetype pos)
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:1645
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
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:1275
constexpr int compare(QChar c) const 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
constexpr QStringView right(qsizetype n) const noexcept
qsizetype count(QStringView s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
const QChar value_type
Alias for {const QChar}.
Definition qstringview.h:80
constexpr QStringView & slice(qsizetype pos, qsizetype n)
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:1093
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
constexpr QStringView last(qsizetype n) const noexcept
constexpr void truncate(qsizetype n) noexcept
Truncates this string view to length length.
constexpr QStringView sliced(qsizetype pos, qsizetype n) 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:1258
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:1269
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:1261
friend Qt::strong_ordering compareThreeWay(const QStringView &lhs, const QByteArrayView &rhs) noexcept
value_type * const_pointer
Alias for {value_type *}.
Definition qstringview.h:86
constexpr QStringView first(qsizetype n) const noexcept
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:1267
bool isValidUtf16() const noexcept
constexpr QChar operator[](qsizetype n) const
Returns the character at position n in this string view.
int compare(QStringView other, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
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:1263
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:1273
bool endsWith(QLatin1StringView s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
Definition qstring.h:99
qsizetype size_type
Alias for qsizetype.
Definition qstringview.h:82
constexpr auto tokenize(Needle &&needle, Flags...flags) const noexcept(noexcept(qTokenize(std::declval< const QStringView & >(), std::forward< Needle >(needle), flags...))) -> decltype(qTokenize(*this, std::forward< Needle >(needle), flags...))
pointer iterator
This typedef provides an STL-style const iterator for QStringView.
Definition qstringview.h:88
constexpr QStringView left(qsizetype n) const noexcept
qsizetype indexOf(QStringView s, qsizetype from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
constexpr QStringView chopped(qsizetype n) const noexcept
Returns the substring of length size() - length starting at the beginning of this object.
bool isUpper() const noexcept
qsizetype toWCharArray(wchar_t *array) const
Definition qstring.h:1435
constexpr QStringView mid(qsizetype pos, qsizetype n=-1) const noexcept
Returns the substring of length length starting at position start in this object.
QStringView trimmed() const noexcept
Strips leading and trailing whitespace and returns the result.
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
constexpr void chop(qsizetype n) noexcept
Truncates this string view by length characters.
constexpr QChar at(qsizetype n) const noexcept
Returns the character at position n in this string view.
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:1271
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
constexpr QStringView sliced(qsizetype pos) const noexcept
int compare(QLatin1StringView other, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
Definition qstring.h:83
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:1265
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:110
#define __has_builtin(x)
QStringView qToStringViewIgnoringNull(const QStringLike &s) noexcept
Q_DECLARE_TYPEINFO(QStringView, Q_PRIMITIVE_TYPE)