Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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#ifndef QSTRINGVIEW_H
5#define QSTRINGVIEW_H
6
7#include <QtCore/qchar.h>
8#include <QtCore/qcompare.h>
9#include <QtCore/qbytearray.h>
10#include <QtCore/qstringliteral.h>
11#include <QtCore/qstringalgorithms.h>
12
13#include <string>
14#include <string_view>
15#include <QtCore/q20type_traits.h>
16
17#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
20#endif
21
23
24class QString;
25class QStringView;
28#ifdef Q_QDOC
29class QUtf8StringView;
30#endif
31
32namespace QtPrivate {
33template <typename Char>
35 : std::integral_constant<bool,
36 std::is_same<Char, QChar>::value ||
37 std::is_same<Char, ushort>::value ||
38 std::is_same<Char, char16_t>::value ||
39 (std::is_same<Char, wchar_t>::value && sizeof(wchar_t) == sizeof(QChar))> {};
40template <typename Char>
42 : IsCompatibleCharTypeHelper<q20::remove_cvref_t<Char>> {};
43
44template <typename Pointer>
45struct IsCompatiblePointerHelper : std::false_type {};
46template <typename Char>
48 : IsCompatibleCharType<Char> {};
49template <typename Pointer>
51 : IsCompatiblePointerHelper<q20::remove_cvref_t<Pointer>> {};
52
53template <typename T, typename Enable = void>
54struct IsContainerCompatibleWithQStringView : std::false_type {};
55
56template <typename T>
57struct IsContainerCompatibleWithQStringView<T, std::enable_if_t<std::conjunction_v<
58 // lacking concepts and ranges, we accept any T whose std::data yields a suitable pointer ...
59 IsCompatiblePointer<decltype( std::data(std::declval<const T &>()) )>,
60 // ... and that has a suitable size ...
61 std::is_convertible<decltype( std::size(std::declval<const T &>()) ), qsizetype>,
62 // ... and it's a range as it defines an iterator-like API
63 IsCompatibleCharType<typename std::iterator_traits<decltype( std::begin(std::declval<const T &>()) )>::value_type>,
64 std::is_convertible<
65 decltype( std::begin(std::declval<const T &>()) != std::end(std::declval<const T &>()) ),
66 bool>,
67
68 // These need to be treated specially due to the empty vs null distinction
69 std::negation<std::is_same<std::decay_t<T>, QString>>,
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
78{
79public:
80 typedef char16_t storage_type;
81 typedef const QChar value_type;
82 typedef std::ptrdiff_t difference_type;
88
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 {
111 return std::char_traits<Char>::length(str);
112 return QtPrivate::qustrlen(reinterpret_cast<const char16_t *>(str));
113 }
114 static qsizetype lengthHelperPointer(const QChar *str) noexcept
115 {
116 return QtPrivate::qustrlen(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_ASSERT(len >= 0), Q_ASSERT(str || !len), len))
135#else
136 : m_size((Q_ASSERT(len >= 0), Q_ASSERT(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
153 template <typename Pointer, if_compatible_pointer<Pointer> = true>
154 constexpr QStringView(const Pointer &str) noexcept
155 : QStringView(str, str ? lengthHelperPointer(str) : 0) {}
156#endif
157
158#ifdef Q_QDOC
159 QStringView(const QString &str) noexcept;
160#else
161 template <typename String, if_compatible_qstring_like<String> = true>
162 QStringView(const String &str) noexcept
163 : QStringView(str.isNull() ? nullptr : str.data(), qsizetype(str.size())) {}
164#endif
165
166 template <typename Container, if_compatible_container<Container> = true>
167 constexpr Q_ALWAYS_INLINE QStringView(const Container &c) noexcept
169
170 template <typename Char, size_t Size, if_compatible_char<Char> = true>
171 [[nodiscard]] constexpr static QStringView fromArray(const Char (&string)[Size]) noexcept
172 { return QStringView(string, Size); }
173
174 [[nodiscard]] inline QString toString() const; // defined in qstring.h
175#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
176 // defined in qcore_foundation.mm
177 [[nodiscard]] Q_CORE_EXPORT CFStringRef toCFString() const Q_DECL_CF_RETURNS_RETAINED;
178 [[nodiscard]] Q_CORE_EXPORT NSString *toNSString() const Q_DECL_NS_RETURNS_AUTORELEASED;
179#endif
180
181 [[nodiscard]] constexpr qsizetype size() const noexcept { return m_size; }
182 [[nodiscard]] const_pointer data() const noexcept { return reinterpret_cast<const_pointer>(m_data); }
183 [[nodiscard]] const_pointer constData() const noexcept { return data(); }
184 [[nodiscard]] constexpr const storage_type *utf16() const noexcept { return m_data; }
185
186 [[nodiscard]] constexpr QChar operator[](qsizetype n) const
187 { verify(n, 1); return QChar(m_data[n]); }
188
189 //
190 // QString API
191 //
192
193 template <typename...Args>
194 [[nodiscard]] inline QString arg(Args &&...args) const; // defined in qstring.h
195
196 [[nodiscard]] QByteArray toLatin1() const { return QtPrivate::convertToLatin1(*this); }
197 [[nodiscard]] QByteArray toUtf8() const { return QtPrivate::convertToUtf8(*this); }
198 [[nodiscard]] QByteArray toLocal8Bit() const { return QtPrivate::convertToLocal8Bit(*this); }
199 [[nodiscard]] inline QList<uint> toUcs4() const; // defined in qlist.h ### Qt 7 char32_t
200
201 [[nodiscard]] constexpr QChar at(qsizetype n) const noexcept { return (*this)[n]; }
202
203 [[nodiscard]] constexpr QStringView mid(qsizetype pos, qsizetype n = -1) const noexcept
204 {
205 using namespace QtPrivate;
206 auto result = QContainerImplHelper::mid(size(), &pos, &n);
207 return result == QContainerImplHelper::Null ? QStringView() : QStringView(m_data + pos, n);
208 }
209 [[nodiscard]] constexpr QStringView left(qsizetype n) const noexcept
210 {
211 if (size_t(n) >= size_t(size()))
212 n = size();
213 return QStringView(m_data, n);
214 }
215 [[nodiscard]] constexpr QStringView right(qsizetype n) const noexcept
216 {
217 if (size_t(n) >= size_t(size()))
218 n = size();
219 return QStringView(m_data + m_size - n, n);
220 }
221
222 [[nodiscard]] constexpr QStringView first(qsizetype n) const noexcept
223 { verify(0, n); return sliced(0, n); }
224 [[nodiscard]] constexpr QStringView last(qsizetype n) const noexcept
225 { verify(0, n); return sliced(size() - n, n); }
226 [[nodiscard]] constexpr QStringView sliced(qsizetype pos) const noexcept
227 { verify(pos, 0); return QStringView(m_data + pos, size() - pos); }
228 [[nodiscard]] constexpr QStringView sliced(qsizetype pos, qsizetype n) const noexcept
229 { verify(pos, n); return QStringView(m_data + pos, n); }
230 [[nodiscard]] constexpr QStringView chopped(qsizetype n) const noexcept
231 { verify(0, n); return sliced(0, m_size - n); }
232
233 constexpr void truncate(qsizetype n) noexcept
234 { verify(0, n); ; m_size = n; }
235 constexpr void chop(qsizetype n) noexcept
236 { verify(0, n); m_size -= n; }
237
238 [[nodiscard]] QStringView trimmed() const noexcept { return QtPrivate::trimmed(*this); }
239
240 template <typename Needle, typename...Flags>
241 [[nodiscard]] constexpr inline auto tokenize(Needle &&needle, Flags...flags) const
242 noexcept(noexcept(qTokenize(std::declval<const QStringView&>(), std::forward<Needle>(needle), flags...)))
243 -> decltype(qTokenize(*this, std::forward<Needle>(needle), flags...))
244 { return qTokenize(*this, std::forward<Needle>(needle), flags...); }
245
246 [[nodiscard]] int compare(QStringView other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
247 { return QtPrivate::compareStrings(*this, other, cs); }
248 [[nodiscard]] inline int compare(QLatin1StringView other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
249 [[nodiscard]] inline int compare(QUtf8StringView other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
250 [[nodiscard]] constexpr int compare(QChar c) const noexcept
251 { return size() >= 1 ? compare_single_char_helper(*utf16() - c.unicode()) : -1; }
252 [[nodiscard]] int compare(QChar c, Qt::CaseSensitivity cs) const noexcept
253 { return QtPrivate::compareStrings(*this, QStringView(&c, 1), cs); }
254
255 [[nodiscard]] inline int localeAwareCompare(QStringView other) const;
256
257 [[nodiscard]] bool startsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
258 { return QtPrivate::startsWith(*this, s, cs); }
259 [[nodiscard]] inline bool startsWith(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
260 [[nodiscard]] bool startsWith(QChar c) const noexcept
261 { return !empty() && front() == c; }
262 [[nodiscard]] bool startsWith(QChar c, Qt::CaseSensitivity cs) const noexcept
263 { return QtPrivate::startsWith(*this, QStringView(&c, 1), cs); }
264
265 [[nodiscard]] bool endsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
266 { return QtPrivate::endsWith(*this, s, cs); }
267 [[nodiscard]] inline bool endsWith(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
268 [[nodiscard]] bool endsWith(QChar c) const noexcept
269 { return !empty() && back() == c; }
270 [[nodiscard]] bool endsWith(QChar c, Qt::CaseSensitivity cs) const noexcept
271 { return QtPrivate::endsWith(*this, QStringView(&c, 1), cs); }
272
273 [[nodiscard]] qsizetype indexOf(QChar c, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
274 { return QtPrivate::findString(*this, from, c.unicode(), cs); }
275 [[nodiscard]] qsizetype indexOf(QStringView s, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
276 { return QtPrivate::findString(*this, from, s, cs); }
277 [[nodiscard]] inline qsizetype indexOf(QLatin1StringView s, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
278
279 [[nodiscard]] bool contains(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
280 { return indexOf(QStringView(&c, 1), 0, cs) != qsizetype(-1); }
281 [[nodiscard]] bool contains(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
282 { return indexOf(s, 0, cs) != qsizetype(-1); }
283 [[nodiscard]] inline bool contains(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
284
285 [[nodiscard]] qsizetype count(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
286 { return QtPrivate::count(*this, c, cs); }
288 { return QtPrivate::count(*this, s, cs); }
290
292 { return lastIndexOf(c, -1, cs); }
294 { return QtPrivate::lastIndexOf(*this, from, c.unicode(), cs); }
296 { return lastIndexOf(s, size(), cs); }
298 { return QtPrivate::lastIndexOf(*this, from, s, cs); }
299 [[nodiscard]] inline qsizetype lastIndexOf(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
300 [[nodiscard]] inline qsizetype lastIndexOf(QLatin1StringView s, qsizetype from, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
301
302#if QT_CONFIG(regularexpression)
303 [[nodiscard]] qsizetype indexOf(const QRegularExpression &re, qsizetype from = 0, QRegularExpressionMatch *rmatch = nullptr) const
304 {
305 return QtPrivate::indexOf(*this, re, from, rmatch);
306 }
307#ifdef Q_QDOC
308 [[nodiscard]] qsizetype lastIndexOf(const QRegularExpression &re, QRegularExpressionMatch *rmatch = nullptr) const;
309#else
310 // prevent an ambiguity when called like this: lastIndexOf(re, 0)
311 template <typename T = QRegularExpressionMatch, std::enable_if_t<std::is_same_v<T, QRegularExpressionMatch>, bool> = false>
312 [[nodiscard]] qsizetype lastIndexOf(const QRegularExpression &re, T *rmatch = nullptr) const
313 {
314 return QtPrivate::lastIndexOf(*this, re, size(), rmatch);
315 }
316#endif
317 [[nodiscard]] qsizetype lastIndexOf(const QRegularExpression &re, qsizetype from, QRegularExpressionMatch *rmatch = nullptr) const
318 {
319 return QtPrivate::lastIndexOf(*this, re, from, rmatch);
320 }
321 [[nodiscard]] bool contains(const QRegularExpression &re, QRegularExpressionMatch *rmatch = nullptr) const
322 {
323 return QtPrivate::contains(*this, re, rmatch);
324 }
325 [[nodiscard]] qsizetype count(const QRegularExpression &re) const
326 {
327 return QtPrivate::count(*this, re);
328 }
329#endif
330
331 [[nodiscard]] bool isRightToLeft() const noexcept
332 { return QtPrivate::isRightToLeft(*this); }
333 [[nodiscard]] bool isValidUtf16() const noexcept
334 { return QtPrivate::isValidUtf16(*this); }
335
336 [[nodiscard]] bool isUpper() const noexcept
337 { return QtPrivate::isUpper(*this); }
338 [[nodiscard]] bool isLower() const noexcept
339 { return QtPrivate::isLower(*this); }
340
341 [[nodiscard]] inline short toShort(bool *ok = nullptr, int base = 10) const;
342 [[nodiscard]] inline ushort toUShort(bool *ok = nullptr, int base = 10) const;
343 [[nodiscard]] inline int toInt(bool *ok = nullptr, int base = 10) const;
344 [[nodiscard]] inline uint toUInt(bool *ok = nullptr, int base = 10) const;
345 [[nodiscard]] inline long toLong(bool *ok = nullptr, int base = 10) const;
346 [[nodiscard]] inline ulong toULong(bool *ok = nullptr, int base = 10) const;
347 [[nodiscard]] inline qlonglong toLongLong(bool *ok = nullptr, int base = 10) const;
348 [[nodiscard]] inline qulonglong toULongLong(bool *ok = nullptr, int base = 10) const;
349 [[nodiscard]] Q_CORE_EXPORT float toFloat(bool *ok = nullptr) const;
350 [[nodiscard]] Q_CORE_EXPORT double toDouble(bool *ok = nullptr) const;
351
352 [[nodiscard]] inline qsizetype toWCharArray(wchar_t *array) const; // defined in qstring.h
353
354
355 [[nodiscard]] Q_CORE_EXPORT
356 QList<QStringView> split(QStringView sep,
357 Qt::SplitBehavior behavior = Qt::KeepEmptyParts,
359 [[nodiscard]] Q_CORE_EXPORT
360 QList<QStringView> split(QChar sep, Qt::SplitBehavior behavior = Qt::KeepEmptyParts,
362
363#if QT_CONFIG(regularexpression)
364 [[nodiscard]] Q_CORE_EXPORT
365 QList<QStringView> split(const QRegularExpression &sep,
366 Qt::SplitBehavior behavior = Qt::KeepEmptyParts) const;
367#endif
368
369 // QStringView <> QStringView
370 friend bool comparesEqual(const QStringView &lhs, const QStringView &rhs) noexcept
371 { return lhs.size() == rhs.size() && QtPrivate::equalStrings(lhs, rhs); }
373 compareThreeWay(const QStringView &lhs, const QStringView &rhs) noexcept
374 {
375 const int res = QtPrivate::compareStrings(lhs, rhs);
376 return Qt::compareThreeWay(res, 0);
377 }
379
380 // QStringView <> QChar
381 friend bool comparesEqual(const QStringView &lhs, QChar rhs) noexcept
382 { return lhs.size() == 1 && lhs[0] == rhs; }
383 friend Qt::strong_ordering compareThreeWay(const QStringView &lhs, QChar rhs) noexcept
384 { return compareThreeWay(lhs, QStringView(&rhs, 1)); }
386
387 //
388 // STL compatibility API:
389 //
390 [[nodiscard]] const_iterator begin() const noexcept { return data(); }
391 [[nodiscard]] const_iterator end() const noexcept { return data() + size(); }
392 [[nodiscard]] const_iterator cbegin() const noexcept { return begin(); }
393 [[nodiscard]] const_iterator cend() const noexcept { return end(); }
394 [[nodiscard]] const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); }
395 [[nodiscard]] const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); }
396 [[nodiscard]] const_reverse_iterator crbegin() const noexcept { return rbegin(); }
397 [[nodiscard]] const_reverse_iterator crend() const noexcept { return rend(); }
398
399 [[nodiscard]] constexpr bool empty() const noexcept { return size() == 0; }
400 [[nodiscard]] constexpr QChar front() const { return Q_ASSERT(!empty()), QChar(m_data[0]); }
401 [[nodiscard]] constexpr QChar back() const { return Q_ASSERT(!empty()), QChar(m_data[m_size - 1]); }
402
403 [[nodiscard]] Q_IMPLICIT operator std::u16string_view() const noexcept
404 { return std::u16string_view(m_data, size_t(m_size)); }
405
406 //
407 // Qt compatibility API:
408 //
409 [[nodiscard]] const_iterator constBegin() const noexcept { return begin(); }
410 [[nodiscard]] const_iterator constEnd() const noexcept { return end(); }
411 [[nodiscard]] constexpr bool isNull() const noexcept { return !m_data; }
412 [[nodiscard]] constexpr bool isEmpty() const noexcept { return empty(); }
413 [[nodiscard]] constexpr qsizetype length() const noexcept
414 { return size(); }
415 [[nodiscard]] constexpr QChar first() const { return front(); }
416 [[nodiscard]] constexpr QChar last() const { return back(); }
417private:
418#if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0) || defined(QT_BOOTSTRAPPED)
419 const storage_type *m_data = nullptr;
420 qsizetype m_size = 0;
421#else
422 qsizetype m_size = 0;
423 const storage_type *m_data = nullptr;
424#endif
425
426 Q_ALWAYS_INLINE constexpr void verify([[maybe_unused]] qsizetype pos = 0,
427 [[maybe_unused]] qsizetype n = 1) const
428 {
429 Q_ASSERT(pos >= 0);
430 Q_ASSERT(pos <= size());
431 Q_ASSERT(n >= 0);
432 Q_ASSERT(n <= size() - pos);
433 }
434
435 constexpr int compare_single_char_helper(int diff) const noexcept
436 { return diff ? diff : size() > 1 ? 1 : 0; }
437
438 Q_CORE_EXPORT static bool equal_helper(QStringView sv, const char *data, qsizetype len);
439 Q_CORE_EXPORT static int compare_helper(QStringView sv, const char *data, qsizetype len);
440
441#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
442 friend bool comparesEqual(const QStringView &lhs, const QByteArrayView &rhs) noexcept
443 { return equal_helper(lhs, rhs.data(), rhs.size()); }
445 compareThreeWay(const QStringView &lhs, const QByteArrayView &rhs) noexcept
446 {
447 const int res = compare_helper(lhs, rhs.data(), rhs.size());
448 return Qt::compareThreeWay(res, 0);
449 }
453#endif // !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
454};
456
457template <typename QStringLike, typename std::enable_if<
458 std::is_same<QStringLike, QString>::value,
459 bool>::type = true>
460inline QStringView qToStringViewIgnoringNull(const QStringLike &s) noexcept
461{ return QStringView(s.begin(), s.size()); }
462
463// QChar inline functions:
464
465[[nodiscard]] constexpr auto QChar::fromUcs4(char32_t c) noexcept
466{
467 struct R {
468 char16_t chars[2];
469 [[nodiscard]] constexpr operator QStringView() const noexcept { return {begin(), end()}; }
470 [[nodiscard]] constexpr qsizetype size() const noexcept { return chars[1] ? 2 : 1; }
471 [[nodiscard]] constexpr const char16_t *begin() const noexcept { return chars; }
472 [[nodiscard]] constexpr const char16_t *end() const noexcept { return begin() + size(); }
473 };
474 return requiresSurrogates(c) ? R{{QChar::highSurrogate(c),
475 QChar::lowSurrogate(c)}} :
476 R{{char16_t(c), u'\0'}} ;
477}
478
480{
481 if (from < -str.size()) // from < 0 && abs(from) > str.size(), avoiding overflow
482 return -1;
483 if (from < 0)
484 from = qMax(from + str.size(), qsizetype(0));
485 if (from < str.size()) {
486 const char16_t *s = str.utf16();
487 char16_t c = ch.unicode();
488 const char16_t *n = s + from;
489 const char16_t *e = s + str.size();
490 if (cs == Qt::CaseSensitive)
491 n = qustrchr(QStringView(n, e), c);
492 else
493 n = qustrcasechr(QStringView(n, e), c);
494 if (n != e)
495 return n - s;
496 }
497 return -1;
498}
499
501
502#endif /* QSTRINGVIEW_H */
NSData * m_data
\inmodule QtCore
Definition qbytearray.h:57
\inmodule QtCore
\inmodule QtCore \reentrant
\inmodule QtCore \reentrant
\inmodule QtCore
Definition qstringview.h:78
const_iterator cend() const noexcept
Same as end().
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:7909
constexpr void truncate(qsizetype n) noexcept
Truncates this string view to length length.
bool isRightToLeft() const noexcept
const_iterator cbegin() const noexcept
Same as begin().
int compare(QChar c, Qt::CaseSensitivity cs) const noexcept
Q_CORE_EXPORT float toFloat(bool *ok=nullptr) const
Returns the string view converted to a float value.
Definition qstring.cpp:7955
value_type & reference
Alias for {value_type &}.
Definition qstringview.h:84
bool startsWith(QStringView s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
bool startsWith(QChar c, Qt::CaseSensitivity cs) const noexcept
Returns true if this string view starts with.
constexpr const storage_type * utf16() const noexcept
bool endsWith(QChar c, Qt::CaseSensitivity cs) const noexcept
Returns true if this string view ends with.
qsizetype count(QChar c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
const QChar value_type
Alias for {const QChar}.
Definition qstringview.h:81
int localeAwareCompare(QStringView other) const
Definition qstring.h:1499
constexpr void chop(qsizetype n) noexcept
Truncates this string view by length characters.
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...))
constexpr QStringView(const Char *f, const Char *l)
Constructs a string view on first with length (last - first).
constexpr bool isEmpty() const noexcept
Returns whether this string view is empty - that is, whether {size() == 0}.
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:1138
friend Qt::strong_ordering compareThreeWay(const QStringView &lhs, QChar rhs) noexcept
const_pointer const_iterator
This typedef provides an STL-style const iterator for QStringView.
Definition qstringview.h:90
QString arg(Args &&...args) const
const_reverse_iterator rbegin() const noexcept
Returns a const \l{STL-style iterators}{STL-style} reverse iterator pointing to the first character i...
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...
const_iterator constEnd() const noexcept
constexpr QStringView first(qsizetype n) const noexcept
constexpr QStringView chopped(qsizetype n) const noexcept
Returns the substring of length size() - length starting at the beginning of this object.
const_reverse_iterator crend() const noexcept
Same as rend().
value_type & const_reference
Alias for {value_type &}.
Definition qstringview.h:85
qsizetype count(QStringView s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
constexpr Q_ALWAYS_INLINE QStringView(const Container &c) noexcept
constexpr QChar last() const
Returns the last character in the string view.
constexpr QStringView left(qsizetype n) const noexcept
const_pointer data() const noexcept
qsizetype lastIndexOf(QChar c, qsizetype from, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
const_iterator begin() const noexcept
Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first character in the st...
QList< uint > toUcs4() const
Returns a UCS-4/UTF-32 representation of the string view as a QList<uint>.
Definition qlist.h:1019
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
pointer iterator
This typedef provides an STL-style const iterator for QStringView.
Definition qstringview.h:89
QByteArray toUtf8() const
Returns a UTF-8 representation of the string view as a QByteArray.
Q_CORE_EXPORT QList< QStringView > split(QStringView sep, Qt::SplitBehavior behavior=Qt::KeepEmptyParts, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Splits the view into substring views wherever sep occurs, and returns the list of those string views.
Definition qstring.cpp:8249
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 sliced(qsizetype pos, qsizetype n) const noexcept
const_reverse_iterator rend() const noexcept
Returns a \l{STL-style iterators}{STL-style} reverse iterator pointing to one past the last character...
std::ptrdiff_t difference_type
Alias for {std::ptrdiff_t}.
Definition qstringview.h:82
QString toString() const
Returns a deep copy of this string view's data as a QString.
Definition qstring.h:1121
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:1132
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:1124
std::reverse_iterator< const_iterator > const_reverse_iterator
This typedef provides an STL-style const reverse iterator for QStringView.
Definition qstringview.h:92
constexpr qsizetype length() const noexcept
Same as size().
constexpr QStringView last(qsizetype n) const noexcept
friend Qt::strong_ordering compareThreeWay(const QStringView &lhs, const QByteArrayView &rhs) noexcept
constexpr QStringView() noexcept
Constructs a null string view.
constexpr QChar operator[](qsizetype n) const
Returns the character at position n in this string view.
value_type * pointer
Alias for {value_type *}.
Definition qstringview.h:86
constexpr bool empty() const noexcept
Returns whether this string view is empty - that is, whether {size() == 0}.
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:1130
constexpr QChar back() const
Returns the last character in the string view.
bool isValidUtf16() const noexcept
constexpr bool isNull() const noexcept
Returns whether this string view is null - that is, whether {data() == nullptr}.
int compare(QStringView other, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
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:1126
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:1136
constexpr QStringView right(qsizetype n) const noexcept
const_reverse_iterator crbegin() const noexcept
Same as rbegin().
constexpr QChar at(qsizetype n) const noexcept
Returns the character at position n in this string view.
char16_t storage_type
Alias for {char16_t}.
Definition qstringview.h:80
constexpr int compare(QChar c) const noexcept
constexpr QStringView mid(qsizetype pos, qsizetype n=-1) const noexcept
Returns the substring of length length starting at position start in this object.
qsizetype indexOf(QStringView s, qsizetype from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
bool isUpper() const noexcept
value_type * const_pointer
Alias for {value_type *}.
Definition qstringview.h:87
QStringView trimmed() const noexcept
Strips leading and trailing whitespace and returns the result.
constexpr QChar front() const
constexpr QStringView(const Char *str, qsizetype len)
Constructs a string view on str with length len.
const_iterator constBegin() const noexcept
const_iterator end() const noexcept
Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary character after...
static constexpr QStringView fromArray(const Char(&string)[Size]) noexcept
Constructs a string view on the full character string literal string, including any trailing {Char(0)...
const_pointer constData() const noexcept
qsizetype lastIndexOf(QChar c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
friend bool comparesEqual(const QStringView &lhs, const QByteArrayView &rhs) noexcept
constexpr QStringView(std::nullptr_t) noexcept
Constructs a null 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:1134
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
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:1128
std::reverse_iterator< iterator > reverse_iterator
This typedef provides an STL-style const reverse iterator for QStringView.
Definition qstringview.h:91
qsizetype toWCharArray(wchar_t *array) const
Definition qstring.h:1296
friend Qt::strong_ordering compareThreeWay(const QStringView &lhs, const QStringView &rhs) noexcept
constexpr QStringView(const Pointer &str) noexcept
qsizetype size_type
Alias for qsizetype.
Definition qstringview.h:83
constexpr QChar first() const
Returns the first character in the string view.
QStringView(const String &str) noexcept
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
const ushort * utf16() const
Returns the QString as a '\0\'-terminated array of unsigned shorts.
Definition qstring.cpp:6995
bool isNull() const
Returns true if this string is null; otherwise returns false.
Definition qstring.h:994
qsizetype size() const noexcept
Returns the number of characters in this string.
Definition qstring.h:186
\inmodule QtCore \title Classes and helpers for defining comparison operators \keyword qtcompare
Definition qcompare.h:400
#define this
Definition dialogs.cpp:9
QString str
[2]
Combined button and popup list for selecting options.
\macro QT_NO_KEYWORDS >
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype lastIndexOf(QByteArrayView haystack, qsizetype from, char needle) noexcept
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool endsWith(QByteArrayView haystack, QByteArrayView needle) noexcept
constexpr Q_ALWAYS_INLINE std::enable_if_t< sizeof(Char)==sizeof(char16_t), qsizetype > lengthHelperContainer(const Char(&str)[N])
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isLower(QStringView s) noexcept
Definition qstring.cpp:5557
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isValidUtf16(QStringView s) noexcept
Definition qstring.cpp:905
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION QByteArrayView trimmed(QByteArrayView s) noexcept
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool startsWith(QByteArrayView haystack, QByteArrayView needle) noexcept
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype count(QByteArrayView haystack, QByteArrayView needle) noexcept
Q_CORE_EXPORT QByteArray convertToLocal8Bit(QStringView str)
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool equalStrings(QStringView lhs, QStringView rhs) noexcept
Definition qstring.cpp:1393
qsizetype findString(QStringView str, qsizetype from, QChar needle, Qt::CaseSensitivity cs=Qt::CaseSensitive) noexcept
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isRightToLeft(QStringView string) noexcept
Q_CORE_EXPORT QByteArray convertToLatin1(QStringView str)
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION int compareStrings(QStringView lhs, QStringView rhs, Qt::CaseSensitivity cs=Qt::CaseSensitive) noexcept
qsizetype indexOf(const QList< V > &list, const U &u, qsizetype from) noexcept
Q_CORE_EXPORT QByteArray convertToUtf8(QStringView str)
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype qustrlen(const char16_t *str) noexcept
Definition qstring.cpp:658
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION const char16_t * qustrcasechr(QStringView str, char16_t ch) noexcept
Definition qstring.cpp:775
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isUpper(QStringView s) noexcept
Definition qstring.cpp:5562
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION const char16_t * qustrchr(QStringView str, char16_t ch) noexcept
Definition qstring.cpp:687
constexpr Qt::strong_ordering compareThreeWay(LeftInt lhs, RightInt rhs) noexcept
CaseSensitivity
@ CaseSensitive
@ KeepEmptyParts
Definition qnamespace.h:127
constexpr bool is_constant_evaluated() noexcept
#define Q_DECLARE_STRONGLY_ORDERED(...)
#define Q_DECL_NS_RETURNS_AUTORELEASED
#define Q_DECL_CF_RETURNS_RETAINED
#define Q_IMPLICIT
#define Q_ALWAYS_INLINE
#define Q_FORWARD_DECLARE_CF_TYPE(type)
#define Q_FORWARD_DECLARE_OBJC_CLASS(classname)
Flags
constexpr const T & qMax(const T &a, const T &b)
Definition qminmax.h:42
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLuint GLuint end
GLenum GLenum GLsizei count
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLfloat GLfloat f
GLenum type
GLbitfield flags
GLfloat n
GLdouble s
[6]
Definition qopenglext.h:235
GLuint res
const GLubyte * c
GLenum array
GLuint64EXT * result
[6]
GLenum GLsizei len
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
QtPrivate::QRegularExpressionMatchIteratorRangeBasedForIterator begin(const QRegularExpressionMatchIterator &iterator)
static constexpr QChar sep
constexpr auto qTokenize(Haystack &&h, Needle &&n, Flags...flags) noexcept(QtPrivate::Tok::is_nothrow_constructible_from< Haystack, Needle >::value) -> decltype(QtPrivate::Tok::TokenizerResult< Haystack, Needle >{std::forward< Haystack >(h), std::forward< Needle >(n), flags...})
QStringView qToStringViewIgnoringNull(const QStringLike &s) noexcept
#define QT_ASCII_CAST_WARN
char Char
@ Q_PRIMITIVE_TYPE
Definition qtypeinfo.h:157
#define Q_DECLARE_TYPEINFO(TYPE, FLAGS)
Definition qtypeinfo.h:180
unsigned long ulong
Definition qtypes.h:35
quint64 qulonglong
Definition qtypes.h:64
ptrdiff_t qsizetype
Definition qtypes.h:165
unsigned int uint
Definition qtypes.h:34
unsigned short ushort
Definition qtypes.h:33
qint64 qlonglong
Definition qtypes.h:63
static const uint base
Definition qurlidna.cpp:20
QSharedPointer< T > other(t)
[5]
QJSValueList args