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