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