| 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 | // Qt-Security score:critical reason:data-parser |
| 6 | |
| 7 | #include "qstringlist.h" |
| 8 | #if QT_CONFIG(regularexpression) |
| 9 | #include "qregularexpression.h" |
| 10 | #endif |
| 11 | #include "qunicodetables_p.h" |
| 12 | #include <private/qstringconverter_p.h> |
| 13 | #include <private/qtools_p.h> |
| 14 | #include "qlocale_tools_p.h" |
| 15 | #include "private/qsimd_p.h" |
| 16 | #include <qnumeric.h> |
| 17 | #include <qdatastream.h> |
| 18 | #include <qlist.h> |
| 19 | #include "qlocale.h" |
| 20 | #include "qlocale_p.h" |
| 21 | #include "qspan.h" |
| 22 | #include "qstringbuilder.h" |
| 23 | #include "qstringmatcher.h" |
| 24 | #include "qvarlengtharray.h" |
| 25 | #include "qdebug.h" |
| 26 | #include "qendian.h" |
| 27 | #include "qcollator.h" |
| 28 | #include "qttypetraits.h" |
| 29 | |
| 30 | #ifdef Q_OS_DARWIN |
| 31 | #include <private/qcore_mac_p.h> |
| 32 | #endif |
| 33 | |
| 34 | #include <private/qfunctions_p.h> |
| 35 | |
| 36 | #include <limits.h> |
| 37 | #include <string.h> |
| 38 | #include <stdlib.h> |
| 39 | #include <stdio.h> |
| 40 | #include <stdarg.h> |
| 41 | #include <wchar.h> |
| 42 | |
| 43 | #include "qchar.cpp" |
| 44 | #include "qlatin1stringmatcher.h" |
| 45 | #include "qstringmatcher.cpp" |
| 46 | #include "qstringiterator_p.h" |
| 47 | #include "qstringalgorithms_p.h" |
| 48 | #include "qthreadstorage.h" |
| 49 | |
| 50 | #include <algorithm> |
| 51 | #include <functional> |
| 52 | |
| 53 | #ifdef Q_OS_WIN |
| 54 | # include <qt_windows.h> |
| 55 | # if !defined(QT_BOOTSTRAPPED) && (defined(QT_NO_CAST_FROM_ASCII) || defined(QT_NO_CAST_TO_ASCII)) |
| 56 | // MSVC requires this, but let's apply it to MinGW compilers too, just in case |
| 57 | # error "This file cannot be compiled with QT_NO_CAST_{TO,FROM}_ASCII, " \ |
| 58 | "otherwise some QString functions will not get exported." |
| 59 | # endif |
| 60 | #endif |
| 61 | |
| 62 | #ifdef truncate |
| 63 | # undef truncate |
| 64 | #endif |
| 65 | |
| 66 | #define REHASH(a) \ |
| 67 | if (sl_minus_1 < sizeof(sl_minus_1) * CHAR_BIT) \ |
| 68 | hashHaystack -= decltype(hashHaystack)(a) << sl_minus_1; \ |
| 69 | hashHaystack <<= 1 |
| 70 | |
| 71 | QT_BEGIN_NAMESPACE |
| 72 | |
| 73 | using namespace Qt::StringLiterals; |
| 74 | using namespace QtMiscUtils; |
| 75 | |
| 76 | const char16_t QString::_empty = 0; |
| 77 | |
| 78 | // in qstringmatcher.cpp |
| 79 | qsizetype qFindStringBoyerMoore(QStringView haystack, qsizetype from, QStringView needle, Qt::CaseSensitivity cs); |
| 80 | |
| 81 | namespace { |
| 82 | enum StringComparisonMode { |
| 83 | CompareStringsForEquality, |
| 84 | CompareStringsForOrdering |
| 85 | }; |
| 86 | |
| 87 | template <typename Pointer> |
| 88 | char32_t foldCaseHelper(Pointer ch, Pointer start) = delete; |
| 89 | |
| 90 | template <> |
| 91 | char32_t foldCaseHelper<const QChar*>(const QChar* ch, const QChar* start) |
| 92 | { |
| 93 | return foldCase(ch: reinterpret_cast<const char16_t*>(ch), |
| 94 | start: reinterpret_cast<const char16_t*>(start)); |
| 95 | } |
| 96 | |
| 97 | template <> |
| 98 | char32_t foldCaseHelper<const char*>(const char* ch, const char*) |
| 99 | { |
| 100 | return foldCase(ch: char16_t(uchar(*ch))); |
| 101 | } |
| 102 | |
| 103 | template <typename T> |
| 104 | char16_t valueTypeToUtf16(T t) = delete; |
| 105 | |
| 106 | template <> |
| 107 | char16_t valueTypeToUtf16<QChar>(QChar t) |
| 108 | { |
| 109 | return t.unicode(); |
| 110 | } |
| 111 | |
| 112 | template <> |
| 113 | char16_t valueTypeToUtf16<char>(char t) |
| 114 | { |
| 115 | return char16_t{uchar(t)}; |
| 116 | } |
| 117 | |
| 118 | template <typename T> |
| 119 | static inline bool foldAndCompare(const T a, const T b) |
| 120 | { |
| 121 | return foldCase(a) == b; |
| 122 | } |
| 123 | |
| 124 | /*! |
| 125 | \internal |
| 126 | |
| 127 | Returns the index position of the first occurrence of the |
| 128 | character \a ch in the string given by \a str and \a len, |
| 129 | searching forward from index |
| 130 | position \a from. Returns -1 if \a ch could not be found. |
| 131 | */ |
| 132 | template <typename Haystack> |
| 133 | static inline qsizetype qLastIndexOf(Haystack haystack, QChar needle, |
| 134 | qsizetype from, Qt::CaseSensitivity cs) noexcept |
| 135 | { |
| 136 | if (haystack.size() == 0) |
| 137 | return -1; |
| 138 | if (from < 0) |
| 139 | from += haystack.size(); |
| 140 | else if (std::size_t(from) > std::size_t(haystack.size())) |
| 141 | from = haystack.size() - 1; |
| 142 | if (from >= 0) { |
| 143 | char16_t c = needle.unicode(); |
| 144 | const auto b = haystack.data(); |
| 145 | auto n = b + from; |
| 146 | if (cs == Qt::CaseSensitive) { |
| 147 | for (; n >= b; --n) |
| 148 | if (valueTypeToUtf16(*n) == c) |
| 149 | return n - b; |
| 150 | } else { |
| 151 | c = foldCase(ch: c); |
| 152 | for (; n >= b; --n) |
| 153 | if (foldCase(valueTypeToUtf16(*n)) == c) |
| 154 | return n - b; |
| 155 | } |
| 156 | } |
| 157 | return -1; |
| 158 | } |
| 159 | template <> qsizetype |
| 160 | qLastIndexOf(QString, QChar, qsizetype, Qt::CaseSensitivity) noexcept = delete; // unwanted, would detach |
| 161 | |
| 162 | template<typename Haystack, typename Needle> |
| 163 | static qsizetype qLastIndexOf(Haystack haystack0, qsizetype from, |
| 164 | Needle needle0, Qt::CaseSensitivity cs) noexcept |
| 165 | { |
| 166 | const qsizetype sl = needle0.size(); |
| 167 | if (sl == 1) |
| 168 | return qLastIndexOf(haystack0, needle0.front(), from, cs); |
| 169 | |
| 170 | const qsizetype l = haystack0.size(); |
| 171 | if (from < 0) |
| 172 | from += l; |
| 173 | if (from == l && sl == 0) |
| 174 | return from; |
| 175 | const qsizetype delta = l - sl; |
| 176 | if (std::size_t(from) > std::size_t(l) || delta < 0) |
| 177 | return -1; |
| 178 | if (from > delta) |
| 179 | from = delta; |
| 180 | |
| 181 | auto sv = [sl](const typename Haystack::value_type *v) { return Haystack(v, sl); }; |
| 182 | |
| 183 | auto haystack = haystack0.data(); |
| 184 | const auto needle = needle0.data(); |
| 185 | const auto *end = haystack; |
| 186 | haystack += from; |
| 187 | const qregisteruint sl_minus_1 = sl ? sl - 1 : 0; |
| 188 | const auto *n = needle + sl_minus_1; |
| 189 | const auto *h = haystack + sl_minus_1; |
| 190 | qregisteruint hashNeedle = 0, hashHaystack = 0; |
| 191 | |
| 192 | if (cs == Qt::CaseSensitive) { |
| 193 | for (qsizetype idx = 0; idx < sl; ++idx) { |
| 194 | hashNeedle = (hashNeedle << 1) + valueTypeToUtf16(*(n - idx)); |
| 195 | hashHaystack = (hashHaystack << 1) + valueTypeToUtf16(*(h - idx)); |
| 196 | } |
| 197 | hashHaystack -= valueTypeToUtf16(*haystack); |
| 198 | |
| 199 | while (haystack >= end) { |
| 200 | hashHaystack += valueTypeToUtf16(*haystack); |
| 201 | if (hashHaystack == hashNeedle |
| 202 | && QtPrivate::compareStrings(needle0, sv(haystack), Qt::CaseSensitive) == 0) |
| 203 | return haystack - end; |
| 204 | --haystack; |
| 205 | REHASH(valueTypeToUtf16(haystack[sl])); |
| 206 | } |
| 207 | } else { |
| 208 | for (qsizetype idx = 0; idx < sl; ++idx) { |
| 209 | hashNeedle = (hashNeedle << 1) + foldCaseHelper(n - idx, needle); |
| 210 | hashHaystack = (hashHaystack << 1) + foldCaseHelper(h - idx, end); |
| 211 | } |
| 212 | hashHaystack -= foldCaseHelper(haystack, end); |
| 213 | |
| 214 | while (haystack >= end) { |
| 215 | hashHaystack += foldCaseHelper(haystack, end); |
| 216 | if (hashHaystack == hashNeedle |
| 217 | && QtPrivate::compareStrings(sv(haystack), needle0, Qt::CaseInsensitive) == 0) |
| 218 | return haystack - end; |
| 219 | --haystack; |
| 220 | REHASH(foldCaseHelper(haystack + sl, end)); |
| 221 | } |
| 222 | } |
| 223 | return -1; |
| 224 | } |
| 225 | |
| 226 | template <typename Haystack, typename Needle> |
| 227 | bool qt_starts_with_impl(Haystack haystack, Needle needle, Qt::CaseSensitivity cs) noexcept |
| 228 | { |
| 229 | if (haystack.isNull()) |
| 230 | return needle.isNull(); |
| 231 | const auto haystackLen = haystack.size(); |
| 232 | const auto needleLen = needle.size(); |
| 233 | if (haystackLen == 0) |
| 234 | return needleLen == 0; |
| 235 | if (needleLen > haystackLen) |
| 236 | return false; |
| 237 | |
| 238 | return QtPrivate::compareStrings(haystack.first(needleLen), needle, cs) == 0; |
| 239 | } |
| 240 | |
| 241 | template <typename Haystack, typename Needle> |
| 242 | bool qt_ends_with_impl(Haystack haystack, Needle needle, Qt::CaseSensitivity cs) noexcept |
| 243 | { |
| 244 | if (haystack.isNull()) |
| 245 | return needle.isNull(); |
| 246 | const auto haystackLen = haystack.size(); |
| 247 | const auto needleLen = needle.size(); |
| 248 | if (haystackLen == 0) |
| 249 | return needleLen == 0; |
| 250 | if (haystackLen < needleLen) |
| 251 | return false; |
| 252 | |
| 253 | return QtPrivate::compareStrings(haystack.last(needleLen), needle, cs) == 0; |
| 254 | } |
| 255 | |
| 256 | template <typename T> |
| 257 | static void append_helper(QString &self, T view) |
| 258 | { |
| 259 | const auto strData = view.data(); |
| 260 | const qsizetype strSize = view.size(); |
| 261 | auto &d = self.data_ptr(); |
| 262 | if (strData && strSize > 0) { |
| 263 | // the number of UTF-8 code units is always at a minimum equal to the number |
| 264 | // of equivalent UTF-16 code units |
| 265 | d.detachAndGrow(where: QArrayData::GrowsAtEnd, n: strSize, data: nullptr, old: nullptr); |
| 266 | Q_CHECK_PTR(d.data()); |
| 267 | Q_ASSERT(strSize <= d.freeSpaceAtEnd()); |
| 268 | |
| 269 | auto dst = std::next(x: d.data(), n: d.size); |
| 270 | if constexpr (std::is_same_v<T, QUtf8StringView>) { |
| 271 | dst = QUtf8::convertToUnicode(dst, view); |
| 272 | } else if constexpr (std::is_same_v<T, QLatin1StringView>) { |
| 273 | QLatin1::convertToUnicode(dst, view); |
| 274 | dst += strSize; |
| 275 | } else { |
| 276 | static_assert(QtPrivate::type_dependent_false<T>(), |
| 277 | "Can only operate on UTF-8 and Latin-1" ); |
| 278 | } |
| 279 | self.resize(size: std::distance(first: d.begin(), last: dst)); |
| 280 | } else if (d.isNull() && !view.isNull()) { // special case |
| 281 | self = QLatin1StringView("" ); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | template <uint MaxCount> struct UnrollTailLoop |
| 286 | { |
| 287 | template <typename RetType, typename Functor1, typename Functor2, typename Number> |
| 288 | static inline RetType exec(Number count, RetType returnIfExited, Functor1 loopCheck, Functor2 returnIfFailed, Number i = 0) |
| 289 | { |
| 290 | /* equivalent to: |
| 291 | * while (count--) { |
| 292 | * if (loopCheck(i)) |
| 293 | * return returnIfFailed(i); |
| 294 | * } |
| 295 | * return returnIfExited; |
| 296 | */ |
| 297 | |
| 298 | if (!count) |
| 299 | return returnIfExited; |
| 300 | |
| 301 | bool check = loopCheck(i); |
| 302 | if (check) |
| 303 | return returnIfFailed(i); |
| 304 | |
| 305 | return UnrollTailLoop<MaxCount - 1>::exec(count - 1, returnIfExited, loopCheck, returnIfFailed, i + 1); |
| 306 | } |
| 307 | |
| 308 | template <typename Functor, typename Number> |
| 309 | static inline void exec(Number count, Functor code) |
| 310 | { |
| 311 | /* equivalent to: |
| 312 | * for (Number i = 0; i < count; ++i) |
| 313 | * code(i); |
| 314 | */ |
| 315 | exec(count, 0, [=](Number i) -> bool { code(i); return false; }, [](Number) { return 0; }); |
| 316 | } |
| 317 | }; |
| 318 | template <> template <typename RetType, typename Functor1, typename Functor2, typename Number> |
| 319 | inline RetType UnrollTailLoop<0>::exec(Number, RetType returnIfExited, Functor1, Functor2, Number) |
| 320 | { |
| 321 | return returnIfExited; |
| 322 | } |
| 323 | } // unnamed namespace |
| 324 | |
| 325 | /* |
| 326 | * Note on the use of SIMD in qstring.cpp: |
| 327 | * |
| 328 | * Several operations with strings are improved with the use of SIMD code, |
| 329 | * since they are repetitive. For MIPS, we have hand-written assembly code |
| 330 | * outside of qstring.cpp targeting MIPS DSP and MIPS DSPr2. For ARM and for |
| 331 | * x86, we can only use intrinsics and therefore everything is contained in |
| 332 | * qstring.cpp. We need to use intrinsics only for those platforms due to the |
| 333 | * different compilers and toolchains used, which have different syntax for |
| 334 | * assembly sources. |
| 335 | * |
| 336 | * ** SSE notes: ** |
| 337 | * |
| 338 | * Whenever multiple alternatives are equivalent or near so, we prefer the one |
| 339 | * using instructions from SSE2, since SSE2 is guaranteed to be enabled for all |
| 340 | * 64-bit builds and we enable it for 32-bit builds by default. Use of higher |
| 341 | * SSE versions should be done when there is a clear performance benefit and |
| 342 | * requires fallback code to SSE2, if it exists. |
| 343 | * |
| 344 | * Performance measurement in the past shows that most strings are short in |
| 345 | * size and, therefore, do not benefit from alignment prologues. That is, |
| 346 | * trying to find a 16-byte-aligned boundary to operate on is often more |
| 347 | * expensive than executing the unaligned operation directly. In addition, note |
| 348 | * that the QString private data is designed so that the data is stored on |
| 349 | * 16-byte boundaries if the system malloc() returns 16-byte aligned pointers |
| 350 | * on its own (64-bit glibc on Linux does; 32-bit glibc on Linux returns them |
| 351 | * 50% of the time), so skipping the alignment prologue is actually optimizing |
| 352 | * for the common case. |
| 353 | */ |
| 354 | |
| 355 | #if defined(__mips_dsp) |
| 356 | // From qstring_mips_dsp_asm.S |
| 357 | extern "C" void qt_fromlatin1_mips_asm_unroll4 (char16_t*, const char*, uint); |
| 358 | extern "C" void qt_fromlatin1_mips_asm_unroll8 (char16_t*, const char*, uint); |
| 359 | extern "C" void qt_toLatin1_mips_dsp_asm(uchar *dst, const char16_t *src, int length); |
| 360 | #endif |
| 361 | |
| 362 | #if defined(__SSE2__) && defined(Q_CC_GNU) |
| 363 | // We may overrun the buffer, but that's a false positive: |
| 364 | // this won't crash nor produce incorrect results |
| 365 | # define ATTRIBUTE_NO_SANITIZE __attribute__((__no_sanitize_address__, __no_sanitize_thread__)) |
| 366 | #else |
| 367 | # define ATTRIBUTE_NO_SANITIZE |
| 368 | #endif |
| 369 | |
| 370 | #ifdef __SSE2__ |
| 371 | static constexpr bool UseSse4_1 = bool(qCompilerCpuFeatures & CpuFeatureSSE4_1); |
| 372 | static constexpr bool UseAvx2 = UseSse4_1 && |
| 373 | (qCompilerCpuFeatures & CpuFeatureArchHaswell) == CpuFeatureArchHaswell; |
| 374 | |
| 375 | [[maybe_unused]] |
| 376 | Q_ALWAYS_INLINE static __m128i mm_load8_zero_extend(const void *ptr) |
| 377 | { |
| 378 | const __m128i *dataptr = static_cast<const __m128i *>(ptr); |
| 379 | if constexpr (UseSse4_1) { |
| 380 | // use a MOVQ followed by PMOVZXBW |
| 381 | // if AVX2 is present, these should combine into a single VPMOVZXBW instruction |
| 382 | __m128i data = _mm_loadl_epi64(p: dataptr); |
| 383 | return _mm_cvtepu8_epi16(V: data); |
| 384 | } |
| 385 | |
| 386 | // use MOVQ followed by PUNPCKLBW |
| 387 | __m128i data = _mm_loadl_epi64(p: dataptr); |
| 388 | return _mm_unpacklo_epi8(a: data, b: _mm_setzero_si128()); |
| 389 | } |
| 390 | |
| 391 | [[maybe_unused]] ATTRIBUTE_NO_SANITIZE |
| 392 | static qsizetype qustrlen_sse2(const char16_t *str) noexcept |
| 393 | { |
| 394 | // find the 16-byte alignment immediately prior or equal to str |
| 395 | quintptr misalignment = quintptr(str) & 0xf; |
| 396 | Q_ASSERT((misalignment & 1) == 0); |
| 397 | const char16_t *ptr = str - (misalignment / 2); |
| 398 | |
| 399 | // load 16 bytes and see if we have a null |
| 400 | // (aligned loads can never segfault) |
| 401 | const __m128i zeroes = _mm_setzero_si128(); |
| 402 | __m128i data = _mm_load_si128(p: reinterpret_cast<const __m128i *>(ptr)); |
| 403 | __m128i comparison = _mm_cmpeq_epi16(a: data, b: zeroes); |
| 404 | uint mask = _mm_movemask_epi8(a: comparison); |
| 405 | |
| 406 | // ignore the result prior to the beginning of str |
| 407 | mask >>= misalignment; |
| 408 | |
| 409 | // Have we found something in the first block? Need to handle it now |
| 410 | // because of the left shift above. |
| 411 | if (mask) |
| 412 | return qCountTrailingZeroBits(v: mask) / sizeof(char16_t); |
| 413 | |
| 414 | constexpr qsizetype Step = sizeof(__m128i) / sizeof(char16_t); |
| 415 | qsizetype size = Step - misalignment / sizeof(char16_t); |
| 416 | |
| 417 | size -= Step; |
| 418 | do { |
| 419 | size += Step; |
| 420 | data = _mm_load_si128(p: reinterpret_cast<const __m128i *>(str + size)); |
| 421 | |
| 422 | comparison = _mm_cmpeq_epi16(a: data, b: zeroes); |
| 423 | mask = _mm_movemask_epi8(a: comparison); |
| 424 | } while (mask == 0); |
| 425 | |
| 426 | // found a null |
| 427 | return size + qCountTrailingZeroBits(v: mask) / sizeof(char16_t); |
| 428 | } |
| 429 | |
| 430 | // Scans from \a ptr to \a end until \a maskval is non-zero. Returns true if |
| 431 | // the no non-zero was found. Returns false and updates \a ptr to point to the |
| 432 | // first 16-bit word that has any bit set (note: if the input is 8-bit, \a ptr |
| 433 | // may be updated to one byte short). |
| 434 | static bool simdTestMask(const char *&ptr, const char *end, quint32 maskval) |
| 435 | { |
| 436 | auto updatePtr = [&](uint result) { |
| 437 | // found a character matching the mask |
| 438 | uint idx = qCountTrailingZeroBits(v: ~result); |
| 439 | ptr += idx; |
| 440 | return false; |
| 441 | }; |
| 442 | |
| 443 | if constexpr (UseSse4_1) { |
| 444 | # ifndef Q_OS_QNX // compiler fails in the code below |
| 445 | __m128i mask; |
| 446 | auto updatePtrSimd = [&](__m128i data) -> bool { |
| 447 | __m128i masked = _mm_and_si128(a: mask, b: data); |
| 448 | __m128i comparison = _mm_cmpeq_epi16(a: masked, b: _mm_setzero_si128()); |
| 449 | uint result = _mm_movemask_epi8(a: comparison); |
| 450 | return updatePtr(result); |
| 451 | }; |
| 452 | |
| 453 | if constexpr (UseAvx2) { |
| 454 | // AVX2 implementation: test 32 bytes at a time |
| 455 | const __m256i mask256 = _mm256_broadcastd_epi32(X: _mm_cvtsi32_si128(a: maskval)); |
| 456 | while (ptr + 32 <= end) { |
| 457 | __m256i data = _mm256_loadu_si256(p: reinterpret_cast<const __m256i *>(ptr)); |
| 458 | if (!_mm256_testz_si256(a: mask256, b: data)) { |
| 459 | // found a character matching the mask |
| 460 | __m256i masked256 = _mm256_and_si256(a: mask256, b: data); |
| 461 | __m256i comparison256 = _mm256_cmpeq_epi16(a: masked256, b: _mm256_setzero_si256()); |
| 462 | return updatePtr(_mm256_movemask_epi8(a: comparison256)); |
| 463 | } |
| 464 | ptr += 32; |
| 465 | } |
| 466 | |
| 467 | mask = _mm256_castsi256_si128(a: mask256); |
| 468 | } else { |
| 469 | // SSE 4.1 implementation: test 32 bytes at a time (two 16-byte |
| 470 | // comparisons, unrolled) |
| 471 | mask = _mm_set1_epi32(i: maskval); |
| 472 | while (ptr + 32 <= end) { |
| 473 | __m128i data1 = _mm_loadu_si128(p: reinterpret_cast<const __m128i *>(ptr)); |
| 474 | __m128i data2 = _mm_loadu_si128(p: reinterpret_cast<const __m128i *>(ptr + 16)); |
| 475 | if (!_mm_testz_si128(M: mask, V: data1)) |
| 476 | return updatePtrSimd(data1); |
| 477 | |
| 478 | ptr += 16; |
| 479 | if (!_mm_testz_si128(M: mask, V: data2)) |
| 480 | return updatePtrSimd(data2); |
| 481 | ptr += 16; |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | // AVX2 and SSE4.1: final 16-byte comparison |
| 486 | if (ptr + 16 <= end) { |
| 487 | __m128i data1 = _mm_loadu_si128(p: reinterpret_cast<const __m128i *>(ptr)); |
| 488 | if (!_mm_testz_si128(M: mask, V: data1)) |
| 489 | return updatePtrSimd(data1); |
| 490 | ptr += 16; |
| 491 | } |
| 492 | |
| 493 | // and final 8-byte comparison |
| 494 | if (ptr + 8 <= end) { |
| 495 | __m128i data1 = _mm_loadl_epi64(p: reinterpret_cast<const __m128i *>(ptr)); |
| 496 | if (!_mm_testz_si128(M: mask, V: data1)) |
| 497 | return updatePtrSimd(data1); |
| 498 | ptr += 8; |
| 499 | } |
| 500 | |
| 501 | return true; |
| 502 | # endif // QNX |
| 503 | } |
| 504 | |
| 505 | // SSE2 implementation: test 16 bytes at a time. |
| 506 | const __m128i mask = _mm_set1_epi32(i: maskval); |
| 507 | while (ptr + 16 <= end) { |
| 508 | __m128i data = _mm_loadu_si128(p: reinterpret_cast<const __m128i *>(ptr)); |
| 509 | __m128i masked = _mm_and_si128(a: mask, b: data); |
| 510 | __m128i comparison = _mm_cmpeq_epi16(a: masked, b: _mm_setzero_si128()); |
| 511 | quint16 result = _mm_movemask_epi8(a: comparison); |
| 512 | if (result != 0xffff) |
| 513 | return updatePtr(result); |
| 514 | ptr += 16; |
| 515 | } |
| 516 | |
| 517 | // and one 8-byte comparison |
| 518 | if (ptr + 8 <= end) { |
| 519 | __m128i data = _mm_loadl_epi64(p: reinterpret_cast<const __m128i *>(ptr)); |
| 520 | __m128i masked = _mm_and_si128(a: mask, b: data); |
| 521 | __m128i comparison = _mm_cmpeq_epi16(a: masked, b: _mm_setzero_si128()); |
| 522 | quint8 result = _mm_movemask_epi8(a: comparison); |
| 523 | if (result != 0xff) |
| 524 | return updatePtr(result); |
| 525 | ptr += 8; |
| 526 | } |
| 527 | |
| 528 | return true; |
| 529 | } |
| 530 | |
| 531 | template <StringComparisonMode Mode, typename Char> [[maybe_unused]] |
| 532 | static int ucstrncmp_sse2(const char16_t *a, const Char *b, size_t l) |
| 533 | { |
| 534 | static_assert(std::is_unsigned_v<Char>); |
| 535 | |
| 536 | // Using the PMOVMSKB instruction, we get two bits for each UTF-16 character |
| 537 | // we compare. This lambda helps extract the code unit. |
| 538 | static const auto codeUnitAt = [](const auto *n, qptrdiff idx) -> int { |
| 539 | constexpr int Stride = 2; |
| 540 | // this is the same as: |
| 541 | // return n[idx / Stride]; |
| 542 | // but using pointer arithmetic to avoid the compiler dividing by two |
| 543 | // and multiplying by two in the case of char16_t (we know idx is even, |
| 544 | // but the compiler does not). This is not UB. |
| 545 | |
| 546 | auto ptr = reinterpret_cast<const uchar *>(n); |
| 547 | ptr += idx / (Stride / sizeof(*n)); |
| 548 | return *reinterpret_cast<decltype(n)>(ptr); |
| 549 | }; |
| 550 | auto difference = [a, b](uint mask, qptrdiff offset) { |
| 551 | if (Mode == CompareStringsForEquality) |
| 552 | return 1; |
| 553 | uint idx = qCountTrailingZeroBits(v: mask); |
| 554 | return codeUnitAt(a + offset, idx) - codeUnitAt(b + offset, idx); |
| 555 | }; |
| 556 | |
| 557 | static const auto load8Chars = [](const auto *ptr) { |
| 558 | if (sizeof(*ptr) == 2) |
| 559 | return _mm_loadu_si128(p: reinterpret_cast<const __m128i *>(ptr)); |
| 560 | __m128i chunk = _mm_loadl_epi64(p: reinterpret_cast<const __m128i *>(ptr)); |
| 561 | return _mm_unpacklo_epi8(a: chunk, b: _mm_setzero_si128()); |
| 562 | }; |
| 563 | static const auto load4Chars = [](const auto *ptr) { |
| 564 | if (sizeof(*ptr) == 2) |
| 565 | return _mm_loadl_epi64(p: reinterpret_cast<const __m128i *>(ptr)); |
| 566 | __m128i chunk = _mm_cvtsi32_si128(qFromUnaligned<quint32>(ptr)); |
| 567 | return _mm_unpacklo_epi8(a: chunk, b: _mm_setzero_si128()); |
| 568 | }; |
| 569 | |
| 570 | // we're going to read a[0..15] and b[0..15] (32 bytes) |
| 571 | auto processChunk16Chars = [a, b](qptrdiff offset) -> uint { |
| 572 | if constexpr (UseAvx2) { |
| 573 | __m256i a_data = _mm256_loadu_si256(p: reinterpret_cast<const __m256i *>(a + offset)); |
| 574 | __m256i b_data; |
| 575 | if (sizeof(Char) == 1) { |
| 576 | // expand to UTF-16 via zero-extension |
| 577 | __m128i chunk = _mm_loadu_si128(p: reinterpret_cast<const __m128i *>(b + offset)); |
| 578 | b_data = _mm256_cvtepu8_epi16(V: chunk); |
| 579 | } else { |
| 580 | b_data = _mm256_loadu_si256(p: reinterpret_cast<const __m256i *>(b + offset)); |
| 581 | } |
| 582 | __m256i result = _mm256_cmpeq_epi16(a: a_data, b: b_data); |
| 583 | return _mm256_movemask_epi8(a: result); |
| 584 | } |
| 585 | |
| 586 | __m128i a_data1 = load8Chars(a + offset); |
| 587 | __m128i a_data2 = load8Chars(a + offset + 8); |
| 588 | __m128i b_data1, b_data2; |
| 589 | if (sizeof(Char) == 1) { |
| 590 | // expand to UTF-16 via unpacking |
| 591 | __m128i b_data = _mm_loadu_si128(p: reinterpret_cast<const __m128i *>(b + offset)); |
| 592 | b_data1 = _mm_unpacklo_epi8(a: b_data, b: _mm_setzero_si128()); |
| 593 | b_data2 = _mm_unpackhi_epi8(a: b_data, b: _mm_setzero_si128()); |
| 594 | } else { |
| 595 | b_data1 = load8Chars(b + offset); |
| 596 | b_data2 = load8Chars(b + offset + 8); |
| 597 | } |
| 598 | __m128i result1 = _mm_cmpeq_epi16(a: a_data1, b: b_data1); |
| 599 | __m128i result2 = _mm_cmpeq_epi16(a: a_data2, b: b_data2); |
| 600 | return _mm_movemask_epi8(a: result1) | _mm_movemask_epi8(a: result2) << 16; |
| 601 | }; |
| 602 | |
| 603 | if (l >= sizeof(__m256i) / sizeof(char16_t)) { |
| 604 | qptrdiff offset = 0; |
| 605 | for ( ; l >= offset + sizeof(__m256i) / sizeof(char16_t); offset += sizeof(__m256i) / sizeof(char16_t)) { |
| 606 | uint mask = ~processChunk16Chars(offset); |
| 607 | if (mask) |
| 608 | return difference(mask, offset); |
| 609 | } |
| 610 | |
| 611 | // maybe overlap the last 32 bytes |
| 612 | if (size_t(offset) < l) { |
| 613 | offset = l - sizeof(__m256i) / sizeof(char16_t); |
| 614 | uint mask = ~processChunk16Chars(offset); |
| 615 | return mask ? difference(mask, offset) : 0; |
| 616 | } |
| 617 | } else if (l >= 4) { |
| 618 | __m128i a_data1, b_data1; |
| 619 | __m128i a_data2, b_data2; |
| 620 | int width; |
| 621 | if (l >= 8) { |
| 622 | width = 8; |
| 623 | a_data1 = load8Chars(a); |
| 624 | b_data1 = load8Chars(b); |
| 625 | a_data2 = load8Chars(a + l - width); |
| 626 | b_data2 = load8Chars(b + l - width); |
| 627 | } else { |
| 628 | // we're going to read a[0..3] and b[0..3] (8 bytes) |
| 629 | width = 4; |
| 630 | a_data1 = load4Chars(a); |
| 631 | b_data1 = load4Chars(b); |
| 632 | a_data2 = load4Chars(a + l - width); |
| 633 | b_data2 = load4Chars(b + l - width); |
| 634 | } |
| 635 | |
| 636 | __m128i result = _mm_cmpeq_epi16(a: a_data1, b: b_data1); |
| 637 | ushort mask = ~_mm_movemask_epi8(a: result); |
| 638 | if (mask) |
| 639 | return difference(mask, 0); |
| 640 | |
| 641 | result = _mm_cmpeq_epi16(a: a_data2, b: b_data2); |
| 642 | mask = ~_mm_movemask_epi8(a: result); |
| 643 | if (mask) |
| 644 | return difference(mask, l - width); |
| 645 | } else { |
| 646 | // reset l |
| 647 | l &= 3; |
| 648 | |
| 649 | const auto lambda = [=](size_t i) -> int { |
| 650 | return a[i] - b[i]; |
| 651 | }; |
| 652 | return UnrollTailLoop<3>::exec(l, 0, lambda, lambda); |
| 653 | } |
| 654 | return 0; |
| 655 | } |
| 656 | #endif |
| 657 | |
| 658 | Q_NEVER_INLINE |
| 659 | qsizetype QtPrivate::qustrlen(const char16_t *str) noexcept |
| 660 | { |
| 661 | #if defined(__SSE2__) && !(defined(__SANITIZE_ADDRESS__) || __has_feature(address_sanitizer)) && !(defined(__SANITIZE_THREAD__) || __has_feature(thread_sanitizer)) |
| 662 | return qustrlen_sse2(str); |
| 663 | #endif |
| 664 | |
| 665 | if (sizeof(wchar_t) == sizeof(char16_t)) |
| 666 | return wcslen(s: reinterpret_cast<const wchar_t *>(str)); |
| 667 | |
| 668 | qsizetype result = 0; |
| 669 | while (*str++) |
| 670 | ++result; |
| 671 | return result; |
| 672 | } |
| 673 | |
| 674 | qsizetype QtPrivate::qustrnlen(const char16_t *str, qsizetype maxlen) noexcept |
| 675 | { |
| 676 | return qustrchr(str: { str, maxlen }, ch: u'\0') - str; |
| 677 | } |
| 678 | |
| 679 | /*! |
| 680 | * \internal |
| 681 | * |
| 682 | * Searches for character \a c in the string \a str and returns a pointer to |
| 683 | * it. Unlike strchr() and wcschr() (but like glibc's strchrnul()), if the |
| 684 | * character is not found, this function returns a pointer to the end of the |
| 685 | * string -- that is, \c{str.end()}. |
| 686 | */ |
| 687 | Q_NEVER_INLINE |
| 688 | const char16_t *QtPrivate::qustrchr(QStringView str, char16_t c) noexcept |
| 689 | { |
| 690 | const char16_t *n = str.utf16(); |
| 691 | const char16_t *e = n + str.size(); |
| 692 | |
| 693 | #ifdef __SSE2__ |
| 694 | bool loops = true; |
| 695 | // Using the PMOVMSKB instruction, we get two bits for each character |
| 696 | // we compare. |
| 697 | __m128i mch; |
| 698 | if constexpr (UseAvx2) { |
| 699 | // we're going to read n[0..15] (32 bytes) |
| 700 | __m256i mch256 = _mm256_set1_epi32(i: c | (c << 16)); |
| 701 | for (const char16_t *next = n + 16; next <= e; n = next, next += 16) { |
| 702 | __m256i data = _mm256_loadu_si256(p: reinterpret_cast<const __m256i *>(n)); |
| 703 | __m256i result = _mm256_cmpeq_epi16(a: data, b: mch256); |
| 704 | uint mask = uint(_mm256_movemask_epi8(a: result)); |
| 705 | if (mask) { |
| 706 | uint idx = qCountTrailingZeroBits(v: mask); |
| 707 | return n + idx / 2; |
| 708 | } |
| 709 | } |
| 710 | loops = false; |
| 711 | mch = _mm256_castsi256_si128(a: mch256); |
| 712 | } else { |
| 713 | mch = _mm_set1_epi32(i: c | (c << 16)); |
| 714 | } |
| 715 | |
| 716 | auto hasMatch = [mch, &n](__m128i data, ushort validityMask) { |
| 717 | __m128i result = _mm_cmpeq_epi16(a: data, b: mch); |
| 718 | uint mask = uint(_mm_movemask_epi8(a: result)); |
| 719 | if ((mask & validityMask) == 0) |
| 720 | return false; |
| 721 | uint idx = qCountTrailingZeroBits(v: mask); |
| 722 | n += idx / 2; |
| 723 | return true; |
| 724 | }; |
| 725 | |
| 726 | // we're going to read n[0..7] (16 bytes) |
| 727 | for (const char16_t *next = n + 8; next <= e; n = next, next += 8) { |
| 728 | __m128i data = _mm_loadu_si128(p: reinterpret_cast<const __m128i *>(n)); |
| 729 | if (hasMatch(data, 0xffff)) |
| 730 | return n; |
| 731 | |
| 732 | if (!loops) { |
| 733 | n += 8; |
| 734 | break; |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | # if !defined(__OPTIMIZE_SIZE__) |
| 739 | // we're going to read n[0..3] (8 bytes) |
| 740 | if (e - n > 3) { |
| 741 | __m128i data = _mm_loadl_epi64(p: reinterpret_cast<const __m128i *>(n)); |
| 742 | if (hasMatch(data, 0xff)) |
| 743 | return n; |
| 744 | |
| 745 | n += 4; |
| 746 | } |
| 747 | |
| 748 | return UnrollTailLoop<3>::exec(count: e - n, returnIfExited: e, |
| 749 | loopCheck: [=](qsizetype i) { return n[i] == c; }, |
| 750 | returnIfFailed: [=](qsizetype i) { return n + i; }); |
| 751 | # endif |
| 752 | #elif defined(__ARM_NEON__) |
| 753 | const uint16x8_t vmask = qvsetq_n_u16(1, 1 << 1, 1 << 2, 1 << 3, 1 << 4, 1 << 5, 1 << 6, 1 << 7); |
| 754 | const uint16x8_t ch_vec = vdupq_n_u16(c); |
| 755 | for (const char16_t *next = n + 8; next <= e; n = next, next += 8) { |
| 756 | uint16x8_t data = vld1q_u16(reinterpret_cast<const uint16_t *>(n)); |
| 757 | uint mask = vaddvq_u16(vandq_u16(vceqq_u16(data, ch_vec), vmask)); |
| 758 | if (ushort(mask)) { |
| 759 | // found a match |
| 760 | return n + qCountTrailingZeroBits(mask); |
| 761 | } |
| 762 | } |
| 763 | #endif // aarch64 |
| 764 | |
| 765 | return std::find(first: n, last: e, val: c); |
| 766 | } |
| 767 | |
| 768 | /*! |
| 769 | * \internal |
| 770 | * |
| 771 | * Searches case-insensitively for character \a c in the string \a str and |
| 772 | * returns a pointer to it. Iif the character is not found, this function |
| 773 | * returns a pointer to the end of the string -- that is, \c{str.end()}. |
| 774 | */ |
| 775 | Q_NEVER_INLINE |
| 776 | const char16_t *QtPrivate::qustrcasechr(QStringView str, char16_t c) noexcept |
| 777 | { |
| 778 | const QChar *n = str.begin(); |
| 779 | const QChar *e = str.end(); |
| 780 | c = foldCase(ch: c); |
| 781 | auto it = std::find_if(first: n, last: e, pred: [c](auto ch) { return foldAndCompare(ch, QChar(c)); }); |
| 782 | return reinterpret_cast<const char16_t *>(it); |
| 783 | } |
| 784 | |
| 785 | // Note: ptr on output may be off by one and point to a preceding US-ASCII |
| 786 | // character. Usually harmless. |
| 787 | bool qt_is_ascii(const char *&ptr, const char *end) noexcept |
| 788 | { |
| 789 | #if defined(__SSE2__) |
| 790 | // Testing for the high bit can be done efficiently with just PMOVMSKB |
| 791 | bool loops = true; |
| 792 | if constexpr (UseAvx2) { |
| 793 | while (ptr + 32 <= end) { |
| 794 | __m256i data = _mm256_loadu_si256(p: reinterpret_cast<const __m256i *>(ptr)); |
| 795 | quint32 mask = _mm256_movemask_epi8(a: data); |
| 796 | if (mask) { |
| 797 | uint idx = qCountTrailingZeroBits(v: mask); |
| 798 | ptr += idx; |
| 799 | return false; |
| 800 | } |
| 801 | ptr += 32; |
| 802 | } |
| 803 | loops = false; |
| 804 | } |
| 805 | |
| 806 | while (ptr + 16 <= end) { |
| 807 | __m128i data = _mm_loadu_si128(p: reinterpret_cast<const __m128i *>(ptr)); |
| 808 | quint32 mask = _mm_movemask_epi8(a: data); |
| 809 | if (mask) { |
| 810 | uint idx = qCountTrailingZeroBits(v: mask); |
| 811 | ptr += idx; |
| 812 | return false; |
| 813 | } |
| 814 | ptr += 16; |
| 815 | |
| 816 | if (!loops) |
| 817 | break; |
| 818 | } |
| 819 | if (ptr + 8 <= end) { |
| 820 | __m128i data = _mm_loadl_epi64(p: reinterpret_cast<const __m128i *>(ptr)); |
| 821 | quint8 mask = _mm_movemask_epi8(a: data); |
| 822 | if (mask) { |
| 823 | uint idx = qCountTrailingZeroBits(v: mask); |
| 824 | ptr += idx; |
| 825 | return false; |
| 826 | } |
| 827 | ptr += 8; |
| 828 | } |
| 829 | #endif |
| 830 | |
| 831 | while (ptr + 4 <= end) { |
| 832 | quint32 data = qFromUnaligned<quint32>(src: ptr); |
| 833 | if (data &= 0x80808080U) { |
| 834 | uint idx = QSysInfo::ByteOrder == QSysInfo::BigEndian |
| 835 | ? qCountLeadingZeroBits(v: data) |
| 836 | : qCountTrailingZeroBits(v: data); |
| 837 | ptr += idx / 8; |
| 838 | return false; |
| 839 | } |
| 840 | ptr += 4; |
| 841 | } |
| 842 | |
| 843 | while (ptr != end) { |
| 844 | if (quint8(*ptr) & 0x80) |
| 845 | return false; |
| 846 | ++ptr; |
| 847 | } |
| 848 | return true; |
| 849 | } |
| 850 | |
| 851 | bool QtPrivate::isAscii(QLatin1StringView s) noexcept |
| 852 | { |
| 853 | const char *ptr = s.begin(); |
| 854 | const char *end = s.end(); |
| 855 | |
| 856 | return qt_is_ascii(ptr, end); |
| 857 | } |
| 858 | |
| 859 | static bool isAscii_helper(const char16_t *&ptr, const char16_t *end) |
| 860 | { |
| 861 | #ifdef __SSE2__ |
| 862 | const char *ptr8 = reinterpret_cast<const char *>(ptr); |
| 863 | const char *end8 = reinterpret_cast<const char *>(end); |
| 864 | bool ok = simdTestMask(ptr&: ptr8, end: end8, maskval: 0xff80ff80); |
| 865 | ptr = reinterpret_cast<const char16_t *>(ptr8); |
| 866 | if (!ok) |
| 867 | return false; |
| 868 | #endif |
| 869 | |
| 870 | while (ptr != end) { |
| 871 | if (*ptr & 0xff80) |
| 872 | return false; |
| 873 | ++ptr; |
| 874 | } |
| 875 | return true; |
| 876 | } |
| 877 | |
| 878 | bool QtPrivate::isAscii(QStringView s) noexcept |
| 879 | { |
| 880 | const char16_t *ptr = s.utf16(); |
| 881 | const char16_t *end = ptr + s.size(); |
| 882 | |
| 883 | return isAscii_helper(ptr, end); |
| 884 | } |
| 885 | |
| 886 | bool QtPrivate::isLatin1(QStringView s) noexcept |
| 887 | { |
| 888 | const char16_t *ptr = s.utf16(); |
| 889 | const char16_t *end = ptr + s.size(); |
| 890 | |
| 891 | #ifdef __SSE2__ |
| 892 | const char *ptr8 = reinterpret_cast<const char *>(ptr); |
| 893 | const char *end8 = reinterpret_cast<const char *>(end); |
| 894 | if (!simdTestMask(ptr&: ptr8, end: end8, maskval: 0xff00ff00)) |
| 895 | return false; |
| 896 | ptr = reinterpret_cast<const char16_t *>(ptr8); |
| 897 | #endif |
| 898 | |
| 899 | while (ptr != end) { |
| 900 | if (*ptr++ > 0xff) |
| 901 | return false; |
| 902 | } |
| 903 | return true; |
| 904 | } |
| 905 | |
| 906 | bool QtPrivate::isValidUtf16(QStringView s) noexcept |
| 907 | { |
| 908 | constexpr char32_t InvalidCodePoint = UINT_MAX; |
| 909 | |
| 910 | QStringIterator i(s); |
| 911 | while (i.hasNext()) { |
| 912 | const char32_t c = i.next(invalidAs: InvalidCodePoint); |
| 913 | if (c == InvalidCodePoint) |
| 914 | return false; |
| 915 | } |
| 916 | |
| 917 | return true; |
| 918 | } |
| 919 | |
| 920 | // conversion between Latin 1 and UTF-16 |
| 921 | Q_CORE_EXPORT void qt_from_latin1(char16_t *dst, const char *str, size_t size) noexcept |
| 922 | { |
| 923 | /* SIMD: |
| 924 | * Unpacking with SSE has been shown to improve performance on recent CPUs |
| 925 | * The same method gives no improvement with NEON. On Aarch64, clang will do the vectorization |
| 926 | * itself in exactly the same way as one would do it with intrinsics. |
| 927 | */ |
| 928 | #if defined(__SSE2__) |
| 929 | // we're going to read str[offset..offset+15] (16 bytes) |
| 930 | const __m128i nullMask = _mm_setzero_si128(); |
| 931 | auto processOneChunk = [=](qptrdiff offset) { |
| 932 | const __m128i chunk = _mm_loadu_si128(p: (const __m128i*)(str + offset)); // load |
| 933 | if constexpr (UseAvx2) { |
| 934 | // zero extend to an YMM register |
| 935 | const __m256i extended = _mm256_cvtepu8_epi16(V: chunk); |
| 936 | |
| 937 | // store |
| 938 | _mm256_storeu_si256(p: (__m256i*)(dst + offset), a: extended); |
| 939 | } else { |
| 940 | // unpack the first 8 bytes, padding with zeros |
| 941 | const __m128i firstHalf = _mm_unpacklo_epi8(a: chunk, b: nullMask); |
| 942 | _mm_storeu_si128(p: (__m128i*)(dst + offset), b: firstHalf); // store |
| 943 | |
| 944 | // unpack the last 8 bytes, padding with zeros |
| 945 | const __m128i secondHalf = _mm_unpackhi_epi8 (a: chunk, b: nullMask); |
| 946 | _mm_storeu_si128(p: (__m128i*)(dst + offset + 8), b: secondHalf); // store |
| 947 | } |
| 948 | }; |
| 949 | |
| 950 | const char *e = str + size; |
| 951 | if (size >= sizeof(__m128i)) { |
| 952 | qptrdiff offset = 0; |
| 953 | for ( ; str + offset + sizeof(__m128i) <= e; offset += sizeof(__m128i)) |
| 954 | processOneChunk(offset); |
| 955 | if (str + offset < e) |
| 956 | processOneChunk(size - sizeof(__m128i)); |
| 957 | return; |
| 958 | } |
| 959 | |
| 960 | # if !defined(__OPTIMIZE_SIZE__) |
| 961 | if (size >= 4) { |
| 962 | // two overlapped loads & stores, of either 64-bit or of 32-bit |
| 963 | if (size >= 8) { |
| 964 | const __m128i unpacked1 = mm_load8_zero_extend(ptr: str); |
| 965 | const __m128i unpacked2 = mm_load8_zero_extend(ptr: str + size - 8); |
| 966 | _mm_storeu_si128(p: reinterpret_cast<__m128i *>(dst), b: unpacked1); |
| 967 | _mm_storeu_si128(p: reinterpret_cast<__m128i *>(dst + size - 8), b: unpacked2); |
| 968 | } else { |
| 969 | const __m128i chunk1 = _mm_cvtsi32_si128(a: qFromUnaligned<quint32>(src: str)); |
| 970 | const __m128i chunk2 = _mm_cvtsi32_si128(a: qFromUnaligned<quint32>(src: str + size - 4)); |
| 971 | const __m128i unpacked1 = _mm_unpacklo_epi8(a: chunk1, b: nullMask); |
| 972 | const __m128i unpacked2 = _mm_unpacklo_epi8(a: chunk2, b: nullMask); |
| 973 | _mm_storel_epi64(p: reinterpret_cast<__m128i *>(dst), a: unpacked1); |
| 974 | _mm_storel_epi64(p: reinterpret_cast<__m128i *>(dst + size - 4), a: unpacked2); |
| 975 | } |
| 976 | return; |
| 977 | } else { |
| 978 | size = size % 4; |
| 979 | return UnrollTailLoop<3>::exec(count: qsizetype(size), code: [=](qsizetype i) { dst[i] = uchar(str[i]); }); |
| 980 | } |
| 981 | # endif |
| 982 | #endif |
| 983 | #if defined(__mips_dsp) |
| 984 | static_assert(sizeof(qsizetype) == sizeof(int), |
| 985 | "oops, the assembler implementation needs to be called in a loop" ); |
| 986 | if (size > 20) |
| 987 | qt_fromlatin1_mips_asm_unroll8(dst, str, size); |
| 988 | else |
| 989 | qt_fromlatin1_mips_asm_unroll4(dst, str, size); |
| 990 | #else |
| 991 | while (size--) |
| 992 | *dst++ = (uchar)*str++; |
| 993 | #endif |
| 994 | } |
| 995 | |
| 996 | static QVarLengthArray<char16_t> qt_from_latin1_to_qvla(QLatin1StringView str) |
| 997 | { |
| 998 | const qsizetype len = str.size(); |
| 999 | QVarLengthArray<char16_t> arr(len); |
| 1000 | qt_from_latin1(dst: arr.data(), str: str.data(), size: len); |
| 1001 | return arr; |
| 1002 | } |
| 1003 | |
| 1004 | template <bool Checked> |
| 1005 | static void qt_to_latin1_internal(uchar *dst, const char16_t *src, qsizetype length) |
| 1006 | { |
| 1007 | #if defined(__SSE2__) |
| 1008 | auto questionMark256 = []() { |
| 1009 | if constexpr (UseAvx2) |
| 1010 | return _mm256_broadcastw_epi16(X: _mm_cvtsi32_si128(a: '?')); |
| 1011 | else |
| 1012 | return 0; |
| 1013 | }(); |
| 1014 | auto outOfRange256 = []() { |
| 1015 | if constexpr (UseAvx2) |
| 1016 | return _mm256_broadcastw_epi16(X: _mm_cvtsi32_si128(a: 0x100)); |
| 1017 | else |
| 1018 | return 0; |
| 1019 | }(); |
| 1020 | __m128i questionMark, outOfRange; |
| 1021 | if constexpr (UseAvx2) { |
| 1022 | questionMark = _mm256_castsi256_si128(questionMark256); |
| 1023 | outOfRange = _mm256_castsi256_si128(outOfRange256); |
| 1024 | } else { |
| 1025 | questionMark = _mm_set1_epi16(w: '?'); |
| 1026 | outOfRange = _mm_set1_epi16(w: 0x100); |
| 1027 | } |
| 1028 | |
| 1029 | auto mergeQuestionMarks = [=](__m128i chunk) { |
| 1030 | if (!Checked) |
| 1031 | return chunk; |
| 1032 | |
| 1033 | // SSE has no compare instruction for unsigned comparison. |
| 1034 | if constexpr (UseSse4_1) { |
| 1035 | // We use an unsigned uc = qMin(uc, 0x100) and then compare for equality. |
| 1036 | chunk = _mm_min_epu16(V1: chunk, V2: outOfRange); |
| 1037 | const __m128i offLimitMask = _mm_cmpeq_epi16(a: chunk, b: outOfRange); |
| 1038 | chunk = _mm_blendv_epi8(V1: chunk, V2: questionMark, M: offLimitMask); |
| 1039 | return chunk; |
| 1040 | } |
| 1041 | // The variables must be shiffted + 0x8000 to be compared |
| 1042 | const __m128i signedBitOffset = _mm_set1_epi16(w: short(0x8000)); |
| 1043 | const __m128i thresholdMask = _mm_set1_epi16(w: short(0xff + 0x8000)); |
| 1044 | |
| 1045 | const __m128i signedChunk = _mm_add_epi16(a: chunk, b: signedBitOffset); |
| 1046 | const __m128i offLimitMask = _mm_cmpgt_epi16(a: signedChunk, b: thresholdMask); |
| 1047 | |
| 1048 | // offLimitQuestionMark contains '?' for each 16 bits that was off-limit |
| 1049 | // the 16 bits that were correct contains zeros |
| 1050 | const __m128i offLimitQuestionMark = _mm_and_si128(a: offLimitMask, b: questionMark); |
| 1051 | |
| 1052 | // correctBytes contains the bytes that were in limit |
| 1053 | // the 16 bits that were off limits contains zeros |
| 1054 | const __m128i correctBytes = _mm_andnot_si128(a: offLimitMask, b: chunk); |
| 1055 | |
| 1056 | // merge offLimitQuestionMark and correctBytes to have the result |
| 1057 | chunk = _mm_or_si128(a: correctBytes, b: offLimitQuestionMark); |
| 1058 | |
| 1059 | Q_UNUSED(outOfRange); |
| 1060 | return chunk; |
| 1061 | }; |
| 1062 | |
| 1063 | // we're going to read to src[offset..offset+15] (16 bytes) |
| 1064 | auto loadChunkAt = [=](qptrdiff offset) { |
| 1065 | __m128i chunk1, chunk2; |
| 1066 | if constexpr (UseAvx2) { |
| 1067 | __m256i chunk = _mm256_loadu_si256(p: reinterpret_cast<const __m256i *>(src + offset)); |
| 1068 | if (Checked) { |
| 1069 | // See mergeQuestionMarks lambda above for details |
| 1070 | chunk = _mm256_min_epu16(chunk, outOfRange256); |
| 1071 | const __m256i offLimitMask = _mm256_cmpeq_epi16(chunk, outOfRange256); |
| 1072 | chunk = _mm256_blendv_epi8(chunk, questionMark256, offLimitMask); |
| 1073 | } |
| 1074 | |
| 1075 | chunk2 = _mm256_extracti128_si256(chunk, 1); |
| 1076 | chunk1 = _mm256_castsi256_si128(a: chunk); |
| 1077 | } else { |
| 1078 | chunk1 = _mm_loadu_si128(p: (const __m128i*)(src + offset)); // load |
| 1079 | chunk1 = mergeQuestionMarks(chunk1); |
| 1080 | |
| 1081 | chunk2 = _mm_loadu_si128(p: (const __m128i*)(src + offset + 8)); // load |
| 1082 | chunk2 = mergeQuestionMarks(chunk2); |
| 1083 | } |
| 1084 | |
| 1085 | // pack the two vector to 16 x 8bits elements |
| 1086 | return _mm_packus_epi16(a: chunk1, b: chunk2); |
| 1087 | }; |
| 1088 | |
| 1089 | if (size_t(length) >= sizeof(__m128i)) { |
| 1090 | // because of possible overlapping, we won't process the last chunk in the loop |
| 1091 | qptrdiff offset = 0; |
| 1092 | for ( ; offset + 2 * sizeof(__m128i) < size_t(length); offset += sizeof(__m128i)) |
| 1093 | _mm_storeu_si128(reinterpret_cast<__m128i *>(dst + offset), loadChunkAt(offset)); |
| 1094 | |
| 1095 | // overlapped conversion of the last full chunk and the tail |
| 1096 | __m128i last1 = loadChunkAt(offset); |
| 1097 | __m128i last2 = loadChunkAt(length - sizeof(__m128i)); |
| 1098 | _mm_storeu_si128(p: reinterpret_cast<__m128i *>(dst + offset), b: last1); |
| 1099 | _mm_storeu_si128(p: reinterpret_cast<__m128i *>(dst + length - sizeof(__m128i)), b: last2); |
| 1100 | return; |
| 1101 | } |
| 1102 | |
| 1103 | # if !defined(__OPTIMIZE_SIZE__) |
| 1104 | if (length >= 4) { |
| 1105 | // this code is fine even for in-place conversion because we load both |
| 1106 | // before any store |
| 1107 | if (length >= 8) { |
| 1108 | __m128i chunk1 = _mm_loadu_si128(p: reinterpret_cast<const __m128i *>(src)); |
| 1109 | __m128i chunk2 = _mm_loadu_si128(p: reinterpret_cast<const __m128i *>(src + length - 8)); |
| 1110 | chunk1 = mergeQuestionMarks(chunk1); |
| 1111 | chunk2 = mergeQuestionMarks(chunk2); |
| 1112 | |
| 1113 | // pack, where the upper half is ignored |
| 1114 | const __m128i result1 = _mm_packus_epi16(a: chunk1, b: chunk1); |
| 1115 | const __m128i result2 = _mm_packus_epi16(a: chunk2, b: chunk2); |
| 1116 | _mm_storel_epi64(p: reinterpret_cast<__m128i *>(dst), a: result1); |
| 1117 | _mm_storel_epi64(p: reinterpret_cast<__m128i *>(dst + length - 8), a: result2); |
| 1118 | } else { |
| 1119 | __m128i chunk1 = _mm_loadl_epi64(p: reinterpret_cast<const __m128i *>(src)); |
| 1120 | __m128i chunk2 = _mm_loadl_epi64(p: reinterpret_cast<const __m128i *>(src + length - 4)); |
| 1121 | chunk1 = mergeQuestionMarks(chunk1); |
| 1122 | chunk2 = mergeQuestionMarks(chunk2); |
| 1123 | |
| 1124 | // pack, we'll zero the upper three quarters |
| 1125 | const __m128i result1 = _mm_packus_epi16(a: chunk1, b: chunk1); |
| 1126 | const __m128i result2 = _mm_packus_epi16(a: chunk2, b: chunk2); |
| 1127 | qToUnaligned(src: _mm_cvtsi128_si32(a: result1), dest: dst); |
| 1128 | qToUnaligned(src: _mm_cvtsi128_si32(a: result2), dest: dst + length - 4); |
| 1129 | } |
| 1130 | return; |
| 1131 | } |
| 1132 | |
| 1133 | length = length % 4; |
| 1134 | return UnrollTailLoop<3>::exec(length, [=](qsizetype i) { |
| 1135 | if (Checked) |
| 1136 | dst[i] = (src[i]>0xff) ? '?' : (uchar) src[i]; |
| 1137 | else |
| 1138 | dst[i] = src[i]; |
| 1139 | }); |
| 1140 | # else |
| 1141 | length = length % 16; |
| 1142 | # endif // optimize size |
| 1143 | #elif defined(__ARM_NEON__) |
| 1144 | // Refer to the documentation of the SSE2 implementation. |
| 1145 | // This uses exactly the same method as for SSE except: |
| 1146 | // 1) neon has unsigned comparison |
| 1147 | // 2) packing is done to 64 bits (8 x 8bits component). |
| 1148 | if (length >= 16) { |
| 1149 | const qsizetype chunkCount = length >> 3; // divided by 8 |
| 1150 | const uint16x8_t questionMark = vdupq_n_u16('?'); // set |
| 1151 | const uint16x8_t thresholdMask = vdupq_n_u16(0xff); // set |
| 1152 | for (qsizetype i = 0; i < chunkCount; ++i) { |
| 1153 | uint16x8_t chunk = vld1q_u16((uint16_t *)src); // load |
| 1154 | src += 8; |
| 1155 | |
| 1156 | if (Checked) { |
| 1157 | const uint16x8_t offLimitMask = vcgtq_u16(chunk, thresholdMask); // chunk > thresholdMask |
| 1158 | const uint16x8_t offLimitQuestionMark = vandq_u16(offLimitMask, questionMark); // offLimitMask & questionMark |
| 1159 | const uint16x8_t correctBytes = vbicq_u16(chunk, offLimitMask); // !offLimitMask & chunk |
| 1160 | chunk = vorrq_u16(correctBytes, offLimitQuestionMark); // correctBytes | offLimitQuestionMark |
| 1161 | } |
| 1162 | const uint8x8_t result = vmovn_u16(chunk); // narrowing move->packing |
| 1163 | vst1_u8(dst, result); // store |
| 1164 | dst += 8; |
| 1165 | } |
| 1166 | length = length % 8; |
| 1167 | } |
| 1168 | #endif |
| 1169 | #if defined(__mips_dsp) |
| 1170 | static_assert(sizeof(qsizetype) == sizeof(int), |
| 1171 | "oops, the assembler implementation needs to be called in a loop" ); |
| 1172 | qt_toLatin1_mips_dsp_asm(dst, src, length); |
| 1173 | #else |
| 1174 | while (length--) { |
| 1175 | if (Checked) |
| 1176 | *dst++ = (*src>0xff) ? '?' : (uchar) *src; |
| 1177 | else |
| 1178 | *dst++ = *src; |
| 1179 | ++src; |
| 1180 | } |
| 1181 | #endif |
| 1182 | } |
| 1183 | |
| 1184 | void qt_to_latin1(uchar *dst, const char16_t *src, qsizetype length) |
| 1185 | { |
| 1186 | qt_to_latin1_internal<true>(dst, src, length); |
| 1187 | } |
| 1188 | |
| 1189 | void qt_to_latin1_unchecked(uchar *dst, const char16_t *src, qsizetype length) |
| 1190 | { |
| 1191 | qt_to_latin1_internal<false>(dst, src, length); |
| 1192 | } |
| 1193 | |
| 1194 | // Unicode case-insensitive comparison (argument order matches QStringView) |
| 1195 | Q_NEVER_INLINE static int ucstricmp(qsizetype alen, const char16_t *a, qsizetype blen, const char16_t *b) |
| 1196 | { |
| 1197 | if (a == b) |
| 1198 | return qt_lencmp(lhs: alen, rhs: blen); |
| 1199 | |
| 1200 | char32_t alast = 0; |
| 1201 | char32_t blast = 0; |
| 1202 | qsizetype l = qMin(a: alen, b: blen); |
| 1203 | qsizetype i; |
| 1204 | for (i = 0; i < l; ++i) { |
| 1205 | // qDebug() << Qt::hex << alast << blast; |
| 1206 | // qDebug() << Qt::hex << "*a=" << *a << "alast=" << alast << "folded=" << foldCase (*a, alast); |
| 1207 | // qDebug() << Qt::hex << "*b=" << *b << "blast=" << blast << "folded=" << foldCase (*b, blast); |
| 1208 | int diff = foldCase(ch: a[i], last&: alast) - foldCase(ch: b[i], last&: blast); |
| 1209 | if ((diff)) |
| 1210 | return diff; |
| 1211 | } |
| 1212 | if (i == alen) { |
| 1213 | if (i == blen) |
| 1214 | return 0; |
| 1215 | return -1; |
| 1216 | } |
| 1217 | return 1; |
| 1218 | } |
| 1219 | |
| 1220 | // Case-insensitive comparison between a QStringView and a QLatin1StringView |
| 1221 | // (argument order matches those types) |
| 1222 | Q_NEVER_INLINE static int ucstricmp(qsizetype alen, const char16_t *a, qsizetype blen, const char *b) |
| 1223 | { |
| 1224 | qsizetype l = qMin(a: alen, b: blen); |
| 1225 | qsizetype i; |
| 1226 | for (i = 0; i < l; ++i) { |
| 1227 | int diff = foldCase(ch: a[i]) - foldCase(ch: char16_t{uchar(b[i])}); |
| 1228 | if ((diff)) |
| 1229 | return diff; |
| 1230 | } |
| 1231 | if (i == alen) { |
| 1232 | if (i == blen) |
| 1233 | return 0; |
| 1234 | return -1; |
| 1235 | } |
| 1236 | return 1; |
| 1237 | } |
| 1238 | |
| 1239 | // Case-insensitive comparison between a Unicode string and a UTF-8 string |
| 1240 | Q_NEVER_INLINE static int ucstricmp8(const char *utf8, const char *utf8end, const QChar *utf16, const QChar *utf16end) |
| 1241 | { |
| 1242 | auto src1 = reinterpret_cast<const qchar8_t *>(utf8); |
| 1243 | auto end1 = reinterpret_cast<const qchar8_t *>(utf8end); |
| 1244 | QStringIterator src2(utf16, utf16end); |
| 1245 | |
| 1246 | while (src1 < end1 && src2.hasNext()) { |
| 1247 | char32_t uc1 = QChar::toCaseFolded(ucs4: QUtf8Functions::nextUcs4FromUtf8(src&: src1, end: end1)); |
| 1248 | char32_t uc2 = QChar::toCaseFolded(ucs4: src2.next()); |
| 1249 | int diff = uc1 - uc2; // can't underflow |
| 1250 | if (diff) |
| 1251 | return diff; |
| 1252 | } |
| 1253 | |
| 1254 | // the shorter string sorts first |
| 1255 | return (end1 > src1) - int(src2.hasNext()); |
| 1256 | } |
| 1257 | |
| 1258 | #if defined(__mips_dsp) |
| 1259 | // From qstring_mips_dsp_asm.S |
| 1260 | extern "C" int qt_ucstrncmp_mips_dsp_asm(const char16_t *a, |
| 1261 | const char16_t *b, |
| 1262 | unsigned len); |
| 1263 | #endif |
| 1264 | |
| 1265 | // Unicode case-sensitive compare two same-sized strings |
| 1266 | template <StringComparisonMode Mode> |
| 1267 | static int ucstrncmp(const char16_t *a, const char16_t *b, size_t l) |
| 1268 | { |
| 1269 | // This function isn't memcmp() because that can return the wrong sorting |
| 1270 | // result in little-endian architectures: 0x00ff must sort before 0x0100, |
| 1271 | // but the bytes in memory are FF 00 and 00 01. |
| 1272 | |
| 1273 | #ifndef __OPTIMIZE_SIZE__ |
| 1274 | # if defined(__mips_dsp) |
| 1275 | static_assert(sizeof(uint) == sizeof(size_t)); |
| 1276 | if (l >= 8) { |
| 1277 | return qt_ucstrncmp_mips_dsp_asm(a, b, l); |
| 1278 | } |
| 1279 | # elif defined(__SSE2__) |
| 1280 | return ucstrncmp_sse2<Mode>(a, b, l); |
| 1281 | # elif defined(__ARM_NEON__) |
| 1282 | if (l >= 8) { |
| 1283 | const char16_t *end = a + l; |
| 1284 | const uint16x8_t mask = qvsetq_n_u16( 1, 1 << 1, 1 << 2, 1 << 3, 1 << 4, 1 << 5, 1 << 6, 1 << 7 ); |
| 1285 | while (end - a > 7) { |
| 1286 | uint16x8_t da = vld1q_u16(reinterpret_cast<const uint16_t *>(a)); |
| 1287 | uint16x8_t db = vld1q_u16(reinterpret_cast<const uint16_t *>(b)); |
| 1288 | |
| 1289 | uint8_t r = ~(uint8_t)vaddvq_u16(vandq_u16(vceqq_u16(da, db), mask)); |
| 1290 | if (r) { |
| 1291 | // found a different QChar |
| 1292 | if (Mode == CompareStringsForEquality) |
| 1293 | return 1; |
| 1294 | uint idx = qCountTrailingZeroBits(r); |
| 1295 | return a[idx] - b[idx]; |
| 1296 | } |
| 1297 | a += 8; |
| 1298 | b += 8; |
| 1299 | } |
| 1300 | l &= 7; |
| 1301 | } |
| 1302 | const auto lambda = [=](size_t i) -> int { |
| 1303 | return a[i] - b[i]; |
| 1304 | }; |
| 1305 | return UnrollTailLoop<7>::exec(l, 0, lambda, lambda); |
| 1306 | # endif // MIPS DSP or __SSE2__ or __ARM_NEON__ |
| 1307 | #endif // __OPTIMIZE_SIZE__ |
| 1308 | |
| 1309 | if (Mode == CompareStringsForEquality || QSysInfo::ByteOrder == QSysInfo::BigEndian) |
| 1310 | return memcmp(s1: a, s2: b, n: l * sizeof(char16_t)); |
| 1311 | |
| 1312 | for (size_t i = 0; i < l; ++i) { |
| 1313 | if (int diff = a[i] - b[i]) |
| 1314 | return diff; |
| 1315 | } |
| 1316 | return 0; |
| 1317 | } |
| 1318 | |
| 1319 | template <StringComparisonMode Mode> |
| 1320 | static int ucstrncmp(const char16_t *a, const char *b, size_t l) |
| 1321 | { |
| 1322 | const uchar *c = reinterpret_cast<const uchar *>(b); |
| 1323 | const char16_t *uc = a; |
| 1324 | const char16_t *e = uc + l; |
| 1325 | |
| 1326 | #if defined(__SSE2__) && !defined(__OPTIMIZE_SIZE__) |
| 1327 | return ucstrncmp_sse2<Mode>(uc, c, l); |
| 1328 | #endif |
| 1329 | |
| 1330 | while (uc < e) { |
| 1331 | int diff = *uc - *c; |
| 1332 | if (diff) |
| 1333 | return diff; |
| 1334 | uc++, c++; |
| 1335 | } |
| 1336 | |
| 1337 | return 0; |
| 1338 | } |
| 1339 | |
| 1340 | // Unicode case-sensitive equality |
| 1341 | template <typename Char2> |
| 1342 | static bool ucstreq(const char16_t *a, size_t alen, const Char2 *b) |
| 1343 | { |
| 1344 | return ucstrncmp<CompareStringsForEquality>(a, b, alen) == 0; |
| 1345 | } |
| 1346 | |
| 1347 | // Unicode case-sensitive comparison |
| 1348 | template <typename Char2> |
| 1349 | static int ucstrcmp(const char16_t *a, size_t alen, const Char2 *b, size_t blen) |
| 1350 | { |
| 1351 | const size_t l = qMin(a: alen, b: blen); |
| 1352 | int cmp = ucstrncmp<CompareStringsForOrdering>(a, b, l); |
| 1353 | return cmp ? cmp : qt_lencmp(lhs: alen, rhs: blen); |
| 1354 | } |
| 1355 | |
| 1356 | using CaseInsensitiveL1 = QtPrivate::QCaseInsensitiveLatin1Hash; |
| 1357 | |
| 1358 | static int latin1nicmp(const char *lhsChar, qsizetype lSize, const char *rhsChar, qsizetype rSize) |
| 1359 | { |
| 1360 | // We're called with QLatin1StringView's .data() and .size(): |
| 1361 | Q_ASSERT(lSize >= 0 && rSize >= 0); |
| 1362 | if (!lSize) |
| 1363 | return rSize ? -1 : 0; |
| 1364 | if (!rSize) |
| 1365 | return 1; |
| 1366 | const qsizetype size = std::min(a: lSize, b: rSize); |
| 1367 | |
| 1368 | Q_ASSERT(lhsChar && rhsChar); // since both lSize and rSize are positive |
| 1369 | for (qsizetype i = 0; i < size; i++) { |
| 1370 | if (int res = CaseInsensitiveL1::difference(lhs: lhsChar[i], rhs: rhsChar[i])) |
| 1371 | return res; |
| 1372 | } |
| 1373 | return qt_lencmp(lhs: lSize, rhs: rSize); |
| 1374 | } |
| 1375 | |
| 1376 | bool QtPrivate::equalStrings(QStringView lhs, QStringView rhs) noexcept |
| 1377 | { |
| 1378 | Q_ASSERT(lhs.size() == rhs.size()); |
| 1379 | return ucstreq(a: lhs.utf16(), alen: lhs.size(), b: rhs.utf16()); |
| 1380 | } |
| 1381 | |
| 1382 | bool QtPrivate::equalStrings(QStringView lhs, QLatin1StringView rhs) noexcept |
| 1383 | { |
| 1384 | Q_ASSERT(lhs.size() == rhs.size()); |
| 1385 | return ucstreq(a: lhs.utf16(), alen: lhs.size(), b: rhs.latin1()); |
| 1386 | } |
| 1387 | |
| 1388 | bool QtPrivate::equalStrings(QLatin1StringView lhs, QStringView rhs) noexcept |
| 1389 | { |
| 1390 | return QtPrivate::equalStrings(lhs: rhs, rhs: lhs); |
| 1391 | } |
| 1392 | |
| 1393 | bool QtPrivate::equalStrings(QLatin1StringView lhs, QLatin1StringView rhs) noexcept |
| 1394 | { |
| 1395 | Q_ASSERT(lhs.size() == rhs.size()); |
| 1396 | return (!lhs.size() || memcmp(s1: lhs.data(), s2: rhs.data(), n: lhs.size()) == 0); |
| 1397 | } |
| 1398 | |
| 1399 | bool QtPrivate::equalStrings(QBasicUtf8StringView<false> lhs, QStringView rhs) noexcept |
| 1400 | { |
| 1401 | return QUtf8::compareUtf8(utf8: lhs, utf16: rhs) == 0; |
| 1402 | } |
| 1403 | |
| 1404 | bool QtPrivate::equalStrings(QStringView lhs, QBasicUtf8StringView<false> rhs) noexcept |
| 1405 | { |
| 1406 | return QtPrivate::equalStrings(lhs: rhs, rhs: lhs); |
| 1407 | } |
| 1408 | |
| 1409 | bool QtPrivate::equalStrings(QLatin1StringView lhs, QBasicUtf8StringView<false> rhs) noexcept |
| 1410 | { |
| 1411 | return QUtf8::compareUtf8(utf8: QByteArrayView(rhs), s: lhs) == 0; |
| 1412 | } |
| 1413 | |
| 1414 | bool QtPrivate::equalStrings(QBasicUtf8StringView<false> lhs, QLatin1StringView rhs) noexcept |
| 1415 | { |
| 1416 | return QtPrivate::equalStrings(lhs: rhs, rhs: lhs); |
| 1417 | } |
| 1418 | |
| 1419 | bool QtPrivate::equalStrings(QBasicUtf8StringView<false> lhs, QBasicUtf8StringView<false> rhs) noexcept |
| 1420 | { |
| 1421 | #if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0) || defined(QT_BOOTSTRAPPED) || defined(QT_STATIC) |
| 1422 | Q_ASSERT(lhs.size() == rhs.size()); |
| 1423 | #else |
| 1424 | // operator== didn't enforce size prior to Qt 6.2 |
| 1425 | if (lhs.size() != rhs.size()) |
| 1426 | return false; |
| 1427 | #endif |
| 1428 | return (!lhs.size() || memcmp(s1: lhs.data(), s2: rhs.data(), n: lhs.size()) == 0); |
| 1429 | } |
| 1430 | |
| 1431 | bool QAnyStringView::equal(QAnyStringView lhs, QAnyStringView rhs) noexcept |
| 1432 | { |
| 1433 | if (lhs.size() != rhs.size() && lhs.isUtf8() == rhs.isUtf8()) |
| 1434 | return false; |
| 1435 | return lhs.visit(v: [rhs](auto lhs) { |
| 1436 | return rhs.visit([lhs](auto rhs) { |
| 1437 | return QtPrivate::equalStrings(lhs, rhs); |
| 1438 | }); |
| 1439 | }); |
| 1440 | } |
| 1441 | |
| 1442 | /*! |
| 1443 | \relates QStringView |
| 1444 | \internal |
| 1445 | \since 5.10 |
| 1446 | |
| 1447 | Returns an integer that compares to 0 as \a lhs compares to \a rhs. |
| 1448 | |
| 1449 | \include qstring.qdocinc {search-comparison-case-sensitivity} {comparison} |
| 1450 | |
| 1451 | Case-sensitive comparison is based exclusively on the numeric Unicode values |
| 1452 | of the characters and is very fast, but is not what a human would expect. |
| 1453 | Consider sorting user-visible strings with QString::localeAwareCompare(). |
| 1454 | |
| 1455 | \sa {Comparing Strings} |
| 1456 | */ |
| 1457 | int QtPrivate::compareStrings(QStringView lhs, QStringView rhs, Qt::CaseSensitivity cs) noexcept |
| 1458 | { |
| 1459 | if (cs == Qt::CaseSensitive) |
| 1460 | return ucstrcmp(a: lhs.utf16(), alen: lhs.size(), b: rhs.utf16(), blen: rhs.size()); |
| 1461 | return ucstricmp(alen: lhs.size(), a: lhs.utf16(), blen: rhs.size(), b: rhs.utf16()); |
| 1462 | } |
| 1463 | |
| 1464 | /*! |
| 1465 | \relates QStringView |
| 1466 | \internal |
| 1467 | \since 5.10 |
| 1468 | \overload |
| 1469 | |
| 1470 | Returns an integer that compares to 0 as \a lhs compares to \a rhs. |
| 1471 | |
| 1472 | \include qstring.qdocinc {search-comparison-case-sensitivity} {comparison} |
| 1473 | |
| 1474 | Case-sensitive comparison is based exclusively on the numeric Unicode values |
| 1475 | of the characters and is very fast, but is not what a human would expect. |
| 1476 | Consider sorting user-visible strings with QString::localeAwareCompare(). |
| 1477 | |
| 1478 | \sa {Comparing Strings} |
| 1479 | */ |
| 1480 | int QtPrivate::compareStrings(QStringView lhs, QLatin1StringView rhs, Qt::CaseSensitivity cs) noexcept |
| 1481 | { |
| 1482 | if (cs == Qt::CaseSensitive) |
| 1483 | return ucstrcmp(a: lhs.utf16(), alen: lhs.size(), b: rhs.latin1(), blen: rhs.size()); |
| 1484 | return ucstricmp(alen: lhs.size(), a: lhs.utf16(), blen: rhs.size(), b: rhs.latin1()); |
| 1485 | } |
| 1486 | |
| 1487 | /*! |
| 1488 | \relates QStringView |
| 1489 | \internal |
| 1490 | \since 6.0 |
| 1491 | \overload |
| 1492 | */ |
| 1493 | int QtPrivate::compareStrings(QStringView lhs, QBasicUtf8StringView<false> rhs, Qt::CaseSensitivity cs) noexcept |
| 1494 | { |
| 1495 | return -compareStrings(lhs: rhs, rhs: lhs, cs); |
| 1496 | } |
| 1497 | |
| 1498 | /*! |
| 1499 | \relates QStringView |
| 1500 | \internal |
| 1501 | \since 5.10 |
| 1502 | \overload |
| 1503 | */ |
| 1504 | int QtPrivate::compareStrings(QLatin1StringView lhs, QStringView rhs, Qt::CaseSensitivity cs) noexcept |
| 1505 | { |
| 1506 | return -compareStrings(lhs: rhs, rhs: lhs, cs); |
| 1507 | } |
| 1508 | |
| 1509 | /*! |
| 1510 | \relates QStringView |
| 1511 | \internal |
| 1512 | \since 5.10 |
| 1513 | \overload |
| 1514 | |
| 1515 | Returns an integer that compares to 0 as \a lhs compares to \a rhs. |
| 1516 | |
| 1517 | \include qstring.qdocinc {search-comparison-case-sensitivity} {comparison} |
| 1518 | |
| 1519 | Case-sensitive comparison is based exclusively on the numeric Latin-1 values |
| 1520 | of the characters and is very fast, but is not what a human would expect. |
| 1521 | Consider sorting user-visible strings with QString::localeAwareCompare(). |
| 1522 | |
| 1523 | \sa {Comparing Strings} |
| 1524 | */ |
| 1525 | int QtPrivate::compareStrings(QLatin1StringView lhs, QLatin1StringView rhs, Qt::CaseSensitivity cs) noexcept |
| 1526 | { |
| 1527 | if (lhs.isEmpty()) |
| 1528 | return qt_lencmp(lhs: qsizetype(0), rhs: rhs.size()); |
| 1529 | if (rhs.isEmpty()) |
| 1530 | return qt_lencmp(lhs: lhs.size(), rhs: qsizetype(0)); |
| 1531 | if (cs == Qt::CaseInsensitive) |
| 1532 | return latin1nicmp(lhsChar: lhs.data(), lSize: lhs.size(), rhsChar: rhs.data(), rSize: rhs.size()); |
| 1533 | const auto l = std::min(a: lhs.size(), b: rhs.size()); |
| 1534 | int r = memcmp(s1: lhs.data(), s2: rhs.data(), n: l); |
| 1535 | return r ? r : qt_lencmp(lhs: lhs.size(), rhs: rhs.size()); |
| 1536 | } |
| 1537 | |
| 1538 | /*! |
| 1539 | \relates QStringView |
| 1540 | \internal |
| 1541 | \since 6.0 |
| 1542 | \overload |
| 1543 | */ |
| 1544 | int QtPrivate::compareStrings(QLatin1StringView lhs, QBasicUtf8StringView<false> rhs, Qt::CaseSensitivity cs) noexcept |
| 1545 | { |
| 1546 | return -QUtf8::compareUtf8(utf8: QByteArrayView(rhs), s: lhs, cs); |
| 1547 | } |
| 1548 | |
| 1549 | /*! |
| 1550 | \relates QStringView |
| 1551 | \internal |
| 1552 | \since 6.0 |
| 1553 | \overload |
| 1554 | */ |
| 1555 | int QtPrivate::compareStrings(QBasicUtf8StringView<false> lhs, QStringView rhs, Qt::CaseSensitivity cs) noexcept |
| 1556 | { |
| 1557 | if (cs == Qt::CaseSensitive) |
| 1558 | return QUtf8::compareUtf8(utf8: lhs, utf16: rhs); |
| 1559 | return ucstricmp8(utf8: lhs.begin(), utf8end: lhs.end(), utf16: rhs.begin(), utf16end: rhs.end()); |
| 1560 | } |
| 1561 | |
| 1562 | /*! |
| 1563 | \relates QStringView |
| 1564 | \internal |
| 1565 | \since 6.0 |
| 1566 | \overload |
| 1567 | */ |
| 1568 | int QtPrivate::compareStrings(QBasicUtf8StringView<false> lhs, QLatin1StringView rhs, Qt::CaseSensitivity cs) noexcept |
| 1569 | { |
| 1570 | return -compareStrings(lhs: rhs, rhs: lhs, cs); |
| 1571 | } |
| 1572 | |
| 1573 | /*! |
| 1574 | \relates QStringView |
| 1575 | \internal |
| 1576 | \since 6.0 |
| 1577 | \overload |
| 1578 | */ |
| 1579 | int QtPrivate::compareStrings(QBasicUtf8StringView<false> lhs, QBasicUtf8StringView<false> rhs, Qt::CaseSensitivity cs) noexcept |
| 1580 | { |
| 1581 | return QUtf8::compareUtf8(lhs: QByteArrayView(lhs), rhs: QByteArrayView(rhs), cs); |
| 1582 | } |
| 1583 | |
| 1584 | int QAnyStringView::compare(QAnyStringView lhs, QAnyStringView rhs, Qt::CaseSensitivity cs) noexcept |
| 1585 | { |
| 1586 | return lhs.visit(v: [rhs, cs](auto lhs) { |
| 1587 | return rhs.visit([lhs, cs](auto rhs) { |
| 1588 | return QtPrivate::compareStrings(lhs, rhs, cs); |
| 1589 | }); |
| 1590 | }); |
| 1591 | } |
| 1592 | |
| 1593 | // ### Qt 7: do not allow anything but ASCII digits |
| 1594 | // in arg()'s replacements. |
| 1595 | #if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) && !defined(QT_BOOTSTRAPPED) |
| 1596 | static bool supportUnicodeDigitValuesInArg() |
| 1597 | { |
| 1598 | static const bool result = []() { |
| 1599 | static const char supportUnicodeDigitValuesEnvVar[] |
| 1600 | = "QT_USE_UNICODE_DIGIT_VALUES_IN_STRING_ARG" ; |
| 1601 | |
| 1602 | if (qEnvironmentVariableIsSet(varName: supportUnicodeDigitValuesEnvVar)) |
| 1603 | return qEnvironmentVariableIntValue(varName: supportUnicodeDigitValuesEnvVar) != 0; |
| 1604 | |
| 1605 | #if QT_VERSION < QT_VERSION_CHECK(6, 6, 0) // keep it in sync with the test |
| 1606 | return true; |
| 1607 | #else |
| 1608 | return false; |
| 1609 | #endif |
| 1610 | }(); |
| 1611 | |
| 1612 | return result; |
| 1613 | } |
| 1614 | #endif |
| 1615 | |
| 1616 | static int qArgDigitValue(QChar ch) noexcept |
| 1617 | { |
| 1618 | #if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) && !defined(QT_BOOTSTRAPPED) |
| 1619 | if (supportUnicodeDigitValuesInArg()) |
| 1620 | return ch.digitValue(); |
| 1621 | #endif |
| 1622 | if (ch >= u'0' && ch <= u'9') |
| 1623 | return int(ch.unicode() - u'0'); |
| 1624 | return -1; |
| 1625 | } |
| 1626 | |
| 1627 | #if QT_CONFIG(regularexpression) |
| 1628 | Q_DECL_COLD_FUNCTION |
| 1629 | static void qtWarnAboutInvalidRegularExpression(const QRegularExpression &re, const char *cls, const char *method) |
| 1630 | { |
| 1631 | extern void qtWarnAboutInvalidRegularExpression(const QString &pattern, const char *cls, const char *method); |
| 1632 | qtWarnAboutInvalidRegularExpression(pattern: re.pattern(), cls, method); |
| 1633 | } |
| 1634 | #endif |
| 1635 | |
| 1636 | /*! |
| 1637 | \macro QT_RESTRICTED_CAST_FROM_ASCII |
| 1638 | \relates QString |
| 1639 | |
| 1640 | Disables most automatic conversions from source literals and 8-bit data |
| 1641 | to unicode QStrings, but allows the use of |
| 1642 | the \c{QChar(char)} and \c{QString(const char (&ch)[N]} constructors, |
| 1643 | and the \c{QString::operator=(const char (&ch)[N])} assignment operator. |
| 1644 | This gives most of the type-safety benefits of \l QT_NO_CAST_FROM_ASCII |
| 1645 | but does not require user code to wrap character and string literals |
| 1646 | with QLatin1Char, QLatin1StringView or similar. |
| 1647 | |
| 1648 | Using this macro together with source strings outside the 7-bit range, |
| 1649 | non-literals, or literals with embedded NUL characters is undefined. |
| 1650 | |
| 1651 | \sa QT_NO_CAST_FROM_ASCII, QT_NO_CAST_TO_ASCII |
| 1652 | */ |
| 1653 | |
| 1654 | /*! |
| 1655 | \macro QT_NO_CAST_FROM_ASCII |
| 1656 | \relates QString |
| 1657 | \relates QChar |
| 1658 | |
| 1659 | Disables automatic conversions from 8-bit strings (\c{char *}) to Unicode |
| 1660 | QStrings, as well as from 8-bit \c{char} types (\c{char} and |
| 1661 | \c{unsigned char}) to QChar. |
| 1662 | |
| 1663 | \sa QT_NO_CAST_TO_ASCII, QT_RESTRICTED_CAST_FROM_ASCII, |
| 1664 | QT_NO_CAST_FROM_BYTEARRAY |
| 1665 | */ |
| 1666 | |
| 1667 | /*! |
| 1668 | \macro QT_NO_CAST_TO_ASCII |
| 1669 | \relates QString |
| 1670 | |
| 1671 | Disables automatic conversion from QString to 8-bit strings (\c{char *}). |
| 1672 | |
| 1673 | \sa QT_NO_CAST_FROM_ASCII, QT_RESTRICTED_CAST_FROM_ASCII, |
| 1674 | QT_NO_CAST_FROM_BYTEARRAY |
| 1675 | */ |
| 1676 | |
| 1677 | /*! |
| 1678 | \macro QT_ASCII_CAST_WARNINGS |
| 1679 | \internal |
| 1680 | \relates QString |
| 1681 | |
| 1682 | This macro can be defined to force a warning whenever a function is |
| 1683 | called that automatically converts between unicode and 8-bit encodings. |
| 1684 | |
| 1685 | Note: This only works for compilers that support warnings for |
| 1686 | deprecated API. |
| 1687 | |
| 1688 | \sa QT_NO_CAST_TO_ASCII, QT_NO_CAST_FROM_ASCII, QT_RESTRICTED_CAST_FROM_ASCII |
| 1689 | */ |
| 1690 | |
| 1691 | /*! |
| 1692 | \class QString |
| 1693 | \inmodule QtCore |
| 1694 | \reentrant |
| 1695 | |
| 1696 | \brief The QString class provides a Unicode character string. |
| 1697 | |
| 1698 | \ingroup tools |
| 1699 | \ingroup shared |
| 1700 | \ingroup string-processing |
| 1701 | |
| 1702 | \compares strong |
| 1703 | \compareswith strong QChar QLatin1StringView {const char16_t *} \ |
| 1704 | QStringView QUtf8StringView |
| 1705 | \endcompareswith |
| 1706 | \compareswith strong QByteArray QByteArrayView {const char *} |
| 1707 | When comparing with byte arrays, their content is interpreted as UTF-8. |
| 1708 | \endcompareswith |
| 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 | that is, 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 are easy to translate if you want to expand |
| 1730 | your application's market at some point. Two prominent cases |
| 1731 | where QByteArray is appropriate are when you need to store raw |
| 1732 | binary data, and when memory conservation is critical (like in |
| 1733 | embedded systems). |
| 1734 | |
| 1735 | \section1 Initializing a string |
| 1736 | |
| 1737 | One way to initialize a QString is to pass a \c{const char |
| 1738 | *} to its constructor. For example, the following code creates a |
| 1739 | QString of size 5 containing the data "Hello": |
| 1740 | |
| 1741 | \snippet qstring/main.cpp 0 |
| 1742 | |
| 1743 | QString converts the \c{const char *} data into Unicode using the |
| 1744 | fromUtf8() function. |
| 1745 | |
| 1746 | In all of the QString functions that take \c{const char *} |
| 1747 | parameters, the \c{const char *} is interpreted as a classic |
| 1748 | C-style \c{'\\0'}-terminated string. Except where the function's |
| 1749 | name overtly indicates some other encoding, such \c{const char *} |
| 1750 | parameters are assumed to be encoded in UTF-8. |
| 1751 | |
| 1752 | You can also provide string data as an array of \l{QChar}s: |
| 1753 | |
| 1754 | \snippet qstring/main.cpp 1 |
| 1755 | |
| 1756 | QString makes a deep copy of the QChar data, so you can modify it |
| 1757 | later without experiencing side effects. You can avoid taking a |
| 1758 | deep copy of the character data by using QStringView or |
| 1759 | QString::fromRawData() instead. |
| 1760 | |
| 1761 | Another approach is to set the size of the string using resize() |
| 1762 | and to initialize the data character per character. QString uses |
| 1763 | 0-based indexes, just like C++ arrays. To access the character at |
| 1764 | a particular index position, you can use \l operator[](). On |
| 1765 | non-\c{const} strings, \l operator[]() returns a reference to a |
| 1766 | character that can be used on the left side of an assignment. For |
| 1767 | example: |
| 1768 | |
| 1769 | \snippet qstring/main.cpp 2 |
| 1770 | |
| 1771 | For read-only access, an alternative syntax is to use the at() |
| 1772 | function: |
| 1773 | |
| 1774 | \snippet qstring/main.cpp 3 |
| 1775 | |
| 1776 | The at() function can be faster than \l operator[]() because it |
| 1777 | never causes a \l{deep copy} to occur. Alternatively, use the |
| 1778 | first(), last(), or sliced() functions to extract several characters |
| 1779 | at a time. |
| 1780 | |
| 1781 | A QString can embed '\\0' characters (QChar::Null). The size() |
| 1782 | function always returns the size of the whole string, including |
| 1783 | embedded '\\0' characters. |
| 1784 | |
| 1785 | After a call to the resize() function, newly allocated characters |
| 1786 | have undefined values. To set all the characters in the string to |
| 1787 | a particular value, use the fill() function. |
| 1788 | |
| 1789 | QString provides dozens of overloads designed to simplify string |
| 1790 | usage. For example, if you want to compare a QString with a string |
| 1791 | literal, you can write code like this and it will work as expected: |
| 1792 | |
| 1793 | \snippet qstring/main.cpp 4 |
| 1794 | |
| 1795 | You can also pass string literals to functions that take QStrings |
| 1796 | as arguments, invoking the QString(const char *) |
| 1797 | constructor. Similarly, you can pass a QString to a function that |
| 1798 | takes a \c{const char *} argument using the \l qPrintable() macro, |
| 1799 | which returns the given QString as a \c{const char *}. This is |
| 1800 | equivalent to calling toLocal8Bit().\l{QByteArray::}{constData()} |
| 1801 | on the QString. |
| 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 | QString may reallocate the memory in which it holds its data. 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 significantly 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. Do not call non-const |
| 1848 | functions while keeping iterators. Accessing an iterator or reference |
| 1849 | after it has been invalidated leads to undefined behavior. See the |
| 1850 | \l{Implicit sharing iterator problem} section for more information. |
| 1851 | |
| 1852 | A frequent requirement is to remove or simplify the spacing between |
| 1853 | visible characters in a string. The characters that make up that spacing |
| 1854 | are those for which \l {QChar::}{isSpace()} returns \c true, such as |
| 1855 | the simple space \c{' '}, the horizontal tab \c{'\\t'} and the newline \c{'\\n'}. |
| 1856 | To obtain a copy of a string leaving out any spacing from its start and end, |
| 1857 | use \l trimmed(). To also replace each sequence of spacing characters within |
| 1858 | the string with a simple space, \c{' '}, use \l 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, the latter searches backward. |
| 1863 | Either can be told an index position from which to start their search. |
| 1864 | Each returns the index position of the character or substring if they |
| 1865 | find it; otherwise, they return -1. For example, here is a typical loop |
| 1866 | that finds all 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 uppercase 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 filtered list |
| 1882 | from a string list by selecting the entries in it that contain a |
| 1883 | particular substring or match a particular QRegularExpression. |
| 1884 | See QStringList::filter() for details. |
| 1885 | |
| 1886 | \section1 Querying string data |
| 1887 | |
| 1888 | To see if a QString starts or ends with a particular substring, use |
| 1889 | startsWith() or endsWith(). To check whether a QString contains a |
| 1890 | specific character or substring, use the contains() function. To |
| 1891 | find out how many times a particular character or substring occurs |
| 1892 | in a string, use count(). |
| 1893 | |
| 1894 | To obtain a pointer to the actual character data, call data() or |
| 1895 | constData(). These functions return a pointer to the beginning of |
| 1896 | the QChar data. The pointer is guaranteed to remain valid until a |
| 1897 | non-\c{const} function is called on the QString. |
| 1898 | |
| 1899 | \section2 Comparing strings |
| 1900 | |
| 1901 | QStrings can be compared using overloaded operators such as \l |
| 1902 | operator<(), \l operator<=(), \l operator==(), \l operator>=(), |
| 1903 | and so on. The comparison is based exclusively on the lexicographical |
| 1904 | order of the two strings, seen as sequences of UTF-16 code units. |
| 1905 | It is very fast but is not what a human would expect; the |
| 1906 | QString::localeAwareCompare() function is usually a better choice for |
| 1907 | sorting user-interface strings, when such a comparison is available. |
| 1908 | |
| 1909 | When Qt is linked with the ICU library (which it usually is), its |
| 1910 | locale-aware sorting is used. Otherwise, platform-specific solutions |
| 1911 | are used: |
| 1912 | \list |
| 1913 | \li On Windows, localeAwareCompare() uses the current user locale, |
| 1914 | as set in the \uicontrol{regional} and \uicontrol{language} |
| 1915 | options portion of \uicontrol{Control Panel}. |
| 1916 | \li On \macos and iOS, \l localeAwareCompare() compares according |
| 1917 | to the \uicontrol{Order for sorted lists} setting in the |
| 1918 | \uicontrol{International preferences} panel. |
| 1919 | \li On other Unix-like systems, the comparison falls back to the |
| 1920 | system library's \c strcoll(). |
| 1921 | \endlist |
| 1922 | |
| 1923 | \section1 Converting between encoded string data and QString |
| 1924 | |
| 1925 | QString provides the following functions that return a |
| 1926 | \c{const char *} version of the string as QByteArray: toUtf8(), |
| 1927 | toLatin1(), and toLocal8Bit(). |
| 1928 | |
| 1929 | \list |
| 1930 | \li toLatin1() returns a Latin-1 (ISO 8859-1) encoded 8-bit string. |
| 1931 | \li toUtf8() returns a UTF-8 encoded 8-bit string. UTF-8 is a |
| 1932 | superset of US-ASCII (ANSI X3.4-1986) that supports the entire |
| 1933 | Unicode character set through multibyte sequences. |
| 1934 | \li toLocal8Bit() returns an 8-bit string using the system's local |
| 1935 | encoding. This is the same as toUtf8() on Unix systems. |
| 1936 | \endlist |
| 1937 | |
| 1938 | To convert from one of these encodings, QString provides |
| 1939 | fromLatin1(), fromUtf8(), and fromLocal8Bit(). Other |
| 1940 | encodings are supported through the QStringEncoder and QStringDecoder |
| 1941 | classes. |
| 1942 | |
| 1943 | As mentioned above, QString provides a lot of functions and |
| 1944 | operators that make it easy to interoperate with \c{const char *} |
| 1945 | strings. But this functionality is a double-edged sword: It makes |
| 1946 | QString more convenient to use if all strings are US-ASCII or |
| 1947 | Latin-1, but there is always the risk that an implicit conversion |
| 1948 | from or to \c{const char *} is done using the wrong 8-bit |
| 1949 | encoding. To minimize these risks, you can turn off these implicit |
| 1950 | conversions by defining some of the following preprocessor symbols: |
| 1951 | |
| 1952 | \list |
| 1953 | \li \l QT_NO_CAST_FROM_ASCII disables automatic conversions from |
| 1954 | C string literals and pointers to Unicode. |
| 1955 | \li \l QT_RESTRICTED_CAST_FROM_ASCII allows automatic conversions |
| 1956 | from C characters and character arrays but disables automatic |
| 1957 | conversions from character pointers to Unicode. |
| 1958 | \li \l QT_NO_CAST_TO_ASCII disables automatic conversion from QString |
| 1959 | to C strings. |
| 1960 | \endlist |
| 1961 | |
| 1962 | You then need to explicitly call fromUtf8(), fromLatin1(), |
| 1963 | or fromLocal8Bit() to construct a QString from an |
| 1964 | 8-bit string, or use the lightweight QLatin1StringView class. For |
| 1965 | example: |
| 1966 | |
| 1967 | \snippet code/src_corelib_text_qstring.cpp 1 |
| 1968 | |
| 1969 | Similarly, you must call toLatin1(), toUtf8(), or |
| 1970 | toLocal8Bit() explicitly to convert the QString to an 8-bit |
| 1971 | string. |
| 1972 | |
| 1973 | \table 100 % |
| 1974 | \header |
| 1975 | \li Note for C Programmers |
| 1976 | |
| 1977 | \row |
| 1978 | \li |
| 1979 | Due to C++'s type system and the fact that QString is |
| 1980 | \l{implicitly shared}, QStrings may be treated like \c{int}s or |
| 1981 | other basic types. For example: |
| 1982 | |
| 1983 | \snippet qstring/main.cpp 7 |
| 1984 | |
| 1985 | The \c result variable is a normal variable allocated on the |
| 1986 | stack. When \c return is called, and because we're returning by |
| 1987 | value, the copy constructor is called and a copy of the string is |
| 1988 | returned. No actual copying takes place thanks to the implicit |
| 1989 | sharing. |
| 1990 | |
| 1991 | \endtable |
| 1992 | |
| 1993 | \section1 Distinction between null and empty strings |
| 1994 | |
| 1995 | For historical reasons, QString distinguishes between null |
| 1996 | and empty strings. A \e null string is a string that is |
| 1997 | initialized using QString's default constructor or by passing |
| 1998 | \nullptr to the constructor. An \e empty string is any |
| 1999 | string with size 0. A null string is always empty, but an empty |
| 2000 | string isn't necessarily null: |
| 2001 | |
| 2002 | \snippet qstring/main.cpp 8 |
| 2003 | |
| 2004 | All functions except isNull() treat null strings the same as empty |
| 2005 | strings. For example, toUtf8().\l{QByteArray::}{constData()} returns a valid pointer |
| 2006 | (not \nullptr) to a '\\0' character for a null string. We |
| 2007 | recommend that you always use the isEmpty() function and avoid isNull(). |
| 2008 | |
| 2009 | \section1 Number formats |
| 2010 | |
| 2011 | When a QString::arg() \c{'%'} format specifier includes the \c{'L'} locale |
| 2012 | qualifier, and the base is ten (its default), the default locale is |
| 2013 | used. This can be set using \l{QLocale::setDefault()}. For more refined |
| 2014 | control of localized string representations of numbers, see |
| 2015 | QLocale::toString(). All other number formatting done by QString follows the |
| 2016 | C locale's representation of numbers. |
| 2017 | |
| 2018 | When QString::arg() applies left-padding to numbers, the fill character |
| 2019 | \c{'0'} is treated specially. If the number is negative, its minus sign |
| 2020 | appears before the zero-padding. If the field is localized, the |
| 2021 | locale-appropriate zero character is used in place of \c{'0'}. For |
| 2022 | floating-point numbers, this special treatment only applies if the number is |
| 2023 | finite. |
| 2024 | |
| 2025 | \section2 Floating-point formats |
| 2026 | |
| 2027 | In member functions (for example, arg() and number()) that format floating-point |
| 2028 | numbers (\c float or \c double) as strings, the representation used can be |
| 2029 | controlled by a choice of \e format and \e precision, whose meanings are as |
| 2030 | for \l {QLocale::toString(double, char, int)}. |
| 2031 | |
| 2032 | If the selected \e format includes an exponent, localized forms follow the |
| 2033 | locale's convention on digits in the exponent. For non-localized formatting, |
| 2034 | the exponent shows its sign and includes at least two digits, left-padding |
| 2035 | with zero if needed. |
| 2036 | |
| 2037 | \section1 More efficient string construction |
| 2038 | |
| 2039 | Many strings are known at compile time. The QString constructor from |
| 2040 | C++ string literals will copy the contents of the string, |
| 2041 | treating the contents as UTF-8. This requires memory allocation and |
| 2042 | re-encoding string data, operations that will happen at runtime. |
| 2043 | If the string data is known at compile time, you can use the QStringLiteral |
| 2044 | macro or similarly \c{operator""_s} to create QString's payload at compile |
| 2045 | time instead. |
| 2046 | |
| 2047 | Using the QString \c{'+'} operator, it is easy to construct a |
| 2048 | complex string from multiple substrings. You will often write code |
| 2049 | like this: |
| 2050 | |
| 2051 | \snippet qstring/stringbuilder.cpp 0 |
| 2052 | |
| 2053 | There is nothing wrong with either of these string constructions, |
| 2054 | but there are a few hidden inefficiencies: |
| 2055 | |
| 2056 | First, repeated use of the \c{'+'} operator may lead to |
| 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 | These allocations can be optimized by an internal class |
| 2062 | \c{QStringBuilder}. 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. |
| 2066 | |
| 2067 | \c{QStringBuilder} uses expression templates and reimplements the |
| 2068 | \c{'%'} operator so that when you use \c{'%'} for string |
| 2069 | concatenation instead of \c{'+'}, multiple substring |
| 2070 | concatenations will be postponed until the final result is about |
| 2071 | to be assigned to a QString. At this point, the amount of memory |
| 2072 | required for the final result is known. The memory allocator is |
| 2073 | then called \e{once} to get the required space, and the substrings |
| 2074 | are copied into it one by one. |
| 2075 | |
| 2076 | Additional efficiency is gained by inlining and reducing reference |
| 2077 | counting (the QString created from a \c{QStringBuilder} |
| 2078 | has a ref count of 1, whereas QString::append() needs an extra |
| 2079 | test). |
| 2080 | |
| 2081 | There are two ways you can access this improved method of string |
| 2082 | construction. The straightforward way is to include |
| 2083 | \c{QStringBuilder} wherever you want to use it and use the |
| 2084 | \c{'%'} operator instead of \c{'+'} when concatenating strings: |
| 2085 | |
| 2086 | \snippet qstring/stringbuilder.cpp 5 |
| 2087 | |
| 2088 | A more global approach, which is more convenient but not entirely |
| 2089 | source-compatible, is to define \c QT_USE_QSTRINGBUILDER (by adding |
| 2090 | it to the compiler flags) at build time. This will make concatenating |
| 2091 | strings with \c{'+'} work the same way as \c{QStringBuilder's} \c{'%'}. |
| 2092 | |
| 2093 | \note Using automatic type deduction (for example, by using the \c |
| 2094 | auto keyword) with the result of string concatenation when QStringBuilder |
| 2095 | is enabled will show that the concatenation is indeed an object of a |
| 2096 | QStringBuilder specialization: |
| 2097 | |
| 2098 | \snippet qstring/stringbuilder.cpp 6 |
| 2099 | |
| 2100 | This does not cause any harm, as QStringBuilder will implicitly convert to |
| 2101 | QString when required. If this is undesirable, then one should specify |
| 2102 | the necessary types instead of having the compiler deduce them: |
| 2103 | |
| 2104 | \snippet qstring/stringbuilder.cpp 7 |
| 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 a 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 cases where Qt |
| 2119 | will throw exceptions. If exceptions are disabled, then running out of |
| 2120 | memory is undefined behavior. |
| 2121 | |
| 2122 | \note Target operating systems may impose limits on how much memory an |
| 2123 | application can allocate, in total, or on the size of individual allocations. |
| 2124 | This may further restrict the size of string a QString can hold. |
| 2125 | Mitigating or controlling the behavior these limits cause is beyond the |
| 2126 | scope of the Qt API. |
| 2127 | |
| 2128 | \sa {Which string class to use?}, fromRawData(), QChar, QStringView, |
| 2129 | QLatin1StringView, QByteArray |
| 2130 | */ |
| 2131 | |
| 2132 | /*! \typedef QString::ConstIterator |
| 2133 | |
| 2134 | Qt-style synonym for QString::const_iterator. |
| 2135 | */ |
| 2136 | |
| 2137 | /*! \typedef QString::Iterator |
| 2138 | |
| 2139 | Qt-style synonym for QString::iterator. |
| 2140 | */ |
| 2141 | |
| 2142 | /*! \typedef QString::const_iterator |
| 2143 | |
| 2144 | \sa QString::iterator |
| 2145 | */ |
| 2146 | |
| 2147 | /*! \typedef QString::iterator |
| 2148 | |
| 2149 | \sa QString::const_iterator |
| 2150 | */ |
| 2151 | |
| 2152 | /*! \typedef QString::const_reverse_iterator |
| 2153 | \since 5.6 |
| 2154 | |
| 2155 | \sa QString::reverse_iterator, QString::const_iterator |
| 2156 | */ |
| 2157 | |
| 2158 | /*! \typedef QString::reverse_iterator |
| 2159 | \since 5.6 |
| 2160 | |
| 2161 | \sa QString::const_reverse_iterator, QString::iterator |
| 2162 | */ |
| 2163 | |
| 2164 | /*! |
| 2165 | \typedef QString::size_type |
| 2166 | */ |
| 2167 | |
| 2168 | /*! |
| 2169 | \typedef QString::difference_type |
| 2170 | */ |
| 2171 | |
| 2172 | /*! |
| 2173 | \typedef QString::const_reference |
| 2174 | */ |
| 2175 | /*! |
| 2176 | \typedef QString::reference |
| 2177 | */ |
| 2178 | |
| 2179 | /*! |
| 2180 | \typedef QString::const_pointer |
| 2181 | |
| 2182 | The QString::const_pointer typedef provides an STL-style |
| 2183 | const pointer to a QString element (QChar). |
| 2184 | */ |
| 2185 | /*! |
| 2186 | \typedef QString::pointer |
| 2187 | |
| 2188 | The QString::pointer typedef provides an STL-style |
| 2189 | pointer to a QString element (QChar). |
| 2190 | */ |
| 2191 | |
| 2192 | /*! |
| 2193 | \typedef QString::value_type |
| 2194 | */ |
| 2195 | |
| 2196 | /*! \fn QString::iterator QString::begin() |
| 2197 | |
| 2198 | Returns an \l{STL-style iterators}{STL-style iterator} pointing to the |
| 2199 | first character in the string. |
| 2200 | |
| 2201 | //! [iterator-invalidation-func-desc] |
| 2202 | \warning The returned iterator is invalidated on detachment or when the |
| 2203 | QString is modified. |
| 2204 | //! [iterator-invalidation-func-desc] |
| 2205 | |
| 2206 | \sa constBegin(), end() |
| 2207 | */ |
| 2208 | |
| 2209 | /*! \fn QString::const_iterator QString::begin() const |
| 2210 | |
| 2211 | \overload begin() |
| 2212 | */ |
| 2213 | |
| 2214 | /*! \fn QString::const_iterator QString::cbegin() const |
| 2215 | \since 5.0 |
| 2216 | |
| 2217 | Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the |
| 2218 | first character in the string. |
| 2219 | |
| 2220 | \include qstring.cpp iterator-invalidation-func-desc |
| 2221 | |
| 2222 | \sa begin(), cend() |
| 2223 | */ |
| 2224 | |
| 2225 | /*! \fn QString::const_iterator QString::constBegin() const |
| 2226 | |
| 2227 | Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the |
| 2228 | first character in the string. |
| 2229 | |
| 2230 | \include qstring.cpp iterator-invalidation-func-desc |
| 2231 | |
| 2232 | \sa begin(), constEnd() |
| 2233 | */ |
| 2234 | |
| 2235 | /*! \fn QString::iterator QString::end() |
| 2236 | |
| 2237 | Returns an \l{STL-style iterators}{STL-style iterator} pointing just after |
| 2238 | the last character in the string. |
| 2239 | |
| 2240 | \include qstring.cpp iterator-invalidation-func-desc |
| 2241 | |
| 2242 | \sa begin(), constEnd() |
| 2243 | */ |
| 2244 | |
| 2245 | /*! \fn QString::const_iterator QString::end() const |
| 2246 | |
| 2247 | \overload end() |
| 2248 | */ |
| 2249 | |
| 2250 | /*! \fn QString::const_iterator QString::cend() const |
| 2251 | \since 5.0 |
| 2252 | |
| 2253 | Returns a const \l{STL-style iterators}{STL-style iterator} pointing just |
| 2254 | after the last character in the string. |
| 2255 | |
| 2256 | \include qstring.cpp iterator-invalidation-func-desc |
| 2257 | |
| 2258 | \sa cbegin(), end() |
| 2259 | */ |
| 2260 | |
| 2261 | /*! \fn QString::const_iterator QString::constEnd() const |
| 2262 | |
| 2263 | Returns a const \l{STL-style iterators}{STL-style iterator} pointing just |
| 2264 | after the last character in the string. |
| 2265 | |
| 2266 | \include qstring.cpp iterator-invalidation-func-desc |
| 2267 | |
| 2268 | \sa constBegin(), end() |
| 2269 | */ |
| 2270 | |
| 2271 | /*! \fn QString::reverse_iterator QString::rbegin() |
| 2272 | \since 5.6 |
| 2273 | |
| 2274 | Returns a \l{STL-style iterators}{STL-style} reverse iterator pointing to |
| 2275 | the first character in the string, in reverse order. |
| 2276 | |
| 2277 | \include qstring.cpp iterator-invalidation-func-desc |
| 2278 | |
| 2279 | \sa begin(), crbegin(), rend() |
| 2280 | */ |
| 2281 | |
| 2282 | /*! \fn QString::const_reverse_iterator QString::rbegin() const |
| 2283 | \since 5.6 |
| 2284 | \overload |
| 2285 | */ |
| 2286 | |
| 2287 | /*! \fn QString::const_reverse_iterator QString::crbegin() const |
| 2288 | \since 5.6 |
| 2289 | |
| 2290 | Returns a const \l{STL-style iterators}{STL-style} reverse iterator |
| 2291 | pointing to the first character in the string, in reverse order. |
| 2292 | |
| 2293 | \include qstring.cpp iterator-invalidation-func-desc |
| 2294 | |
| 2295 | \sa begin(), rbegin(), rend() |
| 2296 | */ |
| 2297 | |
| 2298 | /*! \fn QString::reverse_iterator QString::rend() |
| 2299 | \since 5.6 |
| 2300 | |
| 2301 | Returns a \l{STL-style iterators}{STL-style} reverse iterator pointing just |
| 2302 | after the last character in the string, in reverse order. |
| 2303 | |
| 2304 | \include qstring.cpp iterator-invalidation-func-desc |
| 2305 | |
| 2306 | \sa end(), crend(), rbegin() |
| 2307 | */ |
| 2308 | |
| 2309 | /*! \fn QString::const_reverse_iterator QString::rend() const |
| 2310 | \since 5.6 |
| 2311 | \overload |
| 2312 | */ |
| 2313 | |
| 2314 | /*! \fn QString::const_reverse_iterator QString::crend() const |
| 2315 | \since 5.6 |
| 2316 | |
| 2317 | Returns a const \l{STL-style iterators}{STL-style} reverse iterator |
| 2318 | pointing just after the last character in the string, in reverse order. |
| 2319 | |
| 2320 | \include qstring.cpp iterator-invalidation-func-desc |
| 2321 | |
| 2322 | \sa end(), rend(), rbegin() |
| 2323 | */ |
| 2324 | |
| 2325 | /*! |
| 2326 | \fn QString::QString() |
| 2327 | |
| 2328 | Constructs a null string. Null strings are also considered empty. |
| 2329 | |
| 2330 | \sa isEmpty(), isNull(), {Distinction Between Null and Empty Strings} |
| 2331 | */ |
| 2332 | |
| 2333 | /*! |
| 2334 | \fn QString::QString(QString &&other) |
| 2335 | |
| 2336 | Move-constructs a QString instance, making it point at the same |
| 2337 | object that \a other was pointing to. |
| 2338 | |
| 2339 | \since 5.2 |
| 2340 | */ |
| 2341 | |
| 2342 | /*! \fn QString::QString(const char *str) |
| 2343 | |
| 2344 | Constructs a string initialized with the 8-bit string \a str. The |
| 2345 | given const char pointer is converted to Unicode using the |
| 2346 | fromUtf8() function. |
| 2347 | |
| 2348 | You can disable this constructor by defining |
| 2349 | \l QT_NO_CAST_FROM_ASCII when you compile your applications. This |
| 2350 | can be useful if you want to ensure that all user-visible strings |
| 2351 | go through QObject::tr(), for example. |
| 2352 | |
| 2353 | \note Defining \l QT_RESTRICTED_CAST_FROM_ASCII also disables |
| 2354 | this constructor, but enables a \c{QString(const char (&ch)[N])} |
| 2355 | constructor instead. Using non-literal input, or input with |
| 2356 | embedded NUL characters, or non-7-bit characters is undefined |
| 2357 | in this case. |
| 2358 | |
| 2359 | \sa fromLatin1(), fromLocal8Bit(), fromUtf8() |
| 2360 | */ |
| 2361 | |
| 2362 | /*! \fn QString::QString(const char8_t *str) |
| 2363 | |
| 2364 | Constructs a string initialized with the UTF-8 string \a str. The |
| 2365 | given const char8_t pointer is converted to Unicode using the |
| 2366 | fromUtf8() function. |
| 2367 | |
| 2368 | \since 6.1 |
| 2369 | \sa fromLatin1(), fromLocal8Bit(), fromUtf8() |
| 2370 | */ |
| 2371 | |
| 2372 | /*! |
| 2373 | \fn QString::QString(QStringView sv) |
| 2374 | |
| 2375 | Constructs a string initialized with the string view's data. |
| 2376 | |
| 2377 | The QString will be null if and only if \a sv is null. |
| 2378 | |
| 2379 | \since 6.8 |
| 2380 | |
| 2381 | \sa fromUtf16() |
| 2382 | */ |
| 2383 | |
| 2384 | /* |
| 2385 | //! [from-std-string] |
| 2386 | Returns a copy of the \a str string. The given string is assumed to be |
| 2387 | encoded in \1, and is converted to QString using the \2 function. |
| 2388 | //! [from-std-string] |
| 2389 | */ |
| 2390 | |
| 2391 | /*! \fn QString QString::fromStdString(const std::string &str) |
| 2392 | |
| 2393 | \include qstring.cpp {from-std-string} {UTF-8} {fromUtf8()} |
| 2394 | |
| 2395 | \sa fromLatin1(), fromLocal8Bit(), fromUtf8(), QByteArray::fromStdString() |
| 2396 | */ |
| 2397 | |
| 2398 | /*! \fn QString QString::fromStdWString(const std::wstring &str) |
| 2399 | |
| 2400 | Returns a copy of the \a str string. The given string is assumed |
| 2401 | to be encoded in utf16 if the size of wchar_t is 2 bytes (e.g. on |
| 2402 | windows) and ucs4 if the size of wchar_t is 4 bytes (most Unix |
| 2403 | systems). |
| 2404 | |
| 2405 | \sa fromUtf16(), fromLatin1(), fromLocal8Bit(), fromUtf8(), fromUcs4(), |
| 2406 | fromStdU16String(), fromStdU32String() |
| 2407 | */ |
| 2408 | |
| 2409 | /*! \fn QString QString::fromWCharArray(const wchar_t *string, qsizetype size) |
| 2410 | \since 4.2 |
| 2411 | |
| 2412 | Reads the first \a size code units of the \c wchar_t array to whose start |
| 2413 | \a string points, converting them to Unicode and returning the result as |
| 2414 | a QString. The encoding used by \c wchar_t is assumed to be UTF-32 if the |
| 2415 | type's size is four bytes or UTF-16 if its size is two bytes. |
| 2416 | |
| 2417 | If \a size is -1 (default), the \a string must be '\\0'-terminated. |
| 2418 | |
| 2419 | \sa fromUtf16(), fromLatin1(), fromLocal8Bit(), fromUtf8(), fromUcs4(), |
| 2420 | fromStdWString() |
| 2421 | */ |
| 2422 | |
| 2423 | /*! \fn std::wstring QString::toStdWString() const |
| 2424 | |
| 2425 | Returns a std::wstring object with the data contained in this |
| 2426 | QString. The std::wstring is encoded in UTF-16 on platforms where |
| 2427 | wchar_t is 2 bytes wide (for example, Windows) and in UTF-32 on platforms |
| 2428 | where wchar_t is 4 bytes wide (most Unix systems). |
| 2429 | |
| 2430 | This method is mostly useful to pass a QString to a function |
| 2431 | that accepts a std::wstring object. |
| 2432 | |
| 2433 | \sa utf16(), toLatin1(), toUtf8(), toLocal8Bit(), toStdU16String(), |
| 2434 | toStdU32String() |
| 2435 | */ |
| 2436 | |
| 2437 | qsizetype QString::toUcs4_helper(const char16_t *uc, qsizetype length, char32_t *out) |
| 2438 | { |
| 2439 | qsizetype count = 0; |
| 2440 | |
| 2441 | QStringIterator i(QStringView(uc, length)); |
| 2442 | while (i.hasNext()) |
| 2443 | out[count++] = i.next(); |
| 2444 | |
| 2445 | return count; |
| 2446 | } |
| 2447 | |
| 2448 | /*! \fn qsizetype QString::toWCharArray(wchar_t *array) const |
| 2449 | \since 4.2 |
| 2450 | |
| 2451 | Fills the \a array with the data contained in this QString object. |
| 2452 | The array is encoded in UTF-16 on platforms where |
| 2453 | wchar_t is 2 bytes wide (e.g. windows) and in UTF-32 on platforms |
| 2454 | where wchar_t is 4 bytes wide (most Unix systems). |
| 2455 | |
| 2456 | \a array has to be allocated by the caller and contain enough space to |
| 2457 | hold the complete string (allocating the array with the same length as the |
| 2458 | string is always sufficient). |
| 2459 | |
| 2460 | This function returns the actual length of the string in \a array. |
| 2461 | |
| 2462 | \note This function does not append a null character to the array. |
| 2463 | |
| 2464 | \sa utf16(), toUcs4(), toLatin1(), toUtf8(), toLocal8Bit(), toStdWString(), |
| 2465 | QStringView::toWCharArray() |
| 2466 | */ |
| 2467 | |
| 2468 | /*! \fn QString::QString(const QString &other) |
| 2469 | |
| 2470 | Constructs a copy of \a other. |
| 2471 | |
| 2472 | This operation takes \l{constant time}, because QString is |
| 2473 | \l{implicitly shared}. This makes returning a QString from a |
| 2474 | function very fast. If a shared instance is modified, it will be |
| 2475 | copied (copy-on-write), and that takes \l{linear time}. |
| 2476 | |
| 2477 | \sa operator=() |
| 2478 | */ |
| 2479 | |
| 2480 | /*! |
| 2481 | Constructs a string initialized with the first \a size characters |
| 2482 | of the QChar array \a unicode. |
| 2483 | |
| 2484 | If \a unicode is 0, a null string is constructed. |
| 2485 | |
| 2486 | If \a size is negative, \a unicode is assumed to point to a '\\0'-terminated |
| 2487 | array and its length is determined dynamically. The terminating |
| 2488 | null character is not considered part of the string. |
| 2489 | |
| 2490 | QString makes a deep copy of the string data. The unicode data is copied as |
| 2491 | is and the Byte Order Mark is preserved if present. |
| 2492 | |
| 2493 | \sa fromRawData() |
| 2494 | */ |
| 2495 | QString::QString(const QChar *unicode, qsizetype size) |
| 2496 | { |
| 2497 | if (!unicode) { |
| 2498 | d.clear(); |
| 2499 | } else { |
| 2500 | if (size < 0) |
| 2501 | size = QtPrivate::qustrlen(str: reinterpret_cast<const char16_t *>(unicode)); |
| 2502 | if (!size) { |
| 2503 | d = DataPointer::fromRawData(rawData: &_empty, length: 0); |
| 2504 | } else { |
| 2505 | d = DataPointer(size, size); |
| 2506 | Q_CHECK_PTR(d.data()); |
| 2507 | memcpy(dest: d.data(), src: unicode, n: size * sizeof(QChar)); |
| 2508 | d.data()[size] = '\0'; |
| 2509 | } |
| 2510 | } |
| 2511 | } |
| 2512 | |
| 2513 | /*! |
| 2514 | Constructs a string of the given \a size with every character set |
| 2515 | to \a ch. |
| 2516 | |
| 2517 | \sa fill() |
| 2518 | */ |
| 2519 | QString::QString(qsizetype size, QChar ch) |
| 2520 | { |
| 2521 | if (size <= 0) { |
| 2522 | d = DataPointer::fromRawData(rawData: &_empty, length: 0); |
| 2523 | } else { |
| 2524 | d = DataPointer(size, size); |
| 2525 | Q_CHECK_PTR(d.data()); |
| 2526 | d.data()[size] = '\0'; |
| 2527 | char16_t *b = d.data(); |
| 2528 | char16_t *e = d.data() + size; |
| 2529 | const char16_t value = ch.unicode(); |
| 2530 | std::fill(first: b, last: e, value: value); |
| 2531 | } |
| 2532 | } |
| 2533 | |
| 2534 | /*! \fn QString::QString(qsizetype size, Qt::Initialization) |
| 2535 | \internal |
| 2536 | |
| 2537 | Constructs a string of the given \a size without initializing the |
| 2538 | characters. This is only used in \c QStringBuilder::toString(). |
| 2539 | */ |
| 2540 | QString::QString(qsizetype size, Qt::Initialization) |
| 2541 | { |
| 2542 | if (size <= 0) { |
| 2543 | d = DataPointer::fromRawData(rawData: &_empty, length: 0); |
| 2544 | } else { |
| 2545 | d = DataPointer(size, size); |
| 2546 | Q_CHECK_PTR(d.data()); |
| 2547 | d.data()[size] = '\0'; |
| 2548 | } |
| 2549 | } |
| 2550 | |
| 2551 | /*! \fn QString::QString(QLatin1StringView str) |
| 2552 | |
| 2553 | Constructs a copy of the Latin-1 string viewed by \a str. |
| 2554 | |
| 2555 | \sa fromLatin1() |
| 2556 | */ |
| 2557 | |
| 2558 | /*! |
| 2559 | Constructs a string of size 1 containing the character \a ch. |
| 2560 | */ |
| 2561 | QString::QString(QChar ch) |
| 2562 | { |
| 2563 | d = DataPointer(1, 1); |
| 2564 | Q_CHECK_PTR(d.data()); |
| 2565 | d.data()[0] = ch.unicode(); |
| 2566 | d.data()[1] = '\0'; |
| 2567 | } |
| 2568 | |
| 2569 | /*! \fn QString::QString(const QByteArray &ba) |
| 2570 | |
| 2571 | Constructs a string initialized with the byte array \a ba. The |
| 2572 | given byte array is converted to Unicode using fromUtf8(). |
| 2573 | |
| 2574 | You can disable this constructor by defining |
| 2575 | \l QT_NO_CAST_FROM_ASCII when you compile your applications. This |
| 2576 | can be useful if you want to ensure that all user-visible strings |
| 2577 | go through QObject::tr(), for example. |
| 2578 | |
| 2579 | \note Any null ('\\0') bytes in the byte array will be included in this |
| 2580 | string, converted to Unicode null characters (U+0000). This behavior is |
| 2581 | different from Qt 5.x. |
| 2582 | |
| 2583 | \sa fromLatin1(), fromLocal8Bit(), fromUtf8() |
| 2584 | */ |
| 2585 | |
| 2586 | /*! \fn QString::QString(const Null &) |
| 2587 | \internal |
| 2588 | */ |
| 2589 | |
| 2590 | /*! \fn QString::QString(QStringPrivate) |
| 2591 | \internal |
| 2592 | */ |
| 2593 | |
| 2594 | /*! \fn QString &QString::operator=(const QString::Null &) |
| 2595 | \internal |
| 2596 | */ |
| 2597 | |
| 2598 | /*! |
| 2599 | \fn QString::~QString() |
| 2600 | |
| 2601 | Destroys the string. |
| 2602 | */ |
| 2603 | |
| 2604 | |
| 2605 | /*! \fn void QString::swap(QString &other) |
| 2606 | \since 4.8 |
| 2607 | \memberswap{string} |
| 2608 | */ |
| 2609 | |
| 2610 | /*! \fn void QString::detach() |
| 2611 | |
| 2612 | \internal |
| 2613 | */ |
| 2614 | |
| 2615 | /*! \fn bool QString::isDetached() const |
| 2616 | |
| 2617 | \internal |
| 2618 | */ |
| 2619 | |
| 2620 | /*! \fn bool QString::isSharedWith(const QString &other) const |
| 2621 | |
| 2622 | \internal |
| 2623 | */ |
| 2624 | |
| 2625 | /*! \fn QString::operator std::u16string_view() const |
| 2626 | \since 6.7 |
| 2627 | |
| 2628 | Converts this QString object to a \c{std::u16string_view} object. |
| 2629 | */ |
| 2630 | |
| 2631 | static bool needsReallocate(const QString &str, qsizetype newSize) |
| 2632 | { |
| 2633 | const auto capacityAtEnd = str.capacity() - str.data_ptr().freeSpaceAtBegin(); |
| 2634 | return newSize > capacityAtEnd; |
| 2635 | } |
| 2636 | |
| 2637 | /*! |
| 2638 | Sets the size of the string to \a size characters. |
| 2639 | |
| 2640 | If \a size is greater than the current size, the string is |
| 2641 | extended to make it \a size characters long with the extra |
| 2642 | characters added to the end. The new characters are uninitialized. |
| 2643 | |
| 2644 | If \a size is less than the current size, characters beyond position |
| 2645 | \a size are excluded from the string. |
| 2646 | |
| 2647 | \note While resize() will grow the capacity if needed, it never shrinks |
| 2648 | capacity. To shed excess capacity, use squeeze(). |
| 2649 | |
| 2650 | Example: |
| 2651 | |
| 2652 | \snippet qstring/main.cpp 45 |
| 2653 | |
| 2654 | If you want to append a certain number of identical characters to |
| 2655 | the string, use the \l {QString::}{resize(qsizetype, QChar)} overload. |
| 2656 | |
| 2657 | If you want to expand the string so that it reaches a certain |
| 2658 | width and fill the new positions with a particular character, use |
| 2659 | the leftJustified() function: |
| 2660 | |
| 2661 | If \a size is negative, it is equivalent to passing zero. |
| 2662 | |
| 2663 | \snippet qstring/main.cpp 47 |
| 2664 | |
| 2665 | \sa truncate(), reserve(), squeeze() |
| 2666 | */ |
| 2667 | |
| 2668 | void QString::resize(qsizetype size) |
| 2669 | { |
| 2670 | if (size < 0) |
| 2671 | size = 0; |
| 2672 | |
| 2673 | if (d->needsDetach() || needsReallocate(str: *this, newSize: size)) |
| 2674 | reallocData(alloc: size, option: QArrayData::Grow); |
| 2675 | d.size = size; |
| 2676 | if (d->allocatedCapacity()) |
| 2677 | d.data()[size] = u'\0'; |
| 2678 | } |
| 2679 | |
| 2680 | /*! |
| 2681 | \overload |
| 2682 | \since 5.7 |
| 2683 | |
| 2684 | Unlike \l {QString::}{resize(qsizetype)}, this overload |
| 2685 | initializes the new characters to \a fillChar: |
| 2686 | |
| 2687 | \snippet qstring/main.cpp 46 |
| 2688 | */ |
| 2689 | |
| 2690 | void QString::resize(qsizetype newSize, QChar fillChar) |
| 2691 | { |
| 2692 | const qsizetype oldSize = size(); |
| 2693 | resize(size: newSize); |
| 2694 | const qsizetype difference = size() - oldSize; |
| 2695 | if (difference > 0) |
| 2696 | std::fill_n(first: d.data() + oldSize, n: difference, value: fillChar.unicode()); |
| 2697 | } |
| 2698 | |
| 2699 | |
| 2700 | /*! |
| 2701 | \since 6.8 |
| 2702 | |
| 2703 | Sets the size of the string to \a size characters. If the size of |
| 2704 | the string grows, the new characters are uninitialized. |
| 2705 | |
| 2706 | The behavior is identical to \c{resize(size)}. |
| 2707 | |
| 2708 | \sa resize() |
| 2709 | */ |
| 2710 | |
| 2711 | void QString::resizeForOverwrite(qsizetype size) |
| 2712 | { |
| 2713 | resize(size); |
| 2714 | } |
| 2715 | |
| 2716 | |
| 2717 | /*! \fn qsizetype QString::capacity() const |
| 2718 | |
| 2719 | Returns the maximum number of characters that can be stored in |
| 2720 | the string without forcing a reallocation. |
| 2721 | |
| 2722 | The sole purpose of this function is to provide a means of fine |
| 2723 | tuning QString's memory usage. In general, you will rarely ever |
| 2724 | need to call this function. If you want to know how many |
| 2725 | characters are in the string, call size(). |
| 2726 | |
| 2727 | \note a statically allocated string will report a capacity of 0, |
| 2728 | even if it's not empty. |
| 2729 | |
| 2730 | \note The free space position in the allocated memory block is undefined. In |
| 2731 | other words, one should not assume that the free memory is always located |
| 2732 | after the initialized elements. |
| 2733 | |
| 2734 | \sa reserve(), squeeze() |
| 2735 | */ |
| 2736 | |
| 2737 | /*! |
| 2738 | \fn void QString::reserve(qsizetype size) |
| 2739 | |
| 2740 | Ensures the string has space for at least \a size characters. |
| 2741 | |
| 2742 | If you know in advance how large a string will be, you can call this |
| 2743 | function to save repeated reallocation while building it. |
| 2744 | This can improve performance when building a string incrementally. |
| 2745 | A long sequence of operations that add to a string may trigger several |
| 2746 | reallocations, the last of which may leave you with significantly more |
| 2747 | space than you need. This is less efficient than doing a single |
| 2748 | allocation of the right size at the start. |
| 2749 | |
| 2750 | If in doubt about how much space shall be needed, it is usually better to |
| 2751 | use an upper bound as \a size, or a high estimate of the most likely size, |
| 2752 | if a strict upper bound would be much bigger than this. If \a size is an |
| 2753 | underestimate, the string will grow as needed once the reserved size is |
| 2754 | exceeded, which may lead to a larger allocation than your best |
| 2755 | overestimate would have and will slow the operation that triggers it. |
| 2756 | |
| 2757 | \warning reserve() reserves memory but does not change the size of the |
| 2758 | string. Accessing data beyond the end of the string is undefined behavior. |
| 2759 | If you need to access memory beyond the current end of the string, |
| 2760 | use resize(). |
| 2761 | |
| 2762 | This function is useful for code that needs to build up a long |
| 2763 | string and wants to avoid repeated reallocation. In this example, |
| 2764 | we want to add to the string until some condition is \c true, and |
| 2765 | we're fairly sure that size is large enough to make a call to |
| 2766 | reserve() worthwhile: |
| 2767 | |
| 2768 | \snippet qstring/main.cpp 44 |
| 2769 | |
| 2770 | \sa squeeze(), capacity(), resize() |
| 2771 | */ |
| 2772 | |
| 2773 | /*! |
| 2774 | \fn void QString::squeeze() |
| 2775 | |
| 2776 | Releases any memory not required to store the character data. |
| 2777 | |
| 2778 | The sole purpose of this function is to provide a means of fine |
| 2779 | tuning QString's memory usage. In general, you will rarely ever |
| 2780 | need to call this function. |
| 2781 | |
| 2782 | \sa reserve(), capacity() |
| 2783 | */ |
| 2784 | |
| 2785 | void QString::reallocData(qsizetype alloc, QArrayData::AllocationOption option) |
| 2786 | { |
| 2787 | if (!alloc) { |
| 2788 | d = DataPointer::fromRawData(rawData: &_empty, length: 0); |
| 2789 | return; |
| 2790 | } |
| 2791 | |
| 2792 | // don't use reallocate path when reducing capacity and there's free space |
| 2793 | // at the beginning: might shift data pointer outside of allocated space |
| 2794 | const bool cannotUseReallocate = d.freeSpaceAtBegin() > 0; |
| 2795 | |
| 2796 | if (d->needsDetach() || cannotUseReallocate) { |
| 2797 | DataPointer dd(alloc, qMin(a: alloc, b: d.size), option); |
| 2798 | Q_CHECK_PTR(dd.data()); |
| 2799 | if (dd.size > 0) |
| 2800 | ::memcpy(dest: dd.data(), src: d.data(), n: dd.size * sizeof(QChar)); |
| 2801 | dd.data()[dd.size] = 0; |
| 2802 | d.swap(other&: dd); |
| 2803 | } else { |
| 2804 | d->reallocate(alloc, option); |
| 2805 | } |
| 2806 | } |
| 2807 | |
| 2808 | void QString::reallocGrowData(qsizetype n) |
| 2809 | { |
| 2810 | if (!n) // expected to always allocate |
| 2811 | n = 1; |
| 2812 | |
| 2813 | if (d->needsDetach()) { |
| 2814 | DataPointer dd(DataPointer::allocateGrow(from: d, n, position: QArrayData::GrowsAtEnd)); |
| 2815 | Q_CHECK_PTR(dd.data()); |
| 2816 | dd->copyAppend(b: d.data(), e: d.data() + d.size); |
| 2817 | dd.data()[dd.size] = 0; |
| 2818 | d.swap(other&: dd); |
| 2819 | } else { |
| 2820 | d->reallocate(alloc: d.constAllocatedCapacity() + n, option: QArrayData::Grow); |
| 2821 | } |
| 2822 | } |
| 2823 | |
| 2824 | /*! \fn void QString::clear() |
| 2825 | |
| 2826 | Clears the contents of the string and makes it null. |
| 2827 | |
| 2828 | \sa resize(), isNull() |
| 2829 | */ |
| 2830 | |
| 2831 | /*! \fn QString &QString::operator=(const QString &other) |
| 2832 | |
| 2833 | Assigns \a other to this string and returns a reference to this |
| 2834 | string. |
| 2835 | */ |
| 2836 | |
| 2837 | QString &QString::operator=(const QString &other) noexcept |
| 2838 | { |
| 2839 | d = other.d; |
| 2840 | return *this; |
| 2841 | } |
| 2842 | |
| 2843 | /*! |
| 2844 | \fn QString &QString::operator=(QString &&other) |
| 2845 | |
| 2846 | Move-assigns \a other to this QString instance. |
| 2847 | |
| 2848 | \since 5.2 |
| 2849 | */ |
| 2850 | |
| 2851 | /*! \fn QString &QString::operator=(QLatin1StringView str) |
| 2852 | |
| 2853 | \overload operator=() |
| 2854 | |
| 2855 | Assigns the Latin-1 string viewed by \a str to this string. |
| 2856 | */ |
| 2857 | QString &QString::operator=(QLatin1StringView other) |
| 2858 | { |
| 2859 | const qsizetype capacityAtEnd = capacity() - d.freeSpaceAtBegin(); |
| 2860 | if (isDetached() && other.size() <= capacityAtEnd) { // assumes d->alloc == 0 -> !isDetached() (sharedNull) |
| 2861 | d.size = other.size(); |
| 2862 | d.data()[other.size()] = 0; |
| 2863 | qt_from_latin1(dst: d.data(), str: other.latin1(), size: other.size()); |
| 2864 | } else { |
| 2865 | *this = fromLatin1(str: other.latin1(), size: other.size()); |
| 2866 | } |
| 2867 | return *this; |
| 2868 | } |
| 2869 | |
| 2870 | /*! \fn QString &QString::operator=(const QByteArray &ba) |
| 2871 | |
| 2872 | \overload operator=() |
| 2873 | |
| 2874 | Assigns \a ba to this string. The byte array is converted to Unicode |
| 2875 | using the fromUtf8() function. |
| 2876 | |
| 2877 | You can disable this operator by defining |
| 2878 | \l QT_NO_CAST_FROM_ASCII when you compile your applications. This |
| 2879 | can be useful if you want to ensure that all user-visible strings |
| 2880 | go through QObject::tr(), for example. |
| 2881 | */ |
| 2882 | |
| 2883 | /*! \fn QString &QString::operator=(const char *str) |
| 2884 | |
| 2885 | \overload operator=() |
| 2886 | |
| 2887 | Assigns \a str to this string. The const char pointer is converted |
| 2888 | to Unicode using the fromUtf8() function. |
| 2889 | |
| 2890 | You can disable this operator by defining \l QT_NO_CAST_FROM_ASCII |
| 2891 | or \l QT_RESTRICTED_CAST_FROM_ASCII when you compile your applications. |
| 2892 | This can be useful if you want to ensure that all user-visible strings |
| 2893 | go through QObject::tr(), for example. |
| 2894 | */ |
| 2895 | |
| 2896 | /*! |
| 2897 | \overload operator=() |
| 2898 | |
| 2899 | Sets the string to contain the single character \a ch. |
| 2900 | */ |
| 2901 | QString &QString::operator=(QChar ch) |
| 2902 | { |
| 2903 | return assign(n: 1, c: ch); |
| 2904 | } |
| 2905 | |
| 2906 | /*! |
| 2907 | \fn QString& QString::insert(qsizetype position, const QString &str) |
| 2908 | |
| 2909 | Inserts the string \a str at the given index \a position and |
| 2910 | returns a reference to this string. |
| 2911 | |
| 2912 | Example: |
| 2913 | |
| 2914 | \snippet qstring/main.cpp 26 |
| 2915 | |
| 2916 | //! [string-grow-at-insertion] |
| 2917 | This string grows to accommodate the insertion. If \a position is beyond |
| 2918 | the end of the string, space characters are appended to the string to reach |
| 2919 | this \a position, followed by \a str. |
| 2920 | //! [string-grow-at-insertion] |
| 2921 | |
| 2922 | \sa append(), prepend(), replace(), remove() |
| 2923 | */ |
| 2924 | |
| 2925 | /*! |
| 2926 | \fn QString& QString::insert(qsizetype position, QStringView str) |
| 2927 | \since 6.0 |
| 2928 | \overload insert() |
| 2929 | |
| 2930 | Inserts the string view \a str at the given index \a position and |
| 2931 | returns a reference to this string. |
| 2932 | |
| 2933 | \include qstring.cpp string-grow-at-insertion |
| 2934 | */ |
| 2935 | |
| 2936 | |
| 2937 | /*! |
| 2938 | \fn QString& QString::insert(qsizetype position, const char *str) |
| 2939 | \since 5.5 |
| 2940 | \overload insert() |
| 2941 | |
| 2942 | Inserts the C string \a str at the given index \a position and |
| 2943 | returns a reference to this string. |
| 2944 | |
| 2945 | \include qstring.cpp string-grow-at-insertion |
| 2946 | |
| 2947 | This function is not available when \l QT_NO_CAST_FROM_ASCII is |
| 2948 | defined. |
| 2949 | */ |
| 2950 | |
| 2951 | /*! |
| 2952 | \fn QString& QString::insert(qsizetype position, const QByteArray &str) |
| 2953 | \since 5.5 |
| 2954 | \overload insert() |
| 2955 | |
| 2956 | Interprets the contents of \a str as UTF-8, inserts the Unicode string |
| 2957 | it encodes at the given index \a position and returns a reference to |
| 2958 | this string. |
| 2959 | |
| 2960 | \include qstring.cpp string-grow-at-insertion |
| 2961 | |
| 2962 | This function is not available when \l QT_NO_CAST_FROM_ASCII is |
| 2963 | defined. |
| 2964 | */ |
| 2965 | |
| 2966 | /*! \internal |
| 2967 | T is a view or a container on/of QChar, char16_t, or char |
| 2968 | */ |
| 2969 | template <typename T> |
| 2970 | static void insert_helper(QString &str, qsizetype i, const T &toInsert) |
| 2971 | { |
| 2972 | auto &str_d = str.data_ptr(); |
| 2973 | qsizetype difference = 0; |
| 2974 | if (Q_UNLIKELY(i > str_d.size)) |
| 2975 | difference = i - str_d.size; |
| 2976 | const qsizetype oldSize = str_d.size; |
| 2977 | const qsizetype insert_size = toInsert.size(); |
| 2978 | const qsizetype newSize = str_d.size + difference + insert_size; |
| 2979 | const auto side = i == 0 ? QArrayData::GrowsAtBeginning : QArrayData::GrowsAtEnd; |
| 2980 | |
| 2981 | if (str_d.needsDetach() || needsReallocate(str, newSize)) { |
| 2982 | const auto cbegin = str.cbegin(); |
| 2983 | const auto cend = str.cend(); |
| 2984 | const auto insert_start = difference == 0 ? std::next(x: cbegin, n: i) : cend; |
| 2985 | QString other; |
| 2986 | // Using detachAndGrow() so that prepend optimization works and QStringBuilder |
| 2987 | // unittests pass |
| 2988 | other.data_ptr().detachAndGrow(where: side, n: newSize, data: nullptr, old: nullptr); |
| 2989 | other.append(v: QStringView(cbegin, insert_start)); |
| 2990 | other.resize(newSize: i, fillChar: u' '); |
| 2991 | other.append(toInsert); |
| 2992 | other.append(v: QStringView(insert_start, cend)); |
| 2993 | str.swap(other); |
| 2994 | return; |
| 2995 | } |
| 2996 | |
| 2997 | str_d.detachAndGrow(where: side, n: difference + insert_size, data: nullptr, old: nullptr); |
| 2998 | Q_CHECK_PTR(str_d.data()); |
| 2999 | str.resize(size: newSize); |
| 3000 | |
| 3001 | auto begin = str_d.begin(); |
| 3002 | auto old_end = std::next(x: begin, n: oldSize); |
| 3003 | std::fill_n(first: old_end, n: difference, value: u' '); |
| 3004 | auto insert_start = std::next(x: begin, n: i); |
| 3005 | if (difference == 0) |
| 3006 | std::move_backward(first: insert_start, last: old_end, result: str_d.end()); |
| 3007 | |
| 3008 | using Char = std::remove_cv_t<typename T::value_type>; |
| 3009 | if constexpr(std::is_same_v<Char, QChar>) |
| 3010 | std::copy_n(first: reinterpret_cast<const char16_t *>(toInsert.data()), n: insert_size, result: insert_start); |
| 3011 | else if constexpr (std::is_same_v<Char, char16_t>) |
| 3012 | std::copy_n(toInsert.data(), insert_size, insert_start); |
| 3013 | else if constexpr (std::is_same_v<Char, char>) |
| 3014 | qt_from_latin1(insert_start, toInsert.data(), insert_size); |
| 3015 | } |
| 3016 | |
| 3017 | /*! |
| 3018 | \fn QString &QString::insert(qsizetype position, QLatin1StringView str) |
| 3019 | \overload insert() |
| 3020 | |
| 3021 | Inserts the Latin-1 string viewed by \a str at the given index \a position. |
| 3022 | |
| 3023 | \include qstring.cpp string-grow-at-insertion |
| 3024 | */ |
| 3025 | QString &QString::insert(qsizetype i, QLatin1StringView str) |
| 3026 | { |
| 3027 | const char *s = str.latin1(); |
| 3028 | if (i < 0 || !s || !(*s)) |
| 3029 | return *this; |
| 3030 | |
| 3031 | insert_helper(str&: *this, i, toInsert: str); |
| 3032 | return *this; |
| 3033 | } |
| 3034 | |
| 3035 | /*! |
| 3036 | \fn QString &QString::insert(qsizetype position, QUtf8StringView str) |
| 3037 | \overload insert() |
| 3038 | \since 6.5 |
| 3039 | |
| 3040 | Inserts the UTF-8 string view \a str at the given index \a position. |
| 3041 | |
| 3042 | \note Inserting variable-width UTF-8-encoded string data is conceptually slower |
| 3043 | than inserting fixed-width string data such as UTF-16 (QStringView) or Latin-1 |
| 3044 | (QLatin1StringView) and should thus be used sparingly. |
| 3045 | |
| 3046 | \include qstring.cpp string-grow-at-insertion |
| 3047 | */ |
| 3048 | QString &QString::insert(qsizetype i, QUtf8StringView s) |
| 3049 | { |
| 3050 | auto insert_size = s.size(); |
| 3051 | if (i < 0 || insert_size <= 0) |
| 3052 | return *this; |
| 3053 | |
| 3054 | qsizetype difference = 0; |
| 3055 | if (Q_UNLIKELY(i > d.size)) |
| 3056 | difference = i - d.size; |
| 3057 | |
| 3058 | const qsizetype newSize = d.size + difference + insert_size; |
| 3059 | |
| 3060 | if (d.needsDetach() || needsReallocate(str: *this, newSize)) { |
| 3061 | const auto cbegin = this->cbegin(); |
| 3062 | const auto insert_start = difference == 0 ? std::next(x: cbegin, n: i) : cend(); |
| 3063 | QString other; |
| 3064 | other.reserve(asize: newSize); |
| 3065 | other.append(v: QStringView(cbegin, insert_start)); |
| 3066 | if (difference > 0) |
| 3067 | other.resize(newSize: i, fillChar: u' '); |
| 3068 | other.append(s); |
| 3069 | other.append(v: QStringView(insert_start, cend())); |
| 3070 | swap(other); |
| 3071 | return *this; |
| 3072 | } |
| 3073 | |
| 3074 | if (i >= d.size) { |
| 3075 | d.detachAndGrow(where: QArrayData::GrowsAtEnd, n: difference + insert_size, data: nullptr, old: nullptr); |
| 3076 | Q_CHECK_PTR(d.data()); |
| 3077 | |
| 3078 | if (difference > 0) |
| 3079 | resize(newSize: i, fillChar: u' '); |
| 3080 | append(s); |
| 3081 | } else { |
| 3082 | // Optimal insertion of Utf8 data is at the end, anywhere else could |
| 3083 | // potentially lead to moving characters twice if Utf8 data size |
| 3084 | // (variable-width) is less than the equivalent Utf16 data size |
| 3085 | QVarLengthArray<char16_t> buffer(insert_size); // ### optimize (QTBUG-108546) |
| 3086 | char16_t *b = QUtf8::convertToUnicode(dst: buffer.data(), in: s); |
| 3087 | insert_helper(str&: *this, i, toInsert: QStringView(buffer.data(), b)); |
| 3088 | } |
| 3089 | |
| 3090 | return *this; |
| 3091 | } |
| 3092 | |
| 3093 | /*! |
| 3094 | \fn QString& QString::insert(qsizetype position, const QChar *unicode, qsizetype size) |
| 3095 | \overload insert() |
| 3096 | |
| 3097 | Inserts the first \a size characters of the QChar array \a unicode |
| 3098 | 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 size characters of the QChar array |
| 3103 | \a unicode. |
| 3104 | */ |
| 3105 | QString& QString::insert(qsizetype i, const QChar *unicode, qsizetype size) |
| 3106 | { |
| 3107 | if (i < 0 || size <= 0) |
| 3108 | return *this; |
| 3109 | |
| 3110 | // In case when data points into "this" |
| 3111 | if (!d->needsDetach() && QtPrivate::q_points_into_range(p: unicode, c: *this)) { |
| 3112 | QVarLengthArray copy(unicode, unicode + size); |
| 3113 | insert(i, unicode: copy.data(), size); |
| 3114 | } else { |
| 3115 | insert_helper(str&: *this, i, toInsert: QStringView(unicode, size)); |
| 3116 | } |
| 3117 | |
| 3118 | return *this; |
| 3119 | } |
| 3120 | |
| 3121 | /*! |
| 3122 | \fn QString& QString::insert(qsizetype position, QChar ch) |
| 3123 | \overload insert() |
| 3124 | |
| 3125 | Inserts \a ch at the given index \a position in the string. |
| 3126 | |
| 3127 | This string grows to accommodate the insertion. If \a position is beyond |
| 3128 | the end of the string, space characters are appended to the string to reach |
| 3129 | this \a position, followed by \a ch. |
| 3130 | */ |
| 3131 | |
| 3132 | QString& QString::insert(qsizetype i, QChar ch) |
| 3133 | { |
| 3134 | if (i < 0) |
| 3135 | i += d.size; |
| 3136 | return insert(i, unicode: &ch, size: 1); |
| 3137 | } |
| 3138 | |
| 3139 | /*! |
| 3140 | Appends the string \a str onto the end of this string. |
| 3141 | |
| 3142 | Example: |
| 3143 | |
| 3144 | \snippet qstring/main.cpp 9 |
| 3145 | |
| 3146 | This is the same as using the insert() function: |
| 3147 | |
| 3148 | \snippet qstring/main.cpp 10 |
| 3149 | |
| 3150 | The append() function is typically very fast (\l{constant time}), |
| 3151 | because QString preallocates extra space at the end of the string |
| 3152 | data so it can grow without reallocating the entire string each |
| 3153 | time. |
| 3154 | |
| 3155 | \sa operator+=(), prepend(), insert() |
| 3156 | */ |
| 3157 | QString &QString::append(const QString &str) |
| 3158 | { |
| 3159 | if (!str.isNull()) { |
| 3160 | if (isNull()) { |
| 3161 | if (Q_UNLIKELY(!str.d.isMutable())) |
| 3162 | assign(s: str); // fromRawData, so we do a deep copy |
| 3163 | else |
| 3164 | operator=(other: str); |
| 3165 | } else if (str.size()) { |
| 3166 | append(uc: str.constData(), len: str.size()); |
| 3167 | } |
| 3168 | } |
| 3169 | return *this; |
| 3170 | } |
| 3171 | |
| 3172 | /*! |
| 3173 | \fn QString &QString::append(QStringView v) |
| 3174 | \overload append() |
| 3175 | \since 6.0 |
| 3176 | |
| 3177 | Appends the given string view \a v to this string and returns the result. |
| 3178 | */ |
| 3179 | |
| 3180 | /*! |
| 3181 | \overload append() |
| 3182 | \since 5.0 |
| 3183 | |
| 3184 | Appends \a len characters from the QChar array \a str to this string. |
| 3185 | */ |
| 3186 | QString &QString::append(const QChar *str, qsizetype len) |
| 3187 | { |
| 3188 | if (str && len > 0) { |
| 3189 | static_assert(sizeof(QChar) == sizeof(char16_t), "Unexpected difference in sizes" ); |
| 3190 | // the following should be safe as QChar uses char16_t as underlying data |
| 3191 | const char16_t *char16String = reinterpret_cast<const char16_t *>(str); |
| 3192 | d->growAppend(b: char16String, e: char16String + len); |
| 3193 | d.data()[d.size] = u'\0'; |
| 3194 | } |
| 3195 | return *this; |
| 3196 | } |
| 3197 | |
| 3198 | /*! |
| 3199 | \overload append() |
| 3200 | |
| 3201 | Appends the Latin-1 string viewed by \a str to this string. |
| 3202 | */ |
| 3203 | QString &QString::append(QLatin1StringView str) |
| 3204 | { |
| 3205 | append_helper(self&: *this, view: str); |
| 3206 | return *this; |
| 3207 | } |
| 3208 | |
| 3209 | /*! |
| 3210 | \overload append() |
| 3211 | \since 6.5 |
| 3212 | |
| 3213 | Appends the UTF-8 string view \a str to this string. |
| 3214 | */ |
| 3215 | QString &QString::append(QUtf8StringView str) |
| 3216 | { |
| 3217 | append_helper(self&: *this, view: str); |
| 3218 | return *this; |
| 3219 | } |
| 3220 | |
| 3221 | /*! \fn QString &QString::append(const QByteArray &ba) |
| 3222 | |
| 3223 | \overload append() |
| 3224 | |
| 3225 | Appends the byte array \a ba to this string. The given byte array |
| 3226 | is converted to Unicode using the fromUtf8() function. |
| 3227 | |
| 3228 | You can disable this function by defining \l QT_NO_CAST_FROM_ASCII |
| 3229 | when you compile your applications. This can be useful if you want |
| 3230 | to ensure that all user-visible strings go through QObject::tr(), |
| 3231 | for example. |
| 3232 | */ |
| 3233 | |
| 3234 | /*! \fn QString &QString::append(const char *str) |
| 3235 | |
| 3236 | \overload append() |
| 3237 | |
| 3238 | Appends the string \a str to this string. The given const char |
| 3239 | pointer is converted to Unicode using the fromUtf8() function. |
| 3240 | |
| 3241 | You can disable this function by defining \l QT_NO_CAST_FROM_ASCII |
| 3242 | when you compile your applications. This can be useful if you want |
| 3243 | to ensure that all user-visible strings go through QObject::tr(), |
| 3244 | for example. |
| 3245 | */ |
| 3246 | |
| 3247 | /*! |
| 3248 | \overload append() |
| 3249 | |
| 3250 | Appends the character \a ch to this string. |
| 3251 | */ |
| 3252 | QString &QString::append(QChar ch) |
| 3253 | { |
| 3254 | d.detachAndGrow(where: QArrayData::GrowsAtEnd, n: 1, data: nullptr, old: nullptr); |
| 3255 | d->copyAppend(n: 1, t: ch.unicode()); |
| 3256 | d.data()[d.size] = '\0'; |
| 3257 | return *this; |
| 3258 | } |
| 3259 | |
| 3260 | /*! \fn QString &QString::prepend(const QString &str) |
| 3261 | |
| 3262 | Prepends the string \a str to the beginning of this string and |
| 3263 | returns a reference to this string. |
| 3264 | |
| 3265 | This operation is typically very fast (\l{constant time}), because |
| 3266 | QString preallocates extra space at the beginning of the string data, |
| 3267 | so it can grow without reallocating the entire string each time. |
| 3268 | |
| 3269 | Example: |
| 3270 | |
| 3271 | \snippet qstring/main.cpp 36 |
| 3272 | |
| 3273 | \sa append(), insert() |
| 3274 | */ |
| 3275 | |
| 3276 | /*! \fn QString &QString::prepend(QLatin1StringView str) |
| 3277 | |
| 3278 | \overload prepend() |
| 3279 | |
| 3280 | Prepends the Latin-1 string viewed by \a str to this string. |
| 3281 | */ |
| 3282 | |
| 3283 | /*! \fn QString &QString::prepend(QUtf8StringView str) |
| 3284 | \since 6.5 |
| 3285 | \overload prepend() |
| 3286 | |
| 3287 | Prepends the UTF-8 string view \a str to this string. |
| 3288 | */ |
| 3289 | |
| 3290 | /*! \fn QString &QString::prepend(const QChar *str, qsizetype len) |
| 3291 | \since 5.5 |
| 3292 | \overload prepend() |
| 3293 | |
| 3294 | Prepends \a len characters from the QChar array \a str to this string and |
| 3295 | returns a reference to this string. |
| 3296 | */ |
| 3297 | |
| 3298 | /*! \fn QString &QString::prepend(QStringView str) |
| 3299 | \since 6.0 |
| 3300 | \overload prepend() |
| 3301 | |
| 3302 | Prepends the string view \a str to the beginning of this string and |
| 3303 | returns a reference to this string. |
| 3304 | */ |
| 3305 | |
| 3306 | /*! \fn QString &QString::prepend(const QByteArray &ba) |
| 3307 | |
| 3308 | \overload prepend() |
| 3309 | |
| 3310 | Prepends the byte array \a ba to this string. The byte array is |
| 3311 | converted to Unicode using the fromUtf8() function. |
| 3312 | |
| 3313 | You can disable this function by defining |
| 3314 | \l QT_NO_CAST_FROM_ASCII when you compile your applications. This |
| 3315 | can be useful if you want to ensure that all user-visible strings |
| 3316 | go through QObject::tr(), for example. |
| 3317 | */ |
| 3318 | |
| 3319 | /*! \fn QString &QString::prepend(const char *str) |
| 3320 | |
| 3321 | \overload prepend() |
| 3322 | |
| 3323 | Prepends the string \a str to this string. The const char pointer |
| 3324 | is converted to Unicode using the fromUtf8() function. |
| 3325 | |
| 3326 | You can disable this function by defining |
| 3327 | \l QT_NO_CAST_FROM_ASCII when you compile your applications. This |
| 3328 | can be useful if you want to ensure that all user-visible strings |
| 3329 | go through QObject::tr(), for example. |
| 3330 | */ |
| 3331 | |
| 3332 | /*! \fn QString &QString::prepend(QChar ch) |
| 3333 | |
| 3334 | \overload prepend() |
| 3335 | |
| 3336 | Prepends the character \a ch to this string. |
| 3337 | */ |
| 3338 | |
| 3339 | /*! |
| 3340 | \fn QString &QString::assign(QAnyStringView v) |
| 3341 | \since 6.6 |
| 3342 | |
| 3343 | Replaces the contents of this string with a copy of \a v and returns a |
| 3344 | reference to this string. |
| 3345 | |
| 3346 | The size of this string will be equal to the size of \a v, converted to |
| 3347 | UTF-16 as if by \c{v.toString()}. Unlike QAnyStringView::toString(), however, |
| 3348 | this function only allocates memory if the estimated size exceeds the capacity |
| 3349 | of this string or this string is shared. |
| 3350 | |
| 3351 | \sa QAnyStringView::toString() |
| 3352 | */ |
| 3353 | |
| 3354 | /*! |
| 3355 | \fn QString &QString::assign(qsizetype n, QChar c) |
| 3356 | \since 6.6 |
| 3357 | |
| 3358 | Replaces the contents of this string with \a n copies of \a c and |
| 3359 | returns a reference to this string. |
| 3360 | |
| 3361 | The size of this string will be equal to \a n, which has to be non-negative. |
| 3362 | |
| 3363 | This function will only allocate memory if \a n exceeds the capacity of this |
| 3364 | string or this string is shared. |
| 3365 | |
| 3366 | \sa fill() |
| 3367 | */ |
| 3368 | |
| 3369 | /*! |
| 3370 | \fn template <typename InputIterator, QString::if_compatible_iterator<InputIterator>> QString &QString::assign(InputIterator first, InputIterator last) |
| 3371 | \since 6.6 |
| 3372 | |
| 3373 | Replaces the contents of this string with a copy of the elements in the |
| 3374 | iterator range [\a first, \a last) and returns a reference to this string. |
| 3375 | |
| 3376 | The size of this string will be equal to the decoded length of the elements |
| 3377 | in the range [\a first, \a last), which need not be the same as the length of |
| 3378 | the range itself, because this function transparently recodes the input |
| 3379 | character set to UTF-16. |
| 3380 | |
| 3381 | This function will only allocate memory if the number of elements in the |
| 3382 | range, or, for non-UTF-16-encoded input, the maximum possible size of the |
| 3383 | resulting string, exceeds the capacity of this string, or if this string is |
| 3384 | shared. |
| 3385 | |
| 3386 | \note The behavior is undefined if either argument is an iterator into *this or |
| 3387 | [\a first, \a last) is not a valid range. |
| 3388 | |
| 3389 | \constraints |
| 3390 | \c InputIterator meets the requirements of a |
| 3391 | \l {https://en.cppreference.com/w/cpp/named_req/InputIterator} {LegacyInputIterator} |
| 3392 | and the \c{value_type} of \c InputIterator is one of the following character types: |
| 3393 | \list |
| 3394 | \li QChar |
| 3395 | \li QLatin1Char |
| 3396 | \li \c {char} |
| 3397 | \li \c {unsigned char} |
| 3398 | \li \c {signed char} |
| 3399 | \li \c {char8_t} |
| 3400 | \li \c char16_t |
| 3401 | \li (on platforms, such as Windows, where it is a 16-bit type) \c wchar_t |
| 3402 | \li \c char32_t |
| 3403 | \endlist |
| 3404 | */ |
| 3405 | |
| 3406 | QString &QString::assign(QAnyStringView s) |
| 3407 | { |
| 3408 | if (s.size() <= capacity() && isDetached()) { |
| 3409 | const auto offset = d.freeSpaceAtBegin(); |
| 3410 | if (offset) |
| 3411 | d.setBegin(d.begin() - offset); |
| 3412 | resize(size: 0); |
| 3413 | s.visit(v: [this](auto input) { |
| 3414 | this->append(input); |
| 3415 | }); |
| 3416 | } else { |
| 3417 | *this = s.toString(); |
| 3418 | } |
| 3419 | return *this; |
| 3420 | } |
| 3421 | |
| 3422 | #ifndef QT_BOOTSTRAPPED |
| 3423 | QString &QString::assign_helper(const char32_t *data, qsizetype len) |
| 3424 | { |
| 3425 | // worst case: each char32_t requires a surrogate pair, so |
| 3426 | const auto requiredCapacity = len * 2; |
| 3427 | if (requiredCapacity <= capacity() && isDetached()) { |
| 3428 | const auto offset = d.freeSpaceAtBegin(); |
| 3429 | if (offset) |
| 3430 | d.setBegin(d.begin() - offset); |
| 3431 | auto begin = reinterpret_cast<QChar *>(d.begin()); |
| 3432 | auto ba = QByteArrayView(reinterpret_cast<const std::byte*>(data), len * sizeof(char32_t)); |
| 3433 | QStringConverter::State state; |
| 3434 | const auto end = QUtf32::convertToUnicode(out: begin, ba, state: &state, endian: DetectEndianness); |
| 3435 | d.size = end - begin; |
| 3436 | d.data()[d.size] = u'\0'; |
| 3437 | } else { |
| 3438 | *this = QString::fromUcs4(data, size: len); |
| 3439 | } |
| 3440 | return *this; |
| 3441 | } |
| 3442 | #endif |
| 3443 | |
| 3444 | /*! |
| 3445 | \fn QString &QString::remove(qsizetype position, qsizetype n) |
| 3446 | |
| 3447 | Removes \a n characters from the string, starting at the given \a |
| 3448 | position index, and returns a reference to the string. |
| 3449 | |
| 3450 | If the specified \a position index is within the string, but \a |
| 3451 | position + \a n is beyond the end of the string, the string is |
| 3452 | truncated at the specified \a position. |
| 3453 | |
| 3454 | If \a n is <= 0 nothing is changed. |
| 3455 | |
| 3456 | \snippet qstring/main.cpp 37 |
| 3457 | |
| 3458 | //! [shrinking-erase] |
| 3459 | Element removal will preserve the string's capacity and not reduce the |
| 3460 | amount of allocated memory. To shed extra capacity and free as much memory |
| 3461 | as possible, call squeeze() after the last change to the string's size. |
| 3462 | //! [shrinking-erase] |
| 3463 | |
| 3464 | \sa insert(), replace() |
| 3465 | */ |
| 3466 | QString &QString::remove(qsizetype pos, qsizetype len) |
| 3467 | { |
| 3468 | if (pos < 0) // count from end of string |
| 3469 | pos += size(); |
| 3470 | |
| 3471 | if (size_t(pos) >= size_t(size()) || len <= 0) |
| 3472 | return *this; |
| 3473 | |
| 3474 | len = std::min(a: len, b: size() - pos); |
| 3475 | |
| 3476 | if (!d->isShared()) { |
| 3477 | d->erase(b: d.begin() + pos, n: len); |
| 3478 | d.data()[d.size] = u'\0'; |
| 3479 | } else { |
| 3480 | // TODO: either reserve "size()", which is bigger than needed, or |
| 3481 | // modify the shrinking-erase docs of this method (since the size |
| 3482 | // of "copy" won't have any extra capacity any more) |
| 3483 | const qsizetype sz = size() - len; |
| 3484 | QString copy{sz, Qt::Uninitialized}; |
| 3485 | auto begin = d.begin(); |
| 3486 | auto toRemove_start = d.begin() + pos; |
| 3487 | copy.d->copyRanges(ranges: {{.begin: begin, .end: toRemove_start}, |
| 3488 | {.begin: toRemove_start + len, .end: d.end()}}); |
| 3489 | swap(other&: copy); |
| 3490 | } |
| 3491 | return *this; |
| 3492 | } |
| 3493 | |
| 3494 | template<typename T> |
| 3495 | static void removeStringImpl(QString &s, const T &needle, Qt::CaseSensitivity cs) |
| 3496 | { |
| 3497 | const auto needleSize = needle.size(); |
| 3498 | if (!needleSize) |
| 3499 | return; |
| 3500 | |
| 3501 | // avoid detach if nothing to do: |
| 3502 | qsizetype i = s.indexOf(needle, 0, cs); |
| 3503 | if (i < 0) |
| 3504 | return; |
| 3505 | |
| 3506 | QString::DataPointer &dptr = s.data_ptr(); |
| 3507 | auto begin = dptr.begin(); |
| 3508 | auto end = dptr.end(); |
| 3509 | |
| 3510 | auto copyFunc = [&](auto &dst) { |
| 3511 | auto src = begin + i + needleSize; |
| 3512 | while (src < end) { |
| 3513 | i = s.indexOf(needle, std::distance(begin, src), cs); |
| 3514 | auto hit = i == -1 ? end : begin + i; |
| 3515 | dst = std::copy(src, hit, dst); |
| 3516 | src = hit + needleSize; |
| 3517 | } |
| 3518 | return dst; |
| 3519 | }; |
| 3520 | |
| 3521 | if (!dptr->needsDetach()) { |
| 3522 | auto dst = begin + i; |
| 3523 | dst = copyFunc(dst); |
| 3524 | s.truncate(pos: std::distance(first: begin, last: dst)); |
| 3525 | } else { |
| 3526 | QString copy{s.size(), Qt::Uninitialized}; |
| 3527 | auto copy_begin = copy.begin(); |
| 3528 | auto dst = std::copy(first: begin, last: begin + i, result: copy_begin); // Chunk before the first hit |
| 3529 | dst = copyFunc(dst); |
| 3530 | copy.resize(size: std::distance(first: copy_begin, last: dst)); |
| 3531 | s.swap(other&: copy); |
| 3532 | } |
| 3533 | } |
| 3534 | |
| 3535 | /*! |
| 3536 | Removes every occurrence of the given \a str string in this |
| 3537 | string, and returns a reference to this string. |
| 3538 | |
| 3539 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
| 3540 | |
| 3541 | This is the same as \c replace(str, "", cs). |
| 3542 | |
| 3543 | \include qstring.cpp shrinking-erase |
| 3544 | |
| 3545 | \sa replace() |
| 3546 | */ |
| 3547 | QString &QString::remove(const QString &str, Qt::CaseSensitivity cs) |
| 3548 | { |
| 3549 | const auto s = str.d.data(); |
| 3550 | if (QtPrivate::q_points_into_range(p: s, c: d)) |
| 3551 | removeStringImpl(s&: *this, needle: QStringView{QVarLengthArray(s, s + str.size())}, cs); |
| 3552 | else |
| 3553 | removeStringImpl(s&: *this, needle: qToStringViewIgnoringNull(s: str), cs); |
| 3554 | return *this; |
| 3555 | } |
| 3556 | |
| 3557 | /*! |
| 3558 | \since 5.11 |
| 3559 | \overload |
| 3560 | |
| 3561 | Removes every occurrence of the given Latin-1 string viewed by \a str |
| 3562 | from this string, and returns a reference to this string. |
| 3563 | |
| 3564 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
| 3565 | |
| 3566 | This is the same as \c replace(str, "", cs). |
| 3567 | |
| 3568 | \include qstring.cpp shrinking-erase |
| 3569 | |
| 3570 | \sa replace() |
| 3571 | */ |
| 3572 | QString &QString::remove(QLatin1StringView str, Qt::CaseSensitivity cs) |
| 3573 | { |
| 3574 | removeStringImpl(s&: *this, needle: str, cs); |
| 3575 | return *this; |
| 3576 | } |
| 3577 | |
| 3578 | /*! |
| 3579 | \fn QString &QString::removeAt(qsizetype pos) |
| 3580 | |
| 3581 | \since 6.5 |
| 3582 | |
| 3583 | Removes the character at index \a pos. If \a pos is out of bounds |
| 3584 | (i.e. \a pos >= size()), this function does nothing. |
| 3585 | |
| 3586 | \sa remove() |
| 3587 | */ |
| 3588 | |
| 3589 | /*! |
| 3590 | \fn QString &QString::removeFirst() |
| 3591 | |
| 3592 | \since 6.5 |
| 3593 | |
| 3594 | Removes the first character in this string. If the string is empty, |
| 3595 | this function does nothing. |
| 3596 | |
| 3597 | \sa remove() |
| 3598 | */ |
| 3599 | |
| 3600 | /*! |
| 3601 | \fn QString &QString::removeLast() |
| 3602 | |
| 3603 | \since 6.5 |
| 3604 | |
| 3605 | Removes the last character in this string. If the string is empty, |
| 3606 | this function does nothing. |
| 3607 | |
| 3608 | \sa remove() |
| 3609 | */ |
| 3610 | |
| 3611 | /*! |
| 3612 | Removes every occurrence of the character \a ch in this string, and |
| 3613 | returns a reference to this string. |
| 3614 | |
| 3615 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
| 3616 | |
| 3617 | Example: |
| 3618 | |
| 3619 | \snippet qstring/main.cpp 38 |
| 3620 | |
| 3621 | This is the same as \c replace(ch, "", cs). |
| 3622 | |
| 3623 | \include qstring.cpp shrinking-erase |
| 3624 | |
| 3625 | \sa replace() |
| 3626 | */ |
| 3627 | QString &QString::remove(QChar ch, Qt::CaseSensitivity cs) |
| 3628 | { |
| 3629 | const qsizetype idx = indexOf(c: ch, from: 0, cs); |
| 3630 | if (idx == -1) |
| 3631 | return *this; |
| 3632 | |
| 3633 | const bool isCase = cs == Qt::CaseSensitive; |
| 3634 | ch = isCase ? ch : ch.toCaseFolded(); |
| 3635 | auto match = [ch, isCase](QChar x) { |
| 3636 | return ch == (isCase ? x : x.toCaseFolded()); |
| 3637 | }; |
| 3638 | |
| 3639 | |
| 3640 | auto begin = d.begin(); |
| 3641 | auto first_match = begin + idx; |
| 3642 | auto end = d.end(); |
| 3643 | if (!d->isShared()) { |
| 3644 | auto it = std::remove_if(first: first_match, last: end, pred: match); |
| 3645 | d->erase(b: it, n: std::distance(first: it, last: end)); |
| 3646 | d.data()[d.size] = u'\0'; |
| 3647 | } else { |
| 3648 | // Instead of detaching, create a new string and copy all characters except for |
| 3649 | // the ones we're removing |
| 3650 | // TODO: size() is more than the needed since "copy" would be shorter |
| 3651 | QString copy{size(), Qt::Uninitialized}; |
| 3652 | auto dst = copy.d.begin(); |
| 3653 | auto it = std::copy(first: begin, last: first_match, result: dst); // Chunk before idx |
| 3654 | it = std::remove_copy_if(first: first_match + 1, last: end, result: it, pred: match); |
| 3655 | copy.d.size = std::distance(first: dst, last: it); |
| 3656 | copy.d.data()[copy.d.size] = u'\0'; |
| 3657 | *this = std::move(copy); |
| 3658 | } |
| 3659 | return *this; |
| 3660 | } |
| 3661 | |
| 3662 | /*! |
| 3663 | \fn QString &QString::remove(const QRegularExpression &re) |
| 3664 | \since 5.0 |
| 3665 | |
| 3666 | Removes every occurrence of the regular expression \a re in the |
| 3667 | string, and returns a reference to the string. For example: |
| 3668 | |
| 3669 | \snippet qstring/main.cpp 96 |
| 3670 | |
| 3671 | \include qstring.cpp shrinking-erase |
| 3672 | |
| 3673 | \sa indexOf(), lastIndexOf(), replace() |
| 3674 | */ |
| 3675 | |
| 3676 | /*! |
| 3677 | \fn template <typename Predicate> QString &QString::removeIf(Predicate pred) |
| 3678 | \since 6.1 |
| 3679 | |
| 3680 | Removes all elements for which the predicate \a pred returns true |
| 3681 | from the string. Returns a reference to the string. |
| 3682 | |
| 3683 | \sa remove() |
| 3684 | */ |
| 3685 | |
| 3686 | |
| 3687 | /*! \internal |
| 3688 | Instead of detaching, or reallocating if "before" is shorter than "after" |
| 3689 | and there isn't enough capacity, create a new string, copy characters to it |
| 3690 | as needed, then swap it with "str". |
| 3691 | */ |
| 3692 | static void replace_with_copy(QString &str, QSpan<size_t> indices, qsizetype blen, |
| 3693 | QStringView after) |
| 3694 | { |
| 3695 | const qsizetype alen = after.size(); |
| 3696 | const char16_t *after_b = after.utf16(); |
| 3697 | |
| 3698 | const QString::DataPointer &str_d = str.data_ptr(); |
| 3699 | auto src_start = str_d.begin(); |
| 3700 | const qsizetype newSize = str_d.size + indices.size() * (alen - blen); |
| 3701 | QString copy{ newSize, Qt::Uninitialized }; |
| 3702 | QString::DataPointer ©_d = copy.data_ptr(); |
| 3703 | auto dst = copy_d.begin(); |
| 3704 | for (size_t index : indices) { |
| 3705 | auto hit = str_d.begin() + index; |
| 3706 | dst = std::copy(first: src_start, last: hit, result: dst); |
| 3707 | dst = std::copy_n(first: after_b, n: alen, result: dst); |
| 3708 | src_start = hit + blen; |
| 3709 | } |
| 3710 | dst = std::copy(first: src_start, last: str_d.end(), result: dst); |
| 3711 | str.swap(other&: copy); |
| 3712 | } |
| 3713 | |
| 3714 | // No detaching or reallocation is needed |
| 3715 | static void replace_in_place(QString &str, QSpan<size_t> indices, |
| 3716 | qsizetype blen, QStringView after) |
| 3717 | { |
| 3718 | const qsizetype alen = after.size(); |
| 3719 | const char16_t *after_b = after.utf16(); |
| 3720 | const char16_t *after_e = after.utf16() + after.size(); |
| 3721 | |
| 3722 | if (blen == alen) { // Replace in place |
| 3723 | for (size_t index : indices) |
| 3724 | std::copy_n(first: after_b, n: alen, result: str.data_ptr().begin() + index); |
| 3725 | } else if (blen > alen) { // Replace from front |
| 3726 | char16_t *begin = str.data_ptr().begin(); |
| 3727 | char16_t *hit = begin + indices.front(); |
| 3728 | char16_t *to = hit; |
| 3729 | to = std::copy_n(first: after_b, n: alen, result: to); |
| 3730 | char16_t *movestart = hit + blen; |
| 3731 | for (size_t index : indices.sliced(pos: 1)) { |
| 3732 | hit = begin + index; |
| 3733 | to = std::move(first: movestart, last: hit, result: to); |
| 3734 | to = std::copy_n(first: after_b, n: alen, result: to); |
| 3735 | movestart = hit + blen; |
| 3736 | } |
| 3737 | to = std::move(first: movestart, last: str.data_ptr().end(), result: to); |
| 3738 | str.resize(size: std::distance(first: begin, last: to)); |
| 3739 | } else { // blen < alen, Replace from back |
| 3740 | const qsizetype oldSize = str.data_ptr().size; |
| 3741 | const qsizetype adjust = indices.size() * (alen - blen); |
| 3742 | const qsizetype newSize = oldSize + adjust; |
| 3743 | |
| 3744 | str.resize(size: newSize); |
| 3745 | char16_t *begin = str.data_ptr().begin(); |
| 3746 | char16_t *moveend = begin + oldSize; |
| 3747 | char16_t *to = str.data_ptr().end(); |
| 3748 | |
| 3749 | for (auto it = indices.rbegin(), end = indices.rend(); it != end; ++it) { |
| 3750 | char16_t *hit = begin + *it; |
| 3751 | char16_t *movestart = hit + blen; |
| 3752 | to = std::move_backward(first: movestart, last: moveend, result: to); |
| 3753 | to = std::copy_backward(first: after_b, last: after_e, result: to); |
| 3754 | moveend = hit; |
| 3755 | } |
| 3756 | } |
| 3757 | } |
| 3758 | |
| 3759 | static void replace_helper(QString &str, QSpan<size_t> indices, qsizetype blen, QStringView after) |
| 3760 | { |
| 3761 | const qsizetype oldSize = str.data_ptr().size; |
| 3762 | const qsizetype adjust = indices.size() * (after.size() - blen); |
| 3763 | const qsizetype newSize = oldSize + adjust; |
| 3764 | if (str.data_ptr().needsDetach() || needsReallocate(str, newSize)) { |
| 3765 | replace_with_copy(str, indices, blen, after); |
| 3766 | return; |
| 3767 | } |
| 3768 | |
| 3769 | if (QtPrivate::q_points_into_range(p: after.begin(), c: str)) |
| 3770 | // Copy after if it lies inside our own d.b area (which we could |
| 3771 | // possibly invalidate via a realloc or modify by replacement) |
| 3772 | replace_in_place(str, indices, blen, after: QVarLengthArray(after.begin(), after.end())); |
| 3773 | else |
| 3774 | replace_in_place(str, indices, blen, after); |
| 3775 | } |
| 3776 | |
| 3777 | /*! |
| 3778 | \fn QString &QString::replace(qsizetype position, qsizetype n, const QString &after) |
| 3779 | |
| 3780 | Replaces \a n characters beginning at index \a position with |
| 3781 | the string \a after and returns a reference to this string. |
| 3782 | |
| 3783 | \note If the specified \a position index is within the string, |
| 3784 | but \a position + \a n goes outside the strings range, |
| 3785 | then \a n will be adjusted to stop at the end of the string. |
| 3786 | |
| 3787 | Example: |
| 3788 | |
| 3789 | \snippet qstring/main.cpp 40 |
| 3790 | |
| 3791 | \sa insert(), remove() |
| 3792 | */ |
| 3793 | QString &QString::replace(qsizetype pos, qsizetype len, const QString &after) |
| 3794 | { |
| 3795 | return replace(i: pos, len, s: after.constData(), slen: after.size()); |
| 3796 | } |
| 3797 | |
| 3798 | /*! |
| 3799 | \fn QString &QString::replace(qsizetype position, qsizetype n, const QChar *after, qsizetype alen) |
| 3800 | \overload replace() |
| 3801 | Replaces \a n characters beginning at index \a position with the |
| 3802 | first \a alen characters of the QChar array \a after and returns a |
| 3803 | reference to this string. |
| 3804 | |
| 3805 | \a n must not be negative. |
| 3806 | */ |
| 3807 | QString &QString::replace(qsizetype pos, qsizetype len, const QChar *after, qsizetype alen) |
| 3808 | { |
| 3809 | Q_PRE(len >= 0); |
| 3810 | |
| 3811 | if (size_t(pos) > size_t(this->size())) |
| 3812 | return *this; |
| 3813 | if (len > this->size() - pos) |
| 3814 | len = this->size() - pos; |
| 3815 | |
| 3816 | size_t index = pos; |
| 3817 | replace_helper(str&: *this, indices: QSpan(&index, 1), blen: len, after: QStringView{after, alen}); |
| 3818 | return *this; |
| 3819 | } |
| 3820 | |
| 3821 | /*! |
| 3822 | \fn QString &QString::replace(qsizetype position, qsizetype n, QChar after) |
| 3823 | \overload replace() |
| 3824 | |
| 3825 | Replaces \a n characters beginning at index \a position with the |
| 3826 | character \a after and returns a reference to this string. |
| 3827 | */ |
| 3828 | QString &QString::replace(qsizetype pos, qsizetype len, QChar after) |
| 3829 | { |
| 3830 | return replace(pos, len, after: &after, alen: 1); |
| 3831 | } |
| 3832 | |
| 3833 | /*! |
| 3834 | \overload replace() |
| 3835 | Replaces every occurrence of the string \a before with the string \a |
| 3836 | after and returns a reference to this string. |
| 3837 | |
| 3838 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
| 3839 | |
| 3840 | Example: |
| 3841 | |
| 3842 | \snippet qstring/main.cpp 41 |
| 3843 | |
| 3844 | \note The replacement text is not rescanned after it is inserted. |
| 3845 | |
| 3846 | Example: |
| 3847 | |
| 3848 | \snippet qstring/main.cpp 86 |
| 3849 | |
| 3850 | //! [empty-before-arg-in-replace] |
| 3851 | \note If you use an empty \a before argument, the \a after argument will be |
| 3852 | inserted \e {before and after} each character of the string. |
| 3853 | //! [empty-before-arg-in-replace] |
| 3854 | |
| 3855 | */ |
| 3856 | QString &QString::replace(const QString &before, const QString &after, Qt::CaseSensitivity cs) |
| 3857 | { |
| 3858 | return replace(before: before.constData(), blen: before.size(), after: after.constData(), alen: after.size(), cs); |
| 3859 | } |
| 3860 | |
| 3861 | /*! |
| 3862 | \since 4.5 |
| 3863 | \overload replace() |
| 3864 | |
| 3865 | Replaces each occurrence in this string of the first \a blen |
| 3866 | characters of \a before with the first \a alen characters of \a |
| 3867 | after and returns a reference to this string. |
| 3868 | |
| 3869 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
| 3870 | |
| 3871 | \note If \a before points to an \e empty string (that is, \a blen == 0), |
| 3872 | the string pointed to by \a after will be inserted \e {before and after} |
| 3873 | each character in this string. |
| 3874 | */ |
| 3875 | QString &QString::replace(const QChar *before, qsizetype blen, |
| 3876 | const QChar *after, qsizetype alen, |
| 3877 | Qt::CaseSensitivity cs) |
| 3878 | { |
| 3879 | if (isEmpty()) { |
| 3880 | if (blen) |
| 3881 | return *this; |
| 3882 | } else { |
| 3883 | if (cs == Qt::CaseSensitive && before == after && blen == alen) |
| 3884 | return *this; |
| 3885 | } |
| 3886 | if (alen == 0 && blen == 0) |
| 3887 | return *this; |
| 3888 | if (alen == 1 && blen == 1) |
| 3889 | return replace(before: *before, after: *after, cs); |
| 3890 | |
| 3891 | QStringMatcher matcher(before, blen, cs); |
| 3892 | |
| 3893 | qsizetype index = 0; |
| 3894 | |
| 3895 | QVarLengthArray<size_t> indices; |
| 3896 | while ((index = matcher.indexIn(str: *this, from: index)) != -1) { |
| 3897 | indices.push_back(t: index); |
| 3898 | if (blen) // Step over before: |
| 3899 | index += blen; |
| 3900 | else // Only count one instance of empty between any two characters: |
| 3901 | index++; |
| 3902 | } |
| 3903 | if (indices.isEmpty()) |
| 3904 | return *this; |
| 3905 | |
| 3906 | replace_helper(str&: *this, indices, blen, after: QStringView{after, alen}); |
| 3907 | return *this; |
| 3908 | } |
| 3909 | |
| 3910 | /*! |
| 3911 | \overload replace() |
| 3912 | Replaces every occurrence of the character \a ch in the string with |
| 3913 | \a after and returns a reference to this string. |
| 3914 | |
| 3915 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
| 3916 | */ |
| 3917 | QString& QString::replace(QChar ch, const QString &after, Qt::CaseSensitivity cs) |
| 3918 | { |
| 3919 | if (after.size() == 0) |
| 3920 | return remove(ch, cs); |
| 3921 | |
| 3922 | if (after.size() == 1) |
| 3923 | return replace(before: ch, after: after.front(), cs); |
| 3924 | |
| 3925 | if (size() == 0) |
| 3926 | return *this; |
| 3927 | |
| 3928 | const char16_t cc = (cs == Qt::CaseSensitive ? ch.unicode() : ch.toCaseFolded().unicode()); |
| 3929 | |
| 3930 | QVarLengthArray<size_t> indices; |
| 3931 | if (cs == Qt::CaseSensitive) { |
| 3932 | const char16_t *begin = d.begin(); |
| 3933 | const char16_t *end = d.end(); |
| 3934 | QStringView view(begin, end); |
| 3935 | const char16_t *hit = nullptr; |
| 3936 | while ((hit = QtPrivate::qustrchr(str: view, c: cc)) != end) { |
| 3937 | indices.push_back(t: std::distance(first: begin, last: hit)); |
| 3938 | view = QStringView(std::next(x: hit), end); |
| 3939 | } |
| 3940 | } else { |
| 3941 | for (qsizetype i = 0; i < d.size; ++i) |
| 3942 | if (QChar::toCaseFolded(ucs4: d.data()[i]) == cc) |
| 3943 | indices.push_back(t: i); |
| 3944 | } |
| 3945 | if (indices.isEmpty()) |
| 3946 | return *this; |
| 3947 | |
| 3948 | replace_helper(str&: *this, indices, blen: 1, after); |
| 3949 | return *this; |
| 3950 | } |
| 3951 | |
| 3952 | /*! |
| 3953 | \overload replace() |
| 3954 | Replaces every occurrence of the character \a before with the |
| 3955 | character \a after and returns a reference to this string. |
| 3956 | |
| 3957 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
| 3958 | */ |
| 3959 | QString& QString::replace(QChar before, QChar after, Qt::CaseSensitivity cs) |
| 3960 | { |
| 3961 | const qsizetype idx = indexOf(c: before, from: 0, cs); |
| 3962 | if (idx == -1) |
| 3963 | return *this; |
| 3964 | |
| 3965 | const char16_t achar = after.unicode(); |
| 3966 | char16_t bchar = before.unicode(); |
| 3967 | |
| 3968 | auto matchesCIS = [](char16_t beforeChar) { |
| 3969 | return [beforeChar](char16_t ch) { return foldAndCompare(a: ch, b: beforeChar); }; |
| 3970 | }; |
| 3971 | |
| 3972 | auto hit = d.begin() + idx; |
| 3973 | if (!d.needsDetach()) { |
| 3974 | *hit++ = achar; |
| 3975 | if (cs == Qt::CaseSensitive) { |
| 3976 | std::replace(first: hit, last: d.end(), old_value: bchar, new_value: achar); |
| 3977 | } else { |
| 3978 | bchar = foldCase(ch: bchar); |
| 3979 | std::replace_if(first: hit, last: d.end(), pred: matchesCIS(bchar), new_value: achar); |
| 3980 | } |
| 3981 | } else { |
| 3982 | QString other{ d.size, Qt::Uninitialized }; |
| 3983 | auto dest = std::copy(first: d.begin(), last: hit, result: other.d.begin()); |
| 3984 | *dest++ = achar; |
| 3985 | ++hit; |
| 3986 | if (cs == Qt::CaseSensitive) { |
| 3987 | std::replace_copy(first: hit, last: d.end(), result: dest, old_value: bchar, new_value: achar); |
| 3988 | } else { |
| 3989 | bchar = foldCase(ch: bchar); |
| 3990 | std::replace_copy_if(first: hit, last: d.end(), result: dest, pred: matchesCIS(bchar), new_value: achar); |
| 3991 | } |
| 3992 | |
| 3993 | swap(other); |
| 3994 | } |
| 3995 | return *this; |
| 3996 | } |
| 3997 | |
| 3998 | /*! |
| 3999 | \since 4.5 |
| 4000 | \overload replace() |
| 4001 | |
| 4002 | Replaces every occurrence in this string of the Latin-1 string viewed |
| 4003 | by \a before with the Latin-1 string viewed by \a after, and returns a |
| 4004 | reference to this string. |
| 4005 | |
| 4006 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
| 4007 | |
| 4008 | \note The text is not rescanned after a replacement. |
| 4009 | |
| 4010 | \include qstring.cpp empty-before-arg-in-replace |
| 4011 | */ |
| 4012 | QString &QString::replace(QLatin1StringView before, QLatin1StringView after, Qt::CaseSensitivity cs) |
| 4013 | { |
| 4014 | const qsizetype alen = after.size(); |
| 4015 | const qsizetype blen = before.size(); |
| 4016 | if (blen == 1 && alen == 1) |
| 4017 | return replace(before: before.front(), after: after.front(), cs); |
| 4018 | |
| 4019 | QVarLengthArray<char16_t> a = qt_from_latin1_to_qvla(str: after); |
| 4020 | QVarLengthArray<char16_t> b = qt_from_latin1_to_qvla(str: before); |
| 4021 | return replace(before: (const QChar *)b.data(), blen, after: (const QChar *)a.data(), alen, cs); |
| 4022 | } |
| 4023 | |
| 4024 | /*! |
| 4025 | \since 4.5 |
| 4026 | \overload replace() |
| 4027 | |
| 4028 | Replaces every occurrence in this string of the Latin-1 string viewed |
| 4029 | by \a before with the string \a after, and returns a reference to this |
| 4030 | string. |
| 4031 | |
| 4032 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
| 4033 | |
| 4034 | \note The text is not rescanned after a replacement. |
| 4035 | |
| 4036 | \include qstring.cpp empty-before-arg-in-replace |
| 4037 | */ |
| 4038 | QString &QString::replace(QLatin1StringView before, const QString &after, Qt::CaseSensitivity cs) |
| 4039 | { |
| 4040 | const qsizetype blen = before.size(); |
| 4041 | if (blen == 1 && after.size() == 1) |
| 4042 | return replace(before: before.front(), after: after.front(), cs); |
| 4043 | |
| 4044 | QVarLengthArray<char16_t> b = qt_from_latin1_to_qvla(str: before); |
| 4045 | return replace(before: (const QChar *)b.data(), blen, after: after.constData(), alen: after.d.size, cs); |
| 4046 | } |
| 4047 | |
| 4048 | /*! |
| 4049 | \since 4.5 |
| 4050 | \overload replace() |
| 4051 | |
| 4052 | Replaces every occurrence of the string \a before with the string \a |
| 4053 | after and returns a reference to this string. |
| 4054 | |
| 4055 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
| 4056 | |
| 4057 | \note The text is not rescanned after a replacement. |
| 4058 | |
| 4059 | \include qstring.cpp empty-before-arg-in-replace |
| 4060 | */ |
| 4061 | QString &QString::replace(const QString &before, QLatin1StringView after, Qt::CaseSensitivity cs) |
| 4062 | { |
| 4063 | const qsizetype alen = after.size(); |
| 4064 | if (before.size() == 1 && alen == 1) |
| 4065 | return replace(before: before.front(), after: after.front(), cs); |
| 4066 | |
| 4067 | QVarLengthArray<char16_t> a = qt_from_latin1_to_qvla(str: after); |
| 4068 | return replace(before: before.constData(), blen: before.d.size, after: (const QChar *)a.data(), alen, cs); |
| 4069 | } |
| 4070 | |
| 4071 | /*! |
| 4072 | \since 4.5 |
| 4073 | \overload replace() |
| 4074 | |
| 4075 | Replaces every occurrence of the character \a c with the string \a |
| 4076 | after and returns a reference to this string. |
| 4077 | |
| 4078 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
| 4079 | |
| 4080 | \note The text is not rescanned after a replacement. |
| 4081 | */ |
| 4082 | QString &QString::replace(QChar c, QLatin1StringView after, Qt::CaseSensitivity cs) |
| 4083 | { |
| 4084 | const qsizetype alen = after.size(); |
| 4085 | if (alen == 1) |
| 4086 | return replace(before: c, after: after.front(), cs); |
| 4087 | |
| 4088 | QVarLengthArray<char16_t> a = qt_from_latin1_to_qvla(str: after); |
| 4089 | return replace(before: &c, blen: 1, after: (const QChar *)a.data(), alen, cs); |
| 4090 | } |
| 4091 | |
| 4092 | /*! |
| 4093 | \fn bool QString::operator==(const QString &lhs, const QString &rhs) |
| 4094 | \overload operator==() |
| 4095 | |
| 4096 | Returns \c true if string \a lhs is equal to string \a rhs; otherwise |
| 4097 | returns \c false. |
| 4098 | |
| 4099 | \include qstring.cpp compare-isNull-vs-isEmpty |
| 4100 | |
| 4101 | \sa {Comparing Strings} |
| 4102 | */ |
| 4103 | |
| 4104 | /*! |
| 4105 | \fn bool QString::operator==(const QString &lhs, const QLatin1StringView &rhs) |
| 4106 | |
| 4107 | \overload operator==() |
| 4108 | |
| 4109 | Returns \c true if \a lhs is equal to \a rhs; otherwise |
| 4110 | returns \c false. |
| 4111 | */ |
| 4112 | |
| 4113 | /*! |
| 4114 | \fn bool QString::operator==(const QLatin1StringView &lhs, const QString &rhs) |
| 4115 | |
| 4116 | \overload operator==() |
| 4117 | |
| 4118 | Returns \c true if \a lhs is equal to \a rhs; otherwise |
| 4119 | returns \c false. |
| 4120 | */ |
| 4121 | |
| 4122 | /*! \fn bool QString::operator==(const QString &lhs, const QByteArray &rhs) |
| 4123 | |
| 4124 | \overload operator==() |
| 4125 | |
| 4126 | The \a rhs byte array is converted to a QUtf8StringView. |
| 4127 | |
| 4128 | You can disable this operator by defining |
| 4129 | \l QT_NO_CAST_FROM_ASCII when you compile your applications. This |
| 4130 | can be useful if you want to ensure that all user-visible strings |
| 4131 | go through QObject::tr(), for example. |
| 4132 | |
| 4133 | Returns \c true if string \a lhs is lexically equal to \a rhs. |
| 4134 | Otherwise returns \c false. |
| 4135 | */ |
| 4136 | |
| 4137 | /*! \fn bool QString::operator==(const QString &lhs, const char * const &rhs) |
| 4138 | |
| 4139 | \overload operator==() |
| 4140 | |
| 4141 | The \a rhs const char pointer is converted to a QUtf8StringView. |
| 4142 | |
| 4143 | You can disable this operator by defining |
| 4144 | \l QT_NO_CAST_FROM_ASCII when you compile your applications. This |
| 4145 | can be useful if you want to ensure that all user-visible strings |
| 4146 | go through QObject::tr(), for example. |
| 4147 | */ |
| 4148 | |
| 4149 | /*! |
| 4150 | \fn bool QString::operator<(const QString &lhs, const QString &rhs) |
| 4151 | |
| 4152 | \overload operator<() |
| 4153 | |
| 4154 | Returns \c true if string \a lhs is lexically less than string |
| 4155 | \a rhs; otherwise returns \c false. |
| 4156 | |
| 4157 | \sa {Comparing Strings} |
| 4158 | */ |
| 4159 | |
| 4160 | /*! |
| 4161 | \fn bool QString::operator<(const QString &lhs, const QLatin1StringView &rhs) |
| 4162 | |
| 4163 | \overload operator<() |
| 4164 | |
| 4165 | Returns \c true if \a lhs is lexically less than \a rhs; |
| 4166 | otherwise returns \c false. |
| 4167 | */ |
| 4168 | |
| 4169 | /*! |
| 4170 | \fn bool QString::operator<(const QLatin1StringView &lhs, const QString &rhs) |
| 4171 | |
| 4172 | \overload operator<() |
| 4173 | |
| 4174 | Returns \c true if \a lhs is lexically less than \a rhs; |
| 4175 | otherwise returns \c false. |
| 4176 | */ |
| 4177 | |
| 4178 | /*! \fn bool QString::operator<(const QString &lhs, const QByteArray &rhs) |
| 4179 | |
| 4180 | \overload operator<() |
| 4181 | |
| 4182 | The \a rhs byte array is converted to a QUtf8StringView. |
| 4183 | If any NUL characters ('\\0') are embedded in the byte array, they will be |
| 4184 | included in the transformation. |
| 4185 | |
| 4186 | You can disable this operator |
| 4187 | \l QT_NO_CAST_FROM_ASCII when you compile your applications. This |
| 4188 | can be useful if you want to ensure that all user-visible strings |
| 4189 | go through QObject::tr(), for example. |
| 4190 | */ |
| 4191 | |
| 4192 | /*! \fn bool QString::operator<(const QString &lhs, const char * const &rhs) |
| 4193 | |
| 4194 | Returns \c true if string \a lhs is lexically less than string \a rhs. |
| 4195 | Otherwise returns \c false. |
| 4196 | |
| 4197 | \overload operator<() |
| 4198 | |
| 4199 | The \a rhs const char pointer is converted to a QUtf8StringView. |
| 4200 | |
| 4201 | You can disable this operator by defining |
| 4202 | \l QT_NO_CAST_FROM_ASCII when you compile your applications. This |
| 4203 | can be useful if you want to ensure that all user-visible strings |
| 4204 | go through QObject::tr(), for example. |
| 4205 | */ |
| 4206 | |
| 4207 | /*! \fn bool QString::operator<=(const QString &lhs, const QString &rhs) |
| 4208 | |
| 4209 | Returns \c true if string \a lhs is lexically less than or equal to |
| 4210 | string \a rhs; otherwise returns \c false. |
| 4211 | |
| 4212 | \sa {Comparing Strings} |
| 4213 | */ |
| 4214 | |
| 4215 | /*! |
| 4216 | \fn bool QString::operator<=(const QString &lhs, const QLatin1StringView &rhs) |
| 4217 | |
| 4218 | \overload operator<=() |
| 4219 | |
| 4220 | Returns \c true if \a lhs is lexically less than or equal to \a rhs; |
| 4221 | otherwise returns \c false. |
| 4222 | */ |
| 4223 | |
| 4224 | /*! |
| 4225 | \fn bool QString::operator<=(const QLatin1StringView &lhs, const QString &rhs) |
| 4226 | |
| 4227 | \overload operator<=() |
| 4228 | |
| 4229 | Returns \c true if \a lhs is lexically less than or equal to \a rhs; |
| 4230 | otherwise returns \c false. |
| 4231 | */ |
| 4232 | |
| 4233 | /*! \fn bool QString::operator<=(const QString &lhs, const QByteArray &rhs) |
| 4234 | |
| 4235 | \overload operator<=() |
| 4236 | |
| 4237 | The \a rhs byte array is converted to a QUtf8StringView. |
| 4238 | If any NUL characters ('\\0') are embedded in the byte array, they will be |
| 4239 | included in the transformation. |
| 4240 | |
| 4241 | You can disable this operator by defining |
| 4242 | \l QT_NO_CAST_FROM_ASCII when you compile your applications. This |
| 4243 | can be useful if you want to ensure that all user-visible strings |
| 4244 | go through QObject::tr(), for example. |
| 4245 | */ |
| 4246 | |
| 4247 | /*! \fn bool QString::operator<=(const QString &lhs, const char * const &rhs) |
| 4248 | |
| 4249 | \overload operator<=() |
| 4250 | |
| 4251 | The \a rhs const char pointer is converted to a QUtf8StringView. |
| 4252 | |
| 4253 | You can disable this operator by defining |
| 4254 | \l QT_NO_CAST_FROM_ASCII when you compile your applications. This |
| 4255 | can be useful if you want to ensure that all user-visible strings |
| 4256 | go through QObject::tr(), for example. |
| 4257 | */ |
| 4258 | |
| 4259 | /*! \fn bool QString::operator>(const QString &lhs, const QString &rhs) |
| 4260 | |
| 4261 | Returns \c true if string \a lhs is lexically greater than string \a rhs; |
| 4262 | otherwise returns \c false. |
| 4263 | |
| 4264 | \sa {Comparing Strings} |
| 4265 | */ |
| 4266 | |
| 4267 | /*! |
| 4268 | \fn bool QString::operator>(const QString &lhs, const QLatin1StringView &rhs) |
| 4269 | |
| 4270 | \overload operator>() |
| 4271 | |
| 4272 | Returns \c true if \a lhs is lexically greater than \a rhs; |
| 4273 | otherwise returns \c false. |
| 4274 | */ |
| 4275 | |
| 4276 | /*! |
| 4277 | \fn bool QString::operator>(const QLatin1StringView &lhs, const QString &rhs) |
| 4278 | |
| 4279 | \overload operator>() |
| 4280 | |
| 4281 | Returns \c true if \a lhs is lexically greater than \a rhs; |
| 4282 | otherwise returns \c false. |
| 4283 | */ |
| 4284 | |
| 4285 | /*! \fn bool QString::operator>(const QString &lhs, const QByteArray &rhs) |
| 4286 | |
| 4287 | \overload operator>() |
| 4288 | |
| 4289 | The \a rhs byte array is converted to a QUtf8StringView. |
| 4290 | If any NUL characters ('\\0') are embedded in the byte array, they will be |
| 4291 | included in the transformation. |
| 4292 | |
| 4293 | You can disable this operator by defining |
| 4294 | \l QT_NO_CAST_FROM_ASCII when you compile your applications. This |
| 4295 | can be useful if you want to ensure that all user-visible strings |
| 4296 | go through QObject::tr(), for example. |
| 4297 | */ |
| 4298 | |
| 4299 | /*! \fn bool QString::operator>(const QString &lhs, const char * const &rhs) |
| 4300 | |
| 4301 | \overload operator>() |
| 4302 | |
| 4303 | The \a rhs const char pointer is converted to a QUtf8StringView. |
| 4304 | |
| 4305 | You can disable this operator by defining \l QT_NO_CAST_FROM_ASCII |
| 4306 | when you compile your applications. This can be useful if you want |
| 4307 | to ensure that all user-visible strings go through QObject::tr(), |
| 4308 | for example. |
| 4309 | */ |
| 4310 | |
| 4311 | /*! \fn bool QString::operator>=(const QString &lhs, const QString &rhs) |
| 4312 | |
| 4313 | Returns \c true if string \a lhs is lexically greater than or equal to |
| 4314 | string \a rhs; otherwise returns \c false. |
| 4315 | |
| 4316 | \sa {Comparing Strings} |
| 4317 | */ |
| 4318 | |
| 4319 | /*! |
| 4320 | \fn bool QString::operator>=(const QString &lhs, const QLatin1StringView &rhs) |
| 4321 | |
| 4322 | \overload operator>=() |
| 4323 | |
| 4324 | Returns \c true if \a lhs is lexically greater than or equal to \a rhs; |
| 4325 | otherwise returns \c false. |
| 4326 | */ |
| 4327 | |
| 4328 | /*! |
| 4329 | \fn bool QString::operator>=(const QLatin1StringView &lhs, const QString &rhs) |
| 4330 | |
| 4331 | \overload operator>=() |
| 4332 | |
| 4333 | Returns \c true if \a lhs is lexically greater than or equal to \a rhs; |
| 4334 | otherwise returns \c false. |
| 4335 | */ |
| 4336 | |
| 4337 | /*! \fn bool QString::operator>=(const QString &lhs, const QByteArray &rhs) |
| 4338 | |
| 4339 | \overload operator>=() |
| 4340 | |
| 4341 | The \a rhs byte array is converted to a QUtf8StringView. |
| 4342 | If any NUL characters ('\\0') are embedded in the byte array, they will be |
| 4343 | included in the transformation. |
| 4344 | |
| 4345 | You can disable this operator by defining \l QT_NO_CAST_FROM_ASCII |
| 4346 | when you compile your applications. This can be useful if you want |
| 4347 | to ensure that all user-visible strings go through QObject::tr(), |
| 4348 | for example. |
| 4349 | */ |
| 4350 | |
| 4351 | /*! \fn bool QString::operator>=(const QString &lhs, const char * const &rhs) |
| 4352 | |
| 4353 | \overload operator>=() |
| 4354 | |
| 4355 | The \a rhs const char pointer is converted to a QUtf8StringView. |
| 4356 | |
| 4357 | You can disable this operator by defining \l QT_NO_CAST_FROM_ASCII |
| 4358 | when you compile your applications. This can be useful if you want |
| 4359 | to ensure that all user-visible strings go through QObject::tr(), |
| 4360 | for example. |
| 4361 | */ |
| 4362 | |
| 4363 | /*! \fn bool QString::operator!=(const QString &lhs, const QString &rhs) |
| 4364 | |
| 4365 | Returns \c true if string \a lhs is not equal to string \a rhs; |
| 4366 | otherwise returns \c false. |
| 4367 | |
| 4368 | \sa {Comparing Strings} |
| 4369 | */ |
| 4370 | |
| 4371 | /*! \fn bool QString::operator!=(const QString &lhs, const QLatin1StringView &rhs) |
| 4372 | |
| 4373 | Returns \c true if string \a lhs is not equal to string \a rhs. |
| 4374 | Otherwise returns \c false. |
| 4375 | |
| 4376 | \overload operator!=() |
| 4377 | */ |
| 4378 | |
| 4379 | /*! \fn bool QString::operator!=(const QString &lhs, const QByteArray &rhs) |
| 4380 | |
| 4381 | \overload operator!=() |
| 4382 | |
| 4383 | The \a rhs byte array is converted to a QUtf8StringView. |
| 4384 | If any NUL characters ('\\0') are embedded in the byte array, they will be |
| 4385 | included in the transformation. |
| 4386 | |
| 4387 | You can disable this operator by defining \l QT_NO_CAST_FROM_ASCII |
| 4388 | when you compile your applications. This can be useful if you want |
| 4389 | to ensure that all user-visible strings go through QObject::tr(), |
| 4390 | for example. |
| 4391 | */ |
| 4392 | |
| 4393 | /*! \fn bool QString::operator!=(const QString &lhs, const char * const &rhs) |
| 4394 | |
| 4395 | \overload operator!=() |
| 4396 | |
| 4397 | The \a rhs const char pointer is converted to a QUtf8StringView. |
| 4398 | |
| 4399 | You can disable this operator by defining |
| 4400 | \l QT_NO_CAST_FROM_ASCII when you compile your applications. This |
| 4401 | can be useful if you want to ensure that all user-visible strings |
| 4402 | go through QObject::tr(), for example. |
| 4403 | */ |
| 4404 | |
| 4405 | /*! \fn bool QString::operator==(const QByteArray &lhs, const QString &rhs) |
| 4406 | |
| 4407 | Returns \c true if byte array \a lhs is equal to the UTF-8 encoding of |
| 4408 | \a rhs; otherwise returns \c false. |
| 4409 | |
| 4410 | The comparison is case sensitive. |
| 4411 | |
| 4412 | You can disable this operator by defining \c |
| 4413 | QT_NO_CAST_FROM_ASCII when you compile your applications. You |
| 4414 | then need to call QString::fromUtf8(), QString::fromLatin1(), |
| 4415 | or QString::fromLocal8Bit() explicitly if you want to convert the byte |
| 4416 | array to a QString before doing the comparison. |
| 4417 | */ |
| 4418 | |
| 4419 | /*! \fn bool QString::operator!=(const QByteArray &lhs, const QString &rhs) |
| 4420 | |
| 4421 | Returns \c true if byte array \a lhs is not equal to the UTF-8 encoding of |
| 4422 | \a rhs; otherwise returns \c false. |
| 4423 | |
| 4424 | The comparison is case sensitive. |
| 4425 | |
| 4426 | You can disable this operator by defining \c |
| 4427 | QT_NO_CAST_FROM_ASCII when you compile your applications. You |
| 4428 | then need to call QString::fromUtf8(), QString::fromLatin1(), |
| 4429 | or QString::fromLocal8Bit() explicitly if you want to convert the byte |
| 4430 | array to a QString before doing the comparison. |
| 4431 | */ |
| 4432 | |
| 4433 | /*! \fn bool QString::operator<(const QByteArray &lhs, const QString &rhs) |
| 4434 | |
| 4435 | Returns \c true if byte array \a lhs is lexically less than the UTF-8 encoding |
| 4436 | of \a rhs; otherwise returns \c false. |
| 4437 | |
| 4438 | The comparison is case sensitive. |
| 4439 | |
| 4440 | You can disable this operator by defining \c |
| 4441 | QT_NO_CAST_FROM_ASCII when you compile your applications. You |
| 4442 | then need to call QString::fromUtf8(), QString::fromLatin1(), |
| 4443 | or QString::fromLocal8Bit() explicitly if you want to convert the byte |
| 4444 | array to a QString before doing the comparison. |
| 4445 | */ |
| 4446 | |
| 4447 | /*! \fn bool QString::operator>(const QByteArray &lhs, const QString &rhs) |
| 4448 | |
| 4449 | Returns \c true if byte array \a lhs is lexically greater than the UTF-8 |
| 4450 | encoding of \a rhs; otherwise returns \c false. |
| 4451 | |
| 4452 | The comparison is case sensitive. |
| 4453 | |
| 4454 | You can disable this operator by defining \c |
| 4455 | QT_NO_CAST_FROM_ASCII when you compile your applications. You |
| 4456 | then need to call QString::fromUtf8(), QString::fromLatin1(), |
| 4457 | or QString::fromLocal8Bit() explicitly if you want to convert the byte |
| 4458 | array to a QString before doing the comparison. |
| 4459 | */ |
| 4460 | |
| 4461 | /*! \fn bool QString::operator<=(const QByteArray &lhs, const QString &rhs) |
| 4462 | |
| 4463 | Returns \c true if byte array \a lhs is lexically less than or equal to the |
| 4464 | UTF-8 encoding of \a rhs; otherwise returns \c false. |
| 4465 | |
| 4466 | The comparison is case sensitive. |
| 4467 | |
| 4468 | You can disable this operator by defining \c |
| 4469 | QT_NO_CAST_FROM_ASCII when you compile your applications. You |
| 4470 | then need to call QString::fromUtf8(), QString::fromLatin1(), |
| 4471 | or QString::fromLocal8Bit() explicitly if you want to convert the byte |
| 4472 | array to a QString before doing the comparison. |
| 4473 | */ |
| 4474 | |
| 4475 | /*! \fn bool QString::operator>=(const QByteArray &lhs, const QString &rhs) |
| 4476 | |
| 4477 | Returns \c true if byte array \a lhs is greater than or equal to the UTF-8 |
| 4478 | encoding of \a rhs; otherwise returns \c false. |
| 4479 | |
| 4480 | The comparison is case sensitive. |
| 4481 | |
| 4482 | You can disable this operator by defining \c |
| 4483 | QT_NO_CAST_FROM_ASCII when you compile your applications. You |
| 4484 | then need to call QString::fromUtf8(), QString::fromLatin1(), |
| 4485 | or QString::fromLocal8Bit() explicitly if you want to convert the byte |
| 4486 | array to a QString before doing the comparison. |
| 4487 | */ |
| 4488 | |
| 4489 | /*! |
| 4490 | \include qstring.qdocinc {qstring-first-index-of} {string} {str} |
| 4491 | |
| 4492 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
| 4493 | |
| 4494 | Example: |
| 4495 | |
| 4496 | \snippet qstring/main.cpp 24 |
| 4497 | |
| 4498 | \include qstring.qdocinc negative-index-start-search-from-end |
| 4499 | |
| 4500 | \sa lastIndexOf(), contains(), count() |
| 4501 | */ |
| 4502 | qsizetype QString::indexOf(const QString &str, qsizetype from, Qt::CaseSensitivity cs) const |
| 4503 | { |
| 4504 | return QtPrivate::findString(haystack: QStringView(unicode(), size()), from, needle: QStringView(str.unicode(), str.size()), cs); |
| 4505 | } |
| 4506 | |
| 4507 | /*! |
| 4508 | \fn qsizetype QString::indexOf(QStringView str, qsizetype from, Qt::CaseSensitivity cs) const |
| 4509 | \since 5.14 |
| 4510 | \overload indexOf() |
| 4511 | |
| 4512 | \include qstring.qdocinc {qstring-first-index-of} {string view} {str} |
| 4513 | |
| 4514 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
| 4515 | |
| 4516 | \include qstring.qdocinc negative-index-start-search-from-end |
| 4517 | |
| 4518 | \sa QStringView::indexOf(), lastIndexOf(), contains(), count() |
| 4519 | */ |
| 4520 | |
| 4521 | /*! |
| 4522 | \since 4.5 |
| 4523 | |
| 4524 | \include {qstring.qdocinc} {qstring-first-index-of} {Latin-1 string viewed by} {str} |
| 4525 | |
| 4526 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
| 4527 | |
| 4528 | Example: |
| 4529 | |
| 4530 | \snippet qstring/main.cpp 24 |
| 4531 | |
| 4532 | \include qstring.qdocinc negative-index-start-search-from-end |
| 4533 | |
| 4534 | \sa lastIndexOf(), contains(), count() |
| 4535 | */ |
| 4536 | |
| 4537 | qsizetype QString::indexOf(QLatin1StringView str, qsizetype from, Qt::CaseSensitivity cs) const |
| 4538 | { |
| 4539 | return QtPrivate::findString(haystack: QStringView(unicode(), size()), from, needle: str, cs); |
| 4540 | } |
| 4541 | |
| 4542 | /*! |
| 4543 | \fn qsizetype QString::indexOf(QChar ch, qsizetype from, Qt::CaseSensitivity cs) const |
| 4544 | \overload indexOf() |
| 4545 | |
| 4546 | \include qstring.qdocinc {qstring-first-index-of} {character} {ch} |
| 4547 | */ |
| 4548 | |
| 4549 | /*! |
| 4550 | \include qstring.qdocinc {qstring-last-index-of} {string} {str} |
| 4551 | |
| 4552 | \include qstring.qdocinc negative-index-start-search-from-end |
| 4553 | |
| 4554 | Returns -1 if \a str is not found. |
| 4555 | |
| 4556 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
| 4557 | |
| 4558 | Example: |
| 4559 | |
| 4560 | \snippet qstring/main.cpp 29 |
| 4561 | |
| 4562 | \note When searching for a 0-length \a str, the match at the end of |
| 4563 | the data is excluded from the search by a negative \a from, even |
| 4564 | though \c{-1} is normally thought of as searching from the end of the |
| 4565 | string: the match at the end is \e after the last character, so it is |
| 4566 | excluded. To include such a final empty match, either give a positive |
| 4567 | value for \a from or omit the \a from parameter entirely. |
| 4568 | |
| 4569 | \sa indexOf(), contains(), count() |
| 4570 | */ |
| 4571 | qsizetype QString::lastIndexOf(const QString &str, qsizetype from, Qt::CaseSensitivity cs) const |
| 4572 | { |
| 4573 | return QtPrivate::lastIndexOf(haystack: QStringView(*this), from, needle: str, cs); |
| 4574 | } |
| 4575 | |
| 4576 | /*! |
| 4577 | \fn qsizetype QString::lastIndexOf(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
| 4578 | \since 6.2 |
| 4579 | \overload lastIndexOf() |
| 4580 | |
| 4581 | Returns the index position of the last occurrence of the string \a |
| 4582 | str in this string. Returns -1 if \a str is not found. |
| 4583 | |
| 4584 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
| 4585 | |
| 4586 | Example: |
| 4587 | |
| 4588 | \snippet qstring/main.cpp 29 |
| 4589 | |
| 4590 | \sa indexOf(), contains(), count() |
| 4591 | */ |
| 4592 | |
| 4593 | |
| 4594 | /*! |
| 4595 | \since 4.5 |
| 4596 | \overload lastIndexOf() |
| 4597 | |
| 4598 | \include qstring.qdocinc {qstring-last-index-of} {Latin-1 string viewed by} {str} |
| 4599 | |
| 4600 | \include qstring.qdocinc negative-index-start-search-from-end |
| 4601 | |
| 4602 | Returns -1 if \a str is not found. |
| 4603 | |
| 4604 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
| 4605 | |
| 4606 | Example: |
| 4607 | |
| 4608 | \snippet qstring/main.cpp 29 |
| 4609 | |
| 4610 | \note When searching for a 0-length \a str, the match at the end of |
| 4611 | the data is excluded from the search by a negative \a from, even |
| 4612 | though \c{-1} is normally thought of as searching from the end of the |
| 4613 | string: the match at the end is \e after the last character, so it is |
| 4614 | excluded. To include such a final empty match, either give a positive |
| 4615 | value for \a from or omit the \a from parameter entirely. |
| 4616 | |
| 4617 | \sa indexOf(), contains(), count() |
| 4618 | */ |
| 4619 | qsizetype QString::lastIndexOf(QLatin1StringView str, qsizetype from, Qt::CaseSensitivity cs) const |
| 4620 | { |
| 4621 | return QtPrivate::lastIndexOf(haystack: *this, from, needle: str, cs); |
| 4622 | } |
| 4623 | |
| 4624 | /*! |
| 4625 | \fn qsizetype QString::lastIndexOf(QLatin1StringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
| 4626 | \since 6.2 |
| 4627 | \overload lastIndexOf() |
| 4628 | |
| 4629 | Returns the index position of the last occurrence of the string \a |
| 4630 | str in this string. Returns -1 if \a str is not found. |
| 4631 | |
| 4632 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
| 4633 | |
| 4634 | Example: |
| 4635 | |
| 4636 | \snippet qstring/main.cpp 29 |
| 4637 | |
| 4638 | \sa indexOf(), contains(), count() |
| 4639 | */ |
| 4640 | |
| 4641 | /*! |
| 4642 | \fn qsizetype QString::lastIndexOf(QChar ch, qsizetype from, Qt::CaseSensitivity cs) const |
| 4643 | \overload lastIndexOf() |
| 4644 | |
| 4645 | \include qstring.qdocinc {qstring-last-index-of} {character} {ch} |
| 4646 | */ |
| 4647 | |
| 4648 | /*! |
| 4649 | \fn QString::lastIndexOf(QChar ch, Qt::CaseSensitivity) const |
| 4650 | \since 6.3 |
| 4651 | \overload lastIndexOf() |
| 4652 | */ |
| 4653 | |
| 4654 | /*! |
| 4655 | \fn qsizetype QString::lastIndexOf(QStringView str, qsizetype from, Qt::CaseSensitivity cs) const |
| 4656 | \since 5.14 |
| 4657 | \overload lastIndexOf() |
| 4658 | |
| 4659 | \include qstring.qdocinc {qstring-last-index-of} {string view} {str} |
| 4660 | |
| 4661 | \include qstring.qdocinc negative-index-start-search-from-end |
| 4662 | |
| 4663 | Returns -1 if \a str is not found. |
| 4664 | |
| 4665 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
| 4666 | |
| 4667 | \note When searching for a 0-length \a str, the match at the end of |
| 4668 | the data is excluded from the search by a negative \a from, even |
| 4669 | though \c{-1} is normally thought of as searching from the end of the |
| 4670 | string: the match at the end is \e after the last character, so it is |
| 4671 | excluded. To include such a final empty match, either give a positive |
| 4672 | value for \a from or omit the \a from parameter entirely. |
| 4673 | |
| 4674 | \sa indexOf(), contains(), count() |
| 4675 | */ |
| 4676 | |
| 4677 | /*! |
| 4678 | \fn qsizetype QString::lastIndexOf(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
| 4679 | \since 6.2 |
| 4680 | \overload lastIndexOf() |
| 4681 | |
| 4682 | Returns the index position of the last occurrence of the string view \a |
| 4683 | str in this string. Returns -1 if \a str is not found. |
| 4684 | |
| 4685 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
| 4686 | |
| 4687 | \sa indexOf(), contains(), count() |
| 4688 | */ |
| 4689 | |
| 4690 | #if QT_CONFIG(regularexpression) |
| 4691 | struct QStringCapture |
| 4692 | { |
| 4693 | qsizetype pos; |
| 4694 | qsizetype len; |
| 4695 | int no; |
| 4696 | }; |
| 4697 | Q_DECLARE_TYPEINFO(QStringCapture, Q_PRIMITIVE_TYPE); |
| 4698 | |
| 4699 | /*! |
| 4700 | \overload replace() |
| 4701 | \since 5.0 |
| 4702 | |
| 4703 | Replaces every occurrence of the regular expression \a re in the |
| 4704 | string with \a after. Returns a reference to the string. For |
| 4705 | example: |
| 4706 | |
| 4707 | \snippet qstring/main.cpp 87 |
| 4708 | |
| 4709 | For regular expressions containing capturing groups, |
| 4710 | occurrences of \b{\\1}, \b{\\2}, ..., in \a after are replaced |
| 4711 | with the string captured by the corresponding capturing group. |
| 4712 | |
| 4713 | \snippet qstring/main.cpp 88 |
| 4714 | |
| 4715 | \sa indexOf(), lastIndexOf(), remove(), QRegularExpression, QRegularExpressionMatch |
| 4716 | */ |
| 4717 | QString &QString::replace(const QRegularExpression &re, const QString &after) |
| 4718 | { |
| 4719 | if (!re.isValid()) { |
| 4720 | qtWarnAboutInvalidRegularExpression(re, cls: "QString" , method: "replace" ); |
| 4721 | return *this; |
| 4722 | } |
| 4723 | |
| 4724 | const QString copy(*this); |
| 4725 | QRegularExpressionMatchIterator iterator = re.globalMatch(subject: copy); |
| 4726 | if (!iterator.hasNext()) // no matches at all |
| 4727 | return *this; |
| 4728 | |
| 4729 | reallocData(alloc: d.size, option: QArrayData::KeepSize); |
| 4730 | |
| 4731 | qsizetype numCaptures = re.captureCount(); |
| 4732 | |
| 4733 | // 1. build the backreferences list, holding where the backreferences |
| 4734 | // are in the replacement string |
| 4735 | QVarLengthArray<QStringCapture> backReferences; |
| 4736 | const qsizetype al = after.size(); |
| 4737 | const QChar *ac = after.unicode(); |
| 4738 | |
| 4739 | for (qsizetype i = 0; i < al - 1; i++) { |
| 4740 | if (ac[i] == u'\\') { |
| 4741 | int no = ac[i + 1].digitValue(); |
| 4742 | if (no > 0 && no <= numCaptures) { |
| 4743 | QStringCapture backReference; |
| 4744 | backReference.pos = i; |
| 4745 | backReference.len = 2; |
| 4746 | |
| 4747 | if (i < al - 2) { |
| 4748 | int secondDigit = ac[i + 2].digitValue(); |
| 4749 | if (secondDigit != -1 && ((no * 10) + secondDigit) <= numCaptures) { |
| 4750 | no = (no * 10) + secondDigit; |
| 4751 | ++backReference.len; |
| 4752 | } |
| 4753 | } |
| 4754 | |
| 4755 | backReference.no = no; |
| 4756 | backReferences.append(t: backReference); |
| 4757 | } |
| 4758 | } |
| 4759 | } |
| 4760 | |
| 4761 | // 2. iterate on the matches. For every match, copy in chunks |
| 4762 | // - the part before the match |
| 4763 | // - the after string, with the proper replacements for the backreferences |
| 4764 | |
| 4765 | qsizetype newLength = 0; // length of the new string, with all the replacements |
| 4766 | qsizetype lastEnd = 0; |
| 4767 | QVarLengthArray<QStringView> chunks; |
| 4768 | const QStringView copyView{ copy }, afterView{ after }; |
| 4769 | while (iterator.hasNext()) { |
| 4770 | QRegularExpressionMatch match = iterator.next(); |
| 4771 | qsizetype len; |
| 4772 | // add the part before the match |
| 4773 | len = match.capturedStart() - lastEnd; |
| 4774 | if (len > 0) { |
| 4775 | chunks << copyView.mid(pos: lastEnd, n: len); |
| 4776 | newLength += len; |
| 4777 | } |
| 4778 | |
| 4779 | lastEnd = 0; |
| 4780 | // add the after string, with replacements for the backreferences |
| 4781 | for (const QStringCapture &backReference : std::as_const(t&: backReferences)) { |
| 4782 | // part of "after" before the backreference |
| 4783 | len = backReference.pos - lastEnd; |
| 4784 | if (len > 0) { |
| 4785 | chunks << afterView.mid(pos: lastEnd, n: len); |
| 4786 | newLength += len; |
| 4787 | } |
| 4788 | |
| 4789 | // backreference itself |
| 4790 | len = match.capturedLength(nth: backReference.no); |
| 4791 | if (len > 0) { |
| 4792 | chunks << copyView.mid(pos: match.capturedStart(nth: backReference.no), n: len); |
| 4793 | newLength += len; |
| 4794 | } |
| 4795 | |
| 4796 | lastEnd = backReference.pos + backReference.len; |
| 4797 | } |
| 4798 | |
| 4799 | // add the last part of the after string |
| 4800 | len = afterView.size() - lastEnd; |
| 4801 | if (len > 0) { |
| 4802 | chunks << afterView.mid(pos: lastEnd, n: len); |
| 4803 | newLength += len; |
| 4804 | } |
| 4805 | |
| 4806 | lastEnd = match.capturedEnd(); |
| 4807 | } |
| 4808 | |
| 4809 | // 3. trailing string after the last match |
| 4810 | if (copyView.size() > lastEnd) { |
| 4811 | chunks << copyView.mid(pos: lastEnd); |
| 4812 | newLength += copyView.size() - lastEnd; |
| 4813 | } |
| 4814 | |
| 4815 | // 4. assemble the chunks together |
| 4816 | resize(size: newLength); |
| 4817 | qsizetype i = 0; |
| 4818 | QChar *uc = data(); |
| 4819 | for (const QStringView &chunk : std::as_const(t&: chunks)) { |
| 4820 | qsizetype len = chunk.size(); |
| 4821 | memcpy(dest: uc + i, src: chunk.constData(), n: len * sizeof(QChar)); |
| 4822 | i += len; |
| 4823 | } |
| 4824 | |
| 4825 | return *this; |
| 4826 | } |
| 4827 | #endif // QT_CONFIG(regularexpression) |
| 4828 | |
| 4829 | /*! |
| 4830 | Returns the number of (potentially overlapping) occurrences of |
| 4831 | the string \a str in this string. |
| 4832 | |
| 4833 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
| 4834 | |
| 4835 | \sa contains(), indexOf() |
| 4836 | */ |
| 4837 | |
| 4838 | qsizetype QString::count(const QString &str, Qt::CaseSensitivity cs) const |
| 4839 | { |
| 4840 | return QtPrivate::count(haystack: QStringView(unicode(), size()), needle: QStringView(str.unicode(), str.size()), cs); |
| 4841 | } |
| 4842 | |
| 4843 | /*! |
| 4844 | \overload count() |
| 4845 | |
| 4846 | Returns the number of occurrences of character \a ch in the string. |
| 4847 | |
| 4848 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
| 4849 | |
| 4850 | \sa contains(), indexOf() |
| 4851 | */ |
| 4852 | |
| 4853 | qsizetype QString::count(QChar ch, Qt::CaseSensitivity cs) const |
| 4854 | { |
| 4855 | return QtPrivate::count(haystack: QStringView(unicode(), size()), needle: ch, cs); |
| 4856 | } |
| 4857 | |
| 4858 | /*! |
| 4859 | \since 6.0 |
| 4860 | \overload count() |
| 4861 | Returns the number of (potentially overlapping) occurrences of the |
| 4862 | string view \a str in this string. |
| 4863 | |
| 4864 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
| 4865 | |
| 4866 | \sa contains(), indexOf() |
| 4867 | */ |
| 4868 | qsizetype QString::count(QStringView str, Qt::CaseSensitivity cs) const |
| 4869 | { |
| 4870 | return QtPrivate::count(haystack: *this, needle: str, cs); |
| 4871 | } |
| 4872 | |
| 4873 | /*! \fn bool QString::contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
| 4874 | |
| 4875 | Returns \c true if this string contains an occurrence of the string |
| 4876 | \a str; otherwise returns \c false. |
| 4877 | |
| 4878 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
| 4879 | |
| 4880 | Example: |
| 4881 | \snippet qstring/main.cpp 17 |
| 4882 | |
| 4883 | \sa indexOf(), count() |
| 4884 | */ |
| 4885 | |
| 4886 | /*! \fn bool QString::contains(QLatin1StringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
| 4887 | \since 5.3 |
| 4888 | |
| 4889 | \overload contains() |
| 4890 | |
| 4891 | Returns \c true if this string contains an occurrence of the latin-1 string |
| 4892 | \a str; otherwise returns \c false. |
| 4893 | */ |
| 4894 | |
| 4895 | /*! \fn bool QString::contains(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
| 4896 | |
| 4897 | \overload contains() |
| 4898 | |
| 4899 | Returns \c true if this string contains an occurrence of the |
| 4900 | character \a ch; otherwise returns \c false. |
| 4901 | */ |
| 4902 | |
| 4903 | /*! \fn bool QString::contains(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
| 4904 | \since 5.14 |
| 4905 | \overload contains() |
| 4906 | |
| 4907 | Returns \c true if this string contains an occurrence of the string view |
| 4908 | \a str; otherwise returns \c false. |
| 4909 | |
| 4910 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
| 4911 | |
| 4912 | \sa indexOf(), count() |
| 4913 | */ |
| 4914 | |
| 4915 | #if QT_CONFIG(regularexpression) |
| 4916 | /*! |
| 4917 | \since 5.5 |
| 4918 | |
| 4919 | Returns the index position of the first match of the regular |
| 4920 | expression \a re in the string, searching forward from index |
| 4921 | position \a from. Returns -1 if \a re didn't match anywhere. |
| 4922 | |
| 4923 | If the match is successful and \a rmatch is not \nullptr, it also |
| 4924 | writes the results of the match into the QRegularExpressionMatch object |
| 4925 | pointed to by \a rmatch. |
| 4926 | |
| 4927 | Example: |
| 4928 | |
| 4929 | \snippet qstring/main.cpp 93 |
| 4930 | */ |
| 4931 | qsizetype QString::indexOf(const QRegularExpression &re, qsizetype from, QRegularExpressionMatch *rmatch) const |
| 4932 | { |
| 4933 | return QtPrivate::indexOf(viewHaystack: QStringView(*this), stringHaystack: this, re, from, rmatch); |
| 4934 | } |
| 4935 | |
| 4936 | /*! |
| 4937 | \since 5.5 |
| 4938 | |
| 4939 | Returns the index position of the last match of the regular |
| 4940 | expression \a re in the string, which starts before the index |
| 4941 | position \a from. |
| 4942 | |
| 4943 | \include qstring.qdocinc negative-index-start-search-from-end |
| 4944 | |
| 4945 | Returns -1 if \a re didn't match anywhere. |
| 4946 | |
| 4947 | If the match is successful and \a rmatch is not \nullptr, it also |
| 4948 | writes the results of the match into the QRegularExpressionMatch object |
| 4949 | pointed to by \a rmatch. |
| 4950 | |
| 4951 | Example: |
| 4952 | |
| 4953 | \snippet qstring/main.cpp 94 |
| 4954 | |
| 4955 | \note Due to how the regular expression matching algorithm works, |
| 4956 | this function will actually match repeatedly from the beginning of |
| 4957 | the string until the position \a from is reached. |
| 4958 | |
| 4959 | \note When searching for a regular expression \a re that may match |
| 4960 | 0 characters, the match at the end of the data is excluded from the |
| 4961 | search by a negative \a from, even though \c{-1} is normally |
| 4962 | thought of as searching from the end of the string: the match at |
| 4963 | the end is \e after the last character, so it is excluded. To |
| 4964 | include such a final empty match, either give a positive value for |
| 4965 | \a from or omit the \a from parameter entirely. |
| 4966 | */ |
| 4967 | qsizetype QString::lastIndexOf(const QRegularExpression &re, qsizetype from, QRegularExpressionMatch *rmatch) const |
| 4968 | { |
| 4969 | return QtPrivate::lastIndexOf(viewHaystack: QStringView(*this), stringHaystack: this, re, from, rmatch); |
| 4970 | } |
| 4971 | |
| 4972 | /*! |
| 4973 | \fn qsizetype QString::lastIndexOf(const QRegularExpression &re, QRegularExpressionMatch *rmatch = nullptr) const |
| 4974 | \since 6.2 |
| 4975 | \overload lastIndexOf() |
| 4976 | |
| 4977 | Returns the index position of the last match of the regular |
| 4978 | expression \a re in the string. Returns -1 if \a re didn't match anywhere. |
| 4979 | |
| 4980 | If the match is successful and \a rmatch is not \nullptr, it also |
| 4981 | writes the results of the match into the QRegularExpressionMatch object |
| 4982 | pointed to by \a rmatch. |
| 4983 | |
| 4984 | Example: |
| 4985 | |
| 4986 | \snippet qstring/main.cpp 94 |
| 4987 | |
| 4988 | \note Due to how the regular expression matching algorithm works, |
| 4989 | this function will actually match repeatedly from the beginning of |
| 4990 | the string until the end of the string is reached. |
| 4991 | */ |
| 4992 | |
| 4993 | /*! |
| 4994 | \since 5.1 |
| 4995 | |
| 4996 | Returns \c true if the regular expression \a re matches somewhere in this |
| 4997 | string; otherwise returns \c false. |
| 4998 | |
| 4999 | If the match is successful and \a rmatch is not \nullptr, it also |
| 5000 | writes the results of the match into the QRegularExpressionMatch object |
| 5001 | pointed to by \a rmatch. |
| 5002 | |
| 5003 | \sa QRegularExpression::match() |
| 5004 | */ |
| 5005 | |
| 5006 | bool QString::contains(const QRegularExpression &re, QRegularExpressionMatch *rmatch) const |
| 5007 | { |
| 5008 | return QtPrivate::contains(viewHaystack: QStringView(*this), stringHaystack: this, re, rmatch); |
| 5009 | } |
| 5010 | |
| 5011 | /*! |
| 5012 | \overload count() |
| 5013 | \since 5.0 |
| 5014 | |
| 5015 | Returns the number of times the regular expression \a re matches |
| 5016 | in the string. |
| 5017 | |
| 5018 | For historical reasons, this function counts overlapping matches, |
| 5019 | so in the example below, there are four instances of "ana" or |
| 5020 | "ama": |
| 5021 | |
| 5022 | \snippet qstring/main.cpp 95 |
| 5023 | |
| 5024 | This behavior is different from simply iterating over the matches |
| 5025 | in the string using QRegularExpressionMatchIterator. |
| 5026 | |
| 5027 | \sa QRegularExpression::globalMatch() |
| 5028 | */ |
| 5029 | qsizetype QString::count(const QRegularExpression &re) const |
| 5030 | { |
| 5031 | return QtPrivate::count(haystack: QStringView(*this), re); |
| 5032 | } |
| 5033 | #endif // QT_CONFIG(regularexpression) |
| 5034 | |
| 5035 | #if QT_DEPRECATED_SINCE(6, 4) |
| 5036 | /*! \fn qsizetype QString::count() const |
| 5037 | \deprecated [6.4] Use size() or length() instead. |
| 5038 | \overload count() |
| 5039 | |
| 5040 | Same as size(). |
| 5041 | */ |
| 5042 | #endif |
| 5043 | |
| 5044 | /*! |
| 5045 | \enum QString::SectionFlag |
| 5046 | |
| 5047 | This enum specifies flags that can be used to affect various |
| 5048 | aspects of the section() function's behavior with respect to |
| 5049 | separators and empty fields. |
| 5050 | |
| 5051 | \value SectionDefault Empty fields are counted, leading and |
| 5052 | trailing separators are not included, and the separator is |
| 5053 | compared case sensitively. |
| 5054 | |
| 5055 | \value SectionSkipEmpty Treat empty fields as if they don't exist, |
| 5056 | i.e. they are not considered as far as \e start and \e end are |
| 5057 | concerned. |
| 5058 | |
| 5059 | \value SectionIncludeLeadingSep Include the leading separator (if |
| 5060 | any) in the result string. |
| 5061 | |
| 5062 | \value SectionIncludeTrailingSep Include the trailing separator |
| 5063 | (if any) in the result string. |
| 5064 | |
| 5065 | \value SectionCaseInsensitiveSeps Compare the separator |
| 5066 | case-insensitively. |
| 5067 | |
| 5068 | \sa section() |
| 5069 | */ |
| 5070 | |
| 5071 | /*! |
| 5072 | \fn QString QString::section(QChar sep, qsizetype start, qsizetype end = -1, SectionFlags flags) const |
| 5073 | |
| 5074 | This function returns a section of the string. |
| 5075 | |
| 5076 | This string is treated as a sequence of fields separated by the |
| 5077 | character, \a sep. The returned string consists of the fields from |
| 5078 | position \a start to position \a end inclusive. If \a end is not |
| 5079 | specified, all fields from position \a start to the end of the |
| 5080 | string are included. Fields are numbered 0, 1, 2, etc., counting |
| 5081 | from the left, and -1, -2, etc., counting from right to left. |
| 5082 | |
| 5083 | The \a flags argument can be used to affect some aspects of the |
| 5084 | function's behavior, e.g. whether to be case sensitive, whether |
| 5085 | to skip empty fields and how to deal with leading and trailing |
| 5086 | separators; see \l{SectionFlags}. |
| 5087 | |
| 5088 | \snippet qstring/main.cpp 52 |
| 5089 | |
| 5090 | If \a start or \a end is negative, we count fields from the right |
| 5091 | of the string, the right-most field being -1, the one from |
| 5092 | right-most field being -2, and so on. |
| 5093 | |
| 5094 | \snippet qstring/main.cpp 53 |
| 5095 | |
| 5096 | \sa split() |
| 5097 | */ |
| 5098 | |
| 5099 | /*! |
| 5100 | \overload section() |
| 5101 | |
| 5102 | \snippet qstring/main.cpp 51 |
| 5103 | \snippet qstring/main.cpp 54 |
| 5104 | |
| 5105 | \sa split() |
| 5106 | */ |
| 5107 | |
| 5108 | QString QString::section(const QString &sep, qsizetype start, qsizetype end, SectionFlags flags) const |
| 5109 | { |
| 5110 | const QList<QStringView> sections = QStringView{ *this }.split( |
| 5111 | sep, behavior: Qt::KeepEmptyParts, cs: (flags & SectionCaseInsensitiveSeps) ? Qt::CaseInsensitive : Qt::CaseSensitive); |
| 5112 | const qsizetype sectionsSize = sections.size(); |
| 5113 | if (!(flags & SectionSkipEmpty)) { |
| 5114 | if (start < 0) |
| 5115 | start += sectionsSize; |
| 5116 | if (end < 0) |
| 5117 | end += sectionsSize; |
| 5118 | } else { |
| 5119 | qsizetype skip = 0; |
| 5120 | for (qsizetype k = 0; k < sectionsSize; ++k) { |
| 5121 | if (sections.at(i: k).isEmpty()) |
| 5122 | skip++; |
| 5123 | } |
| 5124 | if (start < 0) |
| 5125 | start += sectionsSize - skip; |
| 5126 | if (end < 0) |
| 5127 | end += sectionsSize - skip; |
| 5128 | } |
| 5129 | if (start >= sectionsSize || end < 0 || start > end) |
| 5130 | return QString(); |
| 5131 | |
| 5132 | QString ret; |
| 5133 | qsizetype first_i = start, last_i = end; |
| 5134 | for (qsizetype x = 0, i = 0; x <= end && i < sectionsSize; ++i) { |
| 5135 | const QStringView §ion = sections.at(i); |
| 5136 | const bool empty = section.isEmpty(); |
| 5137 | if (x >= start) { |
| 5138 | if (x == start) |
| 5139 | first_i = i; |
| 5140 | if (x == end) |
| 5141 | last_i = i; |
| 5142 | if (x > start && i > 0) |
| 5143 | ret += sep; |
| 5144 | ret += section; |
| 5145 | } |
| 5146 | if (!empty || !(flags & SectionSkipEmpty)) |
| 5147 | x++; |
| 5148 | } |
| 5149 | if ((flags & SectionIncludeLeadingSep) && first_i > 0) |
| 5150 | ret.prepend(s: sep); |
| 5151 | if ((flags & SectionIncludeTrailingSep) && last_i < sectionsSize - 1) |
| 5152 | ret += sep; |
| 5153 | return ret; |
| 5154 | } |
| 5155 | |
| 5156 | #if QT_CONFIG(regularexpression) |
| 5157 | struct qt_section_chunk |
| 5158 | { |
| 5159 | qsizetype length; |
| 5160 | QStringView string; |
| 5161 | }; |
| 5162 | Q_DECLARE_TYPEINFO(qt_section_chunk, Q_RELOCATABLE_TYPE); |
| 5163 | |
| 5164 | static QString (QSpan<qt_section_chunk> sections, qsizetype start, qsizetype end, |
| 5165 | QString::SectionFlags flags) |
| 5166 | { |
| 5167 | const qsizetype sectionsSize = sections.size(); |
| 5168 | |
| 5169 | if (!(flags & QString::SectionSkipEmpty)) { |
| 5170 | if (start < 0) |
| 5171 | start += sectionsSize; |
| 5172 | if (end < 0) |
| 5173 | end += sectionsSize; |
| 5174 | } else { |
| 5175 | qsizetype skip = 0; |
| 5176 | for (qsizetype k = 0; k < sectionsSize; ++k) { |
| 5177 | const qt_section_chunk §ion = sections[k]; |
| 5178 | if (section.length == section.string.size()) |
| 5179 | skip++; |
| 5180 | } |
| 5181 | if (start < 0) |
| 5182 | start += sectionsSize - skip; |
| 5183 | if (end < 0) |
| 5184 | end += sectionsSize - skip; |
| 5185 | } |
| 5186 | if (start >= sectionsSize || end < 0 || start > end) |
| 5187 | return QString(); |
| 5188 | |
| 5189 | QString ret; |
| 5190 | qsizetype x = 0; |
| 5191 | qsizetype first_i = start, last_i = end; |
| 5192 | for (qsizetype i = 0; x <= end && i < sectionsSize; ++i) { |
| 5193 | const qt_section_chunk §ion = sections[i]; |
| 5194 | const bool empty = (section.length == section.string.size()); |
| 5195 | if (x >= start) { |
| 5196 | if (x == start) |
| 5197 | first_i = i; |
| 5198 | if (x == end) |
| 5199 | last_i = i; |
| 5200 | if (x != start) |
| 5201 | ret += section.string; |
| 5202 | else |
| 5203 | ret += section.string.mid(pos: section.length); |
| 5204 | } |
| 5205 | if (!empty || !(flags & QString::SectionSkipEmpty)) |
| 5206 | x++; |
| 5207 | } |
| 5208 | |
| 5209 | if ((flags & QString::SectionIncludeLeadingSep) && first_i >= 0) { |
| 5210 | const qt_section_chunk §ion = sections[first_i]; |
| 5211 | ret.prepend(v: section.string.left(n: section.length)); |
| 5212 | } |
| 5213 | |
| 5214 | if ((flags & QString::SectionIncludeTrailingSep) |
| 5215 | && last_i < sectionsSize - 1) { |
| 5216 | const qt_section_chunk §ion = sections[last_i + 1]; |
| 5217 | ret += section.string.left(n: section.length); |
| 5218 | } |
| 5219 | |
| 5220 | return ret; |
| 5221 | } |
| 5222 | |
| 5223 | /*! |
| 5224 | \overload section() |
| 5225 | \since 5.0 |
| 5226 | |
| 5227 | This string is treated as a sequence of fields separated by the |
| 5228 | regular expression, \a re. |
| 5229 | |
| 5230 | \snippet qstring/main.cpp 89 |
| 5231 | |
| 5232 | \warning Using this QRegularExpression version is much more expensive than |
| 5233 | the overloaded string and character versions. |
| 5234 | |
| 5235 | \sa split(), simplified() |
| 5236 | */ |
| 5237 | QString QString::section(const QRegularExpression &re, qsizetype start, qsizetype end, SectionFlags flags) const |
| 5238 | { |
| 5239 | if (!re.isValid()) { |
| 5240 | qtWarnAboutInvalidRegularExpression(re, cls: "QString" , method: "section" ); |
| 5241 | return QString(); |
| 5242 | } |
| 5243 | |
| 5244 | const QChar *uc = unicode(); |
| 5245 | if (!uc) |
| 5246 | return QString(); |
| 5247 | |
| 5248 | QRegularExpression sep(re); |
| 5249 | if (flags & SectionCaseInsensitiveSeps) |
| 5250 | sep.setPatternOptions(sep.patternOptions() | QRegularExpression::CaseInsensitiveOption); |
| 5251 | |
| 5252 | QVarLengthArray<qt_section_chunk> sections; |
| 5253 | qsizetype n = size(), m = 0, last_m = 0, last_len = 0; |
| 5254 | QRegularExpressionMatchIterator iterator = sep.globalMatch(subject: *this); |
| 5255 | while (iterator.hasNext()) { |
| 5256 | QRegularExpressionMatch match = iterator.next(); |
| 5257 | m = match.capturedStart(); |
| 5258 | sections.append(t: qt_section_chunk{.length: last_len, .string: QStringView{*this}.sliced(pos: last_m, n: m - last_m)}); |
| 5259 | last_m = m; |
| 5260 | last_len = match.capturedLength(); |
| 5261 | } |
| 5262 | sections.append(t: qt_section_chunk{.length: last_len, .string: QStringView{*this}.sliced(pos: last_m, n: n - last_m)}); |
| 5263 | |
| 5264 | return extractSections(sections, start, end, flags); |
| 5265 | } |
| 5266 | #endif // QT_CONFIG(regularexpression) |
| 5267 | |
| 5268 | /*! |
| 5269 | \fn QString QString::left(qsizetype n) const & |
| 5270 | \fn QString QString::left(qsizetype n) && |
| 5271 | |
| 5272 | Returns a substring that contains the \a n leftmost characters of |
| 5273 | this string (that is, from the beginning of this string up to, but not |
| 5274 | including, the element at index position \a n). |
| 5275 | |
| 5276 | If you know that \a n cannot be out of bounds, use first() instead in new |
| 5277 | code, because it is faster. |
| 5278 | |
| 5279 | The entire string is returned if \a n is greater than or equal |
| 5280 | to size(), or less than zero. |
| 5281 | |
| 5282 | \sa first(), last(), startsWith(), chopped(), chop(), truncate() |
| 5283 | */ |
| 5284 | |
| 5285 | /*! |
| 5286 | \fn QString QString::right(qsizetype n) const & |
| 5287 | \fn QString QString::right(qsizetype n) && |
| 5288 | |
| 5289 | Returns a substring that contains the \a n rightmost characters |
| 5290 | of the string. |
| 5291 | |
| 5292 | If you know that \a n cannot be out of bounds, use last() instead in new |
| 5293 | code, because it is faster. |
| 5294 | |
| 5295 | The entire string is returned if \a n is greater than or equal |
| 5296 | to size(), or less than zero. |
| 5297 | |
| 5298 | \sa endsWith(), last(), first(), sliced(), chopped(), chop(), truncate(), slice() |
| 5299 | */ |
| 5300 | |
| 5301 | /*! |
| 5302 | \fn QString QString::mid(qsizetype position, qsizetype n) const & |
| 5303 | \fn QString QString::mid(qsizetype position, qsizetype n) && |
| 5304 | |
| 5305 | Returns a string that contains \a n characters of this string, starting |
| 5306 | at the specified \a position index up to, but not including, the element |
| 5307 | at index position \c {\a position + n}. |
| 5308 | |
| 5309 | If you know that \a position and \a n cannot be out of bounds, use sliced() |
| 5310 | instead in new code, because it is faster. |
| 5311 | |
| 5312 | Returns a null string if the \a position index exceeds the |
| 5313 | length of the string. If there are less than \a n characters |
| 5314 | available in the string starting at the given \a position, or if |
| 5315 | \a n is -1 (default), the function returns all characters that |
| 5316 | are available from the specified \a position. |
| 5317 | |
| 5318 | \sa first(), last(), sliced(), chopped(), chop(), truncate(), slice() |
| 5319 | */ |
| 5320 | QString QString::mid(qsizetype position, qsizetype n) const & |
| 5321 | { |
| 5322 | qsizetype p = position; |
| 5323 | qsizetype l = n; |
| 5324 | using namespace QtPrivate; |
| 5325 | switch (QContainerImplHelper::mid(originalLength: size(), position: &p, length: &l)) { |
| 5326 | case QContainerImplHelper::Null: |
| 5327 | return QString(); |
| 5328 | case QContainerImplHelper::Empty: |
| 5329 | return QString(DataPointer::fromRawData(rawData: &_empty, length: 0)); |
| 5330 | case QContainerImplHelper::Full: |
| 5331 | return *this; |
| 5332 | case QContainerImplHelper::Subset: |
| 5333 | return sliced(pos: p, n: l); |
| 5334 | } |
| 5335 | Q_UNREACHABLE_RETURN(QString()); |
| 5336 | } |
| 5337 | |
| 5338 | QString QString::mid(qsizetype position, qsizetype n) && |
| 5339 | { |
| 5340 | qsizetype p = position; |
| 5341 | qsizetype l = n; |
| 5342 | using namespace QtPrivate; |
| 5343 | switch (QContainerImplHelper::mid(originalLength: size(), position: &p, length: &l)) { |
| 5344 | case QContainerImplHelper::Null: |
| 5345 | return QString(); |
| 5346 | case QContainerImplHelper::Empty: |
| 5347 | resize(size: 0); // keep capacity if we've reserve()d |
| 5348 | [[fallthrough]]; |
| 5349 | case QContainerImplHelper::Full: |
| 5350 | return std::move(*this); |
| 5351 | case QContainerImplHelper::Subset: |
| 5352 | return std::move(*this).sliced(pos: p, n: l); |
| 5353 | } |
| 5354 | Q_UNREACHABLE_RETURN(QString()); |
| 5355 | } |
| 5356 | |
| 5357 | /*! |
| 5358 | \fn QString QString::first(qsizetype n) const & |
| 5359 | \fn QString QString::first(qsizetype n) && |
| 5360 | \since 6.0 |
| 5361 | |
| 5362 | Returns a string that contains the first \a n characters of this string, |
| 5363 | (that is, from the beginning of this string up to, but not including, |
| 5364 | the element at index position \a n). |
| 5365 | |
| 5366 | \note The behavior is undefined when \a n < 0 or \a n > size(). |
| 5367 | |
| 5368 | \snippet qstring/main.cpp 31 |
| 5369 | |
| 5370 | \sa last(), sliced(), startsWith(), chopped(), chop(), truncate(), slice() |
| 5371 | */ |
| 5372 | |
| 5373 | /*! |
| 5374 | \fn QString QString::last(qsizetype n) const & |
| 5375 | \fn QString QString::last(qsizetype n) && |
| 5376 | \since 6.0 |
| 5377 | |
| 5378 | Returns the string that contains the last \a n characters of this string. |
| 5379 | |
| 5380 | \note The behavior is undefined when \a n < 0 or \a n > size(). |
| 5381 | |
| 5382 | \snippet qstring/main.cpp 48 |
| 5383 | |
| 5384 | \sa first(), sliced(), endsWith(), chopped(), chop(), truncate(), slice() |
| 5385 | */ |
| 5386 | |
| 5387 | /*! |
| 5388 | \fn QString QString::sliced(qsizetype pos, qsizetype n) const & |
| 5389 | \fn QString QString::sliced(qsizetype pos, qsizetype n) && |
| 5390 | \since 6.0 |
| 5391 | |
| 5392 | Returns a string that contains \a n characters of this string, starting |
| 5393 | at position \a pos up to, but not including, the element at index position |
| 5394 | \c {\a pos + n}. |
| 5395 | |
| 5396 | \note The behavior is undefined when \a pos < 0, \a n < 0, |
| 5397 | or \a pos + \a n > size(). |
| 5398 | |
| 5399 | \snippet qstring/main.cpp 34 |
| 5400 | |
| 5401 | \sa first(), last(), chopped(), chop(), truncate(), slice() |
| 5402 | */ |
| 5403 | QString QString::sliced_helper(QString &str, qsizetype pos, qsizetype n) |
| 5404 | { |
| 5405 | if (n == 0) |
| 5406 | return QString(DataPointer::fromRawData(rawData: &_empty, length: 0)); |
| 5407 | DataPointer d = std::move(str.d).sliced(pos, n); |
| 5408 | d.data()[n] = 0; |
| 5409 | return QString(std::move(d)); |
| 5410 | } |
| 5411 | |
| 5412 | /*! |
| 5413 | \fn QString QString::sliced(qsizetype pos) const & |
| 5414 | \fn QString QString::sliced(qsizetype pos) && |
| 5415 | \since 6.0 |
| 5416 | \overload |
| 5417 | |
| 5418 | Returns a string that contains the portion of this string starting at |
| 5419 | position \a pos and extending to its end. |
| 5420 | |
| 5421 | \note The behavior is undefined when \a pos < 0 or \a pos > size(). |
| 5422 | |
| 5423 | \sa first(), last(), chopped(), chop(), truncate(), slice() |
| 5424 | */ |
| 5425 | |
| 5426 | /*! |
| 5427 | \fn QString &QString::slice(qsizetype pos, qsizetype n) |
| 5428 | \since 6.8 |
| 5429 | |
| 5430 | Modifies this string to start at position \a pos, up to, but not including, |
| 5431 | the character (code point) at index position \c {\a pos + n}; and returns |
| 5432 | a reference to this string. |
| 5433 | |
| 5434 | \note The behavior is undefined if \a pos < 0, \a n < 0, |
| 5435 | or \a pos + \a n > size(). |
| 5436 | |
| 5437 | \snippet qstring/main.cpp slice97 |
| 5438 | |
| 5439 | \sa sliced(), first(), last(), chopped(), chop(), truncate() |
| 5440 | */ |
| 5441 | |
| 5442 | /*! |
| 5443 | \fn QString &QString::slice(qsizetype pos) |
| 5444 | \since 6.8 |
| 5445 | \overload |
| 5446 | |
| 5447 | Modifies this string to start at position \a pos and extending to its end, |
| 5448 | and returns a reference to this string. |
| 5449 | |
| 5450 | \note The behavior is undefined if \a pos < 0 or \a pos > size(). |
| 5451 | |
| 5452 | \sa sliced(), first(), last(), chopped(), chop(), truncate() |
| 5453 | */ |
| 5454 | |
| 5455 | /*! |
| 5456 | \fn QString QString::chopped(qsizetype len) const & |
| 5457 | \fn QString QString::chopped(qsizetype len) && |
| 5458 | \since 5.10 |
| 5459 | |
| 5460 | Returns a string that contains the size() - \a len leftmost characters |
| 5461 | of this string. |
| 5462 | |
| 5463 | \note The behavior is undefined if \a len is negative or greater than size(). |
| 5464 | |
| 5465 | \sa endsWith(), first(), last(), sliced(), chop(), truncate(), slice() |
| 5466 | */ |
| 5467 | |
| 5468 | /*! |
| 5469 | Returns \c true if the string starts with \a s; otherwise returns |
| 5470 | \c false. |
| 5471 | |
| 5472 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
| 5473 | |
| 5474 | \snippet qstring/main.cpp 65 |
| 5475 | |
| 5476 | \sa endsWith() |
| 5477 | */ |
| 5478 | bool QString::startsWith(const QString& s, Qt::CaseSensitivity cs) const |
| 5479 | { |
| 5480 | return qt_starts_with_impl(haystack: QStringView(*this), needle: QStringView(s), cs); |
| 5481 | } |
| 5482 | |
| 5483 | /*! |
| 5484 | \overload startsWith() |
| 5485 | */ |
| 5486 | bool QString::startsWith(QLatin1StringView s, Qt::CaseSensitivity cs) const |
| 5487 | { |
| 5488 | return qt_starts_with_impl(haystack: QStringView(*this), needle: s, cs); |
| 5489 | } |
| 5490 | |
| 5491 | /*! |
| 5492 | \overload startsWith() |
| 5493 | |
| 5494 | Returns \c true if the string starts with \a c; otherwise returns |
| 5495 | \c false. |
| 5496 | */ |
| 5497 | bool QString::startsWith(QChar c, Qt::CaseSensitivity cs) const |
| 5498 | { |
| 5499 | if (!size()) |
| 5500 | return false; |
| 5501 | if (cs == Qt::CaseSensitive) |
| 5502 | return at(i: 0) == c; |
| 5503 | return foldCase(ch: at(i: 0)) == foldCase(ch: c); |
| 5504 | } |
| 5505 | |
| 5506 | /*! |
| 5507 | \fn bool QString::startsWith(QStringView str, Qt::CaseSensitivity cs) const |
| 5508 | \since 5.10 |
| 5509 | \overload |
| 5510 | |
| 5511 | Returns \c true if the string starts with the string view \a str; |
| 5512 | otherwise returns \c false. |
| 5513 | |
| 5514 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
| 5515 | |
| 5516 | \sa endsWith() |
| 5517 | */ |
| 5518 | |
| 5519 | /*! |
| 5520 | Returns \c true if the string ends with \a s; otherwise returns |
| 5521 | \c false. |
| 5522 | |
| 5523 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
| 5524 | |
| 5525 | \snippet qstring/main.cpp 20 |
| 5526 | |
| 5527 | \sa startsWith() |
| 5528 | */ |
| 5529 | bool QString::endsWith(const QString &s, Qt::CaseSensitivity cs) const |
| 5530 | { |
| 5531 | return qt_ends_with_impl(haystack: QStringView(*this), needle: QStringView(s), cs); |
| 5532 | } |
| 5533 | |
| 5534 | /*! |
| 5535 | \fn bool QString::endsWith(QStringView str, Qt::CaseSensitivity cs) const |
| 5536 | \since 5.10 |
| 5537 | \overload endsWith() |
| 5538 | Returns \c true if the string ends with the string view \a str; |
| 5539 | otherwise returns \c false. |
| 5540 | |
| 5541 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
| 5542 | |
| 5543 | \sa startsWith() |
| 5544 | */ |
| 5545 | |
| 5546 | /*! |
| 5547 | \overload endsWith() |
| 5548 | */ |
| 5549 | bool QString::endsWith(QLatin1StringView s, Qt::CaseSensitivity cs) const |
| 5550 | { |
| 5551 | return qt_ends_with_impl(haystack: QStringView(*this), needle: s, cs); |
| 5552 | } |
| 5553 | |
| 5554 | /*! |
| 5555 | Returns \c true if the string ends with \a c; otherwise returns |
| 5556 | \c false. |
| 5557 | |
| 5558 | \overload endsWith() |
| 5559 | */ |
| 5560 | bool QString::endsWith(QChar c, Qt::CaseSensitivity cs) const |
| 5561 | { |
| 5562 | if (!size()) |
| 5563 | return false; |
| 5564 | if (cs == Qt::CaseSensitive) |
| 5565 | return at(i: size() - 1) == c; |
| 5566 | return foldCase(ch: at(i: size() - 1)) == foldCase(ch: c); |
| 5567 | } |
| 5568 | |
| 5569 | static bool checkCase(QStringView s, QUnicodeTables::Case c) noexcept |
| 5570 | { |
| 5571 | QStringIterator it(s); |
| 5572 | while (it.hasNext()) { |
| 5573 | const char32_t uc = it.next(); |
| 5574 | if (qGetProp(ucs4: uc)->cases[c].diff) |
| 5575 | return false; |
| 5576 | } |
| 5577 | return true; |
| 5578 | } |
| 5579 | |
| 5580 | bool QtPrivate::isLower(QStringView s) noexcept |
| 5581 | { |
| 5582 | return checkCase(s, c: QUnicodeTables::LowerCase); |
| 5583 | } |
| 5584 | |
| 5585 | bool QtPrivate::isUpper(QStringView s) noexcept |
| 5586 | { |
| 5587 | return checkCase(s, c: QUnicodeTables::UpperCase); |
| 5588 | } |
| 5589 | |
| 5590 | /*! |
| 5591 | Returns \c true if the string is uppercase, that is, it's identical |
| 5592 | to its toUpper() folding. |
| 5593 | |
| 5594 | Note that this does \e not mean that the string does not contain |
| 5595 | lowercase letters (some lowercase letters do not have a uppercase |
| 5596 | folding; they are left unchanged by toUpper()). |
| 5597 | For more information, refer to the Unicode standard, section 3.13. |
| 5598 | |
| 5599 | \since 5.12 |
| 5600 | |
| 5601 | \sa QChar::toUpper(), isLower() |
| 5602 | */ |
| 5603 | bool QString::isUpper() const |
| 5604 | { |
| 5605 | return QtPrivate::isUpper(s: qToStringViewIgnoringNull(s: *this)); |
| 5606 | } |
| 5607 | |
| 5608 | /*! |
| 5609 | Returns \c true if the string is lowercase, that is, it's identical |
| 5610 | to its toLower() folding. |
| 5611 | |
| 5612 | Note that this does \e not mean that the string does not contain |
| 5613 | uppercase letters (some uppercase letters do not have a lowercase |
| 5614 | folding; they are left unchanged by toLower()). |
| 5615 | For more information, refer to the Unicode standard, section 3.13. |
| 5616 | |
| 5617 | \since 5.12 |
| 5618 | |
| 5619 | \sa QChar::toLower(), isUpper() |
| 5620 | */ |
| 5621 | bool QString::isLower() const |
| 5622 | { |
| 5623 | return QtPrivate::isLower(s: qToStringViewIgnoringNull(s: *this)); |
| 5624 | } |
| 5625 | |
| 5626 | static QByteArray qt_convert_to_latin1(QStringView string); |
| 5627 | |
| 5628 | QByteArray QString::toLatin1_helper(const QString &string) |
| 5629 | { |
| 5630 | return qt_convert_to_latin1(string); |
| 5631 | } |
| 5632 | |
| 5633 | /*! |
| 5634 | \since 6.0 |
| 5635 | \internal |
| 5636 | \relates QAnyStringView |
| 5637 | |
| 5638 | Returns a UTF-16 representation of \a string as a QString. |
| 5639 | |
| 5640 | \sa QString::toLatin1(), QStringView::toLatin1(), QtPrivate::convertToUtf8(), |
| 5641 | QtPrivate::convertToLocal8Bit(), QtPrivate::convertToUcs4() |
| 5642 | */ |
| 5643 | QString QtPrivate::convertToQString(QAnyStringView string) |
| 5644 | { |
| 5645 | return string.visit(v: [] (auto string) { return string.toString(); }); |
| 5646 | } |
| 5647 | |
| 5648 | /*! |
| 5649 | \since 5.10 |
| 5650 | \internal |
| 5651 | \relates QStringView |
| 5652 | |
| 5653 | Returns a Latin-1 representation of \a string as a QByteArray. |
| 5654 | |
| 5655 | The behavior is undefined if \a string contains non-Latin1 characters. |
| 5656 | |
| 5657 | \sa QString::toLatin1(), QStringView::toLatin1(), QtPrivate::convertToUtf8(), |
| 5658 | QtPrivate::convertToLocal8Bit(), QtPrivate::convertToUcs4() |
| 5659 | */ |
| 5660 | QByteArray QtPrivate::convertToLatin1(QStringView string) |
| 5661 | { |
| 5662 | return qt_convert_to_latin1(string); |
| 5663 | } |
| 5664 | |
| 5665 | Q_NEVER_INLINE |
| 5666 | static QByteArray qt_convert_to_latin1(QStringView string) |
| 5667 | { |
| 5668 | if (Q_UNLIKELY(string.isNull())) |
| 5669 | return QByteArray(); |
| 5670 | |
| 5671 | QByteArray ba(string.size(), Qt::Uninitialized); |
| 5672 | |
| 5673 | // since we own the only copy, we're going to const_cast the constData; |
| 5674 | // that avoids an unnecessary call to detach() and expansion code that will never get used |
| 5675 | qt_to_latin1(dst: reinterpret_cast<uchar *>(const_cast<char *>(ba.constData())), |
| 5676 | src: string.utf16(), length: string.size()); |
| 5677 | return ba; |
| 5678 | } |
| 5679 | |
| 5680 | QByteArray QString::toLatin1_helper_inplace(QString &s) |
| 5681 | { |
| 5682 | if (!s.isDetached()) |
| 5683 | return qt_convert_to_latin1(string: s); |
| 5684 | |
| 5685 | // We can return our own buffer to the caller. |
| 5686 | // Conversion to Latin-1 always shrinks the buffer by half. |
| 5687 | // This relies on the fact that we use QArrayData for everything behind the scenes |
| 5688 | |
| 5689 | // First, do the in-place conversion. Since isDetached() == true, the data |
| 5690 | // was allocated by QArrayData, so the null terminator must be there. |
| 5691 | qsizetype length = s.size(); |
| 5692 | char16_t *sdata = s.d->data(); |
| 5693 | Q_ASSERT(sdata[length] == u'\0'); |
| 5694 | qt_to_latin1(dst: reinterpret_cast<uchar *>(sdata), src: sdata, length: length + 1); |
| 5695 | |
| 5696 | // Move the internals over to the byte array. |
| 5697 | // Kids, avert your eyes. Don't try this at home. |
| 5698 | auto ba_d = std::move(s.d).reinterpreted<char>(); |
| 5699 | |
| 5700 | // Some sanity checks |
| 5701 | Q_ASSERT(ba_d.d->allocatedCapacity() >= ba_d.size); |
| 5702 | Q_ASSERT(s.isNull()); |
| 5703 | Q_ASSERT(s.isEmpty()); |
| 5704 | Q_ASSERT(s.constData() == QString().constData()); |
| 5705 | |
| 5706 | return QByteArray(std::move(ba_d)); |
| 5707 | } |
| 5708 | |
| 5709 | /*! |
| 5710 | \since 6.9 |
| 5711 | \internal |
| 5712 | \relates QLatin1StringView |
| 5713 | |
| 5714 | Returns a UTF-8 representation of \a string as a QByteArray. |
| 5715 | */ |
| 5716 | QByteArray QtPrivate::convertToUtf8(QLatin1StringView string) |
| 5717 | { |
| 5718 | if (Q_UNLIKELY(string.isNull())) |
| 5719 | return QByteArray(); |
| 5720 | |
| 5721 | // create a QByteArray with the worst case scenario size |
| 5722 | QByteArray ba(string.size() * 2, Qt::Uninitialized); |
| 5723 | const qsizetype sz = QUtf8::convertFromLatin1(out: ba.data(), in: string) - ba.data(); |
| 5724 | ba.truncate(pos: sz); |
| 5725 | |
| 5726 | return ba; |
| 5727 | } |
| 5728 | |
| 5729 | // QLatin1 methods that use helpers from qstring.cpp |
| 5730 | char16_t *QLatin1::convertToUnicode(char16_t *out, QLatin1StringView in) noexcept |
| 5731 | { |
| 5732 | const qsizetype len = in.size(); |
| 5733 | qt_from_latin1(dst: out, str: in.data(), size: len); |
| 5734 | return std::next(x: out, n: len); |
| 5735 | } |
| 5736 | |
| 5737 | char *QLatin1::convertFromUnicode(char *out, QStringView in) noexcept |
| 5738 | { |
| 5739 | const qsizetype len = in.size(); |
| 5740 | qt_to_latin1(dst: reinterpret_cast<uchar *>(out), src: in.utf16(), length: len); |
| 5741 | return out + len; |
| 5742 | } |
| 5743 | |
| 5744 | /*! |
| 5745 | \fn QByteArray QString::toLatin1() const |
| 5746 | |
| 5747 | Returns a Latin-1 representation of the string as a QByteArray. |
| 5748 | |
| 5749 | The returned byte array is undefined if the string contains non-Latin1 |
| 5750 | characters. Those characters may be suppressed or replaced with a |
| 5751 | question mark. |
| 5752 | |
| 5753 | \sa fromLatin1(), toUtf8(), toLocal8Bit(), QStringEncoder |
| 5754 | */ |
| 5755 | |
| 5756 | static QByteArray qt_convert_to_local_8bit(QStringView string); |
| 5757 | |
| 5758 | /*! |
| 5759 | \fn QByteArray QString::toLocal8Bit() const |
| 5760 | |
| 5761 | Returns the local 8-bit representation of the string as a |
| 5762 | QByteArray. |
| 5763 | |
| 5764 | \include qstring.qdocinc {qstring-local-8-bit-equivalent} {toUtf8} |
| 5765 | |
| 5766 | If this string contains any characters that cannot be encoded in the |
| 5767 | local 8-bit encoding, the returned byte array is undefined. Those |
| 5768 | characters may be suppressed or replaced by another. |
| 5769 | |
| 5770 | \sa fromLocal8Bit(), toLatin1(), toUtf8(), QStringEncoder |
| 5771 | */ |
| 5772 | |
| 5773 | QByteArray QString::toLocal8Bit_helper(const QChar *data, qsizetype size) |
| 5774 | { |
| 5775 | return qt_convert_to_local_8bit(string: QStringView(data, size)); |
| 5776 | } |
| 5777 | |
| 5778 | static QByteArray qt_convert_to_local_8bit(QStringView string) |
| 5779 | { |
| 5780 | if (string.isNull()) |
| 5781 | return QByteArray(); |
| 5782 | QStringEncoder fromUtf16(QStringEncoder::System, QStringEncoder::Flag::Stateless); |
| 5783 | return fromUtf16(string); |
| 5784 | } |
| 5785 | |
| 5786 | /*! |
| 5787 | \since 5.10 |
| 5788 | \internal |
| 5789 | \relates QStringView |
| 5790 | |
| 5791 | Returns a local 8-bit representation of \a string as a QByteArray. |
| 5792 | |
| 5793 | On Unix systems this is equivalent to toUtf8(), on Windows the systems |
| 5794 | current code page is being used. |
| 5795 | |
| 5796 | The behavior is undefined if \a string contains characters not |
| 5797 | supported by the locale's 8-bit encoding. |
| 5798 | |
| 5799 | \sa QString::toLocal8Bit(), QStringView::toLocal8Bit() |
| 5800 | */ |
| 5801 | QByteArray QtPrivate::convertToLocal8Bit(QStringView string) |
| 5802 | { |
| 5803 | return qt_convert_to_local_8bit(string); |
| 5804 | } |
| 5805 | |
| 5806 | static QByteArray qt_convert_to_utf8(QStringView str); |
| 5807 | |
| 5808 | /*! |
| 5809 | \fn QByteArray QString::toUtf8() const |
| 5810 | |
| 5811 | Returns a UTF-8 representation of the string as a QByteArray. |
| 5812 | |
| 5813 | UTF-8 is a Unicode codec and can represent all characters in a Unicode |
| 5814 | string like QString. |
| 5815 | |
| 5816 | \sa fromUtf8(), toLatin1(), toLocal8Bit(), QStringEncoder |
| 5817 | */ |
| 5818 | |
| 5819 | QByteArray QString::toUtf8_helper(const QString &str) |
| 5820 | { |
| 5821 | return qt_convert_to_utf8(str); |
| 5822 | } |
| 5823 | |
| 5824 | static QByteArray qt_convert_to_utf8(QStringView str) |
| 5825 | { |
| 5826 | if (str.isNull()) |
| 5827 | return QByteArray(); |
| 5828 | |
| 5829 | return QUtf8::convertFromUnicode(in: str); |
| 5830 | } |
| 5831 | |
| 5832 | /*! |
| 5833 | \since 5.10 |
| 5834 | \internal |
| 5835 | \relates QStringView |
| 5836 | |
| 5837 | Returns a UTF-8 representation of \a string as a QByteArray. |
| 5838 | |
| 5839 | UTF-8 is a Unicode codec and can represent all characters in a Unicode |
| 5840 | string like QStringView. |
| 5841 | |
| 5842 | \sa QString::toUtf8(), QStringView::toUtf8() |
| 5843 | */ |
| 5844 | QByteArray QtPrivate::convertToUtf8(QStringView string) |
| 5845 | { |
| 5846 | return qt_convert_to_utf8(str: string); |
| 5847 | } |
| 5848 | |
| 5849 | static QList<uint> qt_convert_to_ucs4(QStringView string); |
| 5850 | |
| 5851 | /*! |
| 5852 | \since 4.2 |
| 5853 | |
| 5854 | Returns a UCS-4/UTF-32 representation of the string as a QList<uint>. |
| 5855 | |
| 5856 | UTF-32 is a Unicode codec and therefore it is lossless. All characters from |
| 5857 | this string will be encoded in UTF-32. Any invalid sequence of code units in |
| 5858 | this string is replaced by the Unicode replacement character |
| 5859 | (QChar::ReplacementCharacter, which corresponds to \c{U+FFFD}). |
| 5860 | |
| 5861 | The returned list is not 0-terminated. |
| 5862 | |
| 5863 | \sa fromUtf8(), toUtf8(), toLatin1(), toLocal8Bit(), QStringEncoder, |
| 5864 | fromUcs4(), toWCharArray() |
| 5865 | */ |
| 5866 | QList<uint> QString::toUcs4() const |
| 5867 | { |
| 5868 | return qt_convert_to_ucs4(string: *this); |
| 5869 | } |
| 5870 | |
| 5871 | static QList<uint> qt_convert_to_ucs4(QStringView string) |
| 5872 | { |
| 5873 | QList<uint> v(string.size()); |
| 5874 | uint *a = const_cast<uint*>(v.constData()); |
| 5875 | QStringIterator it(string); |
| 5876 | while (it.hasNext()) |
| 5877 | *a++ = it.next(); |
| 5878 | v.resize(size: a - v.constData()); |
| 5879 | return v; |
| 5880 | } |
| 5881 | |
| 5882 | /*! |
| 5883 | \since 5.10 |
| 5884 | \internal |
| 5885 | \relates QStringView |
| 5886 | |
| 5887 | Returns a UCS-4/UTF-32 representation of \a string as a QList<uint>. |
| 5888 | |
| 5889 | UTF-32 is a Unicode codec and therefore it is lossless. All characters from |
| 5890 | this string will be encoded in UTF-32. Any invalid sequence of code units in |
| 5891 | this string is replaced by the Unicode replacement character |
| 5892 | (QChar::ReplacementCharacter, which corresponds to \c{U+FFFD}). |
| 5893 | |
| 5894 | The returned list is not 0-terminated. |
| 5895 | |
| 5896 | \sa QString::toUcs4(), QStringView::toUcs4(), QtPrivate::convertToLatin1(), |
| 5897 | QtPrivate::convertToLocal8Bit(), QtPrivate::convertToUtf8() |
| 5898 | */ |
| 5899 | QList<uint> QtPrivate::convertToUcs4(QStringView string) |
| 5900 | { |
| 5901 | return qt_convert_to_ucs4(string); |
| 5902 | } |
| 5903 | |
| 5904 | /*! |
| 5905 | \fn QString QString::fromLatin1(QByteArrayView str) |
| 5906 | \overload |
| 5907 | \since 6.0 |
| 5908 | |
| 5909 | Returns a QString initialized with the Latin-1 string \a str. |
| 5910 | |
| 5911 | \note: any null ('\\0') bytes in the byte array will be included in this |
| 5912 | string, converted to Unicode null characters (U+0000). |
| 5913 | */ |
| 5914 | QString QString::fromLatin1(QByteArrayView ba) |
| 5915 | { |
| 5916 | DataPointer d; |
| 5917 | if (!ba.data()) { |
| 5918 | // nothing to do |
| 5919 | } else if (ba.size() == 0) { |
| 5920 | d = DataPointer::fromRawData(rawData: &_empty, length: 0); |
| 5921 | } else { |
| 5922 | d = DataPointer(ba.size(), ba.size()); |
| 5923 | Q_CHECK_PTR(d.data()); |
| 5924 | d.data()[ba.size()] = '\0'; |
| 5925 | char16_t *dst = d.data(); |
| 5926 | |
| 5927 | qt_from_latin1(dst, str: ba.data(), size: size_t(ba.size())); |
| 5928 | } |
| 5929 | return QString(std::move(d)); |
| 5930 | } |
| 5931 | |
| 5932 | /*! |
| 5933 | \fn QString QString::fromLatin1(const char *str, qsizetype size) |
| 5934 | Returns a QString initialized with the first \a size characters |
| 5935 | of the Latin-1 string \a str. |
| 5936 | |
| 5937 | If \a size is \c{-1}, \c{strlen(str)} is used instead. |
| 5938 | |
| 5939 | \sa toLatin1(), fromUtf8(), fromLocal8Bit() |
| 5940 | */ |
| 5941 | |
| 5942 | /*! |
| 5943 | \fn QString QString::fromLatin1(const QByteArray &str) |
| 5944 | \overload |
| 5945 | \since 5.0 |
| 5946 | |
| 5947 | Returns a QString initialized with the Latin-1 string \a str. |
| 5948 | |
| 5949 | \note: any null ('\\0') bytes in the byte array will be included in this |
| 5950 | string, converted to Unicode null characters (U+0000). This behavior is |
| 5951 | different from Qt 5.x. |
| 5952 | */ |
| 5953 | |
| 5954 | /*! |
| 5955 | \fn QString QString::fromLocal8Bit(const char *str, qsizetype size) |
| 5956 | Returns a QString initialized with the first \a size characters |
| 5957 | of the 8-bit string \a str. |
| 5958 | |
| 5959 | If \a size is \c{-1}, \c{strlen(str)} is used instead. |
| 5960 | |
| 5961 | \include qstring.qdocinc {qstring-local-8-bit-equivalent} {fromUtf8} |
| 5962 | |
| 5963 | \sa toLocal8Bit(), fromLatin1(), fromUtf8() |
| 5964 | */ |
| 5965 | |
| 5966 | /*! |
| 5967 | \fn QString QString::fromLocal8Bit(const QByteArray &str) |
| 5968 | \overload |
| 5969 | \since 5.0 |
| 5970 | |
| 5971 | Returns a QString initialized with the 8-bit string \a str. |
| 5972 | |
| 5973 | \include qstring.qdocinc {qstring-local-8-bit-equivalent} {fromUtf8} |
| 5974 | |
| 5975 | \note: any null ('\\0') bytes in the byte array will be included in this |
| 5976 | string, converted to Unicode null characters (U+0000). This behavior is |
| 5977 | different from Qt 5.x. |
| 5978 | */ |
| 5979 | |
| 5980 | /*! |
| 5981 | \fn QString QString::fromLocal8Bit(QByteArrayView str) |
| 5982 | \overload |
| 5983 | \since 6.0 |
| 5984 | |
| 5985 | Returns a QString initialized with the 8-bit string \a str. |
| 5986 | |
| 5987 | \include qstring.qdocinc {qstring-local-8-bit-equivalent} {fromUtf8} |
| 5988 | |
| 5989 | \note: any null ('\\0') bytes in the byte array will be included in this |
| 5990 | string, converted to Unicode null characters (U+0000). |
| 5991 | */ |
| 5992 | QString QString::fromLocal8Bit(QByteArrayView ba) |
| 5993 | { |
| 5994 | if (ba.isNull()) |
| 5995 | return QString(); |
| 5996 | if (ba.isEmpty()) |
| 5997 | return QString(DataPointer::fromRawData(rawData: &_empty, length: 0)); |
| 5998 | QStringDecoder toUtf16(QStringDecoder::System, QStringDecoder::Flag::Stateless); |
| 5999 | return toUtf16(ba); |
| 6000 | } |
| 6001 | |
| 6002 | /*! \fn QString QString::fromUtf8(const char *str, qsizetype size) |
| 6003 | Returns a QString initialized with the first \a size bytes |
| 6004 | of the UTF-8 string \a str. |
| 6005 | |
| 6006 | If \a size is \c{-1}, \c{strlen(str)} is used instead. |
| 6007 | |
| 6008 | UTF-8 is a Unicode codec and can represent all characters in a Unicode |
| 6009 | string like QString. However, invalid sequences are possible with UTF-8 |
| 6010 | and, if any such are found, they will be replaced with one or more |
| 6011 | "replacement characters", or suppressed. These include non-Unicode |
| 6012 | sequences, non-characters, overlong sequences or surrogate codepoints |
| 6013 | encoded into UTF-8. |
| 6014 | |
| 6015 | This function can be used to process incoming data incrementally as long as |
| 6016 | all UTF-8 characters are terminated within the incoming data. Any |
| 6017 | unterminated characters at the end of the string will be replaced or |
| 6018 | suppressed. In order to do stateful decoding, please use \l QStringDecoder. |
| 6019 | |
| 6020 | \sa toUtf8(), fromLatin1(), fromLocal8Bit() |
| 6021 | */ |
| 6022 | |
| 6023 | /*! |
| 6024 | \fn QString QString::fromUtf8(const char8_t *str) |
| 6025 | \overload |
| 6026 | \since 6.1 |
| 6027 | |
| 6028 | This overload is only available when compiling in C++20 mode. |
| 6029 | */ |
| 6030 | |
| 6031 | /*! |
| 6032 | \fn QString QString::fromUtf8(const char8_t *str, qsizetype size) |
| 6033 | \overload |
| 6034 | \since 6.0 |
| 6035 | |
| 6036 | This overload is only available when compiling in C++20 mode. |
| 6037 | */ |
| 6038 | |
| 6039 | /*! |
| 6040 | \fn QString QString::fromUtf8(const QByteArray &str) |
| 6041 | \overload |
| 6042 | \since 5.0 |
| 6043 | |
| 6044 | Returns a QString initialized with the UTF-8 string \a str. |
| 6045 | |
| 6046 | \note: any null ('\\0') bytes in the byte array will be included in this |
| 6047 | string, converted to Unicode null characters (U+0000). This behavior is |
| 6048 | different from Qt 5.x. |
| 6049 | */ |
| 6050 | |
| 6051 | /*! |
| 6052 | \fn QString QString::fromUtf8(QByteArrayView str) |
| 6053 | \overload |
| 6054 | \since 6.0 |
| 6055 | |
| 6056 | Returns a QString initialized with the UTF-8 string \a str. |
| 6057 | |
| 6058 | \note: any null ('\\0') bytes in the byte array will be included in this |
| 6059 | string, converted to Unicode null characters (U+0000). |
| 6060 | */ |
| 6061 | QString QString::fromUtf8(QByteArrayView ba) |
| 6062 | { |
| 6063 | if (ba.isNull()) |
| 6064 | return QString(); |
| 6065 | if (ba.isEmpty()) |
| 6066 | return QString(DataPointer::fromRawData(rawData: &_empty, length: 0)); |
| 6067 | return QUtf8::convertToUnicode(in: ba); |
| 6068 | } |
| 6069 | |
| 6070 | #ifndef QT_BOOTSTRAPPED |
| 6071 | /*! |
| 6072 | \since 5.3 |
| 6073 | Returns a QString initialized with the first \a size characters |
| 6074 | of the Unicode string \a unicode (ISO-10646-UTF-16 encoded). |
| 6075 | |
| 6076 | If \a size is -1 (default), \a unicode must be '\\0'-terminated. |
| 6077 | |
| 6078 | This function checks for a Byte Order Mark (BOM). If it is missing, |
| 6079 | host byte order is assumed. |
| 6080 | |
| 6081 | This function is slow compared to the other Unicode conversions. |
| 6082 | Use QString(const QChar *, qsizetype) or QString(const QChar *) if possible. |
| 6083 | |
| 6084 | QString makes a deep copy of the Unicode data. |
| 6085 | |
| 6086 | \sa utf16(), setUtf16(), fromStdU16String() |
| 6087 | */ |
| 6088 | QString QString::fromUtf16(const char16_t *unicode, qsizetype size) |
| 6089 | { |
| 6090 | if (!unicode) |
| 6091 | return QString(); |
| 6092 | if (size < 0) |
| 6093 | size = QtPrivate::qustrlen(str: unicode); |
| 6094 | QStringDecoder toUtf16(QStringDecoder::Utf16, QStringDecoder::Flag::Stateless); |
| 6095 | return toUtf16(QByteArrayView(reinterpret_cast<const char *>(unicode), size * 2)); |
| 6096 | } |
| 6097 | |
| 6098 | /*! |
| 6099 | \fn QString QString::fromUtf16(const ushort *str, qsizetype size) |
| 6100 | \deprecated [6.0] Use the \c char16_t overload instead. |
| 6101 | */ |
| 6102 | |
| 6103 | /*! |
| 6104 | \fn QString QString::fromUcs4(const uint *str, qsizetype size) |
| 6105 | \since 4.2 |
| 6106 | \deprecated [6.0] Use the \c char32_t overload instead. |
| 6107 | */ |
| 6108 | |
| 6109 | /*! |
| 6110 | \since 5.3 |
| 6111 | |
| 6112 | Returns a QString initialized with the first \a size characters |
| 6113 | of the Unicode string \a unicode (encoded as UTF-32). |
| 6114 | |
| 6115 | If \a size is -1 (default), \a unicode must be '\\0'-terminated. |
| 6116 | |
| 6117 | \sa toUcs4(), fromUtf16(), utf16(), setUtf16(), fromWCharArray(), |
| 6118 | fromStdU32String() |
| 6119 | */ |
| 6120 | QString QString::fromUcs4(const char32_t *unicode, qsizetype size) |
| 6121 | { |
| 6122 | if (!unicode) |
| 6123 | return QString(); |
| 6124 | if (size < 0) { |
| 6125 | if constexpr (sizeof(char32_t) == sizeof(wchar_t)) |
| 6126 | size = wcslen(s: reinterpret_cast<const wchar_t *>(unicode)); |
| 6127 | else |
| 6128 | size = std::char_traits<char32_t>::length(s: unicode); |
| 6129 | } |
| 6130 | QStringDecoder toUtf16(QStringDecoder::Utf32, QStringDecoder::Flag::Stateless); |
| 6131 | return toUtf16(QByteArrayView(reinterpret_cast<const char *>(unicode), size * 4)); |
| 6132 | } |
| 6133 | #endif // !QT_BOOTSTRAPPED |
| 6134 | |
| 6135 | /*! |
| 6136 | Resizes the string to \a size characters and copies \a unicode |
| 6137 | into the string. |
| 6138 | |
| 6139 | If \a unicode is \nullptr, nothing is copied, but the string is still |
| 6140 | resized to \a size. |
| 6141 | |
| 6142 | \sa unicode(), setUtf16() |
| 6143 | */ |
| 6144 | QString& QString::setUnicode(const QChar *unicode, qsizetype size) |
| 6145 | { |
| 6146 | resize(size); |
| 6147 | if (unicode && size) |
| 6148 | memcpy(dest: d.data(), src: unicode, n: size * sizeof(QChar)); |
| 6149 | return *this; |
| 6150 | } |
| 6151 | |
| 6152 | /*! |
| 6153 | \fn QString::setUnicode(const char16_t *unicode, qsizetype size) |
| 6154 | \overload |
| 6155 | \since 6.9 |
| 6156 | |
| 6157 | \sa unicode(), setUtf16() |
| 6158 | */ |
| 6159 | |
| 6160 | /*! |
| 6161 | \fn QString::setUtf16(const char16_t *unicode, qsizetype size) |
| 6162 | \since 6.9 |
| 6163 | |
| 6164 | Resizes the string to \a size characters and copies \a unicode |
| 6165 | into the string. |
| 6166 | |
| 6167 | If \a unicode is \nullptr, nothing is copied, but the string is still |
| 6168 | resized to \a size. |
| 6169 | |
| 6170 | Note that unlike fromUtf16(), this function does not consider BOMs and |
| 6171 | possibly differing byte ordering. |
| 6172 | |
| 6173 | \sa utf16(), setUnicode() |
| 6174 | */ |
| 6175 | |
| 6176 | /*! |
| 6177 | \fn QString &QString::setUtf16(const ushort *unicode, qsizetype size) |
| 6178 | \obsolete Use the \c char16_t overload instead. |
| 6179 | */ |
| 6180 | |
| 6181 | /*! |
| 6182 | \fn QString QString::simplified() const |
| 6183 | |
| 6184 | Returns a string that has whitespace removed from the start |
| 6185 | and the end, and that has each sequence of internal whitespace |
| 6186 | replaced with a single space. |
| 6187 | |
| 6188 | Whitespace means any character for which QChar::isSpace() returns |
| 6189 | \c true. This includes the ASCII characters '\\t', '\\n', '\\v', |
| 6190 | '\\f', '\\r', and ' '. |
| 6191 | |
| 6192 | Example: |
| 6193 | |
| 6194 | \snippet qstring/main.cpp 57 |
| 6195 | |
| 6196 | \sa trimmed() |
| 6197 | */ |
| 6198 | QString QString::simplified_helper(const QString &str) |
| 6199 | { |
| 6200 | return QStringAlgorithms<const QString>::simplified_helper(str); |
| 6201 | } |
| 6202 | |
| 6203 | QString QString::simplified_helper(QString &str) |
| 6204 | { |
| 6205 | return QStringAlgorithms<QString>::simplified_helper(str); |
| 6206 | } |
| 6207 | |
| 6208 | namespace { |
| 6209 | template <typename StringView> |
| 6210 | StringView qt_trimmed(StringView s) noexcept |
| 6211 | { |
| 6212 | const auto [begin, end] = QStringAlgorithms<const StringView>::trimmed_helper_positions(s); |
| 6213 | return StringView{begin, end}; |
| 6214 | } |
| 6215 | } |
| 6216 | |
| 6217 | /*! |
| 6218 | \fn QStringView QtPrivate::trimmed(QStringView s) |
| 6219 | \fn QLatin1StringView QtPrivate::trimmed(QLatin1StringView s) |
| 6220 | \internal |
| 6221 | \relates QStringView |
| 6222 | \since 5.10 |
| 6223 | |
| 6224 | Returns \a s with whitespace removed from the start and the end. |
| 6225 | |
| 6226 | Whitespace means any character for which QChar::isSpace() returns |
| 6227 | \c true. This includes the ASCII characters '\\t', '\\n', '\\v', |
| 6228 | '\\f', '\\r', and ' '. |
| 6229 | |
| 6230 | \sa QString::trimmed(), QStringView::trimmed(), QLatin1StringView::trimmed() |
| 6231 | */ |
| 6232 | QStringView QtPrivate::trimmed(QStringView s) noexcept |
| 6233 | { |
| 6234 | return qt_trimmed(s); |
| 6235 | } |
| 6236 | |
| 6237 | QLatin1StringView QtPrivate::trimmed(QLatin1StringView s) noexcept |
| 6238 | { |
| 6239 | return qt_trimmed(s); |
| 6240 | } |
| 6241 | |
| 6242 | /*! |
| 6243 | \fn QString QString::trimmed() const |
| 6244 | |
| 6245 | Returns a string that has whitespace removed from the start and |
| 6246 | the end. |
| 6247 | |
| 6248 | Whitespace means any character for which QChar::isSpace() returns |
| 6249 | \c true. This includes the ASCII characters '\\t', '\\n', '\\v', |
| 6250 | '\\f', '\\r', and ' '. |
| 6251 | |
| 6252 | Example: |
| 6253 | |
| 6254 | \snippet qstring/main.cpp 82 |
| 6255 | |
| 6256 | Unlike simplified(), trimmed() leaves internal whitespace alone. |
| 6257 | |
| 6258 | \sa simplified() |
| 6259 | */ |
| 6260 | QString QString::trimmed_helper(const QString &str) |
| 6261 | { |
| 6262 | return QStringAlgorithms<const QString>::trimmed_helper(str); |
| 6263 | } |
| 6264 | |
| 6265 | QString QString::trimmed_helper(QString &str) |
| 6266 | { |
| 6267 | return QStringAlgorithms<QString>::trimmed_helper(str); |
| 6268 | } |
| 6269 | |
| 6270 | /*! \fn const QChar QString::at(qsizetype position) const |
| 6271 | |
| 6272 | Returns the character at the given index \a position in the |
| 6273 | string. |
| 6274 | |
| 6275 | The \a position must be a valid index position in the string |
| 6276 | (i.e., 0 <= \a position < size()). |
| 6277 | |
| 6278 | \sa operator[]() |
| 6279 | */ |
| 6280 | |
| 6281 | /*! |
| 6282 | \fn QChar &QString::operator[](qsizetype position) |
| 6283 | |
| 6284 | Returns the character at the specified \a position in the string as a |
| 6285 | modifiable reference. |
| 6286 | |
| 6287 | Example: |
| 6288 | |
| 6289 | \snippet qstring/main.cpp 85 |
| 6290 | |
| 6291 | \sa at() |
| 6292 | */ |
| 6293 | |
| 6294 | /*! |
| 6295 | \fn const QChar QString::operator[](qsizetype position) const |
| 6296 | |
| 6297 | \overload operator[]() |
| 6298 | */ |
| 6299 | |
| 6300 | /*! |
| 6301 | \fn QChar QString::front() const |
| 6302 | \since 5.10 |
| 6303 | |
| 6304 | Returns the first character in the string. |
| 6305 | Same as \c{at(0)}. |
| 6306 | |
| 6307 | This function is provided for STL compatibility. |
| 6308 | |
| 6309 | \warning Calling this function on an empty string constitutes |
| 6310 | undefined behavior. |
| 6311 | |
| 6312 | \sa back(), at(), operator[]() |
| 6313 | */ |
| 6314 | |
| 6315 | /*! |
| 6316 | \fn QChar QString::back() const |
| 6317 | \since 5.10 |
| 6318 | |
| 6319 | Returns the last character in the string. |
| 6320 | Same as \c{at(size() - 1)}. |
| 6321 | |
| 6322 | This function is provided for STL compatibility. |
| 6323 | |
| 6324 | \warning Calling this function on an empty string constitutes |
| 6325 | undefined behavior. |
| 6326 | |
| 6327 | \sa front(), at(), operator[]() |
| 6328 | */ |
| 6329 | |
| 6330 | /*! |
| 6331 | \fn QChar &QString::front() |
| 6332 | \since 5.10 |
| 6333 | |
| 6334 | Returns a reference to the first character in the string. |
| 6335 | Same as \c{operator[](0)}. |
| 6336 | |
| 6337 | This function is provided for STL compatibility. |
| 6338 | |
| 6339 | \warning Calling this function on an empty string constitutes |
| 6340 | undefined behavior. |
| 6341 | |
| 6342 | \sa back(), at(), operator[]() |
| 6343 | */ |
| 6344 | |
| 6345 | /*! |
| 6346 | \fn QChar &QString::back() |
| 6347 | \since 5.10 |
| 6348 | |
| 6349 | Returns a reference to the last character in the string. |
| 6350 | Same as \c{operator[](size() - 1)}. |
| 6351 | |
| 6352 | This function is provided for STL compatibility. |
| 6353 | |
| 6354 | \warning Calling this function on an empty string constitutes |
| 6355 | undefined behavior. |
| 6356 | |
| 6357 | \sa front(), at(), operator[]() |
| 6358 | */ |
| 6359 | |
| 6360 | /*! |
| 6361 | \fn void QString::truncate(qsizetype position) |
| 6362 | |
| 6363 | Truncates the string starting from, and including, the element at index |
| 6364 | \a position. |
| 6365 | |
| 6366 | If the specified \a position index is beyond the end of the |
| 6367 | string, nothing happens. |
| 6368 | |
| 6369 | Example: |
| 6370 | |
| 6371 | \snippet qstring/main.cpp 83 |
| 6372 | |
| 6373 | If \a position is negative, it is equivalent to passing zero. |
| 6374 | |
| 6375 | \sa chop(), resize(), first(), QStringView::truncate() |
| 6376 | */ |
| 6377 | |
| 6378 | void QString::truncate(qsizetype pos) |
| 6379 | { |
| 6380 | if (pos < size()) |
| 6381 | resize(size: pos); |
| 6382 | } |
| 6383 | |
| 6384 | |
| 6385 | /*! |
| 6386 | Removes \a n characters from the end of the string. |
| 6387 | |
| 6388 | If \a n is greater than or equal to size(), the result is an |
| 6389 | empty string; if \a n is negative, it is equivalent to passing zero. |
| 6390 | |
| 6391 | Example: |
| 6392 | \snippet qstring/main.cpp 15 |
| 6393 | |
| 6394 | If you want to remove characters from the \e beginning of the |
| 6395 | string, use remove() instead. |
| 6396 | |
| 6397 | \sa truncate(), resize(), remove(), QStringView::chop() |
| 6398 | */ |
| 6399 | void QString::chop(qsizetype n) |
| 6400 | { |
| 6401 | if (n > 0) |
| 6402 | resize(size: d.size - n); |
| 6403 | } |
| 6404 | |
| 6405 | /*! |
| 6406 | Sets every character in the string to character \a ch. If \a size |
| 6407 | is different from -1 (default), the string is resized to \a |
| 6408 | size beforehand. |
| 6409 | |
| 6410 | Example: |
| 6411 | |
| 6412 | \snippet qstring/main.cpp 21 |
| 6413 | |
| 6414 | \sa resize() |
| 6415 | */ |
| 6416 | |
| 6417 | QString& QString::fill(QChar ch, qsizetype size) |
| 6418 | { |
| 6419 | resize(size: size < 0 ? d.size : size); |
| 6420 | if (d.size) |
| 6421 | std::fill(first: d.data(), last: d.data() + d.size, value: ch.unicode()); |
| 6422 | return *this; |
| 6423 | } |
| 6424 | |
| 6425 | /*! |
| 6426 | \fn qsizetype QString::length() const |
| 6427 | |
| 6428 | Returns the number of characters in this string. Equivalent to |
| 6429 | size(). |
| 6430 | |
| 6431 | \sa resize() |
| 6432 | */ |
| 6433 | |
| 6434 | /*! |
| 6435 | \fn qsizetype QString::size() const |
| 6436 | |
| 6437 | Returns the number of characters in this string. |
| 6438 | |
| 6439 | The last character in the string is at position size() - 1. |
| 6440 | |
| 6441 | Example: |
| 6442 | \snippet qstring/main.cpp 58 |
| 6443 | |
| 6444 | \sa isEmpty(), resize() |
| 6445 | */ |
| 6446 | |
| 6447 | /*! |
| 6448 | \fn qsizetype QString::max_size() const |
| 6449 | \fn qsizetype QString::maxSize() |
| 6450 | \since 6.8 |
| 6451 | |
| 6452 | It returns the maximum number of elements that the string can |
| 6453 | theoretically hold. In practice, the number can be much smaller, |
| 6454 | limited by the amount of memory available to the system. |
| 6455 | */ |
| 6456 | |
| 6457 | /*! \fn bool QString::isNull() const |
| 6458 | |
| 6459 | Returns \c true if this string is null; otherwise returns \c false. |
| 6460 | |
| 6461 | Example: |
| 6462 | |
| 6463 | \snippet qstring/main.cpp 28 |
| 6464 | |
| 6465 | Qt makes a distinction between null strings and empty strings for |
| 6466 | historical reasons. For most applications, what matters is |
| 6467 | whether or not a string contains any data, and this can be |
| 6468 | determined using the isEmpty() function. |
| 6469 | |
| 6470 | \sa isEmpty() |
| 6471 | */ |
| 6472 | |
| 6473 | /*! \fn bool QString::isEmpty() const |
| 6474 | |
| 6475 | Returns \c true if the string has no characters; otherwise returns |
| 6476 | \c false. |
| 6477 | |
| 6478 | Example: |
| 6479 | |
| 6480 | \snippet qstring/main.cpp 27 |
| 6481 | |
| 6482 | \sa size() |
| 6483 | */ |
| 6484 | |
| 6485 | /*! \fn QString &QString::operator+=(const QString &other) |
| 6486 | |
| 6487 | Appends the string \a other onto the end of this string and |
| 6488 | returns a reference to this string. |
| 6489 | |
| 6490 | Example: |
| 6491 | |
| 6492 | \snippet qstring/main.cpp 84 |
| 6493 | |
| 6494 | This operation is typically very fast (\l{constant time}), |
| 6495 | because QString preallocates extra space at the end of the string |
| 6496 | data so it can grow without reallocating the entire string each |
| 6497 | time. |
| 6498 | |
| 6499 | \sa append(), prepend() |
| 6500 | */ |
| 6501 | |
| 6502 | /*! \fn QString &QString::operator+=(QLatin1StringView str) |
| 6503 | |
| 6504 | \overload operator+=() |
| 6505 | |
| 6506 | Appends the Latin-1 string viewed by \a str to this string. |
| 6507 | */ |
| 6508 | |
| 6509 | /*! \fn QString &QString::operator+=(QUtf8StringView str) |
| 6510 | \since 6.5 |
| 6511 | \overload operator+=() |
| 6512 | |
| 6513 | Appends the UTF-8 string view \a str to this string. |
| 6514 | */ |
| 6515 | |
| 6516 | /*! \fn QString &QString::operator+=(const QByteArray &ba) |
| 6517 | |
| 6518 | \overload operator+=() |
| 6519 | |
| 6520 | Appends the byte array \a ba to this string. The byte array is converted |
| 6521 | to Unicode using the fromUtf8() function. If any NUL characters ('\\0') |
| 6522 | are embedded in the \a ba byte array, they will be included in the |
| 6523 | transformation. |
| 6524 | |
| 6525 | You can disable this function by defining |
| 6526 | \l QT_NO_CAST_FROM_ASCII when you compile your applications. This |
| 6527 | can be useful if you want to ensure that all user-visible strings |
| 6528 | go through QObject::tr(), for example. |
| 6529 | */ |
| 6530 | |
| 6531 | /*! \fn QString &QString::operator+=(const char *str) |
| 6532 | |
| 6533 | \overload operator+=() |
| 6534 | |
| 6535 | Appends the string \a str to this string. The const char pointer |
| 6536 | is converted to Unicode using the fromUtf8() function. |
| 6537 | |
| 6538 | You can disable this function by defining \l QT_NO_CAST_FROM_ASCII |
| 6539 | when you compile your applications. This can be useful if you want |
| 6540 | to ensure that all user-visible strings go through QObject::tr(), |
| 6541 | for example. |
| 6542 | */ |
| 6543 | |
| 6544 | /*! \fn QString &QString::operator+=(QStringView str) |
| 6545 | \since 6.0 |
| 6546 | \overload operator+=() |
| 6547 | |
| 6548 | Appends the string view \a str to this string. |
| 6549 | */ |
| 6550 | |
| 6551 | /*! \fn QString &QString::operator+=(QChar ch) |
| 6552 | |
| 6553 | \overload operator+=() |
| 6554 | |
| 6555 | Appends the character \a ch to the string. |
| 6556 | */ |
| 6557 | |
| 6558 | /*! |
| 6559 | \fn bool QString::operator==(const char * const &lhs, const QString &rhs) |
| 6560 | |
| 6561 | \overload operator==() |
| 6562 | |
| 6563 | Returns \c true if \a lhs is equal to \a rhs; otherwise returns \c false. |
| 6564 | Note that no string is equal to \a lhs being 0. |
| 6565 | |
| 6566 | Equivalent to \c {lhs != 0 && compare(lhs, rhs) == 0}. |
| 6567 | */ |
| 6568 | |
| 6569 | /*! |
| 6570 | \fn bool QString::operator!=(const char * const &lhs, const QString &rhs) |
| 6571 | |
| 6572 | Returns \c true if \a lhs is not equal to \a rhs; otherwise returns |
| 6573 | \c false. |
| 6574 | |
| 6575 | For \a lhs != 0, this is equivalent to \c {compare(} \a lhs, \a rhs |
| 6576 | \c {) != 0}. Note that no string is equal to \a lhs being 0. |
| 6577 | */ |
| 6578 | |
| 6579 | /*! |
| 6580 | \fn bool QString::operator<(const char * const &lhs, const QString &rhs) |
| 6581 | |
| 6582 | Returns \c true if \a lhs is lexically less than \a rhs; otherwise |
| 6583 | returns \c false. For \a lhs != 0, this is equivalent to \c |
| 6584 | {compare(lhs, rhs) < 0}. |
| 6585 | |
| 6586 | \sa {Comparing Strings} |
| 6587 | */ |
| 6588 | |
| 6589 | /*! |
| 6590 | \fn bool QString::operator<=(const char * const &lhs, const QString &rhs) |
| 6591 | |
| 6592 | Returns \c true if \a lhs is lexically less than or equal to \a rhs; |
| 6593 | otherwise returns \c false. For \a lhs != 0, this is equivalent to \c |
| 6594 | {compare(lhs, rhs) <= 0}. |
| 6595 | |
| 6596 | \sa {Comparing Strings} |
| 6597 | */ |
| 6598 | |
| 6599 | /*! |
| 6600 | \fn bool QString::operator>(const char * const &lhs, const QString &rhs) |
| 6601 | |
| 6602 | Returns \c true if \a lhs is lexically greater than \a rhs; otherwise |
| 6603 | returns \c false. Equivalent to \c {compare(lhs, rhs) > 0}. |
| 6604 | |
| 6605 | \sa {Comparing Strings} |
| 6606 | */ |
| 6607 | |
| 6608 | /*! |
| 6609 | \fn bool QString::operator>=(const char * const &lhs, const QString &rhs) |
| 6610 | |
| 6611 | Returns \c true if \a lhs is lexically greater than or equal to \a rhs; |
| 6612 | otherwise returns \c false. For \a lhs != 0, this is equivalent to \c |
| 6613 | {compare(lhs, rhs) >= 0}. |
| 6614 | |
| 6615 | \sa {Comparing Strings} |
| 6616 | */ |
| 6617 | |
| 6618 | /*! |
| 6619 | \fn QString operator+(const QString &s1, const QString &s2) |
| 6620 | \fn QString operator+(QString &&s1, const QString &s2) |
| 6621 | \relates QString |
| 6622 | |
| 6623 | Returns a string which is the result of concatenating \a s1 and \a |
| 6624 | s2. |
| 6625 | */ |
| 6626 | |
| 6627 | /*! |
| 6628 | \fn QString operator+(const QString &s1, const char *s2) |
| 6629 | \relates QString |
| 6630 | |
| 6631 | Returns a string which is the result of concatenating \a s1 and \a |
| 6632 | s2 (\a s2 is converted to Unicode using the QString::fromUtf8() |
| 6633 | function). |
| 6634 | |
| 6635 | \sa QString::fromUtf8() |
| 6636 | */ |
| 6637 | |
| 6638 | /*! |
| 6639 | \fn QString operator+(const char *s1, const QString &s2) |
| 6640 | \relates QString |
| 6641 | |
| 6642 | Returns a string which is the result of concatenating \a s1 and \a |
| 6643 | s2 (\a s1 is converted to Unicode using the QString::fromUtf8() |
| 6644 | function). |
| 6645 | |
| 6646 | \sa QString::fromUtf8() |
| 6647 | */ |
| 6648 | |
| 6649 | /*! |
| 6650 | \fn QString operator+(QStringView lhs, const QString &rhs) |
| 6651 | \fn QString operator+(const QString &lhs, QStringView rhs) |
| 6652 | |
| 6653 | \relates QString |
| 6654 | \since 6.9 |
| 6655 | |
| 6656 | Returns a string that is the result of concatenating \a lhs and \a rhs. |
| 6657 | */ |
| 6658 | |
| 6659 | /*! |
| 6660 | \fn int QString::compare(const QString &s1, const QString &s2, Qt::CaseSensitivity cs) |
| 6661 | \since 4.2 |
| 6662 | |
| 6663 | Compares the string \a s1 with the string \a s2 and returns a negative integer |
| 6664 | if \a s1 is less than \a s2, a positive integer if it is greater than \a s2, |
| 6665 | and zero if they are equal. |
| 6666 | |
| 6667 | \include qstring.qdocinc {search-comparison-case-sensitivity} {comparison} |
| 6668 | |
| 6669 | Case sensitive comparison is based exclusively on the numeric |
| 6670 | Unicode values of the characters and is very fast, but is not what |
| 6671 | a human would expect. Consider sorting user-visible strings with |
| 6672 | localeAwareCompare(). |
| 6673 | |
| 6674 | \snippet qstring/main.cpp 16 |
| 6675 | |
| 6676 | //! [compare-isNull-vs-isEmpty] |
| 6677 | \note This function treats null strings the same as empty strings, |
| 6678 | for more details see \l {Distinction Between Null and Empty Strings}. |
| 6679 | //! [compare-isNull-vs-isEmpty] |
| 6680 | |
| 6681 | \sa operator==(), operator<(), operator>(), {Comparing Strings} |
| 6682 | */ |
| 6683 | |
| 6684 | /*! |
| 6685 | \fn int QString::compare(const QString &s1, QLatin1StringView s2, Qt::CaseSensitivity cs) |
| 6686 | \since 4.2 |
| 6687 | \overload compare() |
| 6688 | |
| 6689 | Performs a comparison of \a s1 and \a s2, using the case |
| 6690 | sensitivity setting \a cs. |
| 6691 | */ |
| 6692 | |
| 6693 | /*! |
| 6694 | \fn int QString::compare(QLatin1StringView s1, const QString &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive) |
| 6695 | |
| 6696 | \since 4.2 |
| 6697 | \overload compare() |
| 6698 | |
| 6699 | Performs a comparison of \a s1 and \a s2, using the case |
| 6700 | sensitivity setting \a cs. |
| 6701 | */ |
| 6702 | |
| 6703 | /*! |
| 6704 | \fn int QString::compare(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
| 6705 | |
| 6706 | \since 5.12 |
| 6707 | \overload compare() |
| 6708 | |
| 6709 | Performs a comparison of this with \a s, using the case |
| 6710 | sensitivity setting \a cs. |
| 6711 | */ |
| 6712 | |
| 6713 | /*! |
| 6714 | \fn int QString::compare(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
| 6715 | |
| 6716 | \since 5.14 |
| 6717 | \overload compare() |
| 6718 | |
| 6719 | Performs a comparison of this with \a ch, using the case |
| 6720 | sensitivity setting \a cs. |
| 6721 | */ |
| 6722 | |
| 6723 | /*! |
| 6724 | \overload compare() |
| 6725 | \since 4.2 |
| 6726 | |
| 6727 | Lexically compares this string with the string \a other and returns |
| 6728 | a negative integer if this string is less than \a other, a positive |
| 6729 | integer if it is greater than \a other, and zero if they are equal. |
| 6730 | |
| 6731 | Same as compare(*this, \a other, \a cs). |
| 6732 | */ |
| 6733 | int QString::compare(const QString &other, Qt::CaseSensitivity cs) const noexcept |
| 6734 | { |
| 6735 | return QtPrivate::compareStrings(lhs: *this, rhs: other, cs); |
| 6736 | } |
| 6737 | |
| 6738 | /*! |
| 6739 | \internal |
| 6740 | \since 4.5 |
| 6741 | */ |
| 6742 | int QString::compare_helper(const QChar *data1, qsizetype length1, const QChar *data2, qsizetype length2, |
| 6743 | Qt::CaseSensitivity cs) noexcept |
| 6744 | { |
| 6745 | Q_ASSERT(length1 >= 0); |
| 6746 | Q_ASSERT(length2 >= 0); |
| 6747 | Q_ASSERT(data1 || length1 == 0); |
| 6748 | Q_ASSERT(data2 || length2 == 0); |
| 6749 | return QtPrivate::compareStrings(lhs: QStringView(data1, length1), rhs: QStringView(data2, length2), cs); |
| 6750 | } |
| 6751 | |
| 6752 | /*! |
| 6753 | \overload compare() |
| 6754 | \since 4.2 |
| 6755 | |
| 6756 | Same as compare(*this, \a other, \a cs). |
| 6757 | */ |
| 6758 | int QString::compare(QLatin1StringView other, Qt::CaseSensitivity cs) const noexcept |
| 6759 | { |
| 6760 | return QtPrivate::compareStrings(lhs: *this, rhs: other, cs); |
| 6761 | } |
| 6762 | |
| 6763 | /*! |
| 6764 | \internal |
| 6765 | \since 5.0 |
| 6766 | */ |
| 6767 | int QString::compare_helper(const QChar *data1, qsizetype length1, const char *data2, qsizetype length2, |
| 6768 | Qt::CaseSensitivity cs) noexcept |
| 6769 | { |
| 6770 | Q_ASSERT(length1 >= 0); |
| 6771 | Q_ASSERT(data1 || length1 == 0); |
| 6772 | if (!data2) |
| 6773 | return qt_lencmp(lhs: length1, rhs: 0); |
| 6774 | if (Q_UNLIKELY(length2 < 0)) |
| 6775 | length2 = qsizetype(strlen(s: data2)); |
| 6776 | return QtPrivate::compareStrings(lhs: QStringView(data1, length1), |
| 6777 | rhs: QUtf8StringView(data2, length2), cs); |
| 6778 | } |
| 6779 | |
| 6780 | /*! |
| 6781 | \fn int QString::compare(const QString &s1, QStringView s2, Qt::CaseSensitivity cs = Qt::CaseSensitive) |
| 6782 | \overload compare() |
| 6783 | */ |
| 6784 | |
| 6785 | /*! |
| 6786 | \fn int QString::compare(QStringView s1, const QString &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive) |
| 6787 | \overload compare() |
| 6788 | */ |
| 6789 | |
| 6790 | bool comparesEqual(const QByteArrayView &lhs, const QChar &rhs) noexcept |
| 6791 | { |
| 6792 | return QtPrivate::equalStrings(lhs: QUtf8StringView(lhs), rhs: QStringView(&rhs, 1)); |
| 6793 | } |
| 6794 | |
| 6795 | Qt::strong_ordering compareThreeWay(const QByteArrayView &lhs, const QChar &rhs) noexcept |
| 6796 | { |
| 6797 | const int res = QtPrivate::compareStrings(lhs: QUtf8StringView(lhs), rhs: QStringView(&rhs, 1)); |
| 6798 | return Qt::compareThreeWay(lhs: res, rhs: 0); |
| 6799 | } |
| 6800 | |
| 6801 | bool comparesEqual(const QByteArrayView &lhs, char16_t rhs) noexcept |
| 6802 | { |
| 6803 | return QtPrivate::equalStrings(lhs: QUtf8StringView(lhs), rhs: QStringView(&rhs, 1)); |
| 6804 | } |
| 6805 | |
| 6806 | Qt::strong_ordering compareThreeWay(const QByteArrayView &lhs, char16_t rhs) noexcept |
| 6807 | { |
| 6808 | const int res = QtPrivate::compareStrings(lhs: QUtf8StringView(lhs), rhs: QStringView(&rhs, 1)); |
| 6809 | return Qt::compareThreeWay(lhs: res, rhs: 0); |
| 6810 | } |
| 6811 | |
| 6812 | bool comparesEqual(const QByteArray &lhs, const QChar &rhs) noexcept |
| 6813 | { |
| 6814 | return QtPrivate::equalStrings(lhs: QUtf8StringView(lhs), rhs: QStringView(&rhs, 1)); |
| 6815 | } |
| 6816 | |
| 6817 | Qt::strong_ordering compareThreeWay(const QByteArray &lhs, const QChar &rhs) noexcept |
| 6818 | { |
| 6819 | const int res = QtPrivate::compareStrings(lhs: QUtf8StringView(lhs), rhs: QStringView(&rhs, 1)); |
| 6820 | return Qt::compareThreeWay(lhs: res, rhs: 0); |
| 6821 | } |
| 6822 | |
| 6823 | bool comparesEqual(const QByteArray &lhs, char16_t rhs) noexcept |
| 6824 | { |
| 6825 | return QtPrivate::equalStrings(lhs: QUtf8StringView(lhs), rhs: QStringView(&rhs, 1)); |
| 6826 | } |
| 6827 | |
| 6828 | Qt::strong_ordering compareThreeWay(const QByteArray &lhs, char16_t rhs) noexcept |
| 6829 | { |
| 6830 | const int res = QtPrivate::compareStrings(lhs: QUtf8StringView(lhs), rhs: QStringView(&rhs, 1)); |
| 6831 | return Qt::compareThreeWay(lhs: res, rhs: 0); |
| 6832 | } |
| 6833 | |
| 6834 | /*! |
| 6835 | \internal |
| 6836 | \since 6.8 |
| 6837 | */ |
| 6838 | bool QT_FASTCALL QChar::equal_helper(QChar lhs, const char *rhs) noexcept |
| 6839 | { |
| 6840 | return QtPrivate::equalStrings(lhs: QStringView(&lhs, 1), rhs: QUtf8StringView(rhs)); |
| 6841 | } |
| 6842 | |
| 6843 | int QT_FASTCALL QChar::compare_helper(QChar lhs, const char *rhs) noexcept |
| 6844 | { |
| 6845 | return QtPrivate::compareStrings(lhs: QStringView(&lhs, 1), rhs: QUtf8StringView(rhs)); |
| 6846 | } |
| 6847 | |
| 6848 | /*! |
| 6849 | \internal |
| 6850 | \since 6.8 |
| 6851 | */ |
| 6852 | bool QStringView::equal_helper(QStringView sv, const char *data, qsizetype len) |
| 6853 | { |
| 6854 | Q_ASSERT(len >= 0); |
| 6855 | Q_ASSERT(data || len == 0); |
| 6856 | return QtPrivate::equalStrings(lhs: sv, rhs: QUtf8StringView(data, len)); |
| 6857 | } |
| 6858 | |
| 6859 | /*! |
| 6860 | \internal |
| 6861 | \since 6.8 |
| 6862 | */ |
| 6863 | int QStringView::compare_helper(QStringView sv, const char *data, qsizetype len) |
| 6864 | { |
| 6865 | Q_ASSERT(len >= 0); |
| 6866 | Q_ASSERT(data || len == 0); |
| 6867 | return QtPrivate::compareStrings(lhs: sv, rhs: QUtf8StringView(data, len)); |
| 6868 | } |
| 6869 | |
| 6870 | /*! |
| 6871 | \internal |
| 6872 | \since 6.8 |
| 6873 | */ |
| 6874 | bool QLatin1StringView::equal_helper(QLatin1StringView s1, const char *s2, qsizetype len) noexcept |
| 6875 | { |
| 6876 | // because qlatin1stringview.h can't include qutf8stringview.h |
| 6877 | Q_ASSERT(len >= 0); |
| 6878 | Q_ASSERT(s2 || len == 0); |
| 6879 | return QtPrivate::equalStrings(lhs: s1, rhs: QUtf8StringView(s2, len)); |
| 6880 | } |
| 6881 | |
| 6882 | /*! |
| 6883 | \internal |
| 6884 | \since 6.6 |
| 6885 | */ |
| 6886 | int QLatin1StringView::compare_helper(const QLatin1StringView &s1, const char *s2, qsizetype len) noexcept |
| 6887 | { |
| 6888 | // because qlatin1stringview.h can't include qutf8stringview.h |
| 6889 | Q_ASSERT(len >= 0); |
| 6890 | Q_ASSERT(s2 || len == 0); |
| 6891 | return QtPrivate::compareStrings(lhs: s1, rhs: QUtf8StringView(s2, len)); |
| 6892 | } |
| 6893 | |
| 6894 | /*! |
| 6895 | \internal |
| 6896 | \since 4.5 |
| 6897 | */ |
| 6898 | int QLatin1StringView::compare_helper(const QChar *data1, qsizetype length1, QLatin1StringView s2, |
| 6899 | Qt::CaseSensitivity cs) noexcept |
| 6900 | { |
| 6901 | Q_ASSERT(length1 >= 0); |
| 6902 | Q_ASSERT(data1 || length1 == 0); |
| 6903 | return QtPrivate::compareStrings(lhs: QStringView(data1, length1), rhs: s2, cs); |
| 6904 | } |
| 6905 | |
| 6906 | /*! |
| 6907 | \fn int QString::localeAwareCompare(const QString & s1, const QString & s2) |
| 6908 | |
| 6909 | Compares \a s1 with \a s2 and returns an integer less than, equal |
| 6910 | to, or greater than zero if \a s1 is less than, equal to, or |
| 6911 | greater than \a s2. |
| 6912 | |
| 6913 | The comparison is performed in a locale- and also |
| 6914 | platform-dependent manner. Use this function to present sorted |
| 6915 | lists of strings to the user. |
| 6916 | |
| 6917 | \sa compare(), QLocale, {Comparing Strings} |
| 6918 | */ |
| 6919 | |
| 6920 | /*! |
| 6921 | \fn int QString::localeAwareCompare(QStringView other) const |
| 6922 | \since 6.0 |
| 6923 | \overload localeAwareCompare() |
| 6924 | |
| 6925 | Compares this string with the \a other string and returns an |
| 6926 | integer less than, equal to, or greater than zero if this string |
| 6927 | is less than, equal to, or greater than the \a other string. |
| 6928 | |
| 6929 | The comparison is performed in a locale- and also |
| 6930 | platform-dependent manner. Use this function to present sorted |
| 6931 | lists of strings to the user. |
| 6932 | |
| 6933 | Same as \c {localeAwareCompare(*this, other)}. |
| 6934 | |
| 6935 | \sa {Comparing Strings} |
| 6936 | */ |
| 6937 | |
| 6938 | /*! |
| 6939 | \fn int QString::localeAwareCompare(QStringView s1, QStringView s2) |
| 6940 | \since 6.0 |
| 6941 | \overload localeAwareCompare() |
| 6942 | |
| 6943 | Compares \a s1 with \a s2 and returns an integer less than, equal |
| 6944 | to, or greater than zero if \a s1 is less than, equal to, or |
| 6945 | greater than \a s2. |
| 6946 | |
| 6947 | The comparison is performed in a locale- and also |
| 6948 | platform-dependent manner. Use this function to present sorted |
| 6949 | lists of strings to the user. |
| 6950 | |
| 6951 | \sa {Comparing Strings} |
| 6952 | */ |
| 6953 | |
| 6954 | |
| 6955 | #if !defined(CSTR_LESS_THAN) |
| 6956 | #define CSTR_LESS_THAN 1 |
| 6957 | #define CSTR_EQUAL 2 |
| 6958 | #define CSTR_GREATER_THAN 3 |
| 6959 | #endif |
| 6960 | |
| 6961 | /*! |
| 6962 | \overload localeAwareCompare() |
| 6963 | |
| 6964 | Compares this string with the \a other string and returns an |
| 6965 | integer less than, equal to, or greater than zero if this string |
| 6966 | is less than, equal to, or greater than the \a other string. |
| 6967 | |
| 6968 | The comparison is performed in a locale- and also |
| 6969 | platform-dependent manner. Use this function to present sorted |
| 6970 | lists of strings to the user. |
| 6971 | |
| 6972 | Same as \c {localeAwareCompare(*this, other)}. |
| 6973 | |
| 6974 | \sa {Comparing Strings} |
| 6975 | */ |
| 6976 | int QString::localeAwareCompare(const QString &other) const |
| 6977 | { |
| 6978 | return localeAwareCompare_helper(data1: constData(), length1: size(), data2: other.constData(), length2: other.size()); |
| 6979 | } |
| 6980 | |
| 6981 | /*! |
| 6982 | \internal |
| 6983 | \since 4.5 |
| 6984 | */ |
| 6985 | int QString::localeAwareCompare_helper(const QChar *data1, qsizetype length1, |
| 6986 | const QChar *data2, qsizetype length2) |
| 6987 | { |
| 6988 | Q_ASSERT(length1 >= 0); |
| 6989 | Q_ASSERT(data1 || length1 == 0); |
| 6990 | Q_ASSERT(length2 >= 0); |
| 6991 | Q_ASSERT(data2 || length2 == 0); |
| 6992 | |
| 6993 | // do the right thing for null and empty |
| 6994 | if (length1 == 0 || length2 == 0) |
| 6995 | return QtPrivate::compareStrings(lhs: QStringView(data1, length1), rhs: QStringView(data2, length2), |
| 6996 | cs: Qt::CaseSensitive); |
| 6997 | |
| 6998 | #if QT_CONFIG(icu) |
| 6999 | return QCollator::defaultCompare(s1: QStringView(data1, length1), s2: QStringView(data2, length2)); |
| 7000 | #else |
| 7001 | const QString lhs = QString::fromRawData(data1, length1).normalized(QString::NormalizationForm_C); |
| 7002 | const QString rhs = QString::fromRawData(data2, length2).normalized(QString::NormalizationForm_C); |
| 7003 | # if defined(Q_OS_WIN) |
| 7004 | int res = CompareStringEx(LOCALE_NAME_USER_DEFAULT, 0, (LPWSTR)lhs.constData(), lhs.length(), (LPWSTR)rhs.constData(), rhs.length(), NULL, NULL, 0); |
| 7005 | |
| 7006 | switch (res) { |
| 7007 | case CSTR_LESS_THAN: |
| 7008 | return -1; |
| 7009 | case CSTR_GREATER_THAN: |
| 7010 | return 1; |
| 7011 | default: |
| 7012 | return 0; |
| 7013 | } |
| 7014 | # elif defined (Q_OS_DARWIN) |
| 7015 | // Use CFStringCompare for comparing strings on Mac. This makes Qt order |
| 7016 | // strings the same way as native applications do, and also respects |
| 7017 | // the "Order for sorted lists" setting in the International preferences |
| 7018 | // panel. |
| 7019 | const CFStringRef thisString = |
| 7020 | CFStringCreateWithCharactersNoCopy(kCFAllocatorDefault, |
| 7021 | reinterpret_cast<const UniChar *>(lhs.constData()), lhs.length(), kCFAllocatorNull); |
| 7022 | const CFStringRef otherString = |
| 7023 | CFStringCreateWithCharactersNoCopy(kCFAllocatorDefault, |
| 7024 | reinterpret_cast<const UniChar *>(rhs.constData()), rhs.length(), kCFAllocatorNull); |
| 7025 | |
| 7026 | const int result = CFStringCompare(thisString, otherString, kCFCompareLocalized); |
| 7027 | CFRelease(thisString); |
| 7028 | CFRelease(otherString); |
| 7029 | return result; |
| 7030 | # elif defined(Q_OS_UNIX) |
| 7031 | // declared in <string.h> (no better than QtPrivate::compareStrings() on Android, sadly) |
| 7032 | return strcoll(lhs.toLocal8Bit().constData(), rhs.toLocal8Bit().constData()); |
| 7033 | # else |
| 7034 | # error "This case shouldn't happen" |
| 7035 | return QtPrivate::compareStrings(lhs, rhs, Qt::CaseSensitive); |
| 7036 | # endif |
| 7037 | #endif // !QT_CONFIG(icu) |
| 7038 | } |
| 7039 | |
| 7040 | |
| 7041 | /*! |
| 7042 | \fn const QChar *QString::unicode() const |
| 7043 | |
| 7044 | Returns a Unicode representation of the string. |
| 7045 | The result remains valid until the string is modified. |
| 7046 | |
| 7047 | \note The returned string may not be '\\0'-terminated. |
| 7048 | Use size() to determine the length of the array. |
| 7049 | |
| 7050 | \sa utf16(), fromRawData() |
| 7051 | */ |
| 7052 | |
| 7053 | /*! |
| 7054 | \fn const ushort *QString::utf16() const |
| 7055 | |
| 7056 | Returns the QString as a '\\0\'-terminated array of unsigned |
| 7057 | shorts. The result remains valid until the string is modified. |
| 7058 | |
| 7059 | The returned string is in host byte order. |
| 7060 | |
| 7061 | \sa unicode() |
| 7062 | */ |
| 7063 | |
| 7064 | const ushort *QString::utf16() const |
| 7065 | { |
| 7066 | if (!d->isMutable()) { |
| 7067 | // ensure '\0'-termination for ::fromRawData strings |
| 7068 | const_cast<QString*>(this)->reallocData(alloc: d.size, option: QArrayData::KeepSize); |
| 7069 | } |
| 7070 | return reinterpret_cast<const ushort *>(d.data()); |
| 7071 | } |
| 7072 | |
| 7073 | /*! |
| 7074 | \fn QString &QString::nullTerminate() |
| 7075 | \since 6.10 |
| 7076 | |
| 7077 | If this string data isn't null-terminated, this method will make a deep |
| 7078 | copy of the data and make it null-terminated. |
| 7079 | |
| 7080 | A QString is null-terminated by default, however in some cases (e.g. |
| 7081 | when using fromRawData()), the string data doesn't necessarily end |
| 7082 | with a \c {\0} character, which could be a problem when calling methods |
| 7083 | that expect a null-terminated string. |
| 7084 | |
| 7085 | \sa nullTerminated(), fromRawData(), setRawData() |
| 7086 | */ |
| 7087 | QString &QString::nullTerminate() |
| 7088 | { |
| 7089 | // ensure '\0'-termination for ::fromRawData strings |
| 7090 | if (!d->isMutable()) |
| 7091 | *this = QString{constData(), size()}; |
| 7092 | return *this; |
| 7093 | } |
| 7094 | |
| 7095 | /*! |
| 7096 | \fn QString QString::nullTerminated() const & |
| 7097 | \fn QString QString::nullTerminated() && |
| 7098 | \since 6.10 |
| 7099 | |
| 7100 | Returns a copy of this string that is always null-terminated. |
| 7101 | |
| 7102 | \sa nullTerminate(), fromRawData(), setRawData() |
| 7103 | */ |
| 7104 | QString QString::nullTerminated() const & |
| 7105 | { |
| 7106 | // ensure '\0'-termination for ::fromRawData strings |
| 7107 | if (!d->isMutable()) |
| 7108 | return QString{constData(), size()}; |
| 7109 | return *this; |
| 7110 | } |
| 7111 | |
| 7112 | QString QString::nullTerminated() && |
| 7113 | { |
| 7114 | nullTerminate(); |
| 7115 | return std::move(*this); |
| 7116 | } |
| 7117 | |
| 7118 | /*! |
| 7119 | Returns a string of size \a width that contains this string |
| 7120 | padded by the \a fill character. |
| 7121 | |
| 7122 | If \a truncate is \c false and the size() of the string is more than |
| 7123 | \a width, then the returned string is a copy of the string. |
| 7124 | |
| 7125 | \snippet qstring/main.cpp 32 |
| 7126 | |
| 7127 | If \a truncate is \c true and the size() of the string is more than |
| 7128 | \a width, then any characters in a copy of the string after |
| 7129 | position \a width are removed, and the copy is returned. |
| 7130 | |
| 7131 | \snippet qstring/main.cpp 33 |
| 7132 | |
| 7133 | \sa rightJustified() |
| 7134 | */ |
| 7135 | |
| 7136 | QString QString::leftJustified(qsizetype width, QChar fill, bool truncate) const |
| 7137 | { |
| 7138 | QString result; |
| 7139 | qsizetype len = size(); |
| 7140 | qsizetype padlen = width - len; |
| 7141 | if (padlen > 0) { |
| 7142 | result.resize(size: len+padlen); |
| 7143 | if (len) |
| 7144 | memcpy(dest: result.d.data(), src: d.data(), n: sizeof(QChar)*len); |
| 7145 | QChar *uc = (QChar*)result.d.data() + len; |
| 7146 | while (padlen--) |
| 7147 | * uc++ = fill; |
| 7148 | } else { |
| 7149 | if (truncate) |
| 7150 | result = left(n: width); |
| 7151 | else |
| 7152 | result = *this; |
| 7153 | } |
| 7154 | return result; |
| 7155 | } |
| 7156 | |
| 7157 | /*! |
| 7158 | Returns a string of size() \a width that contains the \a fill |
| 7159 | character followed by the string. For example: |
| 7160 | |
| 7161 | \snippet qstring/main.cpp 49 |
| 7162 | |
| 7163 | If \a truncate is \c false and the size() of the string is more than |
| 7164 | \a width, then the returned string is a copy of the string. |
| 7165 | |
| 7166 | If \a truncate is true and the size() of the string is more than |
| 7167 | \a width, then the resulting string is truncated at position \a |
| 7168 | width. |
| 7169 | |
| 7170 | \snippet qstring/main.cpp 50 |
| 7171 | |
| 7172 | \sa leftJustified() |
| 7173 | */ |
| 7174 | |
| 7175 | QString QString::rightJustified(qsizetype width, QChar fill, bool truncate) const |
| 7176 | { |
| 7177 | QString result; |
| 7178 | qsizetype len = size(); |
| 7179 | qsizetype padlen = width - len; |
| 7180 | if (padlen > 0) { |
| 7181 | result.resize(size: len+padlen); |
| 7182 | QChar *uc = (QChar*)result.d.data(); |
| 7183 | while (padlen--) |
| 7184 | * uc++ = fill; |
| 7185 | if (len) |
| 7186 | memcpy(dest: static_cast<void *>(uc), src: static_cast<const void *>(d.data()), n: sizeof(QChar)*len); |
| 7187 | } else { |
| 7188 | if (truncate) |
| 7189 | result = left(n: width); |
| 7190 | else |
| 7191 | result = *this; |
| 7192 | } |
| 7193 | return result; |
| 7194 | } |
| 7195 | |
| 7196 | /*! |
| 7197 | \fn QString QString::toLower() const |
| 7198 | |
| 7199 | Returns a lowercase copy of the string. |
| 7200 | |
| 7201 | \snippet qstring/main.cpp 75 |
| 7202 | |
| 7203 | The case conversion will always happen in the 'C' locale. For |
| 7204 | locale-dependent case folding use QLocale::toLower() |
| 7205 | |
| 7206 | \sa toUpper(), QLocale::toLower() |
| 7207 | */ |
| 7208 | |
| 7209 | namespace QUnicodeTables { |
| 7210 | /* |
| 7211 | \internal |
| 7212 | Converts the \a str string starting from the position pointed to by the \a |
| 7213 | it iterator, using the Unicode case traits \c Traits, and returns the |
| 7214 | result. The input string must not be empty (the convertCase function below |
| 7215 | guarantees that). |
| 7216 | |
| 7217 | The string type \c{T} is also a template and is either \c{const QString} or |
| 7218 | \c{QString}. This function can do both copy-conversion and in-place |
| 7219 | conversion depending on the state of the \a str parameter: |
| 7220 | \list |
| 7221 | \li \c{T} is \c{const QString}: copy-convert |
| 7222 | \li \c{T} is \c{QString} and its refcount != 1: copy-convert |
| 7223 | \li \c{T} is \c{QString} and its refcount == 1: in-place convert |
| 7224 | \endlist |
| 7225 | |
| 7226 | In copy-convert mode, the local variable \c{s} is detached from the input |
| 7227 | \a str. In the in-place convert mode, \a str is in moved-from state and |
| 7228 | \c{s} contains the only copy of the string, without reallocation (thus, |
| 7229 | \a it is still valid). |
| 7230 | |
| 7231 | There is one pathological case left: when the in-place conversion needs to |
| 7232 | reallocate memory to grow the buffer. In that case, we need to adjust the \a |
| 7233 | it pointer. |
| 7234 | */ |
| 7235 | template <typename T> |
| 7236 | Q_NEVER_INLINE |
| 7237 | static QString detachAndConvertCase(T &str, QStringIterator it, QUnicodeTables::Case which) |
| 7238 | { |
| 7239 | Q_ASSERT(!str.isEmpty()); |
| 7240 | QString s = std::move(str); // will copy if T is const QString |
| 7241 | QChar *pp = s.begin() + it.index(); // will detach if necessary |
| 7242 | |
| 7243 | do { |
| 7244 | const auto folded = fullConvertCase(uc: it.next(), which); |
| 7245 | if (Q_UNLIKELY(folded.size() > 1)) { |
| 7246 | if (folded.chars[0] == *pp && folded.size() == 2) { |
| 7247 | // special case: only second actually changed (e.g. surrogate pairs), |
| 7248 | // avoid slow case |
| 7249 | ++pp; |
| 7250 | *pp++ = folded.chars[1]; |
| 7251 | } else { |
| 7252 | // slow path: the string is growing |
| 7253 | qsizetype inpos = it.index() - 1; |
| 7254 | qsizetype outpos = pp - s.constBegin(); |
| 7255 | |
| 7256 | s.replace(pos: outpos, len: 1, after: reinterpret_cast<const QChar *>(folded.data()), alen: folded.size()); |
| 7257 | pp = const_cast<QChar *>(s.constBegin()) + outpos + folded.size(); |
| 7258 | |
| 7259 | // Adjust the input iterator if we are performing an in-place conversion |
| 7260 | if constexpr (!std::is_const<T>::value) |
| 7261 | it = QStringIterator(s.constBegin(), inpos + folded.size(), s.constEnd()); |
| 7262 | } |
| 7263 | } else { |
| 7264 | *pp++ = folded.chars[0]; |
| 7265 | } |
| 7266 | } while (it.hasNext()); |
| 7267 | |
| 7268 | return s; |
| 7269 | } |
| 7270 | |
| 7271 | template <typename T> |
| 7272 | static QString convertCase(T &str, QUnicodeTables::Case which) |
| 7273 | { |
| 7274 | const QChar *p = str.constBegin(); |
| 7275 | const QChar *e = p + str.size(); |
| 7276 | |
| 7277 | // this avoids out of bounds check in the loop |
| 7278 | while (e != p && e[-1].isHighSurrogate()) |
| 7279 | --e; |
| 7280 | |
| 7281 | QStringIterator it(p, e); |
| 7282 | while (it.hasNext()) { |
| 7283 | const char32_t uc = it.next(); |
| 7284 | if (qGetProp(ucs4: uc)->cases[which].diff) { |
| 7285 | it.recede(); |
| 7286 | return detachAndConvertCase(str, it, which); |
| 7287 | } |
| 7288 | } |
| 7289 | return std::move(str); |
| 7290 | } |
| 7291 | } // namespace QUnicodeTables |
| 7292 | |
| 7293 | QString QString::toLower_helper(const QString &str) |
| 7294 | { |
| 7295 | return QUnicodeTables::convertCase(str, which: QUnicodeTables::LowerCase); |
| 7296 | } |
| 7297 | |
| 7298 | QString QString::toLower_helper(QString &str) |
| 7299 | { |
| 7300 | return QUnicodeTables::convertCase(str, which: QUnicodeTables::LowerCase); |
| 7301 | } |
| 7302 | |
| 7303 | /*! |
| 7304 | \fn QString QString::toCaseFolded() const |
| 7305 | |
| 7306 | Returns the case folded equivalent of the string. For most Unicode |
| 7307 | characters this is the same as toLower(). |
| 7308 | */ |
| 7309 | |
| 7310 | QString QString::toCaseFolded_helper(const QString &str) |
| 7311 | { |
| 7312 | return QUnicodeTables::convertCase(str, which: QUnicodeTables::CaseFold); |
| 7313 | } |
| 7314 | |
| 7315 | QString QString::toCaseFolded_helper(QString &str) |
| 7316 | { |
| 7317 | return QUnicodeTables::convertCase(str, which: QUnicodeTables::CaseFold); |
| 7318 | } |
| 7319 | |
| 7320 | /*! |
| 7321 | \fn QString QString::toUpper() const |
| 7322 | |
| 7323 | Returns an uppercase copy of the string. |
| 7324 | |
| 7325 | \snippet qstring/main.cpp 81 |
| 7326 | |
| 7327 | The case conversion will always happen in the 'C' locale. For |
| 7328 | locale-dependent case folding use QLocale::toUpper(). |
| 7329 | |
| 7330 | \note In some cases the uppercase form of a string may be longer than the |
| 7331 | original. |
| 7332 | |
| 7333 | \sa toLower(), QLocale::toLower() |
| 7334 | */ |
| 7335 | |
| 7336 | QString QString::toUpper_helper(const QString &str) |
| 7337 | { |
| 7338 | return QUnicodeTables::convertCase(str, which: QUnicodeTables::UpperCase); |
| 7339 | } |
| 7340 | |
| 7341 | QString QString::toUpper_helper(QString &str) |
| 7342 | { |
| 7343 | return QUnicodeTables::convertCase(str, which: QUnicodeTables::UpperCase); |
| 7344 | } |
| 7345 | |
| 7346 | /*! |
| 7347 | \since 5.5 |
| 7348 | |
| 7349 | Safely builds a formatted string from the format string \a cformat |
| 7350 | and an arbitrary list of arguments. |
| 7351 | |
| 7352 | The format string supports the conversion specifiers, length modifiers, |
| 7353 | and flags provided by printf() in the standard C++ library. The \a cformat |
| 7354 | string and \c{%s} arguments must be UTF-8 encoded. |
| 7355 | |
| 7356 | \note The \c{%lc} escape sequence expects a unicode character of type |
| 7357 | \c char16_t, or \c ushort (as returned by QChar::unicode()). |
| 7358 | The \c{%ls} escape sequence expects a pointer to a zero-terminated array |
| 7359 | of unicode characters of type \c char16_t, or ushort (as returned by |
| 7360 | QString::utf16()). This is at odds with the printf() in the standard C++ |
| 7361 | library, which defines \c {%lc} to print a wchar_t and \c{%ls} to print |
| 7362 | a \c{wchar_t*}, and might also produce compiler warnings on platforms |
| 7363 | where the size of \c {wchar_t} is not 16 bits. |
| 7364 | |
| 7365 | \warning We do not recommend using QString::asprintf() in new Qt |
| 7366 | code. Instead, consider using QTextStream or arg(), both of |
| 7367 | which support Unicode strings seamlessly and are type-safe. |
| 7368 | Here is an example that uses QTextStream: |
| 7369 | |
| 7370 | \snippet qstring/main.cpp 64 |
| 7371 | |
| 7372 | For \l {QObject::tr()}{translations}, especially if the strings |
| 7373 | contains more than one escape sequence, you should consider using |
| 7374 | the arg() function instead. This allows the order of the |
| 7375 | replacements to be controlled by the translator. |
| 7376 | |
| 7377 | \sa arg() |
| 7378 | */ |
| 7379 | |
| 7380 | QString QString::asprintf(const char *cformat, ...) |
| 7381 | { |
| 7382 | va_list ap; |
| 7383 | va_start(ap, cformat); |
| 7384 | QString s = vasprintf(format: cformat, ap); |
| 7385 | va_end(ap); |
| 7386 | return s; |
| 7387 | } |
| 7388 | |
| 7389 | static void append_utf8(QString &qs, const char *cs, qsizetype len) |
| 7390 | { |
| 7391 | const qsizetype oldSize = qs.size(); |
| 7392 | qs.resize(size: oldSize + len); |
| 7393 | const QChar *newEnd = QUtf8::convertToUnicode(buffer: qs.data() + oldSize, in: QByteArrayView(cs, len)); |
| 7394 | qs.resize(size: newEnd - qs.constData()); |
| 7395 | } |
| 7396 | |
| 7397 | static uint parse_flag_characters(const char * &c) noexcept |
| 7398 | { |
| 7399 | uint flags = QLocaleData::ZeroPadExponent; |
| 7400 | while (true) { |
| 7401 | switch (*c) { |
| 7402 | case '#': |
| 7403 | flags |= QLocaleData::ShowBase | QLocaleData::AddTrailingZeroes |
| 7404 | | QLocaleData::ForcePoint; |
| 7405 | break; |
| 7406 | case '0': flags |= QLocaleData::ZeroPadded; break; |
| 7407 | case '-': flags |= QLocaleData::LeftAdjusted; break; |
| 7408 | case ' ': flags |= QLocaleData::BlankBeforePositive; break; |
| 7409 | case '+': flags |= QLocaleData::AlwaysShowSign; break; |
| 7410 | case '\'': flags |= QLocaleData::GroupDigits; break; |
| 7411 | default: return flags; |
| 7412 | } |
| 7413 | ++c; |
| 7414 | } |
| 7415 | } |
| 7416 | |
| 7417 | static int parse_field_width(const char *&c, qsizetype size) |
| 7418 | { |
| 7419 | Q_ASSERT(isAsciiDigit(*c)); |
| 7420 | const char *const stop = c + size; |
| 7421 | |
| 7422 | // can't be negative - started with a digit |
| 7423 | // contains at least one digit |
| 7424 | auto [result, used] = qstrntoull(nptr: c, size, base: 10); |
| 7425 | c += used; |
| 7426 | if (used <= 0) |
| 7427 | return false; |
| 7428 | // preserve Qt 5.5 behavior of consuming all digits, no matter how many |
| 7429 | while (c < stop && isAsciiDigit(c: *c)) |
| 7430 | ++c; |
| 7431 | return result < qulonglong(std::numeric_limits<int>::max()) ? int(result) : 0; |
| 7432 | } |
| 7433 | |
| 7434 | enum LengthMod { lm_none, lm_hh, lm_h, lm_l, lm_ll, lm_L, lm_j, lm_z, lm_t }; |
| 7435 | |
| 7436 | static inline bool can_consume(const char * &c, char ch) noexcept |
| 7437 | { |
| 7438 | if (*c == ch) { |
| 7439 | ++c; |
| 7440 | return true; |
| 7441 | } |
| 7442 | return false; |
| 7443 | } |
| 7444 | |
| 7445 | static LengthMod parse_length_modifier(const char * &c) noexcept |
| 7446 | { |
| 7447 | switch (*c++) { |
| 7448 | case 'h': return can_consume(c, ch: 'h') ? lm_hh : lm_h; |
| 7449 | case 'l': return can_consume(c, ch: 'l') ? lm_ll : lm_l; |
| 7450 | case 'L': return lm_L; |
| 7451 | case 'j': return lm_j; |
| 7452 | case 'z': |
| 7453 | case 'Z': return lm_z; |
| 7454 | case 't': return lm_t; |
| 7455 | } |
| 7456 | --c; // don't consume *c - it wasn't a flag |
| 7457 | return lm_none; |
| 7458 | } |
| 7459 | |
| 7460 | /*! |
| 7461 | \fn QString QString::vasprintf(const char *cformat, va_list ap) |
| 7462 | \since 5.5 |
| 7463 | |
| 7464 | Equivalent method to asprintf(), but takes a va_list \a ap |
| 7465 | instead a list of variable arguments. See the asprintf() |
| 7466 | documentation for an explanation of \a cformat. |
| 7467 | |
| 7468 | This method does not call the va_end macro, the caller |
| 7469 | is responsible to call va_end on \a ap. |
| 7470 | |
| 7471 | \sa asprintf() |
| 7472 | */ |
| 7473 | |
| 7474 | QString QString::vasprintf(const char *cformat, va_list ap) |
| 7475 | { |
| 7476 | if (!cformat || !*cformat) { |
| 7477 | // Qt 1.x compat |
| 7478 | return fromLatin1(ba: "" ); |
| 7479 | } |
| 7480 | |
| 7481 | // Parse cformat |
| 7482 | |
| 7483 | QString result; |
| 7484 | const char *c = cformat; |
| 7485 | const char *formatEnd = cformat + qstrlen(str: cformat); |
| 7486 | for (;;) { |
| 7487 | // Copy non-escape chars to result |
| 7488 | const char *cb = c; |
| 7489 | while (*c != '\0' && *c != '%') |
| 7490 | c++; |
| 7491 | append_utf8(qs&: result, cs: cb, len: qsizetype(c - cb)); |
| 7492 | |
| 7493 | if (*c == '\0') |
| 7494 | break; |
| 7495 | |
| 7496 | // Found '%' |
| 7497 | const char *escape_start = c; |
| 7498 | ++c; |
| 7499 | |
| 7500 | if (*c == '\0') { |
| 7501 | result.append(ch: u'%'); // a % at the end of the string - treat as non-escape text |
| 7502 | break; |
| 7503 | } |
| 7504 | if (*c == '%') { |
| 7505 | result.append(ch: u'%'); // %% |
| 7506 | ++c; |
| 7507 | continue; |
| 7508 | } |
| 7509 | |
| 7510 | uint flags = parse_flag_characters(c); |
| 7511 | |
| 7512 | if (*c == '\0') { |
| 7513 | result.append(str: QLatin1StringView(escape_start)); // incomplete escape, treat as non-escape text |
| 7514 | break; |
| 7515 | } |
| 7516 | |
| 7517 | // Parse field width |
| 7518 | int width = -1; // -1 means unspecified |
| 7519 | if (isAsciiDigit(c: *c)) { |
| 7520 | width = parse_field_width(c, size: formatEnd - c); |
| 7521 | } else if (*c == '*') { // can't parse this in another function, not portably, at least |
| 7522 | width = va_arg(ap, int); |
| 7523 | if (width < 0) |
| 7524 | width = -1; // treat all negative numbers as unspecified |
| 7525 | ++c; |
| 7526 | } |
| 7527 | |
| 7528 | if (*c == '\0') { |
| 7529 | result.append(str: QLatin1StringView(escape_start)); // incomplete escape, treat as non-escape text |
| 7530 | break; |
| 7531 | } |
| 7532 | |
| 7533 | // Parse precision |
| 7534 | int precision = -1; // -1 means unspecified |
| 7535 | if (*c == '.') { |
| 7536 | ++c; |
| 7537 | precision = 0; |
| 7538 | if (isAsciiDigit(c: *c)) { |
| 7539 | precision = parse_field_width(c, size: formatEnd - c); |
| 7540 | } else if (*c == '*') { // can't parse this in another function, not portably, at least |
| 7541 | precision = va_arg(ap, int); |
| 7542 | if (precision < 0) |
| 7543 | precision = -1; // treat all negative numbers as unspecified |
| 7544 | ++c; |
| 7545 | } |
| 7546 | } |
| 7547 | |
| 7548 | if (*c == '\0') { |
| 7549 | result.append(str: QLatin1StringView(escape_start)); // incomplete escape, treat as non-escape text |
| 7550 | break; |
| 7551 | } |
| 7552 | |
| 7553 | const LengthMod length_mod = parse_length_modifier(c); |
| 7554 | |
| 7555 | if (*c == '\0') { |
| 7556 | result.append(str: QLatin1StringView(escape_start)); // incomplete escape, treat as non-escape text |
| 7557 | break; |
| 7558 | } |
| 7559 | |
| 7560 | // Parse the conversion specifier and do the conversion |
| 7561 | QString subst; |
| 7562 | switch (*c) { |
| 7563 | case 'd': |
| 7564 | case 'i': { |
| 7565 | qint64 i; |
| 7566 | switch (length_mod) { |
| 7567 | case lm_none: i = va_arg(ap, int); break; |
| 7568 | case lm_hh: i = va_arg(ap, int); break; |
| 7569 | case lm_h: i = va_arg(ap, int); break; |
| 7570 | case lm_l: i = va_arg(ap, long int); break; |
| 7571 | case lm_ll: i = va_arg(ap, qint64); break; |
| 7572 | case lm_j: i = va_arg(ap, long int); break; |
| 7573 | |
| 7574 | /* ptrdiff_t actually, but it should be the same for us */ |
| 7575 | case lm_z: i = va_arg(ap, qsizetype); break; |
| 7576 | case lm_t: i = va_arg(ap, qsizetype); break; |
| 7577 | default: i = 0; break; |
| 7578 | } |
| 7579 | subst = QLocaleData::c()->longLongToString(l: i, precision, base: 10, width, flags); |
| 7580 | ++c; |
| 7581 | break; |
| 7582 | } |
| 7583 | case 'o': |
| 7584 | case 'u': |
| 7585 | case 'x': |
| 7586 | case 'X': { |
| 7587 | quint64 u; |
| 7588 | switch (length_mod) { |
| 7589 | case lm_none: u = va_arg(ap, uint); break; |
| 7590 | case lm_hh: u = va_arg(ap, uint); break; |
| 7591 | case lm_h: u = va_arg(ap, uint); break; |
| 7592 | case lm_l: u = va_arg(ap, ulong); break; |
| 7593 | case lm_ll: u = va_arg(ap, quint64); break; |
| 7594 | case lm_t: u = va_arg(ap, size_t); break; |
| 7595 | case lm_z: u = va_arg(ap, size_t); break; |
| 7596 | default: u = 0; break; |
| 7597 | } |
| 7598 | |
| 7599 | if (isAsciiUpper(c: *c)) |
| 7600 | flags |= QLocaleData::CapitalEorX; |
| 7601 | |
| 7602 | int base = 10; |
| 7603 | switch (QtMiscUtils::toAsciiLower(ch: *c)) { |
| 7604 | case 'o': |
| 7605 | base = 8; break; |
| 7606 | case 'u': |
| 7607 | base = 10; break; |
| 7608 | case 'x': |
| 7609 | base = 16; break; |
| 7610 | default: break; |
| 7611 | } |
| 7612 | subst = QLocaleData::c()->unsLongLongToString(l: u, precision, base, width, flags); |
| 7613 | ++c; |
| 7614 | break; |
| 7615 | } |
| 7616 | case 'E': |
| 7617 | case 'e': |
| 7618 | case 'F': |
| 7619 | case 'f': |
| 7620 | case 'G': |
| 7621 | case 'g': |
| 7622 | case 'A': |
| 7623 | case 'a': { |
| 7624 | double d; |
| 7625 | if (length_mod == lm_L) |
| 7626 | d = va_arg(ap, long double); // not supported - converted to a double |
| 7627 | else |
| 7628 | d = va_arg(ap, double); |
| 7629 | |
| 7630 | if (isAsciiUpper(c: *c)) |
| 7631 | flags |= QLocaleData::CapitalEorX; |
| 7632 | |
| 7633 | QLocaleData::DoubleForm form = QLocaleData::DFDecimal; |
| 7634 | switch (QtMiscUtils::toAsciiLower(ch: *c)) { |
| 7635 | case 'e': form = QLocaleData::DFExponent; break; |
| 7636 | case 'a': // not supported - decimal form used instead |
| 7637 | case 'f': form = QLocaleData::DFDecimal; break; |
| 7638 | case 'g': form = QLocaleData::DFSignificantDigits; break; |
| 7639 | default: break; |
| 7640 | } |
| 7641 | subst = QLocaleData::c()->doubleToString(d, precision, form, width, flags); |
| 7642 | ++c; |
| 7643 | break; |
| 7644 | } |
| 7645 | case 'c': { |
| 7646 | if (length_mod == lm_l) |
| 7647 | subst = QChar::fromUcs2(va_arg(ap, int)); |
| 7648 | else |
| 7649 | subst = QLatin1Char((uchar) va_arg(ap, int)); |
| 7650 | ++c; |
| 7651 | break; |
| 7652 | } |
| 7653 | case 's': { |
| 7654 | if (length_mod == lm_l) { |
| 7655 | const char16_t *buff = va_arg(ap, const char16_t*); |
| 7656 | const auto *ch = buff; |
| 7657 | while (precision != 0 && *ch != 0) { |
| 7658 | ++ch; |
| 7659 | --precision; |
| 7660 | } |
| 7661 | subst.setUtf16(utf16: buff, size: ch - buff); |
| 7662 | } else if (precision == -1) { |
| 7663 | subst = QString::fromUtf8(va_arg(ap, const char*)); |
| 7664 | } else { |
| 7665 | const char *buff = va_arg(ap, const char*); |
| 7666 | subst = QString::fromUtf8(utf8: buff, size: qstrnlen(str: buff, maxlen: precision)); |
| 7667 | } |
| 7668 | ++c; |
| 7669 | break; |
| 7670 | } |
| 7671 | case 'p': { |
| 7672 | void *arg = va_arg(ap, void*); |
| 7673 | const quint64 i = reinterpret_cast<quintptr>(arg); |
| 7674 | flags |= QLocaleData::ShowBase; |
| 7675 | subst = QLocaleData::c()->unsLongLongToString(l: i, precision, base: 16, width, flags); |
| 7676 | ++c; |
| 7677 | break; |
| 7678 | } |
| 7679 | case 'n': |
| 7680 | switch (length_mod) { |
| 7681 | case lm_hh: { |
| 7682 | signed char *n = va_arg(ap, signed char*); |
| 7683 | *n = result.size(); |
| 7684 | break; |
| 7685 | } |
| 7686 | case lm_h: { |
| 7687 | short int *n = va_arg(ap, short int*); |
| 7688 | *n = result.size(); |
| 7689 | break; |
| 7690 | } |
| 7691 | case lm_l: { |
| 7692 | long int *n = va_arg(ap, long int*); |
| 7693 | *n = result.size(); |
| 7694 | break; |
| 7695 | } |
| 7696 | case lm_ll: { |
| 7697 | qint64 *n = va_arg(ap, qint64*); |
| 7698 | *n = result.size(); |
| 7699 | break; |
| 7700 | } |
| 7701 | default: { |
| 7702 | int *n = va_arg(ap, int*); |
| 7703 | *n = int(result.size()); |
| 7704 | break; |
| 7705 | } |
| 7706 | } |
| 7707 | ++c; |
| 7708 | break; |
| 7709 | |
| 7710 | default: // bad escape, treat as non-escape text |
| 7711 | for (const char *cc = escape_start; cc != c; ++cc) |
| 7712 | result.append(ch: QLatin1Char(*cc)); |
| 7713 | continue; |
| 7714 | } |
| 7715 | |
| 7716 | if (flags & QLocaleData::LeftAdjusted) |
| 7717 | result.append(str: subst.leftJustified(width)); |
| 7718 | else |
| 7719 | result.append(str: subst.rightJustified(width)); |
| 7720 | } |
| 7721 | |
| 7722 | return result; |
| 7723 | } |
| 7724 | |
| 7725 | /*! |
| 7726 | \fn QString::toLongLong(bool *ok, int base) const |
| 7727 | |
| 7728 | Returns the string converted to a \c{long long} using base \a |
| 7729 | base, which is 10 by default and must be between 2 and 36, or 0. |
| 7730 | Returns 0 if the conversion fails. |
| 7731 | |
| 7732 | If \a ok is not \nullptr, failure is reported by setting *\a{ok} |
| 7733 | to \c false, and success by setting *\a{ok} to \c true. |
| 7734 | |
| 7735 | If \a base is 0, the C language convention is used: if the string begins |
| 7736 | with "0x", base 16 is used; otherwise, if the string begins with "0b", base |
| 7737 | 2 is used; otherwise, if the string begins with "0", base 8 is used; |
| 7738 | otherwise, base 10 is used. |
| 7739 | |
| 7740 | The string conversion will always happen in the 'C' locale. For |
| 7741 | locale-dependent conversion use QLocale::toLongLong() |
| 7742 | |
| 7743 | Example: |
| 7744 | |
| 7745 | \snippet qstring/main.cpp 74 |
| 7746 | |
| 7747 | This function ignores leading and trailing whitespace. |
| 7748 | |
| 7749 | \note Support for the "0b" prefix was added in Qt 6.4. |
| 7750 | |
| 7751 | \sa number(), toULongLong(), toInt(), QLocale::toLongLong() |
| 7752 | */ |
| 7753 | |
| 7754 | template <typename Int> |
| 7755 | static Int toIntegral(QStringView string, bool *ok, int base) |
| 7756 | { |
| 7757 | #if defined(QT_CHECK_RANGE) |
| 7758 | if (base != 0 && (base < 2 || base > 36)) { |
| 7759 | qWarning("QString::toIntegral: Invalid base (%d)" , base); |
| 7760 | base = 10; |
| 7761 | } |
| 7762 | #endif |
| 7763 | |
| 7764 | QVarLengthArray<uchar> latin1(string.size()); |
| 7765 | qt_to_latin1(dst: latin1.data(), src: string.utf16(), length: string.size()); |
| 7766 | QSimpleParsedNumber<Int> r; |
| 7767 | if constexpr (std::is_signed_v<Int>) |
| 7768 | r = QLocaleData::bytearrayToLongLong(num: latin1, base); |
| 7769 | else |
| 7770 | r = QLocaleData::bytearrayToUnsLongLong(num: latin1, base); |
| 7771 | if (ok) |
| 7772 | *ok = r.ok(); |
| 7773 | return r.result; |
| 7774 | } |
| 7775 | |
| 7776 | qlonglong QString::toIntegral_helper(QStringView string, bool *ok, int base) |
| 7777 | { |
| 7778 | return toIntegral<qlonglong>(string, ok, base); |
| 7779 | } |
| 7780 | |
| 7781 | /*! |
| 7782 | \fn QString::toULongLong(bool *ok, int base) const |
| 7783 | |
| 7784 | Returns the string converted to an \c{unsigned long long} using base \a |
| 7785 | base, which is 10 by default and must be between 2 and 36, or 0. |
| 7786 | Returns 0 if the conversion fails. |
| 7787 | |
| 7788 | If \a ok is not \nullptr, failure is reported by setting *\a{ok} |
| 7789 | to \c false, and success by setting *\a{ok} to \c true. |
| 7790 | |
| 7791 | If \a base is 0, the C language convention is used: if the string begins |
| 7792 | with "0x", base 16 is used; otherwise, if the string begins with "0b", base |
| 7793 | 2 is used; otherwise, if the string begins with "0", base 8 is used; |
| 7794 | otherwise, base 10 is used. |
| 7795 | |
| 7796 | The string conversion will always happen in the 'C' locale. For |
| 7797 | locale-dependent conversion use QLocale::toULongLong() |
| 7798 | |
| 7799 | Example: |
| 7800 | |
| 7801 | \snippet qstring/main.cpp 79 |
| 7802 | |
| 7803 | This function ignores leading and trailing whitespace. |
| 7804 | |
| 7805 | \note Support for the "0b" prefix was added in Qt 6.4. |
| 7806 | |
| 7807 | \sa number(), toLongLong(), QLocale::toULongLong() |
| 7808 | */ |
| 7809 | |
| 7810 | qulonglong QString::toIntegral_helper(QStringView string, bool *ok, uint base) |
| 7811 | { |
| 7812 | return toIntegral<qulonglong>(string, ok, base); |
| 7813 | } |
| 7814 | |
| 7815 | /*! |
| 7816 | \fn long QString::toLong(bool *ok, int base) const |
| 7817 | |
| 7818 | Returns the string converted to a \c long using base \a |
| 7819 | base, which is 10 by default and must be between 2 and 36, or 0. |
| 7820 | Returns 0 if the conversion fails. |
| 7821 | |
| 7822 | If \a ok is not \nullptr, failure is reported by setting *\a{ok} |
| 7823 | to \c false, and success by setting *\a{ok} to \c true. |
| 7824 | |
| 7825 | If \a base is 0, the C language convention is used: if the string begins |
| 7826 | with "0x", base 16 is used; otherwise, if the string begins with "0b", base |
| 7827 | 2 is used; otherwise, if the string begins with "0", base 8 is used; |
| 7828 | otherwise, base 10 is used. |
| 7829 | |
| 7830 | The string conversion will always happen in the 'C' locale. For |
| 7831 | locale-dependent conversion use QLocale::toLongLong() |
| 7832 | |
| 7833 | Example: |
| 7834 | |
| 7835 | \snippet qstring/main.cpp 73 |
| 7836 | |
| 7837 | This function ignores leading and trailing whitespace. |
| 7838 | |
| 7839 | \note Support for the "0b" prefix was added in Qt 6.4. |
| 7840 | |
| 7841 | \sa number(), toULong(), toInt(), QLocale::toInt() |
| 7842 | */ |
| 7843 | |
| 7844 | /*! |
| 7845 | \fn ulong QString::toULong(bool *ok, int base) const |
| 7846 | |
| 7847 | Returns the string converted to an \c{unsigned long} using base \a |
| 7848 | base, which is 10 by default and must be between 2 and 36, or 0. |
| 7849 | Returns 0 if the conversion fails. |
| 7850 | |
| 7851 | If \a ok is not \nullptr, failure is reported by setting *\a{ok} |
| 7852 | to \c false, and success by setting *\a{ok} to \c true. |
| 7853 | |
| 7854 | If \a base is 0, the C language convention is used: if the string begins |
| 7855 | with "0x", base 16 is used; otherwise, if the string begins with "0b", base |
| 7856 | 2 is used; otherwise, if the string begins with "0", base 8 is used; |
| 7857 | otherwise, base 10 is used. |
| 7858 | |
| 7859 | The string conversion will always happen in the 'C' locale. For |
| 7860 | locale-dependent conversion use QLocale::toULongLong() |
| 7861 | |
| 7862 | Example: |
| 7863 | |
| 7864 | \snippet qstring/main.cpp 78 |
| 7865 | |
| 7866 | This function ignores leading and trailing whitespace. |
| 7867 | |
| 7868 | \note Support for the "0b" prefix was added in Qt 6.4. |
| 7869 | |
| 7870 | \sa number(), QLocale::toUInt() |
| 7871 | */ |
| 7872 | |
| 7873 | /*! |
| 7874 | \fn int QString::toInt(bool *ok, int base) const |
| 7875 | Returns the string converted to an \c int using base \a |
| 7876 | base, which is 10 by default and must be between 2 and 36, or 0. |
| 7877 | Returns 0 if the conversion fails. |
| 7878 | |
| 7879 | If \a ok is not \nullptr, failure is reported by setting *\a{ok} |
| 7880 | to \c false, and success by setting *\a{ok} to \c true. |
| 7881 | |
| 7882 | If \a base is 0, the C language convention is used: if the string begins |
| 7883 | with "0x", base 16 is used; otherwise, if the string begins with "0b", base |
| 7884 | 2 is used; otherwise, if the string begins with "0", base 8 is used; |
| 7885 | otherwise, base 10 is used. |
| 7886 | |
| 7887 | The string conversion will always happen in the 'C' locale. For |
| 7888 | locale-dependent conversion use QLocale::toInt() |
| 7889 | |
| 7890 | Example: |
| 7891 | |
| 7892 | \snippet qstring/main.cpp 72 |
| 7893 | |
| 7894 | This function ignores leading and trailing whitespace. |
| 7895 | |
| 7896 | \note Support for the "0b" prefix was added in Qt 6.4. |
| 7897 | |
| 7898 | \sa number(), toUInt(), toDouble(), QLocale::toInt() |
| 7899 | */ |
| 7900 | |
| 7901 | /*! |
| 7902 | \fn uint QString::toUInt(bool *ok, int base) const |
| 7903 | Returns the string converted to an \c{unsigned int} using base \a |
| 7904 | base, which is 10 by default and must be between 2 and 36, or 0. |
| 7905 | Returns 0 if the conversion fails. |
| 7906 | |
| 7907 | If \a ok is not \nullptr, failure is reported by setting *\a{ok} |
| 7908 | to \c false, and success by setting *\a{ok} to \c true. |
| 7909 | |
| 7910 | If \a base is 0, the C language convention is used: if the string begins |
| 7911 | with "0x", base 16 is used; otherwise, if the string begins with "0b", base |
| 7912 | 2 is used; otherwise, if the string begins with "0", base 8 is used; |
| 7913 | otherwise, base 10 is used. |
| 7914 | |
| 7915 | The string conversion will always happen in the 'C' locale. For |
| 7916 | locale-dependent conversion use QLocale::toUInt() |
| 7917 | |
| 7918 | Example: |
| 7919 | |
| 7920 | \snippet qstring/main.cpp 77 |
| 7921 | |
| 7922 | This function ignores leading and trailing whitespace. |
| 7923 | |
| 7924 | \note Support for the "0b" prefix was added in Qt 6.4. |
| 7925 | |
| 7926 | \sa number(), toInt(), QLocale::toUInt() |
| 7927 | */ |
| 7928 | |
| 7929 | /*! |
| 7930 | \fn short QString::toShort(bool *ok, int base) const |
| 7931 | |
| 7932 | Returns the string converted to a \c short using base \a |
| 7933 | base, which is 10 by default and must be between 2 and 36, or 0. |
| 7934 | Returns 0 if the conversion fails. |
| 7935 | |
| 7936 | If \a ok is not \nullptr, failure is reported by setting *\a{ok} |
| 7937 | to \c false, and success by setting *\a{ok} to \c true. |
| 7938 | |
| 7939 | If \a base is 0, the C language convention is used: if the string begins |
| 7940 | with "0x", base 16 is used; otherwise, if the string begins with "0b", base |
| 7941 | 2 is used; otherwise, if the string begins with "0", base 8 is used; |
| 7942 | otherwise, base 10 is used. |
| 7943 | |
| 7944 | The string conversion will always happen in the 'C' locale. For |
| 7945 | locale-dependent conversion use QLocale::toShort() |
| 7946 | |
| 7947 | Example: |
| 7948 | |
| 7949 | \snippet qstring/main.cpp 76 |
| 7950 | |
| 7951 | This function ignores leading and trailing whitespace. |
| 7952 | |
| 7953 | \note Support for the "0b" prefix was added in Qt 6.4. |
| 7954 | |
| 7955 | \sa number(), toUShort(), toInt(), QLocale::toShort() |
| 7956 | */ |
| 7957 | |
| 7958 | /*! |
| 7959 | \fn ushort QString::toUShort(bool *ok, int base) const |
| 7960 | |
| 7961 | Returns the string converted to an \c{unsigned short} using base \a |
| 7962 | base, which is 10 by default and must be between 2 and 36, or 0. |
| 7963 | Returns 0 if the conversion fails. |
| 7964 | |
| 7965 | If \a ok is not \nullptr, failure is reported by setting *\a{ok} |
| 7966 | to \c false, and success by setting *\a{ok} to \c true. |
| 7967 | |
| 7968 | If \a base is 0, the C language convention is used: if the string begins |
| 7969 | with "0x", base 16 is used; otherwise, if the string begins with "0b", base |
| 7970 | 2 is used; otherwise, if the string begins with "0", base 8 is used; |
| 7971 | otherwise, base 10 is used. |
| 7972 | |
| 7973 | The string conversion will always happen in the 'C' locale. For |
| 7974 | locale-dependent conversion use QLocale::toUShort() |
| 7975 | |
| 7976 | Example: |
| 7977 | |
| 7978 | \snippet qstring/main.cpp 80 |
| 7979 | |
| 7980 | This function ignores leading and trailing whitespace. |
| 7981 | |
| 7982 | \note Support for the "0b" prefix was added in Qt 6.4. |
| 7983 | |
| 7984 | \sa number(), toShort(), QLocale::toUShort() |
| 7985 | */ |
| 7986 | |
| 7987 | /*! |
| 7988 | Returns the string converted to a \c double value. |
| 7989 | |
| 7990 | Returns an infinity if the conversion overflows or 0.0 if the |
| 7991 | conversion fails for other reasons (e.g. underflow). |
| 7992 | |
| 7993 | If \a ok is not \nullptr, failure is reported by setting *\a{ok} |
| 7994 | to \c false, and success by setting *\a{ok} to \c true. |
| 7995 | |
| 7996 | \snippet qstring/main.cpp 66 |
| 7997 | |
| 7998 | \warning The QString content may only contain valid numerical characters |
| 7999 | which includes the plus/minus sign, the character e used in scientific |
| 8000 | notation, and the decimal point. Including the unit or additional characters |
| 8001 | leads to a conversion error. |
| 8002 | |
| 8003 | \snippet qstring/main.cpp 67 |
| 8004 | |
| 8005 | The string conversion will always happen in the 'C' locale. For |
| 8006 | locale-dependent conversion use QLocale::toDouble() |
| 8007 | |
| 8008 | \snippet qstring/main.cpp 68 |
| 8009 | |
| 8010 | For historical reasons, this function does not handle |
| 8011 | thousands group separators. If you need to convert such numbers, |
| 8012 | use QLocale::toDouble(). |
| 8013 | |
| 8014 | \snippet qstring/main.cpp 69 |
| 8015 | |
| 8016 | This function ignores leading and trailing whitespace. |
| 8017 | |
| 8018 | \sa number(), QLocale::setDefault(), QLocale::toDouble(), trimmed() |
| 8019 | */ |
| 8020 | |
| 8021 | double QString::toDouble(bool *ok) const |
| 8022 | { |
| 8023 | return QStringView(*this).toDouble(ok); |
| 8024 | } |
| 8025 | |
| 8026 | double QStringView::toDouble(bool *ok) const |
| 8027 | { |
| 8028 | QStringView string = qt_trimmed(s: *this); |
| 8029 | QVarLengthArray<uchar> latin1(string.size()); |
| 8030 | qt_to_latin1(dst: latin1.data(), src: string.utf16(), length: string.size()); |
| 8031 | auto r = qt_asciiToDouble(num: reinterpret_cast<const char *>(latin1.data()), numLen: string.size()); |
| 8032 | if (ok != nullptr) |
| 8033 | *ok = r.ok(); |
| 8034 | return r.result; |
| 8035 | } |
| 8036 | |
| 8037 | /*! |
| 8038 | Returns the string converted to a \c float value. |
| 8039 | |
| 8040 | Returns an infinity if the conversion overflows or 0.0 if the |
| 8041 | conversion fails for other reasons (e.g. underflow). |
| 8042 | |
| 8043 | If \a ok is not \nullptr, failure is reported by setting *\a{ok} |
| 8044 | to \c false, and success by setting *\a{ok} to \c true. |
| 8045 | |
| 8046 | \warning The QString content may only contain valid numerical characters |
| 8047 | which includes the plus/minus sign, the character e used in scientific |
| 8048 | notation, and the decimal point. Including the unit or additional characters |
| 8049 | leads to a conversion error. |
| 8050 | |
| 8051 | The string conversion will always happen in the 'C' locale. For |
| 8052 | locale-dependent conversion use QLocale::toFloat() |
| 8053 | |
| 8054 | For historical reasons, this function does not handle |
| 8055 | thousands group separators. If you need to convert such numbers, |
| 8056 | use QLocale::toFloat(). |
| 8057 | |
| 8058 | Example: |
| 8059 | |
| 8060 | \snippet qstring/main.cpp 71 |
| 8061 | |
| 8062 | This function ignores leading and trailing whitespace. |
| 8063 | |
| 8064 | \sa number(), toDouble(), toInt(), QLocale::toFloat(), trimmed() |
| 8065 | */ |
| 8066 | |
| 8067 | float QString::toFloat(bool *ok) const |
| 8068 | { |
| 8069 | return QLocaleData::convertDoubleToFloat(d: toDouble(ok), ok); |
| 8070 | } |
| 8071 | |
| 8072 | float QStringView::toFloat(bool *ok) const |
| 8073 | { |
| 8074 | return QLocaleData::convertDoubleToFloat(d: toDouble(ok), ok); |
| 8075 | } |
| 8076 | |
| 8077 | /*! \fn QString &QString::setNum(int n, int base) |
| 8078 | |
| 8079 | Sets the string to the printed value of \a n in the specified \a |
| 8080 | base, and returns a reference to the string. |
| 8081 | |
| 8082 | The base is 10 by default and must be between 2 and 36. |
| 8083 | |
| 8084 | \snippet qstring/main.cpp 56 |
| 8085 | |
| 8086 | The formatting always uses QLocale::C, i.e., English/UnitedStates. |
| 8087 | To get a localized string representation of a number, use |
| 8088 | QLocale::toString() with the appropriate locale. |
| 8089 | |
| 8090 | \sa number() |
| 8091 | */ |
| 8092 | |
| 8093 | /*! \fn QString &QString::setNum(uint n, int base) |
| 8094 | |
| 8095 | \overload |
| 8096 | */ |
| 8097 | |
| 8098 | /*! \fn QString &QString::setNum(long n, int base) |
| 8099 | |
| 8100 | \overload |
| 8101 | */ |
| 8102 | |
| 8103 | /*! \fn QString &QString::setNum(ulong n, int base) |
| 8104 | |
| 8105 | \overload |
| 8106 | */ |
| 8107 | |
| 8108 | /*! |
| 8109 | \overload |
| 8110 | */ |
| 8111 | QString &QString::setNum(qlonglong n, int base) |
| 8112 | { |
| 8113 | return *this = number(n, base); |
| 8114 | } |
| 8115 | |
| 8116 | /*! |
| 8117 | \overload |
| 8118 | */ |
| 8119 | QString &QString::setNum(qulonglong n, int base) |
| 8120 | { |
| 8121 | return *this = number(n, base); |
| 8122 | } |
| 8123 | |
| 8124 | /*! \fn QString &QString::setNum(short n, int base) |
| 8125 | |
| 8126 | \overload |
| 8127 | */ |
| 8128 | |
| 8129 | /*! \fn QString &QString::setNum(ushort n, int base) |
| 8130 | |
| 8131 | \overload |
| 8132 | */ |
| 8133 | |
| 8134 | /*! |
| 8135 | \overload |
| 8136 | |
| 8137 | Sets the string to the printed value of \a n, formatted according to the |
| 8138 | given \a format and \a precision, and returns a reference to the string. |
| 8139 | |
| 8140 | \sa number(), QLocale::FloatingPointPrecisionOption, {Number Formats} |
| 8141 | */ |
| 8142 | |
| 8143 | QString &QString::setNum(double n, char format, int precision) |
| 8144 | { |
| 8145 | return *this = number(n, format, precision); |
| 8146 | } |
| 8147 | |
| 8148 | /*! |
| 8149 | \fn QString &QString::setNum(float n, char format, int precision) |
| 8150 | \overload |
| 8151 | |
| 8152 | Sets the string to the printed value of \a n, formatted according |
| 8153 | to the given \a format and \a precision, and returns a reference |
| 8154 | to the string. |
| 8155 | |
| 8156 | The formatting always uses QLocale::C, i.e., English/UnitedStates. |
| 8157 | To get a localized string representation of a number, use |
| 8158 | QLocale::toString() with the appropriate locale. |
| 8159 | |
| 8160 | \sa number() |
| 8161 | */ |
| 8162 | |
| 8163 | |
| 8164 | /*! |
| 8165 | \fn QString QString::number(long n, int base) |
| 8166 | |
| 8167 | Returns a string equivalent of the number \a n according to the |
| 8168 | specified \a base. |
| 8169 | |
| 8170 | The base is 10 by default and must be between 2 |
| 8171 | and 36. For bases other than 10, \a n is treated as an |
| 8172 | unsigned integer. |
| 8173 | |
| 8174 | The formatting always uses QLocale::C, i.e., English/UnitedStates. |
| 8175 | To get a localized string representation of a number, use |
| 8176 | QLocale::toString() with the appropriate locale. |
| 8177 | |
| 8178 | \snippet qstring/main.cpp 35 |
| 8179 | |
| 8180 | \sa setNum() |
| 8181 | */ |
| 8182 | |
| 8183 | QString QString::number(long n, int base) |
| 8184 | { |
| 8185 | return number(qlonglong(n), base); |
| 8186 | } |
| 8187 | |
| 8188 | /*! |
| 8189 | \fn QString QString::number(ulong n, int base) |
| 8190 | |
| 8191 | \overload |
| 8192 | */ |
| 8193 | QString QString::number(ulong n, int base) |
| 8194 | { |
| 8195 | return number(qulonglong(n), base); |
| 8196 | } |
| 8197 | |
| 8198 | /*! |
| 8199 | \overload |
| 8200 | */ |
| 8201 | QString QString::number(int n, int base) |
| 8202 | { |
| 8203 | return number(qlonglong(n), base); |
| 8204 | } |
| 8205 | |
| 8206 | /*! |
| 8207 | \overload |
| 8208 | */ |
| 8209 | QString QString::number(uint n, int base) |
| 8210 | { |
| 8211 | return number(qulonglong(n), base); |
| 8212 | } |
| 8213 | |
| 8214 | /*! |
| 8215 | \overload |
| 8216 | */ |
| 8217 | QString QString::number(qlonglong n, int base) |
| 8218 | { |
| 8219 | #if defined(QT_CHECK_RANGE) |
| 8220 | if (base < 2 || base > 36) { |
| 8221 | qWarning("QString::setNum: Invalid base (%d)" , base); |
| 8222 | base = 10; |
| 8223 | } |
| 8224 | #endif |
| 8225 | bool negative = n < 0; |
| 8226 | /* |
| 8227 | Negating std::numeric_limits<qlonglong>::min() hits undefined behavior, so |
| 8228 | taking an absolute value has to take a slight detour. |
| 8229 | */ |
| 8230 | return qulltoBasicLatin(l: negative ? 1u + qulonglong(-(n + 1)) : qulonglong(n), base, negative); |
| 8231 | } |
| 8232 | |
| 8233 | /*! |
| 8234 | \overload |
| 8235 | */ |
| 8236 | QString QString::number(qulonglong n, int base) |
| 8237 | { |
| 8238 | #if defined(QT_CHECK_RANGE) |
| 8239 | if (base < 2 || base > 36) { |
| 8240 | qWarning("QString::setNum: Invalid base (%d)" , base); |
| 8241 | base = 10; |
| 8242 | } |
| 8243 | #endif |
| 8244 | return qulltoBasicLatin(l: n, base, negative: false); |
| 8245 | } |
| 8246 | |
| 8247 | |
| 8248 | /*! |
| 8249 | Returns a string representing the floating-point number \a n. |
| 8250 | |
| 8251 | Returns a string that represents \a n, formatted according to the specified |
| 8252 | \a format and \a precision. |
| 8253 | |
| 8254 | For formats with an exponent, the exponent will show its sign and have at |
| 8255 | least two digits, left-padding the exponent with zero if needed. |
| 8256 | |
| 8257 | \sa setNum(), QLocale::toString(), QLocale::FloatingPointPrecisionOption, {Number Formats} |
| 8258 | */ |
| 8259 | QString QString::number(double n, char format, int precision) |
| 8260 | { |
| 8261 | QLocaleData::DoubleForm form = QLocaleData::DFDecimal; |
| 8262 | |
| 8263 | switch (QtMiscUtils::toAsciiLower(ch: format)) { |
| 8264 | case 'f': |
| 8265 | form = QLocaleData::DFDecimal; |
| 8266 | break; |
| 8267 | case 'e': |
| 8268 | form = QLocaleData::DFExponent; |
| 8269 | break; |
| 8270 | case 'g': |
| 8271 | form = QLocaleData::DFSignificantDigits; |
| 8272 | break; |
| 8273 | default: |
| 8274 | #if defined(QT_CHECK_RANGE) |
| 8275 | qWarning("QString::setNum: Invalid format char '%c'" , format); |
| 8276 | #endif |
| 8277 | break; |
| 8278 | } |
| 8279 | |
| 8280 | return qdtoBasicLatin(d: n, form, precision, uppercase: isAsciiUpper(c: format)); |
| 8281 | } |
| 8282 | |
| 8283 | namespace { |
| 8284 | template<class ResultList, class StringSource> |
| 8285 | static ResultList splitString(const StringSource &source, QStringView sep, |
| 8286 | Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) |
| 8287 | { |
| 8288 | ResultList list; |
| 8289 | typename StringSource::size_type start = 0; |
| 8290 | typename StringSource::size_type end; |
| 8291 | typename StringSource::size_type = 0; |
| 8292 | while ((end = QtPrivate::findString(QStringView(source.constData(), source.size()), start + extra, sep, cs)) != -1) { |
| 8293 | if (start != end || behavior == Qt::KeepEmptyParts) |
| 8294 | list.append(source.sliced(start, end - start)); |
| 8295 | start = end + sep.size(); |
| 8296 | extra = (sep.size() == 0 ? 1 : 0); |
| 8297 | } |
| 8298 | if (start != source.size() || behavior == Qt::KeepEmptyParts) |
| 8299 | list.append(source.sliced(start)); |
| 8300 | return list; |
| 8301 | } |
| 8302 | |
| 8303 | } // namespace |
| 8304 | |
| 8305 | /*! |
| 8306 | Splits the string into substrings wherever \a sep occurs, and |
| 8307 | returns the list of those strings. If \a sep does not match |
| 8308 | anywhere in the string, split() returns a single-element list |
| 8309 | containing this string. |
| 8310 | |
| 8311 | \a cs specifies whether \a sep should be matched case |
| 8312 | sensitively or case insensitively. |
| 8313 | |
| 8314 | If \a behavior is Qt::SkipEmptyParts, empty entries don't |
| 8315 | appear in the result. By default, empty entries are kept. |
| 8316 | |
| 8317 | Example: |
| 8318 | |
| 8319 | \snippet qstring/main.cpp 62 |
| 8320 | |
| 8321 | If \a sep is empty, split() returns an empty string, followed |
| 8322 | by each of the string's characters, followed by another empty string: |
| 8323 | |
| 8324 | \snippet qstring/main.cpp 62-empty |
| 8325 | |
| 8326 | To understand this behavior, recall that the empty string matches |
| 8327 | everywhere, so the above is qualitatively the same as: |
| 8328 | |
| 8329 | \snippet qstring/main.cpp 62-slashes |
| 8330 | |
| 8331 | \sa QStringList::join(), section() |
| 8332 | |
| 8333 | \since 5.14 |
| 8334 | */ |
| 8335 | QStringList QString::split(const QString &sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const |
| 8336 | { |
| 8337 | return splitString<QStringList>(source: *this, sep, behavior, cs); |
| 8338 | } |
| 8339 | |
| 8340 | /*! |
| 8341 | \overload |
| 8342 | \since 5.14 |
| 8343 | */ |
| 8344 | QStringList QString::split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const |
| 8345 | { |
| 8346 | return splitString<QStringList>(source: *this, sep: QStringView(&sep, 1), behavior, cs); |
| 8347 | } |
| 8348 | |
| 8349 | /*! |
| 8350 | \fn QList<QStringView> QStringView::split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const |
| 8351 | \fn QList<QStringView> QStringView::split(QStringView sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const |
| 8352 | |
| 8353 | |
| 8354 | Splits the view into substring views wherever \a sep occurs, and |
| 8355 | returns the list of those string views. |
| 8356 | |
| 8357 | See QString::split() for how \a sep, \a behavior and \a cs interact to form |
| 8358 | the result. |
| 8359 | |
| 8360 | \note All the returned views are valid as long as the data referenced by |
| 8361 | this string view is valid. Destroying the data will cause all views to |
| 8362 | become dangling. |
| 8363 | |
| 8364 | \since 6.0 |
| 8365 | */ |
| 8366 | QList<QStringView> QStringView::split(QStringView sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const |
| 8367 | { |
| 8368 | return splitString<QList<QStringView>>(source: QStringView(*this), sep, behavior, cs); |
| 8369 | } |
| 8370 | |
| 8371 | QList<QStringView> QStringView::split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const |
| 8372 | { |
| 8373 | return split(sep: QStringView(&sep, 1), behavior, cs); |
| 8374 | } |
| 8375 | |
| 8376 | #if QT_CONFIG(regularexpression) |
| 8377 | namespace { |
| 8378 | template<class ResultList, typename String, typename MatchingFunction> |
| 8379 | static ResultList splitString(const String &source, const QRegularExpression &re, |
| 8380 | MatchingFunction matchingFunction, |
| 8381 | Qt::SplitBehavior behavior) |
| 8382 | { |
| 8383 | ResultList list; |
| 8384 | if (!re.isValid()) { |
| 8385 | qtWarnAboutInvalidRegularExpression(re, cls: "QString" , method: "split" ); |
| 8386 | return list; |
| 8387 | } |
| 8388 | |
| 8389 | qsizetype start = 0; |
| 8390 | qsizetype end = 0; |
| 8391 | QRegularExpressionMatchIterator iterator = (re.*matchingFunction)(source, 0, QRegularExpression::NormalMatch, QRegularExpression::NoMatchOption); |
| 8392 | while (iterator.hasNext()) { |
| 8393 | QRegularExpressionMatch match = iterator.next(); |
| 8394 | end = match.capturedStart(); |
| 8395 | if (start != end || behavior == Qt::KeepEmptyParts) |
| 8396 | list.append(source.sliced(start, end - start)); |
| 8397 | start = match.capturedEnd(); |
| 8398 | } |
| 8399 | |
| 8400 | if (start != source.size() || behavior == Qt::KeepEmptyParts) |
| 8401 | list.append(source.sliced(start)); |
| 8402 | |
| 8403 | return list; |
| 8404 | } |
| 8405 | } // namespace |
| 8406 | |
| 8407 | /*! |
| 8408 | \overload |
| 8409 | \since 5.14 |
| 8410 | |
| 8411 | Splits the string into substrings wherever the regular expression |
| 8412 | \a re matches, and returns the list of those strings. If \a re |
| 8413 | does not match anywhere in the string, split() returns a |
| 8414 | single-element list containing this string. |
| 8415 | |
| 8416 | Here is an example where we extract the words in a sentence |
| 8417 | using one or more whitespace characters as the separator: |
| 8418 | |
| 8419 | \snippet qstring/main.cpp 90 |
| 8420 | |
| 8421 | Here is a similar example, but this time we use any sequence of |
| 8422 | non-word characters as the separator: |
| 8423 | |
| 8424 | \snippet qstring/main.cpp 91 |
| 8425 | |
| 8426 | Here is a third example where we use a zero-length assertion, |
| 8427 | \b{\\b} (word boundary), to split the string into an |
| 8428 | alternating sequence of non-word and word tokens: |
| 8429 | |
| 8430 | \snippet qstring/main.cpp 92 |
| 8431 | |
| 8432 | \sa QStringList::join(), section() |
| 8433 | */ |
| 8434 | QStringList QString::split(const QRegularExpression &re, Qt::SplitBehavior behavior) const |
| 8435 | { |
| 8436 | #if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) |
| 8437 | const auto matchingFunction = qOverload<const QString &, qsizetype, QRegularExpression::MatchType, QRegularExpression::MatchOptions>(&QRegularExpression::globalMatch); |
| 8438 | #else |
| 8439 | const auto matchingFunction = &QRegularExpression::globalMatch; |
| 8440 | #endif |
| 8441 | return splitString<QStringList>(source: *this, |
| 8442 | re, |
| 8443 | matchingFunction, |
| 8444 | behavior); |
| 8445 | } |
| 8446 | |
| 8447 | /*! |
| 8448 | \overload |
| 8449 | \since 6.0 |
| 8450 | |
| 8451 | Splits the string into substring views wherever the regular expression \a re |
| 8452 | matches, and returns the list of those strings. If \a re does not match |
| 8453 | anywhere in the string, split() returns a single-element list containing |
| 8454 | this string as view. |
| 8455 | |
| 8456 | \note The views in the returned list are sub-views of this view; as such, |
| 8457 | they reference the same data as it and only remain valid for as long as that |
| 8458 | data remains live. |
| 8459 | */ |
| 8460 | QList<QStringView> QStringView::split(const QRegularExpression &re, Qt::SplitBehavior behavior) const |
| 8461 | { |
| 8462 | return splitString<QList<QStringView>>(source: *this, re, matchingFunction: &QRegularExpression::globalMatchView, behavior); |
| 8463 | } |
| 8464 | |
| 8465 | #endif // QT_CONFIG(regularexpression) |
| 8466 | |
| 8467 | /*! |
| 8468 | \enum QString::NormalizationForm |
| 8469 | |
| 8470 | This enum describes the various normalized forms of Unicode text. |
| 8471 | |
| 8472 | \value NormalizationForm_D Canonical Decomposition |
| 8473 | \value NormalizationForm_C Canonical Decomposition followed by Canonical Composition |
| 8474 | \value NormalizationForm_KD Compatibility Decomposition |
| 8475 | \value NormalizationForm_KC Compatibility Decomposition followed by Canonical Composition |
| 8476 | |
| 8477 | \sa normalized(), |
| 8478 | {https://www.unicode.org/reports/tr15/}{Unicode Standard Annex #15} |
| 8479 | */ |
| 8480 | |
| 8481 | /*! |
| 8482 | \since 4.5 |
| 8483 | |
| 8484 | Returns a copy of this string repeated the specified number of \a times. |
| 8485 | |
| 8486 | If \a times is less than 1, an empty string is returned. |
| 8487 | |
| 8488 | Example: |
| 8489 | |
| 8490 | \snippet code/src_corelib_text_qstring.cpp 8 |
| 8491 | */ |
| 8492 | QString QString::repeated(qsizetype times) const |
| 8493 | { |
| 8494 | if (d.size == 0) |
| 8495 | return *this; |
| 8496 | |
| 8497 | if (times <= 1) { |
| 8498 | if (times == 1) |
| 8499 | return *this; |
| 8500 | return QString(); |
| 8501 | } |
| 8502 | |
| 8503 | const qsizetype resultSize = times * d.size; |
| 8504 | |
| 8505 | QString result; |
| 8506 | result.reserve(asize: resultSize); |
| 8507 | if (result.capacity() != resultSize) |
| 8508 | return QString(); // not enough memory |
| 8509 | |
| 8510 | memcpy(dest: result.d.data(), src: d.data(), n: d.size * sizeof(QChar)); |
| 8511 | |
| 8512 | qsizetype sizeSoFar = d.size; |
| 8513 | char16_t *end = result.d.data() + sizeSoFar; |
| 8514 | |
| 8515 | const qsizetype halfResultSize = resultSize >> 1; |
| 8516 | while (sizeSoFar <= halfResultSize) { |
| 8517 | memcpy(dest: end, src: result.d.data(), n: sizeSoFar * sizeof(QChar)); |
| 8518 | end += sizeSoFar; |
| 8519 | sizeSoFar <<= 1; |
| 8520 | } |
| 8521 | memcpy(dest: end, src: result.d.data(), n: (resultSize - sizeSoFar) * sizeof(QChar)); |
| 8522 | result.d.data()[resultSize] = '\0'; |
| 8523 | result.d.size = resultSize; |
| 8524 | return result; |
| 8525 | } |
| 8526 | |
| 8527 | void qt_string_normalize(QString *data, QString::NormalizationForm mode, QChar::UnicodeVersion version, qsizetype from) |
| 8528 | { |
| 8529 | { |
| 8530 | // check if it's fully ASCII first, because then we have no work |
| 8531 | auto start = reinterpret_cast<const char16_t *>(data->constData()); |
| 8532 | const char16_t *p = start + from; |
| 8533 | if (isAscii_helper(ptr&: p, end: p + data->size() - from)) |
| 8534 | return; |
| 8535 | if (p > start + from) |
| 8536 | from = p - start - 1; // need one before the non-ASCII to perform NFC |
| 8537 | } |
| 8538 | |
| 8539 | if (version == QChar::Unicode_Unassigned) { |
| 8540 | version = QChar::currentUnicodeVersion(); |
| 8541 | } else if (int(version) <= NormalizationCorrectionsVersionMax) { |
| 8542 | const QString &s = *data; |
| 8543 | QChar *d = nullptr; |
| 8544 | for (const NormalizationCorrection &n : uc_normalization_corrections) { |
| 8545 | if (n.version > version) { |
| 8546 | qsizetype pos = from; |
| 8547 | if (QChar::requiresSurrogates(ucs4: n.ucs4)) { |
| 8548 | char16_t ucs4High = QChar::highSurrogate(ucs4: n.ucs4); |
| 8549 | char16_t ucs4Low = QChar::lowSurrogate(ucs4: n.ucs4); |
| 8550 | char16_t oldHigh = QChar::highSurrogate(ucs4: n.old_mapping); |
| 8551 | char16_t oldLow = QChar::lowSurrogate(ucs4: n.old_mapping); |
| 8552 | while (pos < s.size() - 1) { |
| 8553 | if (s.at(i: pos).unicode() == ucs4High && s.at(i: pos + 1).unicode() == ucs4Low) { |
| 8554 | if (!d) |
| 8555 | d = data->data(); |
| 8556 | d[pos] = QChar(oldHigh); |
| 8557 | d[++pos] = QChar(oldLow); |
| 8558 | } |
| 8559 | ++pos; |
| 8560 | } |
| 8561 | } else { |
| 8562 | while (pos < s.size()) { |
| 8563 | if (s.at(i: pos).unicode() == n.ucs4) { |
| 8564 | if (!d) |
| 8565 | d = data->data(); |
| 8566 | d[pos] = QChar(n.old_mapping); |
| 8567 | } |
| 8568 | ++pos; |
| 8569 | } |
| 8570 | } |
| 8571 | } |
| 8572 | } |
| 8573 | } |
| 8574 | |
| 8575 | if (normalizationQuickCheckHelper(str: data, mode, from, lastStable: &from)) |
| 8576 | return; |
| 8577 | |
| 8578 | decomposeHelper(str: data, canonical: mode < QString::NormalizationForm_KD, version, from); |
| 8579 | |
| 8580 | canonicalOrderHelper(str: data, version, from); |
| 8581 | |
| 8582 | if (mode == QString::NormalizationForm_D || mode == QString::NormalizationForm_KD) |
| 8583 | return; |
| 8584 | |
| 8585 | composeHelper(str: data, version, from); |
| 8586 | } |
| 8587 | |
| 8588 | /*! |
| 8589 | Returns the string in the given Unicode normalization \a mode, |
| 8590 | according to the given \a version of the Unicode standard. |
| 8591 | */ |
| 8592 | QString QString::normalized(QString::NormalizationForm mode, QChar::UnicodeVersion version) const |
| 8593 | { |
| 8594 | QString copy = *this; |
| 8595 | qt_string_normalize(data: ©, mode, version, from: 0); |
| 8596 | return copy; |
| 8597 | } |
| 8598 | |
| 8599 | #if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) && !defined(QT_BOOTSTRAPPED) |
| 8600 | static void checkArgEscape(QStringView s) |
| 8601 | { |
| 8602 | // If we're in here, it means that qArgDigitValue has accepted the |
| 8603 | // digit. We can skip the check in case we already know it will |
| 8604 | // succeed. |
| 8605 | if (!supportUnicodeDigitValuesInArg()) |
| 8606 | return; |
| 8607 | |
| 8608 | const auto isNonAsciiDigit = [](QChar c) { |
| 8609 | return c.unicode() < u'0' || c.unicode() > u'9'; |
| 8610 | }; |
| 8611 | |
| 8612 | if (std::any_of(first: s.begin(), last: s.end(), pred: isNonAsciiDigit)) { |
| 8613 | const auto accumulateDigit = [](int partial, QChar digit) { |
| 8614 | return partial * 10 + digit.digitValue(); |
| 8615 | }; |
| 8616 | const int parsedNumber = std::accumulate(first: s.begin(), last: s.end(), init: 0, binary_op: accumulateDigit); |
| 8617 | |
| 8618 | qWarning(msg: "QString::arg(): the replacement \"%%%ls\" contains non-ASCII digits;\n" |
| 8619 | " it is currently being interpreted as the %d-th substitution.\n" |
| 8620 | " This is deprecated; support for non-ASCII digits will be dropped\n" |
| 8621 | " in a future version of Qt." , |
| 8622 | qUtf16Printable(s.toString()), |
| 8623 | parsedNumber); |
| 8624 | } |
| 8625 | } |
| 8626 | #endif |
| 8627 | |
| 8628 | struct ArgEscapeData |
| 8629 | { |
| 8630 | int min_escape; // lowest escape sequence number |
| 8631 | qsizetype occurrences; // number of occurrences of the lowest escape sequence number |
| 8632 | qsizetype locale_occurrences; // number of occurrences of the lowest escape sequence number that |
| 8633 | // contain 'L' |
| 8634 | qsizetype escape_len; // total length of escape sequences which will be replaced |
| 8635 | }; |
| 8636 | |
| 8637 | static ArgEscapeData findArgEscapes(QStringView s) |
| 8638 | { |
| 8639 | const QChar *uc_begin = s.begin(); |
| 8640 | const QChar *uc_end = s.end(); |
| 8641 | |
| 8642 | ArgEscapeData d; |
| 8643 | |
| 8644 | d.min_escape = INT_MAX; |
| 8645 | d.occurrences = 0; |
| 8646 | d.escape_len = 0; |
| 8647 | d.locale_occurrences = 0; |
| 8648 | |
| 8649 | const QChar *c = uc_begin; |
| 8650 | while (c != uc_end) { |
| 8651 | while (c != uc_end && c->unicode() != '%') |
| 8652 | ++c; |
| 8653 | |
| 8654 | if (c == uc_end) |
| 8655 | break; |
| 8656 | const QChar *escape_start = c; |
| 8657 | if (++c == uc_end) |
| 8658 | break; |
| 8659 | |
| 8660 | bool locale_arg = false; |
| 8661 | if (c->unicode() == 'L') { |
| 8662 | locale_arg = true; |
| 8663 | if (++c == uc_end) |
| 8664 | break; |
| 8665 | } |
| 8666 | |
| 8667 | int escape = qArgDigitValue(ch: *c); |
| 8668 | if (escape == -1) |
| 8669 | continue; |
| 8670 | |
| 8671 | // ### Qt 7: do not allow anything but ASCII digits |
| 8672 | // in arg()'s replacements. |
| 8673 | #if QT_VERSION <= QT_VERSION_CHECK(7, 0, 0) && !defined(QT_BOOTSTRAPPED) |
| 8674 | const QChar *escapeBegin = c; |
| 8675 | const QChar *escapeEnd = escapeBegin + 1; |
| 8676 | #endif |
| 8677 | |
| 8678 | ++c; |
| 8679 | |
| 8680 | if (c != uc_end) { |
| 8681 | const int next_escape = qArgDigitValue(ch: *c); |
| 8682 | if (next_escape != -1) { |
| 8683 | escape = (10 * escape) + next_escape; |
| 8684 | ++c; |
| 8685 | #if QT_VERSION <= QT_VERSION_CHECK(7, 0, 0) && !defined(QT_BOOTSTRAPPED) |
| 8686 | ++escapeEnd; |
| 8687 | #endif |
| 8688 | } |
| 8689 | } |
| 8690 | |
| 8691 | #if QT_VERSION <= QT_VERSION_CHECK(7, 0, 0) && !defined(QT_BOOTSTRAPPED) |
| 8692 | checkArgEscape(s: QStringView(escapeBegin, escapeEnd)); |
| 8693 | #endif |
| 8694 | |
| 8695 | if (escape > d.min_escape) |
| 8696 | continue; |
| 8697 | |
| 8698 | if (escape < d.min_escape) { |
| 8699 | d.min_escape = escape; |
| 8700 | d.occurrences = 0; |
| 8701 | d.escape_len = 0; |
| 8702 | d.locale_occurrences = 0; |
| 8703 | } |
| 8704 | |
| 8705 | ++d.occurrences; |
| 8706 | if (locale_arg) |
| 8707 | ++d.locale_occurrences; |
| 8708 | d.escape_len += c - escape_start; |
| 8709 | } |
| 8710 | return d; |
| 8711 | } |
| 8712 | |
| 8713 | static QString replaceArgEscapes(QStringView s, const ArgEscapeData &d, qsizetype field_width, |
| 8714 | QStringView arg, QStringView larg, QChar fillChar) |
| 8715 | { |
| 8716 | // Negative field-width for right-padding, positive for left-padding: |
| 8717 | const qsizetype abs_field_width = qAbs(t: field_width); |
| 8718 | const qsizetype result_len = |
| 8719 | s.size() - d.escape_len |
| 8720 | + (d.occurrences - d.locale_occurrences) * qMax(a: abs_field_width, b: arg.size()) |
| 8721 | + d.locale_occurrences * qMax(a: abs_field_width, b: larg.size()); |
| 8722 | |
| 8723 | QString result(result_len, Qt::Uninitialized); |
| 8724 | QChar *rc = const_cast<QChar *>(result.unicode()); |
| 8725 | QChar *const result_end = rc + result_len; |
| 8726 | qsizetype repl_cnt = 0; |
| 8727 | |
| 8728 | const QChar *c = s.begin(); |
| 8729 | const QChar *const uc_end = s.end(); |
| 8730 | while (c != uc_end) { |
| 8731 | Q_ASSERT(d.occurrences > repl_cnt); |
| 8732 | /* We don't have to check increments of c against uc_end because, as |
| 8733 | long as d.occurrences > repl_cnt, we KNOW there are valid escape |
| 8734 | sequences remaining. */ |
| 8735 | |
| 8736 | const QChar *text_start = c; |
| 8737 | while (c->unicode() != '%') |
| 8738 | ++c; |
| 8739 | |
| 8740 | const QChar *escape_start = c++; |
| 8741 | const bool localize = c->unicode() == 'L'; |
| 8742 | if (localize) |
| 8743 | ++c; |
| 8744 | |
| 8745 | int escape = qArgDigitValue(ch: *c); |
| 8746 | if (escape != -1 && c + 1 != uc_end) { |
| 8747 | const int digit = qArgDigitValue(ch: c[1]); |
| 8748 | if (digit != -1) { |
| 8749 | ++c; |
| 8750 | escape = 10 * escape + digit; |
| 8751 | } |
| 8752 | } |
| 8753 | |
| 8754 | if (escape != d.min_escape) { |
| 8755 | memcpy(dest: rc, src: text_start, n: (c - text_start) * sizeof(QChar)); |
| 8756 | rc += c - text_start; |
| 8757 | } else { |
| 8758 | ++c; |
| 8759 | |
| 8760 | memcpy(dest: rc, src: text_start, n: (escape_start - text_start) * sizeof(QChar)); |
| 8761 | rc += escape_start - text_start; |
| 8762 | |
| 8763 | const QStringView use = localize ? larg : arg; |
| 8764 | const qsizetype pad_chars = abs_field_width - use.size(); |
| 8765 | // (If negative, relevant loops are no-ops: no need to check.) |
| 8766 | |
| 8767 | if (field_width > 0) { // left padded |
| 8768 | rc = std::fill_n(first: rc, n: pad_chars, value: fillChar); |
| 8769 | } |
| 8770 | |
| 8771 | if (use.size()) |
| 8772 | memcpy(dest: rc, src: use.data(), n: use.size() * sizeof(QChar)); |
| 8773 | rc += use.size(); |
| 8774 | |
| 8775 | if (field_width < 0) { // right padded |
| 8776 | rc = std::fill_n(first: rc, n: pad_chars, value: fillChar); |
| 8777 | } |
| 8778 | |
| 8779 | if (++repl_cnt == d.occurrences) { |
| 8780 | memcpy(dest: rc, src: c, n: (uc_end - c) * sizeof(QChar)); |
| 8781 | rc += uc_end - c; |
| 8782 | Q_ASSERT(rc == result_end); |
| 8783 | c = uc_end; |
| 8784 | } |
| 8785 | } |
| 8786 | } |
| 8787 | Q_ASSERT(rc == result_end); |
| 8788 | |
| 8789 | return result; |
| 8790 | } |
| 8791 | |
| 8792 | /*! |
| 8793 | \fn template <typename T, QString::if_string_like<T> = true> QString QString::arg(const T &a, int fieldWidth, QChar fillChar) const |
| 8794 | |
| 8795 | Returns a copy of this string with the lowest-numbered place-marker |
| 8796 | replaced by string \a a, i.e., \c %1, \c %2, ..., \c %99. |
| 8797 | |
| 8798 | \a fieldWidth specifies the minimum amount of space that \a a |
| 8799 | shall occupy. If \a a requires less space than \a fieldWidth, it |
| 8800 | is padded to \a fieldWidth with character \a fillChar. A positive |
| 8801 | \a fieldWidth produces right-aligned text. A negative \a fieldWidth |
| 8802 | produces left-aligned text. |
| 8803 | |
| 8804 | This example shows how we might create a \c status string for |
| 8805 | reporting progress while processing a list of files: |
| 8806 | |
| 8807 | \snippet qstring/main.cpp 11-qstringview |
| 8808 | |
| 8809 | First, \c arg(i) replaces \c %1. Then \c arg(total) replaces \c |
| 8810 | %2. Finally, \c arg(fileName) replaces \c %3. |
| 8811 | |
| 8812 | One advantage of using arg() over asprintf() is that the order of the |
| 8813 | numbered place markers can change, if the application's strings are |
| 8814 | translated into other languages, but each arg() will still replace |
| 8815 | the lowest-numbered unreplaced place-marker, no matter where it |
| 8816 | appears. Also, if place-marker \c %i appears more than once in the |
| 8817 | string, arg() replaces all of them. |
| 8818 | |
| 8819 | If there is no unreplaced place-marker remaining, a warning message |
| 8820 | is printed and the result is undefined. Place-marker numbers must be |
| 8821 | in the range 1 to 99. |
| 8822 | |
| 8823 | \note In Qt versions prior to 6.9, this function was overloaded on |
| 8824 | \c{char}, QChar, QString, QStringView, and QLatin1StringView and in some |
| 8825 | cases, \c{wchar_t} and \c{char16_t} arguments would resolve to the integer |
| 8826 | overloads. In Qt versions prior to 5.10, this function lacked the |
| 8827 | QStringView and QLatin1StringView overloads. |
| 8828 | */ |
| 8829 | QString QString::arg_impl(QAnyStringView a, int fieldWidth, QChar fillChar) const |
| 8830 | { |
| 8831 | ArgEscapeData d = findArgEscapes(s: *this); |
| 8832 | |
| 8833 | if (Q_UNLIKELY(d.occurrences == 0)) { |
| 8834 | qWarning(msg: "QString::arg: Argument missing: \"%ls\", \"%ls\"" , qUtf16Printable(*this), |
| 8835 | qUtf16Printable(a.toString())); |
| 8836 | return *this; |
| 8837 | } |
| 8838 | struct { |
| 8839 | QVarLengthArray<char16_t> out; |
| 8840 | QStringView operator()(QStringView in) noexcept { return in; } |
| 8841 | QStringView operator()(QLatin1StringView in) |
| 8842 | { |
| 8843 | out.resize(sz: in.size()); |
| 8844 | qt_from_latin1(dst: out.data(), str: in.data(), size: size_t(in.size())); |
| 8845 | return out; |
| 8846 | } |
| 8847 | QStringView operator()(QUtf8StringView in) |
| 8848 | { |
| 8849 | out.resize(sz: in.size()); |
| 8850 | return QStringView{out.data(), QUtf8::convertToUnicode(dst: out.data(), in)}; |
| 8851 | } |
| 8852 | } convert; |
| 8853 | |
| 8854 | QStringView sv = a.visit(v: std::ref(t&: convert)); |
| 8855 | return replaceArgEscapes(s: *this, d, field_width: fieldWidth, arg: sv, larg: sv, fillChar); |
| 8856 | } |
| 8857 | |
| 8858 | /*! |
| 8859 | \fn template <typename T, QString::if_integral_non_char<T> = true> QString QString::arg(T a, int fieldWidth, int base, QChar fillChar) const |
| 8860 | \overload arg() |
| 8861 | |
| 8862 | The \a a argument is expressed in base \a base, which is 10 by |
| 8863 | default and must be between 2 and 36. For bases other than 10, \a a |
| 8864 | is treated as an unsigned integer. |
| 8865 | |
| 8866 | \a fieldWidth specifies the minimum amount of space that \a a is |
| 8867 | padded to and filled with the character \a fillChar. A positive |
| 8868 | value produces right-aligned text; a negative value produces |
| 8869 | left-aligned text. |
| 8870 | |
| 8871 | The '%' can be followed by an 'L', in which case the sequence is |
| 8872 | replaced with a localized representation of \a a. The conversion |
| 8873 | uses the default locale, set by QLocale::setDefault(). If no default |
| 8874 | locale was specified, the system locale is used. The 'L' flag is |
| 8875 | ignored if \a base is not 10. |
| 8876 | |
| 8877 | \snippet qstring/main.cpp 12 |
| 8878 | \snippet qstring/main.cpp 14 |
| 8879 | |
| 8880 | \note In Qt versions prior to 6.9, this function was overloaded on various |
| 8881 | integral types and sometimes incorrectly accepted \c char and \c char16_t |
| 8882 | arguments. |
| 8883 | |
| 8884 | \sa {Number Formats} |
| 8885 | */ |
| 8886 | QString QString::arg_impl(qlonglong a, int fieldWidth, int base, QChar fillChar) const |
| 8887 | { |
| 8888 | ArgEscapeData d = findArgEscapes(s: *this); |
| 8889 | |
| 8890 | if (d.occurrences == 0) { |
| 8891 | qWarning(msg: "QString::arg: Argument missing: \"%ls\", %llu" , qUtf16Printable(*this), a); |
| 8892 | return *this; |
| 8893 | } |
| 8894 | |
| 8895 | unsigned flags = QLocaleData::NoFlags; |
| 8896 | // ZeroPadded sorts out left-padding when the fill is zero, to the right of sign: |
| 8897 | if (fillChar == u'0') |
| 8898 | flags = QLocaleData::ZeroPadded; |
| 8899 | |
| 8900 | QString arg; |
| 8901 | if (d.occurrences > d.locale_occurrences) { |
| 8902 | arg = QLocaleData::c()->longLongToString(l: a, precision: -1, base, width: fieldWidth, flags); |
| 8903 | Q_ASSERT(fillChar != u'0' || fieldWidth <= arg.size()); |
| 8904 | } |
| 8905 | |
| 8906 | QString localeArg; |
| 8907 | if (d.locale_occurrences > 0) { |
| 8908 | QLocale locale; |
| 8909 | if (!(locale.numberOptions() & QLocale::OmitGroupSeparator)) |
| 8910 | flags |= QLocaleData::GroupDigits; |
| 8911 | localeArg = locale.d->m_data->longLongToString(l: a, precision: -1, base, width: fieldWidth, flags); |
| 8912 | Q_ASSERT(fillChar != u'0' || fieldWidth <= localeArg.size()); |
| 8913 | } |
| 8914 | |
| 8915 | return replaceArgEscapes(s: *this, d, field_width: fieldWidth, arg, larg: localeArg, fillChar); |
| 8916 | } |
| 8917 | |
| 8918 | QString QString::arg_impl(qulonglong a, int fieldWidth, int base, QChar fillChar) const |
| 8919 | { |
| 8920 | ArgEscapeData d = findArgEscapes(s: *this); |
| 8921 | |
| 8922 | if (d.occurrences == 0) { |
| 8923 | qWarning(msg: "QString::arg: Argument missing: \"%ls\", %lld" , qUtf16Printable(*this), a); |
| 8924 | return *this; |
| 8925 | } |
| 8926 | |
| 8927 | unsigned flags = QLocaleData::NoFlags; |
| 8928 | // ZeroPadded sorts out left-padding when the fill is zero, to the right of sign: |
| 8929 | if (fillChar == u'0') |
| 8930 | flags = QLocaleData::ZeroPadded; |
| 8931 | |
| 8932 | QString arg; |
| 8933 | if (d.occurrences > d.locale_occurrences) { |
| 8934 | arg = QLocaleData::c()->unsLongLongToString(l: a, precision: -1, base, width: fieldWidth, flags); |
| 8935 | Q_ASSERT(fillChar != u'0' || fieldWidth <= arg.size()); |
| 8936 | } |
| 8937 | |
| 8938 | QString localeArg; |
| 8939 | if (d.locale_occurrences > 0) { |
| 8940 | QLocale locale; |
| 8941 | if (!(locale.numberOptions() & QLocale::OmitGroupSeparator)) |
| 8942 | flags |= QLocaleData::GroupDigits; |
| 8943 | localeArg = locale.d->m_data->unsLongLongToString(l: a, precision: -1, base, width: fieldWidth, flags); |
| 8944 | Q_ASSERT(fillChar != u'0' || fieldWidth <= localeArg.size()); |
| 8945 | } |
| 8946 | |
| 8947 | return replaceArgEscapes(s: *this, d, field_width: fieldWidth, arg, larg: localeArg, fillChar); |
| 8948 | } |
| 8949 | |
| 8950 | /*! |
| 8951 | \fn template <typename T, QString::if_floating_point<T> = true> QString QString::arg(T a, int fieldWidth, char format, int precision, QChar fillChar) const |
| 8952 | \overload arg() |
| 8953 | |
| 8954 | Argument \a a is formatted according to the specified \a format and |
| 8955 | \a precision. See \l{Floating-point Formats} for details. |
| 8956 | |
| 8957 | \a fieldWidth specifies the minimum amount of space that \a a is |
| 8958 | padded to and filled with the character \a fillChar. A positive |
| 8959 | value produces right-aligned text; a negative value produces |
| 8960 | left-aligned text. |
| 8961 | |
| 8962 | \snippet code/src_corelib_text_qstring.cpp 2 |
| 8963 | |
| 8964 | \note In Qt versions prior to 6.9, this function was a regular function |
| 8965 | taking \c double. As a consequence of being a template function now, it no |
| 8966 | longer accepts arguments that merely implicitly convert to floating-point |
| 8967 | types. A backwards-compatible fix is to cast such types to one of the C++ |
| 8968 | floating-point types. |
| 8969 | |
| 8970 | \sa QLocale::toString(), QLocale::FloatingPointPrecisionOption, {Number Formats} |
| 8971 | */ |
| 8972 | QString QString::arg_impl(double a, int fieldWidth, char format, int precision, QChar fillChar) const |
| 8973 | { |
| 8974 | ArgEscapeData d = findArgEscapes(s: *this); |
| 8975 | |
| 8976 | if (d.occurrences == 0) { |
| 8977 | qWarning(msg: "QString::arg: Argument missing: \"%ls\", %g" , qUtf16Printable(*this), a); |
| 8978 | return *this; |
| 8979 | } |
| 8980 | |
| 8981 | unsigned flags = QLocaleData::NoFlags; |
| 8982 | // ZeroPadded sorts out left-padding when the fill is zero, to the right of sign: |
| 8983 | if (fillChar == u'0') |
| 8984 | flags |= QLocaleData::ZeroPadded; |
| 8985 | |
| 8986 | if (isAsciiUpper(c: format)) |
| 8987 | flags |= QLocaleData::CapitalEorX; |
| 8988 | |
| 8989 | QLocaleData::DoubleForm form = QLocaleData::DFDecimal; |
| 8990 | switch (QtMiscUtils::toAsciiLower(ch: format)) { |
| 8991 | case 'f': |
| 8992 | form = QLocaleData::DFDecimal; |
| 8993 | break; |
| 8994 | case 'e': |
| 8995 | form = QLocaleData::DFExponent; |
| 8996 | break; |
| 8997 | case 'g': |
| 8998 | form = QLocaleData::DFSignificantDigits; |
| 8999 | break; |
| 9000 | default: |
| 9001 | #if defined(QT_CHECK_RANGE) |
| 9002 | qWarning("QString::arg: Invalid format char '%c'" , format); |
| 9003 | #endif |
| 9004 | break; |
| 9005 | } |
| 9006 | |
| 9007 | QString arg; |
| 9008 | if (d.occurrences > d.locale_occurrences) { |
| 9009 | arg = QLocaleData::c()->doubleToString(d: a, precision, form, width: fieldWidth, |
| 9010 | flags: flags | QLocaleData::ZeroPadExponent); |
| 9011 | Q_ASSERT(fillChar != u'0' || !qt_is_finite(a) |
| 9012 | || fieldWidth <= arg.size()); |
| 9013 | } |
| 9014 | |
| 9015 | QString localeArg; |
| 9016 | if (d.locale_occurrences > 0) { |
| 9017 | QLocale locale; |
| 9018 | |
| 9019 | const QLocale::NumberOptions numberOptions = locale.numberOptions(); |
| 9020 | if (!(numberOptions & QLocale::OmitGroupSeparator)) |
| 9021 | flags |= QLocaleData::GroupDigits; |
| 9022 | if (!(numberOptions & QLocale::OmitLeadingZeroInExponent)) |
| 9023 | flags |= QLocaleData::ZeroPadExponent; |
| 9024 | if (numberOptions & QLocale::IncludeTrailingZeroesAfterDot) |
| 9025 | flags |= QLocaleData::AddTrailingZeroes; |
| 9026 | localeArg = locale.d->m_data->doubleToString(d: a, precision, form, width: fieldWidth, flags); |
| 9027 | Q_ASSERT(fillChar != u'0' || !qt_is_finite(a) |
| 9028 | || fieldWidth <= localeArg.size()); |
| 9029 | } |
| 9030 | |
| 9031 | return replaceArgEscapes(s: *this, d, field_width: fieldWidth, arg, larg: localeArg, fillChar); |
| 9032 | } |
| 9033 | |
| 9034 | static inline char16_t to_unicode(const QChar c) { return c.unicode(); } |
| 9035 | static inline char16_t to_unicode(const char c) { return QLatin1Char{c}.unicode(); } |
| 9036 | |
| 9037 | template <typename Char> |
| 9038 | static int getEscape(const Char *uc, qsizetype *pos, qsizetype len) |
| 9039 | { |
| 9040 | qsizetype i = *pos; |
| 9041 | ++i; |
| 9042 | if (i < len && uc[i] == u'L') |
| 9043 | ++i; |
| 9044 | if (i < len) { |
| 9045 | int escape = to_unicode(uc[i]) - '0'; |
| 9046 | if (uint(escape) >= 10U) |
| 9047 | return -1; |
| 9048 | ++i; |
| 9049 | if (i < len) { |
| 9050 | // there's a second digit |
| 9051 | int digit = to_unicode(uc[i]) - '0'; |
| 9052 | if (uint(digit) < 10U) { |
| 9053 | escape = (escape * 10) + digit; |
| 9054 | ++i; |
| 9055 | } |
| 9056 | } |
| 9057 | *pos = i; |
| 9058 | return escape; |
| 9059 | } |
| 9060 | return -1; |
| 9061 | } |
| 9062 | |
| 9063 | /* |
| 9064 | Algorithm for multiArg: |
| 9065 | |
| 9066 | 1. Parse the string as a sequence of verbatim text and placeholders (%L?\d{,3}). |
| 9067 | The L is parsed and accepted for compatibility with non-multi-arg, but since |
| 9068 | multiArg only accepts strings as replacements, the localization request can |
| 9069 | be safely ignored. |
| 9070 | 2. The result of step (1) is a list of (string-ref,int)-tuples. The string-ref |
| 9071 | either points at text to be copied verbatim (in which case the int is -1), |
| 9072 | or, initially, at the textual representation of the placeholder. In that case, |
| 9073 | the int contains the numerical number as parsed from the placeholder. |
| 9074 | 3. Next, collect all the non-negative ints found, sort them in ascending order and |
| 9075 | remove duplicates. |
| 9076 | 3a. If the result has more entries than multiArg() was given replacement strings, |
| 9077 | we have found placeholders we can't satisfy with replacement strings. That is |
| 9078 | fine (there could be another .arg() call coming after this one), so just |
| 9079 | truncate the result to the number of actual multiArg() replacement strings. |
| 9080 | 3b. If the result has less entries than multiArg() was given replacement strings, |
| 9081 | the string is missing placeholders. This is an error that the user should be |
| 9082 | warned about. |
| 9083 | 4. The result of step (3) is a mapping from the index of any replacement string to |
| 9084 | placeholder number. This is the wrong way around, but since placeholder |
| 9085 | numbers could get as large as 999, while we typically don't have more than 9 |
| 9086 | replacement strings, we trade 4K of sparsely-used memory for doing a reverse lookup |
| 9087 | each time we need to map a placeholder number to a replacement string index |
| 9088 | (that's a linear search; but still *much* faster than using an associative container). |
| 9089 | 5. Next, for each of the tuples found in step (1), do the following: |
| 9090 | 5a. If the int is negative, do nothing. |
| 9091 | 5b. Otherwise, if the int is found in the result of step (3) at index I, replace |
| 9092 | the string-ref with a string-ref for the (complete) I'th replacement string. |
| 9093 | 5c. Otherwise, do nothing. |
| 9094 | 6. Concatenate all string refs into a single result string. |
| 9095 | */ |
| 9096 | |
| 9097 | namespace { |
| 9098 | struct Part |
| 9099 | { |
| 9100 | Part() = default; // for QVarLengthArray; do not use |
| 9101 | constexpr Part(QAnyStringView s, int num = -1) |
| 9102 | : string{s}, number{num} {} |
| 9103 | |
| 9104 | void reset(QAnyStringView s) noexcept { *this = {s, number}; } |
| 9105 | |
| 9106 | QAnyStringView string; |
| 9107 | int number; |
| 9108 | }; |
| 9109 | } // unnamed namespace |
| 9110 | |
| 9111 | Q_DECLARE_TYPEINFO(Part, Q_PRIMITIVE_TYPE); |
| 9112 | |
| 9113 | namespace { |
| 9114 | |
| 9115 | enum { ExpectedParts = 32 }; |
| 9116 | |
| 9117 | typedef QVarLengthArray<Part, ExpectedParts> ParseResult; |
| 9118 | typedef QVarLengthArray<int, ExpectedParts/2> ArgIndexToPlaceholderMap; |
| 9119 | |
| 9120 | template <typename StringView> |
| 9121 | static ParseResult parseMultiArgFormatString_impl(StringView s) |
| 9122 | { |
| 9123 | ParseResult result; |
| 9124 | |
| 9125 | const auto uc = s.data(); |
| 9126 | const auto len = s.size(); |
| 9127 | const auto end = len - 1; |
| 9128 | qsizetype i = 0; |
| 9129 | qsizetype last = 0; |
| 9130 | |
| 9131 | while (i < end) { |
| 9132 | if (uc[i] == u'%') { |
| 9133 | qsizetype percent = i; |
| 9134 | int number = getEscape(uc, &i, len); |
| 9135 | if (number != -1) { |
| 9136 | if (last != percent) |
| 9137 | result.push_back(t: Part{s.sliced(last, percent - last)}); // literal text (incl. failed placeholders) |
| 9138 | result.push_back(t: Part{s.sliced(percent, i - percent), number}); // parsed placeholder |
| 9139 | last = i; |
| 9140 | continue; |
| 9141 | } |
| 9142 | } |
| 9143 | ++i; |
| 9144 | } |
| 9145 | |
| 9146 | if (last < len) |
| 9147 | result.push_back(t: Part{s.sliced(last, len - last)}); // trailing literal text |
| 9148 | |
| 9149 | return result; |
| 9150 | } |
| 9151 | |
| 9152 | static ParseResult parseMultiArgFormatString(QAnyStringView s) |
| 9153 | { |
| 9154 | return s.visit(v: [] (auto s) { return parseMultiArgFormatString_impl(s); }); |
| 9155 | } |
| 9156 | |
| 9157 | static ArgIndexToPlaceholderMap makeArgIndexToPlaceholderMap(const ParseResult &parts) |
| 9158 | { |
| 9159 | ArgIndexToPlaceholderMap result; |
| 9160 | |
| 9161 | for (const Part &part : parts) { |
| 9162 | if (part.number >= 0) |
| 9163 | result.push_back(t: part.number); |
| 9164 | } |
| 9165 | |
| 9166 | std::sort(first: result.begin(), last: result.end()); |
| 9167 | result.erase(abegin: std::unique(first: result.begin(), last: result.end()), |
| 9168 | aend: result.end()); |
| 9169 | |
| 9170 | return result; |
| 9171 | } |
| 9172 | |
| 9173 | static qsizetype resolveStringRefsAndReturnTotalSize(ParseResult &parts, const ArgIndexToPlaceholderMap &argIndexToPlaceholderMap, const QtPrivate::ArgBase *args[]) |
| 9174 | { |
| 9175 | using namespace QtPrivate; |
| 9176 | qsizetype totalSize = 0; |
| 9177 | for (Part &part : parts) { |
| 9178 | if (part.number != -1) { |
| 9179 | const auto it = std::find(first: argIndexToPlaceholderMap.begin(), last: argIndexToPlaceholderMap.end(), val: part.number); |
| 9180 | if (it != argIndexToPlaceholderMap.end()) { |
| 9181 | const auto &arg = *args[it - argIndexToPlaceholderMap.begin()]; |
| 9182 | switch (arg.tag) { |
| 9183 | case ArgBase::L1: |
| 9184 | part.reset(s: static_cast<const QLatin1StringArg&>(arg).string); |
| 9185 | break; |
| 9186 | case ArgBase::Any: |
| 9187 | part.reset(s: static_cast<const QAnyStringArg&>(arg).string); |
| 9188 | break; |
| 9189 | case ArgBase::U16: |
| 9190 | part.reset(s: static_cast<const QStringViewArg&>(arg).string); |
| 9191 | break; |
| 9192 | } |
| 9193 | } |
| 9194 | } |
| 9195 | totalSize += part.string.size(); |
| 9196 | } |
| 9197 | return totalSize; |
| 9198 | } |
| 9199 | |
| 9200 | } // unnamed namespace |
| 9201 | |
| 9202 | QString QtPrivate::argToQString(QAnyStringView pattern, size_t numArgs, const ArgBase **args) |
| 9203 | { |
| 9204 | // Step 1-2 above |
| 9205 | ParseResult parts = parseMultiArgFormatString(s: pattern); |
| 9206 | |
| 9207 | // 3-4 |
| 9208 | ArgIndexToPlaceholderMap argIndexToPlaceholderMap = makeArgIndexToPlaceholderMap(parts); |
| 9209 | |
| 9210 | if (static_cast<size_t>(argIndexToPlaceholderMap.size()) > numArgs) // 3a |
| 9211 | argIndexToPlaceholderMap.resize(sz: qsizetype(numArgs)); |
| 9212 | else if (Q_UNLIKELY(static_cast<size_t>(argIndexToPlaceholderMap.size()) < numArgs)) // 3b |
| 9213 | qWarning(msg: "QString::arg: %d argument(s) missing in %ls" , |
| 9214 | int(numArgs - argIndexToPlaceholderMap.size()), qUtf16Printable(pattern.toString())); |
| 9215 | |
| 9216 | // 5 |
| 9217 | const qsizetype totalSize = resolveStringRefsAndReturnTotalSize(parts, argIndexToPlaceholderMap, args); |
| 9218 | |
| 9219 | // 6: |
| 9220 | QString result(totalSize, Qt::Uninitialized); |
| 9221 | auto out = const_cast<QChar*>(result.constData()); |
| 9222 | |
| 9223 | struct Concatenate { |
| 9224 | QChar *out; |
| 9225 | QChar *operator()(QLatin1String part) noexcept |
| 9226 | { |
| 9227 | if (part.size()) { |
| 9228 | qt_from_latin1(dst: reinterpret_cast<char16_t*>(out), |
| 9229 | str: part.data(), size: part.size()); |
| 9230 | } |
| 9231 | return out + part.size(); |
| 9232 | } |
| 9233 | QChar *operator()(QUtf8StringView part) noexcept |
| 9234 | { |
| 9235 | return QUtf8::convertToUnicode(buffer: out, in: part); |
| 9236 | } |
| 9237 | QChar *operator()(QStringView part) noexcept |
| 9238 | { |
| 9239 | if (part.size()) |
| 9240 | memcpy(dest: out, src: part.data(), n: part.size() * sizeof(QChar)); |
| 9241 | return out + part.size(); |
| 9242 | } |
| 9243 | }; |
| 9244 | |
| 9245 | for (const Part &part : parts) |
| 9246 | out = part.string.visit(v: Concatenate{.out: out}); |
| 9247 | |
| 9248 | // UTF-8 decoding may have caused an overestimate of totalSize - correct it: |
| 9249 | result.truncate(pos: out - result.cbegin()); |
| 9250 | |
| 9251 | return result; |
| 9252 | } |
| 9253 | |
| 9254 | /*! \fn bool QString::isRightToLeft() const |
| 9255 | |
| 9256 | Returns \c true if the string is read right to left. |
| 9257 | |
| 9258 | \sa QStringView::isRightToLeft() |
| 9259 | */ |
| 9260 | bool QString::isRightToLeft() const |
| 9261 | { |
| 9262 | return QtPrivate::isRightToLeft(string: QStringView(*this)); |
| 9263 | } |
| 9264 | |
| 9265 | /*! |
| 9266 | \fn bool QString::isValidUtf16() const noexcept |
| 9267 | \since 5.15 |
| 9268 | |
| 9269 | Returns \c true if the string contains valid UTF-16 encoded data, |
| 9270 | or \c false otherwise. |
| 9271 | |
| 9272 | Note that this function does not perform any special validation of the |
| 9273 | data; it merely checks if it can be successfully decoded from UTF-16. |
| 9274 | The data is assumed to be in host byte order; the presence of a BOM |
| 9275 | is meaningless. |
| 9276 | |
| 9277 | \sa QStringView::isValidUtf16() |
| 9278 | */ |
| 9279 | |
| 9280 | /*! \fn QChar *QString::data() |
| 9281 | |
| 9282 | Returns a pointer to the data stored in the QString. The pointer |
| 9283 | can be used to access and modify the characters that compose the |
| 9284 | string. |
| 9285 | |
| 9286 | Unlike constData() and unicode(), the returned data is always |
| 9287 | '\\0'-terminated. |
| 9288 | |
| 9289 | Example: |
| 9290 | |
| 9291 | \snippet qstring/main.cpp 19 |
| 9292 | |
| 9293 | Note that the pointer remains valid only as long as the string is |
| 9294 | not modified by other means. For read-only access, constData() is |
| 9295 | faster because it never causes a \l{deep copy} to occur. |
| 9296 | |
| 9297 | \sa constData(), operator[]() |
| 9298 | */ |
| 9299 | |
| 9300 | /*! \fn const QChar *QString::data() const |
| 9301 | |
| 9302 | \overload |
| 9303 | |
| 9304 | \note The returned string may not be '\\0'-terminated. |
| 9305 | Use size() to determine the length of the array. |
| 9306 | |
| 9307 | \sa fromRawData() |
| 9308 | */ |
| 9309 | |
| 9310 | /*! \fn const QChar *QString::constData() const |
| 9311 | |
| 9312 | Returns a pointer to the data stored in the QString. The pointer |
| 9313 | can be used to access the characters that compose the string. |
| 9314 | |
| 9315 | Note that the pointer remains valid only as long as the string is |
| 9316 | not modified. |
| 9317 | |
| 9318 | \note The returned string may not be '\\0'-terminated. |
| 9319 | Use size() to determine the length of the array. |
| 9320 | |
| 9321 | \sa data(), operator[](), fromRawData() |
| 9322 | */ |
| 9323 | |
| 9324 | /*! \fn void QString::push_front(const QString &other) |
| 9325 | |
| 9326 | This function is provided for STL compatibility, prepending the |
| 9327 | given \a other string to the beginning of this string. It is |
| 9328 | equivalent to \c prepend(other). |
| 9329 | |
| 9330 | \sa prepend() |
| 9331 | */ |
| 9332 | |
| 9333 | /*! \fn void QString::push_front(QChar ch) |
| 9334 | |
| 9335 | \overload |
| 9336 | |
| 9337 | Prepends the given \a ch character to the beginning of this string. |
| 9338 | */ |
| 9339 | |
| 9340 | /*! \fn void QString::push_back(const QString &other) |
| 9341 | |
| 9342 | This function is provided for STL compatibility, appending the |
| 9343 | given \a other string onto the end of this string. It is |
| 9344 | equivalent to \c append(other). |
| 9345 | |
| 9346 | \sa append() |
| 9347 | */ |
| 9348 | |
| 9349 | /*! \fn void QString::push_back(QChar ch) |
| 9350 | |
| 9351 | \overload |
| 9352 | |
| 9353 | Appends the given \a ch character onto the end of this string. |
| 9354 | */ |
| 9355 | |
| 9356 | /*! |
| 9357 | \since 6.1 |
| 9358 | |
| 9359 | Removes from the string the characters in the half-open range |
| 9360 | [ \a first , \a last ). Returns an iterator to the character |
| 9361 | immediately after the last erased character (i.e. the character |
| 9362 | referred to by \a last before the erase). |
| 9363 | */ |
| 9364 | QString::iterator QString::erase(QString::const_iterator first, QString::const_iterator last) |
| 9365 | { |
| 9366 | const auto start = std::distance(first: cbegin(), last: first); |
| 9367 | const auto len = std::distance(first: first, last: last); |
| 9368 | remove(pos: start, len); |
| 9369 | return begin() + start; |
| 9370 | } |
| 9371 | |
| 9372 | /*! |
| 9373 | \fn QString::iterator QString::erase(QString::const_iterator it) |
| 9374 | |
| 9375 | \overload |
| 9376 | \since 6.5 |
| 9377 | |
| 9378 | Removes the character denoted by \c it from the string. |
| 9379 | Returns an iterator to the character immediately after the |
| 9380 | erased character. |
| 9381 | |
| 9382 | \code |
| 9383 | QString c = "abcdefg"; |
| 9384 | auto it = c.erase(c.cbegin()); // c is now "bcdefg"; "it" points to "b" |
| 9385 | \endcode |
| 9386 | */ |
| 9387 | |
| 9388 | /*! \fn void QString::shrink_to_fit() |
| 9389 | \since 5.10 |
| 9390 | |
| 9391 | This function is provided for STL compatibility. It is |
| 9392 | equivalent to squeeze(). |
| 9393 | |
| 9394 | \sa squeeze() |
| 9395 | */ |
| 9396 | |
| 9397 | /*! |
| 9398 | \fn std::string QString::toStdString() const |
| 9399 | |
| 9400 | Returns a std::string object with the data contained in this |
| 9401 | QString. The Unicode data is converted into 8-bit characters using |
| 9402 | the toUtf8() function. |
| 9403 | |
| 9404 | This method is mostly useful to pass a QString to a function |
| 9405 | that accepts a std::string object. |
| 9406 | |
| 9407 | \sa toLatin1(), toUtf8(), toLocal8Bit(), QByteArray::toStdString() |
| 9408 | */ |
| 9409 | std::string QString::toStdString() const |
| 9410 | { |
| 9411 | std::string result; |
| 9412 | if (isEmpty()) |
| 9413 | return result; |
| 9414 | |
| 9415 | auto writeToBuffer = [this](char *out, size_t) { |
| 9416 | char *last = QUtf8::convertFromUnicode(dst: out, in: *this); |
| 9417 | return last - out; |
| 9418 | }; |
| 9419 | size_t maxSize = size() * 3; // worst case for UTF-8 |
| 9420 | #ifdef __cpp_lib_string_resize_and_overwrite |
| 9421 | // C++23 |
| 9422 | result.resize_and_overwrite(maxSize, writeToBuffer); |
| 9423 | #else |
| 9424 | result.resize(n: maxSize); |
| 9425 | result.resize(n: writeToBuffer(result.data(), result.size())); |
| 9426 | #endif |
| 9427 | return result; |
| 9428 | } |
| 9429 | |
| 9430 | /*! |
| 9431 | \fn QString QString::fromRawData(const char16_t *unicode, qsizetype size) |
| 9432 | \since 6.10 |
| 9433 | |
| 9434 | Constructs a QString that uses the first \a size Unicode characters |
| 9435 | in the array \a unicode. The data in \a unicode is \e not |
| 9436 | copied. The caller must be able to guarantee that \a unicode will |
| 9437 | not be deleted or modified as long as the QString (or an |
| 9438 | unmodified copy of it) exists. |
| 9439 | |
| 9440 | Any attempts to modify the QString or copies of it will cause it |
| 9441 | to create a deep copy of the data, ensuring that the raw data |
| 9442 | isn't modified. |
| 9443 | |
| 9444 | Here is an example of how we can use a QRegularExpression on raw data in |
| 9445 | memory without requiring to copy the data into a QString: |
| 9446 | |
| 9447 | \snippet qstring/main.cpp 22 |
| 9448 | \snippet qstring/main.cpp 23 |
| 9449 | |
| 9450 | \warning A string created with fromRawData() is \e not |
| 9451 | '\\0'-terminated, unless the raw data contains a '\\0' character |
| 9452 | at position \a size. This means unicode() will \e not return a |
| 9453 | '\\0'-terminated string (although utf16() does, at the cost of |
| 9454 | copying the raw data). |
| 9455 | |
| 9456 | \sa fromUtf16(), setRawData(), data(), constData(), |
| 9457 | nullTerminate(), nullTerminated() |
| 9458 | */ |
| 9459 | |
| 9460 | /*! |
| 9461 | \fn QString QString::fromRawData(const QChar *unicode, qsizetype size) |
| 9462 | \overload |
| 9463 | */ |
| 9464 | |
| 9465 | /*! |
| 9466 | \since 4.7 |
| 9467 | |
| 9468 | Resets the QString to use the first \a size Unicode characters |
| 9469 | in the array \a unicode. The data in \a unicode is \e not |
| 9470 | copied. The caller must be able to guarantee that \a unicode will |
| 9471 | not be deleted or modified as long as the QString (or an |
| 9472 | unmodified copy of it) exists. |
| 9473 | |
| 9474 | This function can be used instead of fromRawData() to re-use |
| 9475 | existings QString objects to save memory re-allocations. |
| 9476 | |
| 9477 | \sa fromRawData(), nullTerminate(), nullTerminated() |
| 9478 | */ |
| 9479 | QString &QString::setRawData(const QChar *unicode, qsizetype size) |
| 9480 | { |
| 9481 | if (!unicode || !size) { |
| 9482 | clear(); |
| 9483 | } |
| 9484 | *this = fromRawData(unicode, size); |
| 9485 | return *this; |
| 9486 | } |
| 9487 | |
| 9488 | /*! \fn QString QString::fromStdU16String(const std::u16string &str) |
| 9489 | \since 5.5 |
| 9490 | |
| 9491 | \include qstring.cpp {from-std-string} {UTF-16} {fromUtf16()} |
| 9492 | |
| 9493 | \sa fromUtf16(), fromStdWString(), fromStdU32String() |
| 9494 | */ |
| 9495 | |
| 9496 | /*! |
| 9497 | \fn std::u16string QString::toStdU16String() const |
| 9498 | \since 5.5 |
| 9499 | |
| 9500 | Returns a std::u16string object with the data contained in this |
| 9501 | QString. The Unicode data is the same as returned by the utf16() |
| 9502 | method. |
| 9503 | |
| 9504 | \sa utf16(), toStdWString(), toStdU32String() |
| 9505 | */ |
| 9506 | |
| 9507 | /*! \fn QString QString::fromStdU32String(const std::u32string &str) |
| 9508 | \since 5.5 |
| 9509 | |
| 9510 | \include qstring.cpp {from-std-string} {UTF-32} {fromUcs4()} |
| 9511 | |
| 9512 | \sa fromUcs4(), fromStdWString(), fromStdU16String() |
| 9513 | */ |
| 9514 | |
| 9515 | /*! |
| 9516 | \fn std::u32string QString::toStdU32String() const |
| 9517 | \since 5.5 |
| 9518 | |
| 9519 | Returns a std::u32string object with the data contained in this |
| 9520 | QString. The Unicode data is the same as returned by the toUcs4() |
| 9521 | method. |
| 9522 | |
| 9523 | \sa toUcs4(), toStdWString(), toStdU16String() |
| 9524 | */ |
| 9525 | |
| 9526 | #if !defined(QT_NO_DATASTREAM) |
| 9527 | /*! |
| 9528 | \fn QDataStream &operator<<(QDataStream &stream, const QString &string) |
| 9529 | \relates QString |
| 9530 | |
| 9531 | Writes the given \a string to the specified \a stream. |
| 9532 | |
| 9533 | \sa {Serializing Qt Data Types} |
| 9534 | */ |
| 9535 | |
| 9536 | QDataStream &operator<<(QDataStream &out, const QString &str) |
| 9537 | { |
| 9538 | if (out.version() == 1) { |
| 9539 | out << str.toLatin1(); |
| 9540 | } else { |
| 9541 | if (!str.isNull() || out.version() < 3) { |
| 9542 | if ((out.byteOrder() == QDataStream::BigEndian) == (QSysInfo::ByteOrder == QSysInfo::BigEndian)) { |
| 9543 | out.writeBytes(reinterpret_cast<const char *>(str.unicode()), |
| 9544 | len: static_cast<qsizetype>(sizeof(QChar) * str.size())); |
| 9545 | } else { |
| 9546 | QVarLengthArray<char16_t> buffer(str.size()); |
| 9547 | qbswap<sizeof(char16_t)>(source: str.constData(), count: str.size(), dest: buffer.data()); |
| 9548 | out.writeBytes(reinterpret_cast<const char *>(buffer.data()), |
| 9549 | len: static_cast<qsizetype>(sizeof(char16_t) * buffer.size())); |
| 9550 | } |
| 9551 | } else { |
| 9552 | QDataStream::writeQSizeType(s&: out, value: -1); // write null marker |
| 9553 | } |
| 9554 | } |
| 9555 | return out; |
| 9556 | } |
| 9557 | |
| 9558 | /*! |
| 9559 | \fn QDataStream &operator>>(QDataStream &stream, QString &string) |
| 9560 | \relates QString |
| 9561 | |
| 9562 | Reads a string from the specified \a stream into the given \a string. |
| 9563 | |
| 9564 | \sa {Serializing Qt Data Types} |
| 9565 | */ |
| 9566 | |
| 9567 | QDataStream &operator>>(QDataStream &in, QString &str) |
| 9568 | { |
| 9569 | if (in.version() == 1) { |
| 9570 | QByteArray l; |
| 9571 | in >> l; |
| 9572 | str = QString::fromLatin1(ba: l); |
| 9573 | } else { |
| 9574 | qint64 size = QDataStream::readQSizeType(s&: in); |
| 9575 | qsizetype bytes = size; |
| 9576 | if (size != bytes || size < -1) { |
| 9577 | str.clear(); |
| 9578 | in.setStatus(QDataStream::SizeLimitExceeded); |
| 9579 | return in; |
| 9580 | } |
| 9581 | if (bytes == -1) { // null string |
| 9582 | str = QString(); |
| 9583 | } else if (bytes > 0) { |
| 9584 | if (bytes & 0x1) { |
| 9585 | str.clear(); |
| 9586 | in.setStatus(QDataStream::ReadCorruptData); |
| 9587 | return in; |
| 9588 | } |
| 9589 | |
| 9590 | const qsizetype Step = 1024 * 1024; |
| 9591 | qsizetype len = bytes / 2; |
| 9592 | qsizetype allocated = 0; |
| 9593 | |
| 9594 | while (allocated < len) { |
| 9595 | int blockSize = qMin(a: Step, b: len - allocated); |
| 9596 | str.resize(size: allocated + blockSize); |
| 9597 | if (in.readRawData(reinterpret_cast<char *>(str.data()) + allocated * 2, |
| 9598 | len: blockSize * 2) != blockSize * 2) { |
| 9599 | str.clear(); |
| 9600 | in.setStatus(QDataStream::ReadPastEnd); |
| 9601 | return in; |
| 9602 | } |
| 9603 | allocated += blockSize; |
| 9604 | } |
| 9605 | |
| 9606 | if ((in.byteOrder() == QDataStream::BigEndian) |
| 9607 | != (QSysInfo::ByteOrder == QSysInfo::BigEndian)) { |
| 9608 | char16_t *data = reinterpret_cast<char16_t *>(str.data()); |
| 9609 | qbswap<sizeof(*data)>(source: data, count: len, dest: data); |
| 9610 | } |
| 9611 | } else { |
| 9612 | str = QString(QLatin1StringView("" )); |
| 9613 | } |
| 9614 | } |
| 9615 | return in; |
| 9616 | } |
| 9617 | #endif // QT_NO_DATASTREAM |
| 9618 | |
| 9619 | /*! |
| 9620 | \typedef QString::Data |
| 9621 | \internal |
| 9622 | */ |
| 9623 | |
| 9624 | /*! |
| 9625 | \typedef QString::DataPtr |
| 9626 | \internal |
| 9627 | */ |
| 9628 | |
| 9629 | /*! |
| 9630 | \fn DataPtr & QString::data_ptr() |
| 9631 | \internal |
| 9632 | */ |
| 9633 | |
| 9634 | /*! |
| 9635 | \since 5.11 |
| 9636 | \internal |
| 9637 | \relates QStringView |
| 9638 | |
| 9639 | Returns \c true if the string is read right to left. |
| 9640 | |
| 9641 | \sa QString::isRightToLeft() |
| 9642 | */ |
| 9643 | bool QtPrivate::isRightToLeft(QStringView string) noexcept |
| 9644 | { |
| 9645 | int isolateLevel = 0; |
| 9646 | |
| 9647 | for (QStringIterator i(string); i.hasNext();) { |
| 9648 | const char32_t c = i.next(); |
| 9649 | |
| 9650 | switch (QChar::direction(ucs4: c)) { |
| 9651 | case QChar::DirRLI: |
| 9652 | case QChar::DirLRI: |
| 9653 | case QChar::DirFSI: |
| 9654 | ++isolateLevel; |
| 9655 | break; |
| 9656 | case QChar::DirPDI: |
| 9657 | if (isolateLevel) |
| 9658 | --isolateLevel; |
| 9659 | break; |
| 9660 | case QChar::DirL: |
| 9661 | if (isolateLevel) |
| 9662 | break; |
| 9663 | return false; |
| 9664 | case QChar::DirR: |
| 9665 | case QChar::DirAL: |
| 9666 | if (isolateLevel) |
| 9667 | break; |
| 9668 | return true; |
| 9669 | case QChar::DirEN: |
| 9670 | case QChar::DirES: |
| 9671 | case QChar::DirET: |
| 9672 | case QChar::DirAN: |
| 9673 | case QChar::DirCS: |
| 9674 | case QChar::DirB: |
| 9675 | case QChar::DirS: |
| 9676 | case QChar::DirWS: |
| 9677 | case QChar::DirON: |
| 9678 | case QChar::DirLRE: |
| 9679 | case QChar::DirLRO: |
| 9680 | case QChar::DirRLE: |
| 9681 | case QChar::DirRLO: |
| 9682 | case QChar::DirPDF: |
| 9683 | case QChar::DirNSM: |
| 9684 | case QChar::DirBN: |
| 9685 | break; |
| 9686 | } |
| 9687 | } |
| 9688 | return false; |
| 9689 | } |
| 9690 | |
| 9691 | qsizetype QtPrivate::count(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs) noexcept |
| 9692 | { |
| 9693 | qsizetype num = 0; |
| 9694 | qsizetype i = -1; |
| 9695 | if (haystack.size() > 500 && needle.size() > 5) { |
| 9696 | QStringMatcher matcher(needle, cs); |
| 9697 | while ((i = matcher.indexIn(str: haystack, from: i + 1)) != -1) |
| 9698 | ++num; |
| 9699 | } else { |
| 9700 | while ((i = QtPrivate::findString(haystack, from: i + 1, needle, cs)) != -1) |
| 9701 | ++num; |
| 9702 | } |
| 9703 | return num; |
| 9704 | } |
| 9705 | |
| 9706 | qsizetype QtPrivate::count(QStringView haystack, QChar needle, Qt::CaseSensitivity cs) noexcept |
| 9707 | { |
| 9708 | if (cs == Qt::CaseSensitive) |
| 9709 | return std::count(first: haystack.cbegin(), last: haystack.cend(), value: needle); |
| 9710 | |
| 9711 | needle = foldCase(ch: needle); |
| 9712 | return std::count_if(first: haystack.cbegin(), last: haystack.cend(), |
| 9713 | pred: [needle](const QChar c) { return foldAndCompare(a: c, b: needle); }); |
| 9714 | } |
| 9715 | |
| 9716 | qsizetype QtPrivate::count(QLatin1StringView haystack, QLatin1StringView needle, Qt::CaseSensitivity cs) |
| 9717 | { |
| 9718 | qsizetype num = 0; |
| 9719 | qsizetype i = -1; |
| 9720 | |
| 9721 | QLatin1StringMatcher matcher(needle, cs); |
| 9722 | while ((i = matcher.indexIn(haystack, from: i + 1)) != -1) |
| 9723 | ++num; |
| 9724 | |
| 9725 | return num; |
| 9726 | } |
| 9727 | |
| 9728 | qsizetype QtPrivate::count(QLatin1StringView haystack, QStringView needle, Qt::CaseSensitivity cs) |
| 9729 | { |
| 9730 | if (haystack.size() < needle.size()) |
| 9731 | return 0; |
| 9732 | |
| 9733 | if (!QtPrivate::isLatin1(s: needle)) // won't find non-L1 UTF-16 needles in a L1 haystack! |
| 9734 | return 0; |
| 9735 | |
| 9736 | qsizetype num = 0; |
| 9737 | qsizetype i = -1; |
| 9738 | |
| 9739 | QVarLengthArray<uchar> s(needle.size()); |
| 9740 | qt_to_latin1_unchecked(dst: s.data(), src: needle.utf16(), length: needle.size()); |
| 9741 | |
| 9742 | QLatin1StringMatcher matcher(QLatin1StringView(reinterpret_cast<char *>(s.data()), s.size()), |
| 9743 | cs); |
| 9744 | while ((i = matcher.indexIn(haystack, from: i + 1)) != -1) |
| 9745 | ++num; |
| 9746 | |
| 9747 | return num; |
| 9748 | } |
| 9749 | |
| 9750 | qsizetype QtPrivate::count(QStringView haystack, QLatin1StringView needle, Qt::CaseSensitivity cs) |
| 9751 | { |
| 9752 | if (haystack.size() < needle.size()) |
| 9753 | return -1; |
| 9754 | |
| 9755 | QVarLengthArray<char16_t> s = qt_from_latin1_to_qvla(str: needle); |
| 9756 | return QtPrivate::count(haystack, needle: QStringView(s.data(), s.size()), cs); |
| 9757 | } |
| 9758 | |
| 9759 | qsizetype QtPrivate::count(QLatin1StringView haystack, QChar needle, Qt::CaseSensitivity cs) noexcept |
| 9760 | { |
| 9761 | // non-L1 needles cannot possibly match in L1-only haystacks |
| 9762 | if (needle.unicode() > 0xff) |
| 9763 | return 0; |
| 9764 | |
| 9765 | if (cs == Qt::CaseSensitive) { |
| 9766 | return std::count(first: haystack.cbegin(), last: haystack.cend(), value: needle.toLatin1()); |
| 9767 | } else { |
| 9768 | return std::count_if(first: haystack.cbegin(), last: haystack.cend(), |
| 9769 | pred: CaseInsensitiveL1::matcher(ch: needle.toLatin1())); |
| 9770 | } |
| 9771 | } |
| 9772 | |
| 9773 | /*! |
| 9774 | \fn bool QtPrivate::startsWith(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs) |
| 9775 | \since 5.10 |
| 9776 | \fn bool QtPrivate::startsWith(QStringView haystack, QLatin1StringView needle, Qt::CaseSensitivity cs) |
| 9777 | \since 5.10 |
| 9778 | \fn bool QtPrivate::startsWith(QLatin1StringView haystack, QStringView needle, Qt::CaseSensitivity cs) |
| 9779 | \since 5.10 |
| 9780 | \fn bool QtPrivate::startsWith(QLatin1StringView haystack, QLatin1StringView needle, Qt::CaseSensitivity cs) |
| 9781 | \since 5.10 |
| 9782 | \internal |
| 9783 | \relates QStringView |
| 9784 | |
| 9785 | Returns \c true if \a haystack starts with \a needle, |
| 9786 | otherwise returns \c false. |
| 9787 | |
| 9788 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
| 9789 | |
| 9790 | \sa QtPrivate::endsWith(), QString::endsWith(), QStringView::endsWith(), QLatin1StringView::endsWith() |
| 9791 | */ |
| 9792 | |
| 9793 | bool QtPrivate::startsWith(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs) noexcept |
| 9794 | { |
| 9795 | return qt_starts_with_impl(haystack, needle, cs); |
| 9796 | } |
| 9797 | |
| 9798 | bool QtPrivate::startsWith(QStringView haystack, QLatin1StringView needle, Qt::CaseSensitivity cs) noexcept |
| 9799 | { |
| 9800 | return qt_starts_with_impl(haystack, needle, cs); |
| 9801 | } |
| 9802 | |
| 9803 | bool QtPrivate::startsWith(QLatin1StringView haystack, QStringView needle, Qt::CaseSensitivity cs) noexcept |
| 9804 | { |
| 9805 | return qt_starts_with_impl(haystack, needle, cs); |
| 9806 | } |
| 9807 | |
| 9808 | bool QtPrivate::startsWith(QLatin1StringView haystack, QLatin1StringView needle, Qt::CaseSensitivity cs) noexcept |
| 9809 | { |
| 9810 | return qt_starts_with_impl(haystack, needle, cs); |
| 9811 | } |
| 9812 | |
| 9813 | /*! |
| 9814 | \fn bool QtPrivate::endsWith(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs) |
| 9815 | \since 5.10 |
| 9816 | \fn bool QtPrivate::endsWith(QStringView haystack, QLatin1StringView needle, Qt::CaseSensitivity cs) |
| 9817 | \since 5.10 |
| 9818 | \fn bool QtPrivate::endsWith(QLatin1StringView haystack, QStringView needle, Qt::CaseSensitivity cs) |
| 9819 | \since 5.10 |
| 9820 | \fn bool QtPrivate::endsWith(QLatin1StringView haystack, QLatin1StringView needle, Qt::CaseSensitivity cs) |
| 9821 | \since 5.10 |
| 9822 | \internal |
| 9823 | \relates QStringView |
| 9824 | |
| 9825 | Returns \c true if \a haystack ends with \a needle, |
| 9826 | otherwise returns \c false. |
| 9827 | |
| 9828 | \include qstring.qdocinc {search-comparison-case-sensitivity} {search} |
| 9829 | |
| 9830 | \sa QtPrivate::startsWith(), QString::endsWith(), QStringView::endsWith(), QLatin1StringView::endsWith() |
| 9831 | */ |
| 9832 | |
| 9833 | bool QtPrivate::endsWith(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs) noexcept |
| 9834 | { |
| 9835 | return qt_ends_with_impl(haystack, needle, cs); |
| 9836 | } |
| 9837 | |
| 9838 | bool QtPrivate::endsWith(QStringView haystack, QLatin1StringView needle, Qt::CaseSensitivity cs) noexcept |
| 9839 | { |
| 9840 | return qt_ends_with_impl(haystack, needle, cs); |
| 9841 | } |
| 9842 | |
| 9843 | bool QtPrivate::endsWith(QLatin1StringView haystack, QStringView needle, Qt::CaseSensitivity cs) noexcept |
| 9844 | { |
| 9845 | return qt_ends_with_impl(haystack, needle, cs); |
| 9846 | } |
| 9847 | |
| 9848 | bool QtPrivate::endsWith(QLatin1StringView haystack, QLatin1StringView needle, Qt::CaseSensitivity cs) noexcept |
| 9849 | { |
| 9850 | return qt_ends_with_impl(haystack, needle, cs); |
| 9851 | } |
| 9852 | |
| 9853 | qsizetype QtPrivate::findString(QStringView haystack0, qsizetype from, QStringView needle0, Qt::CaseSensitivity cs) noexcept |
| 9854 | { |
| 9855 | const qsizetype l = haystack0.size(); |
| 9856 | const qsizetype sl = needle0.size(); |
| 9857 | if (sl == 1) |
| 9858 | return findString(str: haystack0, from, ch: needle0[0], cs); |
| 9859 | if (from < 0) |
| 9860 | from += l; |
| 9861 | if (std::size_t(sl + from) > std::size_t(l)) |
| 9862 | return -1; |
| 9863 | if (!sl) |
| 9864 | return from; |
| 9865 | if (!l) |
| 9866 | return -1; |
| 9867 | |
| 9868 | /* |
| 9869 | We use the Boyer-Moore algorithm in cases where the overhead |
| 9870 | for the skip table should pay off, otherwise we use a simple |
| 9871 | hash function. |
| 9872 | */ |
| 9873 | if (l > 500 && sl > 5) |
| 9874 | return qFindStringBoyerMoore(haystack: haystack0, from, needle: needle0, cs); |
| 9875 | |
| 9876 | auto sv = [sl](const char16_t *v) { return QStringView(v, sl); }; |
| 9877 | /* |
| 9878 | We use some hashing for efficiency's sake. Instead of |
| 9879 | comparing strings, we compare the hash value of str with that |
| 9880 | of a part of this QString. Only if that matches, we call |
| 9881 | qt_string_compare(). |
| 9882 | */ |
| 9883 | const char16_t *needle = needle0.utf16(); |
| 9884 | const char16_t *haystack = haystack0.utf16() + from; |
| 9885 | const char16_t *end = haystack0.utf16() + (l - sl); |
| 9886 | const qregisteruint sl_minus_1 = sl - 1; |
| 9887 | qregisteruint hashNeedle = 0, hashHaystack = 0; |
| 9888 | qsizetype idx; |
| 9889 | |
| 9890 | if (cs == Qt::CaseSensitive) { |
| 9891 | for (idx = 0; idx < sl; ++idx) { |
| 9892 | hashNeedle = ((hashNeedle<<1) + needle[idx]); |
| 9893 | hashHaystack = ((hashHaystack<<1) + haystack[idx]); |
| 9894 | } |
| 9895 | hashHaystack -= haystack[sl_minus_1]; |
| 9896 | |
| 9897 | while (haystack <= end) { |
| 9898 | hashHaystack += haystack[sl_minus_1]; |
| 9899 | if (hashHaystack == hashNeedle |
| 9900 | && QtPrivate::compareStrings(lhs: needle0, rhs: sv(haystack), cs: Qt::CaseSensitive) == 0) |
| 9901 | return haystack - haystack0.utf16(); |
| 9902 | |
| 9903 | REHASH(*haystack); |
| 9904 | ++haystack; |
| 9905 | } |
| 9906 | } else { |
| 9907 | const char16_t *haystack_start = haystack0.utf16(); |
| 9908 | for (idx = 0; idx < sl; ++idx) { |
| 9909 | hashNeedle = (hashNeedle<<1) + foldCase(ch: needle + idx, start: needle); |
| 9910 | hashHaystack = (hashHaystack<<1) + foldCase(ch: haystack + idx, start: haystack_start); |
| 9911 | } |
| 9912 | hashHaystack -= foldCase(ch: haystack + sl_minus_1, start: haystack_start); |
| 9913 | |
| 9914 | while (haystack <= end) { |
| 9915 | hashHaystack += foldCase(ch: haystack + sl_minus_1, start: haystack_start); |
| 9916 | if (hashHaystack == hashNeedle |
| 9917 | && QtPrivate::compareStrings(lhs: needle0, rhs: sv(haystack), cs: Qt::CaseInsensitive) == 0) |
| 9918 | return haystack - haystack0.utf16(); |
| 9919 | |
| 9920 | REHASH(foldCase(haystack, haystack_start)); |
| 9921 | ++haystack; |
| 9922 | } |
| 9923 | } |
| 9924 | return -1; |
| 9925 | } |
| 9926 | |
| 9927 | qsizetype QtPrivate::findString(QStringView haystack, qsizetype from, QLatin1StringView needle, Qt::CaseSensitivity cs) noexcept |
| 9928 | { |
| 9929 | if (haystack.size() < needle.size()) |
| 9930 | return -1; |
| 9931 | |
| 9932 | QVarLengthArray<char16_t> s = qt_from_latin1_to_qvla(str: needle); |
| 9933 | return QtPrivate::findString(haystack0: haystack, from, needle0: QStringView(reinterpret_cast<const QChar*>(s.constData()), s.size()), cs); |
| 9934 | } |
| 9935 | |
| 9936 | qsizetype QtPrivate::findString(QLatin1StringView haystack, qsizetype from, QStringView needle, Qt::CaseSensitivity cs) noexcept |
| 9937 | { |
| 9938 | if (haystack.size() < needle.size()) |
| 9939 | return -1; |
| 9940 | |
| 9941 | if (!QtPrivate::isLatin1(s: needle)) // won't find non-L1 UTF-16 needles in a L1 haystack! |
| 9942 | return -1; |
| 9943 | |
| 9944 | if (needle.size() == 1) { |
| 9945 | const char n = needle.front().toLatin1(); |
| 9946 | return QtPrivate::findString(haystack, from, needle: QLatin1StringView(&n, 1), cs); |
| 9947 | } |
| 9948 | |
| 9949 | QVarLengthArray<char> s(needle.size()); |
| 9950 | qt_to_latin1_unchecked(dst: reinterpret_cast<uchar *>(s.data()), src: needle.utf16(), length: needle.size()); |
| 9951 | return QtPrivate::findString(haystack, from, needle: QLatin1StringView(s.data(), s.size()), cs); |
| 9952 | } |
| 9953 | |
| 9954 | qsizetype QtPrivate::findString(QLatin1StringView haystack, qsizetype from, QLatin1StringView needle, Qt::CaseSensitivity cs) noexcept |
| 9955 | { |
| 9956 | if (from < 0) |
| 9957 | from += haystack.size(); |
| 9958 | if (from < 0) |
| 9959 | return -1; |
| 9960 | qsizetype adjustedSize = haystack.size() - from; |
| 9961 | if (adjustedSize < needle.size()) |
| 9962 | return -1; |
| 9963 | if (needle.size() == 0) |
| 9964 | return from; |
| 9965 | |
| 9966 | if (cs == Qt::CaseSensitive) { |
| 9967 | |
| 9968 | if (needle.size() == 1) { |
| 9969 | Q_ASSERT(haystack.data() != nullptr); // see size check above |
| 9970 | if (auto it = memchr(s: haystack.data() + from, c: needle.front().toLatin1(), n: adjustedSize)) |
| 9971 | return static_cast<const char *>(it) - haystack.data(); |
| 9972 | return -1; |
| 9973 | } |
| 9974 | |
| 9975 | const QLatin1StringMatcher matcher(needle, Qt::CaseSensitivity::CaseSensitive); |
| 9976 | return matcher.indexIn(haystack, from); |
| 9977 | } |
| 9978 | |
| 9979 | // If the needle is sufficiently small we simply iteratively search through |
| 9980 | // the haystack. When the needle is too long we use a boyer-moore searcher |
| 9981 | // from the standard library, if available. If it is not available then the |
| 9982 | // QLatin1Strings are converted to QString and compared as such. Though |
| 9983 | // initialization is slower the boyer-moore search it employs still makes up |
| 9984 | // for it when haystack and needle are sufficiently long. |
| 9985 | // The needle size was chosen by testing various lengths using the |
| 9986 | // qstringtokenizer benchmark with the |
| 9987 | // "tokenize_qlatin1string_qlatin1string" test. |
| 9988 | #ifdef Q_CC_MSVC |
| 9989 | const qsizetype threshold = 1; |
| 9990 | #else |
| 9991 | const qsizetype threshold = 13; |
| 9992 | #endif |
| 9993 | if (needle.size() <= threshold) { |
| 9994 | const auto begin = haystack.begin(); |
| 9995 | const auto end = haystack.end() - needle.size() + 1; |
| 9996 | auto ciMatch = CaseInsensitiveL1::matcher(ch: needle[0].toLatin1()); |
| 9997 | const qsizetype nlen1 = needle.size() - 1; |
| 9998 | for (auto it = std::find_if(first: begin + from, last: end, pred: ciMatch); it != end; |
| 9999 | it = std::find_if(first: it + 1, last: end, pred: ciMatch)) { |
| 10000 | // In this comparison we skip the first character because we know it's a match |
| 10001 | if (!nlen1 || QLatin1StringView(it + 1, nlen1).compare(other: needle.sliced(pos: 1), cs) == 0) |
| 10002 | return std::distance(first: begin, last: it); |
| 10003 | } |
| 10004 | return -1; |
| 10005 | } |
| 10006 | |
| 10007 | QLatin1StringMatcher matcher(needle, Qt::CaseSensitivity::CaseInsensitive); |
| 10008 | return matcher.indexIn(haystack, from); |
| 10009 | } |
| 10010 | |
| 10011 | qsizetype QtPrivate::lastIndexOf(QStringView haystack, qsizetype from, char16_t needle, Qt::CaseSensitivity cs) noexcept |
| 10012 | { |
| 10013 | return qLastIndexOf(haystack, needle: QChar(needle), from, cs); |
| 10014 | } |
| 10015 | |
| 10016 | qsizetype QtPrivate::lastIndexOf(QStringView haystack, qsizetype from, QStringView needle, Qt::CaseSensitivity cs) noexcept |
| 10017 | { |
| 10018 | return qLastIndexOf(haystack0: haystack, from, needle0: needle, cs); |
| 10019 | } |
| 10020 | |
| 10021 | qsizetype QtPrivate::lastIndexOf(QStringView haystack, qsizetype from, QLatin1StringView needle, Qt::CaseSensitivity cs) noexcept |
| 10022 | { |
| 10023 | return qLastIndexOf(haystack0: haystack, from, needle0: needle, cs); |
| 10024 | } |
| 10025 | |
| 10026 | qsizetype QtPrivate::lastIndexOf(QLatin1StringView haystack, qsizetype from, QStringView needle, Qt::CaseSensitivity cs) noexcept |
| 10027 | { |
| 10028 | return qLastIndexOf(haystack0: haystack, from, needle0: needle, cs); |
| 10029 | } |
| 10030 | |
| 10031 | qsizetype QtPrivate::lastIndexOf(QLatin1StringView haystack, qsizetype from, QLatin1StringView needle, Qt::CaseSensitivity cs) noexcept |
| 10032 | { |
| 10033 | return qLastIndexOf(haystack0: haystack, from, needle0: needle, cs); |
| 10034 | } |
| 10035 | |
| 10036 | #if QT_CONFIG(regularexpression) |
| 10037 | qsizetype QtPrivate::indexOf(QStringView viewHaystack, const QString *stringHaystack, const QRegularExpression &re, qsizetype from, QRegularExpressionMatch *rmatch) |
| 10038 | { |
| 10039 | if (!re.isValid()) { |
| 10040 | qtWarnAboutInvalidRegularExpression(re, cls: "QString(View)" , method: "indexOf" ); |
| 10041 | return -1; |
| 10042 | } |
| 10043 | |
| 10044 | QRegularExpressionMatch match = stringHaystack |
| 10045 | ? re.match(subject: *stringHaystack, offset: from) |
| 10046 | : re.matchView(subjectView: viewHaystack, offset: from); |
| 10047 | if (match.hasMatch()) { |
| 10048 | const qsizetype ret = match.capturedStart(); |
| 10049 | if (rmatch) |
| 10050 | *rmatch = std::move(match); |
| 10051 | return ret; |
| 10052 | } |
| 10053 | |
| 10054 | return -1; |
| 10055 | } |
| 10056 | |
| 10057 | qsizetype QtPrivate::indexOf(QStringView haystack, const QRegularExpression &re, qsizetype from, QRegularExpressionMatch *rmatch) |
| 10058 | { |
| 10059 | return indexOf(viewHaystack: haystack, stringHaystack: nullptr, re, from, rmatch); |
| 10060 | } |
| 10061 | |
| 10062 | qsizetype QtPrivate::lastIndexOf(QStringView viewHaystack, const QString *stringHaystack, const QRegularExpression &re, qsizetype from, QRegularExpressionMatch *rmatch) |
| 10063 | { |
| 10064 | if (!re.isValid()) { |
| 10065 | qtWarnAboutInvalidRegularExpression(re, cls: "QString(View)" , method: "lastIndexOf" ); |
| 10066 | return -1; |
| 10067 | } |
| 10068 | |
| 10069 | qsizetype endpos = (from < 0) ? (viewHaystack.size() + from + 1) : (from + 1); |
| 10070 | QRegularExpressionMatchIterator iterator = stringHaystack |
| 10071 | ? re.globalMatch(subject: *stringHaystack) |
| 10072 | : re.globalMatchView(subjectView: viewHaystack); |
| 10073 | qsizetype lastIndex = -1; |
| 10074 | while (iterator.hasNext()) { |
| 10075 | QRegularExpressionMatch match = iterator.next(); |
| 10076 | qsizetype start = match.capturedStart(); |
| 10077 | if (start < endpos) { |
| 10078 | lastIndex = start; |
| 10079 | if (rmatch) |
| 10080 | *rmatch = std::move(match); |
| 10081 | } else { |
| 10082 | break; |
| 10083 | } |
| 10084 | } |
| 10085 | |
| 10086 | return lastIndex; |
| 10087 | } |
| 10088 | |
| 10089 | qsizetype QtPrivate::lastIndexOf(QStringView haystack, const QRegularExpression &re, qsizetype from, QRegularExpressionMatch *rmatch) |
| 10090 | { |
| 10091 | return lastIndexOf(viewHaystack: haystack, stringHaystack: nullptr, re, from, rmatch); |
| 10092 | } |
| 10093 | |
| 10094 | bool QtPrivate::contains(QStringView viewHaystack, const QString *stringHaystack, const QRegularExpression &re, QRegularExpressionMatch *rmatch) |
| 10095 | { |
| 10096 | if (!re.isValid()) { |
| 10097 | qtWarnAboutInvalidRegularExpression(re, cls: "QString(View)" , method: "contains" ); |
| 10098 | return false; |
| 10099 | } |
| 10100 | QRegularExpressionMatch m = stringHaystack |
| 10101 | ? re.match(subject: *stringHaystack) |
| 10102 | : re.matchView(subjectView: viewHaystack); |
| 10103 | bool hasMatch = m.hasMatch(); |
| 10104 | if (hasMatch && rmatch) |
| 10105 | *rmatch = std::move(m); |
| 10106 | return hasMatch; |
| 10107 | } |
| 10108 | |
| 10109 | bool QtPrivate::contains(QStringView haystack, const QRegularExpression &re, QRegularExpressionMatch *rmatch) |
| 10110 | { |
| 10111 | return contains(viewHaystack: haystack, stringHaystack: nullptr, re, rmatch); |
| 10112 | } |
| 10113 | |
| 10114 | qsizetype QtPrivate::count(QStringView haystack, const QRegularExpression &re) |
| 10115 | { |
| 10116 | if (!re.isValid()) { |
| 10117 | qtWarnAboutInvalidRegularExpression(re, cls: "QString(View)" , method: "count" ); |
| 10118 | return 0; |
| 10119 | } |
| 10120 | qsizetype count = 0; |
| 10121 | qsizetype index = -1; |
| 10122 | qsizetype len = haystack.size(); |
| 10123 | while (index <= len - 1) { |
| 10124 | QRegularExpressionMatch match = re.matchView(subjectView: haystack, offset: index + 1); |
| 10125 | if (!match.hasMatch()) |
| 10126 | break; |
| 10127 | count++; |
| 10128 | |
| 10129 | // Search again, from the next character after the beginning of this |
| 10130 | // capture. If the capture starts with a surrogate pair, both together |
| 10131 | // count as "one character". |
| 10132 | index = match.capturedStart(); |
| 10133 | if (index < len && haystack[index].isHighSurrogate()) |
| 10134 | ++index; |
| 10135 | } |
| 10136 | return count; |
| 10137 | } |
| 10138 | |
| 10139 | #endif // QT_CONFIG(regularexpression) |
| 10140 | |
| 10141 | /*! |
| 10142 | \since 5.0 |
| 10143 | |
| 10144 | Converts a plain text string to an HTML string with |
| 10145 | HTML metacharacters \c{<}, \c{>}, \c{&}, and \c{"} replaced by HTML |
| 10146 | entities. |
| 10147 | |
| 10148 | Example: |
| 10149 | |
| 10150 | \snippet code/src_corelib_text_qstring.cpp 7 |
| 10151 | */ |
| 10152 | QString QString::toHtmlEscaped() const |
| 10153 | { |
| 10154 | const auto pos = std::u16string_view(*this).find_first_of(str: u"<>&\"" ); |
| 10155 | if (pos == std::u16string_view::npos) |
| 10156 | return *this; |
| 10157 | QString rich; |
| 10158 | const qsizetype len = size(); |
| 10159 | rich.reserve(asize: qsizetype(len * 1.1)); |
| 10160 | rich += qToStringViewIgnoringNull(s: *this).first(n: pos); |
| 10161 | for (auto ch : qToStringViewIgnoringNull(s: *this).sliced(pos)) { |
| 10162 | if (ch == u'<') |
| 10163 | rich += "<"_L1 ; |
| 10164 | else if (ch == u'>') |
| 10165 | rich += ">"_L1 ; |
| 10166 | else if (ch == u'&') |
| 10167 | rich += "&"_L1 ; |
| 10168 | else if (ch == u'"') |
| 10169 | rich += """_L1 ; |
| 10170 | else |
| 10171 | rich += ch; |
| 10172 | } |
| 10173 | rich.squeeze(); |
| 10174 | return rich; |
| 10175 | } |
| 10176 | |
| 10177 | /*! |
| 10178 | \macro QStringLiteral(str) |
| 10179 | \relates QString |
| 10180 | |
| 10181 | The macro generates the data for a QString out of the string literal \a str |
| 10182 | at compile time. Creating a QString from it is free in this case, and the |
| 10183 | generated string data is stored in the read-only segment of the compiled |
| 10184 | object file. |
| 10185 | |
| 10186 | If you have code that looks like this: |
| 10187 | |
| 10188 | \snippet code/src_corelib_text_qstring.cpp 9 |
| 10189 | |
| 10190 | then a temporary QString will be created to be passed as the \c{hasAttribute} |
| 10191 | function parameter. This can be quite expensive, as it involves a memory |
| 10192 | allocation and the copy/conversion of the data into QString's internal |
| 10193 | encoding. |
| 10194 | |
| 10195 | This cost can be avoided by using QStringLiteral instead: |
| 10196 | |
| 10197 | \snippet code/src_corelib_text_qstring.cpp 10 |
| 10198 | |
| 10199 | In this case, QString's internal data will be generated at compile time; no |
| 10200 | conversion or allocation will occur at runtime. |
| 10201 | |
| 10202 | Using QStringLiteral instead of a double quoted plain C++ string literal can |
| 10203 | significantly speed up creation of QString instances from data known at |
| 10204 | compile time. |
| 10205 | |
| 10206 | \note QLatin1StringView can still be more efficient than QStringLiteral |
| 10207 | when the string is passed to a function that has an overload taking |
| 10208 | QLatin1StringView and this overload avoids conversion to QString. For |
| 10209 | instance, QString::operator==() can compare to a QLatin1StringView |
| 10210 | directly: |
| 10211 | |
| 10212 | \snippet code/src_corelib_text_qstring.cpp 11 |
| 10213 | |
| 10214 | \note Some compilers have bugs encoding strings containing characters outside |
| 10215 | the US-ASCII character set. Make sure you prefix your string with \c{u} in |
| 10216 | those cases. It is optional otherwise. |
| 10217 | |
| 10218 | \sa QByteArrayLiteral |
| 10219 | */ |
| 10220 | |
| 10221 | #if QT_DEPRECATED_SINCE(6, 8) |
| 10222 | /*! |
| 10223 | \fn QtLiterals::operator""_qs(const char16_t *str, size_t size) |
| 10224 | |
| 10225 | \relates QString |
| 10226 | \since 6.2 |
| 10227 | \deprecated [6.8] Use \c _s from Qt::StringLiterals namespace instead. |
| 10228 | |
| 10229 | Literal operator that creates a QString out of the first \a size characters in |
| 10230 | the char16_t string literal \a str. |
| 10231 | |
| 10232 | The QString is created at compile time, and the generated string data is stored |
| 10233 | in the read-only segment of the compiled object file. Duplicate literals may |
| 10234 | share the same read-only memory. This functionality is interchangeable with |
| 10235 | QStringLiteral, but saves typing when many string literals are present in the |
| 10236 | code. |
| 10237 | |
| 10238 | The following code creates a QString: |
| 10239 | \code |
| 10240 | auto str = u"hello"_qs; |
| 10241 | \endcode |
| 10242 | |
| 10243 | \sa QStringLiteral, QtLiterals::operator""_qba(const char *str, size_t size) |
| 10244 | */ |
| 10245 | #endif // QT_DEPRECATED_SINCE(6, 8) |
| 10246 | |
| 10247 | /*! |
| 10248 | \fn Qt::Literals::StringLiterals::operator""_s(const char16_t *str, size_t size) |
| 10249 | |
| 10250 | \relates QString |
| 10251 | \since 6.4 |
| 10252 | |
| 10253 | Literal operator that creates a QString out of the first \a size characters in |
| 10254 | the char16_t string literal \a str. |
| 10255 | |
| 10256 | The QString is created at compile time, and the generated string data is stored |
| 10257 | in the read-only segment of the compiled object file. Duplicate literals may |
| 10258 | share the same read-only memory. This functionality is interchangeable with |
| 10259 | QStringLiteral, but saves typing when many string literals are present in the |
| 10260 | code. |
| 10261 | |
| 10262 | The following code creates a QString: |
| 10263 | \code |
| 10264 | using namespace Qt::Literals::StringLiterals; |
| 10265 | |
| 10266 | auto str = u"hello"_s; |
| 10267 | \endcode |
| 10268 | |
| 10269 | \sa Qt::Literals::StringLiterals |
| 10270 | */ |
| 10271 | |
| 10272 | /*! |
| 10273 | \internal |
| 10274 | */ |
| 10275 | void QAbstractConcatenable::appendLatin1To(QLatin1StringView in, QChar *out) noexcept |
| 10276 | { |
| 10277 | qt_from_latin1(dst: reinterpret_cast<char16_t *>(out), str: in.data(), size: size_t(in.size())); |
| 10278 | } |
| 10279 | |
| 10280 | /*! |
| 10281 | \fn template <typename T> qsizetype erase(QString &s, const T &t) |
| 10282 | \relates QString |
| 10283 | \since 6.1 |
| 10284 | |
| 10285 | Removes all elements that compare equal to \a t from the |
| 10286 | string \a s. Returns the number of elements removed, if any. |
| 10287 | |
| 10288 | \sa erase_if |
| 10289 | */ |
| 10290 | |
| 10291 | /*! |
| 10292 | \fn template <typename Predicate> qsizetype erase_if(QString &s, Predicate pred) |
| 10293 | \relates QString |
| 10294 | \since 6.1 |
| 10295 | |
| 10296 | Removes all elements for which the predicate \a pred returns true |
| 10297 | from the string \a s. Returns the number of elements removed, if |
| 10298 | any. |
| 10299 | |
| 10300 | \sa erase |
| 10301 | */ |
| 10302 | |
| 10303 | /*! |
| 10304 | \macro const char *qPrintable(const QString &str) |
| 10305 | \relates QString |
| 10306 | |
| 10307 | Returns \a str as a \c{const char *}. This is equivalent to |
| 10308 | \a{str}.toLocal8Bit().\l{QByteArray::}{constData()}. |
| 10309 | |
| 10310 | The char pointer will be invalid after the statement in which |
| 10311 | qPrintable() is used. This is because the array returned by |
| 10312 | QString::toLocal8Bit() will fall out of scope. |
| 10313 | |
| 10314 | \note qDebug(), qInfo(), qWarning(), qCritical(), qFatal() expect |
| 10315 | %s arguments to be UTF-8 encoded, while qPrintable() converts to |
| 10316 | local 8-bit encoding. Therefore qUtf8Printable() should be used |
| 10317 | for logging strings instead of qPrintable(). |
| 10318 | |
| 10319 | \sa qUtf8Printable() |
| 10320 | */ |
| 10321 | |
| 10322 | /*! |
| 10323 | \macro const char *qUtf8Printable(const QString &str) |
| 10324 | \relates QString |
| 10325 | \since 5.4 |
| 10326 | |
| 10327 | Returns \a str as a \c{const char *}. This is equivalent to |
| 10328 | \a{str}.toUtf8().\l{QByteArray::}{constData()}. |
| 10329 | |
| 10330 | The char pointer will be invalid after the statement in which |
| 10331 | qUtf8Printable() is used. This is because the array returned by |
| 10332 | QString::toUtf8() will fall out of scope. |
| 10333 | |
| 10334 | Example: |
| 10335 | |
| 10336 | \snippet code/src_corelib_text_qstring.cpp qUtf8Printable |
| 10337 | |
| 10338 | \sa qPrintable(), qDebug(), qInfo(), qWarning(), qCritical(), qFatal() |
| 10339 | */ |
| 10340 | |
| 10341 | /*! |
| 10342 | \macro const wchar_t *qUtf16Printable(const QString &str) |
| 10343 | \relates QString |
| 10344 | \since 5.7 |
| 10345 | |
| 10346 | Returns \a str as a \c{const ushort *}, but cast to a \c{const wchar_t *} |
| 10347 | to avoid warnings. This is equivalent to \a{str}.utf16() plus some casting. |
| 10348 | |
| 10349 | The only useful thing you can do with the return value of this macro is to |
| 10350 | pass it to QString::asprintf() for use in a \c{%ls} conversion. In particular, |
| 10351 | the return value is \e{not} a valid \c{const wchar_t*}! |
| 10352 | |
| 10353 | In general, the pointer will be invalid after the statement in which |
| 10354 | qUtf16Printable() is used. This is because the pointer may have been |
| 10355 | obtained from a temporary expression, which will fall out of scope. |
| 10356 | |
| 10357 | Example: |
| 10358 | |
| 10359 | \snippet code/src_corelib_text_qstring.cpp qUtf16Printable |
| 10360 | |
| 10361 | \sa qPrintable(), qDebug(), qInfo(), qWarning(), qCritical(), qFatal() |
| 10362 | */ |
| 10363 | |
| 10364 | QT_END_NAMESPACE |
| 10365 | |
| 10366 | #undef REHASH |
| 10367 | |