1 | // Copyright (C) 2021 The Qt Company Ltd. |
2 | // Copyright (C) 2022 Intel Corporation. |
3 | // Copyright (C) 2019 Mail.ru Group. |
4 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
5 | |
6 | #include "qstringlist.h" |
7 | #if QT_CONFIG(regularexpression) |
8 | #include "qregularexpression.h" |
9 | #endif |
10 | #include "qunicodetables_p.h" |
11 | #include <private/qstringconverter_p.h> |
12 | #include <private/qtools_p.h> |
13 | #include "qlocale_tools_p.h" |
14 | #include "private/qsimd_p.h" |
15 | #include <qnumeric.h> |
16 | #include <qdatastream.h> |
17 | #include <qlist.h> |
18 | #include "qlocale.h" |
19 | #include "qlocale_p.h" |
20 | #include "qstringbuilder.h" |
21 | #include "qstringmatcher.h" |
22 | #include "qvarlengtharray.h" |
23 | #include "qdebug.h" |
24 | #include "qendian.h" |
25 | #include "qcollator.h" |
26 | #include "qttypetraits.h" |
27 | |
28 | #ifdef Q_OS_DARWIN |
29 | #include <private/qcore_mac_p.h> |
30 | #endif |
31 | |
32 | #include <private/qfunctions_p.h> |
33 | |
34 | #include <limits.h> |
35 | #include <string.h> |
36 | #include <stdlib.h> |
37 | #include <stdio.h> |
38 | #include <stdarg.h> |
39 | #include <wchar.h> |
40 | |
41 | #include "qchar.cpp" |
42 | #include "qlatin1stringmatcher.h" |
43 | #include "qstringmatcher.cpp" |
44 | #include "qstringiterator_p.h" |
45 | #include "qstringalgorithms_p.h" |
46 | #include "qthreadstorage.h" |
47 | |
48 | #include <algorithm> |
49 | #include <functional> |
50 | |
51 | #ifdef Q_OS_WIN |
52 | # include <qt_windows.h> |
53 | #endif |
54 | |
55 | #ifdef truncate |
56 | # undef truncate |
57 | #endif |
58 | |
59 | #ifndef LLONG_MAX |
60 | #define LLONG_MAX qint64_C(9223372036854775807) |
61 | #endif |
62 | #ifndef LLONG_MIN |
63 | #define LLONG_MIN (-LLONG_MAX - qint64_C(1)) |
64 | #endif |
65 | #ifndef ULLONG_MAX |
66 | #define ULLONG_MAX quint64_C(18446744073709551615) |
67 | #endif |
68 | |
69 | #define REHASH(a) \ |
70 | if (sl_minus_1 < sizeof(std::size_t) * CHAR_BIT) \ |
71 | hashHaystack -= std::size_t(a) << sl_minus_1; \ |
72 | hashHaystack <<= 1 |
73 | |
74 | QT_BEGIN_NAMESPACE |
75 | |
76 | using namespace Qt::StringLiterals; |
77 | using namespace QtMiscUtils; |
78 | |
79 | const char16_t QString::_empty = 0; |
80 | |
81 | // in qstringmatcher.cpp |
82 | qsizetype qFindStringBoyerMoore(QStringView haystack, qsizetype from, QStringView needle, Qt::CaseSensitivity cs); |
83 | |
84 | namespace { |
85 | enum StringComparisonMode { |
86 | CompareStringsForEquality, |
87 | CompareStringsForOrdering |
88 | }; |
89 | |
90 | template <typename Pointer> |
91 | char32_t foldCaseHelper(Pointer ch, Pointer start) = delete; |
92 | |
93 | template <> |
94 | char32_t foldCaseHelper<const QChar*>(const QChar* ch, const QChar* start) |
95 | { |
96 | return foldCase(ch: reinterpret_cast<const char16_t*>(ch), |
97 | start: reinterpret_cast<const char16_t*>(start)); |
98 | } |
99 | |
100 | template <> |
101 | char32_t foldCaseHelper<const char*>(const char* ch, const char*) |
102 | { |
103 | return foldCase(ch: char16_t(uchar(*ch))); |
104 | } |
105 | |
106 | template <typename T> |
107 | char16_t valueTypeToUtf16(T t) = delete; |
108 | |
109 | template <> |
110 | char16_t valueTypeToUtf16<QChar>(QChar t) |
111 | { |
112 | return t.unicode(); |
113 | } |
114 | |
115 | template <> |
116 | char16_t valueTypeToUtf16<char>(char t) |
117 | { |
118 | return char16_t{uchar(t)}; |
119 | } |
120 | |
121 | template <typename T> |
122 | static inline bool foldAndCompare(const T a, const T b) |
123 | { |
124 | return foldCase(a) == b; |
125 | } |
126 | |
127 | /*! |
128 | \internal |
129 | |
130 | Returns the index position of the first occurrence of the |
131 | character \a ch in the string given by \a str and \a len, |
132 | searching forward from index |
133 | position \a from. Returns -1 if \a ch could not be found. |
134 | */ |
135 | static inline qsizetype qFindChar(QStringView str, QChar ch, qsizetype from, Qt::CaseSensitivity cs) noexcept |
136 | { |
137 | if (-from > str.size()) |
138 | return -1; |
139 | if (from < 0) |
140 | from = qMax(a: from + str.size(), b: qsizetype(0)); |
141 | if (from < str.size()) { |
142 | const char16_t *s = str.utf16(); |
143 | char16_t c = ch.unicode(); |
144 | const char16_t *n = s + from; |
145 | const char16_t *e = s + str.size(); |
146 | if (cs == Qt::CaseSensitive) { |
147 | n = QtPrivate::qustrchr(str: QStringView(n, e), ch: c); |
148 | if (n != e) |
149 | return n - s; |
150 | } else { |
151 | c = foldCase(ch: c); |
152 | auto it = std::find_if(first: n, last: e, pred: [c](const auto &ch) { return foldAndCompare(ch, c); }); |
153 | if (it != e) |
154 | return std::distance(first: s, last: it); |
155 | } |
156 | } |
157 | return -1; |
158 | } |
159 | |
160 | template <typename Haystack> |
161 | static inline qsizetype qLastIndexOf(Haystack haystack, QChar needle, |
162 | qsizetype from, Qt::CaseSensitivity cs) noexcept |
163 | { |
164 | if (haystack.size() == 0) |
165 | return -1; |
166 | if (from < 0) |
167 | from += haystack.size(); |
168 | else if (std::size_t(from) > std::size_t(haystack.size())) |
169 | from = haystack.size() - 1; |
170 | if (from >= 0) { |
171 | char16_t c = needle.unicode(); |
172 | const auto b = haystack.data(); |
173 | auto n = b + from; |
174 | if (cs == Qt::CaseSensitive) { |
175 | for (; n >= b; --n) |
176 | if (valueTypeToUtf16(*n) == c) |
177 | return n - b; |
178 | } else { |
179 | c = foldCase(ch: c); |
180 | for (; n >= b; --n) |
181 | if (foldCase(valueTypeToUtf16(*n)) == c) |
182 | return n - b; |
183 | } |
184 | } |
185 | return -1; |
186 | } |
187 | template <> qsizetype |
188 | qLastIndexOf(QString, QChar, qsizetype, Qt::CaseSensitivity) noexcept = delete; // unwanted, would detach |
189 | |
190 | template<typename Haystack, typename Needle> |
191 | static qsizetype qLastIndexOf(Haystack haystack0, qsizetype from, |
192 | Needle needle0, Qt::CaseSensitivity cs) noexcept |
193 | { |
194 | const qsizetype sl = needle0.size(); |
195 | if (sl == 1) |
196 | return qLastIndexOf(haystack0, needle0.front(), from, cs); |
197 | |
198 | const qsizetype l = haystack0.size(); |
199 | if (from < 0) |
200 | from += l; |
201 | if (from == l && sl == 0) |
202 | return from; |
203 | const qsizetype delta = l - sl; |
204 | if (std::size_t(from) > std::size_t(l) || delta < 0) |
205 | return -1; |
206 | if (from > delta) |
207 | from = delta; |
208 | |
209 | auto sv = [sl](const typename Haystack::value_type *v) { return Haystack(v, sl); }; |
210 | |
211 | auto haystack = haystack0.data(); |
212 | const auto needle = needle0.data(); |
213 | const auto *end = haystack; |
214 | haystack += from; |
215 | const std::size_t sl_minus_1 = sl ? sl - 1 : 0; |
216 | const auto *n = needle + sl_minus_1; |
217 | const auto *h = haystack + sl_minus_1; |
218 | std::size_t hashNeedle = 0, hashHaystack = 0; |
219 | |
220 | if (cs == Qt::CaseSensitive) { |
221 | for (qsizetype idx = 0; idx < sl; ++idx) { |
222 | hashNeedle = (hashNeedle << 1) + valueTypeToUtf16(*(n - idx)); |
223 | hashHaystack = (hashHaystack << 1) + valueTypeToUtf16(*(h - idx)); |
224 | } |
225 | hashHaystack -= valueTypeToUtf16(*haystack); |
226 | |
227 | while (haystack >= end) { |
228 | hashHaystack += valueTypeToUtf16(*haystack); |
229 | if (hashHaystack == hashNeedle |
230 | && QtPrivate::compareStrings(needle0, sv(haystack), Qt::CaseSensitive) == 0) |
231 | return haystack - end; |
232 | --haystack; |
233 | REHASH(valueTypeToUtf16(haystack[sl])); |
234 | } |
235 | } else { |
236 | for (qsizetype idx = 0; idx < sl; ++idx) { |
237 | hashNeedle = (hashNeedle << 1) + foldCaseHelper(n - idx, needle); |
238 | hashHaystack = (hashHaystack << 1) + foldCaseHelper(h - idx, end); |
239 | } |
240 | hashHaystack -= foldCaseHelper(haystack, end); |
241 | |
242 | while (haystack >= end) { |
243 | hashHaystack += foldCaseHelper(haystack, end); |
244 | if (hashHaystack == hashNeedle |
245 | && QtPrivate::compareStrings(sv(haystack), needle0, Qt::CaseInsensitive) == 0) |
246 | return haystack - end; |
247 | --haystack; |
248 | REHASH(foldCaseHelper(haystack + sl, end)); |
249 | } |
250 | } |
251 | return -1; |
252 | } |
253 | |
254 | template <typename Haystack, typename Needle> |
255 | bool qt_starts_with_impl(Haystack haystack, Needle needle, Qt::CaseSensitivity cs) noexcept |
256 | { |
257 | if (haystack.isNull()) |
258 | return needle.isNull(); |
259 | const auto haystackLen = haystack.size(); |
260 | const auto needleLen = needle.size(); |
261 | if (haystackLen == 0) |
262 | return needleLen == 0; |
263 | if (needleLen > haystackLen) |
264 | return false; |
265 | |
266 | return QtPrivate::compareStrings(haystack.left(needleLen), needle, cs) == 0; |
267 | } |
268 | |
269 | template <typename Haystack, typename Needle> |
270 | bool qt_ends_with_impl(Haystack haystack, Needle needle, Qt::CaseSensitivity cs) noexcept |
271 | { |
272 | if (haystack.isNull()) |
273 | return needle.isNull(); |
274 | const auto haystackLen = haystack.size(); |
275 | const auto needleLen = needle.size(); |
276 | if (haystackLen == 0) |
277 | return needleLen == 0; |
278 | if (haystackLen < needleLen) |
279 | return false; |
280 | |
281 | return QtPrivate::compareStrings(haystack.right(needleLen), needle, cs) == 0; |
282 | } |
283 | |
284 | template <typename T> |
285 | static void append_helper(QString &self, T view) |
286 | { |
287 | const auto strData = view.data(); |
288 | const qsizetype strSize = view.size(); |
289 | auto &d = self.data_ptr(); |
290 | if (strData && strSize > 0) { |
291 | // the number of UTF-8 code units is always at a minimum equal to the number |
292 | // of equivalent UTF-16 code units |
293 | d.detachAndGrow(where: QArrayData::GrowsAtEnd, n: strSize, data: nullptr, old: nullptr); |
294 | Q_CHECK_PTR(d.data()); |
295 | Q_ASSERT(strSize <= d.freeSpaceAtEnd()); |
296 | |
297 | auto dst = std::next(x: d.data(), n: d.size); |
298 | if constexpr (std::is_same_v<T, QUtf8StringView>) { |
299 | dst = QUtf8::convertToUnicode(dst, view); |
300 | } else if constexpr (std::is_same_v<T, QLatin1StringView>) { |
301 | QLatin1::convertToUnicode(dst, view); |
302 | dst += strSize; |
303 | } else { |
304 | static_assert(QtPrivate::type_dependent_false<T>(), |
305 | "Can only operate on UTF-8 and Latin-1" ); |
306 | } |
307 | self.resize(size: std::distance(first: d.begin(), last: dst)); |
308 | } else if (d.isNull() && !view.isNull()) { // special case |
309 | self = QLatin1StringView("" ); |
310 | } |
311 | } |
312 | |
313 | template <uint MaxCount> struct UnrollTailLoop |
314 | { |
315 | template <typename RetType, typename Functor1, typename Functor2, typename Number> |
316 | static inline RetType exec(Number count, RetType returnIfExited, Functor1 loopCheck, Functor2 returnIfFailed, Number i = 0) |
317 | { |
318 | /* equivalent to: |
319 | * while (count--) { |
320 | * if (loopCheck(i)) |
321 | * return returnIfFailed(i); |
322 | * } |
323 | * return returnIfExited; |
324 | */ |
325 | |
326 | if (!count) |
327 | return returnIfExited; |
328 | |
329 | bool check = loopCheck(i); |
330 | if (check) |
331 | return returnIfFailed(i); |
332 | |
333 | return UnrollTailLoop<MaxCount - 1>::exec(count - 1, returnIfExited, loopCheck, returnIfFailed, i + 1); |
334 | } |
335 | |
336 | template <typename Functor, typename Number> |
337 | static inline void exec(Number count, Functor code) |
338 | { |
339 | /* equivalent to: |
340 | * for (Number i = 0; i < count; ++i) |
341 | * code(i); |
342 | */ |
343 | exec(count, 0, [=](Number i) -> bool { code(i); return false; }, [](Number) { return 0; }); |
344 | } |
345 | }; |
346 | template <> template <typename RetType, typename Functor1, typename Functor2, typename Number> |
347 | inline RetType UnrollTailLoop<0>::exec(Number, RetType returnIfExited, Functor1, Functor2, Number) |
348 | { |
349 | return returnIfExited; |
350 | } |
351 | } // unnamed namespace |
352 | |
353 | /* |
354 | * Note on the use of SIMD in qstring.cpp: |
355 | * |
356 | * Several operations with strings are improved with the use of SIMD code, |
357 | * since they are repetitive. For MIPS, we have hand-written assembly code |
358 | * outside of qstring.cpp targeting MIPS DSP and MIPS DSPr2. For ARM and for |
359 | * x86, we can only use intrinsics and therefore everything is contained in |
360 | * qstring.cpp. We need to use intrinsics only for those platforms due to the |
361 | * different compilers and toolchains used, which have different syntax for |
362 | * assembly sources. |
363 | * |
364 | * ** SSE notes: ** |
365 | * |
366 | * Whenever multiple alternatives are equivalent or near so, we prefer the one |
367 | * using instructions from SSE2, since SSE2 is guaranteed to be enabled for all |
368 | * 64-bit builds and we enable it for 32-bit builds by default. Use of higher |
369 | * SSE versions should be done when there is a clear performance benefit and |
370 | * requires fallback code to SSE2, if it exists. |
371 | * |
372 | * Performance measurement in the past shows that most strings are short in |
373 | * size and, therefore, do not benefit from alignment prologues. That is, |
374 | * trying to find a 16-byte-aligned boundary to operate on is often more |
375 | * expensive than executing the unaligned operation directly. In addition, note |
376 | * that the QString private data is designed so that the data is stored on |
377 | * 16-byte boundaries if the system malloc() returns 16-byte aligned pointers |
378 | * on its own (64-bit glibc on Linux does; 32-bit glibc on Linux returns them |
379 | * 50% of the time), so skipping the alignment prologue is actually optimizing |
380 | * for the common case. |
381 | */ |
382 | |
383 | #if defined(__mips_dsp) |
384 | // From qstring_mips_dsp_asm.S |
385 | extern "C" void qt_fromlatin1_mips_asm_unroll4 (char16_t*, const char*, uint); |
386 | extern "C" void qt_fromlatin1_mips_asm_unroll8 (char16_t*, const char*, uint); |
387 | extern "C" void qt_toLatin1_mips_dsp_asm(uchar *dst, const char16_t *src, int length); |
388 | #endif |
389 | |
390 | #if defined(__SSE2__) && defined(Q_CC_GNU) |
391 | // We may overrun the buffer, but that's a false positive: |
392 | // this won't crash nor produce incorrect results |
393 | # define ATTRIBUTE_NO_SANITIZE __attribute__((__no_sanitize_address__)) |
394 | #else |
395 | # define ATTRIBUTE_NO_SANITIZE |
396 | #endif |
397 | |
398 | #ifdef __SSE2__ |
399 | static constexpr bool UseSse4_1 = bool(qCompilerCpuFeatures & CpuFeatureSSE4_1); |
400 | static constexpr bool UseAvx2 = UseSse4_1 && |
401 | (qCompilerCpuFeatures & CpuFeatureArchHaswell) == CpuFeatureArchHaswell; |
402 | |
403 | [[maybe_unused]] |
404 | static Q_ALWAYS_INLINE __m128i mm_load8_zero_extend(const void *ptr) |
405 | { |
406 | const __m128i *dataptr = static_cast<const __m128i *>(ptr); |
407 | if constexpr (UseSse4_1) { |
408 | // use a MOVQ followed by PMOVZXBW |
409 | // if AVX2 is present, these should combine into a single VPMOVZXBW instruction |
410 | __m128i data = _mm_loadl_epi64(p: dataptr); |
411 | return _mm_cvtepu8_epi16(V: data); |
412 | } |
413 | |
414 | // use MOVQ followed by PUNPCKLBW |
415 | __m128i data = _mm_loadl_epi64(p: dataptr); |
416 | return _mm_unpacklo_epi8(a: data, b: _mm_setzero_si128()); |
417 | } |
418 | |
419 | [[maybe_unused]] ATTRIBUTE_NO_SANITIZE |
420 | static qsizetype qustrlen_sse2(const char16_t *str) noexcept |
421 | { |
422 | // find the 16-byte alignment immediately prior or equal to str |
423 | quintptr misalignment = quintptr(str) & 0xf; |
424 | Q_ASSERT((misalignment & 1) == 0); |
425 | const char16_t *ptr = str - (misalignment / 2); |
426 | |
427 | // load 16 bytes and see if we have a null |
428 | // (aligned loads can never segfault) |
429 | const __m128i zeroes = _mm_setzero_si128(); |
430 | __m128i data = _mm_load_si128(p: reinterpret_cast<const __m128i *>(ptr)); |
431 | __m128i comparison = _mm_cmpeq_epi16(a: data, b: zeroes); |
432 | uint mask = _mm_movemask_epi8(a: comparison); |
433 | |
434 | // ignore the result prior to the beginning of str |
435 | mask >>= misalignment; |
436 | |
437 | // Have we found something in the first block? Need to handle it now |
438 | // because of the left shift above. |
439 | if (mask) |
440 | return qCountTrailingZeroBits(v: mask) / sizeof(char16_t); |
441 | |
442 | constexpr qsizetype Step = sizeof(__m128i) / sizeof(char16_t); |
443 | qsizetype size = Step - misalignment / sizeof(char16_t); |
444 | |
445 | size -= Step; |
446 | do { |
447 | size += Step; |
448 | data = _mm_load_si128(p: reinterpret_cast<const __m128i *>(str + size)); |
449 | |
450 | comparison = _mm_cmpeq_epi16(a: data, b: zeroes); |
451 | mask = _mm_movemask_epi8(a: comparison); |
452 | } while (mask == 0); |
453 | |
454 | // found a null |
455 | return size + qCountTrailingZeroBits(v: mask) / sizeof(char16_t); |
456 | } |
457 | |
458 | // Scans from \a ptr to \a end until \a maskval is non-zero. Returns true if |
459 | // the no non-zero was found. Returns false and updates \a ptr to point to the |
460 | // first 16-bit word that has any bit set (note: if the input is 8-bit, \a ptr |
461 | // may be updated to one byte short). |
462 | static bool simdTestMask(const char *&ptr, const char *end, quint32 maskval) |
463 | { |
464 | auto updatePtr = [&](uint result) { |
465 | // found a character matching the mask |
466 | uint idx = qCountTrailingZeroBits(v: ~result); |
467 | ptr += idx; |
468 | return false; |
469 | }; |
470 | |
471 | if constexpr (UseSse4_1) { |
472 | # ifndef Q_OS_QNX // compiler fails in the code below |
473 | __m128i mask; |
474 | auto updatePtrSimd = [&](__m128i data) { |
475 | __m128i masked = _mm_and_si128(a: mask, b: data); |
476 | __m128i comparison = _mm_cmpeq_epi16(a: masked, b: _mm_setzero_si128()); |
477 | uint result = _mm_movemask_epi8(a: comparison); |
478 | return updatePtr(result); |
479 | }; |
480 | |
481 | if constexpr (UseAvx2) { |
482 | // AVX2 implementation: test 32 bytes at a time |
483 | const __m256i mask256 = _mm256_broadcastd_epi32(X: _mm_cvtsi32_si128(a: maskval)); |
484 | while (ptr + 32 <= end) { |
485 | __m256i data = _mm256_loadu_si256(p: reinterpret_cast<const __m256i *>(ptr)); |
486 | if (!_mm256_testz_si256(a: mask256, b: data)) { |
487 | // found a character matching the mask |
488 | __m256i masked256 = _mm256_and_si256(a: mask256, b: data); |
489 | __m256i comparison256 = _mm256_cmpeq_epi16(a: masked256, b: _mm256_setzero_si256()); |
490 | return updatePtr(_mm256_movemask_epi8(a: comparison256)); |
491 | } |
492 | ptr += 32; |
493 | } |
494 | |
495 | mask = _mm256_castsi256_si128(a: mask256); |
496 | } else { |
497 | // SSE 4.1 implementation: test 32 bytes at a time (two 16-byte |
498 | // comparisons, unrolled) |
499 | mask = _mm_set1_epi32(i: maskval); |
500 | while (ptr + 32 <= end) { |
501 | __m128i data1 = _mm_loadu_si128(p: reinterpret_cast<const __m128i *>(ptr)); |
502 | __m128i data2 = _mm_loadu_si128(p: reinterpret_cast<const __m128i *>(ptr + 16)); |
503 | if (!_mm_testz_si128(M: mask, V: data1)) |
504 | return updatePtrSimd(data1); |
505 | |
506 | ptr += 16; |
507 | if (!_mm_testz_si128(M: mask, V: data2)) |
508 | return updatePtrSimd(data2); |
509 | ptr += 16; |
510 | } |
511 | } |
512 | |
513 | // AVX2 and SSE4.1: final 16-byte comparison |
514 | if (ptr + 16 <= end) { |
515 | __m128i data1 = _mm_loadu_si128(p: reinterpret_cast<const __m128i *>(ptr)); |
516 | if (!_mm_testz_si128(M: mask, V: data1)) |
517 | return updatePtrSimd(data1); |
518 | ptr += 16; |
519 | } |
520 | |
521 | // and final 8-byte comparison |
522 | if (ptr + 8 <= end) { |
523 | __m128i data1 = _mm_loadl_epi64(p: reinterpret_cast<const __m128i *>(ptr)); |
524 | if (!_mm_testz_si128(M: mask, V: data1)) |
525 | return updatePtrSimd(data1); |
526 | ptr += 8; |
527 | } |
528 | |
529 | return true; |
530 | # endif // QNX |
531 | } |
532 | |
533 | // SSE2 implementation: test 16 bytes at a time. |
534 | const __m128i mask = _mm_set1_epi32(i: maskval); |
535 | while (ptr + 16 <= end) { |
536 | __m128i data = _mm_loadu_si128(p: reinterpret_cast<const __m128i *>(ptr)); |
537 | __m128i masked = _mm_and_si128(a: mask, b: data); |
538 | __m128i comparison = _mm_cmpeq_epi16(a: masked, b: _mm_setzero_si128()); |
539 | quint16 result = _mm_movemask_epi8(a: comparison); |
540 | if (result != 0xffff) |
541 | return updatePtr(result); |
542 | ptr += 16; |
543 | } |
544 | |
545 | // and one 8-byte comparison |
546 | if (ptr + 8 <= end) { |
547 | __m128i data = _mm_loadl_epi64(p: reinterpret_cast<const __m128i *>(ptr)); |
548 | __m128i masked = _mm_and_si128(a: mask, b: data); |
549 | __m128i comparison = _mm_cmpeq_epi16(a: masked, b: _mm_setzero_si128()); |
550 | quint8 result = _mm_movemask_epi8(a: comparison); |
551 | if (result != 0xff) |
552 | return updatePtr(result); |
553 | ptr += 8; |
554 | } |
555 | |
556 | return true; |
557 | } |
558 | |
559 | template <StringComparisonMode Mode, typename Char> [[maybe_unused]] |
560 | static int ucstrncmp_sse2(const char16_t *a, const Char *b, size_t l) |
561 | { |
562 | static_assert(std::is_unsigned_v<Char>); |
563 | |
564 | // Using the PMOVMSKB instruction, we get two bits for each UTF-16 character |
565 | // we compare. This lambda helps extract the code unit. |
566 | static const auto codeUnitAt = [](const auto *n, qptrdiff idx) -> int { |
567 | constexpr int Stride = 2; |
568 | // this is the same as: |
569 | // return n[idx / Stride]; |
570 | // but using pointer arithmetic to avoid the compiler dividing by two |
571 | // and multiplying by two in the case of char16_t (we know idx is even, |
572 | // but the compiler does not). This is not UB. |
573 | |
574 | auto ptr = reinterpret_cast<const uchar *>(n); |
575 | ptr += idx / (Stride / sizeof(*n)); |
576 | return *reinterpret_cast<decltype(n)>(ptr); |
577 | }; |
578 | auto difference = [a, b](uint mask, qptrdiff offset) { |
579 | if (Mode == CompareStringsForEquality) |
580 | return 1; |
581 | uint idx = qCountTrailingZeroBits(v: mask); |
582 | return codeUnitAt(a + offset, idx) - codeUnitAt(b + offset, idx); |
583 | }; |
584 | |
585 | static const auto load8Chars = [](const auto *ptr) { |
586 | if (sizeof(*ptr) == 2) |
587 | return _mm_loadu_si128(p: reinterpret_cast<const __m128i *>(ptr)); |
588 | __m128i chunk = _mm_loadl_epi64(p: reinterpret_cast<const __m128i *>(ptr)); |
589 | return _mm_unpacklo_epi8(a: chunk, b: _mm_setzero_si128()); |
590 | }; |
591 | static const auto load4Chars = [](const auto *ptr) { |
592 | if (sizeof(*ptr) == 2) |
593 | return _mm_loadl_epi64(p: reinterpret_cast<const __m128i *>(ptr)); |
594 | __m128i chunk = _mm_cvtsi32_si128(qFromUnaligned<quint32>(ptr)); |
595 | return _mm_unpacklo_epi8(a: chunk, b: _mm_setzero_si128()); |
596 | }; |
597 | |
598 | // we're going to read a[0..15] and b[0..15] (32 bytes) |
599 | auto processChunk16Chars = [a, b](qptrdiff offset) -> uint { |
600 | if constexpr (UseAvx2) { |
601 | __m256i a_data = _mm256_loadu_si256(p: reinterpret_cast<const __m256i *>(a + offset)); |
602 | __m256i b_data; |
603 | if (sizeof(Char) == 1) { |
604 | // expand to UTF-16 via zero-extension |
605 | __m128i chunk = _mm_loadu_si128(p: reinterpret_cast<const __m128i *>(b + offset)); |
606 | b_data = _mm256_cvtepu8_epi16(V: chunk); |
607 | } else { |
608 | b_data = _mm256_loadu_si256(p: reinterpret_cast<const __m256i *>(b + offset)); |
609 | } |
610 | __m256i result = _mm256_cmpeq_epi16(a: a_data, b: b_data); |
611 | return _mm256_movemask_epi8(a: result); |
612 | } |
613 | |
614 | __m128i a_data1 = load8Chars(a + offset); |
615 | __m128i a_data2 = load8Chars(a + offset + 8); |
616 | __m128i b_data1, b_data2; |
617 | if (sizeof(Char) == 1) { |
618 | // expand to UTF-16 via unpacking |
619 | __m128i b_data = _mm_loadu_si128(p: reinterpret_cast<const __m128i *>(b + offset)); |
620 | b_data1 = _mm_unpacklo_epi8(a: b_data, b: _mm_setzero_si128()); |
621 | b_data2 = _mm_unpackhi_epi8(a: b_data, b: _mm_setzero_si128()); |
622 | } else { |
623 | b_data1 = load8Chars(b + offset); |
624 | b_data2 = load8Chars(b + offset + 8); |
625 | } |
626 | __m128i result1 = _mm_cmpeq_epi16(a: a_data1, b: b_data1); |
627 | __m128i result2 = _mm_cmpeq_epi16(a: a_data2, b: b_data2); |
628 | return _mm_movemask_epi8(a: result1) | _mm_movemask_epi8(a: result2) << 16; |
629 | }; |
630 | |
631 | if (l >= sizeof(__m256i) / sizeof(char16_t)) { |
632 | qptrdiff offset = 0; |
633 | for ( ; l >= offset + sizeof(__m256i) / sizeof(char16_t); offset += sizeof(__m256i) / sizeof(char16_t)) { |
634 | uint mask = ~processChunk16Chars(offset); |
635 | if (mask) |
636 | return difference(mask, offset); |
637 | } |
638 | |
639 | // maybe overlap the last 32 bytes |
640 | if (size_t(offset) < l) { |
641 | offset = l - sizeof(__m256i) / sizeof(char16_t); |
642 | uint mask = ~processChunk16Chars(offset); |
643 | return mask ? difference(mask, offset) : 0; |
644 | } |
645 | } else if (l >= 4) { |
646 | __m128i a_data1, b_data1; |
647 | __m128i a_data2, b_data2; |
648 | int width; |
649 | if (l >= 8) { |
650 | width = 8; |
651 | a_data1 = load8Chars(a); |
652 | b_data1 = load8Chars(b); |
653 | a_data2 = load8Chars(a + l - width); |
654 | b_data2 = load8Chars(b + l - width); |
655 | } else { |
656 | // we're going to read a[0..3] and b[0..3] (8 bytes) |
657 | width = 4; |
658 | a_data1 = load4Chars(a); |
659 | b_data1 = load4Chars(b); |
660 | a_data2 = load4Chars(a + l - width); |
661 | b_data2 = load4Chars(b + l - width); |
662 | } |
663 | |
664 | __m128i result = _mm_cmpeq_epi16(a: a_data1, b: b_data1); |
665 | ushort mask = ~_mm_movemask_epi8(a: result); |
666 | if (mask) |
667 | return difference(mask, 0); |
668 | |
669 | result = _mm_cmpeq_epi16(a: a_data2, b: b_data2); |
670 | mask = ~_mm_movemask_epi8(a: result); |
671 | if (mask) |
672 | return difference(mask, l - width); |
673 | } else { |
674 | // reset l |
675 | l &= 3; |
676 | |
677 | const auto lambda = [=](size_t i) -> int { |
678 | return a[i] - b[i]; |
679 | }; |
680 | return UnrollTailLoop<3>::exec(l, 0, lambda, lambda); |
681 | } |
682 | return 0; |
683 | } |
684 | #endif |
685 | |
686 | qsizetype QtPrivate::qustrlen(const char16_t *str) noexcept |
687 | { |
688 | #if defined(__SSE2__) && !(defined(__SANITIZE_ADDRESS__) || __has_feature(address_sanitizer)) |
689 | return qustrlen_sse2(str); |
690 | #endif |
691 | |
692 | if (sizeof(wchar_t) == sizeof(char16_t)) |
693 | return wcslen(s: reinterpret_cast<const wchar_t *>(str)); |
694 | |
695 | qsizetype result = 0; |
696 | while (*str++) |
697 | ++result; |
698 | return result; |
699 | } |
700 | |
701 | /*! |
702 | * \internal |
703 | * |
704 | * Searches for character \a c in the string \a str and returns a pointer to |
705 | * it. Unlike strchr() and wcschr() (but like glibc's strchrnul()), if the |
706 | * character is not found, this function returns a pointer to the end of the |
707 | * string -- that is, \c{str.end()}. |
708 | */ |
709 | const char16_t *QtPrivate::qustrchr(QStringView str, char16_t c) noexcept |
710 | { |
711 | const char16_t *n = str.utf16(); |
712 | const char16_t *e = n + str.size(); |
713 | |
714 | #ifdef __SSE2__ |
715 | bool loops = true; |
716 | // Using the PMOVMSKB instruction, we get two bits for each character |
717 | // we compare. |
718 | __m128i mch; |
719 | if constexpr (UseAvx2) { |
720 | // we're going to read n[0..15] (32 bytes) |
721 | __m256i mch256 = _mm256_set1_epi32(i: c | (c << 16)); |
722 | for (const char16_t *next = n + 16; next <= e; n = next, next += 16) { |
723 | __m256i data = _mm256_loadu_si256(p: reinterpret_cast<const __m256i *>(n)); |
724 | __m256i result = _mm256_cmpeq_epi16(a: data, b: mch256); |
725 | uint mask = uint(_mm256_movemask_epi8(a: result)); |
726 | if (mask) { |
727 | uint idx = qCountTrailingZeroBits(v: mask); |
728 | return n + idx / 2; |
729 | } |
730 | } |
731 | loops = false; |
732 | mch = _mm256_castsi256_si128(a: mch256); |
733 | } else { |
734 | mch = _mm_set1_epi32(i: c | (c << 16)); |
735 | } |
736 | |
737 | auto hasMatch = [mch, &n](__m128i data, ushort validityMask) { |
738 | __m128i result = _mm_cmpeq_epi16(a: data, b: mch); |
739 | uint mask = uint(_mm_movemask_epi8(a: result)); |
740 | if ((mask & validityMask) == 0) |
741 | return false; |
742 | uint idx = qCountTrailingZeroBits(v: mask); |
743 | n += idx / 2; |
744 | return true; |
745 | }; |
746 | |
747 | // we're going to read n[0..7] (16 bytes) |
748 | for (const char16_t *next = n + 8; next <= e; n = next, next += 8) { |
749 | __m128i data = _mm_loadu_si128(p: reinterpret_cast<const __m128i *>(n)); |
750 | if (hasMatch(data, 0xffff)) |
751 | return n; |
752 | |
753 | if (!loops) { |
754 | n += 8; |
755 | break; |
756 | } |
757 | } |
758 | |
759 | # if !defined(__OPTIMIZE_SIZE__) |
760 | // we're going to read n[0..3] (8 bytes) |
761 | if (e - n > 3) { |
762 | __m128i data = _mm_loadl_epi64(p: reinterpret_cast<const __m128i *>(n)); |
763 | if (hasMatch(data, 0xff)) |
764 | return n; |
765 | |
766 | n += 4; |
767 | } |
768 | |
769 | return UnrollTailLoop<3>::exec(count: e - n, returnIfExited: e, |
770 | loopCheck: [=](qsizetype i) { return n[i] == c; }, |
771 | returnIfFailed: [=](qsizetype i) { return n + i; }); |
772 | # endif |
773 | #elif defined(__ARM_NEON__) |
774 | const uint16x8_t vmask = { 1, 1 << 1, 1 << 2, 1 << 3, 1 << 4, 1 << 5, 1 << 6, 1 << 7 }; |
775 | const uint16x8_t ch_vec = vdupq_n_u16(c); |
776 | for (const char16_t *next = n + 8; next <= e; n = next, next += 8) { |
777 | uint16x8_t data = vld1q_u16(reinterpret_cast<const uint16_t *>(n)); |
778 | uint mask = vaddvq_u16(vandq_u16(vceqq_u16(data, ch_vec), vmask)); |
779 | if (ushort(mask)) { |
780 | // found a match |
781 | return n + qCountTrailingZeroBits(mask); |
782 | } |
783 | } |
784 | #endif // aarch64 |
785 | |
786 | return std::find(first: n, last: e, val: c); |
787 | } |
788 | |
789 | // Note: ptr on output may be off by one and point to a preceding US-ASCII |
790 | // character. Usually harmless. |
791 | bool qt_is_ascii(const char *&ptr, const char *end) noexcept |
792 | { |
793 | #if defined(__SSE2__) |
794 | // Testing for the high bit can be done efficiently with just PMOVMSKB |
795 | bool loops = true; |
796 | if constexpr (UseAvx2) { |
797 | while (ptr + 32 <= end) { |
798 | __m256i data = _mm256_loadu_si256(p: reinterpret_cast<const __m256i *>(ptr)); |
799 | quint32 mask = _mm256_movemask_epi8(a: data); |
800 | if (mask) { |
801 | uint idx = qCountTrailingZeroBits(v: mask); |
802 | ptr += idx; |
803 | return false; |
804 | } |
805 | ptr += 32; |
806 | } |
807 | loops = false; |
808 | } |
809 | |
810 | while (ptr + 16 <= end) { |
811 | __m128i data = _mm_loadu_si128(p: reinterpret_cast<const __m128i *>(ptr)); |
812 | quint32 mask = _mm_movemask_epi8(a: data); |
813 | if (mask) { |
814 | uint idx = qCountTrailingZeroBits(v: mask); |
815 | ptr += idx; |
816 | return false; |
817 | } |
818 | ptr += 16; |
819 | |
820 | if (!loops) |
821 | break; |
822 | } |
823 | if (ptr + 8 <= end) { |
824 | __m128i data = _mm_loadl_epi64(p: reinterpret_cast<const __m128i *>(ptr)); |
825 | quint8 mask = _mm_movemask_epi8(a: data); |
826 | if (mask) { |
827 | uint idx = qCountTrailingZeroBits(v: mask); |
828 | ptr += idx; |
829 | return false; |
830 | } |
831 | ptr += 8; |
832 | } |
833 | #endif |
834 | |
835 | while (ptr + 4 <= end) { |
836 | quint32 data = qFromUnaligned<quint32>(src: ptr); |
837 | if (data &= 0x80808080U) { |
838 | uint idx = QSysInfo::ByteOrder == QSysInfo::BigEndian |
839 | ? qCountLeadingZeroBits(v: data) |
840 | : qCountTrailingZeroBits(v: data); |
841 | ptr += idx / 8; |
842 | return false; |
843 | } |
844 | ptr += 4; |
845 | } |
846 | |
847 | while (ptr != end) { |
848 | if (quint8(*ptr) & 0x80) |
849 | return false; |
850 | ++ptr; |
851 | } |
852 | return true; |
853 | } |
854 | |
855 | bool QtPrivate::isAscii(QLatin1StringView s) noexcept |
856 | { |
857 | const char *ptr = s.begin(); |
858 | const char *end = s.end(); |
859 | |
860 | return qt_is_ascii(ptr, end); |
861 | } |
862 | |
863 | static bool isAscii_helper(const char16_t *&ptr, const char16_t *end) |
864 | { |
865 | #ifdef __SSE2__ |
866 | const char *ptr8 = reinterpret_cast<const char *>(ptr); |
867 | const char *end8 = reinterpret_cast<const char *>(end); |
868 | bool ok = simdTestMask(ptr&: ptr8, end: end8, maskval: 0xff80ff80); |
869 | ptr = reinterpret_cast<const char16_t *>(ptr8); |
870 | if (!ok) |
871 | return false; |
872 | #endif |
873 | |
874 | while (ptr != end) { |
875 | if (*ptr & 0xff80) |
876 | return false; |
877 | ++ptr; |
878 | } |
879 | return true; |
880 | } |
881 | |
882 | bool QtPrivate::isAscii(QStringView s) noexcept |
883 | { |
884 | const char16_t *ptr = s.utf16(); |
885 | const char16_t *end = ptr + s.size(); |
886 | |
887 | return isAscii_helper(ptr, end); |
888 | } |
889 | |
890 | bool QtPrivate::isLatin1(QStringView s) noexcept |
891 | { |
892 | const char16_t *ptr = s.utf16(); |
893 | const char16_t *end = ptr + s.size(); |
894 | |
895 | #ifdef __SSE2__ |
896 | const char *ptr8 = reinterpret_cast<const char *>(ptr); |
897 | const char *end8 = reinterpret_cast<const char *>(end); |
898 | if (!simdTestMask(ptr&: ptr8, end: end8, maskval: 0xff00ff00)) |
899 | return false; |
900 | ptr = reinterpret_cast<const char16_t *>(ptr8); |
901 | #endif |
902 | |
903 | while (ptr != end) { |
904 | if (*ptr++ > 0xff) |
905 | return false; |
906 | } |
907 | return true; |
908 | } |
909 | |
910 | bool QtPrivate::isValidUtf16(QStringView s) noexcept |
911 | { |
912 | constexpr char32_t InvalidCodePoint = UINT_MAX; |
913 | |
914 | QStringIterator i(s); |
915 | while (i.hasNext()) { |
916 | const char32_t c = i.next(invalidAs: InvalidCodePoint); |
917 | if (c == InvalidCodePoint) |
918 | return false; |
919 | } |
920 | |
921 | return true; |
922 | } |
923 | |
924 | // conversion between Latin 1 and UTF-16 |
925 | Q_CORE_EXPORT void qt_from_latin1(char16_t *dst, const char *str, size_t size) noexcept |
926 | { |
927 | /* SIMD: |
928 | * Unpacking with SSE has been shown to improve performance on recent CPUs |
929 | * The same method gives no improvement with NEON. On Aarch64, clang will do the vectorization |
930 | * itself in exactly the same way as one would do it with intrinsics. |
931 | */ |
932 | #if defined(__SSE2__) |
933 | // we're going to read str[offset..offset+15] (16 bytes) |
934 | const __m128i nullMask = _mm_setzero_si128(); |
935 | auto processOneChunk = [=](qptrdiff offset) { |
936 | const __m128i chunk = _mm_loadu_si128(p: (const __m128i*)(str + offset)); // load |
937 | if constexpr (UseAvx2) { |
938 | // zero extend to an YMM register |
939 | const __m256i extended = _mm256_cvtepu8_epi16(V: chunk); |
940 | |
941 | // store |
942 | _mm256_storeu_si256(p: (__m256i*)(dst + offset), a: extended); |
943 | } else { |
944 | // unpack the first 8 bytes, padding with zeros |
945 | const __m128i firstHalf = _mm_unpacklo_epi8(a: chunk, b: nullMask); |
946 | _mm_storeu_si128(p: (__m128i*)(dst + offset), b: firstHalf); // store |
947 | |
948 | // unpack the last 8 bytes, padding with zeros |
949 | const __m128i secondHalf = _mm_unpackhi_epi8 (a: chunk, b: nullMask); |
950 | _mm_storeu_si128(p: (__m128i*)(dst + offset + 8), b: secondHalf); // store |
951 | } |
952 | }; |
953 | |
954 | const char *e = str + size; |
955 | if (size >= sizeof(__m128i)) { |
956 | qptrdiff offset = 0; |
957 | for ( ; str + offset + sizeof(__m128i) <= e; offset += sizeof(__m128i)) |
958 | processOneChunk(offset); |
959 | if (str + offset < e) |
960 | processOneChunk(size - sizeof(__m128i)); |
961 | return; |
962 | } |
963 | |
964 | # if !defined(__OPTIMIZE_SIZE__) |
965 | if (size >= 4) { |
966 | // two overlapped loads & stores, of either 64-bit or of 32-bit |
967 | if (size >= 8) { |
968 | const __m128i unpacked1 = mm_load8_zero_extend(ptr: str); |
969 | const __m128i unpacked2 = mm_load8_zero_extend(ptr: str + size - 8); |
970 | _mm_storeu_si128(p: reinterpret_cast<__m128i *>(dst), b: unpacked1); |
971 | _mm_storeu_si128(p: reinterpret_cast<__m128i *>(dst + size - 8), b: unpacked2); |
972 | } else { |
973 | const __m128i chunk1 = _mm_cvtsi32_si128(a: qFromUnaligned<quint32>(src: str)); |
974 | const __m128i chunk2 = _mm_cvtsi32_si128(a: qFromUnaligned<quint32>(src: str + size - 4)); |
975 | const __m128i unpacked1 = _mm_unpacklo_epi8(a: chunk1, b: nullMask); |
976 | const __m128i unpacked2 = _mm_unpacklo_epi8(a: chunk2, b: nullMask); |
977 | _mm_storel_epi64(p: reinterpret_cast<__m128i *>(dst), a: unpacked1); |
978 | _mm_storel_epi64(p: reinterpret_cast<__m128i *>(dst + size - 4), a: unpacked2); |
979 | } |
980 | return; |
981 | } else { |
982 | size = size % 4; |
983 | return UnrollTailLoop<3>::exec(count: qsizetype(size), code: [=](qsizetype i) { dst[i] = uchar(str[i]); }); |
984 | } |
985 | # endif |
986 | #endif |
987 | #if defined(__mips_dsp) |
988 | static_assert(sizeof(qsizetype) == sizeof(int), |
989 | "oops, the assembler implementation needs to be called in a loop" ); |
990 | if (size > 20) |
991 | qt_fromlatin1_mips_asm_unroll8(dst, str, size); |
992 | else |
993 | qt_fromlatin1_mips_asm_unroll4(dst, str, size); |
994 | #else |
995 | while (size--) |
996 | *dst++ = (uchar)*str++; |
997 | #endif |
998 | } |
999 | |
1000 | static QVarLengthArray<char16_t> qt_from_latin1_to_qvla(QLatin1StringView str) |
1001 | { |
1002 | const qsizetype len = str.size(); |
1003 | QVarLengthArray<char16_t> arr(len); |
1004 | qt_from_latin1(dst: arr.data(), str: str.data(), size: len); |
1005 | return arr; |
1006 | } |
1007 | |
1008 | template <bool Checked> |
1009 | static void qt_to_latin1_internal(uchar *dst, const char16_t *src, qsizetype length) |
1010 | { |
1011 | #if defined(__SSE2__) |
1012 | auto questionMark256 = []() { |
1013 | if constexpr (UseAvx2) |
1014 | return _mm256_broadcastw_epi16(X: _mm_cvtsi32_si128(a: '?')); |
1015 | else |
1016 | return 0; |
1017 | }(); |
1018 | auto outOfRange256 = []() { |
1019 | if constexpr (UseAvx2) |
1020 | return _mm256_broadcastw_epi16(X: _mm_cvtsi32_si128(a: 0x100)); |
1021 | else |
1022 | return 0; |
1023 | }(); |
1024 | __m128i questionMark, outOfRange; |
1025 | if constexpr (UseAvx2) { |
1026 | questionMark = _mm256_castsi256_si128(questionMark256); |
1027 | outOfRange = _mm256_castsi256_si128(outOfRange256); |
1028 | } else { |
1029 | questionMark = _mm_set1_epi16(w: '?'); |
1030 | outOfRange = _mm_set1_epi16(w: 0x100); |
1031 | } |
1032 | |
1033 | auto mergeQuestionMarks = [=](__m128i chunk) { |
1034 | if (!Checked) |
1035 | return chunk; |
1036 | |
1037 | // SSE has no compare instruction for unsigned comparison. |
1038 | if constexpr (UseSse4_1) { |
1039 | // We use an unsigned uc = qMin(uc, 0x100) and then compare for equality. |
1040 | chunk = _mm_min_epu16(V1: chunk, V2: outOfRange); |
1041 | const __m128i offLimitMask = _mm_cmpeq_epi16(a: chunk, b: outOfRange); |
1042 | chunk = _mm_blendv_epi8(V1: chunk, V2: questionMark, M: offLimitMask); |
1043 | return chunk; |
1044 | } |
1045 | // The variables must be shiffted + 0x8000 to be compared |
1046 | const __m128i signedBitOffset = _mm_set1_epi16(w: short(0x8000)); |
1047 | const __m128i thresholdMask = _mm_set1_epi16(w: short(0xff + 0x8000)); |
1048 | |
1049 | const __m128i signedChunk = _mm_add_epi16(a: chunk, b: signedBitOffset); |
1050 | const __m128i offLimitMask = _mm_cmpgt_epi16(a: signedChunk, b: thresholdMask); |
1051 | |
1052 | // offLimitQuestionMark contains '?' for each 16 bits that was off-limit |
1053 | // the 16 bits that were correct contains zeros |
1054 | const __m128i offLimitQuestionMark = _mm_and_si128(a: offLimitMask, b: questionMark); |
1055 | |
1056 | // correctBytes contains the bytes that were in limit |
1057 | // the 16 bits that were off limits contains zeros |
1058 | const __m128i correctBytes = _mm_andnot_si128(a: offLimitMask, b: chunk); |
1059 | |
1060 | // merge offLimitQuestionMark and correctBytes to have the result |
1061 | chunk = _mm_or_si128(a: correctBytes, b: offLimitQuestionMark); |
1062 | |
1063 | Q_UNUSED(outOfRange); |
1064 | return chunk; |
1065 | }; |
1066 | |
1067 | // we're going to read to src[offset..offset+15] (16 bytes) |
1068 | auto loadChunkAt = [=](qptrdiff offset) { |
1069 | __m128i chunk1, chunk2; |
1070 | if constexpr (UseAvx2) { |
1071 | __m256i chunk = _mm256_loadu_si256(p: reinterpret_cast<const __m256i *>(src + offset)); |
1072 | if (Checked) { |
1073 | // See mergeQuestionMarks lambda above for details |
1074 | chunk = _mm256_min_epu16(chunk, outOfRange256); |
1075 | const __m256i offLimitMask = _mm256_cmpeq_epi16(chunk, outOfRange256); |
1076 | chunk = _mm256_blendv_epi8(chunk, questionMark256, offLimitMask); |
1077 | } |
1078 | |
1079 | chunk2 = _mm256_extracti128_si256(chunk, 1); |
1080 | chunk1 = _mm256_castsi256_si128(a: chunk); |
1081 | } else { |
1082 | chunk1 = _mm_loadu_si128(p: (const __m128i*)(src + offset)); // load |
1083 | chunk1 = mergeQuestionMarks(chunk1); |
1084 | |
1085 | chunk2 = _mm_loadu_si128(p: (const __m128i*)(src + offset + 8)); // load |
1086 | chunk2 = mergeQuestionMarks(chunk2); |
1087 | } |
1088 | |
1089 | // pack the two vector to 16 x 8bits elements |
1090 | return _mm_packus_epi16(a: chunk1, b: chunk2); |
1091 | }; |
1092 | |
1093 | if (size_t(length) >= sizeof(__m128i)) { |
1094 | // because of possible overlapping, we won't process the last chunk in the loop |
1095 | qptrdiff offset = 0; |
1096 | for ( ; offset + 2 * sizeof(__m128i) < size_t(length); offset += sizeof(__m128i)) |
1097 | _mm_storeu_si128(reinterpret_cast<__m128i *>(dst + offset), loadChunkAt(offset)); |
1098 | |
1099 | // overlapped conversion of the last full chunk and the tail |
1100 | __m128i last1 = loadChunkAt(offset); |
1101 | __m128i last2 = loadChunkAt(length - sizeof(__m128i)); |
1102 | _mm_storeu_si128(p: reinterpret_cast<__m128i *>(dst + offset), b: last1); |
1103 | _mm_storeu_si128(p: reinterpret_cast<__m128i *>(dst + length - sizeof(__m128i)), b: last2); |
1104 | return; |
1105 | } |
1106 | |
1107 | # if !defined(__OPTIMIZE_SIZE__) |
1108 | if (length >= 4) { |
1109 | // this code is fine even for in-place conversion because we load both |
1110 | // before any store |
1111 | if (length >= 8) { |
1112 | __m128i chunk1 = _mm_loadu_si128(p: reinterpret_cast<const __m128i *>(src)); |
1113 | __m128i chunk2 = _mm_loadu_si128(p: reinterpret_cast<const __m128i *>(src + length - 8)); |
1114 | chunk1 = mergeQuestionMarks(chunk1); |
1115 | chunk2 = mergeQuestionMarks(chunk2); |
1116 | |
1117 | // pack, where the upper half is ignored |
1118 | const __m128i result1 = _mm_packus_epi16(a: chunk1, b: chunk1); |
1119 | const __m128i result2 = _mm_packus_epi16(a: chunk2, b: chunk2); |
1120 | _mm_storel_epi64(p: reinterpret_cast<__m128i *>(dst), a: result1); |
1121 | _mm_storel_epi64(p: reinterpret_cast<__m128i *>(dst + length - 8), a: result2); |
1122 | } else { |
1123 | __m128i chunk1 = _mm_loadl_epi64(p: reinterpret_cast<const __m128i *>(src)); |
1124 | __m128i chunk2 = _mm_loadl_epi64(p: reinterpret_cast<const __m128i *>(src + length - 4)); |
1125 | chunk1 = mergeQuestionMarks(chunk1); |
1126 | chunk2 = mergeQuestionMarks(chunk2); |
1127 | |
1128 | // pack, we'll zero the upper three quarters |
1129 | const __m128i result1 = _mm_packus_epi16(a: chunk1, b: chunk1); |
1130 | const __m128i result2 = _mm_packus_epi16(a: chunk2, b: chunk2); |
1131 | qToUnaligned(src: _mm_cvtsi128_si32(a: result1), dest: dst); |
1132 | qToUnaligned(src: _mm_cvtsi128_si32(a: result2), dest: dst + length - 4); |
1133 | } |
1134 | return; |
1135 | } |
1136 | |
1137 | length = length % 4; |
1138 | return UnrollTailLoop<3>::exec(length, [=](qsizetype i) { |
1139 | if (Checked) |
1140 | dst[i] = (src[i]>0xff) ? '?' : (uchar) src[i]; |
1141 | else |
1142 | dst[i] = src[i]; |
1143 | }); |
1144 | # else |
1145 | length = length % 16; |
1146 | # endif // optimize size |
1147 | #elif defined(__ARM_NEON__) |
1148 | // Refer to the documentation of the SSE2 implementation. |
1149 | // This uses exactly the same method as for SSE except: |
1150 | // 1) neon has unsigned comparison |
1151 | // 2) packing is done to 64 bits (8 x 8bits component). |
1152 | if (length >= 16) { |
1153 | const qsizetype chunkCount = length >> 3; // divided by 8 |
1154 | const uint16x8_t questionMark = vdupq_n_u16('?'); // set |
1155 | const uint16x8_t thresholdMask = vdupq_n_u16(0xff); // set |
1156 | for (qsizetype i = 0; i < chunkCount; ++i) { |
1157 | uint16x8_t chunk = vld1q_u16((uint16_t *)src); // load |
1158 | src += 8; |
1159 | |
1160 | if (Checked) { |
1161 | const uint16x8_t offLimitMask = vcgtq_u16(chunk, thresholdMask); // chunk > thresholdMask |
1162 | const uint16x8_t offLimitQuestionMark = vandq_u16(offLimitMask, questionMark); // offLimitMask & questionMark |
1163 | const uint16x8_t correctBytes = vbicq_u16(chunk, offLimitMask); // !offLimitMask & chunk |
1164 | chunk = vorrq_u16(correctBytes, offLimitQuestionMark); // correctBytes | offLimitQuestionMark |
1165 | } |
1166 | const uint8x8_t result = vmovn_u16(chunk); // narrowing move->packing |
1167 | vst1_u8(dst, result); // store |
1168 | dst += 8; |
1169 | } |
1170 | length = length % 8; |
1171 | } |
1172 | #endif |
1173 | #if defined(__mips_dsp) |
1174 | static_assert(sizeof(qsizetype) == sizeof(int), |
1175 | "oops, the assembler implementation needs to be called in a loop" ); |
1176 | qt_toLatin1_mips_dsp_asm(dst, src, length); |
1177 | #else |
1178 | while (length--) { |
1179 | if (Checked) |
1180 | *dst++ = (*src>0xff) ? '?' : (uchar) *src; |
1181 | else |
1182 | *dst++ = *src; |
1183 | ++src; |
1184 | } |
1185 | #endif |
1186 | } |
1187 | |
1188 | void qt_to_latin1(uchar *dst, const char16_t *src, qsizetype length) |
1189 | { |
1190 | qt_to_latin1_internal<true>(dst, src, length); |
1191 | } |
1192 | |
1193 | void qt_to_latin1_unchecked(uchar *dst, const char16_t *src, qsizetype length) |
1194 | { |
1195 | qt_to_latin1_internal<false>(dst, src, length); |
1196 | } |
1197 | |
1198 | // Unicode case-insensitive comparison (argument order matches QStringView) |
1199 | Q_NEVER_INLINE static int ucstricmp(qsizetype alen, const char16_t *a, qsizetype blen, const char16_t *b) |
1200 | { |
1201 | if (a == b) |
1202 | return qt_lencmp(lhs: alen, rhs: blen); |
1203 | |
1204 | char32_t alast = 0; |
1205 | char32_t blast = 0; |
1206 | qsizetype l = qMin(a: alen, b: blen); |
1207 | qsizetype i; |
1208 | for (i = 0; i < l; ++i) { |
1209 | // qDebug() << Qt::hex << alast << blast; |
1210 | // qDebug() << Qt::hex << "*a=" << *a << "alast=" << alast << "folded=" << foldCase (*a, alast); |
1211 | // qDebug() << Qt::hex << "*b=" << *b << "blast=" << blast << "folded=" << foldCase (*b, blast); |
1212 | int diff = foldCase(ch: a[i], last&: alast) - foldCase(ch: b[i], last&: blast); |
1213 | if ((diff)) |
1214 | return diff; |
1215 | } |
1216 | if (i == alen) { |
1217 | if (i == blen) |
1218 | return 0; |
1219 | return -1; |
1220 | } |
1221 | return 1; |
1222 | } |
1223 | |
1224 | // Case-insensitive comparison between a QStringView and a QLatin1StringView |
1225 | // (argument order matches those types) |
1226 | Q_NEVER_INLINE static int ucstricmp(qsizetype alen, const char16_t *a, qsizetype blen, const char *b) |
1227 | { |
1228 | qsizetype l = qMin(a: alen, b: blen); |
1229 | qsizetype i; |
1230 | for (i = 0; i < l; ++i) { |
1231 | int diff = foldCase(ch: a[i]) - foldCase(ch: char16_t{uchar(b[i])}); |
1232 | if ((diff)) |
1233 | return diff; |
1234 | } |
1235 | if (i == alen) { |
1236 | if (i == blen) |
1237 | return 0; |
1238 | return -1; |
1239 | } |
1240 | return 1; |
1241 | } |
1242 | |
1243 | // Case-insensitive comparison between a Unicode string and a UTF-8 string |
1244 | Q_NEVER_INLINE static int ucstricmp8(const char *utf8, const char *utf8end, const QChar *utf16, const QChar *utf16end) |
1245 | { |
1246 | auto src1 = reinterpret_cast<const uchar *>(utf8); |
1247 | auto end1 = reinterpret_cast<const uchar *>(utf8end); |
1248 | QStringIterator src2(utf16, utf16end); |
1249 | |
1250 | while (src1 < end1 && src2.hasNext()) { |
1251 | char32_t uc1 = 0; |
1252 | char32_t *output = &uc1; |
1253 | uchar b = *src1++; |
1254 | const qsizetype res = QUtf8Functions::fromUtf8<QUtf8BaseTraits>(b, dst&: output, src&: src1, end: end1); |
1255 | if (res < 0) { |
1256 | // decoding error |
1257 | uc1 = QChar::ReplacementCharacter; |
1258 | } else { |
1259 | uc1 = QChar::toCaseFolded(ucs4: uc1); |
1260 | } |
1261 | |
1262 | char32_t uc2 = QChar::toCaseFolded(ucs4: src2.next()); |
1263 | int diff = uc1 - uc2; // can't underflow |
1264 | if (diff) |
1265 | return diff; |
1266 | } |
1267 | |
1268 | // the shorter string sorts first |
1269 | return (end1 > src1) - int(src2.hasNext()); |
1270 | } |
1271 | |
1272 | #if defined(__mips_dsp) |
1273 | // From qstring_mips_dsp_asm.S |
1274 | extern "C" int qt_ucstrncmp_mips_dsp_asm(const char16_t *a, |
1275 | const char16_t *b, |
1276 | unsigned len); |
1277 | #endif |
1278 | |
1279 | // Unicode case-sensitive compare two same-sized strings |
1280 | template <StringComparisonMode Mode> |
1281 | static int ucstrncmp(const char16_t *a, const char16_t *b, size_t l) |
1282 | { |
1283 | // This function isn't memcmp() because that can return the wrong sorting |
1284 | // result in little-endian architectures: 0x00ff must sort before 0x0100, |
1285 | // but the bytes in memory are FF 00 and 00 01. |
1286 | |
1287 | #ifndef __OPTIMIZE_SIZE__ |
1288 | # if defined(__mips_dsp) |
1289 | static_assert(sizeof(uint) == sizeof(size_t)); |
1290 | if (l >= 8) { |
1291 | return qt_ucstrncmp_mips_dsp_asm(a, b, l); |
1292 | } |
1293 | # elif defined(__SSE2__) |
1294 | return ucstrncmp_sse2<Mode>(a, b, l); |
1295 | # elif defined(__ARM_NEON__) |
1296 | if (l >= 8) { |
1297 | const char16_t *end = a + l; |
1298 | const uint16x8_t mask = { 1, 1 << 1, 1 << 2, 1 << 3, 1 << 4, 1 << 5, 1 << 6, 1 << 7 }; |
1299 | while (end - a > 7) { |
1300 | uint16x8_t da = vld1q_u16(reinterpret_cast<const uint16_t *>(a)); |
1301 | uint16x8_t db = vld1q_u16(reinterpret_cast<const uint16_t *>(b)); |
1302 | |
1303 | uint8_t r = ~(uint8_t)vaddvq_u16(vandq_u16(vceqq_u16(da, db), mask)); |
1304 | if (r) { |
1305 | // found a different QChar |
1306 | if (Mode == CompareStringsForEquality) |
1307 | return 1; |
1308 | uint idx = qCountTrailingZeroBits(r); |
1309 | return a[idx] - b[idx]; |
1310 | } |
1311 | a += 8; |
1312 | b += 8; |
1313 | } |
1314 | l &= 7; |
1315 | } |
1316 | const auto lambda = [=](size_t i) -> int { |
1317 | return a[i] - b[i]; |
1318 | }; |
1319 | return UnrollTailLoop<7>::exec(l, 0, lambda, lambda); |
1320 | # endif // MIPS DSP or __SSE2__ or __ARM_NEON__ |
1321 | #endif // __OPTIMIZE_SIZE__ |
1322 | |
1323 | if (Mode == CompareStringsForEquality || QSysInfo::ByteOrder == QSysInfo::BigEndian) |
1324 | return memcmp(s1: a, s2: b, n: l * sizeof(char16_t)); |
1325 | |
1326 | for (size_t i = 0; i < l; ++i) { |
1327 | if (int diff = a[i] - b[i]) |
1328 | return diff; |
1329 | } |
1330 | return 0; |
1331 | } |
1332 | |
1333 | template <StringComparisonMode Mode> |
1334 | static int ucstrncmp(const char16_t *a, const char *b, size_t l) |
1335 | { |
1336 | const uchar *c = reinterpret_cast<const uchar *>(b); |
1337 | const char16_t *uc = a; |
1338 | const char16_t *e = uc + l; |
1339 | |
1340 | #if defined(__SSE2__) && !defined(__OPTIMIZE_SIZE__) |
1341 | return ucstrncmp_sse2<Mode>(uc, c, l); |
1342 | #endif |
1343 | |
1344 | while (uc < e) { |
1345 | int diff = *uc - *c; |
1346 | if (diff) |
1347 | return diff; |
1348 | uc++, c++; |
1349 | } |
1350 | |
1351 | return 0; |
1352 | } |
1353 | |
1354 | // Unicode case-sensitive equality |
1355 | template <typename Char2> |
1356 | static bool ucstreq(const char16_t *a, size_t alen, const Char2 *b, size_t blen) |
1357 | { |
1358 | if (alen != blen) |
1359 | return false; |
1360 | if constexpr (std::is_same_v<decltype(a), decltype(b)>) { |
1361 | if (a == b) |
1362 | return true; |
1363 | } |
1364 | return ucstrncmp<CompareStringsForEquality>(a, b, alen) == 0; |
1365 | } |
1366 | |
1367 | // Unicode case-sensitive comparison |
1368 | template <typename Char2> |
1369 | static int ucstrcmp(const char16_t *a, size_t alen, const Char2 *b, size_t blen) |
1370 | { |
1371 | if constexpr (std::is_same_v<decltype(a), decltype(b)>) { |
1372 | if (a == b && alen == blen) |
1373 | return 0; |
1374 | } |
1375 | const size_t l = qMin(a: alen, b: blen); |
1376 | int cmp = ucstrncmp<CompareStringsForOrdering>(a, b, l); |
1377 | return cmp ? cmp : qt_lencmp(lhs: alen, rhs: blen); |
1378 | } |
1379 | |
1380 | using CaseInsensitiveL1 = QtPrivate::QCaseInsensitiveLatin1Hash; |
1381 | |
1382 | static int latin1nicmp(const char *lhsChar, qsizetype lSize, const char *rhsChar, qsizetype rSize) |
1383 | { |
1384 | // We're called with QLatin1StringView's .data() and .size(): |
1385 | Q_ASSERT(lSize >= 0 && rSize >= 0); |
1386 | if (!lSize) |
1387 | return rSize ? -1 : 0; |
1388 | if (!rSize) |
1389 | return 1; |
1390 | const qsizetype size = std::min(a: lSize, b: rSize); |
1391 | |
1392 | Q_ASSERT(lhsChar && rhsChar); // since both lSize and rSize are positive |
1393 | for (qsizetype i = 0; i < size; i++) { |
1394 | if (int res = CaseInsensitiveL1::difference(lhs: lhsChar[i], rhs: rhsChar[i])) |
1395 | return res; |
1396 | } |
1397 | return qt_lencmp(lhs: lSize, rhs: rSize); |
1398 | } |
1399 | |
1400 | bool QtPrivate::equalStrings(QStringView lhs, QStringView rhs) noexcept |
1401 | { |
1402 | return ucstreq(a: lhs.utf16(), alen: lhs.size(), b: rhs.utf16(), blen: rhs.size()); |
1403 | } |
1404 | |
1405 | bool QtPrivate::equalStrings(QStringView lhs, QLatin1StringView rhs) noexcept |
1406 | { |
1407 | return ucstreq(a: lhs.utf16(), alen: lhs.size(), b: rhs.latin1(), blen: rhs.size()); |
1408 | } |
1409 | |
1410 | bool QtPrivate::equalStrings(QLatin1StringView lhs, QStringView rhs) noexcept |
1411 | { |
1412 | return QtPrivate::equalStrings(lhs: rhs, rhs: lhs); |
1413 | } |
1414 | |
1415 | bool QtPrivate::equalStrings(QLatin1StringView lhs, QLatin1StringView rhs) noexcept |
1416 | { |
1417 | return QByteArrayView(lhs) == QByteArrayView(rhs); |
1418 | } |
1419 | |
1420 | bool QtPrivate::equalStrings(QBasicUtf8StringView<false> lhs, QStringView rhs) noexcept |
1421 | { |
1422 | return QUtf8::compareUtf8(utf8: lhs, utf16: rhs) == 0; |
1423 | } |
1424 | |
1425 | bool QtPrivate::equalStrings(QStringView lhs, QBasicUtf8StringView<false> rhs) noexcept |
1426 | { |
1427 | return QtPrivate::equalStrings(lhs: rhs, rhs: lhs); |
1428 | } |
1429 | |
1430 | bool QtPrivate::equalStrings(QLatin1StringView lhs, QBasicUtf8StringView<false> rhs) noexcept |
1431 | { |
1432 | return QUtf8::compareUtf8(utf8: QByteArrayView(rhs), s: lhs) == 0; |
1433 | } |
1434 | |
1435 | bool QtPrivate::equalStrings(QBasicUtf8StringView<false> lhs, QLatin1StringView rhs) noexcept |
1436 | { |
1437 | return QtPrivate::equalStrings(lhs: rhs, rhs: lhs); |
1438 | } |
1439 | |
1440 | bool QtPrivate::equalStrings(QBasicUtf8StringView<false> lhs, QBasicUtf8StringView<false> rhs) noexcept |
1441 | { |
1442 | return lhs.size() == rhs.size() && (!lhs.size() || memcmp(s1: lhs.data(), s2: rhs.data(), n: lhs.size()) == 0); |
1443 | } |
1444 | |
1445 | bool QAnyStringView::equal(QAnyStringView lhs, QAnyStringView rhs) noexcept |
1446 | { |
1447 | if (lhs.size() != rhs.size() && lhs.isUtf8() == rhs.isUtf8()) |
1448 | return false; |
1449 | return lhs.visit(v: [rhs](auto lhs) { |
1450 | return rhs.visit([lhs](auto rhs) { |
1451 | return QtPrivate::equalStrings(lhs, rhs); |
1452 | }); |
1453 | }); |
1454 | } |
1455 | |
1456 | /*! |
1457 | \relates QStringView |
1458 | \internal |
1459 | \since 5.10 |
1460 | |
1461 | Returns an integer that compares to 0 as \a lhs compares to \a rhs. |
1462 | |
1463 | \include qstring.qdocinc {search-comparison-case-sensitivity} {comparison} |
1464 | |
1465 | Case-sensitive comparison is based exclusively on the numeric Unicode values |
1466 | of the characters and is very fast, but is not what a human would expect. |
1467 | Consider sorting user-visible strings with QString::localeAwareCompare(). |
1468 | |
1469 | \sa {Comparing Strings} |
1470 | */ |
1471 | int QtPrivate::compareStrings(QStringView lhs, QStringView rhs, Qt::CaseSensitivity cs) noexcept |
1472 | { |
1473 | if (cs == Qt::CaseSensitive) |
1474 | return ucstrcmp(a: lhs.utf16(), alen: lhs.size(), b: rhs.utf16(), blen: rhs.size()); |
1475 | return ucstricmp(alen: lhs.size(), a: lhs.utf16(), blen: rhs.size(), b: rhs.utf16()); |
1476 | } |
1477 | |
1478 | /*! |
1479 | \relates QStringView |
1480 | \internal |
1481 | \since 5.10 |
1482 | \overload |
1483 | |
1484 | Returns an integer that compares to 0 as \a lhs compares to \a rhs. |
1485 | |
1486 | \include qstring.qdocinc {search-comparison-case-sensitivity} {comparison} |
1487 | |
1488 | Case-sensitive comparison is based exclusively on the numeric Unicode values |
1489 | of the characters and is very fast, but is not what a human would expect. |
1490 | Consider sorting user-visible strings with QString::localeAwareCompare(). |
1491 | |
1492 | \sa {Comparing Strings} |
1493 | */ |
1494 | int QtPrivate::compareStrings(QStringView lhs, QLatin1StringView rhs, Qt::CaseSensitivity cs) noexcept |
1495 | { |
1496 | if (cs == Qt::CaseSensitive) |
1497 | return ucstrcmp(a: lhs.utf16(), alen: lhs.size(), b: rhs.latin1(), blen: rhs.size()); |
1498 | return ucstricmp(alen: lhs.size(), a: lhs.utf16(), blen: rhs.size(), b: rhs.latin1()); |
1499 | } |
1500 | |
1501 | /*! |
1502 | \relates QStringView |
1503 | \internal |
1504 | \since 6.0 |
1505 | \overload |
1506 | */ |
1507 | int QtPrivate::compareStrings(QStringView lhs, QBasicUtf8StringView<false> rhs, Qt::CaseSensitivity cs) noexcept |
1508 | { |
1509 | return -compareStrings(lhs: rhs, rhs: lhs, cs); |
1510 | } |
1511 | |
1512 | /*! |
1513 | \relates QStringView |
1514 | \internal |
1515 | \since 5.10 |
1516 | \overload |
1517 | */ |
1518 | int QtPrivate::compareStrings(QLatin1StringView lhs, QStringView rhs, Qt::CaseSensitivity cs) noexcept |
1519 | { |
1520 | return -compareStrings(lhs: rhs, rhs: lhs, cs); |
1521 | } |
1522 | |
1523 | /*! |
1524 | \relates QStringView |
1525 | \internal |
1526 | \since 5.10 |
1527 | \overload |
1528 | |
1529 | Returns an integer that compares to 0 as \a lhs compares to \a rhs. |
1530 | |
1531 | \include qstring.qdocinc {search-comparison-case-sensitivity} {comparison} |
1532 | |
1533 | Case-sensitive comparison is based exclusively on the numeric Latin-1 values |
1534 | of the characters and is very fast, but is not what a human would expect. |
1535 | Consider sorting user-visible strings with QString::localeAwareCompare(). |
1536 | |
1537 | \sa {Comparing Strings} |
1538 | */ |
1539 | int QtPrivate::compareStrings(QLatin1StringView lhs, QLatin1StringView rhs, Qt::CaseSensitivity cs) noexcept |
1540 | { |
1541 | if (lhs.isEmpty()) |
1542 | return qt_lencmp(lhs: qsizetype(0), rhs: rhs.size()); |
1543 | if (cs == Qt::CaseInsensitive) |
1544 | return latin1nicmp(lhsChar: lhs.data(), lSize: lhs.size(), rhsChar: rhs.data(), rSize: rhs.size()); |
1545 | const auto l = std::min(a: lhs.size(), b: rhs.size()); |
1546 | int r = memcmp(s1: lhs.data(), s2: rhs.data(), n: l); |
1547 | return r ? r : qt_lencmp(lhs: lhs.size(), rhs: rhs.size()); |
1548 | } |
1549 | |
1550 | /*! |
1551 | \relates QStringView |
1552 | \internal |
1553 | \since 6.0 |
1554 | \overload |
1555 | */ |
1556 | int QtPrivate::compareStrings(QLatin1StringView lhs, QBasicUtf8StringView<false> rhs, Qt::CaseSensitivity cs) noexcept |
1557 | { |
1558 | return -QUtf8::compareUtf8(utf8: QByteArrayView(rhs), s: lhs, cs); |
1559 | } |
1560 | |
1561 | /*! |
1562 | \relates QStringView |
1563 | \internal |
1564 | \since 6.0 |
1565 | \overload |
1566 | */ |
1567 | int QtPrivate::compareStrings(QBasicUtf8StringView<false> lhs, QStringView rhs, Qt::CaseSensitivity cs) noexcept |
1568 | { |
1569 | if (cs == Qt::CaseSensitive) |
1570 | return QUtf8::compareUtf8(utf8: lhs, utf16: rhs); |
1571 | return ucstricmp8(utf8: lhs.begin(), utf8end: lhs.end(), utf16: rhs.begin(), utf16end: rhs.end()); |
1572 | } |
1573 | |
1574 | /*! |
1575 | \relates QStringView |
1576 | \internal |
1577 | \since 6.0 |
1578 | \overload |
1579 | */ |
1580 | int QtPrivate::compareStrings(QBasicUtf8StringView<false> lhs, QLatin1StringView rhs, Qt::CaseSensitivity cs) noexcept |
1581 | { |
1582 | return -compareStrings(lhs: rhs, rhs: lhs, cs); |
1583 | } |
1584 | |
1585 | /*! |
1586 | \relates QStringView |
1587 | \internal |
1588 | \since 6.0 |
1589 | \overload |
1590 | */ |
1591 | int QtPrivate::compareStrings(QBasicUtf8StringView<false> lhs, QBasicUtf8StringView<false> rhs, Qt::CaseSensitivity cs) noexcept |
1592 | { |
1593 | return QUtf8::compareUtf8(lhs: QByteArrayView(lhs), rhs: QByteArrayView(rhs), cs); |
1594 | } |
1595 | |
1596 | int QAnyStringView::compare(QAnyStringView lhs, QAnyStringView rhs, Qt::CaseSensitivity cs) noexcept |
1597 | { |
1598 | return lhs.visit(v: [rhs, cs](auto lhs) { |
1599 | return rhs.visit([lhs, cs](auto rhs) { |
1600 | return QtPrivate::compareStrings(lhs, rhs, cs); |
1601 | }); |
1602 | }); |
1603 | } |
1604 | |
1605 | // ### Qt 7: do not allow anything but ASCII digits |
1606 | // in arg()'s replacements. |
1607 | #if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) |
1608 | static bool supportUnicodeDigitValuesInArg() |
1609 | { |
1610 | static const bool result = []() { |
1611 | static const char supportUnicodeDigitValuesEnvVar[] |
1612 | = "QT_USE_UNICODE_DIGIT_VALUES_IN_STRING_ARG" ; |
1613 | |
1614 | if (qEnvironmentVariableIsSet(varName: supportUnicodeDigitValuesEnvVar)) |
1615 | return qEnvironmentVariableIntValue(varName: supportUnicodeDigitValuesEnvVar) != 0; |
1616 | |
1617 | #if QT_VERSION < QT_VERSION_CHECK(6, 6, 0) // keep it in sync with the test |
1618 | return true; |
1619 | #else |
1620 | return false; |
1621 | #endif |
1622 | }(); |
1623 | |
1624 | return result; |
1625 | } |
1626 | #endif |
1627 | |
1628 | static int qArgDigitValue(QChar ch) noexcept |
1629 | { |
1630 | #if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) |
1631 | if (supportUnicodeDigitValuesInArg()) |
1632 | return ch.digitValue(); |
1633 | #endif |
1634 | if (ch >= u'0' && ch <= u'9') |
1635 | return int(ch.unicode() - u'0'); |
1636 | return -1; |
1637 | } |
1638 | |
1639 | #if QT_CONFIG(regularexpression) |
1640 | Q_DECL_COLD_FUNCTION |
1641 | void qtWarnAboutInvalidRegularExpression(const QString &pattern, const char *where); |
1642 | #endif |
1643 | |
1644 | /*! |
1645 | \macro QT_RESTRICTED_CAST_FROM_ASCII |
1646 | \relates QString |
1647 | |
1648 | Disables most automatic conversions from source literals and 8-bit data |
1649 | to unicode QStrings, but allows the use of |
1650 | the \c{QChar(char)} and \c{QString(const char (&ch)[N]} constructors, |
1651 | and the \c{QString::operator=(const char (&ch)[N])} assignment operator. |
1652 | This gives most of the type-safety benefits of \l QT_NO_CAST_FROM_ASCII |
1653 | but does not require user code to wrap character and string literals |
1654 | with QLatin1Char, QLatin1StringView or similar. |
1655 | |
1656 | Using this macro together with source strings outside the 7-bit range, |
1657 | non-literals, or literals with embedded NUL characters is undefined. |
1658 | |
1659 | \sa QT_NO_CAST_FROM_ASCII, QT_NO_CAST_TO_ASCII |
1660 | */ |
1661 | |
1662 | /*! |
1663 | \macro QT_NO_CAST_FROM_ASCII |
1664 | \relates QString |
1665 | \relates QChar |
1666 | |
1667 | Disables automatic conversions from 8-bit strings (\c{char *}) to Unicode |
1668 | QStrings, as well as from 8-bit \c{char} types (\c{char} and |
1669 | \c{unsigned char}) to QChar. |
1670 | |
1671 | \sa QT_NO_CAST_TO_ASCII, QT_RESTRICTED_CAST_FROM_ASCII, |
1672 | QT_NO_CAST_FROM_BYTEARRAY |
1673 | */ |
1674 | |
1675 | /*! |
1676 | \macro QT_NO_CAST_TO_ASCII |
1677 | \relates QString |
1678 | |
1679 | Disables automatic conversion from QString to 8-bit strings (\c{char *}). |
1680 | |
1681 | \sa QT_NO_CAST_FROM_ASCII, QT_RESTRICTED_CAST_FROM_ASCII, |
1682 | QT_NO_CAST_FROM_BYTEARRAY |
1683 | */ |
1684 | |
1685 | /*! |
1686 | \macro QT_ASCII_CAST_WARNINGS |
1687 | \internal |
1688 | \relates QString |
1689 | |
1690 | This macro can be defined to force a warning whenever a function is |
1691 | called that automatically converts between unicode and 8-bit encodings. |
1692 | |
1693 | Note: This only works for compilers that support warnings for |
1694 | deprecated API. |
1695 | |
1696 | \sa QT_NO_CAST_TO_ASCII, QT_NO_CAST_FROM_ASCII, QT_RESTRICTED_CAST_FROM_ASCII |
1697 | */ |
1698 | |
1699 | /*! |
1700 | \class QString |
1701 | \inmodule QtCore |
1702 | \reentrant |
1703 | |
1704 | \brief The QString class provides a Unicode character string. |
1705 | |
1706 | \ingroup tools |
1707 | \ingroup shared |
1708 | \ingroup string-processing |
1709 | |
1710 | QString stores a string of 16-bit \l{QChar}s, where each QChar |
1711 | corresponds to one UTF-16 code unit. (Unicode characters |
1712 | with code values above 65535 are stored using surrogate pairs, |
1713 | i.e., two consecutive \l{QChar}s.) |
1714 | |
1715 | \l{Unicode} is an international standard that supports most of the |
1716 | writing systems in use today. It is a superset of US-ASCII (ANSI |
1717 | X3.4-1986) and Latin-1 (ISO 8859-1), and all the US-ASCII/Latin-1 |
1718 | characters are available at the same code positions. |
1719 | |
1720 | Behind the scenes, QString uses \l{implicit sharing} |
1721 | (copy-on-write) to reduce memory usage and to avoid the needless |
1722 | copying of data. This also helps reduce the inherent overhead of |
1723 | storing 16-bit characters instead of 8-bit characters. |
1724 | |
1725 | In addition to QString, Qt also provides the QByteArray class to |
1726 | store raw bytes and traditional 8-bit '\\0'-terminated strings. |
1727 | For most purposes, QString is the class you want to use. It is |
1728 | used throughout the Qt API, and the Unicode support ensures that |
1729 | your applications will be easy to translate if you want to expand |
1730 | your application's market at some point. The two main cases where |
1731 | QByteArray is appropriate are when you need to store raw binary |
1732 | data, and when memory conservation is critical (like in embedded |
1733 | systems). |
1734 | |
1735 | \tableofcontents |
1736 | |
1737 | \section1 Initializing a String |
1738 | |
1739 | One way to initialize a QString is simply to pass a \c{const char |
1740 | *} to its constructor. For example, the following code creates a |
1741 | QString of size 5 containing the data "Hello": |
1742 | |
1743 | \snippet qstring/main.cpp 0 |
1744 | |
1745 | QString converts the \c{const char *} data into Unicode using the |
1746 | fromUtf8() function. |
1747 | |
1748 | In all of the QString functions that take \c{const char *} |
1749 | parameters, the \c{const char *} is interpreted as a classic |
1750 | C-style '\\0'-terminated string encoded in UTF-8. It is legal for |
1751 | the \c{const char *} parameter to be \nullptr. |
1752 | |
1753 | You can also provide string data as an array of \l{QChar}s: |
1754 | |
1755 | \snippet qstring/main.cpp 1 |
1756 | |
1757 | QString makes a deep copy of the QChar data, so you can modify it |
1758 | later without experiencing side effects. (If for performance |
1759 | reasons you don't want to take a deep copy of the character data, |
1760 | use QString::fromRawData() instead.) |
1761 | |
1762 | Another approach is to set the size of the string using resize() |
1763 | and to initialize the data character per character. QString uses |
1764 | 0-based indexes, just like C++ arrays. To access the character at |
1765 | a particular index position, you can use \l operator[](). On |
1766 | non-\c{const} strings, \l operator[]() returns a reference to a |
1767 | character that can be used on the left side of an assignment. For |
1768 | example: |
1769 | |
1770 | \snippet qstring/main.cpp 2 |
1771 | |
1772 | For read-only access, an alternative syntax is to use the at() |
1773 | function: |
1774 | |
1775 | \snippet qstring/main.cpp 3 |
1776 | |
1777 | The at() function can be faster than \l operator[](), because it |
1778 | never causes a \l{deep copy} to occur. Alternatively, use the |
1779 | first(), last(), or sliced() functions to extract several characters |
1780 | at a time. |
1781 | |
1782 | A QString can embed '\\0' characters (QChar::Null). The size() |
1783 | function always returns the size of the whole string, including |
1784 | embedded '\\0' characters. |
1785 | |
1786 | After a call to the resize() function, newly allocated characters |
1787 | have undefined values. To set all the characters in the string to |
1788 | a particular value, use the fill() function. |
1789 | |
1790 | QString provides dozens of overloads designed to simplify string |
1791 | usage. For example, if you want to compare a QString with a string |
1792 | literal, you can write code like this and it will work as expected: |
1793 | |
1794 | \snippet qstring/main.cpp 4 |
1795 | |
1796 | You can also pass string literals to functions that take QStrings |
1797 | as arguments, invoking the QString(const char *) |
1798 | constructor. Similarly, you can pass a QString to a function that |
1799 | takes a \c{const char *} argument using the \l qPrintable() macro |
1800 | which returns the given QString as a \c{const char *}. This is |
1801 | equivalent to calling <QString>.toLocal8Bit().constData(). |
1802 | |
1803 | \section1 Manipulating String Data |
1804 | |
1805 | QString provides the following basic functions for modifying the |
1806 | character data: append(), prepend(), insert(), replace(), and |
1807 | remove(). For example: |
1808 | |
1809 | \snippet qstring/main.cpp 5 |
1810 | |
1811 | In the above example the replace() function's first two arguments are the |
1812 | position from which to start replacing and the number of characters that |
1813 | should be replaced. |
1814 | |
1815 | When data-modifying functions increase the size of the string, |
1816 | they may lead to reallocation of memory for the QString object. When |
1817 | this happens, QString expands by more than it immediately needs so as |
1818 | to have space for further expansion without reallocation until the size |
1819 | of the string has greatly increased. |
1820 | |
1821 | The insert(), remove() and, when replacing a sub-string with one of |
1822 | different size, replace() functions can be slow (\l{linear time}) for |
1823 | large strings, because they require moving many characters in the string |
1824 | by at least one position in memory. |
1825 | |
1826 | If you are building a QString gradually and know in advance |
1827 | approximately how many characters the QString will contain, you |
1828 | can call reserve(), asking QString to preallocate a certain amount |
1829 | of memory. You can also call capacity() to find out how much |
1830 | memory the QString actually has allocated. |
1831 | |
1832 | QString provides \l{STL-style iterators} (QString::const_iterator and |
1833 | QString::iterator). In practice, iterators are handy when working with |
1834 | generic algorithms provided by the C++ standard library. |
1835 | |
1836 | \note Iterators over a QString, and references to individual characters |
1837 | within one, cannot be relied on to remain valid when any non-\c{const} |
1838 | method of the QString is called. Accessing such an iterator or reference |
1839 | after the call to a non-\c{const} method leads to undefined behavior. When |
1840 | stability for iterator-like functionality is required, you should use |
1841 | indexes instead of iterators as they are not tied to QString's internal |
1842 | state and thus do not get invalidated. |
1843 | |
1844 | \note Due to \l{implicit sharing}, the first non-\c{const} operator or |
1845 | function used on a given QString may cause it to, internally, perform a deep |
1846 | copy of its data. This invalidates all iterators over the string and |
1847 | references to individual characters within it. After the first non-\c{const} |
1848 | operator, operations that modify QString may completely (in case of |
1849 | reallocation) or partially invalidate iterators and references, but other |
1850 | methods (such as begin() or end()) will not. Accessing an iterator or |
1851 | reference after it has been invalidated leads to undefined behavior. |
1852 | |
1853 | A frequent requirement is to remove whitespace characters from a |
1854 | string ('\\n', '\\t', ' ', etc.). If you want to remove whitespace |
1855 | from both ends of a QString, use the trimmed() function. If you |
1856 | want to remove whitespace from both ends and replace multiple |
1857 | consecutive whitespaces with a single space character within the |
1858 | string, use simplified(). |
1859 | |
1860 | If you want to find all occurrences of a particular character or |
1861 | substring in a QString, use the indexOf() or lastIndexOf() |
1862 | functions. The former searches forward starting from a given index |
1863 | position, the latter searches backward. Both return the index |
1864 | position of the character or substring if they find it; otherwise, |
1865 | they return -1. For example, here is a typical loop that finds all |
1866 | occurrences of a particular substring: |
1867 | |
1868 | \snippet qstring/main.cpp 6 |
1869 | |
1870 | QString provides many functions for converting numbers into |
1871 | strings and strings into numbers. See the arg() functions, the |
1872 | setNum() functions, the number() static functions, and the |
1873 | toInt(), toDouble(), and similar functions. |
1874 | |
1875 | To get an upper- or lowercase version of a string use toUpper() or |
1876 | toLower(). |
1877 | |
1878 | Lists of strings are handled by the QStringList class. You can |
1879 | split a string into a list of strings using the split() function, |
1880 | and join a list of strings into a single string with an optional |
1881 | separator using QStringList::join(). You can obtain a list of |
1882 | strings from a string list that contain a particular substring or |
1883 | that match a particular QRegularExpression using the QStringList::filter() |
1884 | function. |
1885 | |
1886 | \section1 Querying String Data |
1887 | |
1888 | If you want to see if a QString starts or ends with a particular |
1889 | substring use startsWith() or endsWith(). If you simply want to |
1890 | check whether a QString contains a particular character or |
1891 | substring, use the contains() function. If you want to find out |
1892 | how many times a particular character or substring occurs in the |
1893 | string, use count(). |
1894 | |
1895 | To obtain a pointer to the actual character data, call data() or |
1896 | constData(). These functions return a pointer to the beginning of |
1897 | the QChar data. The pointer is guaranteed to remain valid until a |
1898 | non-\c{const} function is called on the QString. |
1899 | |
1900 | \section2 Comparing Strings |
1901 | |
1902 | QStrings can be compared using overloaded operators such as \l |
1903 | operator<(), \l operator<=(), \l operator==(), \l operator>=(), |
1904 | and so on. Note that the comparison is based exclusively on the |
1905 | numeric Unicode values of the characters. It is very fast, but is |
1906 | not what a human would expect; the QString::localeAwareCompare() |
1907 | function is usually a better choice for sorting user-interface |
1908 | strings, when such a comparison is available. |
1909 | |
1910 | On Unix-like platforms (including Linux, \macos and iOS), when Qt |
1911 | is linked with the ICU library (which it usually is), its |
1912 | locale-aware sorting is used. Otherwise, on \macos and iOS, \l |
1913 | localeAwareCompare() compares according the "Order for sorted |
1914 | lists" setting in the International preferences panel. On other |
1915 | Unix-like systems without ICU, the comparison falls back to the |
1916 | system library's \c strcoll(), |
1917 | |
1918 | \section1 Converting Between Encoded Strings Data and QString |
1919 | |
1920 | QString provides the following three functions that return a |
1921 | \c{const char *} version of the string as QByteArray: toUtf8(), |
1922 | toLatin1(), and toLocal8Bit(). |
1923 | |
1924 | \list |
1925 | \li toLatin1() returns a Latin-1 (ISO 8859-1) encoded 8-bit string. |
1926 | \li toUtf8() returns a UTF-8 encoded 8-bit string. UTF-8 is a |
1927 | superset of US-ASCII (ANSI X3.4-1986) that supports the entire |
1928 | Unicode character set through multibyte sequences. |
1929 | \li toLocal8Bit() returns an 8-bit string using the system's local |
1930 | encoding. This is the same as toUtf8() on Unix systems. |
1931 | \endlist |
1932 | |
1933 | To convert from one of these encodings, QString provides |
1934 | fromLatin1(), fromUtf8(), and fromLocal8Bit(). Other |
1935 | encodings are supported through the QStringEncoder and QStringDecoder |
1936 | classes. |
1937 | |
1938 | As mentioned above, QString provides a lot of functions and |
1939 | operators that make it easy to interoperate with \c{const char *} |
1940 | strings. But this functionality is a double-edged sword: It makes |
1941 | QString more convenient to use if all strings are US-ASCII or |
1942 | Latin-1, but there is always the risk that an implicit conversion |
1943 | from or to \c{const char *} is done using the wrong 8-bit |
1944 | encoding. To minimize these risks, you can turn off these implicit |
1945 | conversions by defining some of the following preprocessor symbols: |
1946 | |
1947 | \list |
1948 | \li \l QT_NO_CAST_FROM_ASCII disables automatic conversions from |
1949 | C string literals and pointers to Unicode. |
1950 | \li \l QT_RESTRICTED_CAST_FROM_ASCII allows automatic conversions |
1951 | from C characters and character arrays, but disables automatic |
1952 | conversions from character pointers to Unicode. |
1953 | \li \l QT_NO_CAST_TO_ASCII disables automatic conversion from QString |
1954 | to C strings. |
1955 | \endlist |
1956 | |
1957 | You then need to explicitly call fromUtf8(), fromLatin1(), |
1958 | or fromLocal8Bit() to construct a QString from an |
1959 | 8-bit string, or use the lightweight QLatin1StringView class, for |
1960 | example: |
1961 | |
1962 | \snippet code/src_corelib_text_qstring.cpp 1 |
1963 | |
1964 | Similarly, you must call toLatin1(), toUtf8(), or |
1965 | toLocal8Bit() explicitly to convert the QString to an 8-bit |
1966 | string. |
1967 | |
1968 | \table 100 % |
1969 | \header |
1970 | \li Note for C Programmers |
1971 | |
1972 | \row |
1973 | \li |
1974 | Due to C++'s type system and the fact that QString is |
1975 | \l{implicitly shared}, QStrings may be treated like \c{int}s or |
1976 | other basic types. For example: |
1977 | |
1978 | \snippet qstring/main.cpp 7 |
1979 | |
1980 | The \c result variable, is a normal variable allocated on the |
1981 | stack. When \c return is called, and because we're returning by |
1982 | value, the copy constructor is called and a copy of the string is |
1983 | returned. No actual copying takes place thanks to the implicit |
1984 | sharing. |
1985 | |
1986 | \endtable |
1987 | |
1988 | \section1 Distinction Between Null and Empty Strings |
1989 | |
1990 | For historical reasons, QString distinguishes between a null |
1991 | string and an empty string. A \e null string is a string that is |
1992 | initialized using QString's default constructor or by passing |
1993 | (\c{const char *})0 to the constructor. An \e empty string is any |
1994 | string with size 0. A null string is always empty, but an empty |
1995 | string isn't necessarily null: |
1996 | |
1997 | \snippet qstring/main.cpp 8 |
1998 | |
1999 | All functions except isNull() treat null strings the same as empty |
2000 | strings. For example, toUtf8().constData() returns a valid pointer |
2001 | (\e not nullptr) to a '\\0' character for a null string. We |
2002 | recommend that you always use the isEmpty() function and avoid isNull(). |
2003 | |
2004 | \section1 Number Formats |
2005 | |
2006 | When a QString::arg() \c{'%'} format specifier includes the \c{'L'} locale |
2007 | qualifier, and the base is ten (its default), the default locale is |
2008 | used. This can be set using \l{QLocale::setDefault()}. For more refined |
2009 | control of localized string representations of numbers, see |
2010 | QLocale::toString(). All other number formatting done by QString follows the |
2011 | C locale's representation of numbers. |
2012 | |
2013 | When QString::arg() applies left-padding to numbers, the fill character |
2014 | \c{'0'} is treated specially. If the number is negative, its minus sign will |
2015 | appear before the zero-padding. If the field is localized, the |
2016 | locale-appropriate zero character is used in place of \c{'0'}. For |
2017 | floating-point numbers, this special treatment only applies if the number is |
2018 | finite. |
2019 | |
2020 | \section2 Floating-point Formats |
2021 | |
2022 | In member functions (e.g., arg(), number()) that represent floating-point |
2023 | numbers (\c float or \c double) as strings, the form of display can be |
2024 | controlled by a choice of \e format and \e precision, whose meanings are as |
2025 | for \l {QLocale::toString(double, char, int)}. |
2026 | |
2027 | If the selected \e format includes an exponent, localized forms follow the |
2028 | locale's convention on digits in the exponent. For non-localized formatting, |
2029 | the exponent shows its sign and includes at least two digits, left-padding |
2030 | with zero if needed. |
2031 | |
2032 | \section1 More Efficient String Construction |
2033 | |
2034 | Many strings are known at compile time. But the trivial |
2035 | constructor QString("Hello"), will copy the contents of the string, |
2036 | treating the contents as Latin-1. To avoid this, one can use the |
2037 | QStringLiteral macro to directly create the required data at compile |
2038 | time. Constructing a QString out of the literal does then not cause |
2039 | any overhead at runtime. |
2040 | |
2041 | A slightly less efficient way is to use QLatin1StringView. This class wraps |
2042 | a C string literal, precalculates it length at compile time and can |
2043 | then be used for faster comparison with QStrings and conversion to |
2044 | QStrings than a regular C string literal. |
2045 | |
2046 | Using the QString \c{'+'} operator, it is easy to construct a |
2047 | complex string from multiple substrings. You will often write code |
2048 | like this: |
2049 | |
2050 | \snippet qstring/stringbuilder.cpp 0 |
2051 | |
2052 | There is nothing wrong with either of these string constructions, |
2053 | but there are a few hidden inefficiencies. Beginning with Qt 4.6, |
2054 | you can eliminate them. |
2055 | |
2056 | First, multiple uses of the \c{'+'} operator usually means |
2057 | multiple memory allocations. When concatenating \e{n} substrings, |
2058 | where \e{n > 2}, there can be as many as \e{n - 1} calls to the |
2059 | memory allocator. |
2060 | |
2061 | In 4.6, an internal template class \c{QStringBuilder} has been |
2062 | added along with a few helper functions. This class is marked |
2063 | internal and does not appear in the documentation, because you |
2064 | aren't meant to instantiate it in your code. Its use will be |
2065 | automatic, as described below. The class is found in |
2066 | \c {src/corelib/tools/qstringbuilder.cpp} if you want to have a |
2067 | look at it. |
2068 | |
2069 | \c{QStringBuilder} uses expression templates and reimplements the |
2070 | \c{'%'} operator so that when you use \c{'%'} for string |
2071 | concatenation instead of \c{'+'}, multiple substring |
2072 | concatenations will be postponed until the final result is about |
2073 | to be assigned to a QString. At this point, the amount of memory |
2074 | required for the final result is known. The memory allocator is |
2075 | then called \e{once} to get the required space, and the substrings |
2076 | are copied into it one by one. |
2077 | |
2078 | Additional efficiency is gained by inlining and reduced reference |
2079 | counting (the QString created from a \c{QStringBuilder} typically |
2080 | has a ref count of 1, whereas QString::append() needs an extra |
2081 | test). |
2082 | |
2083 | There are two ways you can access this improved method of string |
2084 | construction. The straightforward way is to include |
2085 | \c{QStringBuilder} wherever you want to use it, and use the |
2086 | \c{'%'} operator instead of \c{'+'} when concatenating strings: |
2087 | |
2088 | \snippet qstring/stringbuilder.cpp 5 |
2089 | |
2090 | A more global approach, which is more convenient but not entirely source |
2091 | compatible, is to define \c QT_USE_QSTRINGBUILDER (by adding it to the compiler |
2092 | flags) at build time. This will make concatenating strings with \c{'+'} work the |
2093 | same way as \c{QStringBuilder} \c{'%'}. |
2094 | |
2095 | \note Take care when using the \c auto keyword with the result of |
2096 | string concatenation using QStringBuilder: |
2097 | \snippet qstring/stringbuilder.cpp 6 |
2098 | |
2099 | Typically this is not what is expected (and can result in undefined behavior). |
2100 | This issue can be fixed by specifying the return type: |
2101 | \snippet qstring/stringbuilder.cpp 7 |
2102 | |
2103 | \note \l {https://invent.kde.org/sdk/clazy} {Clazy} has a check, auto-unexpected-qstringbuilder, |
2104 | that catches this issue. |
2105 | |
2106 | \section1 Maximum Size and Out-of-memory Conditions |
2107 | |
2108 | The maximum size of QString depends on the architecture. Most 64-bit |
2109 | systems can allocate more than 2 GB of memory, with a typical limit |
2110 | of 2^63 bytes. The actual value also depends on the overhead required for |
2111 | managing the data block. As a result, you can expect the maximum size |
2112 | of 2 GB minus overhead on 32-bit platforms, and 2^63 bytes minus overhead |
2113 | on 64-bit platforms. The number of elements that can be stored in a |
2114 | QString is this maximum size divided by the size of QChar. |
2115 | |
2116 | When memory allocation fails, QString throws a \c std::bad_alloc |
2117 | exception if the application was compiled with exception support. |
2118 | Out of memory conditions in Qt containers are the only case where Qt |
2119 | will throw exceptions. If exceptions are disabled, then running out of |
2120 | memory is undefined behavior. |
2121 | |
2122 | Note that the operating system may impose further limits on applications |
2123 | holding a lot of allocated memory, especially large, contiguous blocks. |
2124 | Such considerations, the configuration of such behavior or any mitigation |
2125 | are outside the scope of the Qt API. |
2126 | |
2127 | \sa fromRawData(), QChar, QStringView, QLatin1StringView, QByteArray |
2128 | */ |
2129 | |
2130 | /*! \typedef QString::ConstIterator |
2131 | |
2132 | Qt-style synonym for QString::const_iterator. |
2133 | */ |
2134 | |
2135 | /*! \typedef QString::Iterator |
2136 | |
2137 | Qt-style synonym for QString::iterator. |
2138 | */ |
2139 | |
2140 | /*! \typedef QString::const_iterator |
2141 | |
2142 | \sa QString::iterator |
2143 | */ |
2144 | |
2145 | /*! \typedef QString::iterator |
2146 | |
2147 | \sa QString::const_iterator |
2148 | */ |
2149 | |
2150 | /*! \typedef QString::const_reverse_iterator |
2151 | \since 5.6 |
2152 | |
2153 | \sa QString::reverse_iterator, QString::const_iterator |
2154 | */ |
2155 | |
2156 | /*! \typedef QString::reverse_iterator |
2157 | \since 5.6 |
2158 | |
2159 | \sa QString::const_reverse_iterator, QString::iterator |
2160 | */ |
2161 | |
2162 | /*! |
2163 | \typedef QString::size_type |
2164 | */ |
2165 | |
2166 | /*! |
2167 | \typedef QString::difference_type |
2168 | */ |
2169 | |
2170 | /*! |
2171 | \typedef QString::const_reference |
2172 | */ |
2173 | /*! |
2174 | \typedef QString::reference |
2175 | */ |
2176 | |
2177 | /*! |
2178 | \typedef QString::const_pointer |
2179 | |
2180 | The QString::const_pointer typedef provides an STL-style |
2181 | const pointer to a QString element (QChar). |
2182 | */ |
2183 | /*! |
2184 | \typedef QString::pointer |
2185 | |
2186 | The QString::pointer typedef provides an STL-style |
2187 | pointer to a QString element (QChar). |
2188 | */ |
2189 | |
2190 | /*! |
2191 | \typedef QString::value_type |
2192 | */ |
2193 | |
2194 | /*! \fn QString::iterator QString::begin() |
2195 | |
2196 | Returns an \l{STL-style iterators}{STL-style iterator} pointing to the |
2197 | first character in the string. |
2198 | |
2199 | //! [iterator-invalidation-func-desc] |
2200 | \warning The returned iterator is invalidated on detachment or when the |
2201 | QString is modified. |
2202 | //! [iterator-invalidation-func-desc] |
2203 | |
2204 | \sa constBegin(), end() |
2205 | */ |
2206 | |
2207 | /*! \fn QString::const_iterator QString::begin() const |
2208 | |
2209 | \overload begin() |
2210 | */ |
2211 | |
2212 | /*! \fn QString::const_iterator QString::cbegin() const |
2213 | \since 5.0 |
2214 | |
2215 | Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the |
2216 | first character in the string. |
2217 | |
2218 | \include qstring.cpp iterator-invalidation-func-desc |
2219 | |
2220 | \sa begin(), cend() |
2221 | */ |
2222 | |
2223 | /*! \fn QString::const_iterator QString::constBegin() const |
2224 | |
2225 | Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the |
2226 | first character in the string. |
2227 | |
2228 | \include qstring.cpp iterator-invalidation-func-desc |
2229 | |
2230 | \sa begin(), constEnd() |
2231 | */ |
2232 | |
2233 | /*! \fn QString::iterator QString::end() |
2234 | |
2235 | Returns an \l{STL-style iterators}{STL-style iterator} pointing just after |
2236 | the last character in the string. |
2237 | |
2238 | \include qstring.cpp iterator-invalidation-func-desc |
2239 | |
2240 | \sa begin(), constEnd() |
2241 | */ |
2242 | |
2243 | /*! \fn QString::const_iterator QString::end() const |
2244 | |
2245 | \overload end() |
2246 | */ |
2247 | |
2248 | /*! \fn QString::const_iterator QString::cend() const |
2249 | \since 5.0 |
2250 | |
2251 | Returns a const \l{STL-style iterators}{STL-style iterator} pointing just |
2252 | after the last character in the string. |
2253 | |
2254 | \include qstring.cpp iterator-invalidation-func-desc |
2255 | |
2256 | \sa cbegin(), end() |
2257 | */ |
2258 | |
2259 | /*! \fn QString::const_iterator QString::constEnd() const |
2260 | |
2261 | Returns a const \l{STL-style iterators}{STL-style iterator} pointing just |
2262 | after the last character in the string. |
2263 | |
2264 | \include qstring.cpp iterator-invalidation-func-desc |
2265 | |
2266 | \sa constBegin(), end() |
2267 | */ |
2268 | |
2269 | /*! \fn QString::reverse_iterator QString::rbegin() |
2270 | \since 5.6 |
2271 | |
2272 | Returns a \l{STL-style iterators}{STL-style} reverse iterator pointing to |
2273 | the first character in the string, in reverse order. |
2274 | |
2275 | \include qstring.cpp iterator-invalidation-func-desc |
2276 | |
2277 | \sa begin(), crbegin(), rend() |
2278 | */ |
2279 | |
2280 | /*! \fn QString::const_reverse_iterator QString::rbegin() const |
2281 | \since 5.6 |
2282 | \overload |
2283 | */ |
2284 | |
2285 | /*! \fn QString::const_reverse_iterator QString::crbegin() const |
2286 | \since 5.6 |
2287 | |
2288 | Returns a const \l{STL-style iterators}{STL-style} reverse iterator |
2289 | pointing to the first character in the string, in reverse order. |
2290 | |
2291 | \include qstring.cpp iterator-invalidation-func-desc |
2292 | |
2293 | \sa begin(), rbegin(), rend() |
2294 | */ |
2295 | |
2296 | /*! \fn QString::reverse_iterator QString::rend() |
2297 | \since 5.6 |
2298 | |
2299 | Returns a \l{STL-style iterators}{STL-style} reverse iterator pointing just |
2300 | after the last character in the string, in reverse order. |
2301 | |
2302 | \include qstring.cpp iterator-invalidation-func-desc |
2303 | |
2304 | \sa end(), crend(), rbegin() |
2305 | */ |
2306 | |
2307 | /*! \fn QString::const_reverse_iterator QString::rend() const |
2308 | \since 5.6 |
2309 | \overload |
2310 | */ |
2311 | |
2312 | /*! \fn QString::const_reverse_iterator QString::crend() const |
2313 | \since 5.6 |
2314 | |
2315 | Returns a const \l{STL-style iterators}{STL-style} reverse iterator |
2316 | pointing just after the last character in the string, in reverse order. |
2317 | |
2318 | \include qstring.cpp iterator-invalidation-func-desc |
2319 | |
2320 | \sa end(), rend(), rbegin() |
2321 | */ |
2322 | |
2323 | /*! |
2324 | \fn QString::QString() |
2325 | |
2326 | Constructs a null string. Null strings are also considered empty. |
2327 | |
2328 | \sa isEmpty(), isNull(), {Distinction Between Null and Empty Strings} |
2329 | */ |
2330 | |
2331 | /*! |
2332 | \fn QString::QString(QString &&other) |
2333 | |
2334 | Move-constructs a QString instance, making it point at the same |
2335 | object that \a other was pointing to. |
2336 | |
2337 | \since 5.2 |
2338 | */ |
2339 | |
2340 | /*! \fn QString::QString(const char *str) |
2341 | |
2342 | Constructs a string initialized with the 8-bit string \a str. The |
2343 | given const char pointer is converted to Unicode using the |
2344 | fromUtf8() function. |
2345 | |
2346 | You can disable this constructor by defining |
2347 | \l QT_NO_CAST_FROM_ASCII when you compile your applications. This |
2348 | can be useful if you want to ensure that all user-visible strings |
2349 | go through QObject::tr(), for example. |
2350 | |
2351 | \note Defining \l QT_RESTRICTED_CAST_FROM_ASCII also disables |
2352 | this constructor, but enables a \c{QString(const char (&ch)[N])} |
2353 | constructor instead. Using non-literal input, or input with |
2354 | embedded NUL characters, or non-7-bit characters is undefined |
2355 | in this case. |
2356 | |
2357 | \sa fromLatin1(), fromLocal8Bit(), fromUtf8() |
2358 | */ |
2359 | |
2360 | /*! \fn QString::QString(const char8_t *str) |
2361 | |
2362 | Constructs a string initialized with the UTF-8 string \a str. The |
2363 | given const char8_t pointer is converted to Unicode using the |
2364 | fromUtf8() function. |
2365 | |
2366 | \since 6.1 |
2367 | \sa fromLatin1(), fromLocal8Bit(), fromUtf8() |
2368 | */ |
2369 | |
2370 | /* |
2371 | //! [from-std-string] |
2372 | Returns a copy of the \a str string. The given string is assumed to be |
2373 | encoded in \1, and is converted to QString using the \2 function. |
2374 | //! [from-std-string] |
2375 | */ |
2376 | |
2377 | /*! \fn QString QString::fromStdString(const std::string &str) |
2378 | |
2379 | \include qstring.cpp {from-std-string} {UTF-8} {fromUtf8()} |
2380 | |
2381 | \sa fromLatin1(), fromLocal8Bit(), fromUtf8(), QByteArray::fromStdString() |
2382 | */ |
2383 | |
2384 | /*! \fn QString QString::fromStdWString(const std::wstring &str) |
2385 | |
2386 | Returns a copy of the \a str string. The given string is assumed |
2387 | to be encoded in utf16 if the size of wchar_t is 2 bytes (e.g. on |
2388 | windows) and ucs4 if the size of wchar_t is 4 bytes (most Unix |
2389 | systems). |
2390 | |
2391 | \sa fromUtf16(), fromLatin1(), fromLocal8Bit(), fromUtf8(), fromUcs4(), |
2392 | fromStdU16String(), fromStdU32String() |
2393 | */ |
2394 | |
2395 | /*! \fn QString QString::fromWCharArray(const wchar_t *string, qsizetype size) |
2396 | \since 4.2 |
2397 | |
2398 | Returns a copy of the \a string, where the encoding of \a string depends on |
2399 | the size of wchar. If wchar is 4 bytes, the \a string is interpreted as |
2400 | UCS-4, if wchar is 2 bytes it is interpreted as UTF-16. |
2401 | |
2402 | If \a size is -1 (default), the \a string must be '\\0'-terminated. |
2403 | |
2404 | \sa fromUtf16(), fromLatin1(), fromLocal8Bit(), fromUtf8(), fromUcs4(), |
2405 | fromStdWString() |
2406 | */ |
2407 | |
2408 | /*! \fn std::wstring QString::toStdWString() const |
2409 | |
2410 | Returns a std::wstring object with the data contained in this |
2411 | QString. The std::wstring is encoded in utf16 on platforms where |
2412 | wchar_t is 2 bytes wide (e.g. windows) and in ucs4 on platforms |
2413 | where wchar_t is 4 bytes wide (most Unix systems). |
2414 | |
2415 | This method is mostly useful to pass a QString to a function |
2416 | that accepts a std::wstring object. |
2417 | |
2418 | \sa utf16(), toLatin1(), toUtf8(), toLocal8Bit(), toStdU16String(), |
2419 | toStdU32String() |
2420 | */ |
2421 | |
2422 | qsizetype QString::toUcs4_helper(const char16_t *uc, qsizetype length, char32_t *out) |
2423 | { |
2424 | qsizetype count = 0; |
2425 | |
2426 | QStringIterator i(QStringView(uc, length)); |
2427 | while (i.hasNext()) |
2428 | out[count++] = i.next(); |
2429 | |
2430 | return count; |
2431 | } |
2432 | |
2433 | /*! \fn qsizetype QString::toWCharArray(wchar_t *array) const |
2434 | \since 4.2 |
2435 | |
2436 | Fills the \a array with the data contained in this QString object. |
2437 | The array is encoded in UTF-16 on platforms where |
2438 | wchar_t is 2 bytes wide (e.g. windows) and in UCS-4 on platforms |
2439 | where wchar_t is 4 bytes wide (most Unix systems). |
2440 | |
2441 | \a array has to be allocated by the caller and contain enough space to |
2442 | hold the complete string (allocating the array with the same length as the |
2443 | string is always sufficient). |
2444 | |
2445 | This function returns the actual length of the string in \a array. |
2446 | |
2447 | \note This function does not append a null character to the array. |
2448 | |
2449 | \sa utf16(), toUcs4(), toLatin1(), toUtf8(), toLocal8Bit(), toStdWString(), |
2450 | QStringView::toWCharArray() |
2451 | */ |
2452 | |
2453 | /*! \fn QString::QString(const QString &other) |
2454 | |
2455 | Constructs a copy of \a other. |
2456 | |
2457 | This operation takes \l{constant time}, because QString is |
2458 | \l{implicitly shared}. This makes returning a QString from a |
2459 | function very fast. If a shared instance is modified, it will be |
2460 | copied (copy-on-write), and that takes \l{linear time}. |
2461 | |
2462 | \sa operator=() |
2463 | */ |
2464 | |
2465 | /*! |
2466 | Constructs a string initialized with the first \a size characters |
2467 | of the QChar array \a unicode. |
2468 | |
2469 | If \a unicode is 0, a null string is constructed. |
2470 | |
2471 | If \a size is negative, \a unicode is assumed to point to a \\0'-terminated |
2472 | array and its length is determined dynamically. The terminating |
2473 | null character is not considered part of the string. |
2474 | |
2475 | QString makes a deep copy of the string data. The unicode data is copied as |
2476 | is and the Byte Order Mark is preserved if present. |
2477 | |
2478 | \sa fromRawData() |
2479 | */ |
2480 | QString::QString(const QChar *unicode, qsizetype size) |
2481 | { |
2482 | if (!unicode) { |
2483 | d.clear(); |
2484 | } else { |
2485 | if (size < 0) |
2486 | size = QtPrivate::qustrlen(str: reinterpret_cast<const char16_t *>(unicode)); |
2487 | if (!size) { |
2488 | d = DataPointer::fromRawData(rawData: &_empty, length: 0); |
2489 | } else { |
2490 | d = DataPointer(Data::allocate(capacity: size), size); |
2491 | Q_CHECK_PTR(d.data()); |
2492 | memcpy(dest: d.data(), src: unicode, n: size * sizeof(QChar)); |
2493 | d.data()[size] = '\0'; |
2494 | } |
2495 | } |
2496 | } |
2497 | |
2498 | /*! |
2499 | Constructs a string of the given \a size with every character set |
2500 | to \a ch. |
2501 | |
2502 | \sa fill() |
2503 | */ |
2504 | QString::QString(qsizetype size, QChar ch) |
2505 | { |
2506 | if (size <= 0) { |
2507 | d = DataPointer::fromRawData(rawData: &_empty, length: 0); |
2508 | } else { |
2509 | d = DataPointer(Data::allocate(capacity: size), size); |
2510 | Q_CHECK_PTR(d.data()); |
2511 | d.data()[size] = '\0'; |
2512 | char16_t *b = d.data(); |
2513 | char16_t *e = d.data() + size; |
2514 | const char16_t value = ch.unicode(); |
2515 | std::fill(first: b, last: e, value: value); |
2516 | } |
2517 | } |
2518 | |
2519 | /*! \fn QString::QString(qsizetype size, Qt::Initialization) |
2520 | \internal |
2521 | |
2522 | Constructs a string of the given \a size without initializing the |
2523 | characters. This is only used in \c QStringBuilder::toString(). |
2524 | */ |
2525 | QString::QString(qsizetype size, Qt::Initialization) |
2526 | { |
2527 | if (size <= 0) { |
2528 | d = DataPointer::fromRawData(rawData: &_empty, length: 0); |
2529 | } else { |
2530 | d = DataPointer(Data::allocate(capacity: size), size); |
2531 | Q_CHECK_PTR(d.data()); |
2532 | d.data()[size] = '\0'; |
2533 | } |
2534 | } |
2535 | |
2536 | /*! \fn QString::QString(QLatin1StringView str) |
2537 | |
2538 | Constructs a copy of the Latin-1 string viewed by \a str. |
2539 | |
2540 | \sa fromLatin1() |
2541 | */ |
2542 | |
2543 | /*! |
2544 | Constructs a string of size 1 containing the character \a ch. |
2545 | */ |
2546 | QString::QString(QChar ch) |
2547 | { |
2548 | d = DataPointer(Data::allocate(capacity: 1), 1); |
2549 | Q_CHECK_PTR(d.data()); |
2550 | d.data()[0] = ch.unicode(); |
2551 | d.data()[1] = '\0'; |
2552 | } |
2553 | |
2554 | /*! \fn QString::QString(const QByteArray &ba) |
2555 | |
2556 | Constructs a string initialized with the byte array \a ba. The |
2557 | given byte array is converted to Unicode using fromUtf8(). |
2558 | |
2559 | You can disable this constructor by defining |
2560 | \l QT_NO_CAST_FROM_ASCII when you compile your applications. This |
2561 | can be useful if you want to ensure that all user-visible strings |
2562 | go through QObject::tr(), for example. |
2563 | |
2564 | \note: any null ('\\0') bytes in the byte array will be included in this |
2565 | string, converted to Unicode null characters (U+0000). This behavior is |
2566 | different from Qt 5.x. |
2567 | |
2568 | \sa fromLatin1(), fromLocal8Bit(), fromUtf8() |
2569 | */ |
2570 | |
2571 | /*! \fn QString::QString(const Null &) |
2572 | \internal |
2573 | */ |
2574 | |
2575 | /*! \fn QString::QString(QStringPrivate) |
2576 | \internal |
2577 | */ |
2578 | |
2579 | /*! \fn QString &QString::operator=(const QString::Null &) |
2580 | \internal |
2581 | */ |
2582 | |
2583 | /*! |
2584 | \fn QString::~QString() |
2585 | |
2586 | Destroys the string. |
2587 | */ |
2588 | |
2589 | |
2590 | /*! \fn void QString::swap(QString &other) |
2591 | \since 4.8 |
2592 | |
2593 | Swaps string \a other with this string. This operation is very fast and |
2594 | never fails. |
2595 | */ |
2596 | |
2597 | /*! \fn void QString::detach() |
2598 | |
2599 | \internal |
2600 | */ |
2601 | |
2602 | /*! \fn bool QString::isDetached() const |
2603 | |
2604 | \internal |
2605 | */ |
2606 | |
2607 | /*! \fn bool QString::isSharedWith(const QString &other) const |
2608 | |
2609 | \internal |
2610 | */ |
2611 | |
2612 | static bool needsReallocate(const QString &str, qsizetype newSize) |
2613 | { |
2614 | const auto capacityAtEnd = str.capacity() - str.data_ptr().freeSpaceAtBegin(); |
2615 | return newSize > capacityAtEnd; |
2616 | } |
2617 | |
2618 | /*! |
2619 | Sets the size of the string to \a size characters. |
2620 | |
2621 | If \a size is greater than the current size, the string is |
2622 | extended to make it \a size characters long with the extra |
2623 | characters added to the end. The new characters are uninitialized. |
2624 | |
2625 | If \a size is less than the current size, characters beyond position |
2626 | \a size are excluded from the string. |
2627 | |
2628 | \note While resize() will grow the capacity if needed, it never shrinks |
2629 | capacity. To shed excess capacity, use squeeze(). |
2630 | |
2631 | Example: |
2632 | |
2633 | \snippet qstring/main.cpp 45 |
2634 | |
2635 | If you want to append a certain number of identical characters to |
2636 | the string, use the \l {QString::}{resize(qsizetype, QChar)} overload. |
2637 | |
2638 | If you want to expand the string so that it reaches a certain |
2639 | width and fill the new positions with a particular character, use |
2640 | the leftJustified() function: |
2641 | |
2642 | If \a size is negative, it is equivalent to passing zero. |
2643 | |
2644 | \snippet qstring/main.cpp 47 |
2645 | |
2646 | \sa truncate(), reserve(), squeeze() |
2647 | */ |
2648 | |
2649 | void QString::resize(qsizetype size) |
2650 | { |
2651 | if (size < 0) |
2652 | size = 0; |
2653 | |
2654 | if (d->needsDetach() || needsReallocate(str: *this, newSize: size)) |
2655 | reallocData(alloc: size, option: QArrayData::Grow); |
2656 | d.size = size; |
2657 | if (d->allocatedCapacity()) |
2658 | d.data()[size] = u'\0'; |
2659 | } |
2660 | |
2661 | /*! |
2662 | \overload |
2663 | \since 5.7 |
2664 | |
2665 | Unlike \l {QString::}{resize(qsizetype)}, this overload |
2666 | initializes the new characters to \a fillChar: |
2667 | |
2668 | \snippet qstring/main.cpp 46 |
2669 | */ |
2670 | |
2671 | void QString::resize(qsizetype newSize, QChar fillChar) |
2672 | { |
2673 | const qsizetype oldSize = size(); |
2674 | resize(size: newSize); |
2675 | const qsizetype difference = size() - oldSize; |
2676 | if (difference > 0) |
2677 | std::fill_n(first: d.data() + oldSize, n: difference, value: fillChar.unicode()); |
2678 | } |
2679 | |
2680 | /*! \fn qsizetype QString::capacity() const |
2681 | |
2682 | Returns the maximum number of characters that can be stored in |
2683 | the string without forcing a reallocation. |
2684 | |
2685 | The sole purpose of this function is to provide a means of fine |
2686 | tuning QString's memory usage. In general, you will rarely ever |
2687 | need to call this function. If you want to know how many |
2688 | characters are in the string, call size(). |
2689 | |
2690 | \note a statically allocated string will report a capacity of 0, |
2691 | even if it's not empty. |
2692 | |
2693 | \note The free space position in the allocated memory block is undefined. In |
2694 | other words, one should not assume that the free memory is always located |
2695 | after the initialized elements. |
2696 | |
2697 | \sa reserve(), squeeze() |
2698 | */ |
2699 | |
2700 | /*! |
2701 | \fn void QString::reserve(qsizetype size) |
2702 | |
2703 | Ensures the string has space for at least \a size characters. |
2704 | |
2705 | If you know in advance how large the string will be, you can call this |
2706 | function to save repeated reallocation in the course of building it. |
2707 | This can improve performance when building a string incrementally. |
2708 | A long sequence of operations that add to a string may trigger several |
2709 | reallocations, the last of which may leave you with significantly more |
2710 | space than you really need, which is less efficient than doing a single |
2711 | allocation of the right size at the start. |
2712 | |
2713 | If in doubt about how much space shall be needed, it is usually better to |
2714 | use an upper bound as \a size, or a high estimate of the most likely size, |
2715 | if a strict upper bound would be much bigger than this. If \a size is an |
2716 | underestimate, the string will grow as needed once the reserved size is |
2717 | exceeded, which may lead to a larger allocation than your best overestimate |
2718 | would have and will slow the operation that triggers it. |
2719 | |
2720 | \warning reserve() reserves memory but does not change the size of the |
2721 | string. Accessing data beyond the end of the string is undefined behavior. |
2722 | If you need to access memory beyond the current end of the string, |
2723 | use resize(). |
2724 | |
2725 | This function is useful for code that needs to build up a long |
2726 | string and wants to avoid repeated reallocation. In this example, |
2727 | we want to add to the string until some condition is \c true, and |
2728 | we're fairly sure that size is large enough to make a call to |
2729 | reserve() worthwhile: |
2730 | |
2731 | \snippet qstring/main.cpp 44 |
2732 | |
2733 | \sa squeeze(), capacity(), resize() |
2734 | */ |
2735 | |
2736 | /*! |
2737 | \fn void QString::squeeze() |
2738 | |
2739 | Releases any memory not required to store the character data. |
2740 | |
2741 | The sole purpose of this function is to provide a means of fine |
2742 | tuning QString's memory usage. In general, you will rarely ever |
2743 | need to call this function. |
2744 | |
2745 | \sa reserve(), capacity() |
2746 | */ |
2747 | |
2748 | void QString::reallocData(qsizetype alloc, QArrayData::AllocationOption option) |
2749 | { |
2750 | if (!alloc) { |
2751 | d = DataPointer::fromRawData(rawData: &_empty, length: 0); |
2752 | return; |
2753 | } |
2754 | |
2755 | // don't use reallocate path when reducing capacity and there's free space |
2756 | // at the beginning: might shift data pointer outside of allocated space |
2757 | const bool cannotUseReallocate = d.freeSpaceAtBegin() > 0; |
2758 | |
2759 | if (d->needsDetach() || cannotUseReallocate) { |
2760 | DataPointer dd(Data::allocate(capacity: alloc, option), qMin(a: alloc, b: d.size)); |
2761 | Q_CHECK_PTR(dd.data()); |
2762 | if (dd.size > 0) |
2763 | ::memcpy(dest: dd.data(), src: d.data(), n: dd.size * sizeof(QChar)); |
2764 | dd.data()[dd.size] = 0; |
2765 | d = dd; |
2766 | } else { |
2767 | d->reallocate(alloc, option); |
2768 | } |
2769 | } |
2770 | |
2771 | void QString::reallocGrowData(qsizetype n) |
2772 | { |
2773 | if (!n) // expected to always allocate |
2774 | n = 1; |
2775 | |
2776 | if (d->needsDetach()) { |
2777 | DataPointer dd(DataPointer::allocateGrow(from: d, n, position: QArrayData::GrowsAtEnd)); |
2778 | Q_CHECK_PTR(dd.data()); |
2779 | dd->copyAppend(b: d.data(), e: d.data() + d.size); |
2780 | dd.data()[dd.size] = 0; |
2781 | d = dd; |
2782 | } else { |
2783 | d->reallocate(alloc: d.constAllocatedCapacity() + n, option: QArrayData::Grow); |
2784 | } |
2785 | } |
2786 | |
2787 | /*! \fn void QString::clear() |
2788 | |
2789 | Clears the contents of the string and makes it null. |
2790 | |
2791 | \sa resize(), isNull() |
2792 | */ |
2793 | |
2794 | /*! \fn QString &QString::operator=(const QString &other) |
2795 | |
2796 | Assigns \a other to this string and returns a reference to this |
2797 | string. |
2798 | */ |
2799 | |
2800 | QString &QString::operator=(const QString &other) noexcept |
2801 | { |
2802 | d = other.d; |
2803 | return *this; |
2804 | } |
2805 | |
2806 | /*! |
2807 | \fn QString &QString::operator=(QString &&other) |
2808 | |
2809 | Move-assigns \a other to this QString instance. |
2810 | |
2811 | \since 5.2 |
2812 | */ |
2813 | |
2814 | /*! \fn QString &QString::operator=(QLatin1StringView str) |
2815 | |
2816 | \overload operator=() |
2817 | |
2818 | Assigns the Latin-1 string viewed by \a str to this string. |
2819 | */ |
2820 | QString &QString::operator=(QLatin1StringView other) |
2821 | { |
2822 | const qsizetype capacityAtEnd = capacity() - d.freeSpaceAtBegin(); |
2823 | if (isDetached() && other.size() <= capacityAtEnd) { // assumes d->alloc == 0 -> !isDetached() (sharedNull) |
2824 | d.size = other.size(); |
2825 | d.data()[other.size()] = 0; |
2826 | qt_from_latin1(dst: d.data(), str: other.latin1(), size: other.size()); |
2827 | } else { |
2828 | *this = fromLatin1(str: other.latin1(), size: other.size()); |
2829 | } |
2830 | return *this; |
2831 | } |
2832 | |
2833 | /*! \fn QString &QString::operator=(const QByteArray &ba) |
2834 | |
2835 | \overload operator=() |
2836 | |
2837 | Assigns \a ba to this string. The byte array is converted to Unicode |
2838 | using the fromUtf8() function. |
2839 | |
2840 | You can disable this operator by defining |
2841 | \l QT_NO_CAST_FROM_ASCII when you compile your applications. This |
2842 | can be useful if you want to ensure that all user-visible strings |
2843 | go through QObject::tr(), for example. |
2844 | */ |
2845 | |
2846 | /*! \fn QString &QString::operator=(const char *str) |
2847 | |
2848 | \overload operator=() |
2849 | |
2850 | Assigns \a str to this string. The const char pointer is converted |
2851 | to Unicode using the fromUtf8() function. |
2852 | |
2853 | You can disable this operator by defining \l QT_NO_CAST_FROM_ASCII |
2854 | or \l QT_RESTRICTED_CAST_FROM_ASCII when you compile your applications. |
2855 | This can be useful if you want to ensure that all user-visible strings |
2856 | go through QObject::tr(), for example. |
2857 | */ |
2858 | |
2859 | /*! |
2860 | \overload operator=() |
2861 | |
2862 | Sets the string to contain the single character \a ch. |
2863 | */ |
2864 | QString &QString::operator=(QChar ch) |
2865 | { |
2866 | const qsizetype capacityAtEnd = capacity() - d.freeSpaceAtBegin(); |
2867 | if (isDetached() && capacityAtEnd >= 1) { // assumes d->alloc == 0 -> !isDetached() (sharedNull) |
2868 | // re-use existing capacity: |
2869 | d.data()[0] = ch.unicode(); |
2870 | d.data()[1] = 0; |
2871 | d.size = 1; |
2872 | } else { |
2873 | operator=(other: QString(ch)); |
2874 | } |
2875 | return *this; |
2876 | } |
2877 | |
2878 | /*! |
2879 | \fn QString& QString::insert(qsizetype position, const QString &str) |
2880 | |
2881 | Inserts the string \a str at the given index \a position and |
2882 | returns a reference to this string. |
2883 | |
2884 | Example: |
2885 | |
2886 | \snippet qstring/main.cpp 26 |
2887 | |
2888 | //! [string-grow-at-insertion] |
2889 | This string grows to accommodate the insertion. If \a position is beyond |
2890 | the end of the string, space characters are appended to the string to reach |
2891 | this \a position, followed by \a str. |
2892 | //! [string-grow-at-insertion] |
2893 | |
2894 | \sa append(), prepend(), replace(), remove() |
2895 | */ |
2896 | |
2897 | /*! |
2898 | \fn QString& QString::insert(qsizetype position, QStringView str) |
2899 | \since 6.0 |
2900 | \overload insert() |
2901 | |
2902 | Inserts the string view \a str at the given index \a position and |
2903 | returns a reference to this string. |
2904 | |
2905 | \include qstring.cpp string-grow-at-insertion |
2906 | */ |
2907 | |
2908 | |
2909 | /*! |
2910 | \fn QString& QString::insert(qsizetype position, const char *str) |
2911 | \since 5.5 |
2912 | \overload insert() |
2913 | |
2914 | Inserts the C string \a str at the given index \a position and |
2915 | returns a reference to this string. |
2916 | |
2917 | \include qstring.cpp string-grow-at-insertion |
2918 | |
2919 | This function is not available when \l QT_NO_CAST_FROM_ASCII is |
2920 | defined. |
2921 | */ |
2922 | |
2923 | /*! |
2924 | \fn QString& QString::insert(qsizetype position, const QByteArray &str) |
2925 | \since 5.5 |
2926 | \overload insert() |
2927 | |
2928 | Interprets the contents of \a str as UTF-8, inserts the Unicode string |
2929 | it encodes at the given index \a position and returns a reference to |
2930 | this string. |
2931 | |
2932 | \include qstring.cpp string-grow-at-insertion |
2933 | |
2934 | This function is not available when \l QT_NO_CAST_FROM_ASCII is |
2935 | defined. |
2936 | */ |
2937 | |
2938 | /*! \internal |
2939 | T is a view or a container on/of QChar, char16_t, or char |
2940 | */ |
2941 | template <typename T> |
2942 | static void insert_helper(QString &str, qsizetype i, const T &toInsert) |
2943 | { |
2944 | auto &str_d = str.data_ptr(); |
2945 | qsizetype difference = 0; |
2946 | if (Q_UNLIKELY(i > str_d.size)) |
2947 | difference = i - str_d.size; |
2948 | const qsizetype oldSize = str_d.size; |
2949 | const qsizetype insert_size = toInsert.size(); |
2950 | const qsizetype newSize = str_d.size + difference + insert_size; |
2951 | const auto side = i == 0 ? QArrayData::GrowsAtBeginning : QArrayData::GrowsAtEnd; |
2952 | |
2953 | if (str_d.needsDetach() || needsReallocate(str, newSize)) { |
2954 | const auto cbegin = str.cbegin(); |
2955 | const auto cend = str.cend(); |
2956 | const auto insert_start = difference == 0 ? std::next(x: cbegin, n: i) : cend; |
2957 | QString other; |
2958 | // Using detachAndGrow() so that prepend optimization works and QStringBuilder |
2959 | // unittests pass |
2960 | other.data_ptr().detachAndGrow(where: side, n: newSize, data: nullptr, old: nullptr); |
2961 | other.append(v: QStringView(cbegin, insert_start)); |
2962 | other.resize(newSize: i, fillChar: u' '); |
2963 | other.append(toInsert); |
2964 | other.append(v: QStringView(insert_start, cend)); |
2965 | str.swap(other); |
2966 | return; |
2967 | } |
2968 | |
2969 | str_d.detachAndGrow(where: side, n: difference + insert_size, data: nullptr, old: nullptr); |
2970 | Q_CHECK_PTR(str_d.data()); |
2971 | str.resize(size: newSize); |
2972 | |
2973 | auto begin = str_d.begin(); |
2974 | auto old_end = std::next(x: begin, n: oldSize); |
2975 | std::fill_n(first: old_end, n: difference, value: u' '); |
2976 | auto insert_start = std::next(x: begin, n: i); |
2977 | if (difference == 0) |
2978 | std::move_backward(first: insert_start, last: old_end, result: str_d.end()); |
2979 | |
2980 | using Char = std::remove_cv_t<typename T::value_type>; |
2981 | if constexpr(std::is_same_v<Char, QChar>) |
2982 | std::copy_n(first: reinterpret_cast<const char16_t *>(toInsert.data()), n: insert_size, result: insert_start); |
2983 | else if constexpr (std::is_same_v<Char, char16_t>) |
2984 | std::copy_n(toInsert.data(), insert_size, insert_start); |
2985 | else if constexpr (std::is_same_v<Char, char>) |
2986 | qt_from_latin1(insert_start, toInsert.data(), insert_size); |
2987 | } |
2988 | |
2989 | /*! |
2990 | \fn QString &QString::insert(qsizetype position, QLatin1StringView str) |
2991 | \overload insert() |
2992 | |
2993 | Inserts the Latin-1 string viewed by \a str at the given index \a position. |
2994 | |
2995 | \include qstring.cpp string-grow-at-insertion |
2996 | */ |
2997 | QString &QString::insert(qsizetype i, QLatin1StringView str) |
2998 | { |
2999 | const char *s = str.latin1(); |
3000 | if (i < 0 || !s || !(*s)) |
3001 | return *this; |
3002 | |
3003 | insert_helper(str&: *this, i, toInsert: str); |
3004 | return *this; |
3005 | } |
3006 | |
3007 | /*! |
3008 | \fn QString &QString::insert(qsizetype position, QUtf8StringView str) |
3009 | \overload insert() |
3010 | \since 6.5 |
3011 | |
3012 | Inserts the UTF-8 string view \a str at the given index \a position. |
3013 | |
3014 | \note Inserting variable-width UTF-8-encoded string data is conceptually slower |
3015 | than inserting fixed-width string data such as UTF-16 (QStringView) or Latin-1 |
3016 | (QLatin1StringView) and should thus be used sparingly. |
3017 | |
3018 | \include qstring.cpp string-grow-at-insertion |
3019 | */ |
3020 | QString &QString::insert(qsizetype i, QUtf8StringView s) |
3021 | { |
3022 | auto insert_size = s.size(); |
3023 | if (i < 0 || insert_size <= 0) |
3024 | return *this; |
3025 | |
3026 | qsizetype difference = 0; |
3027 | if (Q_UNLIKELY(i > d.size)) |
3028 | difference = i - d.size; |
3029 | |
3030 | const qsizetype newSize = d.size + difference + insert_size; |
3031 | |
3032 | if (d.needsDetach() || needsReallocate(str: *this, newSize)) { |
3033 | const auto cbegin = this->cbegin(); |
3034 | const auto insert_start = difference == 0 ? std::next(x: cbegin, n: i) : cend(); |
3035 | QString other; |
3036 | other.reserve(asize: newSize); |
3037 | other.append(v: QStringView(cbegin, insert_start)); |
3038 | if (difference > 0) |
3039 | other.resize(newSize: i, fillChar: u' '); |
3040 | other.append(s); |
3041 | other.append(v: QStringView(insert_start, cend())); |
3042 | swap(other); |
3043 | return *this; |
3044 | } |
3045 | |
3046 | if (i >= d.size) { |
3047 | d.detachAndGrow(where: QArrayData::GrowsAtEnd, n: difference + insert_size, data: nullptr, old: nullptr); |
3048 | Q_CHECK_PTR(d.data()); |
3049 | |
3050 | if (difference > 0) |
3051 | resize(newSize: i, fillChar: u' '); |
3052 | append(s); |
3053 | } else { |
3054 | // Optimal insertion of Utf8 data is at the end, anywhere else could |
3055 | // potentially lead to moving characters twice if Utf8 data size |
3056 | // (variable-width) is less than the equiavalent Utf16 data size |
3057 | QVarLengthArray<char16_t> buffer(insert_size); // ### optimize (QTBUG-108546) |
3058 | char16_t *b = QUtf8::convertToUnicode(dst: buffer.data(), in: s); |
3059 | buffer.resize(sz: std::distance(first: buffer.begin(), last: b)); |
3060 | insert_helper(str&: *this, i, toInsert: buffer); |
3061 | } |
3062 | |
3063 | return *this; |
3064 | } |
3065 | |
3066 | /*! |
3067 | \fn QString& QString::insert(qsizetype position, const QChar *unicode, qsizetype size) |
3068 | \overload insert() |
3069 | |
3070 | Inserts the first \a size characters of the QChar array \a unicode |
3071 | at the given index \a position in the string. |
3072 | |
3073 | This string grows to accommodate the insertion. If \a position is beyond |
3074 | the end of the string, space characters are appended to the string to reach |
3075 | this \a position, followed by \a size characters of the QChar array |
3076 | \a unicode. |
3077 | */ |
3078 | QString& QString::insert(qsizetype i, const QChar *unicode, qsizetype size) |
3079 | { |
3080 | if (i < 0 || size <= 0) |
3081 | return *this; |
3082 | |
3083 | // In case when data points into "this" |
3084 | if (!d->needsDetach() && QtPrivate::q_points_into_range(p: unicode, c: *this)) { |
3085 | QVarLengthArray copy(unicode, unicode + size); |
3086 | insert(i, unicode: copy.data(), size); |
3087 | } else { |
3088 | insert_helper(str&: *this, i, toInsert: QStringView(unicode, size)); |
3089 | } |
3090 | |
3091 | return *this; |
3092 | } |
3093 | |
3094 | /*! |
3095 | \fn QString& QString::insert(qsizetype position, QChar ch) |
3096 | \overload insert() |
3097 | |
3098 | Inserts \a ch at the given index \a position in the string. |
3099 | |
3100 | This string grows to accommodate the insertion. If \a position is beyond |
3101 | the end of the string, space characters are appended to the string to reach |
3102 | this \a position, followed by \a ch. |
3103 | */ |
3104 | |
3105 | QString& QString::insert(qsizetype i, QChar ch) |
3106 | { |
3107 | if (i < 0) |
3108 | i += d.size; |
3109 | return insert(i, unicode: &ch, size: 1); |
3110 | } |
3111 | |
3112 | /*! |
3113 | Appends the string \a str onto the end of this string. |
3114 | |
3115 | Example: |
3116 | |
3117 | \snippet qstring/main.cpp 9 |
3118 | |
3119 | This is the same as using the insert() function: |
3120 | |
3121 | \snippet qstring/main.cpp 10 |
3122 | |
3123 | The append() function is typically very fast (\l{constant time}), |
3124 | because QString preallocates extra space at the end of the string |
3125 | data so it can grow without reallocating the entire string each |
3126 | time. |
3127 | |
3128 | \sa operator+=(), prepend(), insert() |
3129 | */ |
3130 | QString &QString::append(const QString &str) |
3131 | { |
3132 | if (!str.isNull()) { |
3133 | if (isNull()) { |
3134 | if (Q_UNLIKELY(!str.d.isMutable())) |
3135 | assign(s: str); // fromRawData, so we do a deep copy |
3136 | else |
3137 | operator=(other: str); |
3138 | } else if (str.size()) { |
3139 | append(uc: str.constData(), len: str.size()); |
3140 | } |
3141 | } |
3142 | return *this; |
3143 | } |
3144 | |
3145 | /*! |
3146 | \fn QString &QString::append(QStringView v) |
3147 | \overload append() |
3148 | \since 6.0 |
3149 | |
3150 | Appends the given string view \a v to this string and returns the result. |
3151 | */ |
3152 | |
3153 | /*! |
3154 | \overload append() |
3155 | \since 5.0 |
3156 | |
3157 | Appends \a len characters from the QChar array \a str to this string. |
3158 | */ |
3159 | QString &QString::append(const QChar *str, qsizetype len) |
3160 | { |
3161 | if (str && len > 0) { |
3162 | static_assert(sizeof(QChar) == sizeof(char16_t), "Unexpected difference in sizes" ); |
3163 | // the following should be safe as QChar uses char16_t as underlying data |
3164 | const char16_t *char16String = reinterpret_cast<const char16_t *>(str); |
3165 | d->growAppend(b: char16String, e: char16String + len); |
3166 | d.data()[d.size] = u'\0'; |
3167 | } |
3168 | return *this; |
3169 | } |
3170 | |
3171 | /*! |
3172 | \overload append() |
3173 | |
3174 | Appends the Latin-1 string viewed by \a str to this string. |
3175 | */ |
3176 | QString &QString::append(QLatin1StringView str) |
3177 | { |
3178 | append_helper(self&: *this, view: str); |
3179 | return *this; |
3180 | } |
3181 | |
3182 | /*! |
3183 | \overload append() |
3184 | \since 6.5 |
3185 | |
3186 | Appends the UTF-8 string view \a str to this string. |
3187 | */ |
3188 | QString &QString::append(QUtf8StringView str) |
3189 | { |
3190 | append_helper(self&: *this, view: str); |
3191 | return *this; |
3192 | } |
3193 | |
3194 | /*! \fn QString &QString::append(const QByteArray &ba) |
3195 | |
3196 | \overload append() |
3197 | |
3198 | Appends the byte array \a ba to this string. The given byte array |
3199 | is converted to Unicode using the fromUtf8() function. |
3200 | |
3201 | You can disable this function by defining \l QT_NO_CAST_FROM_ASCII |
3202 | when you compile your applications. This can be useful if you want |
3203 | to ensure that all user-visible strings go through QObject::tr(), |
3204 | for example. |
3205 | */ |
3206 | |
3207 | /*! \fn QString &QString::append(const char *str) |
3208 | |
3209 | \overload append() |
3210 | |
3211 | Appends the string \a str to this string. The given const char |
3212 | pointer is converted to Unicode using the fromUtf8() function. |
3213 | |
3214 | You can disable this function by defining \l QT_NO_CAST_FROM_ASCII |
3215 | when you compile your applications. This can be useful if you want |
3216 | to ensure that all user-visible strings go through QObject::tr(), |
3217 | for example. |
3218 | */ |
3219 | |
3220 | /*! |
3221 | \overload append() |
3222 | |
3223 | Appends the character \a ch to this string. |
3224 | */ |
3225 | QString &QString::append(QChar ch) |
3226 | { |
3227 | d.detachAndGrow(where: QArrayData::GrowsAtEnd, n: 1, data: nullptr, old: nullptr); |
3228 | d->copyAppend(n: 1, t: ch.unicode()); |
3229 | d.data()[d.size] = '\0'; |
3230 | return *this; |
3231 | } |
3232 | |
3233 | /*! \fn QString &QString::prepend(const QString &str) |
3234 | |
3235 | Prepends the string \a str to the beginning of this string and |
3236 | returns a reference to this string. |
3237 | |
3238 | This operation is typically very fast (\l{constant time}), because |
3239 | QString preallocates extra space at the beginning of the string data, |
3240 | so it can grow without reallocating the entire string each time. |
3241 | |
3242 | Example: |
3243 | |
3244 | \snippet qstring/main.cpp 36 |
3245 | |
3246 | \sa append(), insert() |
3247 | */ |
3248 | |
3249 | /*! \fn QString &QString::prepend(QLatin1StringView str) |
3250 | |
3251 | \overload prepend() |
3252 | |
3253 | Prepends the Latin-1 string viewed by \a str to this string. |
3254 | */ |
3255 | |
3256 | /*! \fn QString &QString::prepend(QUtf8StringView str) |
3257 | \since 6.5 |
3258 | \overload prepend() |
3259 | |
3260 | Prepends the UTF-8 string view \a str to this string. |
3261 | */ |
3262 | |
3263 | /*! \fn QString &QString::prepend(const QChar *str, qsizetype len) |
3264 | \since 5.5 |
3265 | \overload prepend() |
3266 | |
3267 | Prepends \a len characters from the QChar array \a str to this string and |
3268 | returns a reference to this string. |
3269 | */ |
3270 | |
3271 | /*! \fn QString &QString::prepend(QStringView str) |
3272 | \since 6.0 |
3273 | \overload prepend() |
3274 | |
3275 | Prepends the string view \a str to the beginning of this string and |
3276 | returns a reference to this string. |
3277 | */ |
3278 | |
3279 | /*! \fn QString &QString::prepend(const QByteArray &ba) |
3280 | |
3281 | \overload prepend() |
3282 | |
3283 | Prepends the byte array \a ba to this string. The byte array is |
3284 | converted to Unicode using the fromUtf8() function. |
3285 | |
3286 | You can disable this function by defining |
3287 | \l QT_NO_CAST_FROM_ASCII when you compile your applications. This |
3288 | can be useful if you want to ensure that all user-visible strings |
3289 | go through QObject::tr(), for example. |
3290 | */ |
3291 | |
3292 | /*! \fn QString &QString::prepend(const char *str) |
3293 | |
3294 | \overload prepend() |
3295 | |
3296 | Prepends the string \a str to this string. The const char pointer |
3297 | is converted to Unicode using the fromUtf8() function. |
3298 | |
3299 | You can disable this function by defining |
3300 | \l QT_NO_CAST_FROM_ASCII when you compile your applications. This |
3301 | can be useful if you want to ensure that all user-visible strings |
3302 | go through QObject::tr(), for example. |
3303 | */ |
3304 | |
3305 | /*! \fn QString &QString::prepend(QChar ch) |
3306 | |
3307 | \overload prepend() |
3308 | |
3309 | Prepends the character \a ch to this string. |
3310 | */ |
3311 | |
3312 | /*! |
3313 | \fn QString &QString::assign(QAnyStringView v) |
3314 | \since 6.6 |
3315 | |
3316 | Replaces the contents of this string with a copy of \a v and returns a |
3317 | reference to this string. |
3318 | |
3319 | The size of this string will be equal to the size of \a v, converted to |
3320 | UTF-16 as if by \c{v.toString()}. Unlike QAnyStringView::toString(), however, |
3321 | this function only allocates memory if the estimated size exceeds the capacity |
3322 | of this string or this string is shared. |
3323 | |
3324 | \sa QAnyStringView::toString() |
3325 | */ |
3326 | |
3327 | /*! |
3328 | \fn QString &QString::assign(qsizetype n, QChar c) |
3329 | \since 6.6 |
3330 | |
3331 | Replaces the contents of this string with \a n copies of \a c and |
3332 | returns a reference to this string. |
3333 | |
3334 | The size of this string will be equal to \a n, which has to be non-negative. |
3335 | |
3336 | This function will only allocate memory if \a n exceeds the capacity of this |
3337 | string or this string is shared. |
3338 | |
3339 | \sa fill() |
3340 | */ |
3341 | |
3342 | /*! |
3343 | \fn template <typename InputIterator, if_compatible_iterator<InputIterator>> QString &QString::assign(InputIterator first, InputIterator last) |
3344 | \since 6.6 |
3345 | |
3346 | Replaces the contents of this string with a copy of the elements in the |
3347 | iterator range [\a first, \a last) and returns a reference to this string. |
3348 | |
3349 | The size of this string will be equal to the decoded length of the elements |
3350 | in the range [\a first, \a last), which need not be the same as the length of |
3351 | the range itself, because this function transparently recodes the input |
3352 | character set to UTF-16. |
3353 | |
3354 | This function will only allocate memory if the number of elements in the |
3355 | range, or, for non-UTF-16-encoded input, the maximum possible size of the |
3356 | resulting string, exceeds the capacity of this string, or if this string is |
3357 | shared. |
3358 | |
3359 | \note This function overload only participates in overload resolution if |
3360 | \c InputIterator meets the requirements of a |
3361 | \l {https://en.cppreference.com/w/cpp/named_req/InputIterator} {LegacyInputIterator} |
3362 | and the \c{value_type} of \c InputIterator is one of the following character types: |
3363 | \list |
3364 | \li QChar |
3365 | \li QLatin1Char |
3366 | \li \c char16_t |
3367 | \li (on platforms, such as Windows, where it is a 16-bit type) \c wchar_t |
3368 | \li \c char32_t |
3369 | \endlist |
3370 | |
3371 | \note The behavior is undefined if either argument is an iterator into *this or |
3372 | [\a first, \a last) is not a valid range. |
3373 | */ |
3374 | |
3375 | QString &QString::assign(QAnyStringView s) |
3376 | { |
3377 | if (s.size() <= capacity() && isDetached()) { |
3378 | const auto offset = d.freeSpaceAtBegin(); |
3379 | if (offset) |
3380 | d.setBegin(d.begin() - offset); |
3381 | resize(size: 0); |
3382 | s.visit(v: [this](auto input) { |
3383 | this->append(input); |
3384 | }); |
3385 | } else { |
3386 | *this = s.toString(); |
3387 | } |
3388 | return *this; |
3389 | } |
3390 | |
3391 | QString &QString::assign_helper(const char32_t *data, qsizetype len) |
3392 | { |
3393 | // worst case: each char32_t requires a surrogate pair, so |
3394 | const auto requiredCapacity = len * 2; |
3395 | if (requiredCapacity <= capacity() && isDetached()) { |
3396 | const auto offset = d.freeSpaceAtBegin(); |
3397 | if (offset) |
3398 | d.setBegin(d.begin() - offset); |
3399 | auto begin = reinterpret_cast<QChar *>(d.begin()); |
3400 | auto ba = QByteArrayView(reinterpret_cast<const std::byte*>(data), len * sizeof(char32_t)); |
3401 | QStringConverter::State state; |
3402 | const auto end = QUtf32::convertToUnicode(out: begin, ba, state: &state, endian: DetectEndianness); |
3403 | d.size = end - begin; |
3404 | d.data()[d.size] = u'\0'; |
3405 | } else { |
3406 | *this = QString::fromUcs4(data, size: len); |
3407 | } |
3408 | return *this; |
3409 | } |
3410 | |
3411 | /*! |
3412 | \fn QString &QString::remove(qsizetype position, qsizetype n) |
3413 | |
3414 | Removes \a n characters from the string, starting at the given \a |
3415 | position index, and returns a reference to the string. |
3416 | |
3417 | If the specified \a position index is within the string, but \a |
3418 | position + \a n is beyond the end of the string, the string is |
3419 | truncated at the specified \a position. |
3420 | |
3421 | If \a n is <= 0 nothing is changed. |
3422 | |
3423 | \snippet qstring/main.cpp 37 |
3424 | |
3425 | //! [shrinking-erase] |
3426 | Element removal will preserve the string's capacity and not reduce the |
3427 | amount of allocated memory. To shed extra capacity and free as much memory |
3428 | as possible, call squeeze() after the last change to the string's size. |
3429 | //! [shrinking-erase] |
3430 | |
3431 | \sa insert(), replace() |
3432 | */ |
3433 | QString &QString::remove(qsizetype pos, qsizetype len) |
3434 | { |
3435 | if (pos < 0) // count from end of string |
3436 | pos += size(); |
3437 | |
3438 | if (size_t(pos) >= size_t(size()) || len <= 0) |
3439 | return *this; |
3440 | |
3441 | len = std::min(a: len, b: size() - pos); |
3442 | |
3443 | if (!d->isShared()) { |
3444 | d->erase(b: d.begin() + pos, n: len); |
3445 | d.data()[d.size] = u'\0'; |
3446 | } else { |
3447 | // TODO: either reserve "size()", which is bigger than needed, or |
3448 | // modify the shrinking-erase docs of this method (since the size |
3449 | // of "copy" won't have any extra capacity any more) |
3450 | const qsizetype sz = size() - len; |
3451 | QString copy{sz, Qt::Uninitialized}; |
3452 | auto begin = d.begin(); |
3453 | auto toRemove_start = d.begin() + pos; |
3454 | copy.d->copyRanges(ranges: {{.begin: begin, .end: toRemove_start}, |
3455 | {.begin: toRemove_start + len, .end: d.end()}}); |
3456 | swap(other&: copy); |
3457 | } |
3458 | return *this; |
3459 | } |
3460 | |
3461 | template<typename T> |
3462 | static void removeStringImpl(QString &s, const T &needle, Qt::CaseSensitivity cs) |
3463 | { |
3464 | const auto needleSize = needle.size(); |
3465 | if (!needleSize) |
3466 | return; |
3467 | |
3468 | // avoid detach if nothing to do: |
3469 | qsizetype i = s.indexOf(needle, 0, cs); |
3470 | if (i < 0) |
3471 | return; |
3472 | |
3473 | QString::DataPointer &dptr = s.data_ptr(); |
3474 | auto begin = dptr.begin(); |
3475 | auto end = dptr.end(); |
3476 | |
3477 | auto copyFunc = [&](auto &dst) { |
3478 | auto src = begin + i + needleSize; |
3479 | while (src < end) { |
3480 | i = s.indexOf(needle, std::distance(begin, src), cs); |
3481 | auto hit = i == -1 ? end : begin + i; |
3482 | dst = std::copy(src, hit, dst); |
3483 | src = hit + needleSize; |
3484 | } |
3485 | return dst; |
3486 | }; |
3487 | |
3488 | if (!dptr->needsDetach()) { |
3489 | auto dst = begin + i; |
3490 | dst = copyFunc(dst); |
3491 | s.truncate(pos: std::distance(first: begin, last: dst)); |
3492 | } else { |
3493 | QString copy{s.size(), Qt::Uninitialized}; |
3494 | auto copy_begin = copy.begin(); |
3495 | auto dst = std::copy(first: begin, last: begin + i, result: copy_begin); // Chunk before the first hit |
3496 | dst = copyFunc(dst); |
3497 | copy.resize(size: std::distance(first: copy_begin, last: dst)); |
3498 | s.swap(other&: copy); |
3499 | } |
3500 | } |
3501 | |
3502 | /*! |
3503 | Removes every occurrence of the given \a str string in this |
3504 | string, and returns a reference to this string. |
3505 | |
3506 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
3507 | |
3508 | This is the same as \c replace(str, "", cs). |
3509 | |
3510 | \include qstring.cpp shrinking-erase |
3511 | |
3512 | \sa replace() |
3513 | */ |
3514 | QString &QString::remove(const QString &str, Qt::CaseSensitivity cs) |
3515 | { |
3516 | const auto s = str.d.data(); |
3517 | if (QtPrivate::q_points_into_range(p: s, c: d)) |
3518 | removeStringImpl(s&: *this, needle: QStringView{QVarLengthArray(s, s + str.size())}, cs); |
3519 | else |
3520 | removeStringImpl(s&: *this, needle: qToStringViewIgnoringNull(s: str), cs); |
3521 | return *this; |
3522 | } |
3523 | |
3524 | /*! |
3525 | \since 5.11 |
3526 | \overload |
3527 | |
3528 | Removes every occurrence of the given Latin-1 string viewed by \a str |
3529 | from this string, and returns a reference to this string. |
3530 | |
3531 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
3532 | |
3533 | This is the same as \c replace(str, "", cs). |
3534 | |
3535 | \include qstring.cpp shrinking-erase |
3536 | |
3537 | \sa replace() |
3538 | */ |
3539 | QString &QString::remove(QLatin1StringView str, Qt::CaseSensitivity cs) |
3540 | { |
3541 | removeStringImpl(s&: *this, needle: str, cs); |
3542 | return *this; |
3543 | } |
3544 | |
3545 | /*! |
3546 | \fn QString &QString::removeAt(qsizetype pos) |
3547 | |
3548 | \since 6.5 |
3549 | |
3550 | Removes the character at index \a pos. If \a pos is out of bounds |
3551 | (i.e. \a pos >= size()), this function does nothing. |
3552 | |
3553 | \sa remove() |
3554 | */ |
3555 | |
3556 | /*! |
3557 | \fn QString &QString::removeFirst() |
3558 | |
3559 | \since 6.5 |
3560 | |
3561 | Removes the first character in this string. If the string is empty, |
3562 | this function does nothing. |
3563 | |
3564 | \sa remove() |
3565 | */ |
3566 | |
3567 | /*! |
3568 | \fn QString &QString::removeLast() |
3569 | |
3570 | \since 6.5 |
3571 | |
3572 | Removes the last character in this string. If the string is empty, |
3573 | this function does nothing. |
3574 | |
3575 | \sa remove() |
3576 | */ |
3577 | |
3578 | /*! |
3579 | Removes every occurrence of the character \a ch in this string, and |
3580 | returns a reference to this string. |
3581 | |
3582 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
3583 | |
3584 | Example: |
3585 | |
3586 | \snippet qstring/main.cpp 38 |
3587 | |
3588 | This is the same as \c replace(ch, "", cs). |
3589 | |
3590 | \include qstring.cpp shrinking-erase |
3591 | |
3592 | \sa replace() |
3593 | */ |
3594 | QString &QString::remove(QChar ch, Qt::CaseSensitivity cs) |
3595 | { |
3596 | const qsizetype idx = indexOf(c: ch, from: 0, cs); |
3597 | if (idx == -1) |
3598 | return *this; |
3599 | |
3600 | const bool isCase = cs == Qt::CaseSensitive; |
3601 | ch = isCase ? ch : ch.toCaseFolded(); |
3602 | auto match = [ch, isCase](QChar x) { |
3603 | return ch == (isCase ? x : x.toCaseFolded()); |
3604 | }; |
3605 | |
3606 | |
3607 | auto begin = d.begin(); |
3608 | auto first_match = begin + idx; |
3609 | auto end = d.end(); |
3610 | if (!d->isShared()) { |
3611 | auto it = std::remove_if(first: first_match, last: end, pred: match); |
3612 | d->erase(b: it, n: std::distance(first: it, last: end)); |
3613 | d.data()[d.size] = u'\0'; |
3614 | } else { |
3615 | // Instead of detaching, create a new string and copy all characters except for |
3616 | // the ones we're removing |
3617 | // TODO: size() is more than the needed since "copy" would be shorter |
3618 | QString copy{size(), Qt::Uninitialized}; |
3619 | auto dst = copy.d.begin(); |
3620 | auto it = std::copy(first: begin, last: first_match, result: dst); // Chunk before idx |
3621 | it = std::remove_copy_if(first: first_match + 1, last: end, result: it, pred: match); |
3622 | copy.d.size = std::distance(first: dst, last: it); |
3623 | copy.d.data()[copy.d.size] = u'\0'; |
3624 | *this = copy; |
3625 | } |
3626 | return *this; |
3627 | } |
3628 | |
3629 | /*! |
3630 | \fn QString &QString::remove(const QRegularExpression &re) |
3631 | \since 5.0 |
3632 | |
3633 | Removes every occurrence of the regular expression \a re in the |
3634 | string, and returns a reference to the string. For example: |
3635 | |
3636 | \snippet qstring/main.cpp 96 |
3637 | |
3638 | \include qstring.cpp shrinking-erase |
3639 | |
3640 | \sa indexOf(), lastIndexOf(), replace() |
3641 | */ |
3642 | |
3643 | /*! |
3644 | \fn template <typename Predicate> QString &QString::removeIf(Predicate pred) |
3645 | \since 6.1 |
3646 | |
3647 | Removes all elements for which the predicate \a pred returns true |
3648 | from the string. Returns a reference to the string. |
3649 | |
3650 | \sa remove() |
3651 | */ |
3652 | |
3653 | |
3654 | /*! \internal |
3655 | Instead of detaching, or reallocating if "before" is shorter than "after" |
3656 | and there isn't enough capacity, create a new string, copy characters to it |
3657 | as needed, then swap it with "str". |
3658 | */ |
3659 | static void replace_with_copy(QString &str, size_t *indices, qsizetype nIndices, qsizetype blen, |
3660 | QStringView after) |
3661 | { |
3662 | const qsizetype alen = after.size(); |
3663 | const char16_t *after_b = after.utf16(); |
3664 | |
3665 | const QString::DataPointer &str_d = str.data_ptr(); |
3666 | auto src_start = str_d.begin(); |
3667 | const qsizetype newSize = str_d.size + nIndices * (alen - blen); |
3668 | QString copy{ newSize, Qt::Uninitialized }; |
3669 | QString::DataPointer ©_d = copy.data_ptr(); |
3670 | auto dst = copy_d.begin(); |
3671 | for (int i = 0; i < nIndices; ++i) { |
3672 | auto hit = str_d.begin() + indices[i]; |
3673 | dst = std::copy(first: src_start, last: hit, result: dst); |
3674 | dst = std::copy_n(first: after_b, n: alen, result: dst); |
3675 | src_start = hit + blen; |
3676 | } |
3677 | dst = std::copy(first: src_start, last: str_d.end(), result: dst); |
3678 | str.swap(other&: copy); |
3679 | } |
3680 | |
3681 | // No detaching or reallocation is needed |
3682 | static void replace_in_place(QString &str, size_t *indices, qsizetype nIndices, |
3683 | qsizetype blen, QStringView after) |
3684 | { |
3685 | const qsizetype alen = after.size(); |
3686 | const char16_t *after_b = after.utf16(); |
3687 | const char16_t *after_e = after.utf16() + after.size(); |
3688 | |
3689 | if (blen == alen) { // Replace in place |
3690 | for (qsizetype i = 0; i < nIndices; ++i) |
3691 | std::copy_n(first: after_b, n: alen, result: str.data_ptr().begin() + indices[i]); |
3692 | } else if (blen > alen) { // Replace from front |
3693 | char16_t *begin = str.data_ptr().begin(); |
3694 | char16_t *hit = begin + indices[0]; |
3695 | char16_t *to = hit; |
3696 | to = std::copy_n(first: after_b, n: alen, result: to); |
3697 | char16_t *movestart = hit + blen; |
3698 | for (qsizetype i = 1; i < nIndices; ++i) { |
3699 | hit = begin + indices[i]; |
3700 | to = std::move(first: movestart, last: hit, result: to); |
3701 | to = std::copy_n(first: after_b, n: alen, result: to); |
3702 | movestart = hit + blen; |
3703 | } |
3704 | to = std::move(first: movestart, last: str.data_ptr().end(), result: to); |
3705 | str.resize(size: std::distance(first: begin, last: to)); |
3706 | } else { // blen < alen, Replace from back |
3707 | const qsizetype oldSize = str.data_ptr().size; |
3708 | const qsizetype adjust = nIndices * (alen - blen); |
3709 | const qsizetype newSize = oldSize + adjust; |
3710 | |
3711 | str.resize(size: newSize); |
3712 | char16_t *begin = str.data_ptr().begin(); |
3713 | char16_t *moveend = begin + oldSize; |
3714 | char16_t *to = str.data_ptr().end(); |
3715 | |
3716 | while (nIndices) { |
3717 | --nIndices; |
3718 | char16_t *hit = begin + indices[nIndices]; |
3719 | char16_t *movestart = hit + blen; |
3720 | to = std::move_backward(first: movestart, last: moveend, result: to); |
3721 | to = std::copy_backward(first: after_b, last: after_e, result: to); |
3722 | moveend = hit; |
3723 | } |
3724 | } |
3725 | } |
3726 | |
3727 | static void replace_helper(QString &str, size_t *indices, qsizetype nIndices, qsizetype blen, QStringView after) |
3728 | { |
3729 | const qsizetype oldSize = str.data_ptr().size; |
3730 | const qsizetype adjust = nIndices * (after.size() - blen); |
3731 | const qsizetype newSize = oldSize + adjust; |
3732 | if (str.data_ptr().needsDetach() || needsReallocate(str, newSize)) { |
3733 | replace_with_copy(str, indices, nIndices, blen, after); |
3734 | return; |
3735 | } |
3736 | |
3737 | if (QtPrivate::q_points_into_range(p: after.begin(), c: str)) |
3738 | // Copy after if it lies inside our own d.b area (which we could |
3739 | // possibly invalidate via a realloc or modify by replacement) |
3740 | replace_in_place(str, indices, nIndices, blen, after: QVarLengthArray(after.begin(), after.end())); |
3741 | else |
3742 | replace_in_place(str, indices, nIndices, blen, after); |
3743 | } |
3744 | |
3745 | /*! |
3746 | \fn QString &QString::replace(qsizetype position, qsizetype n, const QString &after) |
3747 | |
3748 | Replaces \a n characters beginning at index \a position with |
3749 | the string \a after and returns a reference to this string. |
3750 | |
3751 | \note If the specified \a position index is within the string, |
3752 | but \a position + \a n goes outside the strings range, |
3753 | then \a n will be adjusted to stop at the end of the string. |
3754 | |
3755 | Example: |
3756 | |
3757 | \snippet qstring/main.cpp 40 |
3758 | |
3759 | \sa insert(), remove() |
3760 | */ |
3761 | QString &QString::replace(qsizetype pos, qsizetype len, const QString &after) |
3762 | { |
3763 | return replace(i: pos, len, s: after.constData(), slen: after.size()); |
3764 | } |
3765 | |
3766 | /*! |
3767 | \fn QString &QString::replace(qsizetype position, qsizetype n, const QChar *after, qsizetype alen) |
3768 | \overload replace() |
3769 | Replaces \a n characters beginning at index \a position with the |
3770 | first \a alen characters of the QChar array \a after and returns a |
3771 | reference to this string. |
3772 | */ |
3773 | QString &QString::replace(qsizetype pos, qsizetype len, const QChar *after, qsizetype alen) |
3774 | { |
3775 | if (size_t(pos) > size_t(this->size())) |
3776 | return *this; |
3777 | if (len > this->size() - pos) |
3778 | len = this->size() - pos; |
3779 | |
3780 | size_t index = pos; |
3781 | replace_helper(str&: *this, indices: &index, nIndices: 1, blen: len, after: QStringView{after, alen}); |
3782 | return *this; |
3783 | } |
3784 | |
3785 | /*! |
3786 | \fn QString &QString::replace(qsizetype position, qsizetype n, QChar after) |
3787 | \overload replace() |
3788 | |
3789 | Replaces \a n characters beginning at index \a position with the |
3790 | character \a after and returns a reference to this string. |
3791 | */ |
3792 | QString &QString::replace(qsizetype pos, qsizetype len, QChar after) |
3793 | { |
3794 | return replace(pos, len, after: &after, alen: 1); |
3795 | } |
3796 | |
3797 | /*! |
3798 | \overload replace() |
3799 | Replaces every occurrence of the string \a before with the string \a |
3800 | after and returns a reference to this string. |
3801 | |
3802 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
3803 | |
3804 | Example: |
3805 | |
3806 | \snippet qstring/main.cpp 41 |
3807 | |
3808 | \note The replacement text is not rescanned after it is inserted. |
3809 | |
3810 | Example: |
3811 | |
3812 | \snippet qstring/main.cpp 86 |
3813 | */ |
3814 | QString &QString::replace(const QString &before, const QString &after, Qt::CaseSensitivity cs) |
3815 | { |
3816 | return replace(before: before.constData(), blen: before.size(), after: after.constData(), alen: after.size(), cs); |
3817 | } |
3818 | |
3819 | /*! |
3820 | \since 4.5 |
3821 | \overload replace() |
3822 | |
3823 | Replaces each occurrence in this string of the first \a blen |
3824 | characters of \a before with the first \a alen characters of \a |
3825 | after and returns a reference to this string. |
3826 | |
3827 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
3828 | */ |
3829 | QString &QString::replace(const QChar *before, qsizetype blen, |
3830 | const QChar *after, qsizetype alen, |
3831 | Qt::CaseSensitivity cs) |
3832 | { |
3833 | if (d.size == 0) { |
3834 | if (blen) |
3835 | return *this; |
3836 | } else { |
3837 | if (cs == Qt::CaseSensitive && before == after && blen == alen) |
3838 | return *this; |
3839 | } |
3840 | if (alen == 0 && blen == 0) |
3841 | return *this; |
3842 | if (alen == 1 && blen == 1) |
3843 | return replace(before: *before, after: *after, cs); |
3844 | |
3845 | QStringMatcher matcher(before, blen, cs); |
3846 | |
3847 | qsizetype index = 0; |
3848 | |
3849 | QVarLengthArray<size_t> indices; |
3850 | while ((index = matcher.indexIn(str: *this, from: index)) != -1) { |
3851 | indices.push_back(t: index); |
3852 | if (blen) // Step over before: |
3853 | index += blen; |
3854 | else // Only count one instance of empty between any two characters: |
3855 | index++; |
3856 | } |
3857 | if (indices.isEmpty()) |
3858 | return *this; |
3859 | |
3860 | replace_helper(str&: *this, indices: indices.data(), nIndices: indices.size(), blen, after: QStringView{after, alen}); |
3861 | return *this; |
3862 | } |
3863 | |
3864 | /*! |
3865 | \overload replace() |
3866 | Replaces every occurrence of the character \a ch in the string with |
3867 | \a after and returns a reference to this string. |
3868 | |
3869 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
3870 | */ |
3871 | QString& QString::replace(QChar ch, const QString &after, Qt::CaseSensitivity cs) |
3872 | { |
3873 | if (after.size() == 0) |
3874 | return remove(ch, cs); |
3875 | |
3876 | if (after.size() == 1) |
3877 | return replace(before: ch, after: after.front(), cs); |
3878 | |
3879 | if (size() == 0) |
3880 | return *this; |
3881 | |
3882 | const char16_t cc = (cs == Qt::CaseSensitive ? ch.unicode() : ch.toCaseFolded().unicode()); |
3883 | |
3884 | QVarLengthArray<size_t> indices; |
3885 | if (cs == Qt::CaseSensitive) { |
3886 | const char16_t *begin = d.begin(); |
3887 | const char16_t *end = d.end(); |
3888 | QStringView view(begin, end); |
3889 | const char16_t *hit = nullptr; |
3890 | while ((hit = QtPrivate::qustrchr(str: view, c: cc)) != end) { |
3891 | indices.push_back(t: std::distance(first: begin, last: hit)); |
3892 | view = QStringView(std::next(x: hit), end); |
3893 | } |
3894 | } else { |
3895 | for (qsizetype i = 0; i < d.size; ++i) |
3896 | if (QChar::toCaseFolded(ucs4: d.data()[i]) == cc) |
3897 | indices.push_back(t: i); |
3898 | } |
3899 | if (indices.isEmpty()) |
3900 | return *this; |
3901 | |
3902 | replace_helper(str&: *this, indices: indices.data(), nIndices: indices.size(), blen: 1, after); |
3903 | return *this; |
3904 | } |
3905 | |
3906 | /*! |
3907 | \overload replace() |
3908 | Replaces every occurrence of the character \a before with the |
3909 | character \a after and returns a reference to this string. |
3910 | |
3911 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
3912 | */ |
3913 | QString& QString::replace(QChar before, QChar after, Qt::CaseSensitivity cs) |
3914 | { |
3915 | const qsizetype idx = indexOf(c: before, from: 0, cs); |
3916 | if (idx == -1) |
3917 | return *this; |
3918 | |
3919 | const char16_t achar = after.unicode(); |
3920 | char16_t bchar = before.unicode(); |
3921 | |
3922 | auto matchesCIS = [](char16_t beforeChar) { |
3923 | return [beforeChar](char16_t ch) { return foldAndCompare(a: ch, b: beforeChar); }; |
3924 | }; |
3925 | |
3926 | auto hit = d.begin() + idx; |
3927 | if (!d.needsDetach()) { |
3928 | *hit++ = achar; |
3929 | if (cs == Qt::CaseSensitive) { |
3930 | std::replace(first: hit, last: d.end(), old_value: bchar, new_value: achar); |
3931 | } else { |
3932 | bchar = foldCase(ch: bchar); |
3933 | std::replace_if(first: hit, last: d.end(), pred: matchesCIS(bchar), new_value: achar); |
3934 | } |
3935 | } else { |
3936 | QString other{ d.size, Qt::Uninitialized }; |
3937 | auto dest = std::copy(first: d.begin(), last: hit, result: other.d.begin()); |
3938 | *dest++ = achar; |
3939 | ++hit; |
3940 | if (cs == Qt::CaseSensitive) { |
3941 | std::replace_copy(first: hit, last: d.end(), result: dest, old_value: bchar, new_value: achar); |
3942 | } else { |
3943 | bchar = foldCase(ch: bchar); |
3944 | std::replace_copy_if(first: hit, last: d.end(), result: dest, pred: matchesCIS(bchar), new_value: achar); |
3945 | } |
3946 | |
3947 | swap(other); |
3948 | } |
3949 | return *this; |
3950 | } |
3951 | |
3952 | /*! |
3953 | \since 4.5 |
3954 | \overload replace() |
3955 | |
3956 | Replaces every occurrence in this string of the Latin-1 string viewed |
3957 | by \a before with the Latin-1 string viewed by \a after, and returns a |
3958 | reference to this string. |
3959 | |
3960 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
3961 | |
3962 | \note The text is not rescanned after a replacement. |
3963 | */ |
3964 | QString &QString::replace(QLatin1StringView before, QLatin1StringView after, Qt::CaseSensitivity cs) |
3965 | { |
3966 | const qsizetype alen = after.size(); |
3967 | const qsizetype blen = before.size(); |
3968 | if (blen == 1 && alen == 1) |
3969 | return replace(before: before.front(), after: after.front(), cs); |
3970 | |
3971 | QVarLengthArray<char16_t> a = qt_from_latin1_to_qvla(str: after); |
3972 | QVarLengthArray<char16_t> b = qt_from_latin1_to_qvla(str: before); |
3973 | return replace(before: (const QChar *)b.data(), blen, after: (const QChar *)a.data(), alen, cs); |
3974 | } |
3975 | |
3976 | /*! |
3977 | \since 4.5 |
3978 | \overload replace() |
3979 | |
3980 | Replaces every occurrence in this string of the Latin-1 string viewed |
3981 | by \a before with the string \a after, and returns a reference to this |
3982 | string. |
3983 | |
3984 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
3985 | |
3986 | \note The text is not rescanned after a replacement. |
3987 | */ |
3988 | QString &QString::replace(QLatin1StringView before, const QString &after, Qt::CaseSensitivity cs) |
3989 | { |
3990 | const qsizetype blen = before.size(); |
3991 | if (blen == 1 && after.size() == 1) |
3992 | return replace(before: before.front(), after: after.front(), cs); |
3993 | |
3994 | QVarLengthArray<char16_t> b = qt_from_latin1_to_qvla(str: before); |
3995 | return replace(before: (const QChar *)b.data(), blen, after: after.constData(), alen: after.d.size, cs); |
3996 | } |
3997 | |
3998 | /*! |
3999 | \since 4.5 |
4000 | \overload replace() |
4001 | |
4002 | Replaces every occurrence of the string \a before with the string \a |
4003 | after and returns a reference to this string. |
4004 | |
4005 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
4006 | |
4007 | \note The text is not rescanned after a replacement. |
4008 | */ |
4009 | QString &QString::replace(const QString &before, QLatin1StringView after, Qt::CaseSensitivity cs) |
4010 | { |
4011 | const qsizetype alen = after.size(); |
4012 | if (before.size() == 1 && alen == 1) |
4013 | return replace(before: before.front(), after: after.front(), cs); |
4014 | |
4015 | QVarLengthArray<char16_t> a = qt_from_latin1_to_qvla(str: after); |
4016 | return replace(before: before.constData(), blen: before.d.size, after: (const QChar *)a.data(), alen, cs); |
4017 | } |
4018 | |
4019 | /*! |
4020 | \since 4.5 |
4021 | \overload replace() |
4022 | |
4023 | Replaces every occurrence of the character \a c with the string \a |
4024 | after and returns a reference to this string. |
4025 | |
4026 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
4027 | |
4028 | \note The text is not rescanned after a replacement. |
4029 | */ |
4030 | QString &QString::replace(QChar c, QLatin1StringView after, Qt::CaseSensitivity cs) |
4031 | { |
4032 | const qsizetype alen = after.size(); |
4033 | if (alen == 1) |
4034 | return replace(before: c, after: after.front(), cs); |
4035 | |
4036 | QVarLengthArray<char16_t> a = qt_from_latin1_to_qvla(str: after); |
4037 | return replace(before: &c, blen: 1, after: (const QChar *)a.data(), alen, cs); |
4038 | } |
4039 | |
4040 | /*! |
4041 | \fn bool QString::operator==(const QString &s1, const QString &s2) |
4042 | \overload operator==() |
4043 | |
4044 | Returns \c true if string \a s1 is equal to string \a s2; otherwise |
4045 | returns \c false. |
4046 | |
4047 | \include qstring.cpp compare-isNull-vs-isEmpty |
4048 | |
4049 | \sa {Comparing Strings} |
4050 | */ |
4051 | |
4052 | /*! |
4053 | \fn bool QString::operator==(const QString &s1, QLatin1StringView s2) |
4054 | |
4055 | \overload operator==() |
4056 | |
4057 | Returns \c true if \a s1 is equal to \a s2; otherwise |
4058 | returns \c false. |
4059 | */ |
4060 | |
4061 | /*! |
4062 | \fn bool QString::operator==(QLatin1StringView s1, const QString &s2) |
4063 | |
4064 | \overload operator==() |
4065 | |
4066 | Returns \c true if \a s1 is equal to \a s2; otherwise |
4067 | returns \c false. |
4068 | */ |
4069 | |
4070 | /*! \fn bool QString::operator==(const QByteArray &other) const |
4071 | |
4072 | \overload operator==() |
4073 | |
4074 | The \a other byte array is converted to a QString using the |
4075 | fromUtf8() function. |
4076 | |
4077 | You can disable this operator by defining |
4078 | \l QT_NO_CAST_FROM_ASCII when you compile your applications. This |
4079 | can be useful if you want to ensure that all user-visible strings |
4080 | go through QObject::tr(), for example. |
4081 | |
4082 | Returns \c true if this string is lexically equal to the parameter |
4083 | string \a other. Otherwise returns \c false. |
4084 | */ |
4085 | |
4086 | /*! \fn bool QString::operator==(const char *other) const |
4087 | |
4088 | \overload operator==() |
4089 | |
4090 | The \a other const char pointer is converted to a QString using |
4091 | the fromUtf8() function. |
4092 | |
4093 | You can disable this operator by defining |
4094 | \l QT_NO_CAST_FROM_ASCII when you compile your applications. This |
4095 | can be useful if you want to ensure that all user-visible strings |
4096 | go through QObject::tr(), for example. |
4097 | */ |
4098 | |
4099 | /*! |
4100 | \fn bool QString::operator<(const QString &s1, const QString &s2) |
4101 | |
4102 | \overload operator<() |
4103 | |
4104 | Returns \c true if string \a s1 is lexically less than string |
4105 | \a s2; otherwise returns \c false. |
4106 | |
4107 | \sa {Comparing Strings} |
4108 | */ |
4109 | |
4110 | /*! |
4111 | \fn bool QString::operator<(const QString &s1, QLatin1StringView s2) |
4112 | |
4113 | \overload operator<() |
4114 | |
4115 | Returns \c true if \a s1 is lexically less than \a s2; |
4116 | otherwise returns \c false. |
4117 | */ |
4118 | |
4119 | /*! |
4120 | \fn bool QString::operator<(QLatin1StringView s1, const QString &s2) |
4121 | |
4122 | \overload operator<() |
4123 | |
4124 | Returns \c true if \a s1 is lexically less than \a s2; |
4125 | otherwise returns \c false. |
4126 | */ |
4127 | |
4128 | /*! \fn bool QString::operator<(const QByteArray &other) const |
4129 | |
4130 | \overload operator<() |
4131 | |
4132 | The \a other byte array is converted to a QString using the |
4133 | fromUtf8() function. If any NUL characters ('\\0') are embedded |
4134 | in the byte array, they will be included in the transformation. |
4135 | |
4136 | You can disable this operator |
4137 | \l QT_NO_CAST_FROM_ASCII when you compile your applications. This |
4138 | can be useful if you want to ensure that all user-visible strings |
4139 | go through QObject::tr(), for example. |
4140 | */ |
4141 | |
4142 | /*! \fn bool QString::operator<(const char *other) const |
4143 | |
4144 | Returns \c true if this string is lexically less than string \a other. |
4145 | Otherwise returns \c false. |
4146 | |
4147 | \overload operator<() |
4148 | |
4149 | The \a other const char pointer is converted to a QString using |
4150 | the fromUtf8() function. |
4151 | |
4152 | You can disable this operator by defining |
4153 | \l QT_NO_CAST_FROM_ASCII when you compile your applications. This |
4154 | can be useful if you want to ensure that all user-visible strings |
4155 | go through QObject::tr(), for example. |
4156 | */ |
4157 | |
4158 | /*! \fn bool QString::operator<=(const QString &s1, const QString &s2) |
4159 | |
4160 | Returns \c true if string \a s1 is lexically less than or equal to |
4161 | string \a s2; otherwise returns \c false. |
4162 | |
4163 | \sa {Comparing Strings} |
4164 | */ |
4165 | |
4166 | /*! |
4167 | \fn bool QString::operator<=(const QString &s1, QLatin1StringView s2) |
4168 | |
4169 | \overload operator<=() |
4170 | |
4171 | Returns \c true if \a s1 is lexically less than or equal to \a s2; |
4172 | otherwise returns \c false. |
4173 | */ |
4174 | |
4175 | /*! |
4176 | \fn bool QString::operator<=(QLatin1StringView s1, const QString &s2) |
4177 | |
4178 | \overload operator<=() |
4179 | |
4180 | Returns \c true if \a s1 is lexically less than or equal to \a s2; |
4181 | otherwise returns \c false. |
4182 | */ |
4183 | |
4184 | /*! \fn bool QString::operator<=(const QByteArray &other) const |
4185 | |
4186 | \overload operator<=() |
4187 | |
4188 | The \a other byte array is converted to a QString using the |
4189 | fromUtf8() function. If any NUL characters ('\\0') are embedded |
4190 | in the byte array, they will be included in the transformation. |
4191 | |
4192 | You can disable this operator by defining |
4193 | \l QT_NO_CAST_FROM_ASCII when you compile your applications. This |
4194 | can be useful if you want to ensure that all user-visible strings |
4195 | go through QObject::tr(), for example. |
4196 | */ |
4197 | |
4198 | /*! \fn bool QString::operator<=(const char *other) const |
4199 | |
4200 | \overload operator<=() |
4201 | |
4202 | The \a other const char pointer is converted to a QString using |
4203 | the fromUtf8() function. |
4204 | |
4205 | You can disable this operator by defining |
4206 | \l QT_NO_CAST_FROM_ASCII when you compile your applications. This |
4207 | can be useful if you want to ensure that all user-visible strings |
4208 | go through QObject::tr(), for example. |
4209 | */ |
4210 | |
4211 | /*! \fn bool QString::operator>(const QString &s1, const QString &s2) |
4212 | |
4213 | Returns \c true if string \a s1 is lexically greater than string \a s2; |
4214 | otherwise returns \c false. |
4215 | |
4216 | \sa {Comparing Strings} |
4217 | */ |
4218 | |
4219 | /*! |
4220 | \fn bool QString::operator>(const QString &s1, QLatin1StringView s2) |
4221 | |
4222 | \overload operator>() |
4223 | |
4224 | Returns \c true if \a s1 is lexically greater than \a s2; |
4225 | otherwise returns \c false. |
4226 | */ |
4227 | |
4228 | /*! |
4229 | \fn bool QString::operator>(QLatin1StringView s1, const QString &s2) |
4230 | |
4231 | \overload operator>() |
4232 | |
4233 | Returns \c true if \a s1 is lexically greater than \a s2; |
4234 | otherwise returns \c false. |
4235 | */ |
4236 | |
4237 | /*! \fn bool QString::operator>(const QByteArray &other) const |
4238 | |
4239 | \overload operator>() |
4240 | |
4241 | The \a other byte array is converted to a QString using the |
4242 | fromUtf8() function. If any NUL characters ('\\0') are embedded |
4243 | in the byte array, they will be included in the transformation. |
4244 | |
4245 | You can disable this operator by defining |
4246 | \l QT_NO_CAST_FROM_ASCII when you compile your applications. This |
4247 | can be useful if you want to ensure that all user-visible strings |
4248 | go through QObject::tr(), for example. |
4249 | */ |
4250 | |
4251 | /*! \fn bool QString::operator>(const char *other) const |
4252 | |
4253 | \overload operator>() |
4254 | |
4255 | The \a other const char pointer is converted to a QString using |
4256 | the fromUtf8() function. |
4257 | |
4258 | You can disable this operator by defining \l QT_NO_CAST_FROM_ASCII |
4259 | when you compile your applications. This can be useful if you want |
4260 | to ensure that all user-visible strings go through QObject::tr(), |
4261 | for example. |
4262 | */ |
4263 | |
4264 | /*! \fn bool QString::operator>=(const QString &s1, const QString &s2) |
4265 | |
4266 | Returns \c true if string \a s1 is lexically greater than or equal to |
4267 | string \a s2; otherwise returns \c false. |
4268 | |
4269 | \sa {Comparing Strings} |
4270 | */ |
4271 | |
4272 | /*! |
4273 | \fn bool QString::operator>=(const QString &s1, QLatin1StringView s2) |
4274 | |
4275 | \overload operator>=() |
4276 | |
4277 | Returns \c true if \a s1 is lexically greater than or equal to \a s2; |
4278 | otherwise returns \c false. |
4279 | */ |
4280 | |
4281 | /*! |
4282 | \fn bool QString::operator>=(QLatin1StringView s1, const QString &s2) |
4283 | |
4284 | \overload operator>=() |
4285 | |
4286 | Returns \c true if \a s1 is lexically greater than or equal to \a s2; |
4287 | otherwise returns \c false. |
4288 | */ |
4289 | |
4290 | /*! \fn bool QString::operator>=(const QByteArray &other) const |
4291 | |
4292 | \overload operator>=() |
4293 | |
4294 | The \a other byte array is converted to a QString using the |
4295 | fromUtf8() function. If any NUL characters ('\\0') are embedded in |
4296 | the byte array, they will be included in the transformation. |
4297 | |
4298 | You can disable this operator by defining \l QT_NO_CAST_FROM_ASCII |
4299 | when you compile your applications. This can be useful if you want |
4300 | to ensure that all user-visible strings go through QObject::tr(), |
4301 | for example. |
4302 | */ |
4303 | |
4304 | /*! \fn bool QString::operator>=(const char *other) const |
4305 | |
4306 | \overload operator>=() |
4307 | |
4308 | The \a other const char pointer is converted to a QString using |
4309 | the fromUtf8() function. |
4310 | |
4311 | You can disable this operator by defining \l QT_NO_CAST_FROM_ASCII |
4312 | when you compile your applications. This can be useful if you want |
4313 | to ensure that all user-visible strings go through QObject::tr(), |
4314 | for example. |
4315 | */ |
4316 | |
4317 | /*! \fn bool QString::operator!=(const QString &s1, const QString &s2) |
4318 | |
4319 | Returns \c true if string \a s1 is not equal to string \a s2; |
4320 | otherwise returns \c false. |
4321 | |
4322 | \sa {Comparing Strings} |
4323 | */ |
4324 | |
4325 | /*! \fn bool QString::operator!=(const QString &s1, QLatin1StringView s2) |
4326 | |
4327 | Returns \c true if string \a s1 is not equal to string \a s2. |
4328 | Otherwise returns \c false. |
4329 | |
4330 | \overload operator!=() |
4331 | */ |
4332 | |
4333 | /*! \fn bool QString::operator!=(const QByteArray &other) const |
4334 | |
4335 | \overload operator!=() |
4336 | |
4337 | The \a other byte array is converted to a QString using the |
4338 | fromUtf8() function. If any NUL characters ('\\0') are embedded |
4339 | in the byte array, they will be included in the transformation. |
4340 | |
4341 | You can disable this operator by defining \l QT_NO_CAST_FROM_ASCII |
4342 | when you compile your applications. This can be useful if you want |
4343 | to ensure that all user-visible strings go through QObject::tr(), |
4344 | for example. |
4345 | */ |
4346 | |
4347 | /*! \fn bool QString::operator!=(const char *other) const |
4348 | |
4349 | \overload operator!=() |
4350 | |
4351 | The \a other const char pointer is converted to a QString using |
4352 | the fromUtf8() function. |
4353 | |
4354 | You can disable this operator by defining |
4355 | \l QT_NO_CAST_FROM_ASCII when you compile your applications. This |
4356 | can be useful if you want to ensure that all user-visible strings |
4357 | go through QObject::tr(), for example. |
4358 | */ |
4359 | |
4360 | /*! |
4361 | \include qstring.qdocinc {qstring-first-index-of} {string} {str} |
4362 | |
4363 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
4364 | |
4365 | Example: |
4366 | |
4367 | \snippet qstring/main.cpp 24 |
4368 | |
4369 | \include qstring.qdocinc negative-index-start-search-from-end |
4370 | |
4371 | \sa lastIndexOf(), contains(), count() |
4372 | */ |
4373 | qsizetype QString::indexOf(const QString &str, qsizetype from, Qt::CaseSensitivity cs) const |
4374 | { |
4375 | return QtPrivate::findString(haystack: QStringView(unicode(), size()), from, needle: QStringView(str.unicode(), str.size()), cs); |
4376 | } |
4377 | |
4378 | /*! |
4379 | \fn qsizetype QString::indexOf(QStringView str, qsizetype from, Qt::CaseSensitivity cs) const |
4380 | \since 5.14 |
4381 | \overload indexOf() |
4382 | |
4383 | \include qstring.qdocinc {qstring-first-index-of} {string view} {str} |
4384 | |
4385 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
4386 | |
4387 | \include qstring.qdocinc negative-index-start-search-from-end |
4388 | |
4389 | \sa QStringView::indexOf(), lastIndexOf(), contains(), count() |
4390 | */ |
4391 | |
4392 | /*! |
4393 | \since 4.5 |
4394 | |
4395 | \include {qstring.qdocinc} {qstring-first-index-of} {Latin-1 string viewed by} {str} |
4396 | |
4397 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
4398 | |
4399 | Example: |
4400 | |
4401 | \snippet qstring/main.cpp 24 |
4402 | |
4403 | \include qstring.qdocinc negative-index-start-search-from-end |
4404 | |
4405 | \sa lastIndexOf(), contains(), count() |
4406 | */ |
4407 | |
4408 | qsizetype QString::indexOf(QLatin1StringView str, qsizetype from, Qt::CaseSensitivity cs) const |
4409 | { |
4410 | return QtPrivate::findString(haystack: QStringView(unicode(), size()), from, needle: str, cs); |
4411 | } |
4412 | |
4413 | /*! |
4414 | \overload indexOf() |
4415 | |
4416 | \include qstring.qdocinc {qstring-first-index-of} {character} {ch} |
4417 | */ |
4418 | qsizetype QString::indexOf(QChar ch, qsizetype from, Qt::CaseSensitivity cs) const |
4419 | { |
4420 | return qFindChar(str: QStringView(unicode(), size()), ch, from, cs); |
4421 | } |
4422 | |
4423 | /*! |
4424 | \include qstring.qdocinc {qstring-last-index-of} {string} {str} |
4425 | |
4426 | \include qstring.qdocinc negative-index-start-search-from-end |
4427 | |
4428 | Returns -1 if \a str is not found. |
4429 | |
4430 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
4431 | |
4432 | Example: |
4433 | |
4434 | \snippet qstring/main.cpp 29 |
4435 | |
4436 | \note When searching for a 0-length \a str, the match at the end of |
4437 | the data is excluded from the search by a negative \a from, even |
4438 | though \c{-1} is normally thought of as searching from the end of the |
4439 | string: the match at the end is \e after the last character, so it is |
4440 | excluded. To include such a final empty match, either give a positive |
4441 | value for \a from or omit the \a from parameter entirely. |
4442 | |
4443 | \sa indexOf(), contains(), count() |
4444 | */ |
4445 | qsizetype QString::lastIndexOf(const QString &str, qsizetype from, Qt::CaseSensitivity cs) const |
4446 | { |
4447 | return QtPrivate::lastIndexOf(haystack: QStringView(*this), from, needle: str, cs); |
4448 | } |
4449 | |
4450 | /*! |
4451 | \fn qsizetype QString::lastIndexOf(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
4452 | \since 6.2 |
4453 | \overload lastIndexOf() |
4454 | |
4455 | Returns the index position of the last occurrence of the string \a |
4456 | str in this string. Returns -1 if \a str is not found. |
4457 | |
4458 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
4459 | |
4460 | Example: |
4461 | |
4462 | \snippet qstring/main.cpp 29 |
4463 | |
4464 | \sa indexOf(), contains(), count() |
4465 | */ |
4466 | |
4467 | |
4468 | /*! |
4469 | \since 4.5 |
4470 | \overload lastIndexOf() |
4471 | |
4472 | \include qstring.qdocinc {qstring-last-index-of} {Latin-1 string viewed by} {str} |
4473 | |
4474 | \include qstring.qdocinc negative-index-start-search-from-end |
4475 | |
4476 | Returns -1 if \a str is not found. |
4477 | |
4478 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
4479 | |
4480 | Example: |
4481 | |
4482 | \snippet qstring/main.cpp 29 |
4483 | |
4484 | \note When searching for a 0-length \a str, the match at the end of |
4485 | the data is excluded from the search by a negative \a from, even |
4486 | though \c{-1} is normally thought of as searching from the end of the |
4487 | string: the match at the end is \e after the last character, so it is |
4488 | excluded. To include such a final empty match, either give a positive |
4489 | value for \a from or omit the \a from parameter entirely. |
4490 | |
4491 | \sa indexOf(), contains(), count() |
4492 | */ |
4493 | qsizetype QString::lastIndexOf(QLatin1StringView str, qsizetype from, Qt::CaseSensitivity cs) const |
4494 | { |
4495 | return QtPrivate::lastIndexOf(haystack: *this, from, needle: str, cs); |
4496 | } |
4497 | |
4498 | /*! |
4499 | \fn qsizetype QString::lastIndexOf(QLatin1StringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
4500 | \since 6.2 |
4501 | \overload lastIndexOf() |
4502 | |
4503 | Returns the index position of the last occurrence of the string \a |
4504 | str in this string. Returns -1 if \a str is not found. |
4505 | |
4506 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
4507 | |
4508 | Example: |
4509 | |
4510 | \snippet qstring/main.cpp 29 |
4511 | |
4512 | \sa indexOf(), contains(), count() |
4513 | */ |
4514 | |
4515 | /*! |
4516 | \overload lastIndexOf() |
4517 | |
4518 | \include qstring.qdocinc {qstring-last-index-of} {character} {ch} |
4519 | */ |
4520 | qsizetype QString::lastIndexOf(QChar ch, qsizetype from, Qt::CaseSensitivity cs) const |
4521 | { |
4522 | return qLastIndexOf(haystack: QStringView(*this), needle: ch, from, cs); |
4523 | } |
4524 | |
4525 | /*! |
4526 | \fn QString::lastIndexOf(QChar ch, Qt::CaseSensitivity) const |
4527 | \since 6.3 |
4528 | \overload lastIndexOf() |
4529 | */ |
4530 | |
4531 | /*! |
4532 | \fn qsizetype QString::lastIndexOf(QStringView str, qsizetype from, Qt::CaseSensitivity cs) const |
4533 | \since 5.14 |
4534 | \overload lastIndexOf() |
4535 | |
4536 | \include qstring.qdocinc {qstring-last-index-of} {string view} {str} |
4537 | |
4538 | \include qstring.qdocinc negative-index-start-search-from-end |
4539 | |
4540 | Returns -1 if \a str is not found. |
4541 | |
4542 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
4543 | |
4544 | \note When searching for a 0-length \a str, the match at the end of |
4545 | the data is excluded from the search by a negative \a from, even |
4546 | though \c{-1} is normally thought of as searching from the end of the |
4547 | string: the match at the end is \e after the last character, so it is |
4548 | excluded. To include such a final empty match, either give a positive |
4549 | value for \a from or omit the \a from parameter entirely. |
4550 | |
4551 | \sa indexOf(), contains(), count() |
4552 | */ |
4553 | |
4554 | /*! |
4555 | \fn qsizetype QString::lastIndexOf(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
4556 | \since 6.2 |
4557 | \overload lastIndexOf() |
4558 | |
4559 | Returns the index position of the last occurrence of the string view \a |
4560 | str in this string. Returns -1 if \a str is not found. |
4561 | |
4562 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
4563 | |
4564 | \sa indexOf(), contains(), count() |
4565 | */ |
4566 | |
4567 | #if QT_CONFIG(regularexpression) |
4568 | struct QStringCapture |
4569 | { |
4570 | qsizetype pos; |
4571 | qsizetype len; |
4572 | int no; |
4573 | }; |
4574 | Q_DECLARE_TYPEINFO(QStringCapture, Q_PRIMITIVE_TYPE); |
4575 | |
4576 | /*! |
4577 | \overload replace() |
4578 | \since 5.0 |
4579 | |
4580 | Replaces every occurrence of the regular expression \a re in the |
4581 | string with \a after. Returns a reference to the string. For |
4582 | example: |
4583 | |
4584 | \snippet qstring/main.cpp 87 |
4585 | |
4586 | For regular expressions containing capturing groups, |
4587 | occurrences of \b{\\1}, \b{\\2}, ..., in \a after are replaced |
4588 | with the string captured by the corresponding capturing group. |
4589 | |
4590 | \snippet qstring/main.cpp 88 |
4591 | |
4592 | \sa indexOf(), lastIndexOf(), remove(), QRegularExpression, QRegularExpressionMatch |
4593 | */ |
4594 | QString &QString::replace(const QRegularExpression &re, const QString &after) |
4595 | { |
4596 | if (!re.isValid()) { |
4597 | qtWarnAboutInvalidRegularExpression(pattern: re.pattern(), where: "QString::replace" ); |
4598 | return *this; |
4599 | } |
4600 | |
4601 | const QString copy(*this); |
4602 | QRegularExpressionMatchIterator iterator = re.globalMatch(subject: copy); |
4603 | if (!iterator.hasNext()) // no matches at all |
4604 | return *this; |
4605 | |
4606 | reallocData(alloc: d.size, option: QArrayData::KeepSize); |
4607 | |
4608 | qsizetype numCaptures = re.captureCount(); |
4609 | |
4610 | // 1. build the backreferences list, holding where the backreferences |
4611 | // are in the replacement string |
4612 | QList<QStringCapture> backReferences; |
4613 | const qsizetype al = after.size(); |
4614 | const QChar *ac = after.unicode(); |
4615 | |
4616 | for (qsizetype i = 0; i < al - 1; i++) { |
4617 | if (ac[i] == u'\\') { |
4618 | int no = ac[i + 1].digitValue(); |
4619 | if (no > 0 && no <= numCaptures) { |
4620 | QStringCapture backReference; |
4621 | backReference.pos = i; |
4622 | backReference.len = 2; |
4623 | |
4624 | if (i < al - 2) { |
4625 | int secondDigit = ac[i + 2].digitValue(); |
4626 | if (secondDigit != -1 && ((no * 10) + secondDigit) <= numCaptures) { |
4627 | no = (no * 10) + secondDigit; |
4628 | ++backReference.len; |
4629 | } |
4630 | } |
4631 | |
4632 | backReference.no = no; |
4633 | backReferences.append(t: backReference); |
4634 | } |
4635 | } |
4636 | } |
4637 | |
4638 | // 2. iterate on the matches. For every match, copy in chunks |
4639 | // - the part before the match |
4640 | // - the after string, with the proper replacements for the backreferences |
4641 | |
4642 | qsizetype newLength = 0; // length of the new string, with all the replacements |
4643 | qsizetype lastEnd = 0; |
4644 | QList<QStringView> chunks; |
4645 | const QStringView copyView{ copy }, afterView{ after }; |
4646 | while (iterator.hasNext()) { |
4647 | QRegularExpressionMatch match = iterator.next(); |
4648 | qsizetype len; |
4649 | // add the part before the match |
4650 | len = match.capturedStart() - lastEnd; |
4651 | if (len > 0) { |
4652 | chunks << copyView.mid(pos: lastEnd, n: len); |
4653 | newLength += len; |
4654 | } |
4655 | |
4656 | lastEnd = 0; |
4657 | // add the after string, with replacements for the backreferences |
4658 | for (const QStringCapture &backReference : std::as_const(t&: backReferences)) { |
4659 | // part of "after" before the backreference |
4660 | len = backReference.pos - lastEnd; |
4661 | if (len > 0) { |
4662 | chunks << afterView.mid(pos: lastEnd, n: len); |
4663 | newLength += len; |
4664 | } |
4665 | |
4666 | // backreference itself |
4667 | len = match.capturedLength(nth: backReference.no); |
4668 | if (len > 0) { |
4669 | chunks << copyView.mid(pos: match.capturedStart(nth: backReference.no), n: len); |
4670 | newLength += len; |
4671 | } |
4672 | |
4673 | lastEnd = backReference.pos + backReference.len; |
4674 | } |
4675 | |
4676 | // add the last part of the after string |
4677 | len = afterView.size() - lastEnd; |
4678 | if (len > 0) { |
4679 | chunks << afterView.mid(pos: lastEnd, n: len); |
4680 | newLength += len; |
4681 | } |
4682 | |
4683 | lastEnd = match.capturedEnd(); |
4684 | } |
4685 | |
4686 | // 3. trailing string after the last match |
4687 | if (copyView.size() > lastEnd) { |
4688 | chunks << copyView.mid(pos: lastEnd); |
4689 | newLength += copyView.size() - lastEnd; |
4690 | } |
4691 | |
4692 | // 4. assemble the chunks together |
4693 | resize(size: newLength); |
4694 | qsizetype i = 0; |
4695 | QChar *uc = data(); |
4696 | for (const QStringView &chunk : std::as_const(t&: chunks)) { |
4697 | qsizetype len = chunk.size(); |
4698 | memcpy(dest: uc + i, src: chunk.constData(), n: len * sizeof(QChar)); |
4699 | i += len; |
4700 | } |
4701 | |
4702 | return *this; |
4703 | } |
4704 | #endif // QT_CONFIG(regularexpression) |
4705 | |
4706 | /*! |
4707 | Returns the number of (potentially overlapping) occurrences of |
4708 | the string \a str in this string. |
4709 | |
4710 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
4711 | |
4712 | \sa contains(), indexOf() |
4713 | */ |
4714 | |
4715 | qsizetype QString::count(const QString &str, Qt::CaseSensitivity cs) const |
4716 | { |
4717 | return QtPrivate::count(haystack: QStringView(unicode(), size()), needle: QStringView(str.unicode(), str.size()), cs); |
4718 | } |
4719 | |
4720 | /*! |
4721 | \overload count() |
4722 | |
4723 | Returns the number of occurrences of character \a ch in the string. |
4724 | |
4725 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
4726 | |
4727 | \sa contains(), indexOf() |
4728 | */ |
4729 | |
4730 | qsizetype QString::count(QChar ch, Qt::CaseSensitivity cs) const |
4731 | { |
4732 | return QtPrivate::count(haystack: QStringView(unicode(), size()), needle: ch, cs); |
4733 | } |
4734 | |
4735 | /*! |
4736 | \since 6.0 |
4737 | \overload count() |
4738 | Returns the number of (potentially overlapping) occurrences of the |
4739 | string view \a str in this string. |
4740 | |
4741 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
4742 | |
4743 | \sa contains(), indexOf() |
4744 | */ |
4745 | qsizetype QString::count(QStringView str, Qt::CaseSensitivity cs) const |
4746 | { |
4747 | return QtPrivate::count(haystack: *this, needle: str, cs); |
4748 | } |
4749 | |
4750 | /*! \fn bool QString::contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
4751 | |
4752 | Returns \c true if this string contains an occurrence of the string |
4753 | \a str; otherwise returns \c false. |
4754 | |
4755 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
4756 | |
4757 | Example: |
4758 | \snippet qstring/main.cpp 17 |
4759 | |
4760 | \sa indexOf(), count() |
4761 | */ |
4762 | |
4763 | /*! \fn bool QString::contains(QLatin1StringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
4764 | \since 5.3 |
4765 | |
4766 | \overload contains() |
4767 | |
4768 | Returns \c true if this string contains an occurrence of the latin-1 string |
4769 | \a str; otherwise returns \c false. |
4770 | */ |
4771 | |
4772 | /*! \fn bool QString::contains(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
4773 | |
4774 | \overload contains() |
4775 | |
4776 | Returns \c true if this string contains an occurrence of the |
4777 | character \a ch; otherwise returns \c false. |
4778 | */ |
4779 | |
4780 | /*! \fn bool QString::contains(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
4781 | \since 5.14 |
4782 | \overload contains() |
4783 | |
4784 | Returns \c true if this string contains an occurrence of the string view |
4785 | \a str; otherwise returns \c false. |
4786 | |
4787 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
4788 | |
4789 | \sa indexOf(), count() |
4790 | */ |
4791 | |
4792 | #if QT_CONFIG(regularexpression) |
4793 | /*! |
4794 | \since 5.5 |
4795 | |
4796 | Returns the index position of the first match of the regular |
4797 | expression \a re in the string, searching forward from index |
4798 | position \a from. Returns -1 if \a re didn't match anywhere. |
4799 | |
4800 | If the match is successful and \a rmatch is not \nullptr, it also |
4801 | writes the results of the match into the QRegularExpressionMatch object |
4802 | pointed to by \a rmatch. |
4803 | |
4804 | Example: |
4805 | |
4806 | \snippet qstring/main.cpp 93 |
4807 | */ |
4808 | qsizetype QString::indexOf(const QRegularExpression &re, qsizetype from, QRegularExpressionMatch *rmatch) const |
4809 | { |
4810 | return QtPrivate::indexOf(viewHaystack: QStringView(*this), stringHaystack: this, re, from, rmatch); |
4811 | } |
4812 | |
4813 | /*! |
4814 | \since 5.5 |
4815 | |
4816 | Returns the index position of the last match of the regular |
4817 | expression \a re in the string, which starts before the index |
4818 | position \a from. |
4819 | |
4820 | \include qstring.qdocinc negative-index-start-search-from-end |
4821 | |
4822 | Returns -1 if \a re didn't match anywhere. |
4823 | |
4824 | If the match is successful and \a rmatch is not \nullptr, it also |
4825 | writes the results of the match into the QRegularExpressionMatch object |
4826 | pointed to by \a rmatch. |
4827 | |
4828 | Example: |
4829 | |
4830 | \snippet qstring/main.cpp 94 |
4831 | |
4832 | \note Due to how the regular expression matching algorithm works, |
4833 | this function will actually match repeatedly from the beginning of |
4834 | the string until the position \a from is reached. |
4835 | |
4836 | \note When searching for a regular expression \a re that may match |
4837 | 0 characters, the match at the end of the data is excluded from the |
4838 | search by a negative \a from, even though \c{-1} is normally |
4839 | thought of as searching from the end of the string: the match at |
4840 | the end is \e after the last character, so it is excluded. To |
4841 | include such a final empty match, either give a positive value for |
4842 | \a from or omit the \a from parameter entirely. |
4843 | */ |
4844 | qsizetype QString::lastIndexOf(const QRegularExpression &re, qsizetype from, QRegularExpressionMatch *rmatch) const |
4845 | { |
4846 | return QtPrivate::lastIndexOf(viewHaystack: QStringView(*this), stringHaystack: this, re, from, rmatch); |
4847 | } |
4848 | |
4849 | /*! |
4850 | \fn qsizetype QString::lastIndexOf(const QRegularExpression &re, QRegularExpressionMatch *rmatch = nullptr) const |
4851 | \since 6.2 |
4852 | \overload lastIndexOf() |
4853 | |
4854 | Returns the index position of the last match of the regular |
4855 | expression \a re in the string. Returns -1 if \a re didn't match anywhere. |
4856 | |
4857 | If the match is successful and \a rmatch is not \nullptr, it also |
4858 | writes the results of the match into the QRegularExpressionMatch object |
4859 | pointed to by \a rmatch. |
4860 | |
4861 | Example: |
4862 | |
4863 | \snippet qstring/main.cpp 94 |
4864 | |
4865 | \note Due to how the regular expression matching algorithm works, |
4866 | this function will actually match repeatedly from the beginning of |
4867 | the string until the end of the string is reached. |
4868 | */ |
4869 | |
4870 | /*! |
4871 | \since 5.1 |
4872 | |
4873 | Returns \c true if the regular expression \a re matches somewhere in this |
4874 | string; otherwise returns \c false. |
4875 | |
4876 | If the match is successful and \a rmatch is not \nullptr, it also |
4877 | writes the results of the match into the QRegularExpressionMatch object |
4878 | pointed to by \a rmatch. |
4879 | |
4880 | \sa QRegularExpression::match() |
4881 | */ |
4882 | |
4883 | bool QString::contains(const QRegularExpression &re, QRegularExpressionMatch *rmatch) const |
4884 | { |
4885 | return QtPrivate::contains(viewHaystack: QStringView(*this), stringHaystack: this, re, rmatch); |
4886 | } |
4887 | |
4888 | /*! |
4889 | \overload count() |
4890 | \since 5.0 |
4891 | |
4892 | Returns the number of times the regular expression \a re matches |
4893 | in the string. |
4894 | |
4895 | For historical reasons, this function counts overlapping matches, |
4896 | so in the example below, there are four instances of "ana" or |
4897 | "ama": |
4898 | |
4899 | \snippet qstring/main.cpp 95 |
4900 | |
4901 | This behavior is different from simply iterating over the matches |
4902 | in the string using QRegularExpressionMatchIterator. |
4903 | |
4904 | \sa QRegularExpression::globalMatch() |
4905 | */ |
4906 | qsizetype QString::count(const QRegularExpression &re) const |
4907 | { |
4908 | return QtPrivate::count(haystack: QStringView(*this), re); |
4909 | } |
4910 | #endif // QT_CONFIG(regularexpression) |
4911 | |
4912 | #if QT_DEPRECATED_SINCE(6, 4) |
4913 | /*! \fn qsizetype QString::count() const |
4914 | \deprecated [6.4] Use size() or length() instead. |
4915 | \overload count() |
4916 | |
4917 | Same as size(). |
4918 | */ |
4919 | #endif |
4920 | |
4921 | /*! |
4922 | \enum QString::SectionFlag |
4923 | |
4924 | This enum specifies flags that can be used to affect various |
4925 | aspects of the section() function's behavior with respect to |
4926 | separators and empty fields. |
4927 | |
4928 | \value SectionDefault Empty fields are counted, leading and |
4929 | trailing separators are not included, and the separator is |
4930 | compared case sensitively. |
4931 | |
4932 | \value SectionSkipEmpty Treat empty fields as if they don't exist, |
4933 | i.e. they are not considered as far as \e start and \e end are |
4934 | concerned. |
4935 | |
4936 | \value SectionIncludeLeadingSep Include the leading separator (if |
4937 | any) in the result string. |
4938 | |
4939 | \value SectionIncludeTrailingSep Include the trailing separator |
4940 | (if any) in the result string. |
4941 | |
4942 | \value SectionCaseInsensitiveSeps Compare the separator |
4943 | case-insensitively. |
4944 | |
4945 | \sa section() |
4946 | */ |
4947 | |
4948 | /*! |
4949 | \fn QString QString::section(QChar sep, qsizetype start, qsizetype end = -1, SectionFlags flags) const |
4950 | |
4951 | This function returns a section of the string. |
4952 | |
4953 | This string is treated as a sequence of fields separated by the |
4954 | character, \a sep. The returned string consists of the fields from |
4955 | position \a start to position \a end inclusive. If \a end is not |
4956 | specified, all fields from position \a start to the end of the |
4957 | string are included. Fields are numbered 0, 1, 2, etc., counting |
4958 | from the left, and -1, -2, etc., counting from right to left. |
4959 | |
4960 | The \a flags argument can be used to affect some aspects of the |
4961 | function's behavior, e.g. whether to be case sensitive, whether |
4962 | to skip empty fields and how to deal with leading and trailing |
4963 | separators; see \l{SectionFlags}. |
4964 | |
4965 | \snippet qstring/main.cpp 52 |
4966 | |
4967 | If \a start or \a end is negative, we count fields from the right |
4968 | of the string, the right-most field being -1, the one from |
4969 | right-most field being -2, and so on. |
4970 | |
4971 | \snippet qstring/main.cpp 53 |
4972 | |
4973 | \sa split() |
4974 | */ |
4975 | |
4976 | /*! |
4977 | \overload section() |
4978 | |
4979 | \snippet qstring/main.cpp 51 |
4980 | \snippet qstring/main.cpp 54 |
4981 | |
4982 | \sa split() |
4983 | */ |
4984 | |
4985 | QString QString::section(const QString &sep, qsizetype start, qsizetype end, SectionFlags flags) const |
4986 | { |
4987 | const QList<QStringView> sections = QStringView{ *this }.split( |
4988 | sep, behavior: Qt::KeepEmptyParts, cs: (flags & SectionCaseInsensitiveSeps) ? Qt::CaseInsensitive : Qt::CaseSensitive); |
4989 | const qsizetype sectionsSize = sections.size(); |
4990 | if (!(flags & SectionSkipEmpty)) { |
4991 | if (start < 0) |
4992 | start += sectionsSize; |
4993 | if (end < 0) |
4994 | end += sectionsSize; |
4995 | } else { |
4996 | qsizetype skip = 0; |
4997 | for (qsizetype k = 0; k < sectionsSize; ++k) { |
4998 | if (sections.at(i: k).isEmpty()) |
4999 | skip++; |
5000 | } |
5001 | if (start < 0) |
5002 | start += sectionsSize - skip; |
5003 | if (end < 0) |
5004 | end += sectionsSize - skip; |
5005 | } |
5006 | if (start >= sectionsSize || end < 0 || start > end) |
5007 | return QString(); |
5008 | |
5009 | QString ret; |
5010 | qsizetype first_i = start, last_i = end; |
5011 | for (qsizetype x = 0, i = 0; x <= end && i < sectionsSize; ++i) { |
5012 | const QStringView §ion = sections.at(i); |
5013 | const bool empty = section.isEmpty(); |
5014 | if (x >= start) { |
5015 | if (x == start) |
5016 | first_i = i; |
5017 | if (x == end) |
5018 | last_i = i; |
5019 | if (x > start && i > 0) |
5020 | ret += sep; |
5021 | ret += section; |
5022 | } |
5023 | if (!empty || !(flags & SectionSkipEmpty)) |
5024 | x++; |
5025 | } |
5026 | if ((flags & SectionIncludeLeadingSep) && first_i > 0) |
5027 | ret.prepend(s: sep); |
5028 | if ((flags & SectionIncludeTrailingSep) && last_i < sectionsSize - 1) |
5029 | ret += sep; |
5030 | return ret; |
5031 | } |
5032 | |
5033 | #if QT_CONFIG(regularexpression) |
5034 | class qt_section_chunk { |
5035 | public: |
5036 | qt_section_chunk() {} |
5037 | qt_section_chunk(qsizetype l, QStringView s) : length(l), string(std::move(s)) {} |
5038 | qsizetype length; |
5039 | QStringView string; |
5040 | }; |
5041 | Q_DECLARE_TYPEINFO(qt_section_chunk, Q_RELOCATABLE_TYPE); |
5042 | |
5043 | static QString (const QList<qt_section_chunk> §ions, qsizetype start, qsizetype end, |
5044 | QString::SectionFlags flags) |
5045 | { |
5046 | const qsizetype sectionsSize = sections.size(); |
5047 | |
5048 | if (!(flags & QString::SectionSkipEmpty)) { |
5049 | if (start < 0) |
5050 | start += sectionsSize; |
5051 | if (end < 0) |
5052 | end += sectionsSize; |
5053 | } else { |
5054 | qsizetype skip = 0; |
5055 | for (qsizetype k = 0; k < sectionsSize; ++k) { |
5056 | const qt_section_chunk §ion = sections.at(i: k); |
5057 | if (section.length == section.string.size()) |
5058 | skip++; |
5059 | } |
5060 | if (start < 0) |
5061 | start += sectionsSize - skip; |
5062 | if (end < 0) |
5063 | end += sectionsSize - skip; |
5064 | } |
5065 | if (start >= sectionsSize || end < 0 || start > end) |
5066 | return QString(); |
5067 | |
5068 | QString ret; |
5069 | qsizetype x = 0; |
5070 | qsizetype first_i = start, last_i = end; |
5071 | for (qsizetype i = 0; x <= end && i < sectionsSize; ++i) { |
5072 | const qt_section_chunk §ion = sections.at(i); |
5073 | const bool empty = (section.length == section.string.size()); |
5074 | if (x >= start) { |
5075 | if (x == start) |
5076 | first_i = i; |
5077 | if (x == end) |
5078 | last_i = i; |
5079 | if (x != start) |
5080 | ret += section.string; |
5081 | else |
5082 | ret += section.string.mid(pos: section.length); |
5083 | } |
5084 | if (!empty || !(flags & QString::SectionSkipEmpty)) |
5085 | x++; |
5086 | } |
5087 | |
5088 | if ((flags & QString::SectionIncludeLeadingSep) && first_i >= 0) { |
5089 | const qt_section_chunk §ion = sections.at(i: first_i); |
5090 | ret.prepend(v: section.string.left(n: section.length)); |
5091 | } |
5092 | |
5093 | if ((flags & QString::SectionIncludeTrailingSep) |
5094 | && last_i < sectionsSize - 1) { |
5095 | const qt_section_chunk §ion = sections.at(i: last_i+1); |
5096 | ret += section.string.left(n: section.length); |
5097 | } |
5098 | |
5099 | return ret; |
5100 | } |
5101 | |
5102 | /*! |
5103 | \overload section() |
5104 | \since 5.0 |
5105 | |
5106 | This string is treated as a sequence of fields separated by the |
5107 | regular expression, \a re. |
5108 | |
5109 | \snippet qstring/main.cpp 89 |
5110 | |
5111 | \warning Using this QRegularExpression version is much more expensive than |
5112 | the overloaded string and character versions. |
5113 | |
5114 | \sa split(), simplified() |
5115 | */ |
5116 | QString QString::section(const QRegularExpression &re, qsizetype start, qsizetype end, SectionFlags flags) const |
5117 | { |
5118 | if (!re.isValid()) { |
5119 | qtWarnAboutInvalidRegularExpression(pattern: re.pattern(), where: "QString::section" ); |
5120 | return QString(); |
5121 | } |
5122 | |
5123 | const QChar *uc = unicode(); |
5124 | if (!uc) |
5125 | return QString(); |
5126 | |
5127 | QRegularExpression sep(re); |
5128 | if (flags & SectionCaseInsensitiveSeps) |
5129 | sep.setPatternOptions(sep.patternOptions() | QRegularExpression::CaseInsensitiveOption); |
5130 | |
5131 | QList<qt_section_chunk> sections; |
5132 | qsizetype n = size(), m = 0, last_m = 0, last_len = 0; |
5133 | QRegularExpressionMatchIterator iterator = sep.globalMatch(subject: *this); |
5134 | while (iterator.hasNext()) { |
5135 | QRegularExpressionMatch match = iterator.next(); |
5136 | m = match.capturedStart(); |
5137 | sections.append(t: qt_section_chunk(last_len, QStringView{ *this }.sliced(pos: last_m, n: m - last_m))); |
5138 | last_m = m; |
5139 | last_len = match.capturedLength(); |
5140 | } |
5141 | sections.append(t: qt_section_chunk(last_len, QStringView{ *this }.sliced(pos: last_m, n: n - last_m))); |
5142 | |
5143 | return extractSections(sections, start, end, flags); |
5144 | } |
5145 | #endif // QT_CONFIG(regularexpression) |
5146 | |
5147 | /*! |
5148 | Returns a substring that contains the \a n leftmost characters |
5149 | of the string. |
5150 | |
5151 | If you know that \a n cannot be out of bounds, use first() instead in new |
5152 | code, because it is faster. |
5153 | |
5154 | The entire string is returned if \a n is greater than or equal |
5155 | to size(), or less than zero. |
5156 | |
5157 | \sa first(), last(), startsWith(), chopped(), chop(), truncate() |
5158 | */ |
5159 | QString QString::left(qsizetype n) const |
5160 | { |
5161 | if (size_t(n) >= size_t(size())) |
5162 | return *this; |
5163 | return QString((const QChar*) d.data(), n); |
5164 | } |
5165 | |
5166 | /*! |
5167 | Returns a substring that contains the \a n rightmost characters |
5168 | of the string. |
5169 | |
5170 | If you know that \a n cannot be out of bounds, use last() instead in new |
5171 | code, because it is faster. |
5172 | |
5173 | The entire string is returned if \a n is greater than or equal |
5174 | to size(), or less than zero. |
5175 | |
5176 | \sa endsWith(), last(), first(), sliced(), chopped(), chop(), truncate() |
5177 | */ |
5178 | QString QString::right(qsizetype n) const |
5179 | { |
5180 | if (size_t(n) >= size_t(size())) |
5181 | return *this; |
5182 | return QString(constData() + size() - n, n); |
5183 | } |
5184 | |
5185 | /*! |
5186 | Returns a string that contains \a n characters of this string, |
5187 | starting at the specified \a position index. |
5188 | |
5189 | If you know that \a position and \a n cannot be out of bounds, use sliced() |
5190 | instead in new code, because it is faster. |
5191 | |
5192 | Returns a null string if the \a position index exceeds the |
5193 | length of the string. If there are less than \a n characters |
5194 | available in the string starting at the given \a position, or if |
5195 | \a n is -1 (default), the function returns all characters that |
5196 | are available from the specified \a position. |
5197 | |
5198 | |
5199 | \sa first(), last(), sliced(), chopped(), chop(), truncate() |
5200 | */ |
5201 | |
5202 | QString QString::mid(qsizetype position, qsizetype n) const |
5203 | { |
5204 | qsizetype p = position; |
5205 | qsizetype l = n; |
5206 | using namespace QtPrivate; |
5207 | switch (QContainerImplHelper::mid(originalLength: size(), position: &p, length: &l)) { |
5208 | case QContainerImplHelper::Null: |
5209 | return QString(); |
5210 | case QContainerImplHelper::Empty: |
5211 | return QString(DataPointer::fromRawData(rawData: &_empty, length: 0)); |
5212 | case QContainerImplHelper::Full: |
5213 | return *this; |
5214 | case QContainerImplHelper::Subset: |
5215 | return QString(constData() + p, l); |
5216 | } |
5217 | Q_UNREACHABLE_RETURN(QString()); |
5218 | } |
5219 | |
5220 | /*! |
5221 | \fn QString QString::first(qsizetype n) const |
5222 | \since 6.0 |
5223 | |
5224 | Returns a string that contains the first \a n characters |
5225 | of this string. |
5226 | |
5227 | \note The behavior is undefined when \a n < 0 or \a n > size(). |
5228 | |
5229 | \snippet qstring/main.cpp 31 |
5230 | |
5231 | \sa last(), sliced(), startsWith(), chopped(), chop(), truncate() |
5232 | */ |
5233 | |
5234 | /*! |
5235 | \fn QString QString::last(qsizetype n) const |
5236 | \since 6.0 |
5237 | |
5238 | Returns the string that contains the last \a n characters of this string. |
5239 | |
5240 | \note The behavior is undefined when \a n < 0 or \a n > size(). |
5241 | |
5242 | \snippet qstring/main.cpp 48 |
5243 | |
5244 | \sa first(), sliced(), endsWith(), chopped(), chop(), truncate() |
5245 | */ |
5246 | |
5247 | /*! |
5248 | \fn QString QString::sliced(qsizetype pos, qsizetype n) const |
5249 | \since 6.0 |
5250 | |
5251 | Returns a string that contains \a n characters of this string, |
5252 | starting at position \a pos. |
5253 | |
5254 | \note The behavior is undefined when \a pos < 0, \a n < 0, |
5255 | or \a pos + \a n > size(). |
5256 | |
5257 | \snippet qstring/main.cpp 34 |
5258 | |
5259 | \sa first(), last(), chopped(), chop(), truncate() |
5260 | */ |
5261 | |
5262 | /*! |
5263 | \fn QString QString::sliced(qsizetype pos) const |
5264 | \since 6.0 |
5265 | \overload |
5266 | |
5267 | Returns a string that contains the portion of this string starting at |
5268 | position \a pos and extending to its end. |
5269 | |
5270 | \note The behavior is undefined when \a pos < 0 or \a pos > size(). |
5271 | |
5272 | \sa first(), last(), sliced(), chopped(), chop(), truncate() |
5273 | */ |
5274 | |
5275 | /*! |
5276 | \fn QString QString::chopped(qsizetype len) const |
5277 | \since 5.10 |
5278 | |
5279 | Returns a string that contains the size() - \a len leftmost characters |
5280 | of this string. |
5281 | |
5282 | \note The behavior is undefined if \a len is negative or greater than size(). |
5283 | |
5284 | \sa endsWith(), first(), last(), sliced(), chop(), truncate() |
5285 | */ |
5286 | |
5287 | /*! |
5288 | Returns \c true if the string starts with \a s; otherwise returns |
5289 | \c false. |
5290 | |
5291 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
5292 | |
5293 | \snippet qstring/main.cpp 65 |
5294 | |
5295 | \sa endsWith() |
5296 | */ |
5297 | bool QString::startsWith(const QString& s, Qt::CaseSensitivity cs) const |
5298 | { |
5299 | return qt_starts_with_impl(haystack: QStringView(*this), needle: QStringView(s), cs); |
5300 | } |
5301 | |
5302 | /*! |
5303 | \overload startsWith() |
5304 | */ |
5305 | bool QString::startsWith(QLatin1StringView s, Qt::CaseSensitivity cs) const |
5306 | { |
5307 | return qt_starts_with_impl(haystack: QStringView(*this), needle: s, cs); |
5308 | } |
5309 | |
5310 | /*! |
5311 | \overload startsWith() |
5312 | |
5313 | Returns \c true if the string starts with \a c; otherwise returns |
5314 | \c false. |
5315 | */ |
5316 | bool QString::startsWith(QChar c, Qt::CaseSensitivity cs) const |
5317 | { |
5318 | if (!size()) |
5319 | return false; |
5320 | if (cs == Qt::CaseSensitive) |
5321 | return at(i: 0) == c; |
5322 | return foldCase(ch: at(i: 0)) == foldCase(ch: c); |
5323 | } |
5324 | |
5325 | /*! |
5326 | \fn bool QString::startsWith(QStringView str, Qt::CaseSensitivity cs) const |
5327 | \since 5.10 |
5328 | \overload |
5329 | |
5330 | Returns \c true if the string starts with the string view \a str; |
5331 | otherwise returns \c false. |
5332 | |
5333 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
5334 | |
5335 | \sa endsWith() |
5336 | */ |
5337 | |
5338 | /*! |
5339 | Returns \c true if the string ends with \a s; otherwise returns |
5340 | \c false. |
5341 | |
5342 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
5343 | |
5344 | \snippet qstring/main.cpp 20 |
5345 | |
5346 | \sa startsWith() |
5347 | */ |
5348 | bool QString::endsWith(const QString &s, Qt::CaseSensitivity cs) const |
5349 | { |
5350 | return qt_ends_with_impl(haystack: QStringView(*this), needle: QStringView(s), cs); |
5351 | } |
5352 | |
5353 | /*! |
5354 | \fn bool QString::endsWith(QStringView str, Qt::CaseSensitivity cs) const |
5355 | \since 5.10 |
5356 | \overload endsWith() |
5357 | Returns \c true if the string ends with the string view \a str; |
5358 | otherwise returns \c false. |
5359 | |
5360 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
5361 | |
5362 | \sa startsWith() |
5363 | */ |
5364 | |
5365 | /*! |
5366 | \overload endsWith() |
5367 | */ |
5368 | bool QString::endsWith(QLatin1StringView s, Qt::CaseSensitivity cs) const |
5369 | { |
5370 | return qt_ends_with_impl(haystack: QStringView(*this), needle: s, cs); |
5371 | } |
5372 | |
5373 | /*! |
5374 | Returns \c true if the string ends with \a c; otherwise returns |
5375 | \c false. |
5376 | |
5377 | \overload endsWith() |
5378 | */ |
5379 | bool QString::endsWith(QChar c, Qt::CaseSensitivity cs) const |
5380 | { |
5381 | if (!size()) |
5382 | return false; |
5383 | if (cs == Qt::CaseSensitive) |
5384 | return at(i: size() - 1) == c; |
5385 | return foldCase(ch: at(i: size() - 1)) == foldCase(ch: c); |
5386 | } |
5387 | |
5388 | /*! |
5389 | Returns \c true if the string is uppercase, that is, it's identical |
5390 | to its toUpper() folding. |
5391 | |
5392 | Note that this does \e not mean that the string does not contain |
5393 | lowercase letters (some lowercase letters do not have a uppercase |
5394 | folding; they are left unchanged by toUpper()). |
5395 | For more information, refer to the Unicode standard, section 3.13. |
5396 | |
5397 | \since 5.12 |
5398 | |
5399 | \sa QChar::toUpper(), isLower() |
5400 | */ |
5401 | bool QString::isUpper() const |
5402 | { |
5403 | QStringIterator it(*this); |
5404 | |
5405 | while (it.hasNext()) { |
5406 | const char32_t uc = it.next(); |
5407 | if (qGetProp(ucs4: uc)->cases[QUnicodeTables::UpperCase].diff) |
5408 | return false; |
5409 | } |
5410 | |
5411 | return true; |
5412 | } |
5413 | |
5414 | /*! |
5415 | Returns \c true if the string is lowercase, that is, it's identical |
5416 | to its toLower() folding. |
5417 | |
5418 | Note that this does \e not mean that the string does not contain |
5419 | uppercase letters (some uppercase letters do not have a lowercase |
5420 | folding; they are left unchanged by toLower()). |
5421 | For more information, refer to the Unicode standard, section 3.13. |
5422 | |
5423 | \since 5.12 |
5424 | |
5425 | \sa QChar::toLower(), isUpper() |
5426 | */ |
5427 | bool QString::isLower() const |
5428 | { |
5429 | QStringIterator it(*this); |
5430 | |
5431 | while (it.hasNext()) { |
5432 | const char32_t uc = it.next(); |
5433 | if (qGetProp(ucs4: uc)->cases[QUnicodeTables::LowerCase].diff) |
5434 | return false; |
5435 | } |
5436 | |
5437 | return true; |
5438 | } |
5439 | |
5440 | static QByteArray qt_convert_to_latin1(QStringView string); |
5441 | |
5442 | QByteArray QString::toLatin1_helper(const QString &string) |
5443 | { |
5444 | return qt_convert_to_latin1(string); |
5445 | } |
5446 | |
5447 | /*! |
5448 | \since 6.0 |
5449 | \internal |
5450 | \relates QAnyStringView |
5451 | |
5452 | Returns a UTF-16 representation of \a string as a QString. |
5453 | |
5454 | \sa QString::toLatin1(), QStringView::toLatin1(), QtPrivate::convertToUtf8(), |
5455 | QtPrivate::convertToLocal8Bit(), QtPrivate::convertToUcs4() |
5456 | */ |
5457 | QString QtPrivate::convertToQString(QAnyStringView string) |
5458 | { |
5459 | return string.visit(v: [] (auto string) { return string.toString(); }); |
5460 | } |
5461 | |
5462 | /*! |
5463 | \since 5.10 |
5464 | \internal |
5465 | \relates QStringView |
5466 | |
5467 | Returns a Latin-1 representation of \a string as a QByteArray. |
5468 | |
5469 | The behavior is undefined if \a string contains non-Latin1 characters. |
5470 | |
5471 | \sa QString::toLatin1(), QStringView::toLatin1(), QtPrivate::convertToUtf8(), |
5472 | QtPrivate::convertToLocal8Bit(), QtPrivate::convertToUcs4() |
5473 | */ |
5474 | QByteArray QtPrivate::convertToLatin1(QStringView string) |
5475 | { |
5476 | return qt_convert_to_latin1(string); |
5477 | } |
5478 | |
5479 | Q_NEVER_INLINE |
5480 | static QByteArray qt_convert_to_latin1(QStringView string) |
5481 | { |
5482 | if (Q_UNLIKELY(string.isNull())) |
5483 | return QByteArray(); |
5484 | |
5485 | QByteArray ba(string.size(), Qt::Uninitialized); |
5486 | |
5487 | // since we own the only copy, we're going to const_cast the constData; |
5488 | // that avoids an unnecessary call to detach() and expansion code that will never get used |
5489 | qt_to_latin1(dst: reinterpret_cast<uchar *>(const_cast<char *>(ba.constData())), |
5490 | src: string.utf16(), length: string.size()); |
5491 | return ba; |
5492 | } |
5493 | |
5494 | QByteArray QString::toLatin1_helper_inplace(QString &s) |
5495 | { |
5496 | if (!s.isDetached()) |
5497 | return qt_convert_to_latin1(string: s); |
5498 | |
5499 | // We can return our own buffer to the caller. |
5500 | // Conversion to Latin-1 always shrinks the buffer by half. |
5501 | // This relies on the fact that we use QArrayData for everything behind the scenes |
5502 | |
5503 | // First, do the in-place conversion. Since isDetached() == true, the data |
5504 | // was allocated by QArrayData, so the null terminator must be there. |
5505 | qsizetype length = s.size(); |
5506 | char16_t *sdata = s.d->data(); |
5507 | Q_ASSERT(sdata[length] == u'\0'); |
5508 | qt_to_latin1(dst: reinterpret_cast<uchar *>(sdata), src: sdata, length: length + 1); |
5509 | |
5510 | // Move the internals over to the byte array. |
5511 | // Kids, avert your eyes. Don't try this at home. |
5512 | auto ba_d = std::move(s.d).reinterpreted<char>(); |
5513 | |
5514 | // Some sanity checks |
5515 | Q_ASSERT(ba_d.d->allocatedCapacity() >= ba_d.size); |
5516 | Q_ASSERT(s.isNull()); |
5517 | Q_ASSERT(s.isEmpty()); |
5518 | Q_ASSERT(s.constData() == QString().constData()); |
5519 | |
5520 | return QByteArray(std::move(ba_d)); |
5521 | } |
5522 | |
5523 | // QLatin1 methods that use helpers from qstring.cpp |
5524 | char16_t *QLatin1::convertToUnicode(char16_t *out, QLatin1StringView in) noexcept |
5525 | { |
5526 | const qsizetype len = in.size(); |
5527 | qt_from_latin1(dst: out, str: in.data(), size: len); |
5528 | return std::next(x: out, n: len); |
5529 | } |
5530 | |
5531 | char *QLatin1::convertFromUnicode(char *out, QStringView in) noexcept |
5532 | { |
5533 | const qsizetype len = in.size(); |
5534 | qt_to_latin1(dst: reinterpret_cast<uchar *>(out), src: in.utf16(), length: len); |
5535 | return out + len; |
5536 | } |
5537 | |
5538 | /*! |
5539 | \fn QByteArray QString::toLatin1() const |
5540 | |
5541 | Returns a Latin-1 representation of the string as a QByteArray. |
5542 | |
5543 | The returned byte array is undefined if the string contains non-Latin1 |
5544 | characters. Those characters may be suppressed or replaced with a |
5545 | question mark. |
5546 | |
5547 | \sa fromLatin1(), toUtf8(), toLocal8Bit(), QStringEncoder |
5548 | */ |
5549 | |
5550 | static QByteArray qt_convert_to_local_8bit(QStringView string); |
5551 | |
5552 | /*! |
5553 | \fn QByteArray QString::toLocal8Bit() const |
5554 | |
5555 | Returns the local 8-bit representation of the string as a |
5556 | QByteArray. |
5557 | |
5558 | \include qstring.qdocinc {qstring-local-8-bit-equivalent} {toUtf8} |
5559 | |
5560 | If this string contains any characters that cannot be encoded in the |
5561 | local 8-bit encoding, the returned byte array is undefined. Those |
5562 | characters may be suppressed or replaced by another. |
5563 | |
5564 | \sa fromLocal8Bit(), toLatin1(), toUtf8(), QStringEncoder |
5565 | */ |
5566 | |
5567 | QByteArray QString::toLocal8Bit_helper(const QChar *data, qsizetype size) |
5568 | { |
5569 | return qt_convert_to_local_8bit(string: QStringView(data, size)); |
5570 | } |
5571 | |
5572 | static QByteArray qt_convert_to_local_8bit(QStringView string) |
5573 | { |
5574 | if (string.isNull()) |
5575 | return QByteArray(); |
5576 | QStringEncoder fromUtf16(QStringEncoder::System, QStringEncoder::Flag::Stateless); |
5577 | return fromUtf16(string); |
5578 | } |
5579 | |
5580 | /*! |
5581 | \since 5.10 |
5582 | \internal |
5583 | \relates QStringView |
5584 | |
5585 | Returns a local 8-bit representation of \a string as a QByteArray. |
5586 | |
5587 | On Unix systems this is equivalent to toUtf8(), on Windows the systems |
5588 | current code page is being used. |
5589 | |
5590 | The behavior is undefined if \a string contains characters not |
5591 | supported by the locale's 8-bit encoding. |
5592 | |
5593 | \sa QString::toLocal8Bit(), QStringView::toLocal8Bit() |
5594 | */ |
5595 | QByteArray QtPrivate::convertToLocal8Bit(QStringView string) |
5596 | { |
5597 | return qt_convert_to_local_8bit(string); |
5598 | } |
5599 | |
5600 | static QByteArray qt_convert_to_utf8(QStringView str); |
5601 | |
5602 | /*! |
5603 | \fn QByteArray QString::toUtf8() const |
5604 | |
5605 | Returns a UTF-8 representation of the string as a QByteArray. |
5606 | |
5607 | UTF-8 is a Unicode codec and can represent all characters in a Unicode |
5608 | string like QString. |
5609 | |
5610 | \sa fromUtf8(), toLatin1(), toLocal8Bit(), QStringEncoder |
5611 | */ |
5612 | |
5613 | QByteArray QString::toUtf8_helper(const QString &str) |
5614 | { |
5615 | return qt_convert_to_utf8(str); |
5616 | } |
5617 | |
5618 | static QByteArray qt_convert_to_utf8(QStringView str) |
5619 | { |
5620 | if (str.isNull()) |
5621 | return QByteArray(); |
5622 | |
5623 | return QUtf8::convertFromUnicode(in: str); |
5624 | } |
5625 | |
5626 | /*! |
5627 | \since 5.10 |
5628 | \internal |
5629 | \relates QStringView |
5630 | |
5631 | Returns a UTF-8 representation of \a string as a QByteArray. |
5632 | |
5633 | UTF-8 is a Unicode codec and can represent all characters in a Unicode |
5634 | string like QStringView. |
5635 | |
5636 | \sa QString::toUtf8(), QStringView::toUtf8() |
5637 | */ |
5638 | QByteArray QtPrivate::convertToUtf8(QStringView string) |
5639 | { |
5640 | return qt_convert_to_utf8(str: string); |
5641 | } |
5642 | |
5643 | static QList<uint> qt_convert_to_ucs4(QStringView string); |
5644 | |
5645 | /*! |
5646 | \since 4.2 |
5647 | |
5648 | Returns a UCS-4/UTF-32 representation of the string as a QList<uint>. |
5649 | |
5650 | UCS-4 is a Unicode codec and therefore it is lossless. All characters from |
5651 | this string will be encoded in UCS-4. Any invalid sequence of code units in |
5652 | this string is replaced by the Unicode's replacement character |
5653 | (QChar::ReplacementCharacter, which corresponds to \c{U+FFFD}). |
5654 | |
5655 | The returned list is not \\0'-terminated. |
5656 | |
5657 | \sa fromUtf8(), toUtf8(), toLatin1(), toLocal8Bit(), QStringEncoder, |
5658 | fromUcs4(), toWCharArray() |
5659 | */ |
5660 | QList<uint> QString::toUcs4() const |
5661 | { |
5662 | return qt_convert_to_ucs4(string: *this); |
5663 | } |
5664 | |
5665 | static QList<uint> qt_convert_to_ucs4(QStringView string) |
5666 | { |
5667 | QList<uint> v(string.size()); |
5668 | uint *a = const_cast<uint*>(v.constData()); |
5669 | QStringIterator it(string); |
5670 | while (it.hasNext()) |
5671 | *a++ = it.next(); |
5672 | v.resize(size: a - v.constData()); |
5673 | return v; |
5674 | } |
5675 | |
5676 | /*! |
5677 | \since 5.10 |
5678 | \internal |
5679 | \relates QStringView |
5680 | |
5681 | Returns a UCS-4/UTF-32 representation of \a string as a QList<uint>. |
5682 | |
5683 | UCS-4 is a Unicode codec and therefore it is lossless. All characters from |
5684 | this string will be encoded in UCS-4. Any invalid sequence of code units in |
5685 | this string is replaced by the Unicode's replacement character |
5686 | (QChar::ReplacementCharacter, which corresponds to \c{U+FFFD}). |
5687 | |
5688 | The returned list is not \\0'-terminated. |
5689 | |
5690 | \sa QString::toUcs4(), QStringView::toUcs4(), QtPrivate::convertToLatin1(), |
5691 | QtPrivate::convertToLocal8Bit(), QtPrivate::convertToUtf8() |
5692 | */ |
5693 | QList<uint> QtPrivate::convertToUcs4(QStringView string) |
5694 | { |
5695 | return qt_convert_to_ucs4(string); |
5696 | } |
5697 | |
5698 | /*! |
5699 | \fn QString QString::fromLatin1(QByteArrayView str) |
5700 | \overload |
5701 | \since 6.0 |
5702 | |
5703 | Returns a QString initialized with the Latin-1 string \a str. |
5704 | |
5705 | \note: any null ('\\0') bytes in the byte array will be included in this |
5706 | string, converted to Unicode null characters (U+0000). |
5707 | */ |
5708 | QString QString::fromLatin1(QByteArrayView ba) |
5709 | { |
5710 | DataPointer d; |
5711 | if (!ba.data()) { |
5712 | // nothing to do |
5713 | } else if (ba.size() == 0) { |
5714 | d = DataPointer::fromRawData(rawData: &_empty, length: 0); |
5715 | } else { |
5716 | d = DataPointer(Data::allocate(capacity: ba.size()), ba.size()); |
5717 | Q_CHECK_PTR(d.data()); |
5718 | d.data()[ba.size()] = '\0'; |
5719 | char16_t *dst = d.data(); |
5720 | |
5721 | qt_from_latin1(dst, str: ba.data(), size: size_t(ba.size())); |
5722 | } |
5723 | return QString(std::move(d)); |
5724 | } |
5725 | |
5726 | /*! |
5727 | \fn QString QString::fromLatin1(const char *str, qsizetype size) |
5728 | Returns a QString initialized with the first \a size characters |
5729 | of the Latin-1 string \a str. |
5730 | |
5731 | If \a size is \c{-1}, \c{strlen(str)} is used instead. |
5732 | |
5733 | \sa toLatin1(), fromUtf8(), fromLocal8Bit() |
5734 | */ |
5735 | |
5736 | /*! |
5737 | \fn QString QString::fromLatin1(const QByteArray &str) |
5738 | \overload |
5739 | \since 5.0 |
5740 | |
5741 | Returns a QString initialized with the Latin-1 string \a str. |
5742 | |
5743 | \note: any null ('\\0') bytes in the byte array will be included in this |
5744 | string, converted to Unicode null characters (U+0000). This behavior is |
5745 | different from Qt 5.x. |
5746 | */ |
5747 | |
5748 | /*! |
5749 | \fn QString QString::fromLocal8Bit(const char *str, qsizetype size) |
5750 | Returns a QString initialized with the first \a size characters |
5751 | of the 8-bit string \a str. |
5752 | |
5753 | If \a size is \c{-1}, \c{strlen(str)} is used instead. |
5754 | |
5755 | \include qstring.qdocinc {qstring-local-8-bit-equivalent} {fromUtf8} |
5756 | |
5757 | \sa toLocal8Bit(), fromLatin1(), fromUtf8() |
5758 | */ |
5759 | |
5760 | /*! |
5761 | \fn QString QString::fromLocal8Bit(const QByteArray &str) |
5762 | \overload |
5763 | \since 5.0 |
5764 | |
5765 | Returns a QString initialized with the 8-bit string \a str. |
5766 | |
5767 | \include qstring.qdocinc {qstring-local-8-bit-equivalent} {fromUtf8} |
5768 | |
5769 | \note: any null ('\\0') bytes in the byte array will be included in this |
5770 | string, converted to Unicode null characters (U+0000). This behavior is |
5771 | different from Qt 5.x. |
5772 | */ |
5773 | |
5774 | /*! |
5775 | \fn QString QString::fromLocal8Bit(QByteArrayView str) |
5776 | \overload |
5777 | \since 6.0 |
5778 | |
5779 | Returns a QString initialized with the 8-bit string \a str. |
5780 | |
5781 | \include qstring.qdocinc {qstring-local-8-bit-equivalent} {fromUtf8} |
5782 | |
5783 | \note: any null ('\\0') bytes in the byte array will be included in this |
5784 | string, converted to Unicode null characters (U+0000). |
5785 | */ |
5786 | QString QString::fromLocal8Bit(QByteArrayView ba) |
5787 | { |
5788 | if (ba.isNull()) |
5789 | return QString(); |
5790 | if (ba.isEmpty()) |
5791 | return QString(DataPointer::fromRawData(rawData: &_empty, length: 0)); |
5792 | QStringDecoder toUtf16(QStringDecoder::System, QStringDecoder::Flag::Stateless); |
5793 | return toUtf16(ba); |
5794 | } |
5795 | |
5796 | /*! \fn QString QString::fromUtf8(const char *str, qsizetype size) |
5797 | Returns a QString initialized with the first \a size bytes |
5798 | of the UTF-8 string \a str. |
5799 | |
5800 | If \a size is \c{-1}, \c{strlen(str)} is used instead. |
5801 | |
5802 | UTF-8 is a Unicode codec and can represent all characters in a Unicode |
5803 | string like QString. However, invalid sequences are possible with UTF-8 |
5804 | and, if any such are found, they will be replaced with one or more |
5805 | "replacement characters", or suppressed. These include non-Unicode |
5806 | sequences, non-characters, overlong sequences or surrogate codepoints |
5807 | encoded into UTF-8. |
5808 | |
5809 | This function can be used to process incoming data incrementally as long as |
5810 | all UTF-8 characters are terminated within the incoming data. Any |
5811 | unterminated characters at the end of the string will be replaced or |
5812 | suppressed. In order to do stateful decoding, please use \l QStringDecoder. |
5813 | |
5814 | \sa toUtf8(), fromLatin1(), fromLocal8Bit() |
5815 | */ |
5816 | |
5817 | /*! |
5818 | \fn QString QString::fromUtf8(const char8_t *str) |
5819 | \overload |
5820 | \since 6.1 |
5821 | |
5822 | This overload is only available when compiling in C++20 mode. |
5823 | */ |
5824 | |
5825 | /*! |
5826 | \fn QString QString::fromUtf8(const char8_t *str, qsizetype size) |
5827 | \overload |
5828 | \since 6.0 |
5829 | |
5830 | This overload is only available when compiling in C++20 mode. |
5831 | */ |
5832 | |
5833 | /*! |
5834 | \fn QString QString::fromUtf8(const QByteArray &str) |
5835 | \overload |
5836 | \since 5.0 |
5837 | |
5838 | Returns a QString initialized with the UTF-8 string \a str. |
5839 | |
5840 | \note: any null ('\\0') bytes in the byte array will be included in this |
5841 | string, converted to Unicode null characters (U+0000). This behavior is |
5842 | different from Qt 5.x. |
5843 | */ |
5844 | |
5845 | /*! |
5846 | \fn QString QString::fromUtf8(QByteArrayView str) |
5847 | \overload |
5848 | \since 6.0 |
5849 | |
5850 | Returns a QString initialized with the UTF-8 string \a str. |
5851 | |
5852 | \note: any null ('\\0') bytes in the byte array will be included in this |
5853 | string, converted to Unicode null characters (U+0000). |
5854 | */ |
5855 | QString QString::fromUtf8(QByteArrayView ba) |
5856 | { |
5857 | if (ba.isNull()) |
5858 | return QString(); |
5859 | if (ba.isEmpty()) |
5860 | return QString(DataPointer::fromRawData(rawData: &_empty, length: 0)); |
5861 | return QUtf8::convertToUnicode(in: ba); |
5862 | } |
5863 | |
5864 | /*! |
5865 | \since 5.3 |
5866 | Returns a QString initialized with the first \a size characters |
5867 | of the Unicode string \a unicode (ISO-10646-UTF-16 encoded). |
5868 | |
5869 | If \a size is -1 (default), \a unicode must be \\0'-terminated. |
5870 | |
5871 | This function checks for a Byte Order Mark (BOM). If it is missing, |
5872 | host byte order is assumed. |
5873 | |
5874 | This function is slow compared to the other Unicode conversions. |
5875 | Use QString(const QChar *, qsizetype) or QString(const QChar *) if possible. |
5876 | |
5877 | QString makes a deep copy of the Unicode data. |
5878 | |
5879 | \sa utf16(), setUtf16(), fromStdU16String() |
5880 | */ |
5881 | QString QString::fromUtf16(const char16_t *unicode, qsizetype size) |
5882 | { |
5883 | if (!unicode) |
5884 | return QString(); |
5885 | if (size < 0) |
5886 | size = QtPrivate::qustrlen(str: unicode); |
5887 | QStringDecoder toUtf16(QStringDecoder::Utf16, QStringDecoder::Flag::Stateless); |
5888 | return toUtf16(QByteArrayView(reinterpret_cast<const char *>(unicode), size * 2)); |
5889 | } |
5890 | |
5891 | /*! |
5892 | \fn QString QString::fromUtf16(const ushort *str, qsizetype size) |
5893 | \deprecated [6.0] Use the \c char16_t overload instead. |
5894 | */ |
5895 | |
5896 | /*! |
5897 | \fn QString QString::fromUcs4(const uint *str, qsizetype size) |
5898 | \since 4.2 |
5899 | \deprecated [6.0] Use the \c char32_t overload instead. |
5900 | */ |
5901 | |
5902 | /*! |
5903 | \since 5.3 |
5904 | |
5905 | Returns a QString initialized with the first \a size characters |
5906 | of the Unicode string \a unicode (ISO-10646-UCS-4 encoded). |
5907 | |
5908 | If \a size is -1 (default), \a unicode must be \\0'-terminated. |
5909 | |
5910 | \sa toUcs4(), fromUtf16(), utf16(), setUtf16(), fromWCharArray(), |
5911 | fromStdU32String() |
5912 | */ |
5913 | QString QString::fromUcs4(const char32_t *unicode, qsizetype size) |
5914 | { |
5915 | if (!unicode) |
5916 | return QString(); |
5917 | if (size < 0) { |
5918 | size = 0; |
5919 | while (unicode[size] != 0) |
5920 | ++size; |
5921 | } |
5922 | QStringDecoder toUtf16(QStringDecoder::Utf32, QStringDecoder::Flag::Stateless); |
5923 | return toUtf16(QByteArrayView(reinterpret_cast<const char *>(unicode), size * 4)); |
5924 | } |
5925 | |
5926 | |
5927 | /*! |
5928 | Resizes the string to \a size characters and copies \a unicode |
5929 | into the string. |
5930 | |
5931 | If \a unicode is \nullptr, nothing is copied, but the string is still |
5932 | resized to \a size. |
5933 | |
5934 | \sa unicode(), setUtf16() |
5935 | */ |
5936 | QString& QString::setUnicode(const QChar *unicode, qsizetype size) |
5937 | { |
5938 | resize(size); |
5939 | if (unicode && size) |
5940 | memcpy(dest: d.data(), src: unicode, n: size * sizeof(QChar)); |
5941 | return *this; |
5942 | } |
5943 | |
5944 | /*! |
5945 | \fn QString &QString::setUtf16(const ushort *unicode, qsizetype size) |
5946 | |
5947 | Resizes the string to \a size characters and copies \a unicode |
5948 | into the string. |
5949 | |
5950 | If \a unicode is \nullptr, nothing is copied, but the string is still |
5951 | resized to \a size. |
5952 | |
5953 | Note that unlike fromUtf16(), this function does not consider BOMs and |
5954 | possibly differing byte ordering. |
5955 | |
5956 | \sa utf16(), setUnicode() |
5957 | */ |
5958 | |
5959 | /*! |
5960 | \fn QString QString::simplified() const |
5961 | |
5962 | Returns a string that has whitespace removed from the start |
5963 | and the end, and that has each sequence of internal whitespace |
5964 | replaced with a single space. |
5965 | |
5966 | Whitespace means any character for which QChar::isSpace() returns |
5967 | \c true. This includes the ASCII characters '\\t', '\\n', '\\v', |
5968 | '\\f', '\\r', and ' '. |
5969 | |
5970 | Example: |
5971 | |
5972 | \snippet qstring/main.cpp 57 |
5973 | |
5974 | \sa trimmed() |
5975 | */ |
5976 | QString QString::simplified_helper(const QString &str) |
5977 | { |
5978 | return QStringAlgorithms<const QString>::simplified_helper(str); |
5979 | } |
5980 | |
5981 | QString QString::simplified_helper(QString &str) |
5982 | { |
5983 | return QStringAlgorithms<QString>::simplified_helper(str); |
5984 | } |
5985 | |
5986 | namespace { |
5987 | template <typename StringView> |
5988 | StringView qt_trimmed(StringView s) noexcept |
5989 | { |
5990 | auto begin = s.begin(); |
5991 | auto end = s.end(); |
5992 | QStringAlgorithms<const StringView>::trimmed_helper_positions(begin, end); |
5993 | return StringView{begin, end}; |
5994 | } |
5995 | } |
5996 | |
5997 | /*! |
5998 | \fn QStringView QtPrivate::trimmed(QStringView s) |
5999 | \fn QLatin1StringView QtPrivate::trimmed(QLatin1StringView s) |
6000 | \internal |
6001 | \relates QStringView |
6002 | \since 5.10 |
6003 | |
6004 | Returns \a s with whitespace removed from the start and the end. |
6005 | |
6006 | Whitespace means any character for which QChar::isSpace() returns |
6007 | \c true. This includes the ASCII characters '\\t', '\\n', '\\v', |
6008 | '\\f', '\\r', and ' '. |
6009 | |
6010 | \sa QString::trimmed(), QStringView::trimmed(), QLatin1StringView::trimmed() |
6011 | */ |
6012 | QStringView QtPrivate::trimmed(QStringView s) noexcept |
6013 | { |
6014 | return qt_trimmed(s); |
6015 | } |
6016 | |
6017 | QLatin1StringView QtPrivate::trimmed(QLatin1StringView s) noexcept |
6018 | { |
6019 | return qt_trimmed(s); |
6020 | } |
6021 | |
6022 | /*! |
6023 | \fn QString QString::trimmed() const |
6024 | |
6025 | Returns a string that has whitespace removed from the start and |
6026 | the end. |
6027 | |
6028 | Whitespace means any character for which QChar::isSpace() returns |
6029 | \c true. This includes the ASCII characters '\\t', '\\n', '\\v', |
6030 | '\\f', '\\r', and ' '. |
6031 | |
6032 | Example: |
6033 | |
6034 | \snippet qstring/main.cpp 82 |
6035 | |
6036 | Unlike simplified(), trimmed() leaves internal whitespace alone. |
6037 | |
6038 | \sa simplified() |
6039 | */ |
6040 | QString QString::trimmed_helper(const QString &str) |
6041 | { |
6042 | return QStringAlgorithms<const QString>::trimmed_helper(str); |
6043 | } |
6044 | |
6045 | QString QString::trimmed_helper(QString &str) |
6046 | { |
6047 | return QStringAlgorithms<QString>::trimmed_helper(str); |
6048 | } |
6049 | |
6050 | /*! \fn const QChar QString::at(qsizetype position) const |
6051 | |
6052 | Returns the character at the given index \a position in the |
6053 | string. |
6054 | |
6055 | The \a position must be a valid index position in the string |
6056 | (i.e., 0 <= \a position < size()). |
6057 | |
6058 | \sa operator[]() |
6059 | */ |
6060 | |
6061 | /*! |
6062 | \fn QChar &QString::operator[](qsizetype position) |
6063 | |
6064 | Returns the character at the specified \a position in the string as a |
6065 | modifiable reference. |
6066 | |
6067 | Example: |
6068 | |
6069 | \snippet qstring/main.cpp 85 |
6070 | |
6071 | \sa at() |
6072 | */ |
6073 | |
6074 | /*! |
6075 | \fn const QChar QString::operator[](qsizetype position) const |
6076 | |
6077 | \overload operator[]() |
6078 | */ |
6079 | |
6080 | /*! |
6081 | \fn QChar QString::front() const |
6082 | \since 5.10 |
6083 | |
6084 | Returns the first character in the string. |
6085 | Same as \c{at(0)}. |
6086 | |
6087 | This function is provided for STL compatibility. |
6088 | |
6089 | \warning Calling this function on an empty string constitutes |
6090 | undefined behavior. |
6091 | |
6092 | \sa back(), at(), operator[]() |
6093 | */ |
6094 | |
6095 | /*! |
6096 | \fn QChar QString::back() const |
6097 | \since 5.10 |
6098 | |
6099 | Returns the last character in the string. |
6100 | Same as \c{at(size() - 1)}. |
6101 | |
6102 | This function is provided for STL compatibility. |
6103 | |
6104 | \warning Calling this function on an empty string constitutes |
6105 | undefined behavior. |
6106 | |
6107 | \sa front(), at(), operator[]() |
6108 | */ |
6109 | |
6110 | /*! |
6111 | \fn QChar &QString::front() |
6112 | \since 5.10 |
6113 | |
6114 | Returns a reference to the first character in the string. |
6115 | Same as \c{operator[](0)}. |
6116 | |
6117 | This function is provided for STL compatibility. |
6118 | |
6119 | \warning Calling this function on an empty string constitutes |
6120 | undefined behavior. |
6121 | |
6122 | \sa back(), at(), operator[]() |
6123 | */ |
6124 | |
6125 | /*! |
6126 | \fn QChar &QString::back() |
6127 | \since 5.10 |
6128 | |
6129 | Returns a reference to the last character in the string. |
6130 | Same as \c{operator[](size() - 1)}. |
6131 | |
6132 | This function is provided for STL compatibility. |
6133 | |
6134 | \warning Calling this function on an empty string constitutes |
6135 | undefined behavior. |
6136 | |
6137 | \sa front(), at(), operator[]() |
6138 | */ |
6139 | |
6140 | /*! |
6141 | \fn void QString::truncate(qsizetype position) |
6142 | |
6143 | Truncates the string at the given \a position index. |
6144 | |
6145 | If the specified \a position index is beyond the end of the |
6146 | string, nothing happens. |
6147 | |
6148 | Example: |
6149 | |
6150 | \snippet qstring/main.cpp 83 |
6151 | |
6152 | If \a position is negative, it is equivalent to passing zero. |
6153 | |
6154 | \sa chop(), resize(), first(), QStringView::truncate() |
6155 | */ |
6156 | |
6157 | void QString::truncate(qsizetype pos) |
6158 | { |
6159 | if (pos < size()) |
6160 | resize(size: pos); |
6161 | } |
6162 | |
6163 | |
6164 | /*! |
6165 | Removes \a n characters from the end of the string. |
6166 | |
6167 | If \a n is greater than or equal to size(), the result is an |
6168 | empty string; if \a n is negative, it is equivalent to passing zero. |
6169 | |
6170 | Example: |
6171 | \snippet qstring/main.cpp 15 |
6172 | |
6173 | If you want to remove characters from the \e beginning of the |
6174 | string, use remove() instead. |
6175 | |
6176 | \sa truncate(), resize(), remove(), QStringView::chop() |
6177 | */ |
6178 | void QString::chop(qsizetype n) |
6179 | { |
6180 | if (n > 0) |
6181 | resize(size: d.size - n); |
6182 | } |
6183 | |
6184 | /*! |
6185 | Sets every character in the string to character \a ch. If \a size |
6186 | is different from -1 (default), the string is resized to \a |
6187 | size beforehand. |
6188 | |
6189 | Example: |
6190 | |
6191 | \snippet qstring/main.cpp 21 |
6192 | |
6193 | \sa resize() |
6194 | */ |
6195 | |
6196 | QString& QString::fill(QChar ch, qsizetype size) |
6197 | { |
6198 | resize(size: size < 0 ? d.size : size); |
6199 | if (d.size) { |
6200 | QChar *i = (QChar*)d.data() + d.size; |
6201 | QChar *b = (QChar*)d.data(); |
6202 | std::fill(first: b, last: i, value: ch); |
6203 | } |
6204 | return *this; |
6205 | } |
6206 | |
6207 | /*! |
6208 | \fn qsizetype QString::length() const |
6209 | |
6210 | Returns the number of characters in this string. Equivalent to |
6211 | size(). |
6212 | |
6213 | \sa resize() |
6214 | */ |
6215 | |
6216 | /*! |
6217 | \fn qsizetype QString::size() const |
6218 | |
6219 | Returns the number of characters in this string. |
6220 | |
6221 | The last character in the string is at position size() - 1. |
6222 | |
6223 | Example: |
6224 | \snippet qstring/main.cpp 58 |
6225 | |
6226 | \sa isEmpty(), resize() |
6227 | */ |
6228 | |
6229 | /*! \fn bool QString::isNull() const |
6230 | |
6231 | Returns \c true if this string is null; otherwise returns \c false. |
6232 | |
6233 | Example: |
6234 | |
6235 | \snippet qstring/main.cpp 28 |
6236 | |
6237 | Qt makes a distinction between null strings and empty strings for |
6238 | historical reasons. For most applications, what matters is |
6239 | whether or not a string contains any data, and this can be |
6240 | determined using the isEmpty() function. |
6241 | |
6242 | \sa isEmpty() |
6243 | */ |
6244 | |
6245 | /*! \fn bool QString::isEmpty() const |
6246 | |
6247 | Returns \c true if the string has no characters; otherwise returns |
6248 | \c false. |
6249 | |
6250 | Example: |
6251 | |
6252 | \snippet qstring/main.cpp 27 |
6253 | |
6254 | \sa size() |
6255 | */ |
6256 | |
6257 | /*! \fn QString &QString::operator+=(const QString &other) |
6258 | |
6259 | Appends the string \a other onto the end of this string and |
6260 | returns a reference to this string. |
6261 | |
6262 | Example: |
6263 | |
6264 | \snippet qstring/main.cpp 84 |
6265 | |
6266 | This operation is typically very fast (\l{constant time}), |
6267 | because QString preallocates extra space at the end of the string |
6268 | data so it can grow without reallocating the entire string each |
6269 | time. |
6270 | |
6271 | \sa append(), prepend() |
6272 | */ |
6273 | |
6274 | /*! \fn QString &QString::operator+=(QLatin1StringView str) |
6275 | |
6276 | \overload operator+=() |
6277 | |
6278 | Appends the Latin-1 string viewed by \a str to this string. |
6279 | */ |
6280 | |
6281 | /*! \fn QString &QString::operator+=(QUtf8StringView str) |
6282 | \since 6.5 |
6283 | \overload operator+=() |
6284 | |
6285 | Appends the UTF-8 string view \a str to this string. |
6286 | */ |
6287 | |
6288 | /*! \fn QString &QString::operator+=(const QByteArray &ba) |
6289 | |
6290 | \overload operator+=() |
6291 | |
6292 | Appends the byte array \a ba to this string. The byte array is converted |
6293 | to Unicode using the fromUtf8() function. If any NUL characters ('\\0') |
6294 | are embedded in the \a ba byte array, they will be included in the |
6295 | transformation. |
6296 | |
6297 | You can disable this function by defining |
6298 | \l QT_NO_CAST_FROM_ASCII when you compile your applications. This |
6299 | can be useful if you want to ensure that all user-visible strings |
6300 | go through QObject::tr(), for example. |
6301 | */ |
6302 | |
6303 | /*! \fn QString &QString::operator+=(const char *str) |
6304 | |
6305 | \overload operator+=() |
6306 | |
6307 | Appends the string \a str to this string. The const char pointer |
6308 | is converted to Unicode using the fromUtf8() function. |
6309 | |
6310 | You can disable this function by defining \l QT_NO_CAST_FROM_ASCII |
6311 | when you compile your applications. This can be useful if you want |
6312 | to ensure that all user-visible strings go through QObject::tr(), |
6313 | for example. |
6314 | */ |
6315 | |
6316 | /*! \fn QString &QString::operator+=(QStringView str) |
6317 | \since 6.0 |
6318 | \overload operator+=() |
6319 | |
6320 | Appends the string view \a str to this string. |
6321 | */ |
6322 | |
6323 | /*! \fn QString &QString::operator+=(QChar ch) |
6324 | |
6325 | \overload operator+=() |
6326 | |
6327 | Appends the character \a ch to the string. |
6328 | */ |
6329 | |
6330 | /*! |
6331 | \fn bool QString::operator==(const char *s1, const QString &s2) |
6332 | |
6333 | \overload operator==() |
6334 | |
6335 | Returns \c true if \a s1 is equal to \a s2; otherwise returns \c false. |
6336 | Note that no string is equal to \a s1 being 0. |
6337 | |
6338 | Equivalent to \c {s1 != 0 && compare(s1, s2) == 0}. |
6339 | */ |
6340 | |
6341 | /*! |
6342 | \fn bool QString::operator!=(const char *s1, const QString &s2) |
6343 | |
6344 | Returns \c true if \a s1 is not equal to \a s2; otherwise returns |
6345 | \c false. |
6346 | |
6347 | For \a s1 != 0, this is equivalent to \c {compare(} \a s1, \a s2 |
6348 | \c {) != 0}. Note that no string is equal to \a s1 being 0. |
6349 | */ |
6350 | |
6351 | /*! |
6352 | \fn bool QString::operator<(const char *s1, const QString &s2) |
6353 | |
6354 | Returns \c true if \a s1 is lexically less than \a s2; otherwise |
6355 | returns \c false. For \a s1 != 0, this is equivalent to \c |
6356 | {compare(s1, s2) < 0}. |
6357 | |
6358 | \sa {Comparing Strings} |
6359 | */ |
6360 | |
6361 | /*! |
6362 | \fn bool QString::operator<=(const char *s1, const QString &s2) |
6363 | |
6364 | Returns \c true if \a s1 is lexically less than or equal to \a s2; |
6365 | otherwise returns \c false. For \a s1 != 0, this is equivalent to \c |
6366 | {compare(s1, s2) <= 0}. |
6367 | |
6368 | \sa {Comparing Strings} |
6369 | */ |
6370 | |
6371 | /*! |
6372 | \fn bool QString::operator>(const char *s1, const QString &s2) |
6373 | |
6374 | Returns \c true if \a s1 is lexically greater than \a s2; otherwise |
6375 | returns \c false. Equivalent to \c {compare(s1, s2) > 0}. |
6376 | |
6377 | \sa {Comparing Strings} |
6378 | */ |
6379 | |
6380 | /*! |
6381 | \fn bool QString::operator>=(const char *s1, const QString &s2) |
6382 | |
6383 | Returns \c true if \a s1 is lexically greater than or equal to \a s2; |
6384 | otherwise returns \c false. For \a s1 != 0, this is equivalent to \c |
6385 | {compare(s1, s2) >= 0}. |
6386 | |
6387 | \sa {Comparing Strings} |
6388 | */ |
6389 | |
6390 | /*! |
6391 | \fn QString operator+(const QString &s1, const QString &s2) |
6392 | \fn QString operator+(QString &&s1, const QString &s2) |
6393 | \relates QString |
6394 | |
6395 | Returns a string which is the result of concatenating \a s1 and \a |
6396 | s2. |
6397 | */ |
6398 | |
6399 | /*! |
6400 | \fn QString operator+(const QString &s1, const char *s2) |
6401 | \relates QString |
6402 | |
6403 | Returns a string which is the result of concatenating \a s1 and \a |
6404 | s2 (\a s2 is converted to Unicode using the QString::fromUtf8() |
6405 | function). |
6406 | |
6407 | \sa QString::fromUtf8() |
6408 | */ |
6409 | |
6410 | /*! |
6411 | \fn QString operator+(const char *s1, const QString &s2) |
6412 | \relates QString |
6413 | |
6414 | Returns a string which is the result of concatenating \a s1 and \a |
6415 | s2 (\a s1 is converted to Unicode using the QString::fromUtf8() |
6416 | function). |
6417 | |
6418 | \sa QString::fromUtf8() |
6419 | */ |
6420 | |
6421 | /*! |
6422 | \fn int QString::compare(const QString &s1, const QString &s2, Qt::CaseSensitivity cs) |
6423 | \since 4.2 |
6424 | |
6425 | Compares \a s1 with \a s2 and returns an integer less than, equal |
6426 | to, or greater than zero if \a s1 is less than, equal to, or |
6427 | greater than \a s2. |
6428 | |
6429 | \include qstring.qdocinc {search-comparison-case-sensitivity} {comparison} |
6430 | |
6431 | Case sensitive comparison is based exclusively on the numeric |
6432 | Unicode values of the characters and is very fast, but is not what |
6433 | a human would expect. Consider sorting user-visible strings with |
6434 | localeAwareCompare(). |
6435 | |
6436 | \snippet qstring/main.cpp 16 |
6437 | |
6438 | //! [compare-isNull-vs-isEmpty] |
6439 | \note This function treats null strings the same as empty strings, |
6440 | for more details see \l {Distinction Between Null and Empty Strings}. |
6441 | //! [compare-isNull-vs-isEmpty] |
6442 | |
6443 | \sa operator==(), operator<(), operator>(), {Comparing Strings} |
6444 | */ |
6445 | |
6446 | /*! |
6447 | \fn int QString::compare(const QString &s1, QLatin1StringView s2, Qt::CaseSensitivity cs) |
6448 | \since 4.2 |
6449 | \overload compare() |
6450 | |
6451 | Performs a comparison of \a s1 and \a s2, using the case |
6452 | sensitivity setting \a cs. |
6453 | */ |
6454 | |
6455 | /*! |
6456 | \fn int QString::compare(QLatin1StringView s1, const QString &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive) |
6457 | |
6458 | \since 4.2 |
6459 | \overload compare() |
6460 | |
6461 | Performs a comparison of \a s1 and \a s2, using the case |
6462 | sensitivity setting \a cs. |
6463 | */ |
6464 | |
6465 | /*! |
6466 | \fn int QString::compare(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
6467 | |
6468 | \since 5.12 |
6469 | \overload compare() |
6470 | |
6471 | Performs a comparison of this with \a s, using the case |
6472 | sensitivity setting \a cs. |
6473 | */ |
6474 | |
6475 | /*! |
6476 | \fn int QString::compare(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
6477 | |
6478 | \since 5.14 |
6479 | \overload compare() |
6480 | |
6481 | Performs a comparison of this with \a ch, using the case |
6482 | sensitivity setting \a cs. |
6483 | */ |
6484 | |
6485 | /*! |
6486 | \overload compare() |
6487 | \since 4.2 |
6488 | |
6489 | Lexically compares this string with the \a other string and |
6490 | returns an integer less than, equal to, or greater than zero if |
6491 | this string is less than, equal to, or greater than the other |
6492 | string. |
6493 | |
6494 | Same as compare(*this, \a other, \a cs). |
6495 | */ |
6496 | int QString::compare(const QString &other, Qt::CaseSensitivity cs) const noexcept |
6497 | { |
6498 | return QtPrivate::compareStrings(lhs: *this, rhs: other, cs); |
6499 | } |
6500 | |
6501 | /*! |
6502 | \internal |
6503 | \since 4.5 |
6504 | */ |
6505 | int QString::compare_helper(const QChar *data1, qsizetype length1, const QChar *data2, qsizetype length2, |
6506 | Qt::CaseSensitivity cs) noexcept |
6507 | { |
6508 | Q_ASSERT(length1 >= 0); |
6509 | Q_ASSERT(length2 >= 0); |
6510 | Q_ASSERT(data1 || length1 == 0); |
6511 | Q_ASSERT(data2 || length2 == 0); |
6512 | return QtPrivate::compareStrings(lhs: QStringView(data1, length1), rhs: QStringView(data2, length2), cs); |
6513 | } |
6514 | |
6515 | /*! |
6516 | \overload compare() |
6517 | \since 4.2 |
6518 | |
6519 | Same as compare(*this, \a other, \a cs). |
6520 | */ |
6521 | int QString::compare(QLatin1StringView other, Qt::CaseSensitivity cs) const noexcept |
6522 | { |
6523 | return QtPrivate::compareStrings(lhs: *this, rhs: other, cs); |
6524 | } |
6525 | |
6526 | /*! |
6527 | \internal |
6528 | \since 5.0 |
6529 | */ |
6530 | int QString::compare_helper(const QChar *data1, qsizetype length1, const char *data2, qsizetype length2, |
6531 | Qt::CaseSensitivity cs) noexcept |
6532 | { |
6533 | Q_ASSERT(length1 >= 0); |
6534 | Q_ASSERT(data1 || length1 == 0); |
6535 | if (!data2) |
6536 | return qt_lencmp(lhs: length1, rhs: 0); |
6537 | if (Q_UNLIKELY(length2 < 0)) |
6538 | length2 = qsizetype(strlen(s: data2)); |
6539 | return QtPrivate::compareStrings(lhs: QStringView(data1, length1), |
6540 | rhs: QUtf8StringView(data2, length2), cs); |
6541 | } |
6542 | |
6543 | /*! |
6544 | \fn int QString::compare(const QString &s1, QStringView s2, Qt::CaseSensitivity cs = Qt::CaseSensitive) |
6545 | \overload compare() |
6546 | */ |
6547 | |
6548 | /*! |
6549 | \fn int QString::compare(QStringView s1, const QString &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive) |
6550 | \overload compare() |
6551 | */ |
6552 | |
6553 | /*! |
6554 | \internal |
6555 | \since 6.6 |
6556 | */ |
6557 | int QLatin1StringView::compare_helper(const QLatin1StringView &s1, const char *s2, qsizetype len) noexcept |
6558 | { |
6559 | // because qlatin1stringview.h can't include qutf8stringview.h |
6560 | Q_ASSERT(len >= 0); |
6561 | Q_ASSERT(s2 || len == 0); |
6562 | return QtPrivate::compareStrings(lhs: s1, rhs: QUtf8StringView(s2, len)); |
6563 | } |
6564 | |
6565 | /*! |
6566 | \internal |
6567 | \since 4.5 |
6568 | */ |
6569 | int QLatin1StringView::compare_helper(const QChar *data1, qsizetype length1, QLatin1StringView s2, |
6570 | Qt::CaseSensitivity cs) noexcept |
6571 | { |
6572 | Q_ASSERT(length1 >= 0); |
6573 | Q_ASSERT(data1 || length1 == 0); |
6574 | return QtPrivate::compareStrings(lhs: QStringView(data1, length1), rhs: s2, cs); |
6575 | } |
6576 | |
6577 | /*! |
6578 | \fn int QString::localeAwareCompare(const QString & s1, const QString & s2) |
6579 | |
6580 | Compares \a s1 with \a s2 and returns an integer less than, equal |
6581 | to, or greater than zero if \a s1 is less than, equal to, or |
6582 | greater than \a s2. |
6583 | |
6584 | The comparison is performed in a locale- and also |
6585 | platform-dependent manner. Use this function to present sorted |
6586 | lists of strings to the user. |
6587 | |
6588 | \sa compare(), QLocale, {Comparing Strings} |
6589 | */ |
6590 | |
6591 | /*! |
6592 | \fn int QString::localeAwareCompare(QStringView other) const |
6593 | \since 6.0 |
6594 | \overload localeAwareCompare() |
6595 | |
6596 | Compares this string with the \a other string and returns an |
6597 | integer less than, equal to, or greater than zero if this string |
6598 | is less than, equal to, or greater than the \a other string. |
6599 | |
6600 | The comparison is performed in a locale- and also |
6601 | platform-dependent manner. Use this function to present sorted |
6602 | lists of strings to the user. |
6603 | |
6604 | Same as \c {localeAwareCompare(*this, other)}. |
6605 | |
6606 | \sa {Comparing Strings} |
6607 | */ |
6608 | |
6609 | /*! |
6610 | \fn int QString::localeAwareCompare(QStringView s1, QStringView s2) |
6611 | \since 6.0 |
6612 | \overload localeAwareCompare() |
6613 | |
6614 | Compares \a s1 with \a s2 and returns an integer less than, equal |
6615 | to, or greater than zero if \a s1 is less than, equal to, or |
6616 | greater than \a s2. |
6617 | |
6618 | The comparison is performed in a locale- and also |
6619 | platform-dependent manner. Use this function to present sorted |
6620 | lists of strings to the user. |
6621 | |
6622 | \sa {Comparing Strings} |
6623 | */ |
6624 | |
6625 | |
6626 | #if !defined(CSTR_LESS_THAN) |
6627 | #define CSTR_LESS_THAN 1 |
6628 | #define CSTR_EQUAL 2 |
6629 | #define CSTR_GREATER_THAN 3 |
6630 | #endif |
6631 | |
6632 | /*! |
6633 | \overload localeAwareCompare() |
6634 | |
6635 | Compares this string with the \a other string and returns an |
6636 | integer less than, equal to, or greater than zero if this string |
6637 | is less than, equal to, or greater than the \a other string. |
6638 | |
6639 | The comparison is performed in a locale- and also |
6640 | platform-dependent manner. Use this function to present sorted |
6641 | lists of strings to the user. |
6642 | |
6643 | Same as \c {localeAwareCompare(*this, other)}. |
6644 | |
6645 | \sa {Comparing Strings} |
6646 | */ |
6647 | int QString::localeAwareCompare(const QString &other) const |
6648 | { |
6649 | return localeAwareCompare_helper(data1: constData(), length1: size(), data2: other.constData(), length2: other.size()); |
6650 | } |
6651 | |
6652 | /*! |
6653 | \internal |
6654 | \since 4.5 |
6655 | */ |
6656 | int QString::localeAwareCompare_helper(const QChar *data1, qsizetype length1, |
6657 | const QChar *data2, qsizetype length2) |
6658 | { |
6659 | Q_ASSERT(length1 >= 0); |
6660 | Q_ASSERT(data1 || length1 == 0); |
6661 | Q_ASSERT(length2 >= 0); |
6662 | Q_ASSERT(data2 || length2 == 0); |
6663 | |
6664 | // do the right thing for null and empty |
6665 | if (length1 == 0 || length2 == 0) |
6666 | return QtPrivate::compareStrings(lhs: QStringView(data1, length1), rhs: QStringView(data2, length2), |
6667 | cs: Qt::CaseSensitive); |
6668 | |
6669 | #if QT_CONFIG(icu) |
6670 | return QCollator::defaultCompare(s1: QStringView(data1, length1), s2: QStringView(data2, length2)); |
6671 | #else |
6672 | const QString lhs = QString::fromRawData(data1, length1).normalized(QString::NormalizationForm_C); |
6673 | const QString rhs = QString::fromRawData(data2, length2).normalized(QString::NormalizationForm_C); |
6674 | # if defined(Q_OS_WIN) |
6675 | int res = CompareStringEx(LOCALE_NAME_USER_DEFAULT, 0, (LPWSTR)lhs.constData(), lhs.length(), (LPWSTR)rhs.constData(), rhs.length(), NULL, NULL, 0); |
6676 | |
6677 | switch (res) { |
6678 | case CSTR_LESS_THAN: |
6679 | return -1; |
6680 | case CSTR_GREATER_THAN: |
6681 | return 1; |
6682 | default: |
6683 | return 0; |
6684 | } |
6685 | # elif defined (Q_OS_DARWIN) |
6686 | // Use CFStringCompare for comparing strings on Mac. This makes Qt order |
6687 | // strings the same way as native applications do, and also respects |
6688 | // the "Order for sorted lists" setting in the International preferences |
6689 | // panel. |
6690 | const CFStringRef thisString = |
6691 | CFStringCreateWithCharactersNoCopy(kCFAllocatorDefault, |
6692 | reinterpret_cast<const UniChar *>(lhs.constData()), lhs.length(), kCFAllocatorNull); |
6693 | const CFStringRef otherString = |
6694 | CFStringCreateWithCharactersNoCopy(kCFAllocatorDefault, |
6695 | reinterpret_cast<const UniChar *>(rhs.constData()), rhs.length(), kCFAllocatorNull); |
6696 | |
6697 | const int result = CFStringCompare(thisString, otherString, kCFCompareLocalized); |
6698 | CFRelease(thisString); |
6699 | CFRelease(otherString); |
6700 | return result; |
6701 | # elif defined(Q_OS_UNIX) |
6702 | // declared in <string.h> (no better than QtPrivate::compareStrings() on Android, sadly) |
6703 | return strcoll(lhs.toLocal8Bit().constData(), rhs.toLocal8Bit().constData()); |
6704 | # else |
6705 | # error "This case shouldn't happen" |
6706 | return QtPrivate::compareStrings(lhs, rhs, Qt::CaseSensitive); |
6707 | # endif |
6708 | #endif // !QT_CONFIG(icu) |
6709 | } |
6710 | |
6711 | |
6712 | /*! |
6713 | \fn const QChar *QString::unicode() const |
6714 | |
6715 | Returns a Unicode representation of the string. |
6716 | The result remains valid until the string is modified. |
6717 | |
6718 | \note The returned string may not be '\\0'-terminated. |
6719 | Use size() to determine the length of the array. |
6720 | |
6721 | \sa utf16(), fromRawData() |
6722 | */ |
6723 | |
6724 | /*! |
6725 | \fn const ushort *QString::utf16() const |
6726 | |
6727 | Returns the QString as a '\\0\'-terminated array of unsigned |
6728 | shorts. The result remains valid until the string is modified. |
6729 | |
6730 | The returned string is in host byte order. |
6731 | |
6732 | \sa unicode() |
6733 | */ |
6734 | |
6735 | const ushort *QString::utf16() const |
6736 | { |
6737 | if (!d->isMutable()) { |
6738 | // ensure '\0'-termination for ::fromRawData strings |
6739 | const_cast<QString*>(this)->reallocData(alloc: d.size, option: QArrayData::KeepSize); |
6740 | } |
6741 | return reinterpret_cast<const ushort *>(d.data()); |
6742 | } |
6743 | |
6744 | /*! |
6745 | Returns a string of size \a width that contains this string |
6746 | padded by the \a fill character. |
6747 | |
6748 | If \a truncate is \c false and the size() of the string is more than |
6749 | \a width, then the returned string is a copy of the string. |
6750 | |
6751 | \snippet qstring/main.cpp 32 |
6752 | |
6753 | If \a truncate is \c true and the size() of the string is more than |
6754 | \a width, then any characters in a copy of the string after |
6755 | position \a width are removed, and the copy is returned. |
6756 | |
6757 | \snippet qstring/main.cpp 33 |
6758 | |
6759 | \sa rightJustified() |
6760 | */ |
6761 | |
6762 | QString QString::leftJustified(qsizetype width, QChar fill, bool truncate) const |
6763 | { |
6764 | QString result; |
6765 | qsizetype len = size(); |
6766 | qsizetype padlen = width - len; |
6767 | if (padlen > 0) { |
6768 | result.resize(size: len+padlen); |
6769 | if (len) |
6770 | memcpy(dest: result.d.data(), src: d.data(), n: sizeof(QChar)*len); |
6771 | QChar *uc = (QChar*)result.d.data() + len; |
6772 | while (padlen--) |
6773 | * uc++ = fill; |
6774 | } else { |
6775 | if (truncate) |
6776 | result = left(n: width); |
6777 | else |
6778 | result = *this; |
6779 | } |
6780 | return result; |
6781 | } |
6782 | |
6783 | /*! |
6784 | Returns a string of size() \a width that contains the \a fill |
6785 | character followed by the string. For example: |
6786 | |
6787 | \snippet qstring/main.cpp 49 |
6788 | |
6789 | If \a truncate is \c false and the size() of the string is more than |
6790 | \a width, then the returned string is a copy of the string. |
6791 | |
6792 | If \a truncate is true and the size() of the string is more than |
6793 | \a width, then the resulting string is truncated at position \a |
6794 | width. |
6795 | |
6796 | \snippet qstring/main.cpp 50 |
6797 | |
6798 | \sa leftJustified() |
6799 | */ |
6800 | |
6801 | QString QString::rightJustified(qsizetype width, QChar fill, bool truncate) const |
6802 | { |
6803 | QString result; |
6804 | qsizetype len = size(); |
6805 | qsizetype padlen = width - len; |
6806 | if (padlen > 0) { |
6807 | result.resize(size: len+padlen); |
6808 | QChar *uc = (QChar*)result.d.data(); |
6809 | while (padlen--) |
6810 | * uc++ = fill; |
6811 | if (len) |
6812 | memcpy(dest: static_cast<void *>(uc), src: static_cast<const void *>(d.data()), n: sizeof(QChar)*len); |
6813 | } else { |
6814 | if (truncate) |
6815 | result = left(n: width); |
6816 | else |
6817 | result = *this; |
6818 | } |
6819 | return result; |
6820 | } |
6821 | |
6822 | /*! |
6823 | \fn QString QString::toLower() const |
6824 | |
6825 | Returns a lowercase copy of the string. |
6826 | |
6827 | \snippet qstring/main.cpp 75 |
6828 | |
6829 | The case conversion will always happen in the 'C' locale. For |
6830 | locale-dependent case folding use QLocale::toLower() |
6831 | |
6832 | \sa toUpper(), QLocale::toLower() |
6833 | */ |
6834 | |
6835 | namespace QUnicodeTables { |
6836 | /* |
6837 | \internal |
6838 | Converts the \a str string starting from the position pointed to by the \a |
6839 | it iterator, using the Unicode case traits \c Traits, and returns the |
6840 | result. The input string must not be empty (the convertCase function below |
6841 | guarantees that). |
6842 | |
6843 | The string type \c{T} is also a template and is either \c{const QString} or |
6844 | \c{QString}. This function can do both copy-conversion and in-place |
6845 | conversion depending on the state of the \a str parameter: |
6846 | \list |
6847 | \li \c{T} is \c{const QString}: copy-convert |
6848 | \li \c{T} is \c{QString} and its refcount != 1: copy-convert |
6849 | \li \c{T} is \c{QString} and its refcount == 1: in-place convert |
6850 | \endlist |
6851 | |
6852 | In copy-convert mode, the local variable \c{s} is detached from the input |
6853 | \a str. In the in-place convert mode, \a str is in moved-from state and |
6854 | \c{s} contains the only copy of the string, without reallocation (thus, |
6855 | \a it is still valid). |
6856 | |
6857 | There is one pathological case left: when the in-place conversion needs to |
6858 | reallocate memory to grow the buffer. In that case, we need to adjust the \a |
6859 | it pointer. |
6860 | */ |
6861 | template <typename T> |
6862 | Q_NEVER_INLINE |
6863 | static QString detachAndConvertCase(T &str, QStringIterator it, QUnicodeTables::Case which) |
6864 | { |
6865 | Q_ASSERT(!str.isEmpty()); |
6866 | QString s = std::move(str); // will copy if T is const QString |
6867 | QChar *pp = s.begin() + it.index(); // will detach if necessary |
6868 | |
6869 | do { |
6870 | const auto folded = fullConvertCase(uc: it.next(), which); |
6871 | if (Q_UNLIKELY(folded.size() > 1)) { |
6872 | if (folded.chars[0] == *pp && folded.size() == 2) { |
6873 | // special case: only second actually changed (e.g. surrogate pairs), |
6874 | // avoid slow case |
6875 | ++pp; |
6876 | *pp++ = folded.chars[1]; |
6877 | } else { |
6878 | // slow path: the string is growing |
6879 | qsizetype inpos = it.index() - 1; |
6880 | qsizetype outpos = pp - s.constBegin(); |
6881 | |
6882 | s.replace(pos: outpos, len: 1, after: reinterpret_cast<const QChar *>(folded.data()), alen: folded.size()); |
6883 | pp = const_cast<QChar *>(s.constBegin()) + outpos + folded.size(); |
6884 | |
6885 | // Adjust the input iterator if we are performing an in-place conversion |
6886 | if constexpr (!std::is_const<T>::value) |
6887 | it = QStringIterator(s.constBegin(), inpos + folded.size(), s.constEnd()); |
6888 | } |
6889 | } else { |
6890 | *pp++ = folded.chars[0]; |
6891 | } |
6892 | } while (it.hasNext()); |
6893 | |
6894 | return s; |
6895 | } |
6896 | |
6897 | template <typename T> |
6898 | static QString convertCase(T &str, QUnicodeTables::Case which) |
6899 | { |
6900 | const QChar *p = str.constBegin(); |
6901 | const QChar *e = p + str.size(); |
6902 | |
6903 | // this avoids out of bounds check in the loop |
6904 | while (e != p && e[-1].isHighSurrogate()) |
6905 | --e; |
6906 | |
6907 | QStringIterator it(p, e); |
6908 | while (it.hasNext()) { |
6909 | const char32_t uc = it.next(); |
6910 | if (qGetProp(ucs4: uc)->cases[which].diff) { |
6911 | it.recede(); |
6912 | return detachAndConvertCase(str, it, which); |
6913 | } |
6914 | } |
6915 | return std::move(str); |
6916 | } |
6917 | } // namespace QUnicodeTables |
6918 | |
6919 | QString QString::toLower_helper(const QString &str) |
6920 | { |
6921 | return QUnicodeTables::convertCase(str, which: QUnicodeTables::LowerCase); |
6922 | } |
6923 | |
6924 | QString QString::toLower_helper(QString &str) |
6925 | { |
6926 | return QUnicodeTables::convertCase(str, which: QUnicodeTables::LowerCase); |
6927 | } |
6928 | |
6929 | /*! |
6930 | \fn QString QString::toCaseFolded() const |
6931 | |
6932 | Returns the case folded equivalent of the string. For most Unicode |
6933 | characters this is the same as toLower(). |
6934 | */ |
6935 | |
6936 | QString QString::toCaseFolded_helper(const QString &str) |
6937 | { |
6938 | return QUnicodeTables::convertCase(str, which: QUnicodeTables::CaseFold); |
6939 | } |
6940 | |
6941 | QString QString::toCaseFolded_helper(QString &str) |
6942 | { |
6943 | return QUnicodeTables::convertCase(str, which: QUnicodeTables::CaseFold); |
6944 | } |
6945 | |
6946 | /*! |
6947 | \fn QString QString::toUpper() const |
6948 | |
6949 | Returns an uppercase copy of the string. |
6950 | |
6951 | \snippet qstring/main.cpp 81 |
6952 | |
6953 | The case conversion will always happen in the 'C' locale. For |
6954 | locale-dependent case folding use QLocale::toUpper() |
6955 | |
6956 | \sa toLower(), QLocale::toLower() |
6957 | */ |
6958 | |
6959 | QString QString::toUpper_helper(const QString &str) |
6960 | { |
6961 | return QUnicodeTables::convertCase(str, which: QUnicodeTables::UpperCase); |
6962 | } |
6963 | |
6964 | QString QString::toUpper_helper(QString &str) |
6965 | { |
6966 | return QUnicodeTables::convertCase(str, which: QUnicodeTables::UpperCase); |
6967 | } |
6968 | |
6969 | /*! |
6970 | \since 5.5 |
6971 | |
6972 | Safely builds a formatted string from the format string \a cformat |
6973 | and an arbitrary list of arguments. |
6974 | |
6975 | The format string supports the conversion specifiers, length modifiers, |
6976 | and flags provided by printf() in the standard C++ library. The \a cformat |
6977 | string and \c{%s} arguments must be UTF-8 encoded. |
6978 | |
6979 | \note The \c{%lc} escape sequence expects a unicode character of type |
6980 | \c char16_t, or \c ushort (as returned by QChar::unicode()). |
6981 | The \c{%ls} escape sequence expects a pointer to a zero-terminated array |
6982 | of unicode characters of type \c char16_t, or ushort (as returned by |
6983 | QString::utf16()). This is at odds with the printf() in the standard C++ |
6984 | library, which defines \c {%lc} to print a wchar_t and \c{%ls} to print |
6985 | a \c{wchar_t*}, and might also produce compiler warnings on platforms |
6986 | where the size of \c {wchar_t} is not 16 bits. |
6987 | |
6988 | \warning We do not recommend using QString::asprintf() in new Qt |
6989 | code. Instead, consider using QTextStream or arg(), both of |
6990 | which support Unicode strings seamlessly and are type-safe. |
6991 | Here is an example that uses QTextStream: |
6992 | |
6993 | \snippet qstring/main.cpp 64 |
6994 | |
6995 | For \l {QObject::tr()}{translations}, especially if the strings |
6996 | contains more than one escape sequence, you should consider using |
6997 | the arg() function instead. This allows the order of the |
6998 | replacements to be controlled by the translator. |
6999 | |
7000 | \sa arg() |
7001 | */ |
7002 | |
7003 | QString QString::asprintf(const char *cformat, ...) |
7004 | { |
7005 | va_list ap; |
7006 | va_start(ap, cformat); |
7007 | const QString s = vasprintf(format: cformat, ap); |
7008 | va_end(ap); |
7009 | return s; |
7010 | } |
7011 | |
7012 | static void append_utf8(QString &qs, const char *cs, qsizetype len) |
7013 | { |
7014 | const qsizetype oldSize = qs.size(); |
7015 | qs.resize(size: oldSize + len); |
7016 | const QChar *newEnd = QUtf8::convertToUnicode(buffer: qs.data() + oldSize, in: QByteArrayView(cs, len)); |
7017 | qs.resize(size: newEnd - qs.constData()); |
7018 | } |
7019 | |
7020 | static uint parse_flag_characters(const char * &c) noexcept |
7021 | { |
7022 | uint flags = QLocaleData::ZeroPadExponent; |
7023 | while (true) { |
7024 | switch (*c) { |
7025 | case '#': |
7026 | flags |= QLocaleData::ShowBase | QLocaleData::AddTrailingZeroes |
7027 | | QLocaleData::ForcePoint; |
7028 | break; |
7029 | case '0': flags |= QLocaleData::ZeroPadded; break; |
7030 | case '-': flags |= QLocaleData::LeftAdjusted; break; |
7031 | case ' ': flags |= QLocaleData::BlankBeforePositive; break; |
7032 | case '+': flags |= QLocaleData::AlwaysShowSign; break; |
7033 | case '\'': flags |= QLocaleData::GroupDigits; break; |
7034 | default: return flags; |
7035 | } |
7036 | ++c; |
7037 | } |
7038 | } |
7039 | |
7040 | static int parse_field_width(const char *&c, qsizetype size) |
7041 | { |
7042 | Q_ASSERT(isAsciiDigit(*c)); |
7043 | const char *const stop = c + size; |
7044 | |
7045 | // can't be negative - started with a digit |
7046 | // contains at least one digit |
7047 | auto [result, used] = qstrntoull(nptr: c, size, base: 10); |
7048 | c += used; |
7049 | if (used <= 0) |
7050 | return false; |
7051 | // preserve Qt 5.5 behavior of consuming all digits, no matter how many |
7052 | while (c < stop && isAsciiDigit(c: *c)) |
7053 | ++c; |
7054 | return result < qulonglong(std::numeric_limits<int>::max()) ? int(result) : 0; |
7055 | } |
7056 | |
7057 | enum LengthMod { lm_none, lm_hh, lm_h, lm_l, lm_ll, lm_L, lm_j, lm_z, lm_t }; |
7058 | |
7059 | static inline bool can_consume(const char * &c, char ch) noexcept |
7060 | { |
7061 | if (*c == ch) { |
7062 | ++c; |
7063 | return true; |
7064 | } |
7065 | return false; |
7066 | } |
7067 | |
7068 | static LengthMod parse_length_modifier(const char * &c) noexcept |
7069 | { |
7070 | switch (*c++) { |
7071 | case 'h': return can_consume(c, ch: 'h') ? lm_hh : lm_h; |
7072 | case 'l': return can_consume(c, ch: 'l') ? lm_ll : lm_l; |
7073 | case 'L': return lm_L; |
7074 | case 'j': return lm_j; |
7075 | case 'z': |
7076 | case 'Z': return lm_z; |
7077 | case 't': return lm_t; |
7078 | } |
7079 | --c; // don't consume *c - it wasn't a flag |
7080 | return lm_none; |
7081 | } |
7082 | |
7083 | /*! |
7084 | \fn QString QString::vasprintf(const char *cformat, va_list ap) |
7085 | \since 5.5 |
7086 | |
7087 | Equivalent method to asprintf(), but takes a va_list \a ap |
7088 | instead a list of variable arguments. See the asprintf() |
7089 | documentation for an explanation of \a cformat. |
7090 | |
7091 | This method does not call the va_end macro, the caller |
7092 | is responsible to call va_end on \a ap. |
7093 | |
7094 | \sa asprintf() |
7095 | */ |
7096 | |
7097 | QString QString::vasprintf(const char *cformat, va_list ap) |
7098 | { |
7099 | if (!cformat || !*cformat) { |
7100 | // Qt 1.x compat |
7101 | return fromLatin1(ba: "" ); |
7102 | } |
7103 | |
7104 | // Parse cformat |
7105 | |
7106 | QString result; |
7107 | const char *c = cformat; |
7108 | const char *formatEnd = cformat + qstrlen(str: cformat); |
7109 | for (;;) { |
7110 | // Copy non-escape chars to result |
7111 | const char *cb = c; |
7112 | while (*c != '\0' && *c != '%') |
7113 | c++; |
7114 | append_utf8(qs&: result, cs: cb, len: qsizetype(c - cb)); |
7115 | |
7116 | if (*c == '\0') |
7117 | break; |
7118 | |
7119 | // Found '%' |
7120 | const char *escape_start = c; |
7121 | ++c; |
7122 | |
7123 | if (*c == '\0') { |
7124 | result.append(ch: u'%'); // a % at the end of the string - treat as non-escape text |
7125 | break; |
7126 | } |
7127 | if (*c == '%') { |
7128 | result.append(ch: u'%'); // %% |
7129 | ++c; |
7130 | continue; |
7131 | } |
7132 | |
7133 | uint flags = parse_flag_characters(c); |
7134 | |
7135 | if (*c == '\0') { |
7136 | result.append(str: QLatin1StringView(escape_start)); // incomplete escape, treat as non-escape text |
7137 | break; |
7138 | } |
7139 | |
7140 | // Parse field width |
7141 | int width = -1; // -1 means unspecified |
7142 | if (isAsciiDigit(c: *c)) { |
7143 | width = parse_field_width(c, size: formatEnd - c); |
7144 | } else if (*c == '*') { // can't parse this in another function, not portably, at least |
7145 | width = va_arg(ap, int); |
7146 | if (width < 0) |
7147 | width = -1; // treat all negative numbers as unspecified |
7148 | ++c; |
7149 | } |
7150 | |
7151 | if (*c == '\0') { |
7152 | result.append(str: QLatin1StringView(escape_start)); // incomplete escape, treat as non-escape text |
7153 | break; |
7154 | } |
7155 | |
7156 | // Parse precision |
7157 | int precision = -1; // -1 means unspecified |
7158 | if (*c == '.') { |
7159 | ++c; |
7160 | precision = 0; |
7161 | if (isAsciiDigit(c: *c)) { |
7162 | precision = parse_field_width(c, size: formatEnd - c); |
7163 | } else if (*c == '*') { // can't parse this in another function, not portably, at least |
7164 | precision = va_arg(ap, int); |
7165 | if (precision < 0) |
7166 | precision = -1; // treat all negative numbers as unspecified |
7167 | ++c; |
7168 | } |
7169 | } |
7170 | |
7171 | if (*c == '\0') { |
7172 | result.append(str: QLatin1StringView(escape_start)); // incomplete escape, treat as non-escape text |
7173 | break; |
7174 | } |
7175 | |
7176 | const LengthMod length_mod = parse_length_modifier(c); |
7177 | |
7178 | if (*c == '\0') { |
7179 | result.append(str: QLatin1StringView(escape_start)); // incomplete escape, treat as non-escape text |
7180 | break; |
7181 | } |
7182 | |
7183 | // Parse the conversion specifier and do the conversion |
7184 | QString subst; |
7185 | switch (*c) { |
7186 | case 'd': |
7187 | case 'i': { |
7188 | qint64 i; |
7189 | switch (length_mod) { |
7190 | case lm_none: i = va_arg(ap, int); break; |
7191 | case lm_hh: i = va_arg(ap, int); break; |
7192 | case lm_h: i = va_arg(ap, int); break; |
7193 | case lm_l: i = va_arg(ap, long int); break; |
7194 | case lm_ll: i = va_arg(ap, qint64); break; |
7195 | case lm_j: i = va_arg(ap, long int); break; |
7196 | |
7197 | /* ptrdiff_t actually, but it should be the same for us */ |
7198 | case lm_z: i = va_arg(ap, qsizetype); break; |
7199 | case lm_t: i = va_arg(ap, qsizetype); break; |
7200 | default: i = 0; break; |
7201 | } |
7202 | subst = QLocaleData::c()->longLongToString(l: i, precision, base: 10, width, flags); |
7203 | ++c; |
7204 | break; |
7205 | } |
7206 | case 'o': |
7207 | case 'u': |
7208 | case 'x': |
7209 | case 'X': { |
7210 | quint64 u; |
7211 | switch (length_mod) { |
7212 | case lm_none: u = va_arg(ap, uint); break; |
7213 | case lm_hh: u = va_arg(ap, uint); break; |
7214 | case lm_h: u = va_arg(ap, uint); break; |
7215 | case lm_l: u = va_arg(ap, ulong); break; |
7216 | case lm_ll: u = va_arg(ap, quint64); break; |
7217 | case lm_t: u = va_arg(ap, size_t); break; |
7218 | case lm_z: u = va_arg(ap, size_t); break; |
7219 | default: u = 0; break; |
7220 | } |
7221 | |
7222 | if (isAsciiUpper(c: *c)) |
7223 | flags |= QLocaleData::CapitalEorX; |
7224 | |
7225 | int base = 10; |
7226 | switch (QtMiscUtils::toAsciiLower(ch: *c)) { |
7227 | case 'o': |
7228 | base = 8; break; |
7229 | case 'u': |
7230 | base = 10; break; |
7231 | case 'x': |
7232 | base = 16; break; |
7233 | default: break; |
7234 | } |
7235 | subst = QLocaleData::c()->unsLongLongToString(l: u, precision, base, width, flags); |
7236 | ++c; |
7237 | break; |
7238 | } |
7239 | case 'E': |
7240 | case 'e': |
7241 | case 'F': |
7242 | case 'f': |
7243 | case 'G': |
7244 | case 'g': |
7245 | case 'A': |
7246 | case 'a': { |
7247 | double d; |
7248 | if (length_mod == lm_L) |
7249 | d = va_arg(ap, long double); // not supported - converted to a double |
7250 | else |
7251 | d = va_arg(ap, double); |
7252 | |
7253 | if (isAsciiUpper(c: *c)) |
7254 | flags |= QLocaleData::CapitalEorX; |
7255 | |
7256 | QLocaleData::DoubleForm form = QLocaleData::DFDecimal; |
7257 | switch (QtMiscUtils::toAsciiLower(ch: *c)) { |
7258 | case 'e': form = QLocaleData::DFExponent; break; |
7259 | case 'a': // not supported - decimal form used instead |
7260 | case 'f': form = QLocaleData::DFDecimal; break; |
7261 | case 'g': form = QLocaleData::DFSignificantDigits; break; |
7262 | default: break; |
7263 | } |
7264 | subst = QLocaleData::c()->doubleToString(d, precision, form, width, flags); |
7265 | ++c; |
7266 | break; |
7267 | } |
7268 | case 'c': { |
7269 | if (length_mod == lm_l) |
7270 | subst = QChar::fromUcs2(va_arg(ap, int)); |
7271 | else |
7272 | subst = QLatin1Char((uchar) va_arg(ap, int)); |
7273 | ++c; |
7274 | break; |
7275 | } |
7276 | case 's': { |
7277 | if (length_mod == lm_l) { |
7278 | const ushort *buff = va_arg(ap, const ushort*); |
7279 | const ushort *ch = buff; |
7280 | while (precision != 0 && *ch != 0) { |
7281 | ++ch; |
7282 | --precision; |
7283 | } |
7284 | subst.setUtf16(autf16: buff, asize: ch - buff); |
7285 | } else if (precision == -1) { |
7286 | subst = QString::fromUtf8(va_arg(ap, const char*)); |
7287 | } else { |
7288 | const char *buff = va_arg(ap, const char*); |
7289 | subst = QString::fromUtf8(utf8: buff, size: qstrnlen(str: buff, maxlen: precision)); |
7290 | } |
7291 | ++c; |
7292 | break; |
7293 | } |
7294 | case 'p': { |
7295 | void *arg = va_arg(ap, void*); |
7296 | const quint64 i = reinterpret_cast<quintptr>(arg); |
7297 | flags |= QLocaleData::ShowBase; |
7298 | subst = QLocaleData::c()->unsLongLongToString(l: i, precision, base: 16, width, flags); |
7299 | ++c; |
7300 | break; |
7301 | } |
7302 | case 'n': |
7303 | switch (length_mod) { |
7304 | case lm_hh: { |
7305 | signed char *n = va_arg(ap, signed char*); |
7306 | *n = result.size(); |
7307 | break; |
7308 | } |
7309 | case lm_h: { |
7310 | short int *n = va_arg(ap, short int*); |
7311 | *n = result.size(); |
7312 | break; |
7313 | } |
7314 | case lm_l: { |
7315 | long int *n = va_arg(ap, long int*); |
7316 | *n = result.size(); |
7317 | break; |
7318 | } |
7319 | case lm_ll: { |
7320 | qint64 *n = va_arg(ap, qint64*); |
7321 | *n = result.size(); |
7322 | break; |
7323 | } |
7324 | default: { |
7325 | int *n = va_arg(ap, int*); |
7326 | *n = int(result.size()); |
7327 | break; |
7328 | } |
7329 | } |
7330 | ++c; |
7331 | break; |
7332 | |
7333 | default: // bad escape, treat as non-escape text |
7334 | for (const char *cc = escape_start; cc != c; ++cc) |
7335 | result.append(ch: QLatin1Char(*cc)); |
7336 | continue; |
7337 | } |
7338 | |
7339 | if (flags & QLocaleData::LeftAdjusted) |
7340 | result.append(str: subst.leftJustified(width)); |
7341 | else |
7342 | result.append(str: subst.rightJustified(width)); |
7343 | } |
7344 | |
7345 | return result; |
7346 | } |
7347 | |
7348 | /*! |
7349 | \fn QString::toLongLong(bool *ok, int base) const |
7350 | |
7351 | Returns the string converted to a \c{long long} using base \a |
7352 | base, which is 10 by default and must be between 2 and 36, or 0. |
7353 | Returns 0 if the conversion fails. |
7354 | |
7355 | If \a ok is not \nullptr, failure is reported by setting *\a{ok} |
7356 | to \c false, and success by setting *\a{ok} to \c true. |
7357 | |
7358 | If \a base is 0, the C language convention is used: if the string begins |
7359 | with "0x", base 16 is used; otherwise, if the string begins with "0b", base |
7360 | 2 is used; otherwise, if the string begins with "0", base 8 is used; |
7361 | otherwise, base 10 is used. |
7362 | |
7363 | The string conversion will always happen in the 'C' locale. For |
7364 | locale-dependent conversion use QLocale::toLongLong() |
7365 | |
7366 | Example: |
7367 | |
7368 | \snippet qstring/main.cpp 74 |
7369 | |
7370 | This function ignores leading and trailing whitespace. |
7371 | |
7372 | \note Support for the "0b" prefix was added in Qt 6.4. |
7373 | |
7374 | \sa number(), toULongLong(), toInt(), QLocale::toLongLong() |
7375 | */ |
7376 | |
7377 | template <typename Int> |
7378 | static Int toIntegral(QStringView string, bool *ok, int base) |
7379 | { |
7380 | #if defined(QT_CHECK_RANGE) |
7381 | if (base != 0 && (base < 2 || base > 36)) { |
7382 | qWarning("QString::toIntegral: Invalid base (%d)" , base); |
7383 | base = 10; |
7384 | } |
7385 | #endif |
7386 | |
7387 | QVarLengthArray<uchar> latin1(string.size()); |
7388 | qt_to_latin1(dst: latin1.data(), src: string.utf16(), length: string.size()); |
7389 | if constexpr (std::is_signed_v<Int>) |
7390 | return QLocaleData::bytearrayToLongLong(num: latin1, base, ok); |
7391 | else |
7392 | return QLocaleData::bytearrayToUnsLongLong(num: latin1, base, ok); |
7393 | } |
7394 | |
7395 | qlonglong QString::toIntegral_helper(QStringView string, bool *ok, int base) |
7396 | { |
7397 | return toIntegral<qlonglong>(string, ok, base); |
7398 | } |
7399 | |
7400 | /*! |
7401 | \fn QString::toULongLong(bool *ok, int base) const |
7402 | |
7403 | Returns the string converted to an \c{unsigned long long} using base \a |
7404 | base, which is 10 by default and must be between 2 and 36, or 0. |
7405 | Returns 0 if the conversion fails. |
7406 | |
7407 | If \a ok is not \nullptr, failure is reported by setting *\a{ok} |
7408 | to \c false, and success by setting *\a{ok} to \c true. |
7409 | |
7410 | If \a base is 0, the C language convention is used: if the string begins |
7411 | with "0x", base 16 is used; otherwise, if the string begins with "0b", base |
7412 | 2 is used; otherwise, if the string begins with "0", base 8 is used; |
7413 | otherwise, base 10 is used. |
7414 | |
7415 | The string conversion will always happen in the 'C' locale. For |
7416 | locale-dependent conversion use QLocale::toULongLong() |
7417 | |
7418 | Example: |
7419 | |
7420 | \snippet qstring/main.cpp 79 |
7421 | |
7422 | This function ignores leading and trailing whitespace. |
7423 | |
7424 | \note Support for the "0b" prefix was added in Qt 6.4. |
7425 | |
7426 | \sa number(), toLongLong(), QLocale::toULongLong() |
7427 | */ |
7428 | |
7429 | qulonglong QString::toIntegral_helper(QStringView string, bool *ok, uint base) |
7430 | { |
7431 | return toIntegral<qulonglong>(string, ok, base); |
7432 | } |
7433 | |
7434 | /*! |
7435 | \fn long QString::toLong(bool *ok, int base) const |
7436 | |
7437 | Returns the string converted to a \c long using base \a |
7438 | base, which is 10 by default and must be between 2 and 36, or 0. |
7439 | Returns 0 if the conversion fails. |
7440 | |
7441 | If \a ok is not \nullptr, failure is reported by setting *\a{ok} |
7442 | to \c false, and success by setting *\a{ok} to \c true. |
7443 | |
7444 | If \a base is 0, the C language convention is used: if the string begins |
7445 | with "0x", base 16 is used; otherwise, if the string begins with "0b", base |
7446 | 2 is used; otherwise, if the string begins with "0", base 8 is used; |
7447 | otherwise, base 10 is used. |
7448 | |
7449 | The string conversion will always happen in the 'C' locale. For |
7450 | locale-dependent conversion use QLocale::toLongLong() |
7451 | |
7452 | Example: |
7453 | |
7454 | \snippet qstring/main.cpp 73 |
7455 | |
7456 | This function ignores leading and trailing whitespace. |
7457 | |
7458 | \note Support for the "0b" prefix was added in Qt 6.4. |
7459 | |
7460 | \sa number(), toULong(), toInt(), QLocale::toInt() |
7461 | */ |
7462 | |
7463 | /*! |
7464 | \fn ulong QString::toULong(bool *ok, int base) const |
7465 | |
7466 | Returns the string converted to an \c{unsigned long} using base \a |
7467 | base, which is 10 by default and must be between 2 and 36, or 0. |
7468 | Returns 0 if the conversion fails. |
7469 | |
7470 | If \a ok is not \nullptr, failure is reported by setting *\a{ok} |
7471 | to \c false, and success by setting *\a{ok} to \c true. |
7472 | |
7473 | If \a base is 0, the C language convention is used: if the string begins |
7474 | with "0x", base 16 is used; otherwise, if the string begins with "0b", base |
7475 | 2 is used; otherwise, if the string begins with "0", base 8 is used; |
7476 | otherwise, base 10 is used. |
7477 | |
7478 | The string conversion will always happen in the 'C' locale. For |
7479 | locale-dependent conversion use QLocale::toULongLong() |
7480 | |
7481 | Example: |
7482 | |
7483 | \snippet qstring/main.cpp 78 |
7484 | |
7485 | This function ignores leading and trailing whitespace. |
7486 | |
7487 | \note Support for the "0b" prefix was added in Qt 6.4. |
7488 | |
7489 | \sa number(), QLocale::toUInt() |
7490 | */ |
7491 | |
7492 | /*! |
7493 | \fn int QString::toInt(bool *ok, int base) const |
7494 | Returns the string converted to an \c int using base \a |
7495 | base, which is 10 by default and must be between 2 and 36, or 0. |
7496 | Returns 0 if the conversion fails. |
7497 | |
7498 | If \a ok is not \nullptr, failure is reported by setting *\a{ok} |
7499 | to \c false, and success by setting *\a{ok} to \c true. |
7500 | |
7501 | If \a base is 0, the C language convention is used: if the string begins |
7502 | with "0x", base 16 is used; otherwise, if the string begins with "0b", base |
7503 | 2 is used; otherwise, if the string begins with "0", base 8 is used; |
7504 | otherwise, base 10 is used. |
7505 | |
7506 | The string conversion will always happen in the 'C' locale. For |
7507 | locale-dependent conversion use QLocale::toInt() |
7508 | |
7509 | Example: |
7510 | |
7511 | \snippet qstring/main.cpp 72 |
7512 | |
7513 | This function ignores leading and trailing whitespace. |
7514 | |
7515 | \note Support for the "0b" prefix was added in Qt 6.4. |
7516 | |
7517 | \sa number(), toUInt(), toDouble(), QLocale::toInt() |
7518 | */ |
7519 | |
7520 | /*! |
7521 | \fn uint QString::toUInt(bool *ok, int base) const |
7522 | Returns the string converted to an \c{unsigned int} using base \a |
7523 | base, which is 10 by default and must be between 2 and 36, or 0. |
7524 | Returns 0 if the conversion fails. |
7525 | |
7526 | If \a ok is not \nullptr, failure is reported by setting *\a{ok} |
7527 | to \c false, and success by setting *\a{ok} to \c true. |
7528 | |
7529 | If \a base is 0, the C language convention is used: if the string begins |
7530 | with "0x", base 16 is used; otherwise, if the string begins with "0b", base |
7531 | 2 is used; otherwise, if the string begins with "0", base 8 is used; |
7532 | otherwise, base 10 is used. |
7533 | |
7534 | The string conversion will always happen in the 'C' locale. For |
7535 | locale-dependent conversion use QLocale::toUInt() |
7536 | |
7537 | Example: |
7538 | |
7539 | \snippet qstring/main.cpp 77 |
7540 | |
7541 | This function ignores leading and trailing whitespace. |
7542 | |
7543 | \note Support for the "0b" prefix was added in Qt 6.4. |
7544 | |
7545 | \sa number(), toInt(), QLocale::toUInt() |
7546 | */ |
7547 | |
7548 | /*! |
7549 | \fn short QString::toShort(bool *ok, int base) const |
7550 | |
7551 | Returns the string converted to a \c short using base \a |
7552 | base, which is 10 by default and must be between 2 and 36, or 0. |
7553 | Returns 0 if the conversion fails. |
7554 | |
7555 | If \a ok is not \nullptr, failure is reported by setting *\a{ok} |
7556 | to \c false, and success by setting *\a{ok} to \c true. |
7557 | |
7558 | If \a base is 0, the C language convention is used: if the string begins |
7559 | with "0x", base 16 is used; otherwise, if the string begins with "0b", base |
7560 | 2 is used; otherwise, if the string begins with "0", base 8 is used; |
7561 | otherwise, base 10 is used. |
7562 | |
7563 | The string conversion will always happen in the 'C' locale. For |
7564 | locale-dependent conversion use QLocale::toShort() |
7565 | |
7566 | Example: |
7567 | |
7568 | \snippet qstring/main.cpp 76 |
7569 | |
7570 | This function ignores leading and trailing whitespace. |
7571 | |
7572 | \note Support for the "0b" prefix was added in Qt 6.4. |
7573 | |
7574 | \sa number(), toUShort(), toInt(), QLocale::toShort() |
7575 | */ |
7576 | |
7577 | /*! |
7578 | \fn ushort QString::toUShort(bool *ok, int base) const |
7579 | |
7580 | Returns the string converted to an \c{unsigned short} using base \a |
7581 | base, which is 10 by default and must be between 2 and 36, or 0. |
7582 | Returns 0 if the conversion fails. |
7583 | |
7584 | If \a ok is not \nullptr, failure is reported by setting *\a{ok} |
7585 | to \c false, and success by setting *\a{ok} to \c true. |
7586 | |
7587 | If \a base is 0, the C language convention is used: if the string begins |
7588 | with "0x", base 16 is used; otherwise, if the string begins with "0b", base |
7589 | 2 is used; otherwise, if the string begins with "0", base 8 is used; |
7590 | otherwise, base 10 is used. |
7591 | |
7592 | The string conversion will always happen in the 'C' locale. For |
7593 | locale-dependent conversion use QLocale::toUShort() |
7594 | |
7595 | Example: |
7596 | |
7597 | \snippet qstring/main.cpp 80 |
7598 | |
7599 | This function ignores leading and trailing whitespace. |
7600 | |
7601 | \note Support for the "0b" prefix was added in Qt 6.4. |
7602 | |
7603 | \sa number(), toShort(), QLocale::toUShort() |
7604 | */ |
7605 | |
7606 | /*! |
7607 | Returns the string converted to a \c double value. |
7608 | |
7609 | Returns an infinity if the conversion overflows or 0.0 if the |
7610 | conversion fails for other reasons (e.g. underflow). |
7611 | |
7612 | If \a ok is not \nullptr, failure is reported by setting *\a{ok} |
7613 | to \c false, and success by setting *\a{ok} to \c true. |
7614 | |
7615 | \snippet qstring/main.cpp 66 |
7616 | |
7617 | \warning The QString content may only contain valid numerical characters |
7618 | which includes the plus/minus sign, the character e used in scientific |
7619 | notation, and the decimal point. Including the unit or additional characters |
7620 | leads to a conversion error. |
7621 | |
7622 | \snippet qstring/main.cpp 67 |
7623 | |
7624 | The string conversion will always happen in the 'C' locale. For |
7625 | locale-dependent conversion use QLocale::toDouble() |
7626 | |
7627 | \snippet qstring/main.cpp 68 |
7628 | |
7629 | For historical reasons, this function does not handle |
7630 | thousands group separators. If you need to convert such numbers, |
7631 | use QLocale::toDouble(). |
7632 | |
7633 | \snippet qstring/main.cpp 69 |
7634 | |
7635 | This function ignores leading and trailing whitespace. |
7636 | |
7637 | \sa number(), QLocale::setDefault(), QLocale::toDouble(), trimmed() |
7638 | */ |
7639 | |
7640 | double QString::toDouble(bool *ok) const |
7641 | { |
7642 | return QStringView(*this).toDouble(ok); |
7643 | } |
7644 | |
7645 | double QStringView::toDouble(bool *ok) const |
7646 | { |
7647 | QStringView string = qt_trimmed(s: *this); |
7648 | QVarLengthArray<uchar> latin1(string.size()); |
7649 | qt_to_latin1(dst: latin1.data(), src: string.utf16(), length: string.size()); |
7650 | auto r = qt_asciiToDouble(num: reinterpret_cast<const char *>(latin1.data()), numLen: string.size()); |
7651 | if (ok != nullptr) |
7652 | *ok = r.ok(); |
7653 | return r.result; |
7654 | } |
7655 | |
7656 | /*! |
7657 | Returns the string converted to a \c float value. |
7658 | |
7659 | Returns an infinity if the conversion overflows or 0.0 if the |
7660 | conversion fails for other reasons (e.g. underflow). |
7661 | |
7662 | If \a ok is not \nullptr, failure is reported by setting *\a{ok} |
7663 | to \c false, and success by setting *\a{ok} to \c true. |
7664 | |
7665 | \warning The QString content may only contain valid numerical characters |
7666 | which includes the plus/minus sign, the character e used in scientific |
7667 | notation, and the decimal point. Including the unit or additional characters |
7668 | leads to a conversion error. |
7669 | |
7670 | The string conversion will always happen in the 'C' locale. For |
7671 | locale-dependent conversion use QLocale::toFloat() |
7672 | |
7673 | For historical reasons, this function does not handle |
7674 | thousands group separators. If you need to convert such numbers, |
7675 | use QLocale::toFloat(). |
7676 | |
7677 | Example: |
7678 | |
7679 | \snippet qstring/main.cpp 71 |
7680 | |
7681 | This function ignores leading and trailing whitespace. |
7682 | |
7683 | \sa number(), toDouble(), toInt(), QLocale::toFloat(), trimmed() |
7684 | */ |
7685 | |
7686 | float QString::toFloat(bool *ok) const |
7687 | { |
7688 | return QLocaleData::convertDoubleToFloat(d: toDouble(ok), ok); |
7689 | } |
7690 | |
7691 | float QStringView::toFloat(bool *ok) const |
7692 | { |
7693 | return QLocaleData::convertDoubleToFloat(d: toDouble(ok), ok); |
7694 | } |
7695 | |
7696 | /*! \fn QString &QString::setNum(int n, int base) |
7697 | |
7698 | Sets the string to the printed value of \a n in the specified \a |
7699 | base, and returns a reference to the string. |
7700 | |
7701 | The base is 10 by default and must be between 2 and 36. |
7702 | |
7703 | \snippet qstring/main.cpp 56 |
7704 | |
7705 | The formatting always uses QLocale::C, i.e., English/UnitedStates. |
7706 | To get a localized string representation of a number, use |
7707 | QLocale::toString() with the appropriate locale. |
7708 | |
7709 | \sa number() |
7710 | */ |
7711 | |
7712 | /*! \fn QString &QString::setNum(uint n, int base) |
7713 | |
7714 | \overload |
7715 | */ |
7716 | |
7717 | /*! \fn QString &QString::setNum(long n, int base) |
7718 | |
7719 | \overload |
7720 | */ |
7721 | |
7722 | /*! \fn QString &QString::setNum(ulong n, int base) |
7723 | |
7724 | \overload |
7725 | */ |
7726 | |
7727 | /*! |
7728 | \overload |
7729 | */ |
7730 | QString &QString::setNum(qlonglong n, int base) |
7731 | { |
7732 | return *this = number(n, base); |
7733 | } |
7734 | |
7735 | /*! |
7736 | \overload |
7737 | */ |
7738 | QString &QString::setNum(qulonglong n, int base) |
7739 | { |
7740 | return *this = number(n, base); |
7741 | } |
7742 | |
7743 | /*! \fn QString &QString::setNum(short n, int base) |
7744 | |
7745 | \overload |
7746 | */ |
7747 | |
7748 | /*! \fn QString &QString::setNum(ushort n, int base) |
7749 | |
7750 | \overload |
7751 | */ |
7752 | |
7753 | /*! |
7754 | \overload |
7755 | |
7756 | Sets the string to the printed value of \a n, formatted according to the |
7757 | given \a format and \a precision, and returns a reference to the string. |
7758 | |
7759 | \sa number(), QLocale::FloatingPointPrecisionOption, {Number Formats} |
7760 | */ |
7761 | |
7762 | QString &QString::setNum(double n, char format, int precision) |
7763 | { |
7764 | return *this = number(n, format, precision); |
7765 | } |
7766 | |
7767 | /*! |
7768 | \fn QString &QString::setNum(float n, char format, int precision) |
7769 | \overload |
7770 | |
7771 | Sets the string to the printed value of \a n, formatted according |
7772 | to the given \a format and \a precision, and returns a reference |
7773 | to the string. |
7774 | |
7775 | The formatting always uses QLocale::C, i.e., English/UnitedStates. |
7776 | To get a localized string representation of a number, use |
7777 | QLocale::toString() with the appropriate locale. |
7778 | |
7779 | \sa number() |
7780 | */ |
7781 | |
7782 | |
7783 | /*! |
7784 | \fn QString QString::number(long n, int base) |
7785 | |
7786 | Returns a string equivalent of the number \a n according to the |
7787 | specified \a base. |
7788 | |
7789 | The base is 10 by default and must be between 2 |
7790 | and 36. For bases other than 10, \a n is treated as an |
7791 | unsigned integer. |
7792 | |
7793 | The formatting always uses QLocale::C, i.e., English/UnitedStates. |
7794 | To get a localized string representation of a number, use |
7795 | QLocale::toString() with the appropriate locale. |
7796 | |
7797 | \snippet qstring/main.cpp 35 |
7798 | |
7799 | \sa setNum() |
7800 | */ |
7801 | |
7802 | QString QString::number(long n, int base) |
7803 | { |
7804 | return number(qlonglong(n), base); |
7805 | } |
7806 | |
7807 | /*! |
7808 | \fn QString QString::number(ulong n, int base) |
7809 | |
7810 | \overload |
7811 | */ |
7812 | QString QString::number(ulong n, int base) |
7813 | { |
7814 | return number(qulonglong(n), base); |
7815 | } |
7816 | |
7817 | /*! |
7818 | \overload |
7819 | */ |
7820 | QString QString::number(int n, int base) |
7821 | { |
7822 | return number(qlonglong(n), base); |
7823 | } |
7824 | |
7825 | /*! |
7826 | \overload |
7827 | */ |
7828 | QString QString::number(uint n, int base) |
7829 | { |
7830 | return number(qulonglong(n), base); |
7831 | } |
7832 | |
7833 | /*! |
7834 | \overload |
7835 | */ |
7836 | QString QString::number(qlonglong n, int base) |
7837 | { |
7838 | #if defined(QT_CHECK_RANGE) |
7839 | if (base < 2 || base > 36) { |
7840 | qWarning("QString::setNum: Invalid base (%d)" , base); |
7841 | base = 10; |
7842 | } |
7843 | #endif |
7844 | bool negative = n < 0; |
7845 | /* |
7846 | Negating std::numeric_limits<qlonglong>::min() hits undefined behavior, so |
7847 | taking an absolute value has to take a slight detour. |
7848 | */ |
7849 | return qulltoBasicLatin(l: negative ? 1u + qulonglong(-(n + 1)) : qulonglong(n), base, negative); |
7850 | } |
7851 | |
7852 | /*! |
7853 | \overload |
7854 | */ |
7855 | QString QString::number(qulonglong n, int base) |
7856 | { |
7857 | #if defined(QT_CHECK_RANGE) |
7858 | if (base < 2 || base > 36) { |
7859 | qWarning("QString::setNum: Invalid base (%d)" , base); |
7860 | base = 10; |
7861 | } |
7862 | #endif |
7863 | return qulltoBasicLatin(l: n, base, negative: false); |
7864 | } |
7865 | |
7866 | |
7867 | /*! |
7868 | Returns a string representing the floating-point number \a n. |
7869 | |
7870 | Returns a string that represents \a n, formatted according to the specified |
7871 | \a format and \a precision. |
7872 | |
7873 | For formats with an exponent, the exponent will show its sign and have at |
7874 | least two digits, left-padding the exponent with zero if needed. |
7875 | |
7876 | \sa setNum(), QLocale::toString(), QLocale::FloatingPointPrecisionOption, {Number Formats} |
7877 | */ |
7878 | QString QString::number(double n, char format, int precision) |
7879 | { |
7880 | QLocaleData::DoubleForm form = QLocaleData::DFDecimal; |
7881 | |
7882 | switch (QtMiscUtils::toAsciiLower(ch: format)) { |
7883 | case 'f': |
7884 | form = QLocaleData::DFDecimal; |
7885 | break; |
7886 | case 'e': |
7887 | form = QLocaleData::DFExponent; |
7888 | break; |
7889 | case 'g': |
7890 | form = QLocaleData::DFSignificantDigits; |
7891 | break; |
7892 | default: |
7893 | #if defined(QT_CHECK_RANGE) |
7894 | qWarning("QString::setNum: Invalid format char '%c'" , format); |
7895 | #endif |
7896 | break; |
7897 | } |
7898 | |
7899 | return qdtoBasicLatin(d: n, form, precision, uppercase: isAsciiUpper(c: format)); |
7900 | } |
7901 | |
7902 | namespace { |
7903 | template<class ResultList, class StringSource> |
7904 | static ResultList splitString(const StringSource &source, QStringView sep, |
7905 | Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) |
7906 | { |
7907 | ResultList list; |
7908 | typename StringSource::size_type start = 0; |
7909 | typename StringSource::size_type end; |
7910 | typename StringSource::size_type = 0; |
7911 | while ((end = QtPrivate::findString(QStringView(source.constData(), source.size()), start + extra, sep, cs)) != -1) { |
7912 | if (start != end || behavior == Qt::KeepEmptyParts) |
7913 | list.append(source.sliced(start, end - start)); |
7914 | start = end + sep.size(); |
7915 | extra = (sep.size() == 0 ? 1 : 0); |
7916 | } |
7917 | if (start != source.size() || behavior == Qt::KeepEmptyParts) |
7918 | list.append(source.sliced(start)); |
7919 | return list; |
7920 | } |
7921 | |
7922 | } // namespace |
7923 | |
7924 | /*! |
7925 | Splits the string into substrings wherever \a sep occurs, and |
7926 | returns the list of those strings. If \a sep does not match |
7927 | anywhere in the string, split() returns a single-element list |
7928 | containing this string. |
7929 | |
7930 | \a cs specifies whether \a sep should be matched case |
7931 | sensitively or case insensitively. |
7932 | |
7933 | If \a behavior is Qt::SkipEmptyParts, empty entries don't |
7934 | appear in the result. By default, empty entries are kept. |
7935 | |
7936 | Example: |
7937 | |
7938 | \snippet qstring/main.cpp 62 |
7939 | |
7940 | If \a sep is empty, split() returns an empty string, followed |
7941 | by each of the string's characters, followed by another empty string: |
7942 | |
7943 | \snippet qstring/main.cpp 62-empty |
7944 | |
7945 | To understand this behavior, recall that the empty string matches |
7946 | everywhere, so the above is qualitatively the same as: |
7947 | |
7948 | \snippet qstring/main.cpp 62-slashes |
7949 | |
7950 | \sa QStringList::join(), section() |
7951 | |
7952 | \since 5.14 |
7953 | */ |
7954 | QStringList QString::split(const QString &sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const |
7955 | { |
7956 | return splitString<QStringList>(source: *this, sep, behavior, cs); |
7957 | } |
7958 | |
7959 | /*! |
7960 | \overload |
7961 | \since 5.14 |
7962 | */ |
7963 | QStringList QString::split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const |
7964 | { |
7965 | return splitString<QStringList>(source: *this, sep: QStringView(&sep, 1), behavior, cs); |
7966 | } |
7967 | |
7968 | /*! |
7969 | \fn QList<QStringView> QStringView::split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const |
7970 | \fn QList<QStringView> QStringView::split(QStringView sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const |
7971 | |
7972 | |
7973 | Splits the view into substring views wherever \a sep occurs, and |
7974 | returns the list of those string views. |
7975 | |
7976 | See QString::split() for how \a sep, \a behavior and \a cs interact to form |
7977 | the result. |
7978 | |
7979 | \note All the returned views are valid as long as the data referenced by |
7980 | this string view is valid. Destroying the data will cause all views to |
7981 | become dangling. |
7982 | |
7983 | \since 6.0 |
7984 | */ |
7985 | QList<QStringView> QStringView::split(QStringView sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const |
7986 | { |
7987 | return splitString<QList<QStringView>>(source: QStringView(*this), sep, behavior, cs); |
7988 | } |
7989 | |
7990 | QList<QStringView> QStringView::split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const |
7991 | { |
7992 | return split(sep: QStringView(&sep, 1), behavior, cs); |
7993 | } |
7994 | |
7995 | #if QT_CONFIG(regularexpression) |
7996 | namespace { |
7997 | template<class ResultList, typename String, typename MatchingFunction> |
7998 | static ResultList splitString(const String &source, const QRegularExpression &re, |
7999 | MatchingFunction matchingFunction, |
8000 | Qt::SplitBehavior behavior) |
8001 | { |
8002 | ResultList list; |
8003 | if (!re.isValid()) { |
8004 | qtWarnAboutInvalidRegularExpression(pattern: re.pattern(), where: "QString::split" ); |
8005 | return list; |
8006 | } |
8007 | |
8008 | qsizetype start = 0; |
8009 | qsizetype end = 0; |
8010 | QRegularExpressionMatchIterator iterator = (re.*matchingFunction)(source, 0, QRegularExpression::NormalMatch, QRegularExpression::NoMatchOption); |
8011 | while (iterator.hasNext()) { |
8012 | QRegularExpressionMatch match = iterator.next(); |
8013 | end = match.capturedStart(); |
8014 | if (start != end || behavior == Qt::KeepEmptyParts) |
8015 | list.append(source.sliced(start, end - start)); |
8016 | start = match.capturedEnd(); |
8017 | } |
8018 | |
8019 | if (start != source.size() || behavior == Qt::KeepEmptyParts) |
8020 | list.append(source.sliced(start)); |
8021 | |
8022 | return list; |
8023 | } |
8024 | } // namespace |
8025 | |
8026 | /*! |
8027 | \overload |
8028 | \since 5.14 |
8029 | |
8030 | Splits the string into substrings wherever the regular expression |
8031 | \a re matches, and returns the list of those strings. If \a re |
8032 | does not match anywhere in the string, split() returns a |
8033 | single-element list containing this string. |
8034 | |
8035 | Here is an example where we extract the words in a sentence |
8036 | using one or more whitespace characters as the separator: |
8037 | |
8038 | \snippet qstring/main.cpp 90 |
8039 | |
8040 | Here is a similar example, but this time we use any sequence of |
8041 | non-word characters as the separator: |
8042 | |
8043 | \snippet qstring/main.cpp 91 |
8044 | |
8045 | Here is a third example where we use a zero-length assertion, |
8046 | \b{\\b} (word boundary), to split the string into an |
8047 | alternating sequence of non-word and word tokens: |
8048 | |
8049 | \snippet qstring/main.cpp 92 |
8050 | |
8051 | \sa QStringList::join(), section() |
8052 | */ |
8053 | QStringList QString::split(const QRegularExpression &re, Qt::SplitBehavior behavior) const |
8054 | { |
8055 | #if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) |
8056 | const auto matchingFunction = qOverload<const QString &, qsizetype, QRegularExpression::MatchType, QRegularExpression::MatchOptions>(&QRegularExpression::globalMatch); |
8057 | #else |
8058 | const auto matchingFunction = &QRegularExpression::globalMatch; |
8059 | #endif |
8060 | return splitString<QStringList>(source: *this, |
8061 | re, |
8062 | matchingFunction, |
8063 | behavior); |
8064 | } |
8065 | |
8066 | /*! |
8067 | \since 6.0 |
8068 | |
8069 | Splits the string into substring views wherever the regular expression \a re |
8070 | matches, and returns the list of those strings. If \a re does not match |
8071 | anywhere in the string, split() returns a single-element list containing |
8072 | this string as view. |
8073 | |
8074 | \note The views in the returned list are sub-views of this view; as such, |
8075 | they reference the same data as it and only remain valid for as long as that |
8076 | data remains live. |
8077 | */ |
8078 | QList<QStringView> QStringView::split(const QRegularExpression &re, Qt::SplitBehavior behavior) const |
8079 | { |
8080 | return splitString<QList<QStringView>>(source: *this, re, matchingFunction: &QRegularExpression::globalMatchView, behavior); |
8081 | } |
8082 | |
8083 | #endif // QT_CONFIG(regularexpression) |
8084 | |
8085 | /*! |
8086 | \enum QString::NormalizationForm |
8087 | |
8088 | This enum describes the various normalized forms of Unicode text. |
8089 | |
8090 | \value NormalizationForm_D Canonical Decomposition |
8091 | \value NormalizationForm_C Canonical Decomposition followed by Canonical Composition |
8092 | \value NormalizationForm_KD Compatibility Decomposition |
8093 | \value NormalizationForm_KC Compatibility Decomposition followed by Canonical Composition |
8094 | |
8095 | \sa normalized(), |
8096 | {https://www.unicode.org/reports/tr15/}{Unicode Standard Annex #15} |
8097 | */ |
8098 | |
8099 | /*! |
8100 | \since 4.5 |
8101 | |
8102 | Returns a copy of this string repeated the specified number of \a times. |
8103 | |
8104 | If \a times is less than 1, an empty string is returned. |
8105 | |
8106 | Example: |
8107 | |
8108 | \snippet code/src_corelib_text_qstring.cpp 8 |
8109 | */ |
8110 | QString QString::repeated(qsizetype times) const |
8111 | { |
8112 | if (d.size == 0) |
8113 | return *this; |
8114 | |
8115 | if (times <= 1) { |
8116 | if (times == 1) |
8117 | return *this; |
8118 | return QString(); |
8119 | } |
8120 | |
8121 | const qsizetype resultSize = times * d.size; |
8122 | |
8123 | QString result; |
8124 | result.reserve(asize: resultSize); |
8125 | if (result.capacity() != resultSize) |
8126 | return QString(); // not enough memory |
8127 | |
8128 | memcpy(dest: result.d.data(), src: d.data(), n: d.size * sizeof(QChar)); |
8129 | |
8130 | qsizetype sizeSoFar = d.size; |
8131 | char16_t *end = result.d.data() + sizeSoFar; |
8132 | |
8133 | const qsizetype halfResultSize = resultSize >> 1; |
8134 | while (sizeSoFar <= halfResultSize) { |
8135 | memcpy(dest: end, src: result.d.data(), n: sizeSoFar * sizeof(QChar)); |
8136 | end += sizeSoFar; |
8137 | sizeSoFar <<= 1; |
8138 | } |
8139 | memcpy(dest: end, src: result.d.data(), n: (resultSize - sizeSoFar) * sizeof(QChar)); |
8140 | result.d.data()[resultSize] = '\0'; |
8141 | result.d.size = resultSize; |
8142 | return result; |
8143 | } |
8144 | |
8145 | void qt_string_normalize(QString *data, QString::NormalizationForm mode, QChar::UnicodeVersion version, qsizetype from) |
8146 | { |
8147 | { |
8148 | // check if it's fully ASCII first, because then we have no work |
8149 | auto start = reinterpret_cast<const char16_t *>(data->constData()); |
8150 | const char16_t *p = start + from; |
8151 | if (isAscii_helper(ptr&: p, end: p + data->size() - from)) |
8152 | return; |
8153 | if (p > start + from) |
8154 | from = p - start - 1; // need one before the non-ASCII to perform NFC |
8155 | } |
8156 | |
8157 | if (version == QChar::Unicode_Unassigned) { |
8158 | version = QChar::currentUnicodeVersion(); |
8159 | } else if (int(version) <= NormalizationCorrectionsVersionMax) { |
8160 | const QString &s = *data; |
8161 | QChar *d = nullptr; |
8162 | for (const NormalizationCorrection &n : uc_normalization_corrections) { |
8163 | if (n.version > version) { |
8164 | qsizetype pos = from; |
8165 | if (QChar::requiresSurrogates(ucs4: n.ucs4)) { |
8166 | char16_t ucs4High = QChar::highSurrogate(ucs4: n.ucs4); |
8167 | char16_t ucs4Low = QChar::lowSurrogate(ucs4: n.ucs4); |
8168 | char16_t oldHigh = QChar::highSurrogate(ucs4: n.old_mapping); |
8169 | char16_t oldLow = QChar::lowSurrogate(ucs4: n.old_mapping); |
8170 | while (pos < s.size() - 1) { |
8171 | if (s.at(i: pos).unicode() == ucs4High && s.at(i: pos + 1).unicode() == ucs4Low) { |
8172 | if (!d) |
8173 | d = data->data(); |
8174 | d[pos] = QChar(oldHigh); |
8175 | d[++pos] = QChar(oldLow); |
8176 | } |
8177 | ++pos; |
8178 | } |
8179 | } else { |
8180 | while (pos < s.size()) { |
8181 | if (s.at(i: pos).unicode() == n.ucs4) { |
8182 | if (!d) |
8183 | d = data->data(); |
8184 | d[pos] = QChar(n.old_mapping); |
8185 | } |
8186 | ++pos; |
8187 | } |
8188 | } |
8189 | } |
8190 | } |
8191 | } |
8192 | |
8193 | if (normalizationQuickCheckHelper(str: data, mode, from, lastStable: &from)) |
8194 | return; |
8195 | |
8196 | decomposeHelper(str: data, canonical: mode < QString::NormalizationForm_KD, version, from); |
8197 | |
8198 | canonicalOrderHelper(str: data, version, from); |
8199 | |
8200 | if (mode == QString::NormalizationForm_D || mode == QString::NormalizationForm_KD) |
8201 | return; |
8202 | |
8203 | composeHelper(str: data, version, from); |
8204 | } |
8205 | |
8206 | /*! |
8207 | Returns the string in the given Unicode normalization \a mode, |
8208 | according to the given \a version of the Unicode standard. |
8209 | */ |
8210 | QString QString::normalized(QString::NormalizationForm mode, QChar::UnicodeVersion version) const |
8211 | { |
8212 | QString copy = *this; |
8213 | qt_string_normalize(data: ©, mode, version, from: 0); |
8214 | return copy; |
8215 | } |
8216 | |
8217 | #if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) |
8218 | static void checkArgEscape(QStringView s) |
8219 | { |
8220 | // If we're in here, it means that qArgDigitValue has accepted the |
8221 | // digit. We can skip the check in case we already know it will |
8222 | // succeed. |
8223 | if (!supportUnicodeDigitValuesInArg()) |
8224 | return; |
8225 | |
8226 | const auto isNonAsciiDigit = [](QChar c) { |
8227 | return c.unicode() < u'0' || c.unicode() > u'9'; |
8228 | }; |
8229 | |
8230 | if (std::any_of(first: s.begin(), last: s.end(), pred: isNonAsciiDigit)) { |
8231 | const auto accumulateDigit = [](int partial, QChar digit) { |
8232 | return partial * 10 + digit.digitValue(); |
8233 | }; |
8234 | const int parsedNumber = std::accumulate(first: s.begin(), last: s.end(), init: 0, binary_op: accumulateDigit); |
8235 | |
8236 | qWarning(msg: "QString::arg(): the replacement \"%%%ls\" contains non-ASCII digits;\n" |
8237 | " it is currently being interpreted as the %d-th substitution.\n" |
8238 | " This is deprecated; support for non-ASCII digits will be dropped\n" |
8239 | " in a future version of Qt." , |
8240 | qUtf16Printable(s.toString()), |
8241 | parsedNumber); |
8242 | } |
8243 | } |
8244 | #endif |
8245 | |
8246 | struct ArgEscapeData |
8247 | { |
8248 | int min_escape; // lowest escape sequence number |
8249 | qsizetype occurrences; // number of occurrences of the lowest escape sequence number |
8250 | qsizetype locale_occurrences; // number of occurrences of the lowest escape sequence number that |
8251 | // contain 'L' |
8252 | qsizetype escape_len; // total length of escape sequences which will be replaced |
8253 | }; |
8254 | |
8255 | static ArgEscapeData findArgEscapes(QStringView s) |
8256 | { |
8257 | const QChar *uc_begin = s.begin(); |
8258 | const QChar *uc_end = s.end(); |
8259 | |
8260 | ArgEscapeData d; |
8261 | |
8262 | d.min_escape = INT_MAX; |
8263 | d.occurrences = 0; |
8264 | d.escape_len = 0; |
8265 | d.locale_occurrences = 0; |
8266 | |
8267 | const QChar *c = uc_begin; |
8268 | while (c != uc_end) { |
8269 | while (c != uc_end && c->unicode() != '%') |
8270 | ++c; |
8271 | |
8272 | if (c == uc_end) |
8273 | break; |
8274 | const QChar *escape_start = c; |
8275 | if (++c == uc_end) |
8276 | break; |
8277 | |
8278 | bool locale_arg = false; |
8279 | if (c->unicode() == 'L') { |
8280 | locale_arg = true; |
8281 | if (++c == uc_end) |
8282 | break; |
8283 | } |
8284 | |
8285 | int escape = qArgDigitValue(ch: *c); |
8286 | if (escape == -1) |
8287 | continue; |
8288 | |
8289 | // ### Qt 7: do not allow anything but ASCII digits |
8290 | // in arg()'s replacements. |
8291 | #if QT_VERSION <= QT_VERSION_CHECK(7, 0, 0) |
8292 | const QChar *escapeBegin = c; |
8293 | const QChar *escapeEnd = escapeBegin + 1; |
8294 | #endif |
8295 | |
8296 | ++c; |
8297 | |
8298 | if (c != uc_end) { |
8299 | const int next_escape = qArgDigitValue(ch: *c); |
8300 | if (next_escape != -1) { |
8301 | escape = (10 * escape) + next_escape; |
8302 | ++c; |
8303 | #if QT_VERSION <= QT_VERSION_CHECK(7, 0, 0) |
8304 | ++escapeEnd; |
8305 | #endif |
8306 | } |
8307 | } |
8308 | |
8309 | #if QT_VERSION <= QT_VERSION_CHECK(7, 0, 0) |
8310 | checkArgEscape(s: QStringView(escapeBegin, escapeEnd)); |
8311 | #endif |
8312 | |
8313 | if (escape > d.min_escape) |
8314 | continue; |
8315 | |
8316 | if (escape < d.min_escape) { |
8317 | d.min_escape = escape; |
8318 | d.occurrences = 0; |
8319 | d.escape_len = 0; |
8320 | d.locale_occurrences = 0; |
8321 | } |
8322 | |
8323 | ++d.occurrences; |
8324 | if (locale_arg) |
8325 | ++d.locale_occurrences; |
8326 | d.escape_len += c - escape_start; |
8327 | } |
8328 | return d; |
8329 | } |
8330 | |
8331 | static QString replaceArgEscapes(QStringView s, const ArgEscapeData &d, qsizetype field_width, |
8332 | QStringView arg, QStringView larg, QChar fillChar) |
8333 | { |
8334 | // Negative field-width for right-padding, positive for left-padding: |
8335 | const qsizetype abs_field_width = qAbs(t: field_width); |
8336 | const qsizetype result_len = |
8337 | s.size() - d.escape_len |
8338 | + (d.occurrences - d.locale_occurrences) * qMax(a: abs_field_width, b: arg.size()) |
8339 | + d.locale_occurrences * qMax(a: abs_field_width, b: larg.size()); |
8340 | |
8341 | QString result(result_len, Qt::Uninitialized); |
8342 | QChar *rc = const_cast<QChar *>(result.unicode()); |
8343 | QChar *const result_end = rc + result_len; |
8344 | qsizetype repl_cnt = 0; |
8345 | |
8346 | const QChar *c = s.begin(); |
8347 | const QChar *const uc_end = s.end(); |
8348 | while (c != uc_end) { |
8349 | Q_ASSERT(d.occurrences > repl_cnt); |
8350 | /* We don't have to check increments of c against uc_end because, as |
8351 | long as d.occurrences > repl_cnt, we KNOW there are valid escape |
8352 | sequences remaining. */ |
8353 | |
8354 | const QChar *text_start = c; |
8355 | while (c->unicode() != '%') |
8356 | ++c; |
8357 | |
8358 | const QChar *escape_start = c++; |
8359 | const bool localize = c->unicode() == 'L'; |
8360 | if (localize) |
8361 | ++c; |
8362 | |
8363 | int escape = qArgDigitValue(ch: *c); |
8364 | if (escape != -1 && c + 1 != uc_end) { |
8365 | const int digit = qArgDigitValue(ch: c[1]); |
8366 | if (digit != -1) { |
8367 | ++c; |
8368 | escape = 10 * escape + digit; |
8369 | } |
8370 | } |
8371 | |
8372 | if (escape != d.min_escape) { |
8373 | memcpy(dest: rc, src: text_start, n: (c - text_start) * sizeof(QChar)); |
8374 | rc += c - text_start; |
8375 | } else { |
8376 | ++c; |
8377 | |
8378 | memcpy(dest: rc, src: text_start, n: (escape_start - text_start) * sizeof(QChar)); |
8379 | rc += escape_start - text_start; |
8380 | |
8381 | const QStringView use = localize ? larg : arg; |
8382 | const qsizetype pad_chars = abs_field_width - use.size(); |
8383 | // (If negative, relevant loops are no-ops: no need to check.) |
8384 | |
8385 | if (field_width > 0) { // left padded |
8386 | rc = std::fill_n(first: rc, n: pad_chars, value: fillChar); |
8387 | } |
8388 | |
8389 | memcpy(dest: rc, src: use.data(), n: use.size() * sizeof(QChar)); |
8390 | rc += use.size(); |
8391 | |
8392 | if (field_width < 0) { // right padded |
8393 | rc = std::fill_n(first: rc, n: pad_chars, value: fillChar); |
8394 | } |
8395 | |
8396 | if (++repl_cnt == d.occurrences) { |
8397 | memcpy(dest: rc, src: c, n: (uc_end - c) * sizeof(QChar)); |
8398 | rc += uc_end - c; |
8399 | Q_ASSERT(rc == result_end); |
8400 | c = uc_end; |
8401 | } |
8402 | } |
8403 | } |
8404 | Q_ASSERT(rc == result_end); |
8405 | |
8406 | return result; |
8407 | } |
8408 | |
8409 | /*! |
8410 | Returns a copy of this string with the lowest numbered place marker |
8411 | replaced by string \a a, i.e., \c %1, \c %2, ..., \c %99. |
8412 | |
8413 | \a fieldWidth specifies the minimum amount of space that argument \a |
8414 | a shall occupy. If \a a requires less space than \a fieldWidth, it |
8415 | is padded to \a fieldWidth with character \a fillChar. A positive |
8416 | \a fieldWidth produces right-aligned text. A negative \a fieldWidth |
8417 | produces left-aligned text. |
8418 | |
8419 | This example shows how we might create a \c status string for |
8420 | reporting progress while processing a list of files: |
8421 | |
8422 | \snippet qstring/main.cpp 11 |
8423 | |
8424 | First, \c arg(i) replaces \c %1. Then \c arg(total) replaces \c |
8425 | %2. Finally, \c arg(fileName) replaces \c %3. |
8426 | |
8427 | One advantage of using arg() over asprintf() is that the order of the |
8428 | numbered place markers can change, if the application's strings are |
8429 | translated into other languages, but each arg() will still replace |
8430 | the lowest numbered unreplaced place marker, no matter where it |
8431 | appears. Also, if place marker \c %i appears more than once in the |
8432 | string, the arg() replaces all of them. |
8433 | |
8434 | If there is no unreplaced place marker remaining, a warning message |
8435 | is output and the result is undefined. Place marker numbers must be |
8436 | in the range 1 to 99. |
8437 | */ |
8438 | QString QString::arg(const QString &a, int fieldWidth, QChar fillChar) const |
8439 | { |
8440 | return arg(a: qToStringViewIgnoringNull(s: a), fieldWidth, fillChar); |
8441 | } |
8442 | |
8443 | /*! |
8444 | \overload |
8445 | \since 5.10 |
8446 | |
8447 | Returns a copy of this string with the lowest-numbered place-marker |
8448 | replaced by string \a a, i.e., \c %1, \c %2, ..., \c %99. |
8449 | |
8450 | \a fieldWidth specifies the minimum amount of space that \a a |
8451 | shall occupy. If \a a requires less space than \a fieldWidth, it |
8452 | is padded to \a fieldWidth with character \a fillChar. A positive |
8453 | \a fieldWidth produces right-aligned text. A negative \a fieldWidth |
8454 | produces left-aligned text. |
8455 | |
8456 | This example shows how we might create a \c status string for |
8457 | reporting progress while processing a list of files: |
8458 | |
8459 | \snippet qstring/main.cpp 11-qstringview |
8460 | |
8461 | First, \c arg(i) replaces \c %1. Then \c arg(total) replaces \c |
8462 | %2. Finally, \c arg(fileName) replaces \c %3. |
8463 | |
8464 | One advantage of using arg() over asprintf() is that the order of the |
8465 | numbered place markers can change, if the application's strings are |
8466 | translated into other languages, but each arg() will still replace |
8467 | the lowest-numbered unreplaced place-marker, no matter where it |
8468 | appears. Also, if place-marker \c %i appears more than once in the |
8469 | string, arg() replaces all of them. |
8470 | |
8471 | If there is no unreplaced place-marker remaining, a warning message |
8472 | is printed and the result is undefined. Place-marker numbers must be |
8473 | in the range 1 to 99. |
8474 | */ |
8475 | QString QString::arg(QStringView a, int fieldWidth, QChar fillChar) const |
8476 | { |
8477 | ArgEscapeData d = findArgEscapes(s: *this); |
8478 | |
8479 | if (Q_UNLIKELY(d.occurrences == 0)) { |
8480 | qWarning(msg: "QString::arg: Argument missing: %ls, %ls" , qUtf16Printable(*this), |
8481 | qUtf16Printable(a.toString())); |
8482 | return *this; |
8483 | } |
8484 | return replaceArgEscapes(s: *this, d, field_width: fieldWidth, arg: a, larg: a, fillChar); |
8485 | } |
8486 | |
8487 | /*! |
8488 | \overload |
8489 | \since 5.10 |
8490 | |
8491 | Returns a copy of this string with the lowest-numbered place-marker |
8492 | replaced by the Latin-1 string viewed by \a a, i.e., \c %1, \c %2, ..., \c %99. |
8493 | |
8494 | \a fieldWidth specifies the minimum amount of space that \a a |
8495 | shall occupy. If \a a requires less space than \a fieldWidth, it |
8496 | is padded to \a fieldWidth with character \a fillChar. A positive |
8497 | \a fieldWidth produces right-aligned text. A negative \a fieldWidth |
8498 | produces left-aligned text. |
8499 | |
8500 | One advantage of using arg() over asprintf() is that the order of the |
8501 | numbered place markers can change, if the application's strings are |
8502 | translated into other languages, but each arg() will still replace |
8503 | the lowest-numbered unreplaced place-marker, no matter where it |
8504 | appears. Also, if place-marker \c %i appears more than once in the |
8505 | string, arg() replaces all of them. |
8506 | |
8507 | If there is no unreplaced place-marker remaining, a warning message |
8508 | is printed and the result is undefined. Place-marker numbers must be |
8509 | in the range 1 to 99. |
8510 | */ |
8511 | QString QString::arg(QLatin1StringView a, int fieldWidth, QChar fillChar) const |
8512 | { |
8513 | QVarLengthArray<char16_t> utf16 = qt_from_latin1_to_qvla(str: a); |
8514 | return arg(a: QStringView(utf16.data(), utf16.size()), fieldWidth, fillChar); |
8515 | } |
8516 | |
8517 | /*! \fn QString QString::arg(int a, int fieldWidth, int base, QChar fillChar) const |
8518 | \overload arg() |
8519 | |
8520 | The \a a argument is expressed in base \a base, which is 10 by |
8521 | default and must be between 2 and 36. For bases other than 10, \a a |
8522 | is treated as an unsigned integer. |
8523 | |
8524 | \a fieldWidth specifies the minimum amount of space that \a a is |
8525 | padded to and filled with the character \a fillChar. A positive |
8526 | value produces right-aligned text; a negative value produces |
8527 | left-aligned text. |
8528 | |
8529 | The '%' can be followed by an 'L', in which case the sequence is |
8530 | replaced with a localized representation of \a a. The conversion |
8531 | uses the default locale, set by QLocale::setDefault(). If no default |
8532 | locale was specified, the system locale is used. The 'L' flag is |
8533 | ignored if \a base is not 10. |
8534 | |
8535 | \snippet qstring/main.cpp 12 |
8536 | \snippet qstring/main.cpp 14 |
8537 | |
8538 | \sa {Number Formats} |
8539 | */ |
8540 | |
8541 | /*! \fn QString QString::arg(uint a, int fieldWidth, int base, QChar fillChar) const |
8542 | \overload arg() |
8543 | |
8544 | The \a base argument specifies the base to use when converting the |
8545 | integer \a a into a string. The base must be between 2 and 36. |
8546 | |
8547 | \sa {Number Formats} |
8548 | */ |
8549 | |
8550 | /*! \fn QString QString::arg(long a, int fieldWidth, int base, QChar fillChar) const |
8551 | \overload arg() |
8552 | |
8553 | \a fieldWidth specifies the minimum amount of space that \a a is |
8554 | padded to and filled with the character \a fillChar. A positive |
8555 | value produces right-aligned text; a negative value produces |
8556 | left-aligned text. |
8557 | |
8558 | The \a a argument is expressed in the given \a base, which is 10 by |
8559 | default and must be between 2 and 36. |
8560 | |
8561 | The '%' can be followed by an 'L', in which case the sequence is |
8562 | replaced with a localized representation of \a a. The conversion |
8563 | uses the default locale. The default locale is determined from the |
8564 | system's locale settings at application startup. It can be changed |
8565 | using QLocale::setDefault(). The 'L' flag is ignored if \a base is |
8566 | not 10. |
8567 | |
8568 | \snippet qstring/main.cpp 12 |
8569 | \snippet qstring/main.cpp 14 |
8570 | |
8571 | \sa {Number Formats} |
8572 | */ |
8573 | |
8574 | /*! |
8575 | \fn QString QString::arg(ulong a, int fieldWidth, int base, QChar fillChar) const |
8576 | \overload arg() |
8577 | |
8578 | \a fieldWidth specifies the minimum amount of space that \a a is |
8579 | padded to and filled with the character \a fillChar. A positive |
8580 | value produces right-aligned text; a negative value produces |
8581 | left-aligned text. |
8582 | |
8583 | The \a base argument specifies the base to use when converting the |
8584 | integer \a a to a string. The base must be between 2 and 36, with 8 |
8585 | giving octal, 10 decimal, and 16 hexadecimal numbers. |
8586 | |
8587 | \sa {Number Formats} |
8588 | */ |
8589 | |
8590 | /*! |
8591 | \overload arg() |
8592 | |
8593 | \a fieldWidth specifies the minimum amount of space that \a a is |
8594 | padded to and filled with the character \a fillChar. A positive |
8595 | value produces right-aligned text; a negative value produces |
8596 | left-aligned text. |
8597 | |
8598 | The \a base argument specifies the base to use when converting the |
8599 | integer \a a into a string. The base must be between 2 and 36, with |
8600 | 8 giving octal, 10 decimal, and 16 hexadecimal numbers. |
8601 | |
8602 | \sa {Number Formats} |
8603 | */ |
8604 | QString QString::arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const |
8605 | { |
8606 | ArgEscapeData d = findArgEscapes(s: *this); |
8607 | |
8608 | if (d.occurrences == 0) { |
8609 | qWarning() << "QString::arg: Argument missing:" << *this << ',' << a; |
8610 | return *this; |
8611 | } |
8612 | |
8613 | unsigned flags = QLocaleData::NoFlags; |
8614 | // ZeroPadded sorts out left-padding when the fill is zero, to the right of sign: |
8615 | if (fillChar == u'0') |
8616 | flags = QLocaleData::ZeroPadded; |
8617 | |
8618 | QString arg; |
8619 | if (d.occurrences > d.locale_occurrences) { |
8620 | arg = QLocaleData::c()->longLongToString(l: a, precision: -1, base, width: fieldWidth, flags); |
8621 | Q_ASSERT(fillChar != u'0' || !qIsFinite(a) |
8622 | || fieldWidth <= arg.size()); |
8623 | } |
8624 | |
8625 | QString localeArg; |
8626 | if (d.locale_occurrences > 0) { |
8627 | QLocale locale; |
8628 | if (!(locale.numberOptions() & QLocale::OmitGroupSeparator)) |
8629 | flags |= QLocaleData::GroupDigits; |
8630 | localeArg = locale.d->m_data->longLongToString(l: a, precision: -1, base, width: fieldWidth, flags); |
8631 | Q_ASSERT(fillChar != u'0' || !qIsFinite(a) |
8632 | || fieldWidth <= localeArg.size()); |
8633 | } |
8634 | |
8635 | return replaceArgEscapes(s: *this, d, field_width: fieldWidth, arg, larg: localeArg, fillChar); |
8636 | } |
8637 | |
8638 | /*! |
8639 | \overload arg() |
8640 | |
8641 | \a fieldWidth specifies the minimum amount of space that \a a is |
8642 | padded to and filled with the character \a fillChar. A positive |
8643 | value produces right-aligned text; a negative value produces |
8644 | left-aligned text. |
8645 | |
8646 | The \a base argument specifies the base to use when converting the |
8647 | integer \a a into a string. \a base must be between 2 and 36, with 8 |
8648 | giving octal, 10 decimal, and 16 hexadecimal numbers. |
8649 | |
8650 | \sa {Number Formats} |
8651 | */ |
8652 | QString QString::arg(qulonglong a, int fieldWidth, int base, QChar fillChar) const |
8653 | { |
8654 | ArgEscapeData d = findArgEscapes(s: *this); |
8655 | |
8656 | if (d.occurrences == 0) { |
8657 | qWarning() << "QString::arg: Argument missing:" << *this << ',' << a; |
8658 | return *this; |
8659 | } |
8660 | |
8661 | unsigned flags = QLocaleData::NoFlags; |
8662 | // ZeroPadded sorts out left-padding when the fill is zero, to the right of sign: |
8663 | if (fillChar == u'0') |
8664 | flags = QLocaleData::ZeroPadded; |
8665 | |
8666 | QString arg; |
8667 | if (d.occurrences > d.locale_occurrences) { |
8668 | arg = QLocaleData::c()->unsLongLongToString(l: a, precision: -1, base, width: fieldWidth, flags); |
8669 | Q_ASSERT(fillChar != u'0' || !qIsFinite(a) |
8670 | || fieldWidth <= arg.size()); |
8671 | } |
8672 | |
8673 | QString localeArg; |
8674 | if (d.locale_occurrences > 0) { |
8675 | QLocale locale; |
8676 | if (!(locale.numberOptions() & QLocale::OmitGroupSeparator)) |
8677 | flags |= QLocaleData::GroupDigits; |
8678 | localeArg = locale.d->m_data->unsLongLongToString(l: a, precision: -1, base, width: fieldWidth, flags); |
8679 | Q_ASSERT(fillChar != u'0' || !qIsFinite(a) |
8680 | || fieldWidth <= localeArg.size()); |
8681 | } |
8682 | |
8683 | return replaceArgEscapes(s: *this, d, field_width: fieldWidth, arg, larg: localeArg, fillChar); |
8684 | } |
8685 | |
8686 | /*! |
8687 | \overload arg() |
8688 | |
8689 | \fn QString QString::arg(short a, int fieldWidth, int base, QChar fillChar) const |
8690 | |
8691 | \a fieldWidth specifies the minimum amount of space that \a a is |
8692 | padded to and filled with the character \a fillChar. A positive |
8693 | value produces right-aligned text; a negative value produces |
8694 | left-aligned text. |
8695 | |
8696 | The \a base argument specifies the base to use when converting the |
8697 | integer \a a into a string. The base must be between 2 and 36, with |
8698 | 8 giving octal, 10 decimal, and 16 hexadecimal numbers. |
8699 | |
8700 | \sa {Number Formats} |
8701 | */ |
8702 | |
8703 | /*! |
8704 | \fn QString QString::arg(ushort a, int fieldWidth, int base, QChar fillChar) const |
8705 | \overload arg() |
8706 | |
8707 | \a fieldWidth specifies the minimum amount of space that \a a is |
8708 | padded to and filled with the character \a fillChar. A positive |
8709 | value produces right-aligned text; a negative value produces |
8710 | left-aligned text. |
8711 | |
8712 | The \a base argument specifies the base to use when converting the |
8713 | integer \a a into a string. The base must be between 2 and 36, with |
8714 | 8 giving octal, 10 decimal, and 16 hexadecimal numbers. |
8715 | |
8716 | \sa {Number Formats} |
8717 | */ |
8718 | |
8719 | /*! |
8720 | \overload arg() |
8721 | */ |
8722 | QString QString::arg(QChar a, int fieldWidth, QChar fillChar) const |
8723 | { |
8724 | return arg(a: QStringView{&a, 1}, fieldWidth, fillChar); |
8725 | } |
8726 | |
8727 | /*! |
8728 | \overload arg() |
8729 | |
8730 | The \a a argument is interpreted as a Latin-1 character. |
8731 | */ |
8732 | QString QString::arg(char a, int fieldWidth, QChar fillChar) const |
8733 | { |
8734 | return arg(a: QLatin1Char(a), fieldWidth, fillChar); |
8735 | } |
8736 | |
8737 | /*! |
8738 | \overload arg() |
8739 | |
8740 | Argument \a a is formatted according to the specified \a format and |
8741 | \a precision. See \l{Floating-point Formats} for details. |
8742 | |
8743 | \a fieldWidth specifies the minimum amount of space that \a a is |
8744 | padded to and filled with the character \a fillChar. A positive |
8745 | value produces right-aligned text; a negative value produces |
8746 | left-aligned text. |
8747 | |
8748 | \snippet code/src_corelib_text_qstring.cpp 2 |
8749 | |
8750 | \sa QLocale::toString(), QLocale::FloatingPointPrecisionOption, {Number Formats} |
8751 | */ |
8752 | QString QString::arg(double a, int fieldWidth, char format, int precision, QChar fillChar) const |
8753 | { |
8754 | ArgEscapeData d = findArgEscapes(s: *this); |
8755 | |
8756 | if (d.occurrences == 0) { |
8757 | qWarning(msg: "QString::arg: Argument missing: %s, %g" , toLocal8Bit().data(), a); |
8758 | return *this; |
8759 | } |
8760 | |
8761 | unsigned flags = QLocaleData::NoFlags; |
8762 | // ZeroPadded sorts out left-padding when the fill is zero, to the right of sign: |
8763 | if (fillChar == u'0') |
8764 | flags |= QLocaleData::ZeroPadded; |
8765 | |
8766 | if (isAsciiUpper(c: format)) |
8767 | flags |= QLocaleData::CapitalEorX; |
8768 | |
8769 | QLocaleData::DoubleForm form = QLocaleData::DFDecimal; |
8770 | switch (QtMiscUtils::toAsciiLower(ch: format)) { |
8771 | case 'f': |
8772 | form = QLocaleData::DFDecimal; |
8773 | break; |
8774 | case 'e': |
8775 | form = QLocaleData::DFExponent; |
8776 | break; |
8777 | case 'g': |
8778 | form = QLocaleData::DFSignificantDigits; |
8779 | break; |
8780 | default: |
8781 | #if defined(QT_CHECK_RANGE) |
8782 | qWarning("QString::arg: Invalid format char '%c'" , format); |
8783 | #endif |
8784 | break; |
8785 | } |
8786 | |
8787 | QString arg; |
8788 | if (d.occurrences > d.locale_occurrences) { |
8789 | arg = QLocaleData::c()->doubleToString(d: a, precision, form, width: fieldWidth, |
8790 | flags: flags | QLocaleData::ZeroPadExponent); |
8791 | Q_ASSERT(fillChar != u'0' || !qIsFinite(a) |
8792 | || fieldWidth <= arg.size()); |
8793 | } |
8794 | |
8795 | QString localeArg; |
8796 | if (d.locale_occurrences > 0) { |
8797 | QLocale locale; |
8798 | |
8799 | const QLocale::NumberOptions numberOptions = locale.numberOptions(); |
8800 | if (!(numberOptions & QLocale::OmitGroupSeparator)) |
8801 | flags |= QLocaleData::GroupDigits; |
8802 | if (!(numberOptions & QLocale::OmitLeadingZeroInExponent)) |
8803 | flags |= QLocaleData::ZeroPadExponent; |
8804 | if (numberOptions & QLocale::IncludeTrailingZeroesAfterDot) |
8805 | flags |= QLocaleData::AddTrailingZeroes; |
8806 | localeArg = locale.d->m_data->doubleToString(d: a, precision, form, width: fieldWidth, flags); |
8807 | Q_ASSERT(fillChar != u'0' || !qIsFinite(a) |
8808 | || fieldWidth <= localeArg.size()); |
8809 | } |
8810 | |
8811 | return replaceArgEscapes(s: *this, d, field_width: fieldWidth, arg, larg: localeArg, fillChar); |
8812 | } |
8813 | |
8814 | static inline char16_t to_unicode(const QChar c) { return c.unicode(); } |
8815 | static inline char16_t to_unicode(const char c) { return QLatin1Char{c}.unicode(); } |
8816 | |
8817 | template <typename Char> |
8818 | static int getEscape(const Char *uc, qsizetype *pos, qsizetype len, int maxNumber = 999) |
8819 | { |
8820 | qsizetype i = *pos; |
8821 | ++i; |
8822 | if (i < len && uc[i] == u'L') |
8823 | ++i; |
8824 | if (i < len) { |
8825 | int escape = to_unicode(uc[i]) - '0'; |
8826 | if (uint(escape) >= 10U) |
8827 | return -1; |
8828 | ++i; |
8829 | while (i < len) { |
8830 | int digit = to_unicode(uc[i]) - '0'; |
8831 | if (uint(digit) >= 10U) |
8832 | break; |
8833 | escape = (escape * 10) + digit; |
8834 | ++i; |
8835 | } |
8836 | if (escape <= maxNumber) { |
8837 | *pos = i; |
8838 | return escape; |
8839 | } |
8840 | } |
8841 | return -1; |
8842 | } |
8843 | |
8844 | /* |
8845 | Algorithm for multiArg: |
8846 | |
8847 | 1. Parse the string as a sequence of verbatim text and placeholders (%L?\d{,3}). |
8848 | The L is parsed and accepted for compatibility with non-multi-arg, but since |
8849 | multiArg only accepts strings as replacements, the localization request can |
8850 | be safely ignored. |
8851 | 2. The result of step (1) is a list of (string-ref,int)-tuples. The string-ref |
8852 | either points at text to be copied verbatim (in which case the int is -1), |
8853 | or, initially, at the textual representation of the placeholder. In that case, |
8854 | the int contains the numerical number as parsed from the placeholder. |
8855 | 3. Next, collect all the non-negative ints found, sort them in ascending order and |
8856 | remove duplicates. |
8857 | 3a. If the result has more entries than multiArg() was given replacement strings, |
8858 | we have found placeholders we can't satisfy with replacement strings. That is |
8859 | fine (there could be another .arg() call coming after this one), so just |
8860 | truncate the result to the number of actual multiArg() replacement strings. |
8861 | 3b. If the result has less entries than multiArg() was given replacement strings, |
8862 | the string is missing placeholders. This is an error that the user should be |
8863 | warned about. |
8864 | 4. The result of step (3) is a mapping from the index of any replacement string to |
8865 | placeholder number. This is the wrong way around, but since placeholder |
8866 | numbers could get as large as 999, while we typically don't have more than 9 |
8867 | replacement strings, we trade 4K of sparsely-used memory for doing a reverse lookup |
8868 | each time we need to map a placeholder number to a replacement string index |
8869 | (that's a linear search; but still *much* faster than using an associative container). |
8870 | 5. Next, for each of the tuples found in step (1), do the following: |
8871 | 5a. If the int is negative, do nothing. |
8872 | 5b. Otherwise, if the int is found in the result of step (3) at index I, replace |
8873 | the string-ref with a string-ref for the (complete) I'th replacement string. |
8874 | 5c. Otherwise, do nothing. |
8875 | 6. Concatenate all string refs into a single result string. |
8876 | */ |
8877 | |
8878 | namespace { |
8879 | struct Part |
8880 | { |
8881 | Part() = default; // for QVarLengthArray; do not use |
8882 | constexpr Part(QStringView s, int num = -1) |
8883 | : tag{QtPrivate::ArgBase::U16}, number{num}, data{s.utf16()}, size{s.size()} {} |
8884 | constexpr Part(QLatin1StringView s, int num = -1) |
8885 | : tag{QtPrivate::ArgBase::L1}, number{num}, data{s.data()}, size{s.size()} {} |
8886 | |
8887 | void reset(QStringView s) noexcept { *this = {s, number}; } |
8888 | void reset(QLatin1StringView s) noexcept { *this = {s, number}; } |
8889 | |
8890 | QtPrivate::ArgBase::Tag tag; |
8891 | int number; |
8892 | const void *data; |
8893 | qsizetype size; |
8894 | }; |
8895 | } // unnamed namespace |
8896 | |
8897 | Q_DECLARE_TYPEINFO(Part, Q_PRIMITIVE_TYPE); |
8898 | |
8899 | namespace { |
8900 | |
8901 | enum { ExpectedParts = 32 }; |
8902 | |
8903 | typedef QVarLengthArray<Part, ExpectedParts> ParseResult; |
8904 | typedef QVarLengthArray<int, ExpectedParts/2> ArgIndexToPlaceholderMap; |
8905 | |
8906 | template <typename StringView> |
8907 | static ParseResult parseMultiArgFormatString(StringView s) |
8908 | { |
8909 | ParseResult result; |
8910 | |
8911 | const auto uc = s.data(); |
8912 | const auto len = s.size(); |
8913 | const auto end = len - 1; |
8914 | qsizetype i = 0; |
8915 | qsizetype last = 0; |
8916 | |
8917 | while (i < end) { |
8918 | if (uc[i] == u'%') { |
8919 | qsizetype percent = i; |
8920 | int number = getEscape(uc, &i, len); |
8921 | if (number != -1) { |
8922 | if (last != percent) |
8923 | result.push_back(t: Part{s.sliced(last, percent - last)}); // literal text (incl. failed placeholders) |
8924 | result.push_back(t: Part{s.sliced(percent, i - percent), number}); // parsed placeholder |
8925 | last = i; |
8926 | continue; |
8927 | } |
8928 | } |
8929 | ++i; |
8930 | } |
8931 | |
8932 | if (last < len) |
8933 | result.push_back(t: Part{s.sliced(last, len - last)}); // trailing literal text |
8934 | |
8935 | return result; |
8936 | } |
8937 | |
8938 | static ArgIndexToPlaceholderMap makeArgIndexToPlaceholderMap(const ParseResult &parts) |
8939 | { |
8940 | ArgIndexToPlaceholderMap result; |
8941 | |
8942 | for (const Part &part : parts) { |
8943 | if (part.number >= 0) |
8944 | result.push_back(t: part.number); |
8945 | } |
8946 | |
8947 | std::sort(first: result.begin(), last: result.end()); |
8948 | result.erase(abegin: std::unique(first: result.begin(), last: result.end()), |
8949 | aend: result.end()); |
8950 | |
8951 | return result; |
8952 | } |
8953 | |
8954 | static qsizetype resolveStringRefsAndReturnTotalSize(ParseResult &parts, const ArgIndexToPlaceholderMap &argIndexToPlaceholderMap, const QtPrivate::ArgBase *args[]) |
8955 | { |
8956 | using namespace QtPrivate; |
8957 | qsizetype totalSize = 0; |
8958 | for (Part &part : parts) { |
8959 | if (part.number != -1) { |
8960 | const auto it = std::find(first: argIndexToPlaceholderMap.begin(), last: argIndexToPlaceholderMap.end(), val: part.number); |
8961 | if (it != argIndexToPlaceholderMap.end()) { |
8962 | const auto &arg = *args[it - argIndexToPlaceholderMap.begin()]; |
8963 | switch (arg.tag) { |
8964 | case ArgBase::L1: |
8965 | part.reset(s: static_cast<const QLatin1StringArg&>(arg).string); |
8966 | break; |
8967 | case ArgBase::U8: |
8968 | Q_UNREACHABLE(); // waiting for QUtf8String... |
8969 | break; |
8970 | case ArgBase::U16: |
8971 | part.reset(s: static_cast<const QStringViewArg&>(arg).string); |
8972 | break; |
8973 | } |
8974 | } |
8975 | } |
8976 | totalSize += part.size; |
8977 | } |
8978 | return totalSize; |
8979 | } |
8980 | |
8981 | } // unnamed namespace |
8982 | |
8983 | Q_ALWAYS_INLINE QString to_string(QLatin1StringView s) noexcept { return s; } |
8984 | Q_ALWAYS_INLINE QString to_string(QStringView s) noexcept { return s.toString(); } |
8985 | |
8986 | template <typename StringView> |
8987 | static QString argToQStringImpl(StringView pattern, size_t numArgs, const QtPrivate::ArgBase **args) |
8988 | { |
8989 | // Step 1-2 above |
8990 | ParseResult parts = parseMultiArgFormatString(pattern); |
8991 | |
8992 | // 3-4 |
8993 | ArgIndexToPlaceholderMap argIndexToPlaceholderMap = makeArgIndexToPlaceholderMap(parts); |
8994 | |
8995 | if (static_cast<size_t>(argIndexToPlaceholderMap.size()) > numArgs) // 3a |
8996 | argIndexToPlaceholderMap.resize(sz: qsizetype(numArgs)); |
8997 | else if (Q_UNLIKELY(static_cast<size_t>(argIndexToPlaceholderMap.size()) < numArgs)) // 3b |
8998 | qWarning(msg: "QString::arg: %d argument(s) missing in %ls" , |
8999 | int(numArgs - argIndexToPlaceholderMap.size()), qUtf16Printable(to_string(pattern))); |
9000 | |
9001 | // 5 |
9002 | const qsizetype totalSize = resolveStringRefsAndReturnTotalSize(parts, argIndexToPlaceholderMap, args); |
9003 | |
9004 | // 6: |
9005 | QString result(totalSize, Qt::Uninitialized); |
9006 | auto out = const_cast<QChar*>(result.constData()); |
9007 | |
9008 | for (const Part &part : parts) { |
9009 | switch (part.tag) { |
9010 | case QtPrivate::ArgBase::L1: |
9011 | if (part.size) { |
9012 | qt_from_latin1(dst: reinterpret_cast<char16_t*>(out), |
9013 | str: reinterpret_cast<const char*>(part.data), size: part.size); |
9014 | } |
9015 | break; |
9016 | case QtPrivate::ArgBase::U8: |
9017 | Q_UNREACHABLE(); // waiting for QUtf8String |
9018 | break; |
9019 | case QtPrivate::ArgBase::U16: |
9020 | if (part.size) |
9021 | memcpy(dest: out, src: part.data, n: part.size * sizeof(QChar)); |
9022 | break; |
9023 | } |
9024 | out += part.size; |
9025 | } |
9026 | |
9027 | return result; |
9028 | } |
9029 | |
9030 | QString QtPrivate::argToQString(QStringView pattern, size_t n, const ArgBase **args) |
9031 | { |
9032 | return argToQStringImpl(pattern, numArgs: n, args); |
9033 | } |
9034 | |
9035 | QString QtPrivate::argToQString(QLatin1StringView pattern, size_t n, const ArgBase **args) |
9036 | { |
9037 | return argToQStringImpl(pattern, numArgs: n, args); |
9038 | } |
9039 | |
9040 | /*! \fn bool QString::isSimpleText() const |
9041 | |
9042 | \internal |
9043 | */ |
9044 | bool QString::isSimpleText() const |
9045 | { |
9046 | const char16_t *p = d.data(); |
9047 | const char16_t * const end = p + d.size; |
9048 | while (p < end) { |
9049 | char16_t uc = *p; |
9050 | // sort out regions of complex text formatting |
9051 | if (uc > 0x058f && (uc < 0x1100 || uc > 0xfb0f)) { |
9052 | return false; |
9053 | } |
9054 | p++; |
9055 | } |
9056 | |
9057 | return true; |
9058 | } |
9059 | |
9060 | /*! \fn bool QString::isRightToLeft() const |
9061 | |
9062 | Returns \c true if the string is read right to left. |
9063 | |
9064 | \sa QStringView::isRightToLeft() |
9065 | */ |
9066 | bool QString::isRightToLeft() const |
9067 | { |
9068 | return QtPrivate::isRightToLeft(string: QStringView(*this)); |
9069 | } |
9070 | |
9071 | /*! |
9072 | \fn bool QString::isValidUtf16() const noexcept |
9073 | \since 5.15 |
9074 | |
9075 | Returns \c true if the string contains valid UTF-16 encoded data, |
9076 | or \c false otherwise. |
9077 | |
9078 | Note that this function does not perform any special validation of the |
9079 | data; it merely checks if it can be successfully decoded from UTF-16. |
9080 | The data is assumed to be in host byte order; the presence of a BOM |
9081 | is meaningless. |
9082 | |
9083 | \sa QStringView::isValidUtf16() |
9084 | */ |
9085 | |
9086 | /*! \fn QChar *QString::data() |
9087 | |
9088 | Returns a pointer to the data stored in the QString. The pointer |
9089 | can be used to access and modify the characters that compose the |
9090 | string. |
9091 | |
9092 | Unlike constData() and unicode(), the returned data is always |
9093 | '\\0'-terminated. |
9094 | |
9095 | Example: |
9096 | |
9097 | \snippet qstring/main.cpp 19 |
9098 | |
9099 | Note that the pointer remains valid only as long as the string is |
9100 | not modified by other means. For read-only access, constData() is |
9101 | faster because it never causes a \l{deep copy} to occur. |
9102 | |
9103 | \sa constData(), operator[]() |
9104 | */ |
9105 | |
9106 | /*! \fn const QChar *QString::data() const |
9107 | |
9108 | \overload |
9109 | |
9110 | \note The returned string may not be '\\0'-terminated. |
9111 | Use size() to determine the length of the array. |
9112 | |
9113 | \sa fromRawData() |
9114 | */ |
9115 | |
9116 | /*! \fn const QChar *QString::constData() const |
9117 | |
9118 | Returns a pointer to the data stored in the QString. The pointer |
9119 | can be used to access the characters that compose the string. |
9120 | |
9121 | Note that the pointer remains valid only as long as the string is |
9122 | not modified. |
9123 | |
9124 | \note The returned string may not be '\\0'-terminated. |
9125 | Use size() to determine the length of the array. |
9126 | |
9127 | \sa data(), operator[](), fromRawData() |
9128 | */ |
9129 | |
9130 | /*! \fn void QString::push_front(const QString &other) |
9131 | |
9132 | This function is provided for STL compatibility, prepending the |
9133 | given \a other string to the beginning of this string. It is |
9134 | equivalent to \c prepend(other). |
9135 | |
9136 | \sa prepend() |
9137 | */ |
9138 | |
9139 | /*! \fn void QString::push_front(QChar ch) |
9140 | |
9141 | \overload |
9142 | |
9143 | Prepends the given \a ch character to the beginning of this string. |
9144 | */ |
9145 | |
9146 | /*! \fn void QString::push_back(const QString &other) |
9147 | |
9148 | This function is provided for STL compatibility, appending the |
9149 | given \a other string onto the end of this string. It is |
9150 | equivalent to \c append(other). |
9151 | |
9152 | \sa append() |
9153 | */ |
9154 | |
9155 | /*! \fn void QString::push_back(QChar ch) |
9156 | |
9157 | \overload |
9158 | |
9159 | Appends the given \a ch character onto the end of this string. |
9160 | */ |
9161 | |
9162 | /*! |
9163 | \since 6.1 |
9164 | |
9165 | Removes from the string the characters in the half-open range |
9166 | [ \a first , \a last ). Returns an iterator to the character |
9167 | immediately after the last erased character (i.e. the character |
9168 | referred to by \a last before the erase). |
9169 | */ |
9170 | QString::iterator QString::erase(QString::const_iterator first, QString::const_iterator last) |
9171 | { |
9172 | const auto start = std::distance(first: cbegin(), last: first); |
9173 | const auto len = std::distance(first: first, last: last); |
9174 | remove(pos: start, len); |
9175 | return begin() + start; |
9176 | } |
9177 | |
9178 | /*! |
9179 | \fn QString::iterator QString::erase(QString::const_iterator it) |
9180 | |
9181 | \since 6.5 |
9182 | |
9183 | Removes the character denoted by \c it from the string. |
9184 | Returns an iterator to the character immediately after the |
9185 | erased character. |
9186 | |
9187 | \code |
9188 | QString c = "abcdefg"; |
9189 | auto it = c.erase(c.cbegin()); // c is now "bcdefg"; "it" points to "b" |
9190 | \endcode |
9191 | */ |
9192 | |
9193 | /*! \fn void QString::shrink_to_fit() |
9194 | \since 5.10 |
9195 | |
9196 | This function is provided for STL compatibility. It is |
9197 | equivalent to squeeze(). |
9198 | |
9199 | \sa squeeze() |
9200 | */ |
9201 | |
9202 | /*! |
9203 | \fn std::string QString::toStdString() const |
9204 | |
9205 | Returns a std::string object with the data contained in this |
9206 | QString. The Unicode data is converted into 8-bit characters using |
9207 | the toUtf8() function. |
9208 | |
9209 | This method is mostly useful to pass a QString to a function |
9210 | that accepts a std::string object. |
9211 | |
9212 | \sa toLatin1(), toUtf8(), toLocal8Bit(), QByteArray::toStdString() |
9213 | */ |
9214 | |
9215 | /*! |
9216 | Constructs a QString that uses the first \a size Unicode characters |
9217 | in the array \a unicode. The data in \a unicode is \e not |
9218 | copied. The caller must be able to guarantee that \a unicode will |
9219 | not be deleted or modified as long as the QString (or an |
9220 | unmodified copy of it) exists. |
9221 | |
9222 | Any attempts to modify the QString or copies of it will cause it |
9223 | to create a deep copy of the data, ensuring that the raw data |
9224 | isn't modified. |
9225 | |
9226 | Here is an example of how we can use a QRegularExpression on raw data in |
9227 | memory without requiring to copy the data into a QString: |
9228 | |
9229 | \snippet qstring/main.cpp 22 |
9230 | \snippet qstring/main.cpp 23 |
9231 | |
9232 | \warning A string created with fromRawData() is \e not |
9233 | '\\0'-terminated, unless the raw data contains a '\\0' character |
9234 | at position \a size. This means unicode() will \e not return a |
9235 | '\\0'-terminated string (although utf16() does, at the cost of |
9236 | copying the raw data). |
9237 | |
9238 | \sa fromUtf16(), setRawData() |
9239 | */ |
9240 | QString QString::fromRawData(const QChar *unicode, qsizetype size) |
9241 | { |
9242 | return QString(DataPointer::fromRawData(rawData: const_cast<char16_t *>(reinterpret_cast<const char16_t *>(unicode)), length: size)); |
9243 | } |
9244 | |
9245 | /*! |
9246 | \since 4.7 |
9247 | |
9248 | Resets the QString to use the first \a size Unicode characters |
9249 | in the array \a unicode. The data in \a unicode is \e not |
9250 | copied. The caller must be able to guarantee that \a unicode will |
9251 | not be deleted or modified as long as the QString (or an |
9252 | unmodified copy of it) exists. |
9253 | |
9254 | This function can be used instead of fromRawData() to re-use |
9255 | existings QString objects to save memory re-allocations. |
9256 | |
9257 | \sa fromRawData() |
9258 | */ |
9259 | QString &QString::setRawData(const QChar *unicode, qsizetype size) |
9260 | { |
9261 | if (!unicode || !size) { |
9262 | clear(); |
9263 | } |
9264 | *this = fromRawData(unicode, size); |
9265 | return *this; |
9266 | } |
9267 | |
9268 | /*! \fn QString QString::fromStdU16String(const std::u16string &str) |
9269 | \since 5.5 |
9270 | |
9271 | \include qstring.cpp {from-std-string} {UTF-16} {fromUtf16()} |
9272 | |
9273 | \sa fromUtf16(), fromStdWString(), fromStdU32String() |
9274 | */ |
9275 | |
9276 | /*! |
9277 | \fn std::u16string QString::toStdU16String() const |
9278 | \since 5.5 |
9279 | |
9280 | Returns a std::u16string object with the data contained in this |
9281 | QString. The Unicode data is the same as returned by the utf16() |
9282 | method. |
9283 | |
9284 | \sa utf16(), toStdWString(), toStdU32String() |
9285 | */ |
9286 | |
9287 | /*! \fn QString QString::fromStdU32String(const std::u32string &str) |
9288 | \since 5.5 |
9289 | |
9290 | \include qstring.cpp {from-std-string} {UCS-4} {fromUcs4()} |
9291 | |
9292 | \sa fromUcs4(), fromStdWString(), fromStdU16String() |
9293 | */ |
9294 | |
9295 | /*! |
9296 | \fn std::u32string QString::toStdU32String() const |
9297 | \since 5.5 |
9298 | |
9299 | Returns a std::u32string object with the data contained in this |
9300 | QString. The Unicode data is the same as returned by the toUcs4() |
9301 | method. |
9302 | |
9303 | \sa toUcs4(), toStdWString(), toStdU16String() |
9304 | */ |
9305 | |
9306 | #if !defined(QT_NO_DATASTREAM) || defined(QT_BOOTSTRAPPED) |
9307 | /*! |
9308 | \fn QDataStream &operator<<(QDataStream &stream, const QString &string) |
9309 | \relates QString |
9310 | |
9311 | Writes the given \a string to the specified \a stream. |
9312 | |
9313 | \sa {Serializing Qt Data Types} |
9314 | */ |
9315 | |
9316 | QDataStream &operator<<(QDataStream &out, const QString &str) |
9317 | { |
9318 | if (out.version() == 1) { |
9319 | out << str.toLatin1(); |
9320 | } else { |
9321 | if (!str.isNull() || out.version() < 3) { |
9322 | if ((out.byteOrder() == QDataStream::BigEndian) == (QSysInfo::ByteOrder == QSysInfo::BigEndian)) { |
9323 | out.writeBytes(reinterpret_cast<const char *>(str.unicode()), |
9324 | len: static_cast<uint>(sizeof(QChar) * str.size())); |
9325 | } else { |
9326 | QVarLengthArray<char16_t> buffer(str.size()); |
9327 | qbswap<sizeof(char16_t)>(source: str.constData(), count: str.size(), dest: buffer.data()); |
9328 | out.writeBytes(reinterpret_cast<const char *>(buffer.data()), |
9329 | len: static_cast<uint>(sizeof(char16_t) * buffer.size())); |
9330 | } |
9331 | } else { |
9332 | // write null marker |
9333 | out << (quint32)0xffffffff; |
9334 | } |
9335 | } |
9336 | return out; |
9337 | } |
9338 | |
9339 | /*! |
9340 | \fn QDataStream &operator>>(QDataStream &stream, QString &string) |
9341 | \relates QString |
9342 | |
9343 | Reads a string from the specified \a stream into the given \a string. |
9344 | |
9345 | \sa {Serializing Qt Data Types} |
9346 | */ |
9347 | |
9348 | QDataStream &operator>>(QDataStream &in, QString &str) |
9349 | { |
9350 | if (in.version() == 1) { |
9351 | QByteArray l; |
9352 | in >> l; |
9353 | str = QString::fromLatin1(ba: l); |
9354 | } else { |
9355 | quint32 bytes = 0; |
9356 | in >> bytes; // read size of string |
9357 | if (bytes == 0xffffffff) { // null string |
9358 | str = QString(); |
9359 | } else if (bytes > 0) { // not empty |
9360 | if (bytes & 0x1) { |
9361 | str.clear(); |
9362 | in.setStatus(QDataStream::ReadCorruptData); |
9363 | return in; |
9364 | } |
9365 | |
9366 | const quint32 Step = 1024 * 1024; |
9367 | quint32 len = bytes / 2; |
9368 | quint32 allocated = 0; |
9369 | |
9370 | while (allocated < len) { |
9371 | int blockSize = qMin(a: Step, b: len - allocated); |
9372 | str.resize(size: allocated + blockSize); |
9373 | if (in.readRawData(reinterpret_cast<char *>(str.data()) + allocated * 2, |
9374 | len: blockSize * 2) != blockSize * 2) { |
9375 | str.clear(); |
9376 | in.setStatus(QDataStream::ReadPastEnd); |
9377 | return in; |
9378 | } |
9379 | allocated += blockSize; |
9380 | } |
9381 | |
9382 | if ((in.byteOrder() == QDataStream::BigEndian) |
9383 | != (QSysInfo::ByteOrder == QSysInfo::BigEndian)) { |
9384 | char16_t *data = reinterpret_cast<char16_t *>(str.data()); |
9385 | qbswap<sizeof(*data)>(source: data, count: len, dest: data); |
9386 | } |
9387 | } else { |
9388 | str = QString(QLatin1StringView("" )); |
9389 | } |
9390 | } |
9391 | return in; |
9392 | } |
9393 | #endif // QT_NO_DATASTREAM |
9394 | |
9395 | /*! |
9396 | \typedef QString::Data |
9397 | \internal |
9398 | */ |
9399 | |
9400 | /*! |
9401 | \typedef QString::DataPtr |
9402 | \internal |
9403 | */ |
9404 | |
9405 | /*! |
9406 | \fn DataPtr & QString::data_ptr() |
9407 | \internal |
9408 | */ |
9409 | |
9410 | /*! |
9411 | \since 5.11 |
9412 | \internal |
9413 | \relates QStringView |
9414 | |
9415 | Returns \c true if the string is read right to left. |
9416 | |
9417 | \sa QString::isRightToLeft() |
9418 | */ |
9419 | bool QtPrivate::isRightToLeft(QStringView string) noexcept |
9420 | { |
9421 | int isolateLevel = 0; |
9422 | |
9423 | for (QStringIterator i(string); i.hasNext();) { |
9424 | const char32_t c = i.next(); |
9425 | |
9426 | switch (QChar::direction(ucs4: c)) { |
9427 | case QChar::DirRLI: |
9428 | case QChar::DirLRI: |
9429 | case QChar::DirFSI: |
9430 | ++isolateLevel; |
9431 | break; |
9432 | case QChar::DirPDI: |
9433 | if (isolateLevel) |
9434 | --isolateLevel; |
9435 | break; |
9436 | case QChar::DirL: |
9437 | if (isolateLevel) |
9438 | break; |
9439 | return false; |
9440 | case QChar::DirR: |
9441 | case QChar::DirAL: |
9442 | if (isolateLevel) |
9443 | break; |
9444 | return true; |
9445 | case QChar::DirEN: |
9446 | case QChar::DirES: |
9447 | case QChar::DirET: |
9448 | case QChar::DirAN: |
9449 | case QChar::DirCS: |
9450 | case QChar::DirB: |
9451 | case QChar::DirS: |
9452 | case QChar::DirWS: |
9453 | case QChar::DirON: |
9454 | case QChar::DirLRE: |
9455 | case QChar::DirLRO: |
9456 | case QChar::DirRLE: |
9457 | case QChar::DirRLO: |
9458 | case QChar::DirPDF: |
9459 | case QChar::DirNSM: |
9460 | case QChar::DirBN: |
9461 | break; |
9462 | } |
9463 | } |
9464 | return false; |
9465 | } |
9466 | |
9467 | qsizetype QtPrivate::count(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs) noexcept |
9468 | { |
9469 | qsizetype num = 0; |
9470 | qsizetype i = -1; |
9471 | if (haystack.size() > 500 && needle.size() > 5) { |
9472 | QStringMatcher matcher(needle, cs); |
9473 | while ((i = matcher.indexIn(str: haystack, from: i + 1)) != -1) |
9474 | ++num; |
9475 | } else { |
9476 | while ((i = QtPrivate::findString(haystack, from: i + 1, needle, cs)) != -1) |
9477 | ++num; |
9478 | } |
9479 | return num; |
9480 | } |
9481 | |
9482 | qsizetype QtPrivate::count(QStringView haystack, QChar needle, Qt::CaseSensitivity cs) noexcept |
9483 | { |
9484 | if (cs == Qt::CaseSensitive) |
9485 | return std::count(first: haystack.cbegin(), last: haystack.cend(), value: needle); |
9486 | |
9487 | needle = foldCase(ch: needle); |
9488 | return std::count_if(first: haystack.cbegin(), last: haystack.cend(), |
9489 | pred: [needle](const QChar c) { return foldAndCompare(a: c, b: needle); }); |
9490 | } |
9491 | |
9492 | qsizetype QtPrivate::count(QLatin1StringView haystack, QLatin1StringView needle, Qt::CaseSensitivity cs) |
9493 | { |
9494 | qsizetype num = 0; |
9495 | qsizetype i = -1; |
9496 | |
9497 | QLatin1StringMatcher matcher(needle, cs); |
9498 | while ((i = matcher.indexIn(haystack, from: i + 1)) != -1) |
9499 | ++num; |
9500 | |
9501 | return num; |
9502 | } |
9503 | |
9504 | qsizetype QtPrivate::count(QLatin1StringView haystack, QStringView needle, Qt::CaseSensitivity cs) |
9505 | { |
9506 | if (haystack.size() < needle.size()) |
9507 | return 0; |
9508 | |
9509 | if (!QtPrivate::isLatin1(s: needle)) // won't find non-L1 UTF-16 needles in a L1 haystack! |
9510 | return 0; |
9511 | |
9512 | qsizetype num = 0; |
9513 | qsizetype i = -1; |
9514 | |
9515 | QVarLengthArray<uchar> s(needle.size()); |
9516 | qt_to_latin1_unchecked(dst: s.data(), src: needle.utf16(), length: needle.size()); |
9517 | |
9518 | QLatin1StringMatcher matcher(QLatin1StringView(reinterpret_cast<char *>(s.data()), s.size()), |
9519 | cs); |
9520 | while ((i = matcher.indexIn(haystack, from: i + 1)) != -1) |
9521 | ++num; |
9522 | |
9523 | return num; |
9524 | } |
9525 | |
9526 | qsizetype QtPrivate::count(QStringView haystack, QLatin1StringView needle, Qt::CaseSensitivity cs) |
9527 | { |
9528 | if (haystack.size() < needle.size()) |
9529 | return -1; |
9530 | |
9531 | QVarLengthArray<char16_t> s = qt_from_latin1_to_qvla(str: needle); |
9532 | return QtPrivate::count(haystack, needle: QStringView(s.data(), s.size()), cs); |
9533 | } |
9534 | |
9535 | qsizetype QtPrivate::count(QLatin1StringView haystack, QChar needle, Qt::CaseSensitivity cs) noexcept |
9536 | { |
9537 | // non-L1 needles cannot possibly match in L1-only haystacks |
9538 | if (needle.unicode() > 0xff) |
9539 | return 0; |
9540 | |
9541 | if (cs == Qt::CaseSensitive) { |
9542 | return std::count(first: haystack.cbegin(), last: haystack.cend(), value: needle.toLatin1()); |
9543 | } else { |
9544 | return std::count_if(first: haystack.cbegin(), last: haystack.cend(), |
9545 | pred: CaseInsensitiveL1::matcher(ch: needle.toLatin1())); |
9546 | } |
9547 | } |
9548 | |
9549 | /*! |
9550 | \fn bool QtPrivate::startsWith(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs) |
9551 | \since 5.10 |
9552 | \fn bool QtPrivate::startsWith(QStringView haystack, QLatin1StringView needle, Qt::CaseSensitivity cs) |
9553 | \since 5.10 |
9554 | \fn bool QtPrivate::startsWith(QLatin1StringView haystack, QStringView needle, Qt::CaseSensitivity cs) |
9555 | \since 5.10 |
9556 | \fn bool QtPrivate::startsWith(QLatin1StringView haystack, QLatin1StringView needle, Qt::CaseSensitivity cs) |
9557 | \since 5.10 |
9558 | \internal |
9559 | \relates QStringView |
9560 | |
9561 | Returns \c true if \a haystack starts with \a needle, |
9562 | otherwise returns \c false. |
9563 | |
9564 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
9565 | |
9566 | \sa QtPrivate::endsWith(), QString::endsWith(), QStringView::endsWith(), QLatin1StringView::endsWith() |
9567 | */ |
9568 | |
9569 | bool QtPrivate::startsWith(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs) noexcept |
9570 | { |
9571 | return qt_starts_with_impl(haystack, needle, cs); |
9572 | } |
9573 | |
9574 | bool QtPrivate::startsWith(QStringView haystack, QLatin1StringView needle, Qt::CaseSensitivity cs) noexcept |
9575 | { |
9576 | return qt_starts_with_impl(haystack, needle, cs); |
9577 | } |
9578 | |
9579 | bool QtPrivate::startsWith(QLatin1StringView haystack, QStringView needle, Qt::CaseSensitivity cs) noexcept |
9580 | { |
9581 | return qt_starts_with_impl(haystack, needle, cs); |
9582 | } |
9583 | |
9584 | bool QtPrivate::startsWith(QLatin1StringView haystack, QLatin1StringView needle, Qt::CaseSensitivity cs) noexcept |
9585 | { |
9586 | return qt_starts_with_impl(haystack, needle, cs); |
9587 | } |
9588 | |
9589 | /*! |
9590 | \fn bool QtPrivate::endsWith(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs) |
9591 | \since 5.10 |
9592 | \fn bool QtPrivate::endsWith(QStringView haystack, QLatin1StringView needle, Qt::CaseSensitivity cs) |
9593 | \since 5.10 |
9594 | \fn bool QtPrivate::endsWith(QLatin1StringView haystack, QStringView needle, Qt::CaseSensitivity cs) |
9595 | \since 5.10 |
9596 | \fn bool QtPrivate::endsWith(QLatin1StringView haystack, QLatin1StringView needle, Qt::CaseSensitivity cs) |
9597 | \since 5.10 |
9598 | \internal |
9599 | \relates QStringView |
9600 | |
9601 | Returns \c true if \a haystack ends with \a needle, |
9602 | otherwise returns \c false. |
9603 | |
9604 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
9605 | |
9606 | \sa QtPrivate::startsWith(), QString::endsWith(), QStringView::endsWith(), QLatin1StringView::endsWith() |
9607 | */ |
9608 | |
9609 | bool QtPrivate::endsWith(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs) noexcept |
9610 | { |
9611 | return qt_ends_with_impl(haystack, needle, cs); |
9612 | } |
9613 | |
9614 | bool QtPrivate::endsWith(QStringView haystack, QLatin1StringView needle, Qt::CaseSensitivity cs) noexcept |
9615 | { |
9616 | return qt_ends_with_impl(haystack, needle, cs); |
9617 | } |
9618 | |
9619 | bool QtPrivate::endsWith(QLatin1StringView haystack, QStringView needle, Qt::CaseSensitivity cs) noexcept |
9620 | { |
9621 | return qt_ends_with_impl(haystack, needle, cs); |
9622 | } |
9623 | |
9624 | bool QtPrivate::endsWith(QLatin1StringView haystack, QLatin1StringView needle, Qt::CaseSensitivity cs) noexcept |
9625 | { |
9626 | return qt_ends_with_impl(haystack, needle, cs); |
9627 | } |
9628 | |
9629 | qsizetype QtPrivate::findString(QStringView haystack0, qsizetype from, QStringView needle0, Qt::CaseSensitivity cs) noexcept |
9630 | { |
9631 | const qsizetype l = haystack0.size(); |
9632 | const qsizetype sl = needle0.size(); |
9633 | if (from < 0) |
9634 | from += l; |
9635 | if (std::size_t(sl + from) > std::size_t(l)) |
9636 | return -1; |
9637 | if (!sl) |
9638 | return from; |
9639 | if (!l) |
9640 | return -1; |
9641 | |
9642 | if (sl == 1) |
9643 | return qFindChar(str: haystack0, ch: needle0[0], from, cs); |
9644 | |
9645 | /* |
9646 | We use the Boyer-Moore algorithm in cases where the overhead |
9647 | for the skip table should pay off, otherwise we use a simple |
9648 | hash function. |
9649 | */ |
9650 | if (l > 500 && sl > 5) |
9651 | return qFindStringBoyerMoore(haystack: haystack0, from, needle: needle0, cs); |
9652 | |
9653 | auto sv = [sl](const char16_t *v) { return QStringView(v, sl); }; |
9654 | /* |
9655 | We use some hashing for efficiency's sake. Instead of |
9656 | comparing strings, we compare the hash value of str with that |
9657 | of a part of this QString. Only if that matches, we call |
9658 | qt_string_compare(). |
9659 | */ |
9660 | const char16_t *needle = needle0.utf16(); |
9661 | const char16_t *haystack = haystack0.utf16() + from; |
9662 | const char16_t *end = haystack0.utf16() + (l - sl); |
9663 | const std::size_t sl_minus_1 = sl - 1; |
9664 | std::size_t hashNeedle = 0, hashHaystack = 0; |
9665 | qsizetype idx; |
9666 | |
9667 | if (cs == Qt::CaseSensitive) { |
9668 | for (idx = 0; idx < sl; ++idx) { |
9669 | hashNeedle = ((hashNeedle<<1) + needle[idx]); |
9670 | hashHaystack = ((hashHaystack<<1) + haystack[idx]); |
9671 | } |
9672 | hashHaystack -= haystack[sl_minus_1]; |
9673 | |
9674 | while (haystack <= end) { |
9675 | hashHaystack += haystack[sl_minus_1]; |
9676 | if (hashHaystack == hashNeedle |
9677 | && QtPrivate::compareStrings(lhs: needle0, rhs: sv(haystack), cs: Qt::CaseSensitive) == 0) |
9678 | return haystack - haystack0.utf16(); |
9679 | |
9680 | REHASH(*haystack); |
9681 | ++haystack; |
9682 | } |
9683 | } else { |
9684 | const char16_t *haystack_start = haystack0.utf16(); |
9685 | for (idx = 0; idx < sl; ++idx) { |
9686 | hashNeedle = (hashNeedle<<1) + foldCase(ch: needle + idx, start: needle); |
9687 | hashHaystack = (hashHaystack<<1) + foldCase(ch: haystack + idx, start: haystack_start); |
9688 | } |
9689 | hashHaystack -= foldCase(ch: haystack + sl_minus_1, start: haystack_start); |
9690 | |
9691 | while (haystack <= end) { |
9692 | hashHaystack += foldCase(ch: haystack + sl_minus_1, start: haystack_start); |
9693 | if (hashHaystack == hashNeedle |
9694 | && QtPrivate::compareStrings(lhs: needle0, rhs: sv(haystack), cs: Qt::CaseInsensitive) == 0) |
9695 | return haystack - haystack0.utf16(); |
9696 | |
9697 | REHASH(foldCase(haystack, haystack_start)); |
9698 | ++haystack; |
9699 | } |
9700 | } |
9701 | return -1; |
9702 | } |
9703 | |
9704 | qsizetype QtPrivate::findString(QStringView haystack, qsizetype from, QLatin1StringView needle, Qt::CaseSensitivity cs) noexcept |
9705 | { |
9706 | if (haystack.size() < needle.size()) |
9707 | return -1; |
9708 | |
9709 | QVarLengthArray<char16_t> s = qt_from_latin1_to_qvla(str: needle); |
9710 | return QtPrivate::findString(haystack0: haystack, from, needle0: QStringView(reinterpret_cast<const QChar*>(s.constData()), s.size()), cs); |
9711 | } |
9712 | |
9713 | qsizetype QtPrivate::findString(QLatin1StringView haystack, qsizetype from, QStringView needle, Qt::CaseSensitivity cs) noexcept |
9714 | { |
9715 | if (haystack.size() < needle.size()) |
9716 | return -1; |
9717 | |
9718 | if (!QtPrivate::isLatin1(s: needle)) // won't find non-L1 UTF-16 needles in a L1 haystack! |
9719 | return -1; |
9720 | |
9721 | if (needle.size() == 1) { |
9722 | const char n = needle.front().toLatin1(); |
9723 | return QtPrivate::findString(haystack, from, needle: QLatin1StringView(&n, 1), cs); |
9724 | } |
9725 | |
9726 | QVarLengthArray<char> s(needle.size()); |
9727 | qt_to_latin1_unchecked(dst: reinterpret_cast<uchar *>(s.data()), src: needle.utf16(), length: needle.size()); |
9728 | return QtPrivate::findString(haystack, from, needle: QLatin1StringView(s.data(), s.size()), cs); |
9729 | } |
9730 | |
9731 | qsizetype QtPrivate::findString(QLatin1StringView haystack, qsizetype from, QLatin1StringView needle, Qt::CaseSensitivity cs) noexcept |
9732 | { |
9733 | if (from < 0) |
9734 | from += haystack.size(); |
9735 | if (from < 0) |
9736 | return -1; |
9737 | qsizetype adjustedSize = haystack.size() - from; |
9738 | if (adjustedSize < needle.size()) |
9739 | return -1; |
9740 | if (needle.size() == 0) |
9741 | return from; |
9742 | |
9743 | if (cs == Qt::CaseSensitive) { |
9744 | |
9745 | if (needle.size() == 1) { |
9746 | Q_ASSUME(haystack.data() != nullptr); // see size check above |
9747 | if (auto it = memchr(s: haystack.data() + from, c: needle.front().toLatin1(), n: adjustedSize)) |
9748 | return static_cast<const char *>(it) - haystack.data(); |
9749 | return -1; |
9750 | } |
9751 | |
9752 | const QLatin1StringMatcher matcher(needle, Qt::CaseSensitivity::CaseSensitive); |
9753 | return matcher.indexIn(haystack, from); |
9754 | } |
9755 | |
9756 | // If the needle is sufficiently small we simply iteratively search through |
9757 | // the haystack. When the needle is too long we use a boyer-moore searcher |
9758 | // from the standard library, if available. If it is not available then the |
9759 | // QLatin1Strings are converted to QString and compared as such. Though |
9760 | // initialization is slower the boyer-moore search it employs still makes up |
9761 | // for it when haystack and needle are sufficiently long. |
9762 | // The needle size was chosen by testing various lengths using the |
9763 | // qstringtokenizer benchmark with the |
9764 | // "tokenize_qlatin1string_qlatin1string" test. |
9765 | #ifdef Q_CC_MSVC |
9766 | const qsizetype threshold = 1; |
9767 | #else |
9768 | const qsizetype threshold = 13; |
9769 | #endif |
9770 | if (needle.size() <= threshold) { |
9771 | const auto begin = haystack.begin(); |
9772 | const auto end = haystack.end() - needle.size() + 1; |
9773 | auto ciMatch = CaseInsensitiveL1::matcher(ch: needle[0].toLatin1()); |
9774 | const qsizetype nlen1 = needle.size() - 1; |
9775 | for (auto it = std::find_if(first: begin + from, last: end, pred: ciMatch); it < end; |
9776 | it = std::find_if(first: it + 1, last: end, pred: ciMatch)) { |
9777 | // In this comparison we skip the first character because we know it's a match |
9778 | if (!nlen1 || QLatin1StringView(it + 1, nlen1).compare(other: needle.sliced(pos: 1), cs) == 0) |
9779 | return std::distance(first: begin, last: it); |
9780 | } |
9781 | return -1; |
9782 | } |
9783 | |
9784 | QLatin1StringMatcher matcher(needle, Qt::CaseSensitivity::CaseInsensitive); |
9785 | return matcher.indexIn(haystack, from); |
9786 | } |
9787 | |
9788 | qsizetype QtPrivate::lastIndexOf(QStringView haystack, qsizetype from, QStringView needle, Qt::CaseSensitivity cs) noexcept |
9789 | { |
9790 | return qLastIndexOf(haystack0: haystack, from, needle0: needle, cs); |
9791 | } |
9792 | |
9793 | qsizetype QtPrivate::lastIndexOf(QStringView haystack, qsizetype from, QLatin1StringView needle, Qt::CaseSensitivity cs) noexcept |
9794 | { |
9795 | return qLastIndexOf(haystack0: haystack, from, needle0: needle, cs); |
9796 | } |
9797 | |
9798 | qsizetype QtPrivate::lastIndexOf(QLatin1StringView haystack, qsizetype from, QStringView needle, Qt::CaseSensitivity cs) noexcept |
9799 | { |
9800 | return qLastIndexOf(haystack0: haystack, from, needle0: needle, cs); |
9801 | } |
9802 | |
9803 | qsizetype QtPrivate::lastIndexOf(QLatin1StringView haystack, qsizetype from, QLatin1StringView needle, Qt::CaseSensitivity cs) noexcept |
9804 | { |
9805 | return qLastIndexOf(haystack0: haystack, from, needle0: needle, cs); |
9806 | } |
9807 | |
9808 | #if QT_CONFIG(regularexpression) |
9809 | qsizetype QtPrivate::indexOf(QStringView viewHaystack, const QString *stringHaystack, const QRegularExpression &re, qsizetype from, QRegularExpressionMatch *rmatch) |
9810 | { |
9811 | if (!re.isValid()) { |
9812 | qtWarnAboutInvalidRegularExpression(pattern: re.pattern(), where: "QString(View)::indexOf" ); |
9813 | return -1; |
9814 | } |
9815 | |
9816 | QRegularExpressionMatch match = stringHaystack |
9817 | ? re.match(subject: *stringHaystack, offset: from) |
9818 | : re.matchView(subjectView: viewHaystack, offset: from); |
9819 | if (match.hasMatch()) { |
9820 | const qsizetype ret = match.capturedStart(); |
9821 | if (rmatch) |
9822 | *rmatch = std::move(match); |
9823 | return ret; |
9824 | } |
9825 | |
9826 | return -1; |
9827 | } |
9828 | |
9829 | qsizetype QtPrivate::indexOf(QStringView haystack, const QRegularExpression &re, qsizetype from, QRegularExpressionMatch *rmatch) |
9830 | { |
9831 | return indexOf(viewHaystack: haystack, stringHaystack: nullptr, re, from, rmatch); |
9832 | } |
9833 | |
9834 | qsizetype QtPrivate::lastIndexOf(QStringView viewHaystack, const QString *stringHaystack, const QRegularExpression &re, qsizetype from, QRegularExpressionMatch *rmatch) |
9835 | { |
9836 | if (!re.isValid()) { |
9837 | qtWarnAboutInvalidRegularExpression(pattern: re.pattern(), where: "QString(View)::lastIndexOf" ); |
9838 | return -1; |
9839 | } |
9840 | |
9841 | qsizetype endpos = (from < 0) ? (viewHaystack.size() + from + 1) : (from + 1); |
9842 | QRegularExpressionMatchIterator iterator = stringHaystack |
9843 | ? re.globalMatch(subject: *stringHaystack) |
9844 | : re.globalMatchView(subjectView: viewHaystack); |
9845 | qsizetype lastIndex = -1; |
9846 | while (iterator.hasNext()) { |
9847 | QRegularExpressionMatch match = iterator.next(); |
9848 | qsizetype start = match.capturedStart(); |
9849 | if (start < endpos) { |
9850 | lastIndex = start; |
9851 | if (rmatch) |
9852 | *rmatch = std::move(match); |
9853 | } else { |
9854 | break; |
9855 | } |
9856 | } |
9857 | |
9858 | return lastIndex; |
9859 | } |
9860 | |
9861 | qsizetype QtPrivate::lastIndexOf(QStringView haystack, const QRegularExpression &re, qsizetype from, QRegularExpressionMatch *rmatch) |
9862 | { |
9863 | return lastIndexOf(viewHaystack: haystack, stringHaystack: nullptr, re, from, rmatch); |
9864 | } |
9865 | |
9866 | bool QtPrivate::contains(QStringView viewHaystack, const QString *stringHaystack, const QRegularExpression &re, QRegularExpressionMatch *rmatch) |
9867 | { |
9868 | if (!re.isValid()) { |
9869 | qtWarnAboutInvalidRegularExpression(pattern: re.pattern(), where: "QString(View)::contains" ); |
9870 | return false; |
9871 | } |
9872 | QRegularExpressionMatch m = stringHaystack |
9873 | ? re.match(subject: *stringHaystack) |
9874 | : re.matchView(subjectView: viewHaystack); |
9875 | bool hasMatch = m.hasMatch(); |
9876 | if (hasMatch && rmatch) |
9877 | *rmatch = std::move(m); |
9878 | return hasMatch; |
9879 | } |
9880 | |
9881 | bool QtPrivate::contains(QStringView haystack, const QRegularExpression &re, QRegularExpressionMatch *rmatch) |
9882 | { |
9883 | return contains(viewHaystack: haystack, stringHaystack: nullptr, re, rmatch); |
9884 | } |
9885 | |
9886 | qsizetype QtPrivate::count(QStringView haystack, const QRegularExpression &re) |
9887 | { |
9888 | if (!re.isValid()) { |
9889 | qtWarnAboutInvalidRegularExpression(pattern: re.pattern(), where: "QString(View)::count" ); |
9890 | return 0; |
9891 | } |
9892 | qsizetype count = 0; |
9893 | qsizetype index = -1; |
9894 | qsizetype len = haystack.size(); |
9895 | while (index <= len - 1) { |
9896 | QRegularExpressionMatch match = re.matchView(subjectView: haystack, offset: index + 1); |
9897 | if (!match.hasMatch()) |
9898 | break; |
9899 | count++; |
9900 | |
9901 | // Search again, from the next character after the beginning of this |
9902 | // capture. If the capture starts with a surrogate pair, both together |
9903 | // count as "one character". |
9904 | index = match.capturedStart(); |
9905 | if (index < len && haystack[index].isHighSurrogate()) |
9906 | ++index; |
9907 | } |
9908 | return count; |
9909 | } |
9910 | |
9911 | #endif // QT_CONFIG(regularexpression) |
9912 | |
9913 | /*! |
9914 | \since 5.0 |
9915 | |
9916 | Converts a plain text string to an HTML string with |
9917 | HTML metacharacters \c{<}, \c{>}, \c{&}, and \c{"} replaced by HTML |
9918 | entities. |
9919 | |
9920 | Example: |
9921 | |
9922 | \snippet code/src_corelib_text_qstring.cpp 7 |
9923 | */ |
9924 | QString QString::toHtmlEscaped() const |
9925 | { |
9926 | QString rich; |
9927 | const qsizetype len = size(); |
9928 | rich.reserve(asize: qsizetype(len * 1.1)); |
9929 | for (QChar ch : *this) { |
9930 | if (ch == u'<') |
9931 | rich += "<"_L1 ; |
9932 | else if (ch == u'>') |
9933 | rich += ">"_L1 ; |
9934 | else if (ch == u'&') |
9935 | rich += "&"_L1 ; |
9936 | else if (ch == u'"') |
9937 | rich += """_L1 ; |
9938 | else |
9939 | rich += ch; |
9940 | } |
9941 | rich.squeeze(); |
9942 | return rich; |
9943 | } |
9944 | |
9945 | /*! |
9946 | \macro QStringLiteral(str) |
9947 | \relates QString |
9948 | |
9949 | The macro generates the data for a QString out of the string literal \a str |
9950 | at compile time. Creating a QString from it is free in this case, and the |
9951 | generated string data is stored in the read-only segment of the compiled |
9952 | object file. |
9953 | |
9954 | If you have code that looks like this: |
9955 | |
9956 | \snippet code/src_corelib_text_qstring.cpp 9 |
9957 | |
9958 | then a temporary QString will be created to be passed as the \c{hasAttribute} |
9959 | function parameter. This can be quite expensive, as it involves a memory |
9960 | allocation and the copy/conversion of the data into QString's internal |
9961 | encoding. |
9962 | |
9963 | This cost can be avoided by using QStringLiteral instead: |
9964 | |
9965 | \snippet code/src_corelib_text_qstring.cpp 10 |
9966 | |
9967 | In this case, QString's internal data will be generated at compile time; no |
9968 | conversion or allocation will occur at runtime. |
9969 | |
9970 | Using QStringLiteral instead of a double quoted plain C++ string literal can |
9971 | significantly speed up creation of QString instances from data known at |
9972 | compile time. |
9973 | |
9974 | \note QLatin1StringView can still be more efficient than QStringLiteral |
9975 | when the string is passed to a function that has an overload taking |
9976 | QLatin1StringView and this overload avoids conversion to QString. For |
9977 | instance, QString::operator==() can compare to a QLatin1StringView |
9978 | directly: |
9979 | |
9980 | \snippet code/src_corelib_text_qstring.cpp 11 |
9981 | |
9982 | \note Some compilers have bugs encoding strings containing characters outside |
9983 | the US-ASCII character set. Make sure you prefix your string with \c{u} in |
9984 | those cases. It is optional otherwise. |
9985 | |
9986 | \sa QByteArrayLiteral |
9987 | */ |
9988 | |
9989 | #if QT_DEPRECATED_SINCE(6, 8) |
9990 | /*! |
9991 | \fn QtLiterals::operator""_qs(const char16_t *str, size_t size) |
9992 | |
9993 | \relates QString |
9994 | \since 6.2 |
9995 | \deprecated [6.8] Use \c _s from Qt::StringLiterals namespace instead. |
9996 | |
9997 | Literal operator that creates a QString out of the first \a size characters in |
9998 | the char16_t string literal \a str. |
9999 | |
10000 | The QString is created at compile time, and the generated string data is stored |
10001 | in the read-only segment of the compiled object file. Duplicate literals may |
10002 | share the same read-only memory. This functionality is interchangeable with |
10003 | QStringLiteral, but saves typing when many string literals are present in the |
10004 | code. |
10005 | |
10006 | The following code creates a QString: |
10007 | \code |
10008 | auto str = u"hello"_qs; |
10009 | \endcode |
10010 | |
10011 | \sa QStringLiteral, QtLiterals::operator""_qba(const char *str, size_t size) |
10012 | */ |
10013 | #endif // QT_DEPRECATED_SINCE(6, 8) |
10014 | |
10015 | /*! |
10016 | \fn Qt::Literals::StringLiterals::operator""_s(const char16_t *str, size_t size) |
10017 | |
10018 | \relates QString |
10019 | \since 6.4 |
10020 | |
10021 | Literal operator that creates a QString out of the first \a size characters in |
10022 | the char16_t string literal \a str. |
10023 | |
10024 | The QString is created at compile time, and the generated string data is stored |
10025 | in the read-only segment of the compiled object file. Duplicate literals may |
10026 | share the same read-only memory. This functionality is interchangeable with |
10027 | QStringLiteral, but saves typing when many string literals are present in the |
10028 | code. |
10029 | |
10030 | The following code creates a QString: |
10031 | \code |
10032 | using namespace Qt::Literals::StringLiterals; |
10033 | |
10034 | auto str = u"hello"_s; |
10035 | \endcode |
10036 | |
10037 | \sa Qt::Literals::StringLiterals |
10038 | */ |
10039 | |
10040 | /*! |
10041 | \internal |
10042 | */ |
10043 | void QAbstractConcatenable::appendLatin1To(QLatin1StringView in, QChar *out) noexcept |
10044 | { |
10045 | qt_from_latin1(dst: reinterpret_cast<char16_t *>(out), str: in.data(), size: size_t(in.size())); |
10046 | } |
10047 | |
10048 | /*! |
10049 | \fn template <typename T> qsizetype erase(QString &s, const T &t) |
10050 | \relates QString |
10051 | \since 6.1 |
10052 | |
10053 | Removes all elements that compare equal to \a t from the |
10054 | string \a s. Returns the number of elements removed, if any. |
10055 | |
10056 | \sa erase_if |
10057 | */ |
10058 | |
10059 | /*! |
10060 | \fn template <typename Predicate> qsizetype erase_if(QString &s, Predicate pred) |
10061 | \relates QString |
10062 | \since 6.1 |
10063 | |
10064 | Removes all elements for which the predicate \a pred returns true |
10065 | from the string \a s. Returns the number of elements removed, if |
10066 | any. |
10067 | |
10068 | \sa erase |
10069 | */ |
10070 | |
10071 | /*! |
10072 | \macro const char *qPrintable(const QString &str) |
10073 | \relates QString |
10074 | |
10075 | Returns \a str as a \c{const char *}. This is equivalent to |
10076 | \a{str}.toLocal8Bit().constData(). |
10077 | |
10078 | The char pointer will be invalid after the statement in which |
10079 | qPrintable() is used. This is because the array returned by |
10080 | QString::toLocal8Bit() will fall out of scope. |
10081 | |
10082 | \note qDebug(), qInfo(), qWarning(), qCritical(), qFatal() expect |
10083 | %s arguments to be UTF-8 encoded, while qPrintable() converts to |
10084 | local 8-bit encoding. Therefore qUtf8Printable() should be used |
10085 | for logging strings instead of qPrintable(). |
10086 | |
10087 | \sa qUtf8Printable() |
10088 | */ |
10089 | |
10090 | /*! |
10091 | \macro const char *qUtf8Printable(const QString &str) |
10092 | \relates QString |
10093 | \since 5.4 |
10094 | |
10095 | Returns \a str as a \c{const char *}. This is equivalent to |
10096 | \a{str}.toUtf8().constData(). |
10097 | |
10098 | The char pointer will be invalid after the statement in which |
10099 | qUtf8Printable() is used. This is because the array returned by |
10100 | QString::toUtf8() will fall out of scope. |
10101 | |
10102 | Example: |
10103 | |
10104 | \snippet code/src_corelib_text_qstring.cpp qUtf8Printable |
10105 | |
10106 | \sa qPrintable(), qDebug(), qInfo(), qWarning(), qCritical(), qFatal() |
10107 | */ |
10108 | |
10109 | /*! |
10110 | \macro const wchar_t *qUtf16Printable(const QString &str) |
10111 | \relates QString |
10112 | \since 5.7 |
10113 | |
10114 | Returns \a str as a \c{const ushort *}, but cast to a \c{const wchar_t *} |
10115 | to avoid warnings. This is equivalent to \a{str}.utf16() plus some casting. |
10116 | |
10117 | The only useful thing you can do with the return value of this macro is to |
10118 | pass it to QString::asprintf() for use in a \c{%ls} conversion. In particular, |
10119 | the return value is \e{not} a valid \c{const wchar_t*}! |
10120 | |
10121 | In general, the pointer will be invalid after the statement in which |
10122 | qUtf16Printable() is used. This is because the pointer may have been |
10123 | obtained from a temporary expression, which will fall out of scope. |
10124 | |
10125 | Example: |
10126 | |
10127 | \snippet code/src_corelib_text_qstring.cpp qUtf16Printable |
10128 | |
10129 | \sa qPrintable(), qDebug(), qInfo(), qWarning(), qCritical(), qFatal() |
10130 | */ |
10131 | |
10132 | QT_END_NAMESPACE |
10133 | |
10134 | #undef REHASH |
10135 | |