1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2020 The Qt Company Ltd. |
4 | ** Copyright (C) 2019 Intel Corporation. |
5 | ** Copyright (C) 2019 Mail.ru Group. |
6 | ** Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@kdab.com> |
7 | ** Contact: https://www.qt.io/licensing/ |
8 | ** |
9 | ** This file is part of the QtCore module of the Qt Toolkit. |
10 | ** |
11 | ** $QT_BEGIN_LICENSE:LGPL$ |
12 | ** Commercial License Usage |
13 | ** Licensees holding valid commercial Qt licenses may use this file in |
14 | ** accordance with the commercial license agreement provided with the |
15 | ** Software or, alternatively, in accordance with the terms contained in |
16 | ** a written agreement between you and The Qt Company. For licensing terms |
17 | ** and conditions see https://www.qt.io/terms-conditions. For further |
18 | ** information use the contact form at https://www.qt.io/contact-us. |
19 | ** |
20 | ** GNU Lesser General Public License Usage |
21 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
22 | ** General Public License version 3 as published by the Free Software |
23 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
24 | ** packaging of this file. Please review the following information to |
25 | ** ensure the GNU Lesser General Public License version 3 requirements |
26 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
27 | ** |
28 | ** GNU General Public License Usage |
29 | ** Alternatively, this file may be used under the terms of the GNU |
30 | ** General Public License version 2.0 or (at your option) the GNU General |
31 | ** Public license version 3 or any later version approved by the KDE Free |
32 | ** Qt Foundation. The licenses are as published by the Free Software |
33 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
34 | ** included in the packaging of this file. Please review the following |
35 | ** information to ensure the GNU General Public License requirements will |
36 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
37 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
38 | ** |
39 | ** $QT_END_LICENSE$ |
40 | ** |
41 | ****************************************************************************/ |
42 | |
43 | #ifndef QSTRING_H |
44 | #define QSTRING_H |
45 | |
46 | #if defined(QT_NO_CAST_FROM_ASCII) && defined(QT_RESTRICTED_CAST_FROM_ASCII) |
47 | #error QT_NO_CAST_FROM_ASCII and QT_RESTRICTED_CAST_FROM_ASCII must not be defined at the same time |
48 | #endif |
49 | |
50 | #include <QtCore/qchar.h> |
51 | #include <QtCore/qbytearray.h> |
52 | #include <QtCore/qarraydata.h> |
53 | #include <QtCore/qnamespace.h> |
54 | #include <QtCore/qstringliteral.h> |
55 | #include <QtCore/qstringalgorithms.h> |
56 | #include <QtCore/qanystringview.h> |
57 | #include <QtCore/qstringtokenizer.h> |
58 | |
59 | #include <string> |
60 | #include <iterator> |
61 | |
62 | #include <stdarg.h> |
63 | |
64 | #ifdef truncate |
65 | #error qstring.h must be included before any header file that defines truncate |
66 | #endif |
67 | |
68 | #if defined(Q_OS_DARWIN) || defined(Q_QDOC) |
69 | Q_FORWARD_DECLARE_CF_TYPE(CFString); |
70 | Q_FORWARD_DECLARE_OBJC_CLASS(NSString); |
71 | #endif |
72 | |
73 | QT_BEGIN_NAMESPACE |
74 | |
75 | class QRegularExpression; |
76 | class QRegularExpressionMatch; |
77 | class QString; |
78 | |
79 | namespace QtPrivate { |
80 | template <bool...B> class BoolList; |
81 | } |
82 | |
83 | class QLatin1String |
84 | { |
85 | public: |
86 | constexpr inline QLatin1String() noexcept : m_size(0), m_data(nullptr) {} |
87 | constexpr inline explicit QLatin1String(const char *s) noexcept : m_size(s ? qsizetype(strlen(s)) : 0), m_data(s) {} |
88 | constexpr explicit QLatin1String(const char *f, const char *l) |
89 | : QLatin1String(f, qsizetype(l - f)) {} |
90 | constexpr inline explicit QLatin1String(const char *s, qsizetype sz) noexcept : m_size(sz), m_data(s) {} |
91 | explicit QLatin1String(const QByteArray &s) noexcept : m_size(qsizetype(qstrnlen(s.constData(), s.size()))), m_data(s.constData()) {} |
92 | |
93 | inline QString toString() const; |
94 | |
95 | constexpr const char *latin1() const noexcept { return m_data; } |
96 | constexpr qsizetype size() const noexcept { return m_size; } |
97 | constexpr const char *data() const noexcept { return m_data; } |
98 | |
99 | constexpr bool isNull() const noexcept { return !data(); } |
100 | constexpr bool isEmpty() const noexcept { return !size(); } |
101 | |
102 | template <typename...Args> |
103 | [[nodiscard]] inline QString arg(Args &&...args) const; |
104 | |
105 | [[nodiscard]] constexpr QLatin1Char at(qsizetype i) const |
106 | { return Q_ASSERT(i >= 0), Q_ASSERT(i < size()), QLatin1Char(m_data[i]); } |
107 | [[nodiscard]] constexpr QLatin1Char operator[](qsizetype i) const { return at(i); } |
108 | |
109 | [[nodiscard]] constexpr QLatin1Char front() const { return at(0); } |
110 | [[nodiscard]] constexpr QLatin1Char back() const { return at(size() - 1); } |
111 | |
112 | [[nodiscard]] int compare(QStringView other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept |
113 | { return QtPrivate::compareStrings(*this, other, cs); } |
114 | [[nodiscard]] int compare(QLatin1String other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept |
115 | { return QtPrivate::compareStrings(*this, other, cs); } |
116 | [[nodiscard]] constexpr int compare(QChar c) const noexcept |
117 | { return isEmpty() || front() == c ? size() - 1 : uchar(m_data[0]) - c.unicode() ; } |
118 | [[nodiscard]] int compare(QChar c, Qt::CaseSensitivity cs) const noexcept |
119 | { return QtPrivate::compareStrings(*this, QStringView(&c, 1), cs); } |
120 | |
121 | [[nodiscard]] bool startsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept |
122 | { return QtPrivate::startsWith(*this, s, cs); } |
123 | [[nodiscard]] bool startsWith(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept |
124 | { return QtPrivate::startsWith(*this, s, cs); } |
125 | [[nodiscard]] constexpr bool startsWith(QChar c) const noexcept |
126 | { return !isEmpty() && front() == c; } |
127 | [[nodiscard]] inline bool startsWith(QChar c, Qt::CaseSensitivity cs) const noexcept |
128 | { return QtPrivate::startsWith(*this, QStringView(&c, 1), cs); } |
129 | |
130 | [[nodiscard]] bool endsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept |
131 | { return QtPrivate::endsWith(*this, s, cs); } |
132 | [[nodiscard]] bool endsWith(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept |
133 | { return QtPrivate::endsWith(*this, s, cs); } |
134 | [[nodiscard]] constexpr bool endsWith(QChar c) const noexcept |
135 | { return !isEmpty() && back() == c; } |
136 | [[nodiscard]] inline bool endsWith(QChar c, Qt::CaseSensitivity cs) const noexcept |
137 | { return QtPrivate::endsWith(*this, QStringView(&c, 1), cs); } |
138 | |
139 | [[nodiscard]] qsizetype indexOf(QStringView s, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept |
140 | { return QtPrivate::findString(*this, from, s, cs); } |
141 | [[nodiscard]] qsizetype indexOf(QLatin1String s, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept |
142 | { return QtPrivate::findString(*this, from, s, cs); } |
143 | [[nodiscard]] qsizetype indexOf(QChar c, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept |
144 | { return QtPrivate::findString(*this, from, QStringView(&c, 1), cs); } |
145 | |
146 | [[nodiscard]] bool contains(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept |
147 | { return indexOf(s, 0, cs) != -1; } |
148 | [[nodiscard]] bool contains(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept |
149 | { return indexOf(s, 0, cs) != -1; } |
150 | [[nodiscard]] inline bool contains(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept |
151 | { return indexOf(QStringView(&c, 1), 0, cs) != -1; } |
152 | |
153 | [[nodiscard]] qsizetype lastIndexOf(QStringView s, qsizetype from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept |
154 | { return QtPrivate::lastIndexOf(*this, from, s, cs); } |
155 | [[nodiscard]] qsizetype lastIndexOf(QLatin1String s, qsizetype from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept |
156 | { return QtPrivate::lastIndexOf(*this, from, s, cs); } |
157 | [[nodiscard]] qsizetype lastIndexOf(QChar c, qsizetype from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept |
158 | { return QtPrivate::lastIndexOf(*this, from, QStringView(&c, 1), cs); } |
159 | |
160 | using value_type = const char; |
161 | using reference = value_type&; |
162 | using const_reference = reference; |
163 | using iterator = value_type*; |
164 | using const_iterator = iterator; |
165 | using difference_type = qsizetype; // violates Container concept requirements |
166 | using size_type = qsizetype; // violates Container concept requirements |
167 | |
168 | constexpr const_iterator begin() const noexcept { return data(); } |
169 | constexpr const_iterator cbegin() const noexcept { return data(); } |
170 | constexpr const_iterator end() const noexcept { return data() + size(); } |
171 | constexpr const_iterator cend() const noexcept { return data() + size(); } |
172 | |
173 | using reverse_iterator = std::reverse_iterator<iterator>; |
174 | using const_reverse_iterator = reverse_iterator; |
175 | |
176 | const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); } |
177 | const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(end()); } |
178 | const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); } |
179 | const_reverse_iterator crend() const noexcept { return const_reverse_iterator(begin()); } |
180 | |
181 | [[nodiscard]] constexpr QLatin1String mid(qsizetype pos, qsizetype n = -1) const |
182 | { |
183 | using namespace QtPrivate; |
184 | auto result = QContainerImplHelper::mid(size(), &pos, &n); |
185 | return result == QContainerImplHelper::Null ? QLatin1String() : QLatin1String(m_data + pos, n); |
186 | } |
187 | [[nodiscard]] constexpr QLatin1String left(qsizetype n) const |
188 | { |
189 | if (size_t(n) >= size_t(size())) |
190 | n = size(); |
191 | return QLatin1String(m_data, n); |
192 | } |
193 | [[nodiscard]] constexpr QLatin1String right(qsizetype n) const |
194 | { |
195 | if (size_t(n) >= size_t(size())) |
196 | n = size(); |
197 | return QLatin1String(m_data + m_size - n, n); |
198 | } |
199 | |
200 | [[nodiscard]] constexpr QLatin1String sliced(qsizetype pos) const |
201 | { verify(pos); return QLatin1String(m_data + pos, m_size - pos); } |
202 | [[nodiscard]] constexpr QLatin1String sliced(qsizetype pos, qsizetype n) const |
203 | { verify(pos, n); return QLatin1String(m_data + pos, n); } |
204 | [[nodiscard]] constexpr QLatin1String first(qsizetype n) const |
205 | { verify(n); return QLatin1String(m_data, n); } |
206 | [[nodiscard]] constexpr QLatin1String last(qsizetype n) const |
207 | { verify(n); return QLatin1String(m_data + size() - n, n); } |
208 | [[nodiscard]] constexpr QLatin1String chopped(qsizetype n) const |
209 | { verify(n); return QLatin1String(m_data, size() - n); } |
210 | |
211 | constexpr void chop(qsizetype n) |
212 | { verify(n); m_size -= n; } |
213 | constexpr void truncate(qsizetype n) |
214 | { verify(n); m_size = n; } |
215 | |
216 | [[nodiscard]] QLatin1String trimmed() const noexcept { return QtPrivate::trimmed(*this); } |
217 | |
218 | template <typename Needle, typename...Flags> |
219 | [[nodiscard]] inline constexpr auto tokenize(Needle &&needle, Flags...flags) const |
220 | noexcept(noexcept(qTokenize(std::declval<const QLatin1String &>(), std::forward<Needle>(needle), flags...))) |
221 | -> decltype(qTokenize(*this, std::forward<Needle>(needle), flags...)) |
222 | { return qTokenize(*this, std::forward<Needle>(needle), flags...); } |
223 | |
224 | friend inline bool operator==(QLatin1String s1, QLatin1String s2) noexcept |
225 | { return s1.size() == s2.size() && (!s1.size() || !memcmp(s1.latin1(), s2.latin1(), s1.size())); } |
226 | friend inline bool operator!=(QLatin1String s1, QLatin1String s2) noexcept |
227 | { return !(s1 == s2); } |
228 | friend inline bool operator<(QLatin1String s1, QLatin1String s2) noexcept |
229 | { |
230 | const qsizetype len = qMin(s1.size(), s2.size()); |
231 | const int r = len ? memcmp(s1.latin1(), s2.latin1(), len) : 0; |
232 | return r < 0 || (r == 0 && s1.size() < s2.size()); |
233 | } |
234 | friend inline bool operator>(QLatin1String s1, QLatin1String s2) noexcept |
235 | { return s2 < s1; } |
236 | friend inline bool operator<=(QLatin1String s1, QLatin1String s2) noexcept |
237 | { return !(s1 > s2); } |
238 | friend inline bool operator>=(QLatin1String s1, QLatin1String s2) noexcept |
239 | { return !(s1 < s2); } |
240 | |
241 | // QChar <> QLatin1String |
242 | friend inline bool operator==(QChar lhs, QLatin1String rhs) noexcept { return rhs.size() == 1 && lhs == rhs.front(); } |
243 | friend inline bool operator< (QChar lhs, QLatin1String rhs) noexcept { return compare_helper(&lhs, 1, rhs) < 0; } |
244 | friend inline bool operator> (QChar lhs, QLatin1String rhs) noexcept { return compare_helper(&lhs, 1, rhs) > 0; } |
245 | friend inline bool operator!=(QChar lhs, QLatin1String rhs) noexcept { return !(lhs == rhs); } |
246 | friend inline bool operator<=(QChar lhs, QLatin1String rhs) noexcept { return !(lhs > rhs); } |
247 | friend inline bool operator>=(QChar lhs, QLatin1String rhs) noexcept { return !(lhs < rhs); } |
248 | |
249 | friend inline bool operator==(QLatin1String lhs, QChar rhs) noexcept { return rhs == lhs; } |
250 | friend inline bool operator!=(QLatin1String lhs, QChar rhs) noexcept { return !(rhs == lhs); } |
251 | friend inline bool operator< (QLatin1String lhs, QChar rhs) noexcept { return rhs > lhs; } |
252 | friend inline bool operator> (QLatin1String lhs, QChar rhs) noexcept { return rhs < lhs; } |
253 | friend inline bool operator<=(QLatin1String lhs, QChar rhs) noexcept { return !(rhs < lhs); } |
254 | friend inline bool operator>=(QLatin1String lhs, QChar rhs) noexcept { return !(rhs > lhs); } |
255 | |
256 | // QStringView <> QLatin1String |
257 | friend inline bool operator==(QStringView lhs, QLatin1String rhs) noexcept |
258 | { return lhs.size() == rhs.size() && QtPrivate::equalStrings(lhs, rhs); } |
259 | friend inline bool operator!=(QStringView lhs, QLatin1String rhs) noexcept { return !(lhs == rhs); } |
260 | friend inline bool operator< (QStringView lhs, QLatin1String rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) < 0; } |
261 | friend inline bool operator<=(QStringView lhs, QLatin1String rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) <= 0; } |
262 | friend inline bool operator> (QStringView lhs, QLatin1String rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) > 0; } |
263 | friend inline bool operator>=(QStringView lhs, QLatin1String rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) >= 0; } |
264 | |
265 | friend inline bool operator==(QLatin1String lhs, QStringView rhs) noexcept |
266 | { return lhs.size() == rhs.size() && QtPrivate::equalStrings(lhs, rhs); } |
267 | friend inline bool operator!=(QLatin1String lhs, QStringView rhs) noexcept { return !(lhs == rhs); } |
268 | friend inline bool operator< (QLatin1String lhs, QStringView rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) < 0; } |
269 | friend inline bool operator<=(QLatin1String lhs, QStringView rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) <= 0; } |
270 | friend inline bool operator> (QLatin1String lhs, QStringView rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) > 0; } |
271 | friend inline bool operator>=(QLatin1String lhs, QStringView rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) >= 0; } |
272 | |
273 | |
274 | #if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) |
275 | QT_ASCII_CAST_WARN inline bool operator==(const char *s) const; |
276 | QT_ASCII_CAST_WARN inline bool operator!=(const char *s) const; |
277 | QT_ASCII_CAST_WARN inline bool operator<(const char *s) const; |
278 | QT_ASCII_CAST_WARN inline bool operator>(const char *s) const; |
279 | QT_ASCII_CAST_WARN inline bool operator<=(const char *s) const; |
280 | QT_ASCII_CAST_WARN inline bool operator>=(const char *s) const; |
281 | |
282 | QT_ASCII_CAST_WARN inline bool operator==(const QByteArray &s) const; |
283 | QT_ASCII_CAST_WARN inline bool operator!=(const QByteArray &s) const; |
284 | QT_ASCII_CAST_WARN inline bool operator<(const QByteArray &s) const; |
285 | QT_ASCII_CAST_WARN inline bool operator>(const QByteArray &s) const; |
286 | QT_ASCII_CAST_WARN inline bool operator<=(const QByteArray &s) const; |
287 | QT_ASCII_CAST_WARN inline bool operator>=(const QByteArray &s) const; |
288 | |
289 | QT_ASCII_CAST_WARN friend bool operator==(const char *s1, QLatin1String s2) { return compare_helper(s2, s1) == 0; } |
290 | QT_ASCII_CAST_WARN friend bool operator!=(const char *s1, QLatin1String s2) { return compare_helper(s2, s1) != 0; } |
291 | QT_ASCII_CAST_WARN friend bool operator< (const char *s1, QLatin1String s2) { return compare_helper(s2, s1) > 0; } |
292 | QT_ASCII_CAST_WARN friend bool operator> (const char *s1, QLatin1String s2) { return compare_helper(s2, s1) < 0; } |
293 | QT_ASCII_CAST_WARN friend bool operator<=(const char *s1, QLatin1String s2) { return compare_helper(s2, s1) >= 0; } |
294 | QT_ASCII_CAST_WARN friend bool operator>=(const char *s1, QLatin1String s2) { return compare_helper(s2, s1) <= 0; } |
295 | #endif // !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) |
296 | |
297 | private: |
298 | #if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) |
299 | static inline int compare_helper(const QLatin1String &s1, const char *s2); |
300 | #endif |
301 | Q_ALWAYS_INLINE constexpr void verify(qsizetype pos, qsizetype n = 0) const |
302 | { |
303 | Q_ASSERT(pos >= 0); |
304 | Q_ASSERT(pos <= size()); |
305 | Q_ASSERT(n >= 0); |
306 | Q_ASSERT(n <= size() - pos); |
307 | } |
308 | Q_CORE_EXPORT static int compare_helper(const QChar *data1, qsizetype length1, |
309 | QLatin1String s2, |
310 | Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept; |
311 | qsizetype m_size; |
312 | const char *m_data; |
313 | }; |
314 | Q_DECLARE_TYPEINFO(QLatin1String, Q_RELOCATABLE_TYPE); |
315 | |
316 | // Qt 4.x compatibility |
317 | |
318 | // |
319 | // QLatin1String inline implementations |
320 | // |
321 | constexpr bool QtPrivate::isLatin1(QLatin1String) noexcept |
322 | { return true; } |
323 | |
324 | // |
325 | // QStringView members that require QLatin1String: |
326 | // |
327 | int QStringView::compare(QLatin1String s, Qt::CaseSensitivity cs) const noexcept |
328 | { return QtPrivate::compareStrings(*this, s, cs); } |
329 | bool QStringView::startsWith(QLatin1String s, Qt::CaseSensitivity cs) const noexcept |
330 | { return QtPrivate::startsWith(*this, s, cs); } |
331 | bool QStringView::endsWith(QLatin1String s, Qt::CaseSensitivity cs) const noexcept |
332 | { return QtPrivate::endsWith(*this, s, cs); } |
333 | qsizetype QStringView::indexOf(QLatin1String s, qsizetype from, Qt::CaseSensitivity cs) const noexcept |
334 | { return QtPrivate::findString(*this, from, s, cs); } |
335 | bool QStringView::contains(QLatin1String s, Qt::CaseSensitivity cs) const noexcept |
336 | { return indexOf(s, 0, cs) != qsizetype(-1); } |
337 | qsizetype QStringView::lastIndexOf(QLatin1String s, qsizetype from, Qt::CaseSensitivity cs) const noexcept |
338 | { return QtPrivate::lastIndexOf(*this, from, s, cs); } |
339 | |
340 | // |
341 | // QAnyStringView members that require QLatin1String |
342 | // |
343 | |
344 | constexpr QAnyStringView::QAnyStringView(QLatin1String str) noexcept |
345 | : m_data{str.data()}, m_size{size_t(str.size()) | Tag::Latin1} {} |
346 | |
347 | constexpr QLatin1String QAnyStringView::asLatin1StringView() const |
348 | { |
349 | Q_ASSERT(isLatin1()); |
350 | return QLatin1String{m_data_utf8, int(size())}; |
351 | } |
352 | |
353 | template <typename Visitor> |
354 | constexpr decltype(auto) QAnyStringView::visit(Visitor &&v) const |
355 | { |
356 | if (isUtf16()) |
357 | return std::forward<Visitor>(v)(asStringView()); |
358 | else if (isLatin1()) |
359 | return std::forward<Visitor>(v)(asLatin1StringView()); |
360 | else |
361 | return std::forward<Visitor>(v)(asUtf8StringView()); |
362 | } |
363 | |
364 | // |
365 | // QAnyStringView members that require QAnyStringView::visit() |
366 | // |
367 | |
368 | constexpr QChar QAnyStringView::front() const |
369 | { |
370 | return visit([] (auto that) { return QAnyStringView::toQChar(that.front()); }); |
371 | } |
372 | constexpr QChar QAnyStringView::back() const |
373 | { |
374 | return visit([] (auto that) { return QAnyStringView::toQChar(that.back()); }); |
375 | } |
376 | |
377 | |
378 | class Q_CORE_EXPORT QString |
379 | { |
380 | typedef QTypedArrayData<char16_t> Data; |
381 | public: |
382 | typedef QStringPrivate DataPointer; |
383 | |
384 | inline constexpr QString() noexcept; |
385 | explicit QString(const QChar *unicode, qsizetype size = -1); |
386 | QString(QChar c); |
387 | QString(qsizetype size, QChar c); |
388 | inline QString(QLatin1String latin1); |
389 | #if defined(__cpp_char8_t) || defined(Q_CLANG_QDOC) |
390 | Q_WEAK_OVERLOAD |
391 | inline QString(const char8_t *str) |
392 | : QString(fromUtf8(str)) |
393 | {} |
394 | #endif |
395 | inline QString(const QString &) noexcept; |
396 | inline ~QString(); |
397 | QString &operator=(QChar c); |
398 | QString &operator=(const QString &) noexcept; |
399 | QString &operator=(QLatin1String latin1); |
400 | inline QString(QString &&other) noexcept |
401 | { qSwap(d, other.d); } |
402 | QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QString) |
403 | inline void swap(QString &other) noexcept { qSwap(d, other.d); } |
404 | inline qsizetype size() const { return d.size; } |
405 | inline qsizetype count() const { return d.size; } |
406 | inline qsizetype length() const { return d.size; } |
407 | inline bool isEmpty() const; |
408 | void resize(qsizetype size); |
409 | void resize(qsizetype size, QChar fillChar); |
410 | |
411 | QString &fill(QChar c, qsizetype size = -1); |
412 | void truncate(qsizetype pos); |
413 | void chop(qsizetype n); |
414 | |
415 | inline qsizetype capacity() const; |
416 | inline void reserve(qsizetype size); |
417 | inline void squeeze(); |
418 | |
419 | inline const QChar *unicode() const; |
420 | inline QChar *data(); |
421 | inline const QChar *data() const; |
422 | inline const QChar *constData() const; |
423 | |
424 | inline void detach(); |
425 | inline bool isDetached() const; |
426 | inline bool isSharedWith(const QString &other) const { return d.isSharedWith(other.d); } |
427 | void clear(); |
428 | |
429 | inline const QChar at(qsizetype i) const; |
430 | const QChar operator[](qsizetype i) const; |
431 | [[nodiscard]] QChar &operator[](qsizetype i); |
432 | |
433 | [[nodiscard]] inline QChar front() const { return at(0); } |
434 | [[nodiscard]] inline QChar &front(); |
435 | [[nodiscard]] inline QChar back() const { return at(size() - 1); } |
436 | [[nodiscard]] inline QChar &back(); |
437 | |
438 | [[nodiscard]] QString arg(qlonglong a, int fieldwidth=0, int base=10, |
439 | QChar fillChar = QLatin1Char(' ')) const; |
440 | [[nodiscard]] QString arg(qulonglong a, int fieldwidth=0, int base=10, |
441 | QChar fillChar = QLatin1Char(' ')) const; |
442 | [[nodiscard]] QString arg(long a, int fieldwidth=0, int base=10, |
443 | QChar fillChar = QLatin1Char(' ')) const; |
444 | [[nodiscard]] QString arg(ulong a, int fieldwidth=0, int base=10, |
445 | QChar fillChar = QLatin1Char(' ')) const; |
446 | [[nodiscard]] QString arg(int a, int fieldWidth = 0, int base = 10, |
447 | QChar fillChar = QLatin1Char(' ')) const; |
448 | [[nodiscard]] QString arg(uint a, int fieldWidth = 0, int base = 10, |
449 | QChar fillChar = QLatin1Char(' ')) const; |
450 | [[nodiscard]] QString arg(short a, int fieldWidth = 0, int base = 10, |
451 | QChar fillChar = QLatin1Char(' ')) const; |
452 | [[nodiscard]] QString arg(ushort a, int fieldWidth = 0, int base = 10, |
453 | QChar fillChar = QLatin1Char(' ')) const; |
454 | [[nodiscard]] QString arg(double a, int fieldWidth = 0, char fmt = 'g', int prec = -1, |
455 | QChar fillChar = QLatin1Char(' ')) const; |
456 | [[nodiscard]] QString arg(char a, int fieldWidth = 0, |
457 | QChar fillChar = QLatin1Char(' ')) const; |
458 | [[nodiscard]] QString arg(QChar a, int fieldWidth = 0, |
459 | QChar fillChar = QLatin1Char(' ')) const; |
460 | #if QT_STRINGVIEW_LEVEL < 2 |
461 | [[nodiscard]] QString arg(const QString &a, int fieldWidth = 0, |
462 | QChar fillChar = QLatin1Char(' ')) const; |
463 | #endif |
464 | [[nodiscard]] QString arg(QStringView a, int fieldWidth = 0, |
465 | QChar fillChar = QLatin1Char(' ')) const; |
466 | [[nodiscard]] QString arg(QLatin1String a, int fieldWidth = 0, |
467 | QChar fillChar = QLatin1Char(' ')) const; |
468 | private: |
469 | template <typename T> |
470 | struct is_convertible_to_view_or_qstring_helper |
471 | : std::integral_constant<bool, |
472 | std::is_convertible<T, QString>::value || |
473 | std::is_convertible<T, QStringView>::value || |
474 | std::is_convertible<T, QLatin1String>::value> {}; |
475 | template <typename T> |
476 | struct is_convertible_to_view_or_qstring |
477 | : is_convertible_to_view_or_qstring_helper<typename std::decay<T>::type> {}; |
478 | public: |
479 | template <typename...Args> |
480 | [[nodiscard]] |
481 | #ifdef Q_CLANG_QDOC |
482 | QString |
483 | #else |
484 | typename std::enable_if< |
485 | sizeof...(Args) >= 2 && std::is_same< |
486 | QtPrivate::BoolList<is_convertible_to_view_or_qstring<Args>::value..., true>, |
487 | QtPrivate::BoolList<true, is_convertible_to_view_or_qstring<Args>::value...> |
488 | >::value, |
489 | QString |
490 | >::type |
491 | #endif |
492 | arg(Args &&...args) const |
493 | { return qToStringViewIgnoringNull(*this).arg(std::forward<Args>(args)...); } |
494 | |
495 | static QString vasprintf(const char *format, va_list ap) Q_ATTRIBUTE_FORMAT_PRINTF(1, 0); |
496 | static QString asprintf(const char *format, ...) Q_ATTRIBUTE_FORMAT_PRINTF(1, 2); |
497 | |
498 | [[nodiscard]] qsizetype indexOf(QChar c, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; |
499 | [[nodiscard]] qsizetype indexOf(QLatin1String s, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; |
500 | #if QT_STRINGVIEW_LEVEL < 2 |
501 | [[nodiscard]] qsizetype indexOf(const QString &s, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; |
502 | #endif |
503 | [[nodiscard]] qsizetype indexOf(QStringView s, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept |
504 | { return QtPrivate::findString(*this, from, s, cs); } |
505 | [[nodiscard]] qsizetype lastIndexOf(QChar c, qsizetype from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; |
506 | [[nodiscard]] qsizetype lastIndexOf(QLatin1String s, qsizetype from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; |
507 | #if QT_STRINGVIEW_LEVEL < 2 |
508 | [[nodiscard]] qsizetype lastIndexOf(const QString &s, qsizetype from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; |
509 | #endif |
510 | |
511 | [[nodiscard]] qsizetype lastIndexOf(QStringView s, qsizetype from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept |
512 | { return QtPrivate::lastIndexOf(*this, from, s, cs); } |
513 | |
514 | [[nodiscard]] inline bool contains(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; |
515 | #if QT_STRINGVIEW_LEVEL < 2 |
516 | [[nodiscard]] inline bool contains(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; |
517 | #endif |
518 | [[nodiscard]] inline bool contains(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; |
519 | [[nodiscard]] inline bool contains(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; |
520 | [[nodiscard]] qsizetype count(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; |
521 | [[nodiscard]] qsizetype count(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; |
522 | [[nodiscard]] qsizetype count(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; |
523 | |
524 | #if QT_CONFIG(regularexpression) |
525 | [[nodiscard]] qsizetype indexOf(const QRegularExpression &re, qsizetype from = 0, |
526 | QRegularExpressionMatch *rmatch = nullptr) const; |
527 | [[nodiscard]] qsizetype lastIndexOf(const QRegularExpression &re, qsizetype from = -1, |
528 | QRegularExpressionMatch *rmatch = nullptr) const; |
529 | [[nodiscard]] bool contains(const QRegularExpression &re, QRegularExpressionMatch *rmatch = nullptr) const; |
530 | [[nodiscard]] qsizetype count(const QRegularExpression &re) const; |
531 | #endif |
532 | |
533 | enum SectionFlag { |
534 | SectionDefault = 0x00, |
535 | SectionSkipEmpty = 0x01, |
536 | SectionIncludeLeadingSep = 0x02, |
537 | SectionIncludeTrailingSep = 0x04, |
538 | SectionCaseInsensitiveSeps = 0x08 |
539 | }; |
540 | Q_DECLARE_FLAGS(SectionFlags, SectionFlag) |
541 | |
542 | [[nodiscard]] QString section(QChar sep, qsizetype start, qsizetype end = -1, SectionFlags flags = SectionDefault) const; |
543 | [[nodiscard]] QString section(const QString &in_sep, qsizetype start, qsizetype end = -1, SectionFlags flags = SectionDefault) const; |
544 | #if QT_CONFIG(regularexpression) |
545 | [[nodiscard]] QString section(const QRegularExpression &re, qsizetype start, qsizetype end = -1, SectionFlags flags = SectionDefault) const; |
546 | #endif |
547 | [[nodiscard]] QString left(qsizetype n) const; |
548 | [[nodiscard]] QString right(qsizetype n) const; |
549 | [[nodiscard]] QString mid(qsizetype position, qsizetype n = -1) const; |
550 | |
551 | [[nodiscard]] QString first(qsizetype n) const |
552 | { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); return QString(data(), n); } |
553 | [[nodiscard]] QString last(qsizetype n) const |
554 | { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); return QString(data() + size() - n, n); } |
555 | [[nodiscard]] QString sliced(qsizetype pos) const |
556 | { Q_ASSERT(pos >= 0); Q_ASSERT(pos <= size()); return QString(data() + pos, size() - pos); } |
557 | [[nodiscard]] QString sliced(qsizetype pos, qsizetype n) const |
558 | { Q_ASSERT(pos >= 0); Q_ASSERT(n >= 0); Q_ASSERT(size_t(pos) + size_t(n) <= size_t(size())); return QString(data() + pos, n); } |
559 | [[nodiscard]] QString chopped(qsizetype n) const |
560 | { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); return first(size() - n); } |
561 | |
562 | |
563 | #if QT_STRINGVIEW_LEVEL < 2 |
564 | bool startsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; |
565 | #endif |
566 | [[nodiscard]] bool startsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept |
567 | { return QtPrivate::startsWith(*this, s, cs); } |
568 | bool startsWith(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; |
569 | bool startsWith(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; |
570 | |
571 | #if QT_STRINGVIEW_LEVEL < 2 |
572 | bool endsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; |
573 | #endif |
574 | [[nodiscard]] bool endsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept |
575 | { return QtPrivate::endsWith(*this, s, cs); } |
576 | bool endsWith(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; |
577 | bool endsWith(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; |
578 | |
579 | bool isUpper() const; |
580 | bool isLower() const; |
581 | |
582 | [[nodiscard]] QString leftJustified(qsizetype width, QChar fill = QLatin1Char(' '), bool trunc = false) const; |
583 | [[nodiscard]] QString rightJustified(qsizetype width, QChar fill = QLatin1Char(' '), bool trunc = false) const; |
584 | |
585 | #if !defined(Q_CLANG_QDOC) |
586 | [[nodiscard]] QString toLower() const & |
587 | { return toLower_helper(*this); } |
588 | [[nodiscard]] QString toLower() && |
589 | { return toLower_helper(*this); } |
590 | [[nodiscard]] QString toUpper() const & |
591 | { return toUpper_helper(*this); } |
592 | [[nodiscard]] QString toUpper() && |
593 | { return toUpper_helper(*this); } |
594 | [[nodiscard]] QString toCaseFolded() const & |
595 | { return toCaseFolded_helper(*this); } |
596 | [[nodiscard]] QString toCaseFolded() && |
597 | { return toCaseFolded_helper(*this); } |
598 | [[nodiscard]] QString trimmed() const & |
599 | { return trimmed_helper(*this); } |
600 | [[nodiscard]] QString trimmed() && |
601 | { return trimmed_helper(*this); } |
602 | [[nodiscard]] QString simplified() const & |
603 | { return simplified_helper(*this); } |
604 | [[nodiscard]] QString simplified() && |
605 | { return simplified_helper(*this); } |
606 | #else |
607 | [[nodiscard]] QString toLower() const; |
608 | [[nodiscard]] QString toUpper() const; |
609 | [[nodiscard]] QString toCaseFolded() const; |
610 | [[nodiscard]] QString trimmed() const; |
611 | [[nodiscard]] QString simplified() const; |
612 | #endif |
613 | [[nodiscard]] QString toHtmlEscaped() const; |
614 | |
615 | QString &insert(qsizetype i, QChar c); |
616 | QString &insert(qsizetype i, const QChar *uc, qsizetype len); |
617 | #if QT_STRINGVIEW_LEVEL < 2 |
618 | inline QString &insert(qsizetype i, const QString &s) { return insert(i, s.constData(), s.length()); } |
619 | #endif |
620 | inline QString &insert(qsizetype i, QStringView v) { return insert(i, v.data(), v.length()); } |
621 | QString &insert(qsizetype i, QLatin1String s); |
622 | |
623 | QString &append(QChar c); |
624 | QString &append(const QChar *uc, qsizetype len); |
625 | #if QT_STRINGVIEW_LEVEL < 2 |
626 | QString &append(const QString &s); |
627 | #endif |
628 | inline QString &append(QStringView v) { return append(v.data(), v.length()); } |
629 | QString &append(QLatin1String s); |
630 | |
631 | inline QString &prepend(QChar c) { return insert(0, c); } |
632 | inline QString &prepend(const QChar *uc, qsizetype len) { return insert(0, uc, len); } |
633 | #if QT_STRINGVIEW_LEVEL < 2 |
634 | inline QString &prepend(const QString &s) { return insert(0, s); } |
635 | #endif |
636 | inline QString &prepend(QStringView v) { return prepend(v.data(), v.length()); } |
637 | inline QString &prepend(QLatin1String s) { return insert(0, s); } |
638 | |
639 | inline QString &operator+=(QChar c) { return append(c); } |
640 | |
641 | #if QT_STRINGVIEW_LEVEL < 2 |
642 | inline QString &operator+=(const QString &s) { return append(s); } |
643 | #endif |
644 | inline QString &operator+=(QStringView v) { return append(v); } |
645 | inline QString &operator+=(QLatin1String s) { return append(s); } |
646 | |
647 | QString &remove(qsizetype i, qsizetype len); |
648 | QString &remove(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive); |
649 | QString &remove(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive); |
650 | QString &remove(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive); |
651 | template <typename Predicate> |
652 | QString &removeIf(Predicate pred) |
653 | { |
654 | QtPrivate::sequential_erase_if(*this, pred); |
655 | return *this; |
656 | } |
657 | QString &replace(qsizetype i, qsizetype len, QChar after); |
658 | QString &replace(qsizetype i, qsizetype len, const QChar *s, qsizetype slen); |
659 | QString &replace(qsizetype i, qsizetype len, const QString &after); |
660 | QString &replace(QChar before, QChar after, Qt::CaseSensitivity cs = Qt::CaseSensitive); |
661 | QString &replace(const QChar *before, qsizetype blen, const QChar *after, qsizetype alen, Qt::CaseSensitivity cs = Qt::CaseSensitive); |
662 | QString &replace(QLatin1String before, QLatin1String after, Qt::CaseSensitivity cs = Qt::CaseSensitive); |
663 | QString &replace(QLatin1String before, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive); |
664 | QString &replace(const QString &before, QLatin1String after, Qt::CaseSensitivity cs = Qt::CaseSensitive); |
665 | QString &replace(const QString &before, const QString &after, |
666 | Qt::CaseSensitivity cs = Qt::CaseSensitive); |
667 | QString &replace(QChar c, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive); |
668 | QString &replace(QChar c, QLatin1String after, Qt::CaseSensitivity cs = Qt::CaseSensitive); |
669 | #if QT_CONFIG(regularexpression) |
670 | QString &replace(const QRegularExpression &re, const QString &after); |
671 | inline QString &remove(const QRegularExpression &re) |
672 | { return replace(re, QString()); } |
673 | #endif |
674 | |
675 | public: |
676 | [[nodiscard]] |
677 | QStringList split(const QString &sep, Qt::SplitBehavior behavior = Qt::KeepEmptyParts, |
678 | Qt::CaseSensitivity cs = Qt::CaseSensitive) const; |
679 | [[nodiscard]] |
680 | QStringList split(QChar sep, Qt::SplitBehavior behavior = Qt::KeepEmptyParts, |
681 | Qt::CaseSensitivity cs = Qt::CaseSensitive) const; |
682 | #ifndef QT_NO_REGULAREXPRESSION |
683 | [[nodiscard]] |
684 | QStringList split(const QRegularExpression &sep, |
685 | Qt::SplitBehavior behavior = Qt::KeepEmptyParts) const; |
686 | #endif |
687 | |
688 | template <typename Needle, typename...Flags> |
689 | [[nodiscard]] inline auto tokenize(Needle &&needle, Flags...flags) const & |
690 | noexcept(noexcept(qTokenize(std::declval<const QString &>(), std::forward<Needle>(needle), flags...))) |
691 | -> decltype(qTokenize(*this, std::forward<Needle>(needle), flags...)) |
692 | { return qTokenize(qToStringViewIgnoringNull(*this), std::forward<Needle>(needle), flags...); } |
693 | |
694 | template <typename Needle, typename...Flags> |
695 | [[nodiscard]] inline auto tokenize(Needle &&needle, Flags...flags) const && |
696 | noexcept(noexcept(qTokenize(std::declval<const QString>(), std::forward<Needle>(needle), flags...))) |
697 | -> decltype(qTokenize(std::move(*this), std::forward<Needle>(needle), flags...)) |
698 | { return qTokenize(std::move(*this), std::forward<Needle>(needle), flags...); } |
699 | |
700 | template <typename Needle, typename...Flags> |
701 | [[nodiscard]] inline auto tokenize(Needle &&needle, Flags...flags) && |
702 | noexcept(noexcept(qTokenize(std::declval<QString>(), std::forward<Needle>(needle), flags...))) |
703 | -> decltype(qTokenize(std::move(*this), std::forward<Needle>(needle), flags...)) |
704 | { return qTokenize(std::move(*this), std::forward<Needle>(needle), flags...); } |
705 | |
706 | |
707 | enum NormalizationForm { |
708 | NormalizationForm_D, |
709 | NormalizationForm_C, |
710 | NormalizationForm_KD, |
711 | NormalizationForm_KC |
712 | }; |
713 | [[nodiscard]] QString normalized(NormalizationForm mode, QChar::UnicodeVersion version = QChar::Unicode_Unassigned) const; |
714 | |
715 | [[nodiscard]] QString repeated(qsizetype times) const; |
716 | |
717 | const ushort *utf16() const; // ### Qt 7 char16_t |
718 | |
719 | #if !defined(Q_CLANG_QDOC) |
720 | [[nodiscard]] QByteArray toLatin1() const & |
721 | { return toLatin1_helper(*this); } |
722 | [[nodiscard]] QByteArray toLatin1() && |
723 | { return toLatin1_helper_inplace(*this); } |
724 | [[nodiscard]] QByteArray toUtf8() const & |
725 | { return toUtf8_helper(*this); } |
726 | [[nodiscard]] QByteArray toUtf8() && |
727 | { return toUtf8_helper(*this); } |
728 | [[nodiscard]] QByteArray toLocal8Bit() const & |
729 | { return toLocal8Bit_helper(isNull() ? nullptr : constData(), size()); } |
730 | [[nodiscard]] QByteArray toLocal8Bit() && |
731 | { return toLocal8Bit_helper(isNull() ? nullptr : constData(), size()); } |
732 | #else |
733 | [[nodiscard]] QByteArray toLatin1() const; |
734 | [[nodiscard]] QByteArray toUtf8() const; |
735 | [[nodiscard]] QByteArray toLocal8Bit() const; |
736 | #endif |
737 | [[nodiscard]] QList<uint> toUcs4() const; // ### Qt 7 char32_t |
738 | |
739 | // note - this are all inline so we can benefit from strlen() compile time optimizations |
740 | static QString fromLatin1(QByteArrayView ba); |
741 | Q_WEAK_OVERLOAD |
742 | static inline QString fromLatin1(const QByteArray &ba) { return fromLatin1(QByteArrayView(ba)); } |
743 | static inline QString fromLatin1(const char *str, qsizetype size) |
744 | { |
745 | return fromLatin1(QByteArrayView(str, !str || size < 0 ? qstrlen(str) : size)); |
746 | } |
747 | static QString fromUtf8(QByteArrayView utf8); |
748 | Q_WEAK_OVERLOAD |
749 | static inline QString fromUtf8(const QByteArray &ba) { return fromUtf8(QByteArrayView(ba)); } |
750 | static inline QString fromUtf8(const char *utf8, qsizetype size) |
751 | { |
752 | return fromUtf8(QByteArrayView(utf8, !utf8 || size < 0 ? qstrlen(utf8) : size)); |
753 | } |
754 | #if defined(__cpp_char8_t) || defined(Q_CLANG_QDOC) |
755 | Q_WEAK_OVERLOAD |
756 | static inline QString fromUtf8(const char8_t *str) |
757 | { return fromUtf8(reinterpret_cast<const char *>(str)); } |
758 | Q_WEAK_OVERLOAD |
759 | static inline QString fromUtf8(const char8_t *str, qsizetype size) |
760 | { return fromUtf8(reinterpret_cast<const char *>(str), size); } |
761 | #endif |
762 | static QString fromLocal8Bit(QByteArrayView ba); |
763 | Q_WEAK_OVERLOAD |
764 | static inline QString fromLocal8Bit(const QByteArray &ba) { return fromLocal8Bit(QByteArrayView(ba)); } |
765 | static inline QString fromLocal8Bit(const char *str, qsizetype size) |
766 | { |
767 | return fromLocal8Bit(QByteArrayView(str, !str || size < 0 ? qstrlen(str) : size)); |
768 | } |
769 | static QString fromUtf16(const char16_t *, qsizetype size = -1); |
770 | static QString fromUcs4(const char32_t *, qsizetype size = -1); |
771 | static QString fromRawData(const QChar *, qsizetype size); |
772 | |
773 | #if QT_DEPRECATED_SINCE(6, 0) |
774 | QT_DEPRECATED_VERSION_X_6_0("Use char16_t* overload." ) |
775 | static QString fromUtf16(const ushort *str, qsizetype size = -1) |
776 | { return fromUtf16(reinterpret_cast<const char16_t *>(str), size); } |
777 | QT_DEPRECATED_VERSION_X_6_0("Use char32_t* overload." ) |
778 | static QString fromUcs4(const uint *str, qsizetype size = -1) |
779 | { return fromUcs4(reinterpret_cast<const char32_t *>(str), size); } |
780 | #endif |
781 | |
782 | inline qsizetype toWCharArray(wchar_t *array) const; |
783 | [[nodiscard]] static inline QString fromWCharArray(const wchar_t *string, qsizetype size = -1); |
784 | |
785 | QString &setRawData(const QChar *unicode, qsizetype size); |
786 | QString &setUnicode(const QChar *unicode, qsizetype size); |
787 | inline QString &setUtf16(const ushort *utf16, qsizetype size); // ### Qt 7 char16_t |
788 | |
789 | #if QT_STRINGVIEW_LEVEL < 2 |
790 | int compare(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; |
791 | #endif |
792 | int compare(QLatin1String other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; |
793 | inline int compare(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; |
794 | int compare(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept |
795 | { return compare(QStringView{&ch, 1}, cs); } |
796 | |
797 | static inline int compare(const QString &s1, const QString &s2, |
798 | Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept |
799 | { return s1.compare(s2, cs); } |
800 | |
801 | static inline int compare(const QString &s1, QLatin1String s2, |
802 | Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept |
803 | { return s1.compare(s2, cs); } |
804 | static inline int compare(QLatin1String s1, const QString &s2, |
805 | Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept |
806 | { return -s2.compare(s1, cs); } |
807 | static int compare(const QString &s1, QStringView s2, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept |
808 | { return s1.compare(s2, cs); } |
809 | static int compare(QStringView s1, const QString &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept |
810 | { return -s2.compare(s1, cs); } |
811 | |
812 | int localeAwareCompare(const QString& s) const; |
813 | int localeAwareCompare(QStringView s) const; |
814 | static int localeAwareCompare(const QString& s1, const QString& s2) |
815 | { return s1.localeAwareCompare(s2); } |
816 | |
817 | static int localeAwareCompare(QStringView s1, QStringView s2); |
818 | |
819 | short toShort(bool *ok=nullptr, int base=10) const |
820 | { return toIntegral_helper<short>(*this, ok, base); } |
821 | ushort toUShort(bool *ok=nullptr, int base=10) const |
822 | { return toIntegral_helper<ushort>(*this, ok, base); } |
823 | int toInt(bool *ok=nullptr, int base=10) const |
824 | { return toIntegral_helper<int>(*this, ok, base); } |
825 | uint toUInt(bool *ok=nullptr, int base=10) const |
826 | { return toIntegral_helper<uint>(*this, ok, base); } |
827 | long toLong(bool *ok=nullptr, int base=10) const |
828 | { return toIntegral_helper<long>(*this, ok, base); } |
829 | ulong toULong(bool *ok=nullptr, int base=10) const |
830 | { return toIntegral_helper<ulong>(*this, ok, base); } |
831 | qlonglong toLongLong(bool *ok=nullptr, int base=10) const; |
832 | qulonglong toULongLong(bool *ok=nullptr, int base=10) const; |
833 | float toFloat(bool *ok=nullptr) const; |
834 | double toDouble(bool *ok=nullptr) const; |
835 | |
836 | QString &setNum(short, int base=10); |
837 | QString &setNum(ushort, int base=10); |
838 | QString &setNum(int, int base=10); |
839 | QString &setNum(uint, int base=10); |
840 | QString &setNum(long, int base=10); |
841 | QString &setNum(ulong, int base=10); |
842 | QString &setNum(qlonglong, int base=10); |
843 | QString &setNum(qulonglong, int base=10); |
844 | QString &setNum(float, char f='g', int prec=6); |
845 | QString &setNum(double, char f='g', int prec=6); |
846 | |
847 | static QString number(int, int base=10); |
848 | static QString number(uint, int base=10); |
849 | static QString number(long, int base=10); |
850 | static QString number(ulong, int base=10); |
851 | static QString number(qlonglong, int base=10); |
852 | static QString number(qulonglong, int base=10); |
853 | static QString number(double, char f='g', int prec=6); |
854 | |
855 | friend bool operator==(const QString &s1, const QString &s2) noexcept |
856 | { return (s1.size() == s2.size()) && QtPrivate::compareStrings(s1, s2, Qt::CaseSensitive) == 0; } |
857 | friend bool operator< (const QString &s1, const QString &s2) noexcept |
858 | { return QtPrivate::compareStrings(s1, s2, Qt::CaseSensitive) < 0; } |
859 | friend bool operator> (const QString &s1, const QString &s2) noexcept { return s2 < s1; } |
860 | friend bool operator!=(const QString &s1, const QString &s2) noexcept { return !(s1 == s2); } |
861 | friend bool operator<=(const QString &s1, const QString &s2) noexcept { return !(s1 > s2); } |
862 | friend bool operator>=(const QString &s1, const QString &s2) noexcept { return !(s1 < s2); } |
863 | |
864 | friend bool operator==(const QString &s1, QLatin1String s2) noexcept |
865 | { return (s1.size() == s2.size()) && QtPrivate::compareStrings(s1, s2, Qt::CaseSensitive) == 0; } |
866 | friend bool operator< (const QString &s1, QLatin1String s2) noexcept |
867 | { return QtPrivate::compareStrings(s1, s2, Qt::CaseSensitive) < 0; } |
868 | friend bool operator> (const QString &s1, QLatin1String s2) noexcept |
869 | { return QtPrivate::compareStrings(s1, s2, Qt::CaseSensitive) > 0; } |
870 | friend bool operator!=(const QString &s1, QLatin1String s2) noexcept { return !(s1 == s2); } |
871 | friend bool operator<=(const QString &s1, QLatin1String s2) noexcept { return !(s1 > s2); } |
872 | friend bool operator>=(const QString &s1, QLatin1String s2) noexcept { return !(s1 < s2); } |
873 | |
874 | friend bool operator==(QLatin1String s1, const QString &s2) noexcept { return s2 == s1; } |
875 | friend bool operator< (QLatin1String s1, const QString &s2) noexcept { return s2 > s1; } |
876 | friend bool operator> (QLatin1String s1, const QString &s2) noexcept { return s2 < s1; } |
877 | friend bool operator!=(QLatin1String s1, const QString &s2) noexcept { return s2 != s1; } |
878 | friend bool operator<=(QLatin1String s1, const QString &s2) noexcept { return s2 >= s1; } |
879 | friend bool operator>=(QLatin1String s1, const QString &s2) noexcept { return s2 <= s1; } |
880 | |
881 | // Check isEmpty() instead of isNull() for backwards compatibility. |
882 | friend bool operator==(const QString &s1, std::nullptr_t) noexcept { return s1.isEmpty(); } |
883 | friend bool operator!=(const QString &s1, std::nullptr_t) noexcept { return !s1.isEmpty(); } |
884 | friend bool operator< (const QString & , std::nullptr_t) noexcept { return false; } |
885 | friend bool operator> (const QString &s1, std::nullptr_t) noexcept { return !s1.isEmpty(); } |
886 | friend bool operator<=(const QString &s1, std::nullptr_t) noexcept { return s1.isEmpty(); } |
887 | friend bool operator>=(const QString & , std::nullptr_t) noexcept { return true; } |
888 | friend bool operator==(std::nullptr_t, const QString &s2) noexcept { return s2 == nullptr; } |
889 | friend bool operator!=(std::nullptr_t, const QString &s2) noexcept { return s2 != nullptr; } |
890 | friend bool operator< (std::nullptr_t, const QString &s2) noexcept { return s2 > nullptr; } |
891 | friend bool operator> (std::nullptr_t, const QString &s2) noexcept { return s2 < nullptr; } |
892 | friend bool operator<=(std::nullptr_t, const QString &s2) noexcept { return s2 >= nullptr; } |
893 | friend bool operator>=(std::nullptr_t, const QString &s2) noexcept { return s2 <= nullptr; } |
894 | |
895 | friend bool operator==(const QString &s1, const char16_t *s2) noexcept { return s1 == QStringView(s2); } |
896 | friend bool operator!=(const QString &s1, const char16_t *s2) noexcept { return s1 != QStringView(s2); } |
897 | friend bool operator< (const QString &s1, const char16_t *s2) noexcept { return s1 < QStringView(s2); } |
898 | friend bool operator> (const QString &s1, const char16_t *s2) noexcept { return s1 > QStringView(s2); } |
899 | friend bool operator<=(const QString &s1, const char16_t *s2) noexcept { return s1 <= QStringView(s2); } |
900 | friend bool operator>=(const QString &s1, const char16_t *s2) noexcept { return s1 >= QStringView(s2); } |
901 | |
902 | friend bool operator==(const char16_t *s1, const QString &s2) noexcept { return s2 == s1; } |
903 | friend bool operator!=(const char16_t *s1, const QString &s2) noexcept { return s2 != s1; } |
904 | friend bool operator< (const char16_t *s1, const QString &s2) noexcept { return s2 > s1; } |
905 | friend bool operator> (const char16_t *s1, const QString &s2) noexcept { return s2 < s1; } |
906 | friend bool operator<=(const char16_t *s1, const QString &s2) noexcept { return s2 >= s1; } |
907 | friend bool operator>=(const char16_t *s1, const QString &s2) noexcept { return s2 <= s1; } |
908 | |
909 | // QChar <> QString |
910 | friend inline bool operator==(QChar lhs, const QString &rhs) noexcept |
911 | { return rhs.size() == 1 && lhs == rhs.front(); } |
912 | friend inline bool operator< (QChar lhs, const QString &rhs) noexcept |
913 | { return compare_helper(&lhs, 1, rhs.data(), rhs.size()) < 0; } |
914 | friend inline bool operator> (QChar lhs, const QString &rhs) noexcept |
915 | { return compare_helper(&lhs, 1, rhs.data(), rhs.size()) > 0; } |
916 | |
917 | friend inline bool operator!=(QChar lhs, const QString &rhs) noexcept { return !(lhs == rhs); } |
918 | friend inline bool operator<=(QChar lhs, const QString &rhs) noexcept { return !(lhs > rhs); } |
919 | friend inline bool operator>=(QChar lhs, const QString &rhs) noexcept { return !(lhs < rhs); } |
920 | |
921 | friend inline bool operator==(const QString &lhs, QChar rhs) noexcept { return rhs == lhs; } |
922 | friend inline bool operator!=(const QString &lhs, QChar rhs) noexcept { return !(rhs == lhs); } |
923 | friend inline bool operator< (const QString &lhs, QChar rhs) noexcept { return rhs > lhs; } |
924 | friend inline bool operator> (const QString &lhs, QChar rhs) noexcept { return rhs < lhs; } |
925 | friend inline bool operator<=(const QString &lhs, QChar rhs) noexcept { return !(rhs < lhs); } |
926 | friend inline bool operator>=(const QString &lhs, QChar rhs) noexcept { return !(rhs > lhs); } |
927 | |
928 | // ASCII compatibility |
929 | #if defined(QT_RESTRICTED_CAST_FROM_ASCII) |
930 | template <qsizetype N> |
931 | inline QString(const char (&ch)[N]) |
932 | : QString(fromUtf8(ch)) |
933 | {} |
934 | template <qsizetype N> |
935 | QString(char (&)[N]) = delete; |
936 | template <qsizetype N> |
937 | inline QString &operator=(const char (&ch)[N]) |
938 | { return (*this = fromUtf8(ch, N - 1)); } |
939 | template <qsizetype N> |
940 | QString &operator=(char (&)[N]) = delete; |
941 | #endif |
942 | #if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) |
943 | QT_ASCII_CAST_WARN inline QString(const char *ch) |
944 | : QString(fromUtf8(ch)) |
945 | {} |
946 | QT_ASCII_CAST_WARN inline QString(const QByteArray &a) |
947 | : QString(fromUtf8(a)) |
948 | {} |
949 | QT_ASCII_CAST_WARN inline QString &operator=(const char *ch) |
950 | { return (*this = fromUtf8(ch)); } |
951 | QT_ASCII_CAST_WARN inline QString &operator=(const QByteArray &a) |
952 | { return (*this = fromUtf8(a)); } |
953 | |
954 | // these are needed, so it compiles with STL support enabled |
955 | QT_ASCII_CAST_WARN inline QString &prepend(const char *s) |
956 | { return prepend(QString::fromUtf8(s)); } |
957 | QT_ASCII_CAST_WARN inline QString &prepend(const QByteArray &s) |
958 | { return prepend(QString::fromUtf8(s)); } |
959 | QT_ASCII_CAST_WARN inline QString &append(const char *s) |
960 | { return append(QString::fromUtf8(s)); } |
961 | QT_ASCII_CAST_WARN inline QString &append(const QByteArray &s) |
962 | { return append(QString::fromUtf8(s)); } |
963 | QT_ASCII_CAST_WARN inline QString &insert(qsizetype i, const char *s) |
964 | { return insert(i, QString::fromUtf8(s)); } |
965 | QT_ASCII_CAST_WARN inline QString &insert(qsizetype i, const QByteArray &s) |
966 | { return insert(i, QString::fromUtf8(s)); } |
967 | QT_ASCII_CAST_WARN inline QString &operator+=(const char *s) |
968 | { return append(QString::fromUtf8(s)); } |
969 | QT_ASCII_CAST_WARN inline QString &operator+=(const QByteArray &s) |
970 | { return append(QString::fromUtf8(s)); } |
971 | |
972 | QT_ASCII_CAST_WARN inline bool operator==(const char *s) const; |
973 | QT_ASCII_CAST_WARN inline bool operator!=(const char *s) const; |
974 | QT_ASCII_CAST_WARN inline bool operator<(const char *s) const; |
975 | QT_ASCII_CAST_WARN inline bool operator<=(const char *s) const; |
976 | QT_ASCII_CAST_WARN inline bool operator>(const char *s) const; |
977 | QT_ASCII_CAST_WARN inline bool operator>=(const char *s) const; |
978 | |
979 | QT_ASCII_CAST_WARN inline bool operator==(const QByteArray &s) const; |
980 | QT_ASCII_CAST_WARN inline bool operator!=(const QByteArray &s) const; |
981 | QT_ASCII_CAST_WARN inline bool operator<(const QByteArray &s) const; |
982 | QT_ASCII_CAST_WARN inline bool operator>(const QByteArray &s) const; |
983 | QT_ASCII_CAST_WARN inline bool operator<=(const QByteArray &s) const; |
984 | QT_ASCII_CAST_WARN inline bool operator>=(const QByteArray &s) const; |
985 | |
986 | QT_ASCII_CAST_WARN friend bool operator==(const char *s1, const QString &s2) |
987 | { return QString::compare_helper(s2.constData(), s2.size(), s1, -1) == 0; } |
988 | QT_ASCII_CAST_WARN friend bool operator!=(const char *s1, const QString &s2) |
989 | { return QString::compare_helper(s2.constData(), s2.size(), s1, -1) != 0; } |
990 | QT_ASCII_CAST_WARN friend bool operator< (const char *s1, const QString &s2) |
991 | { return QString::compare_helper(s2.constData(), s2.size(), s1, -1) > 0; } |
992 | QT_ASCII_CAST_WARN friend bool operator> (const char *s1, const QString &s2) |
993 | { return QString::compare_helper(s2.constData(), s2.size(), s1, -1) < 0; } |
994 | QT_ASCII_CAST_WARN friend bool operator<=(const char *s1, const QString &s2) |
995 | { return QString::compare_helper(s2.constData(), s2.size(), s1, -1) >= 0; } |
996 | QT_ASCII_CAST_WARN friend bool operator>=(const char *s1, const QString &s2) |
997 | { return QString::compare_helper(s2.constData(), s2.size(), s1, -1) <= 0; } |
998 | #endif |
999 | |
1000 | typedef QChar *iterator; |
1001 | typedef const QChar *const_iterator; |
1002 | typedef iterator Iterator; |
1003 | typedef const_iterator ConstIterator; |
1004 | typedef std::reverse_iterator<iterator> reverse_iterator; |
1005 | typedef std::reverse_iterator<const_iterator> const_reverse_iterator; |
1006 | inline iterator begin(); |
1007 | inline const_iterator begin() const; |
1008 | inline const_iterator cbegin() const; |
1009 | inline const_iterator constBegin() const; |
1010 | inline iterator end(); |
1011 | inline const_iterator end() const; |
1012 | inline const_iterator cend() const; |
1013 | inline const_iterator constEnd() const; |
1014 | reverse_iterator rbegin() { return reverse_iterator(end()); } |
1015 | reverse_iterator rend() { return reverse_iterator(begin()); } |
1016 | const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); } |
1017 | const_reverse_iterator rend() const { return const_reverse_iterator(begin()); } |
1018 | const_reverse_iterator crbegin() const { return const_reverse_iterator(end()); } |
1019 | const_reverse_iterator crend() const { return const_reverse_iterator(begin()); } |
1020 | |
1021 | // STL compatibility |
1022 | typedef qsizetype size_type; |
1023 | typedef qptrdiff difference_type; |
1024 | typedef const QChar & const_reference; |
1025 | typedef QChar & reference; |
1026 | typedef QChar *pointer; |
1027 | typedef const QChar *const_pointer; |
1028 | typedef QChar value_type; |
1029 | inline void push_back(QChar c) { append(c); } |
1030 | inline void push_back(const QString &s) { append(s); } |
1031 | inline void push_front(QChar c) { prepend(c); } |
1032 | inline void push_front(const QString &s) { prepend(s); } |
1033 | void shrink_to_fit() { squeeze(); } |
1034 | iterator erase(const_iterator first, const_iterator last); |
1035 | |
1036 | static inline QString fromStdString(const std::string &s); |
1037 | inline std::string toStdString() const; |
1038 | static inline QString fromStdWString(const std::wstring &s); |
1039 | inline std::wstring toStdWString() const; |
1040 | |
1041 | static inline QString fromStdU16String(const std::u16string &s); |
1042 | inline std::u16string toStdU16String() const; |
1043 | static inline QString fromStdU32String(const std::u32string &s); |
1044 | inline std::u32string toStdU32String() const; |
1045 | |
1046 | #if defined(Q_OS_DARWIN) || defined(Q_QDOC) |
1047 | static QString fromCFString(CFStringRef string); |
1048 | CFStringRef toCFString() const Q_DECL_CF_RETURNS_RETAINED; |
1049 | static QString fromNSString(const NSString *string); |
1050 | NSString *toNSString() const Q_DECL_NS_RETURNS_AUTORELEASED; |
1051 | #endif |
1052 | |
1053 | inline bool isNull() const { return d->isNull(); } |
1054 | |
1055 | |
1056 | bool isSimpleText() const; |
1057 | bool isRightToLeft() const; |
1058 | [[nodiscard]] bool isValidUtf16() const noexcept |
1059 | { return QStringView(*this).isValidUtf16(); } |
1060 | |
1061 | QString(qsizetype size, Qt::Initialization); |
1062 | explicit QString(DataPointer &&dd) : d(std::move(dd)) {} |
1063 | |
1064 | private: |
1065 | #if defined(QT_NO_CAST_FROM_ASCII) |
1066 | QString &operator+=(const char *s); |
1067 | QString &operator+=(const QByteArray &s); |
1068 | QString(const char *ch); |
1069 | QString(const QByteArray &a); |
1070 | QString &operator=(const char *ch); |
1071 | QString &operator=(const QByteArray &a); |
1072 | #endif |
1073 | |
1074 | DataPointer d; |
1075 | static const char16_t _empty; |
1076 | |
1077 | void reallocData(qsizetype alloc, QArrayData::AllocationOption option); |
1078 | void reallocGrowData(qsizetype n); |
1079 | static int compare_helper(const QChar *data1, qsizetype length1, |
1080 | const QChar *data2, qsizetype length2, |
1081 | Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept; |
1082 | static int compare_helper(const QChar *data1, qsizetype length1, |
1083 | const char *data2, qsizetype length2, |
1084 | Qt::CaseSensitivity cs = Qt::CaseSensitive); |
1085 | static int localeAwareCompare_helper(const QChar *data1, qsizetype length1, |
1086 | const QChar *data2, qsizetype length2); |
1087 | static QString toLower_helper(const QString &str); |
1088 | static QString toLower_helper(QString &str); |
1089 | static QString toUpper_helper(const QString &str); |
1090 | static QString toUpper_helper(QString &str); |
1091 | static QString toCaseFolded_helper(const QString &str); |
1092 | static QString toCaseFolded_helper(QString &str); |
1093 | static QString trimmed_helper(const QString &str); |
1094 | static QString trimmed_helper(QString &str); |
1095 | static QString simplified_helper(const QString &str); |
1096 | static QString simplified_helper(QString &str); |
1097 | static QByteArray toLatin1_helper(const QString &); |
1098 | static QByteArray toLatin1_helper_inplace(QString &); |
1099 | static QByteArray toUtf8_helper(const QString &); |
1100 | static QByteArray toLocal8Bit_helper(const QChar *data, qsizetype size); |
1101 | static qsizetype toUcs4_helper(const ushort *uc, qsizetype length, uint *out); // ### Qt 7 char16_t |
1102 | static qlonglong toIntegral_helper(QStringView string, bool *ok, int base); |
1103 | static qulonglong toIntegral_helper(QStringView string, bool *ok, uint base); |
1104 | void replace_helper(size_t *indices, qsizetype nIndices, qsizetype blen, const QChar *after, qsizetype alen); |
1105 | friend class QStringView; |
1106 | friend class QByteArray; |
1107 | friend class QCollator; |
1108 | friend struct QAbstractConcatenable; |
1109 | |
1110 | template <typename T> static |
1111 | T toIntegral_helper(QStringView string, bool *ok, int base) |
1112 | { |
1113 | using Int64 = typename std::conditional<std::is_unsigned<T>::value, qulonglong, qlonglong>::type; |
1114 | using Int32 = typename std::conditional<std::is_unsigned<T>::value, uint, int>::type; |
1115 | |
1116 | // we select the right overload by casting base to int or uint |
1117 | Int64 val = toIntegral_helper(string, ok, Int32(base)); |
1118 | if (T(val) != val) { |
1119 | if (ok) |
1120 | *ok = false; |
1121 | val = 0; |
1122 | } |
1123 | return T(val); |
1124 | } |
1125 | |
1126 | public: |
1127 | inline DataPointer &data_ptr() { return d; } |
1128 | inline const DataPointer &data_ptr() const { return d; } |
1129 | }; |
1130 | |
1131 | // |
1132 | // QLatin1String inline members that require QString: |
1133 | // |
1134 | |
1135 | QString QLatin1String::toString() const { return *this; } |
1136 | |
1137 | // |
1138 | // QStringView inline members that require QString: |
1139 | // |
1140 | |
1141 | QString QStringView::toString() const |
1142 | { return Q_ASSERT(size() == length()), QString(data(), length()); } |
1143 | |
1144 | qint64 QStringView::toLongLong(bool *ok, int base) const |
1145 | { return QString::toIntegral_helper<qint64>(*this, ok, base); } |
1146 | quint64 QStringView::toULongLong(bool *ok, int base) const |
1147 | { return QString::toIntegral_helper<quint64>(*this, ok, base); } |
1148 | long QStringView::toLong(bool *ok, int base) const |
1149 | { return QString::toIntegral_helper<long>(*this, ok, base); } |
1150 | ulong QStringView::toULong(bool *ok, int base) const |
1151 | { return QString::toIntegral_helper<ulong>(*this, ok, base); } |
1152 | int QStringView::toInt(bool *ok, int base) const |
1153 | { return QString::toIntegral_helper<int>(*this, ok, base); } |
1154 | uint QStringView::toUInt(bool *ok, int base) const |
1155 | { return QString::toIntegral_helper<uint>(*this, ok, base); } |
1156 | short QStringView::toShort(bool *ok, int base) const |
1157 | { return QString::toIntegral_helper<short>(*this, ok, base); } |
1158 | ushort QStringView::toUShort(bool *ok, int base) const |
1159 | { return QString::toIntegral_helper<ushort>(*this, ok, base); } |
1160 | |
1161 | // |
1162 | // QUtf8StringView inline members that require QString: |
1163 | // |
1164 | |
1165 | template <bool UseChar8T> |
1166 | QString QBasicUtf8StringView<UseChar8T>::toString() const |
1167 | { |
1168 | Q_ASSERT(size() == int(size())); |
1169 | return QString::fromUtf8(data(), int(size())); |
1170 | } |
1171 | |
1172 | // |
1173 | // QAnyStringView inline members that require QString: |
1174 | // |
1175 | |
1176 | QAnyStringView::QAnyStringView(const QByteArray &str) noexcept |
1177 | : QAnyStringView{str.isNull() ? nullptr : str.data(), str.size()} {} |
1178 | QAnyStringView::QAnyStringView(const QString &str) noexcept |
1179 | : QAnyStringView{str.isNull() ? nullptr : str.data(), str.size()} {} |
1180 | |
1181 | QString QAnyStringView::toString() const |
1182 | { return QtPrivate::convertToQString(*this); } |
1183 | |
1184 | // |
1185 | // QString inline members |
1186 | // |
1187 | inline QString::QString(QLatin1String latin1) |
1188 | { *this = QString::fromLatin1(latin1.data(), latin1.size()); } |
1189 | inline const QChar QString::at(qsizetype i) const |
1190 | { Q_ASSERT(size_t(i) < size_t(size())); return QChar(d.data()[i]); } |
1191 | inline const QChar QString::operator[](qsizetype i) const |
1192 | { Q_ASSERT(size_t(i) < size_t(size())); return QChar(d.data()[i]); } |
1193 | inline bool QString::isEmpty() const |
1194 | { return d.size == 0; } |
1195 | inline const QChar *QString::unicode() const |
1196 | { return data(); } |
1197 | inline const QChar *QString::data() const |
1198 | { |
1199 | #if QT5_NULL_STRINGS == 1 |
1200 | return reinterpret_cast<const QChar *>(d.data() ? d.data() : &_empty); |
1201 | #else |
1202 | return reinterpret_cast<const QChar *>(d.data()); |
1203 | #endif |
1204 | } |
1205 | inline QChar *QString::data() |
1206 | { |
1207 | detach(); |
1208 | Q_ASSERT(d.data()); |
1209 | return reinterpret_cast<QChar *>(d.data()); |
1210 | } |
1211 | inline const QChar *QString::constData() const |
1212 | { return data(); } |
1213 | inline void QString::detach() |
1214 | { if (d->needsDetach()) reallocData(d.size, QArrayData::KeepSize); } |
1215 | inline bool QString::isDetached() const |
1216 | { return !d->isShared(); } |
1217 | inline void QString::clear() |
1218 | { if (!isNull()) *this = QString(); } |
1219 | inline QString::QString(const QString &other) noexcept : d(other.d) |
1220 | { } |
1221 | inline qsizetype QString::capacity() const { return qsizetype(d->constAllocatedCapacity()); } |
1222 | inline QString &QString::setNum(short n, int base) |
1223 | { return setNum(qlonglong(n), base); } |
1224 | inline QString &QString::setNum(ushort n, int base) |
1225 | { return setNum(qulonglong(n), base); } |
1226 | inline QString &QString::setNum(int n, int base) |
1227 | { return setNum(qlonglong(n), base); } |
1228 | inline QString &QString::setNum(uint n, int base) |
1229 | { return setNum(qulonglong(n), base); } |
1230 | inline QString &QString::setNum(long n, int base) |
1231 | { return setNum(qlonglong(n), base); } |
1232 | inline QString &QString::setNum(ulong n, int base) |
1233 | { return setNum(qulonglong(n), base); } |
1234 | inline QString &QString::setNum(float n, char f, int prec) |
1235 | { return setNum(double(n),f,prec); } |
1236 | inline QString QString::arg(int a, int fieldWidth, int base, QChar fillChar) const |
1237 | { return arg(qlonglong(a), fieldWidth, base, fillChar); } |
1238 | inline QString QString::arg(uint a, int fieldWidth, int base, QChar fillChar) const |
1239 | { return arg(qulonglong(a), fieldWidth, base, fillChar); } |
1240 | inline QString QString::arg(long a, int fieldWidth, int base, QChar fillChar) const |
1241 | { return arg(qlonglong(a), fieldWidth, base, fillChar); } |
1242 | inline QString QString::arg(ulong a, int fieldWidth, int base, QChar fillChar) const |
1243 | { return arg(qulonglong(a), fieldWidth, base, fillChar); } |
1244 | inline QString QString::arg(short a, int fieldWidth, int base, QChar fillChar) const |
1245 | { return arg(qlonglong(a), fieldWidth, base, fillChar); } |
1246 | inline QString QString::arg(ushort a, int fieldWidth, int base, QChar fillChar) const |
1247 | { return arg(qulonglong(a), fieldWidth, base, fillChar); } |
1248 | |
1249 | inline QString QString::section(QChar asep, qsizetype astart, qsizetype aend, SectionFlags aflags) const |
1250 | { return section(QString(asep), astart, aend, aflags); } |
1251 | |
1252 | QT_WARNING_PUSH |
1253 | QT_WARNING_DISABLE_MSVC(4127) // "conditional expression is constant" |
1254 | QT_WARNING_DISABLE_INTEL(111) // "statement is unreachable" |
1255 | |
1256 | inline qsizetype QString::toWCharArray(wchar_t *array) const |
1257 | { |
1258 | return qToStringViewIgnoringNull(*this).toWCharArray(array); |
1259 | } |
1260 | |
1261 | qsizetype QStringView::toWCharArray(wchar_t *array) const |
1262 | { |
1263 | if (sizeof(wchar_t) == sizeof(QChar)) { |
1264 | if (auto src = data()) |
1265 | memcpy(array, src, sizeof(QChar) * size()); |
1266 | return size(); |
1267 | } else { |
1268 | return QString::toUcs4_helper(reinterpret_cast<const ushort *>(data()), size(), |
1269 | reinterpret_cast<uint *>(array)); |
1270 | } |
1271 | } |
1272 | |
1273 | QT_WARNING_POP |
1274 | |
1275 | inline QString QString::fromWCharArray(const wchar_t *string, qsizetype size) |
1276 | { |
1277 | return sizeof(wchar_t) == sizeof(QChar) ? fromUtf16(reinterpret_cast<const char16_t *>(string), size) |
1278 | : fromUcs4(reinterpret_cast<const char32_t *>(string), size); |
1279 | } |
1280 | |
1281 | inline constexpr QString::QString() noexcept {} |
1282 | inline QString::~QString() {} |
1283 | |
1284 | inline void QString::reserve(qsizetype asize) |
1285 | { |
1286 | if (d->needsDetach() || asize >= capacity() - d.freeSpaceAtBegin()) |
1287 | reallocData(qMax(asize, size()), QArrayData::KeepSize); |
1288 | if (d->constAllocatedCapacity()) |
1289 | d->setFlag(Data::CapacityReserved); |
1290 | } |
1291 | |
1292 | inline void QString::squeeze() |
1293 | { |
1294 | if (!d.isMutable()) |
1295 | return; |
1296 | if (d->needsDetach() || size() < capacity()) |
1297 | reallocData(d.size, QArrayData::KeepSize); |
1298 | if (d->constAllocatedCapacity()) |
1299 | d->clearFlag(Data::CapacityReserved); |
1300 | } |
1301 | |
1302 | inline QString &QString::setUtf16(const ushort *autf16, qsizetype asize) |
1303 | { return setUnicode(reinterpret_cast<const QChar *>(autf16), asize); } |
1304 | inline QChar &QString::operator[](qsizetype i) |
1305 | { Q_ASSERT(i >= 0 && i < size()); return data()[i]; } |
1306 | inline QChar &QString::front() { return operator[](0); } |
1307 | inline QChar &QString::back() { return operator[](size() - 1); } |
1308 | inline QString::iterator QString::begin() |
1309 | { detach(); return reinterpret_cast<QChar*>(d.data()); } |
1310 | inline QString::const_iterator QString::begin() const |
1311 | { return reinterpret_cast<const QChar*>(d.data()); } |
1312 | inline QString::const_iterator QString::cbegin() const |
1313 | { return reinterpret_cast<const QChar*>(d.data()); } |
1314 | inline QString::const_iterator QString::constBegin() const |
1315 | { return reinterpret_cast<const QChar*>(d.data()); } |
1316 | inline QString::iterator QString::end() |
1317 | { detach(); return reinterpret_cast<QChar*>(d.data() + d.size); } |
1318 | inline QString::const_iterator QString::end() const |
1319 | { return reinterpret_cast<const QChar*>(d.data() + d.size); } |
1320 | inline QString::const_iterator QString::cend() const |
1321 | { return reinterpret_cast<const QChar*>(d.data() + d.size); } |
1322 | inline QString::const_iterator QString::constEnd() const |
1323 | { return reinterpret_cast<const QChar*>(d.data() + d.size); } |
1324 | #if QT_STRINGVIEW_LEVEL < 2 |
1325 | inline bool QString::contains(const QString &s, Qt::CaseSensitivity cs) const |
1326 | { return indexOf(s, 0, cs) != -1; } |
1327 | #endif |
1328 | inline bool QString::contains(QLatin1String s, Qt::CaseSensitivity cs) const |
1329 | { return indexOf(s, 0, cs) != -1; } |
1330 | inline bool QString::contains(QChar c, Qt::CaseSensitivity cs) const |
1331 | { return indexOf(c, 0, cs) != -1; } |
1332 | inline bool QString::contains(QStringView s, Qt::CaseSensitivity cs) const noexcept |
1333 | { return indexOf(s, 0, cs) != -1; } |
1334 | |
1335 | #if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) |
1336 | inline bool QString::operator==(const char *s) const |
1337 | { return QString::compare_helper(constData(), size(), s, -1) == 0; } |
1338 | inline bool QString::operator!=(const char *s) const |
1339 | { return QString::compare_helper(constData(), size(), s, -1) != 0; } |
1340 | inline bool QString::operator<(const char *s) const |
1341 | { return QString::compare_helper(constData(), size(), s, -1) < 0; } |
1342 | inline bool QString::operator>(const char *s) const |
1343 | { return QString::compare_helper(constData(), size(), s, -1) > 0; } |
1344 | inline bool QString::operator<=(const char *s) const |
1345 | { return QString::compare_helper(constData(), size(), s, -1) <= 0; } |
1346 | inline bool QString::operator>=(const char *s) const |
1347 | { return QString::compare_helper(constData(), size(), s, -1) >= 0; } |
1348 | |
1349 | QT_ASCII_CAST_WARN inline bool QLatin1String::operator==(const char *s) const |
1350 | { return QString::fromUtf8(s) == *this; } |
1351 | QT_ASCII_CAST_WARN inline bool QLatin1String::operator!=(const char *s) const |
1352 | { return QString::fromUtf8(s) != *this; } |
1353 | QT_ASCII_CAST_WARN inline bool QLatin1String::operator<(const char *s) const |
1354 | { return QString::fromUtf8(s) > *this; } |
1355 | QT_ASCII_CAST_WARN inline bool QLatin1String::operator>(const char *s) const |
1356 | { return QString::fromUtf8(s) < *this; } |
1357 | QT_ASCII_CAST_WARN inline bool QLatin1String::operator<=(const char *s) const |
1358 | { return QString::fromUtf8(s) >= *this; } |
1359 | QT_ASCII_CAST_WARN inline bool QLatin1String::operator>=(const char *s) const |
1360 | { return QString::fromUtf8(s) <= *this; } |
1361 | |
1362 | QT_ASCII_CAST_WARN inline bool QLatin1String::operator==(const QByteArray &s) const |
1363 | { return QString::fromUtf8(s) == *this; } |
1364 | QT_ASCII_CAST_WARN inline bool QLatin1String::operator!=(const QByteArray &s) const |
1365 | { return QString::fromUtf8(s) != *this; } |
1366 | QT_ASCII_CAST_WARN inline bool QLatin1String::operator<(const QByteArray &s) const |
1367 | { return QString::fromUtf8(s) > *this; } |
1368 | QT_ASCII_CAST_WARN inline bool QLatin1String::operator>(const QByteArray &s) const |
1369 | { return QString::fromUtf8(s) < *this; } |
1370 | QT_ASCII_CAST_WARN inline bool QLatin1String::operator<=(const QByteArray &s) const |
1371 | { return QString::fromUtf8(s) >= *this; } |
1372 | QT_ASCII_CAST_WARN inline bool QLatin1String::operator>=(const QByteArray &s) const |
1373 | { return QString::fromUtf8(s) <= *this; } |
1374 | |
1375 | inline int QLatin1String::compare_helper(const QLatin1String &s1, const char *s2) |
1376 | { |
1377 | return QString::compare(s1, QString::fromUtf8(s2)); |
1378 | } |
1379 | |
1380 | QT_ASCII_CAST_WARN inline bool QString::operator==(const QByteArray &s) const |
1381 | { return QString::compare_helper(constData(), size(), s.constData(), s.size()) == 0; } |
1382 | QT_ASCII_CAST_WARN inline bool QString::operator!=(const QByteArray &s) const |
1383 | { return QString::compare_helper(constData(), size(), s.constData(), s.size()) != 0; } |
1384 | QT_ASCII_CAST_WARN inline bool QString::operator<(const QByteArray &s) const |
1385 | { return QString::compare_helper(constData(), size(), s.constData(), s.size()) < 0; } |
1386 | QT_ASCII_CAST_WARN inline bool QString::operator>(const QByteArray &s) const |
1387 | { return QString::compare_helper(constData(), size(), s.constData(), s.size()) > 0; } |
1388 | QT_ASCII_CAST_WARN inline bool QString::operator<=(const QByteArray &s) const |
1389 | { return QString::compare_helper(constData(), size(), s.constData(), s.size()) <= 0; } |
1390 | QT_ASCII_CAST_WARN inline bool QString::operator>=(const QByteArray &s) const |
1391 | { return QString::compare_helper(constData(), size(), s.constData(), s.size()) >= 0; } |
1392 | |
1393 | inline bool QByteArray::operator==(const QString &s) const |
1394 | { return QString::compare_helper(s.constData(), s.size(), constData(), qstrnlen(constData(), size())) == 0; } |
1395 | inline bool QByteArray::operator!=(const QString &s) const |
1396 | { return QString::compare_helper(s.constData(), s.size(), constData(), qstrnlen(constData(), size())) != 0; } |
1397 | inline bool QByteArray::operator<(const QString &s) const |
1398 | { return QString::compare_helper(s.constData(), s.size(), constData(), size()) > 0; } |
1399 | inline bool QByteArray::operator>(const QString &s) const |
1400 | { return QString::compare_helper(s.constData(), s.size(), constData(), size()) < 0; } |
1401 | inline bool QByteArray::operator<=(const QString &s) const |
1402 | { return QString::compare_helper(s.constData(), s.size(), constData(), size()) >= 0; } |
1403 | inline bool QByteArray::operator>=(const QString &s) const |
1404 | { return QString::compare_helper(s.constData(), s.size(), constData(), size()) <= 0; } |
1405 | #endif // !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) |
1406 | |
1407 | #if !defined(QT_USE_FAST_OPERATOR_PLUS) && !defined(QT_USE_QSTRINGBUILDER) |
1408 | inline const QString operator+(const QString &s1, const QString &s2) |
1409 | { QString t(s1); t += s2; return t; } |
1410 | inline const QString operator+(const QString &s1, QChar s2) |
1411 | { QString t(s1); t += s2; return t; } |
1412 | inline const QString operator+(QChar s1, const QString &s2) |
1413 | { QString t(s1); t += s2; return t; } |
1414 | # if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) |
1415 | QT_ASCII_CAST_WARN inline const QString operator+(const QString &s1, const char *s2) |
1416 | { QString t(s1); t += QString::fromUtf8(s2); return t; } |
1417 | QT_ASCII_CAST_WARN inline const QString operator+(const char *s1, const QString &s2) |
1418 | { QString t = QString::fromUtf8(s1); t += s2; return t; } |
1419 | QT_ASCII_CAST_WARN inline const QString operator+(const QByteArray &ba, const QString &s) |
1420 | { QString t = QString::fromUtf8(ba); t += s; return t; } |
1421 | QT_ASCII_CAST_WARN inline const QString operator+(const QString &s, const QByteArray &ba) |
1422 | { QString t(s); t += QString::fromUtf8(ba); return t; } |
1423 | # endif // QT_NO_CAST_FROM_ASCII |
1424 | #endif // QT_USE_QSTRINGBUILDER |
1425 | |
1426 | inline std::string QString::toStdString() const |
1427 | { return toUtf8().toStdString(); } |
1428 | |
1429 | inline QString QString::fromStdString(const std::string &s) |
1430 | { return fromUtf8(s.data(), int(s.size())); } |
1431 | |
1432 | inline std::wstring QString::toStdWString() const |
1433 | { |
1434 | std::wstring str; |
1435 | str.resize(length()); |
1436 | #if __cplusplus >= 201703L |
1437 | str.resize(toWCharArray(str.data())); |
1438 | #else |
1439 | if (length()) |
1440 | str.resize(toWCharArray(&str.front())); |
1441 | #endif |
1442 | return str; |
1443 | } |
1444 | |
1445 | inline QString QString::fromStdWString(const std::wstring &s) |
1446 | { return fromWCharArray(s.data(), int(s.size())); } |
1447 | |
1448 | inline QString QString::fromStdU16String(const std::u16string &s) |
1449 | { return fromUtf16(s.data(), int(s.size())); } |
1450 | |
1451 | inline std::u16string QString::toStdU16String() const |
1452 | { return std::u16string(reinterpret_cast<const char16_t*>(utf16()), length()); } |
1453 | |
1454 | inline QString QString::fromStdU32String(const std::u32string &s) |
1455 | { return fromUcs4(s.data(), int(s.size())); } |
1456 | |
1457 | inline std::u32string QString::toStdU32String() const |
1458 | { |
1459 | std::u32string u32str(length(), char32_t(0)); |
1460 | qsizetype len = toUcs4_helper(reinterpret_cast<const ushort *>(constData()), |
1461 | length(), reinterpret_cast<uint*>(&u32str[0])); |
1462 | u32str.resize(len); |
1463 | return u32str; |
1464 | } |
1465 | |
1466 | #if !defined(QT_NO_DATASTREAM) || defined(QT_BOOTSTRAPPED) |
1467 | Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QString &); |
1468 | Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QString &); |
1469 | #endif |
1470 | |
1471 | Q_DECLARE_SHARED(QString) |
1472 | Q_DECLARE_OPERATORS_FOR_FLAGS(QString::SectionFlags) |
1473 | |
1474 | inline int QString::compare(QStringView s, Qt::CaseSensitivity cs) const noexcept |
1475 | { return -s.compare(*this, cs); } |
1476 | |
1477 | inline int QString::localeAwareCompare(QStringView s) const |
1478 | { return localeAwareCompare_helper(constData(), length(), s.constData(), s.length()); } |
1479 | inline int QString::localeAwareCompare(QStringView s1, QStringView s2) |
1480 | { return localeAwareCompare_helper(s1.constData(), s1.length(), s2.constData(), s2.length()); } |
1481 | |
1482 | namespace QtPrivate { |
1483 | // used by qPrintable() and qUtf8Printable() macros |
1484 | inline const QString &asString(const QString &s) { return s; } |
1485 | inline QString &&asString(QString &&s) { return std::move(s); } |
1486 | } |
1487 | |
1488 | // |
1489 | // QStringView::arg() implementation |
1490 | // |
1491 | |
1492 | namespace QtPrivate { |
1493 | |
1494 | struct ArgBase { |
1495 | enum Tag : uchar { L1, U8, U16 } tag; |
1496 | }; |
1497 | |
1498 | struct QStringViewArg : ArgBase { |
1499 | QStringView string; |
1500 | QStringViewArg() = default; |
1501 | constexpr explicit QStringViewArg(QStringView v) noexcept : ArgBase{U16}, string{v} {} |
1502 | }; |
1503 | |
1504 | struct QLatin1StringArg : ArgBase { |
1505 | QLatin1String string; |
1506 | QLatin1StringArg() = default; |
1507 | constexpr explicit QLatin1StringArg(QLatin1String v) noexcept : ArgBase{L1}, string{v} {} |
1508 | }; |
1509 | |
1510 | [[nodiscard]] Q_CORE_EXPORT QString argToQString(QStringView pattern, size_t n, const ArgBase **args); |
1511 | [[nodiscard]] Q_CORE_EXPORT QString argToQString(QLatin1String pattern, size_t n, const ArgBase **args); |
1512 | |
1513 | template <typename StringView, typename...Args> |
1514 | [[nodiscard]] Q_ALWAYS_INLINE QString argToQStringDispatch(StringView pattern, const Args &...args) |
1515 | { |
1516 | const ArgBase *argBases[] = {&args..., /* avoid zero-sized array */ nullptr}; |
1517 | return QtPrivate::argToQString(pattern, sizeof...(Args), argBases); |
1518 | } |
1519 | |
1520 | inline QStringViewArg qStringLikeToArg(const QString &s) noexcept { return QStringViewArg{qToStringViewIgnoringNull(s)}; } |
1521 | constexpr inline QStringViewArg qStringLikeToArg(QStringView s) noexcept { return QStringViewArg{s}; } |
1522 | inline QStringViewArg qStringLikeToArg(const QChar &c) noexcept { return QStringViewArg{QStringView{&c, 1}}; } |
1523 | constexpr inline QLatin1StringArg qStringLikeToArg(QLatin1String s) noexcept { return QLatin1StringArg{s}; } |
1524 | |
1525 | } // namespace QtPrivate |
1526 | |
1527 | template <typename...Args> |
1528 | Q_ALWAYS_INLINE |
1529 | QString QStringView::arg(Args &&...args) const |
1530 | { |
1531 | return QtPrivate::argToQStringDispatch(*this, QtPrivate::qStringLikeToArg(args)...); |
1532 | } |
1533 | |
1534 | template <typename...Args> |
1535 | Q_ALWAYS_INLINE |
1536 | QString QLatin1String::arg(Args &&...args) const |
1537 | { |
1538 | return QtPrivate::argToQStringDispatch(*this, QtPrivate::qStringLikeToArg(args)...); |
1539 | } |
1540 | |
1541 | template <typename T> |
1542 | qsizetype erase(QString &s, const T &t) |
1543 | { |
1544 | return QtPrivate::sequential_erase(s, t); |
1545 | } |
1546 | |
1547 | template <typename Predicate> |
1548 | qsizetype erase_if(QString &s, Predicate pred) |
1549 | { |
1550 | return QtPrivate::sequential_erase_if(s, pred); |
1551 | } |
1552 | |
1553 | inline namespace QtLiterals { |
1554 | inline QString operator"" _qs(const char16_t *str, size_t size) noexcept |
1555 | { |
1556 | return QString(QStringPrivate(nullptr, const_cast<char16_t *>(str), qsizetype(size))); |
1557 | } |
1558 | } // QtLiterals |
1559 | |
1560 | QT_END_NAMESPACE |
1561 | |
1562 | #if defined(QT_USE_FAST_OPERATOR_PLUS) || defined(QT_USE_QSTRINGBUILDER) |
1563 | #include <QtCore/qstringbuilder.h> |
1564 | #endif |
1565 | |
1566 | #endif // QSTRING_H |
1567 | |