| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2020 The Qt Company Ltd. |
| 4 | ** Copyright (C) 2018 Intel Corporation. |
| 5 | ** Copyright (C) 2019 Mail.ru Group. |
| 6 | ** Contact: https://www.qt.io/licensing/ |
| 7 | ** |
| 8 | ** This file is part of the QtCore module of the Qt Toolkit. |
| 9 | ** |
| 10 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 11 | ** Commercial License Usage |
| 12 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 13 | ** accordance with the commercial license agreement provided with the |
| 14 | ** Software or, alternatively, in accordance with the terms contained in |
| 15 | ** a written agreement between you and The Qt Company. For licensing terms |
| 16 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 17 | ** information use the contact form at https://www.qt.io/contact-us. |
| 18 | ** |
| 19 | ** GNU Lesser General Public License Usage |
| 20 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 21 | ** General Public License version 3 as published by the Free Software |
| 22 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 23 | ** packaging of this file. Please review the following information to |
| 24 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 25 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 26 | ** |
| 27 | ** GNU General Public License Usage |
| 28 | ** Alternatively, this file may be used under the terms of the GNU |
| 29 | ** General Public License version 2.0 or (at your option) the GNU General |
| 30 | ** Public license version 3 or any later version approved by the KDE Free |
| 31 | ** Qt Foundation. The licenses are as published by the Free Software |
| 32 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 33 | ** included in the packaging of this file. Please review the following |
| 34 | ** information to ensure the GNU General Public License requirements will |
| 35 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 36 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 37 | ** |
| 38 | ** $QT_END_LICENSE$ |
| 39 | ** |
| 40 | ****************************************************************************/ |
| 41 | |
| 42 | #include "qstringlist.h" |
| 43 | #include "qregexp.h" |
| 44 | #if QT_CONFIG(regularexpression) |
| 45 | #include "qregularexpression.h" |
| 46 | #endif |
| 47 | #include "qunicodetables_p.h" |
| 48 | #if QT_CONFIG(textcodec) |
| 49 | #include <qtextcodec.h> |
| 50 | #endif |
| 51 | #include <private/qutfcodec_p.h> |
| 52 | #include "qlocale_tools_p.h" |
| 53 | #include "private/qsimd_p.h" |
| 54 | #include <qnumeric.h> |
| 55 | #include <qdatastream.h> |
| 56 | #include <qlist.h> |
| 57 | #include "qlocale.h" |
| 58 | #include "qlocale_p.h" |
| 59 | #include "qstringbuilder.h" |
| 60 | #include "qstringmatcher.h" |
| 61 | #include "qvarlengtharray.h" |
| 62 | #include "qdebug.h" |
| 63 | #include "qendian.h" |
| 64 | #include "qcollator.h" |
| 65 | |
| 66 | #ifdef Q_OS_MAC |
| 67 | #include <private/qcore_mac_p.h> |
| 68 | #endif |
| 69 | |
| 70 | #include <private/qfunctions_p.h> |
| 71 | |
| 72 | #include <limits.h> |
| 73 | #include <string.h> |
| 74 | #include <stdlib.h> |
| 75 | #include <stdio.h> |
| 76 | #include <stdarg.h> |
| 77 | #include <wchar.h> |
| 78 | |
| 79 | #include "qchar.cpp" |
| 80 | #include "qstringmatcher.cpp" |
| 81 | #include "qstringiterator_p.h" |
| 82 | #include "qstringalgorithms_p.h" |
| 83 | #include "qthreadstorage.h" |
| 84 | |
| 85 | #ifdef Q_OS_WIN |
| 86 | # include <qt_windows.h> |
| 87 | #endif |
| 88 | |
| 89 | #ifdef truncate |
| 90 | # undef truncate |
| 91 | #endif |
| 92 | |
| 93 | #ifndef LLONG_MAX |
| 94 | #define LLONG_MAX qint64_C(9223372036854775807) |
| 95 | #endif |
| 96 | #ifndef LLONG_MIN |
| 97 | #define LLONG_MIN (-LLONG_MAX - qint64_C(1)) |
| 98 | #endif |
| 99 | #ifndef ULLONG_MAX |
| 100 | #define ULLONG_MAX quint64_C(18446744073709551615) |
| 101 | #endif |
| 102 | |
| 103 | #define IS_RAW_DATA(d) ((d)->offset != sizeof(QStringData)) |
| 104 | |
| 105 | QT_BEGIN_NAMESPACE |
| 106 | |
| 107 | /* |
| 108 | * Note on the use of SIMD in qstring.cpp: |
| 109 | * |
| 110 | * Several operations with strings are improved with the use of SIMD code, |
| 111 | * since they are repetitive. For MIPS, we have hand-written assembly code |
| 112 | * outside of qstring.cpp targeting MIPS DSP and MIPS DSPr2. For ARM and for |
| 113 | * x86, we can only use intrinsics and therefore everything is contained in |
| 114 | * qstring.cpp. We need to use intrinsics only for those platforms due to the |
| 115 | * different compilers and toolchains used, which have different syntax for |
| 116 | * assembly sources. |
| 117 | * |
| 118 | * ** SSE notes: ** |
| 119 | * |
| 120 | * Whenever multiple alternatives are equivalent or near so, we prefer the one |
| 121 | * using instructions from SSE2, since SSE2 is guaranteed to be enabled for all |
| 122 | * 64-bit builds and we enable it for 32-bit builds by default. Use of higher |
| 123 | * SSE versions should be done when there is a clear performance benefit and |
| 124 | * requires fallback code to SSE2, if it exists. |
| 125 | * |
| 126 | * Performance measurement in the past shows that most strings are short in |
| 127 | * size and, therefore, do not benefit from alignment prologues. That is, |
| 128 | * trying to find a 16-byte-aligned boundary to operate on is often more |
| 129 | * expensive than executing the unaligned operation directly. In addition, note |
| 130 | * that the QString private data is designed so that the data is stored on |
| 131 | * 16-byte boundaries if the system malloc() returns 16-byte aligned pointers |
| 132 | * on its own (64-bit glibc on Linux does; 32-bit glibc on Linux returns them |
| 133 | * 50% of the time), so skipping the alignment prologue is actually optimizing |
| 134 | * for the common case. |
| 135 | */ |
| 136 | |
| 137 | #if defined(__mips_dsp) |
| 138 | // From qstring_mips_dsp_asm.S |
| 139 | extern "C" void qt_fromlatin1_mips_asm_unroll4 (ushort*, const char*, uint); |
| 140 | extern "C" void qt_fromlatin1_mips_asm_unroll8 (ushort*, const char*, uint); |
| 141 | extern "C" void qt_toLatin1_mips_dsp_asm(uchar *dst, const ushort *src, int length); |
| 142 | #endif |
| 143 | |
| 144 | // internal |
| 145 | qsizetype qFindStringBoyerMoore(QStringView haystack, qsizetype from, QStringView needle, Qt::CaseSensitivity cs); |
| 146 | static inline qsizetype qFindChar(QStringView str, QChar ch, qsizetype from, Qt::CaseSensitivity cs) noexcept; |
| 147 | template <typename Haystack> |
| 148 | static inline qsizetype qLastIndexOf(Haystack haystack, QChar needle, qsizetype from, Qt::CaseSensitivity cs) noexcept; |
| 149 | template <> |
| 150 | inline qsizetype qLastIndexOf(QString haystack, QChar needle, |
| 151 | qsizetype from, Qt::CaseSensitivity cs) noexcept = delete; // unwanted, would detach |
| 152 | static inline qsizetype qt_string_count(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs); |
| 153 | static inline qsizetype qt_string_count(QStringView haystack, QChar needle, Qt::CaseSensitivity cs); |
| 154 | |
| 155 | static inline bool qt_starts_with(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs); |
| 156 | static inline bool qt_starts_with(QStringView haystack, QLatin1String needle, Qt::CaseSensitivity cs); |
| 157 | static inline bool qt_starts_with(QStringView haystack, QChar needle, Qt::CaseSensitivity cs); |
| 158 | static inline bool qt_ends_with(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs); |
| 159 | static inline bool qt_ends_with(QStringView haystack, QLatin1String needle, Qt::CaseSensitivity cs); |
| 160 | static inline bool qt_ends_with(QStringView haystack, QChar needle, Qt::CaseSensitivity cs); |
| 161 | |
| 162 | #if defined(__SSE2__) && defined(Q_CC_GNU) && !defined(Q_CC_INTEL) |
| 163 | # if defined(__SANITIZE_ADDRESS__) && Q_CC_GNU < 800 && !defined(Q_CC_CLANG) |
| 164 | # warning "The __attribute__ on below will likely cause a build failure with your GCC version. Your choices are:" |
| 165 | # warning "1) disable ASan;" |
| 166 | # warning "2) disable the optimized code in qustrlen (change __SSE2__ to anything else);" |
| 167 | # warning "3) upgrade your compiler (preferred)." |
| 168 | # endif |
| 169 | |
| 170 | // We may overrun the buffer, but that's a false positive: |
| 171 | // this won't crash nor produce incorrect results |
| 172 | __attribute__((__no_sanitize_address__)) |
| 173 | #endif |
| 174 | qsizetype QtPrivate::qustrlen(const ushort *str) noexcept |
| 175 | { |
| 176 | qsizetype result = 0; |
| 177 | |
| 178 | #ifdef __SSE2__ |
| 179 | // find the 16-byte alignment immediately prior or equal to str |
| 180 | quintptr misalignment = quintptr(str) & 0xf; |
| 181 | Q_ASSERT((misalignment & 1) == 0); |
| 182 | const ushort *ptr = str - (misalignment / 2); |
| 183 | |
| 184 | // load 16 bytes and see if we have a null |
| 185 | // (aligned loads can never segfault) |
| 186 | const __m128i zeroes = _mm_setzero_si128(); |
| 187 | __m128i data = _mm_load_si128(p: reinterpret_cast<const __m128i *>(ptr)); |
| 188 | __m128i comparison = _mm_cmpeq_epi16(a: data, b: zeroes); |
| 189 | quint32 mask = _mm_movemask_epi8(a: comparison); |
| 190 | |
| 191 | // ignore the result prior to the beginning of str |
| 192 | mask >>= misalignment; |
| 193 | |
| 194 | // Have we found something in the first block? Need to handle it now |
| 195 | // because of the left shift above. |
| 196 | if (mask) |
| 197 | return qCountTrailingZeroBits(v: quint32(mask)) / 2; |
| 198 | |
| 199 | do { |
| 200 | ptr += 8; |
| 201 | data = _mm_load_si128(p: reinterpret_cast<const __m128i *>(ptr)); |
| 202 | |
| 203 | comparison = _mm_cmpeq_epi16(a: data, b: zeroes); |
| 204 | mask = _mm_movemask_epi8(a: comparison); |
| 205 | } while (mask == 0); |
| 206 | |
| 207 | // found a null |
| 208 | uint idx = qCountTrailingZeroBits(v: quint32(mask)); |
| 209 | return ptr - str + idx / 2; |
| 210 | #endif |
| 211 | |
| 212 | if (sizeof(wchar_t) == sizeof(ushort)) |
| 213 | return wcslen(s: reinterpret_cast<const wchar_t *>(str)); |
| 214 | |
| 215 | while (*str++) |
| 216 | ++result; |
| 217 | return result; |
| 218 | } |
| 219 | |
| 220 | #if !defined(__OPTIMIZE_SIZE__) |
| 221 | namespace { |
| 222 | template <uint MaxCount> struct UnrollTailLoop |
| 223 | { |
| 224 | template <typename RetType, typename Functor1, typename Functor2, typename Number> |
| 225 | static inline RetType exec(Number count, RetType returnIfExited, Functor1 loopCheck, Functor2 returnIfFailed, Number i = 0) |
| 226 | { |
| 227 | /* equivalent to: |
| 228 | * while (count--) { |
| 229 | * if (loopCheck(i)) |
| 230 | * return returnIfFailed(i); |
| 231 | * } |
| 232 | * return returnIfExited; |
| 233 | */ |
| 234 | |
| 235 | if (!count) |
| 236 | return returnIfExited; |
| 237 | |
| 238 | bool check = loopCheck(i); |
| 239 | if (check) |
| 240 | return returnIfFailed(i); |
| 241 | |
| 242 | return UnrollTailLoop<MaxCount - 1>::exec(count - 1, returnIfExited, loopCheck, returnIfFailed, i + 1); |
| 243 | } |
| 244 | |
| 245 | template <typename Functor, typename Number> |
| 246 | static inline void exec(Number count, Functor code) |
| 247 | { |
| 248 | /* equivalent to: |
| 249 | * for (Number i = 0; i < count; ++i) |
| 250 | * code(i); |
| 251 | */ |
| 252 | exec(count, 0, [=](Number i) -> bool { code(i); return false; }, [](Number) { return 0; }); |
| 253 | } |
| 254 | }; |
| 255 | template <> template <typename RetType, typename Functor1, typename Functor2, typename Number> |
| 256 | inline RetType UnrollTailLoop<0>::exec(Number, RetType returnIfExited, Functor1, Functor2, Number) |
| 257 | { |
| 258 | return returnIfExited; |
| 259 | } |
| 260 | } |
| 261 | #endif |
| 262 | |
| 263 | /*! |
| 264 | * \internal |
| 265 | * |
| 266 | * Searches for character \a c in the string \a str and returns a pointer to |
| 267 | * it. Unlike strchr() and wcschr() (but like glibc's strchrnul()), if the |
| 268 | * character is not found, this function returns a pointer to the end of the |
| 269 | * string -- that is, \c{str.end()}. |
| 270 | */ |
| 271 | const ushort *QtPrivate::qustrchr(QStringView str, ushort c) noexcept |
| 272 | { |
| 273 | const ushort *n = reinterpret_cast<const ushort *>(str.begin()); |
| 274 | const ushort *e = reinterpret_cast<const ushort *>(str.end()); |
| 275 | |
| 276 | #ifdef __SSE2__ |
| 277 | bool loops = true; |
| 278 | // Using the PMOVMSKB instruction, we get two bits for each character |
| 279 | // we compare. |
| 280 | # if defined(__AVX2__) && !defined(__OPTIMIZE_SIZE__) |
| 281 | // we're going to read n[0..15] (32 bytes) |
| 282 | __m256i mch256 = _mm256_set1_epi32(c | (c << 16)); |
| 283 | for (const ushort *next = n + 16; next <= e; n = next, next += 16) { |
| 284 | __m256i data = _mm256_loadu_si256(reinterpret_cast<const __m256i *>(n)); |
| 285 | __m256i result = _mm256_cmpeq_epi16(data, mch256); |
| 286 | uint mask = uint(_mm256_movemask_epi8(result)); |
| 287 | if (mask) { |
| 288 | uint idx = qCountTrailingZeroBits(mask); |
| 289 | return n + idx / 2; |
| 290 | } |
| 291 | } |
| 292 | loops = false; |
| 293 | __m128i mch = _mm256_castsi256_si128(mch256); |
| 294 | # else |
| 295 | __m128i mch = _mm_set1_epi32(i: c | (c << 16)); |
| 296 | # endif |
| 297 | |
| 298 | auto hasMatch = [mch, &n](__m128i data, ushort validityMask) { |
| 299 | __m128i result = _mm_cmpeq_epi16(a: data, b: mch); |
| 300 | uint mask = uint(_mm_movemask_epi8(a: result)); |
| 301 | if ((mask & validityMask) == 0) |
| 302 | return false; |
| 303 | uint idx = qCountTrailingZeroBits(v: mask); |
| 304 | n += idx / 2; |
| 305 | return true; |
| 306 | }; |
| 307 | |
| 308 | // we're going to read n[0..7] (16 bytes) |
| 309 | for (const ushort *next = n + 8; next <= e; n = next, next += 8) { |
| 310 | __m128i data = _mm_loadu_si128(p: reinterpret_cast<const __m128i *>(n)); |
| 311 | if (hasMatch(data, 0xffff)) |
| 312 | return n; |
| 313 | |
| 314 | if (!loops) { |
| 315 | n += 8; |
| 316 | break; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | # if !defined(__OPTIMIZE_SIZE__) |
| 321 | // we're going to read n[0..3] (8 bytes) |
| 322 | if (e - n > 3) { |
| 323 | __m128i data = _mm_loadl_epi64(p: reinterpret_cast<const __m128i *>(n)); |
| 324 | if (hasMatch(data, 0xff)) |
| 325 | return n; |
| 326 | |
| 327 | n += 4; |
| 328 | } |
| 329 | |
| 330 | return UnrollTailLoop<3>::exec(count: e - n, returnIfExited: e, |
| 331 | loopCheck: [=](int i) { return n[i] == c; }, |
| 332 | returnIfFailed: [=](int i) { return n + i; }); |
| 333 | # endif |
| 334 | #elif defined(__ARM_NEON__) && defined(Q_PROCESSOR_ARM_64) // vaddv is only available on Aarch64 |
| 335 | const uint16x8_t vmask = { 1, 1 << 1, 1 << 2, 1 << 3, 1 << 4, 1 << 5, 1 << 6, 1 << 7 }; |
| 336 | const uint16x8_t ch_vec = vdupq_n_u16(c); |
| 337 | for (const ushort *next = n + 8; next <= e; n = next, next += 8) { |
| 338 | uint16x8_t data = vld1q_u16(n); |
| 339 | uint mask = vaddvq_u16(vandq_u16(vceqq_u16(data, ch_vec), vmask)); |
| 340 | if (ushort(mask)) { |
| 341 | // found a match |
| 342 | return n + qCountTrailingZeroBits(mask); |
| 343 | } |
| 344 | } |
| 345 | #endif // aarch64 |
| 346 | |
| 347 | --n; |
| 348 | while (++n != e) |
| 349 | if (*n == c) |
| 350 | return n; |
| 351 | |
| 352 | return n; |
| 353 | } |
| 354 | |
| 355 | #ifdef __SSE2__ |
| 356 | // Scans from \a ptr to \a end until \a maskval is non-zero. Returns true if |
| 357 | // the no non-zero was found. Returns false and updates \a ptr to point to the |
| 358 | // first 16-bit word that has any bit set (note: if the input is 8-bit, \a ptr |
| 359 | // may be updated to one byte short). |
| 360 | static bool simdTestMask(const char *&ptr, const char *end, quint32 maskval) |
| 361 | { |
| 362 | auto updatePtr = [&](uint result) { |
| 363 | // found a character matching the mask |
| 364 | uint idx = qCountTrailingZeroBits(v: ~result); |
| 365 | ptr += idx; |
| 366 | return false; |
| 367 | }; |
| 368 | |
| 369 | # if defined(__SSE4_1__) |
| 370 | __m128i mask; |
| 371 | auto updatePtrSimd = [&](__m128i data) { |
| 372 | __m128i masked = _mm_and_si128(mask, data); |
| 373 | __m128i comparison = _mm_cmpeq_epi16(masked, _mm_setzero_si128()); |
| 374 | uint result = _mm_movemask_epi8(comparison); |
| 375 | return updatePtr(result); |
| 376 | }; |
| 377 | |
| 378 | # if defined(__AVX2__) |
| 379 | // AVX2 implementation: test 32 bytes at a time |
| 380 | const __m256i mask256 = _mm256_broadcastd_epi32(_mm_cvtsi32_si128(maskval)); |
| 381 | while (ptr + 32 <= end) { |
| 382 | __m256i data = _mm256_loadu_si256(reinterpret_cast<const __m256i *>(ptr)); |
| 383 | if (!_mm256_testz_si256(mask256, data)) { |
| 384 | // found a character matching the mask |
| 385 | __m256i masked256 = _mm256_and_si256(mask256, data); |
| 386 | __m256i comparison256 = _mm256_cmpeq_epi16(masked256, _mm256_setzero_si256()); |
| 387 | return updatePtr(_mm256_movemask_epi8(comparison256)); |
| 388 | } |
| 389 | ptr += 32; |
| 390 | } |
| 391 | |
| 392 | mask = _mm256_castsi256_si128(mask256); |
| 393 | # else |
| 394 | // SSE 4.1 implementation: test 32 bytes at a time (two 16-byte |
| 395 | // comparisons, unrolled) |
| 396 | mask = _mm_set1_epi32(maskval); |
| 397 | while (ptr + 32 <= end) { |
| 398 | __m128i data1 = _mm_loadu_si128(reinterpret_cast<const __m128i *>(ptr)); |
| 399 | __m128i data2 = _mm_loadu_si128(reinterpret_cast<const __m128i *>(ptr + 16)); |
| 400 | if (!_mm_testz_si128(mask, data1)) |
| 401 | return updatePtrSimd(data1); |
| 402 | |
| 403 | ptr += 16; |
| 404 | if (!_mm_testz_si128(mask, data2)) |
| 405 | return updatePtrSimd(data2); |
| 406 | ptr += 16; |
| 407 | } |
| 408 | # endif |
| 409 | |
| 410 | // AVX2 and SSE4.1: final 16-byte comparison |
| 411 | if (ptr + 16 <= end) { |
| 412 | __m128i data1 = _mm_loadu_si128(reinterpret_cast<const __m128i *>(ptr)); |
| 413 | if (!_mm_testz_si128(mask, data1)) |
| 414 | return updatePtrSimd(data1); |
| 415 | ptr += 16; |
| 416 | } |
| 417 | |
| 418 | // and final 8-byte comparison |
| 419 | if (ptr + 8 <= end) { |
| 420 | __m128i data1 = _mm_loadl_epi64(reinterpret_cast<const __m128i *>(ptr)); |
| 421 | if (!_mm_testz_si128(mask, data1)) |
| 422 | return updatePtrSimd(data1); |
| 423 | ptr += 8; |
| 424 | } |
| 425 | |
| 426 | # else |
| 427 | // SSE2 implementation: test 16 bytes at a time. |
| 428 | const __m128i mask = _mm_set1_epi32(i: maskval); |
| 429 | while (ptr + 16 <= end) { |
| 430 | __m128i data = _mm_loadu_si128(p: reinterpret_cast<const __m128i *>(ptr)); |
| 431 | __m128i masked = _mm_and_si128(a: mask, b: data); |
| 432 | __m128i comparison = _mm_cmpeq_epi16(a: masked, b: _mm_setzero_si128()); |
| 433 | quint16 result = _mm_movemask_epi8(a: comparison); |
| 434 | if (result != 0xffff) |
| 435 | return updatePtr(result); |
| 436 | ptr += 16; |
| 437 | } |
| 438 | |
| 439 | // and one 8-byte comparison |
| 440 | if (ptr + 8 <= end) { |
| 441 | __m128i data = _mm_loadl_epi64(p: reinterpret_cast<const __m128i *>(ptr)); |
| 442 | __m128i masked = _mm_and_si128(a: mask, b: data); |
| 443 | __m128i comparison = _mm_cmpeq_epi16(a: masked, b: _mm_setzero_si128()); |
| 444 | quint8 result = _mm_movemask_epi8(a: comparison); |
| 445 | if (result != 0xff) |
| 446 | return updatePtr(result); |
| 447 | ptr += 8; |
| 448 | } |
| 449 | # endif |
| 450 | |
| 451 | return true; |
| 452 | } |
| 453 | |
| 454 | static Q_ALWAYS_INLINE __m128i mm_load8_zero_extend(const void *ptr) |
| 455 | { |
| 456 | const __m128i *dataptr = static_cast<const __m128i *>(ptr); |
| 457 | #if defined(__SSE4_1__) |
| 458 | // use a MOVQ followed by PMOVZXBW |
| 459 | // if AVX2 is present, these should combine into a single VPMOVZXBW instruction |
| 460 | __m128i data = _mm_loadl_epi64(dataptr); |
| 461 | return _mm_cvtepu8_epi16(data); |
| 462 | # else |
| 463 | // use MOVQ followed by PUNPCKLBW |
| 464 | __m128i data = _mm_loadl_epi64(p: dataptr); |
| 465 | return _mm_unpacklo_epi8(a: data, b: _mm_setzero_si128()); |
| 466 | # endif |
| 467 | } |
| 468 | #endif |
| 469 | |
| 470 | // Note: ptr on output may be off by one and point to a preceding US-ASCII |
| 471 | // character. Usually harmless. |
| 472 | bool qt_is_ascii(const char *&ptr, const char *end) noexcept |
| 473 | { |
| 474 | #if defined(__SSE2__) |
| 475 | // Testing for the high bit can be done efficiently with just PMOVMSKB |
| 476 | # if defined(__AVX2__) |
| 477 | while (ptr + 32 <= end) { |
| 478 | __m256i data = _mm256_loadu_si256(reinterpret_cast<const __m256i *>(ptr)); |
| 479 | quint32 mask = _mm256_movemask_epi8(data); |
| 480 | if (mask) { |
| 481 | uint idx = qCountTrailingZeroBits(mask); |
| 482 | ptr += idx; |
| 483 | return false; |
| 484 | } |
| 485 | ptr += 32; |
| 486 | } |
| 487 | # endif |
| 488 | while (ptr + 16 <= end) { |
| 489 | __m128i data = _mm_loadu_si128(p: reinterpret_cast<const __m128i *>(ptr)); |
| 490 | quint32 mask = _mm_movemask_epi8(a: data); |
| 491 | if (mask) { |
| 492 | uint idx = qCountTrailingZeroBits(v: mask); |
| 493 | ptr += idx; |
| 494 | return false; |
| 495 | } |
| 496 | ptr += 16; |
| 497 | } |
| 498 | if (ptr + 8 <= end) { |
| 499 | __m128i data = _mm_loadl_epi64(p: reinterpret_cast<const __m128i *>(ptr)); |
| 500 | quint8 mask = _mm_movemask_epi8(a: data); |
| 501 | if (mask) { |
| 502 | uint idx = qCountTrailingZeroBits(v: mask); |
| 503 | ptr += idx; |
| 504 | return false; |
| 505 | } |
| 506 | ptr += 8; |
| 507 | } |
| 508 | #endif |
| 509 | |
| 510 | while (ptr + 4 <= end) { |
| 511 | quint32 data = qFromUnaligned<quint32>(src: ptr); |
| 512 | if (data &= 0x80808080U) { |
| 513 | #if Q_BYTE_ORDER == Q_BIG_ENDIAN |
| 514 | uint idx = qCountLeadingZeroBits(data); |
| 515 | #else |
| 516 | uint idx = qCountTrailingZeroBits(v: data); |
| 517 | #endif |
| 518 | ptr += idx / 8; |
| 519 | return false; |
| 520 | } |
| 521 | ptr += 4; |
| 522 | } |
| 523 | |
| 524 | while (ptr != end) { |
| 525 | if (quint8(*ptr) & 0x80) |
| 526 | return false; |
| 527 | ++ptr; |
| 528 | } |
| 529 | return true; |
| 530 | } |
| 531 | |
| 532 | bool QtPrivate::isAscii(QLatin1String s) noexcept |
| 533 | { |
| 534 | const char *ptr = s.begin(); |
| 535 | const char *end = s.end(); |
| 536 | |
| 537 | return qt_is_ascii(ptr, end); |
| 538 | } |
| 539 | |
| 540 | static bool isAscii(const QChar *&ptr, const QChar *end) |
| 541 | { |
| 542 | #ifdef __SSE2__ |
| 543 | const char *ptr8 = reinterpret_cast<const char *>(ptr); |
| 544 | const char *end8 = reinterpret_cast<const char *>(end); |
| 545 | bool ok = simdTestMask(ptr&: ptr8, end: end8, maskval: 0xff80ff80); |
| 546 | ptr = reinterpret_cast<const QChar *>(ptr8); |
| 547 | if (!ok) |
| 548 | return false; |
| 549 | #endif |
| 550 | |
| 551 | while (ptr != end) { |
| 552 | if (ptr->unicode() & 0xff80) |
| 553 | return false; |
| 554 | ++ptr; |
| 555 | } |
| 556 | return true; |
| 557 | } |
| 558 | |
| 559 | bool QtPrivate::isAscii(QStringView s) noexcept |
| 560 | { |
| 561 | const QChar *ptr = s.begin(); |
| 562 | const QChar *end = s.end(); |
| 563 | |
| 564 | return isAscii(ptr, end); |
| 565 | } |
| 566 | |
| 567 | bool QtPrivate::isLatin1(QStringView s) noexcept |
| 568 | { |
| 569 | const QChar *ptr = s.begin(); |
| 570 | const QChar *end = s.end(); |
| 571 | |
| 572 | #ifdef __SSE2__ |
| 573 | const char *ptr8 = reinterpret_cast<const char *>(ptr); |
| 574 | const char *end8 = reinterpret_cast<const char *>(end); |
| 575 | if (!simdTestMask(ptr&: ptr8, end: end8, maskval: 0xff00ff00)) |
| 576 | return false; |
| 577 | ptr = reinterpret_cast<const QChar *>(ptr8); |
| 578 | #endif |
| 579 | |
| 580 | while (ptr != end) { |
| 581 | if ((*ptr++).unicode() > 0xff) |
| 582 | return false; |
| 583 | } |
| 584 | return true; |
| 585 | } |
| 586 | |
| 587 | bool QtPrivate::isValidUtf16(QStringView s) noexcept |
| 588 | { |
| 589 | Q_CONSTEXPR uint InvalidCodePoint = UINT_MAX; |
| 590 | |
| 591 | QStringIterator i(s); |
| 592 | while (i.hasNext()) { |
| 593 | uint c = i.next(invalidAs: InvalidCodePoint); |
| 594 | if (c == InvalidCodePoint) |
| 595 | return false; |
| 596 | } |
| 597 | |
| 598 | return true; |
| 599 | } |
| 600 | |
| 601 | // conversion between Latin 1 and UTF-16 |
| 602 | void qt_from_latin1(ushort *dst, const char *str, size_t size) noexcept |
| 603 | { |
| 604 | /* SIMD: |
| 605 | * Unpacking with SSE has been shown to improve performance on recent CPUs |
| 606 | * The same method gives no improvement with NEON. On Aarch64, clang will do the vectorization |
| 607 | * itself in exactly the same way as one would do it with intrinsics. |
| 608 | */ |
| 609 | #if defined(__SSE2__) |
| 610 | const char *e = str + size; |
| 611 | qptrdiff offset = 0; |
| 612 | |
| 613 | // we're going to read str[offset..offset+15] (16 bytes) |
| 614 | for ( ; str + offset + 15 < e; offset += 16) { |
| 615 | const __m128i chunk = _mm_loadu_si128(p: (const __m128i*)(str + offset)); // load |
| 616 | #ifdef __AVX2__ |
| 617 | // zero extend to an YMM register |
| 618 | const __m256i extended = _mm256_cvtepu8_epi16(chunk); |
| 619 | |
| 620 | // store |
| 621 | _mm256_storeu_si256((__m256i*)(dst + offset), extended); |
| 622 | #else |
| 623 | const __m128i nullMask = _mm_set1_epi32(i: 0); |
| 624 | |
| 625 | // unpack the first 8 bytes, padding with zeros |
| 626 | const __m128i firstHalf = _mm_unpacklo_epi8(a: chunk, b: nullMask); |
| 627 | _mm_storeu_si128(p: (__m128i*)(dst + offset), b: firstHalf); // store |
| 628 | |
| 629 | // unpack the last 8 bytes, padding with zeros |
| 630 | const __m128i secondHalf = _mm_unpackhi_epi8 (a: chunk, b: nullMask); |
| 631 | _mm_storeu_si128(p: (__m128i*)(dst + offset + 8), b: secondHalf); // store |
| 632 | #endif |
| 633 | } |
| 634 | |
| 635 | // we're going to read str[offset..offset+7] (8 bytes) |
| 636 | if (str + offset + 7 < e) { |
| 637 | const __m128i unpacked = mm_load8_zero_extend(ptr: str + offset); |
| 638 | _mm_storeu_si128(p: reinterpret_cast<__m128i *>(dst + offset), b: unpacked); |
| 639 | offset += 8; |
| 640 | } |
| 641 | |
| 642 | size = size % 8; |
| 643 | dst += offset; |
| 644 | str += offset; |
| 645 | # if !defined(__OPTIMIZE_SIZE__) |
| 646 | return UnrollTailLoop<7>::exec(count: int(size), code: [=](int i) { dst[i] = (uchar)str[i]; }); |
| 647 | # endif |
| 648 | #endif |
| 649 | #if defined(__mips_dsp) |
| 650 | if (size > 20) |
| 651 | qt_fromlatin1_mips_asm_unroll8(dst, str, size); |
| 652 | else |
| 653 | qt_fromlatin1_mips_asm_unroll4(dst, str, size); |
| 654 | #else |
| 655 | while (size--) |
| 656 | *dst++ = (uchar)*str++; |
| 657 | #endif |
| 658 | } |
| 659 | |
| 660 | template <bool Checked> |
| 661 | static void qt_to_latin1_internal(uchar *dst, const ushort *src, qsizetype length) |
| 662 | { |
| 663 | #if defined(__SSE2__) |
| 664 | uchar *e = dst + length; |
| 665 | qptrdiff offset = 0; |
| 666 | |
| 667 | # ifdef __AVX2__ |
| 668 | const __m256i questionMark256 = _mm256_broadcastw_epi16(_mm_cvtsi32_si128('?')); |
| 669 | const __m256i outOfRange256 = _mm256_broadcastw_epi16(_mm_cvtsi32_si128(0x100)); |
| 670 | const __m128i questionMark = _mm256_castsi256_si128(questionMark256); |
| 671 | const __m128i outOfRange = _mm256_castsi256_si128(outOfRange256); |
| 672 | # else |
| 673 | const __m128i questionMark = _mm_set1_epi16(w: '?'); |
| 674 | const __m128i outOfRange = _mm_set1_epi16(w: 0x100); |
| 675 | # endif |
| 676 | |
| 677 | auto mergeQuestionMarks = [=](__m128i chunk) { |
| 678 | // SSE has no compare instruction for unsigned comparison. |
| 679 | # ifdef __SSE4_1__ |
| 680 | // We use an unsigned uc = qMin(uc, 0x100) and then compare for equality. |
| 681 | chunk = _mm_min_epu16(chunk, outOfRange); |
| 682 | const __m128i offLimitMask = _mm_cmpeq_epi16(chunk, outOfRange); |
| 683 | chunk = _mm_blendv_epi8(chunk, questionMark, offLimitMask); |
| 684 | # else |
| 685 | // The variables must be shiffted + 0x8000 to be compared |
| 686 | const __m128i signedBitOffset = _mm_set1_epi16(w: short(0x8000)); |
| 687 | const __m128i thresholdMask = _mm_set1_epi16(w: short(0xff + 0x8000)); |
| 688 | |
| 689 | const __m128i signedChunk = _mm_add_epi16(a: chunk, b: signedBitOffset); |
| 690 | const __m128i offLimitMask = _mm_cmpgt_epi16(a: signedChunk, b: thresholdMask); |
| 691 | |
| 692 | // offLimitQuestionMark contains '?' for each 16 bits that was off-limit |
| 693 | // the 16 bits that were correct contains zeros |
| 694 | const __m128i offLimitQuestionMark = _mm_and_si128(a: offLimitMask, b: questionMark); |
| 695 | |
| 696 | // correctBytes contains the bytes that were in limit |
| 697 | // the 16 bits that were off limits contains zeros |
| 698 | const __m128i correctBytes = _mm_andnot_si128(a: offLimitMask, b: chunk); |
| 699 | |
| 700 | // merge offLimitQuestionMark and correctBytes to have the result |
| 701 | chunk = _mm_or_si128(a: correctBytes, b: offLimitQuestionMark); |
| 702 | |
| 703 | Q_UNUSED(outOfRange); |
| 704 | # endif |
| 705 | return chunk; |
| 706 | }; |
| 707 | |
| 708 | // we're going to write to dst[offset..offset+15] (16 bytes) |
| 709 | for ( ; dst + offset + 15 < e; offset += 16) { |
| 710 | # if defined(__AVX2__) |
| 711 | __m256i chunk = _mm256_loadu_si256(reinterpret_cast<const __m256i *>(src + offset)); |
| 712 | if (Checked) { |
| 713 | // See mergeQuestionMarks lambda above for details |
| 714 | chunk = _mm256_min_epu16(chunk, outOfRange256); |
| 715 | const __m256i offLimitMask = _mm256_cmpeq_epi16(chunk, outOfRange256); |
| 716 | chunk = _mm256_blendv_epi8(chunk, questionMark256, offLimitMask); |
| 717 | } |
| 718 | |
| 719 | const __m128i chunk2 = _mm256_extracti128_si256(chunk, 1); |
| 720 | const __m128i chunk1 = _mm256_castsi256_si128(chunk); |
| 721 | # else |
| 722 | __m128i chunk1 = _mm_loadu_si128(p: (const __m128i*)(src + offset)); // load |
| 723 | if (Checked) |
| 724 | chunk1 = mergeQuestionMarks(chunk1); |
| 725 | |
| 726 | __m128i chunk2 = _mm_loadu_si128(p: (const __m128i*)(src + offset + 8)); // load |
| 727 | if (Checked) |
| 728 | chunk2 = mergeQuestionMarks(chunk2); |
| 729 | # endif |
| 730 | |
| 731 | // pack the two vector to 16 x 8bits elements |
| 732 | const __m128i result = _mm_packus_epi16(a: chunk1, b: chunk2); |
| 733 | _mm_storeu_si128(p: (__m128i*)(dst + offset), b: result); // store |
| 734 | } |
| 735 | |
| 736 | # if !defined(__OPTIMIZE_SIZE__) |
| 737 | // we're going to write to dst[offset..offset+7] (8 bytes) |
| 738 | if (dst + offset + 7 < e) { |
| 739 | __m128i chunk = _mm_loadu_si128(p: reinterpret_cast<const __m128i *>(src + offset)); |
| 740 | if (Checked) |
| 741 | chunk = mergeQuestionMarks(chunk); |
| 742 | |
| 743 | // pack, where the upper half is ignored |
| 744 | const __m128i result = _mm_packus_epi16(a: chunk, b: chunk); |
| 745 | _mm_storel_epi64(p: reinterpret_cast<__m128i *>(dst + offset), a: result); |
| 746 | offset += 8; |
| 747 | } |
| 748 | |
| 749 | // we're going to write to dst[offset..offset+3] (4 bytes) |
| 750 | if (dst + offset + 3 < e) { |
| 751 | __m128i chunk = _mm_loadl_epi64(p: reinterpret_cast<const __m128i *>(src + offset)); |
| 752 | if (Checked) |
| 753 | chunk = mergeQuestionMarks(chunk); |
| 754 | |
| 755 | // pack, we'll the upper three quarters |
| 756 | const __m128i result = _mm_packus_epi16(a: chunk, b: chunk); |
| 757 | qToUnaligned(src: _mm_cvtsi128_si32(a: result), dest: dst + offset); |
| 758 | offset += 4; |
| 759 | } |
| 760 | |
| 761 | length = length % 4; |
| 762 | # else |
| 763 | length = length % 16; |
| 764 | # endif // optimize size |
| 765 | |
| 766 | // advance dst, src for tail processing |
| 767 | dst += offset; |
| 768 | src += offset; |
| 769 | |
| 770 | # if !defined(__OPTIMIZE_SIZE__) |
| 771 | return UnrollTailLoop<3>::exec(length, [=](int i) { |
| 772 | if (Checked) |
| 773 | dst[i] = (src[i]>0xff) ? '?' : (uchar) src[i]; |
| 774 | else |
| 775 | dst[i] = src[i]; |
| 776 | }); |
| 777 | # endif |
| 778 | #elif defined(__ARM_NEON__) |
| 779 | // Refer to the documentation of the SSE2 implementation |
| 780 | // this use eactly the same method as for SSE except: |
| 781 | // 1) neon has unsigned comparison |
| 782 | // 2) packing is done to 64 bits (8 x 8bits component). |
| 783 | if (length >= 16) { |
| 784 | const int chunkCount = length >> 3; // divided by 8 |
| 785 | const uint16x8_t questionMark = vdupq_n_u16('?'); // set |
| 786 | const uint16x8_t thresholdMask = vdupq_n_u16(0xff); // set |
| 787 | for (int i = 0; i < chunkCount; ++i) { |
| 788 | uint16x8_t chunk = vld1q_u16((uint16_t *)src); // load |
| 789 | src += 8; |
| 790 | |
| 791 | if (Checked) { |
| 792 | const uint16x8_t offLimitMask = vcgtq_u16(chunk, thresholdMask); // chunk > thresholdMask |
| 793 | const uint16x8_t offLimitQuestionMark = vandq_u16(offLimitMask, questionMark); // offLimitMask & questionMark |
| 794 | const uint16x8_t correctBytes = vbicq_u16(chunk, offLimitMask); // !offLimitMask & chunk |
| 795 | chunk = vorrq_u16(correctBytes, offLimitQuestionMark); // correctBytes | offLimitQuestionMark |
| 796 | } |
| 797 | const uint8x8_t result = vmovn_u16(chunk); // narrowing move->packing |
| 798 | vst1_u8(dst, result); // store |
| 799 | dst += 8; |
| 800 | } |
| 801 | length = length % 8; |
| 802 | } |
| 803 | #endif |
| 804 | #if defined(__mips_dsp) |
| 805 | qt_toLatin1_mips_dsp_asm(dst, src, length); |
| 806 | #else |
| 807 | while (length--) { |
| 808 | if (Checked) |
| 809 | *dst++ = (*src>0xff) ? '?' : (uchar) *src; |
| 810 | else |
| 811 | *dst++ = *src; |
| 812 | ++src; |
| 813 | } |
| 814 | #endif |
| 815 | } |
| 816 | |
| 817 | static void qt_to_latin1(uchar *dst, const ushort *src, qsizetype length) |
| 818 | { |
| 819 | qt_to_latin1_internal<true>(dst, src, length); |
| 820 | } |
| 821 | |
| 822 | void qt_to_latin1_unchecked(uchar *dst, const ushort *src, qsizetype length) |
| 823 | { |
| 824 | qt_to_latin1_internal<false>(dst, src, length); |
| 825 | } |
| 826 | |
| 827 | // Unicode case-insensitive comparison |
| 828 | static int ucstricmp(const QChar *a, const QChar *ae, const QChar *b, const QChar *be) |
| 829 | { |
| 830 | if (a == b) |
| 831 | return (ae - be); |
| 832 | |
| 833 | const QChar *e = ae; |
| 834 | if (be - b < ae - a) |
| 835 | e = a + (be - b); |
| 836 | |
| 837 | uint alast = 0; |
| 838 | uint blast = 0; |
| 839 | while (a < e) { |
| 840 | // qDebug() << Qt::hex << alast << blast; |
| 841 | // qDebug() << Qt::hex << "*a=" << *a << "alast=" << alast << "folded=" << foldCase (*a, alast); |
| 842 | // qDebug() << Qt::hex << "*b=" << *b << "blast=" << blast << "folded=" << foldCase (*b, blast); |
| 843 | int diff = foldCase(ch: a->unicode(), last&: alast) - foldCase(ch: b->unicode(), last&: blast); |
| 844 | if ((diff)) |
| 845 | return diff; |
| 846 | ++a; |
| 847 | ++b; |
| 848 | } |
| 849 | if (a == ae) { |
| 850 | if (b == be) |
| 851 | return 0; |
| 852 | return -1; |
| 853 | } |
| 854 | return 1; |
| 855 | } |
| 856 | |
| 857 | // Case-insensitive comparison between a Unicode string and a QLatin1String |
| 858 | static int ucstricmp(const QChar *a, const QChar *ae, const char *b, const char *be) |
| 859 | { |
| 860 | auto e = ae; |
| 861 | if (be - b < ae - a) |
| 862 | e = a + (be - b); |
| 863 | |
| 864 | while (a < e) { |
| 865 | int diff = foldCase(ch: a->unicode()) - foldCase(ch: uchar(*b)); |
| 866 | if ((diff)) |
| 867 | return diff; |
| 868 | ++a; |
| 869 | ++b; |
| 870 | } |
| 871 | if (a == ae) { |
| 872 | if (b == be) |
| 873 | return 0; |
| 874 | return -1; |
| 875 | } |
| 876 | return 1; |
| 877 | } |
| 878 | |
| 879 | #if defined(__mips_dsp) |
| 880 | // From qstring_mips_dsp_asm.S |
| 881 | extern "C" int qt_ucstrncmp_mips_dsp_asm(const ushort *a, |
| 882 | const ushort *b, |
| 883 | unsigned len); |
| 884 | #endif |
| 885 | |
| 886 | // Unicode case-sensitive compare two same-sized strings |
| 887 | static int ucstrncmp(const QChar *a, const QChar *b, size_t l) |
| 888 | { |
| 889 | #ifdef __OPTIMIZE_SIZE__ |
| 890 | const QChar *end = a + l; |
| 891 | while (a < end) { |
| 892 | if (int diff = (int)a->unicode() - (int)b->unicode()) |
| 893 | return diff; |
| 894 | ++a; |
| 895 | ++b; |
| 896 | } |
| 897 | return 0; |
| 898 | #else |
| 899 | #if defined(__mips_dsp) |
| 900 | Q_STATIC_ASSERT(sizeof(uint) == sizeof(size_t)); |
| 901 | if (l >= 8) { |
| 902 | return qt_ucstrncmp_mips_dsp_asm(reinterpret_cast<const ushort*>(a), |
| 903 | reinterpret_cast<const ushort*>(b), |
| 904 | l); |
| 905 | } |
| 906 | #endif // __mips_dsp |
| 907 | #ifdef __SSE2__ |
| 908 | const QChar *end = a + l; |
| 909 | qptrdiff offset = 0; |
| 910 | |
| 911 | // Using the PMOVMSKB instruction, we get two bits for each character |
| 912 | // we compare. |
| 913 | int retval; |
| 914 | auto isDifferent = [a, b, &offset, &retval](__m128i a_data, __m128i b_data) { |
| 915 | __m128i result = _mm_cmpeq_epi16(a: a_data, b: b_data); |
| 916 | uint mask = ~uint(_mm_movemask_epi8(a: result)); |
| 917 | if (ushort(mask) == 0) |
| 918 | return false; |
| 919 | uint idx = qCountTrailingZeroBits(v: mask); |
| 920 | retval = a[offset + idx / 2].unicode() - b[offset + idx / 2].unicode(); |
| 921 | return true; |
| 922 | }; |
| 923 | |
| 924 | // we're going to read a[0..15] and b[0..15] (32 bytes) |
| 925 | for ( ; end - a >= offset + 16; offset += 16) { |
| 926 | #ifdef __AVX2__ |
| 927 | __m256i a_data = _mm256_loadu_si256(reinterpret_cast<const __m256i *>(a + offset)); |
| 928 | __m256i b_data = _mm256_loadu_si256(reinterpret_cast<const __m256i *>(b + offset)); |
| 929 | __m256i result = _mm256_cmpeq_epi16(a_data, b_data); |
| 930 | uint mask = _mm256_movemask_epi8(result); |
| 931 | #else |
| 932 | __m128i a_data1 = _mm_loadu_si128(p: reinterpret_cast<const __m128i *>(a + offset)); |
| 933 | __m128i a_data2 = _mm_loadu_si128(p: reinterpret_cast<const __m128i *>(a + offset + 8)); |
| 934 | __m128i b_data1 = _mm_loadu_si128(p: reinterpret_cast<const __m128i *>(b + offset)); |
| 935 | __m128i b_data2 = _mm_loadu_si128(p: reinterpret_cast<const __m128i *>(b + offset + 8)); |
| 936 | __m128i result1 = _mm_cmpeq_epi16(a: a_data1, b: b_data1); |
| 937 | __m128i result2 = _mm_cmpeq_epi16(a: a_data2, b: b_data2); |
| 938 | uint mask = _mm_movemask_epi8(a: result1) | (_mm_movemask_epi8(a: result2) << 16); |
| 939 | #endif |
| 940 | mask = ~mask; |
| 941 | if (mask) { |
| 942 | // found a different character |
| 943 | uint idx = qCountTrailingZeroBits(v: mask); |
| 944 | return a[offset + idx / 2].unicode() - b[offset + idx / 2].unicode(); |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | // we're going to read a[0..7] and b[0..7] (16 bytes) |
| 949 | if (end - a >= offset + 8) { |
| 950 | __m128i a_data = _mm_loadu_si128(p: reinterpret_cast<const __m128i *>(a + offset)); |
| 951 | __m128i b_data = _mm_loadu_si128(p: reinterpret_cast<const __m128i *>(b + offset)); |
| 952 | if (isDifferent(a_data, b_data)) |
| 953 | return retval; |
| 954 | |
| 955 | offset += 8; |
| 956 | } |
| 957 | |
| 958 | // we're going to read a[0..3] and b[0..3] (8 bytes) |
| 959 | if (end - a >= offset + 4) { |
| 960 | __m128i a_data = _mm_loadl_epi64(p: reinterpret_cast<const __m128i *>(a + offset)); |
| 961 | __m128i b_data = _mm_loadl_epi64(p: reinterpret_cast<const __m128i *>(b + offset)); |
| 962 | if (isDifferent(a_data, b_data)) |
| 963 | return retval; |
| 964 | |
| 965 | offset += 4; |
| 966 | } |
| 967 | |
| 968 | // reset l |
| 969 | l &= 3; |
| 970 | |
| 971 | const auto lambda = [=](size_t i) -> int { |
| 972 | return a[offset + i].unicode() - b[offset + i].unicode(); |
| 973 | }; |
| 974 | return UnrollTailLoop<3>::exec(count: l, returnIfExited: 0, loopCheck: lambda, returnIfFailed: lambda); |
| 975 | #endif |
| 976 | #if defined(__ARM_NEON__) && defined(Q_PROCESSOR_ARM_64) // vaddv is only available on Aarch64 |
| 977 | if (l >= 8) { |
| 978 | const QChar *end = a + l; |
| 979 | const uint16x8_t mask = { 1, 1 << 1, 1 << 2, 1 << 3, 1 << 4, 1 << 5, 1 << 6, 1 << 7 }; |
| 980 | while (end - a > 7) { |
| 981 | uint16x8_t da = vld1q_u16(reinterpret_cast<const uint16_t *>(a)); |
| 982 | uint16x8_t db = vld1q_u16(reinterpret_cast<const uint16_t *>(b)); |
| 983 | |
| 984 | uint8_t r = ~(uint8_t)vaddvq_u16(vandq_u16(vceqq_u16(da, db), mask)); |
| 985 | if (r) { |
| 986 | // found a different QChar |
| 987 | uint idx = qCountTrailingZeroBits(r); |
| 988 | return (int)a[idx].unicode() - (int)b[idx].unicode(); |
| 989 | } |
| 990 | a += 8; |
| 991 | b += 8; |
| 992 | } |
| 993 | l &= 7; |
| 994 | } |
| 995 | const auto lambda = [=](size_t i) -> int { |
| 996 | return a[i].unicode() - b[i].unicode(); |
| 997 | }; |
| 998 | return UnrollTailLoop<7>::exec(l, 0, lambda, lambda); |
| 999 | #endif // __ARM_NEON__ |
| 1000 | if (!l) |
| 1001 | return 0; |
| 1002 | |
| 1003 | // check alignment |
| 1004 | if ((reinterpret_cast<quintptr>(a) & 2) == (reinterpret_cast<quintptr>(b) & 2)) { |
| 1005 | // both addresses have the same alignment |
| 1006 | if (reinterpret_cast<quintptr>(a) & 2) { |
| 1007 | // both addresses are not aligned to 4-bytes boundaries |
| 1008 | // compare the first character |
| 1009 | if (*a != *b) |
| 1010 | return a->unicode() - b->unicode(); |
| 1011 | --l; |
| 1012 | ++a; |
| 1013 | ++b; |
| 1014 | |
| 1015 | // now both addresses are 4-bytes aligned |
| 1016 | } |
| 1017 | |
| 1018 | // both addresses are 4-bytes aligned |
| 1019 | // do a fast 32-bit comparison |
| 1020 | const quint32 *da = reinterpret_cast<const quint32 *>(a); |
| 1021 | const quint32 *db = reinterpret_cast<const quint32 *>(b); |
| 1022 | const quint32 *e = da + (l >> 1); |
| 1023 | for ( ; da != e; ++da, ++db) { |
| 1024 | if (*da != *db) { |
| 1025 | a = reinterpret_cast<const QChar *>(da); |
| 1026 | b = reinterpret_cast<const QChar *>(db); |
| 1027 | if (*a != *b) |
| 1028 | return a->unicode() - b->unicode(); |
| 1029 | return a[1].unicode() - b[1].unicode(); |
| 1030 | } |
| 1031 | } |
| 1032 | |
| 1033 | // do we have a tail? |
| 1034 | a = reinterpret_cast<const QChar *>(da); |
| 1035 | b = reinterpret_cast<const QChar *>(db); |
| 1036 | return (l & 1) ? a->unicode() - b->unicode() : 0; |
| 1037 | } else { |
| 1038 | // one of the addresses isn't 4-byte aligned but the other is |
| 1039 | const QChar *e = a + l; |
| 1040 | for ( ; a != e; ++a, ++b) { |
| 1041 | if (*a != *b) |
| 1042 | return a->unicode() - b->unicode(); |
| 1043 | } |
| 1044 | } |
| 1045 | return 0; |
| 1046 | #endif |
| 1047 | } |
| 1048 | |
| 1049 | static int ucstrncmp(const QChar *a, const uchar *c, size_t l) |
| 1050 | { |
| 1051 | const ushort *uc = reinterpret_cast<const ushort *>(a); |
| 1052 | const ushort *e = uc + l; |
| 1053 | |
| 1054 | #ifdef __SSE2__ |
| 1055 | __m128i nullmask = _mm_setzero_si128(); |
| 1056 | qptrdiff offset = 0; |
| 1057 | |
| 1058 | # if !defined(__OPTIMIZE_SIZE__) |
| 1059 | // Using the PMOVMSKB instruction, we get two bits for each character |
| 1060 | // we compare. |
| 1061 | int retval; |
| 1062 | auto isDifferent = [uc, c, &offset, &retval](__m128i a_data, __m128i b_data) { |
| 1063 | __m128i result = _mm_cmpeq_epi16(a: a_data, b: b_data); |
| 1064 | uint mask = ~uint(_mm_movemask_epi8(a: result)); |
| 1065 | if (ushort(mask) == 0) |
| 1066 | return false; |
| 1067 | uint idx = qCountTrailingZeroBits(v: mask); |
| 1068 | retval = uc[offset + idx / 2] - c[offset + idx / 2]; |
| 1069 | return true; |
| 1070 | }; |
| 1071 | # endif |
| 1072 | |
| 1073 | // we're going to read uc[offset..offset+15] (32 bytes) |
| 1074 | // and c[offset..offset+15] (16 bytes) |
| 1075 | for ( ; uc + offset + 15 < e; offset += 16) { |
| 1076 | // similar to fromLatin1_helper: |
| 1077 | // load 16 bytes of Latin 1 data |
| 1078 | __m128i chunk = _mm_loadu_si128(p: (const __m128i*)(c + offset)); |
| 1079 | |
| 1080 | # ifdef __AVX2__ |
| 1081 | // expand Latin 1 data via zero extension |
| 1082 | __m256i ldata = _mm256_cvtepu8_epi16(chunk); |
| 1083 | |
| 1084 | // load UTF-16 data and compare |
| 1085 | __m256i ucdata = _mm256_loadu_si256((const __m256i*)(uc + offset)); |
| 1086 | __m256i result = _mm256_cmpeq_epi16(ldata, ucdata); |
| 1087 | |
| 1088 | uint mask = ~_mm256_movemask_epi8(result); |
| 1089 | # else |
| 1090 | // expand via unpacking |
| 1091 | __m128i firstHalf = _mm_unpacklo_epi8(a: chunk, b: nullmask); |
| 1092 | __m128i secondHalf = _mm_unpackhi_epi8(a: chunk, b: nullmask); |
| 1093 | |
| 1094 | // load UTF-16 data and compare |
| 1095 | __m128i ucdata1 = _mm_loadu_si128(p: (const __m128i*)(uc + offset)); |
| 1096 | __m128i ucdata2 = _mm_loadu_si128(p: (const __m128i*)(uc + offset + 8)); |
| 1097 | __m128i result1 = _mm_cmpeq_epi16(a: firstHalf, b: ucdata1); |
| 1098 | __m128i result2 = _mm_cmpeq_epi16(a: secondHalf, b: ucdata2); |
| 1099 | |
| 1100 | uint mask = ~(_mm_movemask_epi8(a: result1) | _mm_movemask_epi8(a: result2) << 16); |
| 1101 | # endif |
| 1102 | if (mask) { |
| 1103 | // found a different character |
| 1104 | uint idx = qCountTrailingZeroBits(v: mask); |
| 1105 | return uc[offset + idx / 2] - c[offset + idx / 2]; |
| 1106 | } |
| 1107 | } |
| 1108 | |
| 1109 | # if !defined(__OPTIMIZE_SIZE__) |
| 1110 | // we'll read uc[offset..offset+7] (16 bytes) and c[offset..offset+7] (8 bytes) |
| 1111 | if (uc + offset + 7 < e) { |
| 1112 | // same, but we're using an 8-byte load |
| 1113 | __m128i secondHalf = mm_load8_zero_extend(ptr: c + offset); |
| 1114 | |
| 1115 | __m128i ucdata = _mm_loadu_si128(p: (const __m128i*)(uc + offset)); |
| 1116 | if (isDifferent(ucdata, secondHalf)) |
| 1117 | return retval; |
| 1118 | |
| 1119 | // still matched |
| 1120 | offset += 8; |
| 1121 | } |
| 1122 | |
| 1123 | enum { MaxTailLength = 3 }; |
| 1124 | // we'll read uc[offset..offset+3] (8 bytes) and c[offset..offset+3] (4 bytes) |
| 1125 | if (uc + offset + 3 < e) { |
| 1126 | __m128i chunk = _mm_cvtsi32_si128(a: qFromUnaligned<int>(src: c + offset)); |
| 1127 | __m128i secondHalf = _mm_unpacklo_epi8(a: chunk, b: nullmask); |
| 1128 | |
| 1129 | __m128i ucdata = _mm_loadl_epi64(p: reinterpret_cast<const __m128i *>(uc + offset)); |
| 1130 | if (isDifferent(ucdata, secondHalf)) |
| 1131 | return retval; |
| 1132 | |
| 1133 | // still matched |
| 1134 | offset += 4; |
| 1135 | } |
| 1136 | # endif // optimize size |
| 1137 | |
| 1138 | // reset uc and c |
| 1139 | uc += offset; |
| 1140 | c += offset; |
| 1141 | |
| 1142 | # if !defined(__OPTIMIZE_SIZE__) |
| 1143 | const auto lambda = [=](size_t i) { return uc[i] - ushort(c[i]); }; |
| 1144 | return UnrollTailLoop<MaxTailLength>::exec(count: e - uc, returnIfExited: 0, loopCheck: lambda, returnIfFailed: lambda); |
| 1145 | # endif |
| 1146 | #endif |
| 1147 | |
| 1148 | while (uc < e) { |
| 1149 | int diff = *uc - *c; |
| 1150 | if (diff) |
| 1151 | return diff; |
| 1152 | uc++, c++; |
| 1153 | } |
| 1154 | |
| 1155 | return 0; |
| 1156 | } |
| 1157 | |
| 1158 | template <typename Number> |
| 1159 | Q_DECL_CONSTEXPR int lencmp(Number lhs, Number rhs) noexcept |
| 1160 | { |
| 1161 | return lhs == rhs ? 0 : |
| 1162 | lhs > rhs ? 1 : |
| 1163 | /* else */ -1 ; |
| 1164 | } |
| 1165 | |
| 1166 | // Unicode case-sensitive comparison |
| 1167 | static int ucstrcmp(const QChar *a, size_t alen, const QChar *b, size_t blen) |
| 1168 | { |
| 1169 | if (a == b && alen == blen) |
| 1170 | return 0; |
| 1171 | const size_t l = qMin(a: alen, b: blen); |
| 1172 | int cmp = ucstrncmp(a, b, l); |
| 1173 | return cmp ? cmp : lencmp(lhs: alen, rhs: blen); |
| 1174 | } |
| 1175 | |
| 1176 | static int ucstrcmp(const QChar *a, size_t alen, const char *b, size_t blen) |
| 1177 | { |
| 1178 | const size_t l = qMin(a: alen, b: blen); |
| 1179 | const int cmp = ucstrncmp(a, c: reinterpret_cast<const uchar*>(b), l); |
| 1180 | return cmp ? cmp : lencmp(lhs: alen, rhs: blen); |
| 1181 | } |
| 1182 | |
| 1183 | static int qt_compare_strings(QStringView lhs, QStringView rhs, Qt::CaseSensitivity cs) noexcept |
| 1184 | { |
| 1185 | if (cs == Qt::CaseSensitive) |
| 1186 | return ucstrcmp(a: lhs.begin(), alen: lhs.size(), b: rhs.begin(), blen: rhs.size()); |
| 1187 | else |
| 1188 | return ucstricmp(a: lhs.begin(), ae: lhs.end(), b: rhs.begin(), be: rhs.end()); |
| 1189 | } |
| 1190 | |
| 1191 | static int qt_compare_strings(QStringView lhs, QLatin1String rhs, Qt::CaseSensitivity cs) noexcept |
| 1192 | { |
| 1193 | if (cs == Qt::CaseSensitive) |
| 1194 | return ucstrcmp(a: lhs.begin(), alen: lhs.size(), b: rhs.begin(), blen: rhs.size()); |
| 1195 | else |
| 1196 | return ucstricmp(a: lhs.begin(), ae: lhs.end(), b: rhs.begin(), be: rhs.end()); |
| 1197 | } |
| 1198 | |
| 1199 | static int qt_compare_strings(QLatin1String lhs, QStringView rhs, Qt::CaseSensitivity cs) noexcept |
| 1200 | { |
| 1201 | return -qt_compare_strings(lhs: rhs, rhs: lhs, cs); |
| 1202 | } |
| 1203 | |
| 1204 | static int qt_compare_strings(QLatin1String lhs, QLatin1String rhs, Qt::CaseSensitivity cs) noexcept |
| 1205 | { |
| 1206 | if (lhs.isEmpty()) |
| 1207 | return lencmp(lhs: 0, rhs: rhs.size()); |
| 1208 | if (cs == Qt::CaseInsensitive) |
| 1209 | return qstrnicmp(lhs.data(), lhs.size(), rhs.data(), rhs.size()); |
| 1210 | const auto l = std::min(a: lhs.size(), b: rhs.size()); |
| 1211 | int r = qstrncmp(str1: lhs.data(), str2: rhs.data(), len: l); |
| 1212 | return r ? r : lencmp(lhs: lhs.size(), rhs: rhs.size()); |
| 1213 | } |
| 1214 | |
| 1215 | /*! |
| 1216 | \relates QStringView |
| 1217 | \internal |
| 1218 | \since 5.10 |
| 1219 | |
| 1220 | Returns an integer that compares to 0 as \a lhs compares to \a rhs. |
| 1221 | |
| 1222 | If \a cs is Qt::CaseSensitive (the default), the comparison is case-sensitive; |
| 1223 | otherwise the comparison is case-insensitive. |
| 1224 | |
| 1225 | Case-sensitive comparison is based exclusively on the numeric Unicode values |
| 1226 | of the characters and is very fast, but is not what a human would expect. |
| 1227 | Consider sorting user-visible strings with QString::localeAwareCompare(). |
| 1228 | |
| 1229 | \sa {Comparing Strings} |
| 1230 | */ |
| 1231 | int QtPrivate::compareStrings(QStringView lhs, QStringView rhs, Qt::CaseSensitivity cs) noexcept |
| 1232 | { |
| 1233 | return qt_compare_strings(lhs, rhs, cs); |
| 1234 | } |
| 1235 | |
| 1236 | /*! |
| 1237 | \relates QStringView |
| 1238 | \internal |
| 1239 | \since 5.10 |
| 1240 | \overload |
| 1241 | |
| 1242 | Returns an integer that compares to 0 as \a lhs compares to \a rhs. |
| 1243 | |
| 1244 | If \a cs is Qt::CaseSensitive (the default), the comparison is case-sensitive; |
| 1245 | otherwise the comparison is case-insensitive. |
| 1246 | |
| 1247 | Case-sensitive comparison is based exclusively on the numeric Unicode values |
| 1248 | of the characters and is very fast, but is not what a human would expect. |
| 1249 | Consider sorting user-visible strings with QString::localeAwareCompare(). |
| 1250 | |
| 1251 | \sa {Comparing Strings} |
| 1252 | */ |
| 1253 | int QtPrivate::compareStrings(QStringView lhs, QLatin1String rhs, Qt::CaseSensitivity cs) noexcept |
| 1254 | { |
| 1255 | return qt_compare_strings(lhs, rhs, cs); |
| 1256 | } |
| 1257 | |
| 1258 | /*! |
| 1259 | \relates QStringView |
| 1260 | \internal |
| 1261 | \since 5.10 |
| 1262 | \overload |
| 1263 | |
| 1264 | Returns an integer that compares to 0 as \a lhs compares to \a rhs. |
| 1265 | |
| 1266 | If \a cs is Qt::CaseSensitive (the default), the comparison is case-sensitive; |
| 1267 | otherwise the comparison is case-insensitive. |
| 1268 | |
| 1269 | Case-sensitive comparison is based exclusively on the numeric Unicode values |
| 1270 | of the characters and is very fast, but is not what a human would expect. |
| 1271 | Consider sorting user-visible strings with QString::localeAwareCompare(). |
| 1272 | |
| 1273 | \sa {Comparing Strings} |
| 1274 | */ |
| 1275 | int QtPrivate::compareStrings(QLatin1String lhs, QStringView rhs, Qt::CaseSensitivity cs) noexcept |
| 1276 | { |
| 1277 | return qt_compare_strings(lhs, rhs, cs); |
| 1278 | } |
| 1279 | |
| 1280 | /*! |
| 1281 | \relates QStringView |
| 1282 | \internal |
| 1283 | \since 5.10 |
| 1284 | \overload |
| 1285 | |
| 1286 | Returns an integer that compares to 0 as \a lhs compares to \a rhs. |
| 1287 | |
| 1288 | If \a cs is Qt::CaseSensitive (the default), the comparison is case-sensitive; |
| 1289 | otherwise the comparison is case-insensitive. |
| 1290 | |
| 1291 | Case-sensitive comparison is based exclusively on the numeric Latin-1 values |
| 1292 | of the characters and is very fast, but is not what a human would expect. |
| 1293 | Consider sorting user-visible strings with QString::localeAwareCompare(). |
| 1294 | |
| 1295 | \sa {Comparing Strings} |
| 1296 | */ |
| 1297 | int QtPrivate::compareStrings(QLatin1String lhs, QLatin1String rhs, Qt::CaseSensitivity cs) noexcept |
| 1298 | { |
| 1299 | return qt_compare_strings(lhs, rhs, cs); |
| 1300 | } |
| 1301 | |
| 1302 | #define REHASH(a) \ |
| 1303 | if (sl_minus_1 < sizeof(std::size_t) * CHAR_BIT) \ |
| 1304 | hashHaystack -= std::size_t(a) << sl_minus_1; \ |
| 1305 | hashHaystack <<= 1 |
| 1306 | |
| 1307 | inline bool qIsUpper(char ch) |
| 1308 | { |
| 1309 | return ch >= 'A' && ch <= 'Z'; |
| 1310 | } |
| 1311 | |
| 1312 | inline bool qIsDigit(char ch) |
| 1313 | { |
| 1314 | return ch >= '0' && ch <= '9'; |
| 1315 | } |
| 1316 | |
| 1317 | inline char qToLower(char ch) |
| 1318 | { |
| 1319 | if (ch >= 'A' && ch <= 'Z') |
| 1320 | return ch - 'A' + 'a'; |
| 1321 | else |
| 1322 | return ch; |
| 1323 | } |
| 1324 | |
| 1325 | |
| 1326 | #if QT_DEPRECATED_SINCE(5, 9) |
| 1327 | const QString::Null QString::null = { }; |
| 1328 | #endif |
| 1329 | |
| 1330 | /*! |
| 1331 | \macro QT_RESTRICTED_CAST_FROM_ASCII |
| 1332 | \relates QString |
| 1333 | |
| 1334 | Disables most automatic conversions from source literals and 8-bit data |
| 1335 | to unicode QStrings, but allows the use of |
| 1336 | the \c{QChar(char)} and \c{QString(const char (&ch)[N]} constructors, |
| 1337 | and the \c{QString::operator=(const char (&ch)[N])} assignment operator. |
| 1338 | This gives most of the type-safety benefits of \c QT_NO_CAST_FROM_ASCII |
| 1339 | but does not require user code to wrap character and string literals |
| 1340 | with QLatin1Char, QLatin1String or similar. |
| 1341 | |
| 1342 | Using this macro together with source strings outside the 7-bit range, |
| 1343 | non-literals, or literals with embedded NUL characters is undefined. |
| 1344 | |
| 1345 | \sa QT_NO_CAST_FROM_ASCII, QT_NO_CAST_TO_ASCII |
| 1346 | */ |
| 1347 | |
| 1348 | /*! |
| 1349 | \macro QT_NO_CAST_FROM_ASCII |
| 1350 | \relates QString |
| 1351 | |
| 1352 | Disables automatic conversions from 8-bit strings (char *) to unicode QStrings |
| 1353 | |
| 1354 | \sa QT_NO_CAST_TO_ASCII, QT_RESTRICTED_CAST_FROM_ASCII, QT_NO_CAST_FROM_BYTEARRAY |
| 1355 | */ |
| 1356 | |
| 1357 | /*! |
| 1358 | \macro QT_NO_CAST_TO_ASCII |
| 1359 | \relates QString |
| 1360 | |
| 1361 | Disables automatic conversion from QString to 8-bit strings (char *). |
| 1362 | |
| 1363 | \sa QT_NO_CAST_FROM_ASCII, QT_RESTRICTED_CAST_FROM_ASCII, QT_NO_CAST_FROM_BYTEARRAY |
| 1364 | */ |
| 1365 | |
| 1366 | /*! |
| 1367 | \macro QT_ASCII_CAST_WARNINGS |
| 1368 | \internal |
| 1369 | \relates QString |
| 1370 | |
| 1371 | This macro can be defined to force a warning whenever a function is |
| 1372 | called that automatically converts between unicode and 8-bit encodings. |
| 1373 | |
| 1374 | Note: This only works for compilers that support warnings for |
| 1375 | deprecated API. |
| 1376 | |
| 1377 | \sa QT_NO_CAST_TO_ASCII, QT_NO_CAST_FROM_ASCII, QT_RESTRICTED_CAST_FROM_ASCII |
| 1378 | */ |
| 1379 | |
| 1380 | /*! |
| 1381 | \class QCharRef |
| 1382 | \inmodule QtCore |
| 1383 | \reentrant |
| 1384 | \brief The QCharRef class is a helper class for QString. |
| 1385 | |
| 1386 | \internal |
| 1387 | |
| 1388 | \ingroup string-processing |
| 1389 | |
| 1390 | When you get an object of type QCharRef, if you can assign to it, |
| 1391 | the assignment will apply to the character in the string from |
| 1392 | which you got the reference. That is its whole purpose in life. |
| 1393 | The QCharRef becomes invalid once modifications are made to the |
| 1394 | string: if you want to keep the character, copy it into a QChar. |
| 1395 | |
| 1396 | Most of the QChar member functions also exist in QCharRef. |
| 1397 | However, they are not explicitly documented here. |
| 1398 | |
| 1399 | \sa QString::operator[](), QString::at(), QChar |
| 1400 | */ |
| 1401 | |
| 1402 | /*! |
| 1403 | \class QString |
| 1404 | \inmodule QtCore |
| 1405 | \reentrant |
| 1406 | |
| 1407 | \brief The QString class provides a Unicode character string. |
| 1408 | |
| 1409 | \ingroup tools |
| 1410 | \ingroup shared |
| 1411 | \ingroup string-processing |
| 1412 | |
| 1413 | QString stores a string of 16-bit \l{QChar}s, where each QChar |
| 1414 | corresponds to one UTF-16 code unit. (Unicode characters |
| 1415 | with code values above 65535 are stored using surrogate pairs, |
| 1416 | i.e., two consecutive \l{QChar}s.) |
| 1417 | |
| 1418 | \l{Unicode} is an international standard that supports most of the |
| 1419 | writing systems in use today. It is a superset of US-ASCII (ANSI |
| 1420 | X3.4-1986) and Latin-1 (ISO 8859-1), and all the US-ASCII/Latin-1 |
| 1421 | characters are available at the same code positions. |
| 1422 | |
| 1423 | Behind the scenes, QString uses \l{implicit sharing} |
| 1424 | (copy-on-write) to reduce memory usage and to avoid the needless |
| 1425 | copying of data. This also helps reduce the inherent overhead of |
| 1426 | storing 16-bit characters instead of 8-bit characters. |
| 1427 | |
| 1428 | In addition to QString, Qt also provides the QByteArray class to |
| 1429 | store raw bytes and traditional 8-bit '\\0'-terminated strings. |
| 1430 | For most purposes, QString is the class you want to use. It is |
| 1431 | used throughout the Qt API, and the Unicode support ensures that |
| 1432 | your applications will be easy to translate if you want to expand |
| 1433 | your application's market at some point. The two main cases where |
| 1434 | QByteArray is appropriate are when you need to store raw binary |
| 1435 | data, and when memory conservation is critical (like in embedded |
| 1436 | systems). |
| 1437 | |
| 1438 | \tableofcontents |
| 1439 | |
| 1440 | \section1 Initializing a String |
| 1441 | |
| 1442 | One way to initialize a QString is simply to pass a \c{const char |
| 1443 | *} to its constructor. For example, the following code creates a |
| 1444 | QString of size 5 containing the data "Hello": |
| 1445 | |
| 1446 | \snippet qstring/main.cpp 0 |
| 1447 | |
| 1448 | QString converts the \c{const char *} data into Unicode using the |
| 1449 | fromUtf8() function. |
| 1450 | |
| 1451 | In all of the QString functions that take \c{const char *} |
| 1452 | parameters, the \c{const char *} is interpreted as a classic |
| 1453 | C-style '\\0'-terminated string encoded in UTF-8. It is legal for |
| 1454 | the \c{const char *} parameter to be \nullptr. |
| 1455 | |
| 1456 | You can also provide string data as an array of \l{QChar}s: |
| 1457 | |
| 1458 | \snippet qstring/main.cpp 1 |
| 1459 | |
| 1460 | QString makes a deep copy of the QChar data, so you can modify it |
| 1461 | later without experiencing side effects. (If for performance |
| 1462 | reasons you don't want to take a deep copy of the character data, |
| 1463 | use QString::fromRawData() instead.) |
| 1464 | |
| 1465 | Another approach is to set the size of the string using resize() |
| 1466 | and to initialize the data character per character. QString uses |
| 1467 | 0-based indexes, just like C++ arrays. To access the character at |
| 1468 | a particular index position, you can use \l operator[](). On |
| 1469 | non-const strings, \l operator[]() returns a reference to a |
| 1470 | character that can be used on the left side of an assignment. For |
| 1471 | example: |
| 1472 | |
| 1473 | \snippet qstring/main.cpp 2 |
| 1474 | |
| 1475 | For read-only access, an alternative syntax is to use the at() |
| 1476 | function: |
| 1477 | |
| 1478 | \snippet qstring/main.cpp 3 |
| 1479 | |
| 1480 | The at() function can be faster than \l operator[](), because it |
| 1481 | never causes a \l{deep copy} to occur. Alternatively, use the |
| 1482 | left(), right(), or mid() functions to extract several characters |
| 1483 | at a time. |
| 1484 | |
| 1485 | A QString can embed '\\0' characters (QChar::Null). The size() |
| 1486 | function always returns the size of the whole string, including |
| 1487 | embedded '\\0' characters. |
| 1488 | |
| 1489 | After a call to the resize() function, newly allocated characters |
| 1490 | have undefined values. To set all the characters in the string to |
| 1491 | a particular value, use the fill() function. |
| 1492 | |
| 1493 | QString provides dozens of overloads designed to simplify string |
| 1494 | usage. For example, if you want to compare a QString with a string |
| 1495 | literal, you can write code like this and it will work as expected: |
| 1496 | |
| 1497 | \snippet qstring/main.cpp 4 |
| 1498 | |
| 1499 | You can also pass string literals to functions that take QStrings |
| 1500 | as arguments, invoking the QString(const char *) |
| 1501 | constructor. Similarly, you can pass a QString to a function that |
| 1502 | takes a \c{const char *} argument using the \l qPrintable() macro |
| 1503 | which returns the given QString as a \c{const char *}. This is |
| 1504 | equivalent to calling <QString>.toLocal8Bit().constData(). |
| 1505 | |
| 1506 | \section1 Manipulating String Data |
| 1507 | |
| 1508 | QString provides the following basic functions for modifying the |
| 1509 | character data: append(), prepend(), insert(), replace(), and |
| 1510 | remove(). For example: |
| 1511 | |
| 1512 | \snippet qstring/main.cpp 5 |
| 1513 | |
| 1514 | If you are building a QString gradually and know in advance |
| 1515 | approximately how many characters the QString will contain, you |
| 1516 | can call reserve(), asking QString to preallocate a certain amount |
| 1517 | of memory. You can also call capacity() to find out how much |
| 1518 | memory QString actually allocated. |
| 1519 | |
| 1520 | The replace() and remove() functions' first two arguments are the |
| 1521 | position from which to start erasing and the number of characters |
| 1522 | that should be erased. If you want to replace all occurrences of |
| 1523 | a particular substring with another, use one of the two-parameter |
| 1524 | replace() overloads. |
| 1525 | |
| 1526 | A frequent requirement is to remove whitespace characters from a |
| 1527 | string ('\\n', '\\t', ' ', etc.). If you want to remove whitespace |
| 1528 | from both ends of a QString, use the trimmed() function. If you |
| 1529 | want to remove whitespace from both ends and replace multiple |
| 1530 | consecutive whitespaces with a single space character within the |
| 1531 | string, use simplified(). |
| 1532 | |
| 1533 | If you want to find all occurrences of a particular character or |
| 1534 | substring in a QString, use the indexOf() or lastIndexOf() |
| 1535 | functions. The former searches forward starting from a given index |
| 1536 | position, the latter searches backward. Both return the index |
| 1537 | position of the character or substring if they find it; otherwise, |
| 1538 | they return -1. For example, here is a typical loop that finds all |
| 1539 | occurrences of a particular substring: |
| 1540 | |
| 1541 | \snippet qstring/main.cpp 6 |
| 1542 | |
| 1543 | QString provides many functions for converting numbers into |
| 1544 | strings and strings into numbers. See the arg() functions, the |
| 1545 | setNum() functions, the number() static functions, and the |
| 1546 | toInt(), toDouble(), and similar functions. |
| 1547 | |
| 1548 | To get an upper- or lowercase version of a string use toUpper() or |
| 1549 | toLower(). |
| 1550 | |
| 1551 | Lists of strings are handled by the QStringList class. You can |
| 1552 | split a string into a list of strings using the split() function, |
| 1553 | and join a list of strings into a single string with an optional |
| 1554 | separator using QStringList::join(). You can obtain a list of |
| 1555 | strings from a string list that contain a particular substring or |
| 1556 | that match a particular QRegExp using the QStringList::filter() |
| 1557 | function. |
| 1558 | |
| 1559 | \section1 Querying String Data |
| 1560 | |
| 1561 | If you want to see if a QString starts or ends with a particular |
| 1562 | substring use startsWith() or endsWith(). If you simply want to |
| 1563 | check whether a QString contains a particular character or |
| 1564 | substring, use the contains() function. If you want to find out |
| 1565 | how many times a particular character or substring occurs in the |
| 1566 | string, use count(). |
| 1567 | |
| 1568 | To obtain a pointer to the actual character data, call data() or |
| 1569 | constData(). These functions return a pointer to the beginning of |
| 1570 | the QChar data. The pointer is guaranteed to remain valid until a |
| 1571 | non-const function is called on the QString. |
| 1572 | |
| 1573 | \section2 Comparing Strings |
| 1574 | |
| 1575 | QStrings can be compared using overloaded operators such as \l |
| 1576 | operator<(), \l operator<=(), \l operator==(), \l operator>=(), |
| 1577 | and so on. Note that the comparison is based exclusively on the |
| 1578 | numeric Unicode values of the characters. It is very fast, but is |
| 1579 | not what a human would expect; the QString::localeAwareCompare() |
| 1580 | function is usually a better choice for sorting user-interface |
| 1581 | strings, when such a comparison is available. |
| 1582 | |
| 1583 | On Unix-like platforms (including Linux, \macos and iOS), when Qt |
| 1584 | is linked with the ICU library (which it usually is), its |
| 1585 | locale-aware sorting is used. Otherwise, on \macos and iOS, \l |
| 1586 | localeAwareCompare() compares according the "Order for sorted |
| 1587 | lists" setting in the International preferences panel. On other |
| 1588 | Unix-like systems without ICU, the comparison falls back to the |
| 1589 | system library's \c strcoll(), falling back when it considers |
| 1590 | strings equal to QString's (locale-unaware) comparison, described |
| 1591 | above, |
| 1592 | |
| 1593 | \section1 Converting Between 8-Bit Strings and Unicode Strings |
| 1594 | |
| 1595 | QString provides the following three functions that return a |
| 1596 | \c{const char *} version of the string as QByteArray: toUtf8(), |
| 1597 | toLatin1(), and toLocal8Bit(). |
| 1598 | |
| 1599 | \list |
| 1600 | \li toLatin1() returns a Latin-1 (ISO 8859-1) encoded 8-bit string. |
| 1601 | \li toUtf8() returns a UTF-8 encoded 8-bit string. UTF-8 is a |
| 1602 | superset of US-ASCII (ANSI X3.4-1986) that supports the entire |
| 1603 | Unicode character set through multibyte sequences. |
| 1604 | \li toLocal8Bit() returns an 8-bit string using the system's local |
| 1605 | encoding. |
| 1606 | \endlist |
| 1607 | |
| 1608 | To convert from one of these encodings, QString provides |
| 1609 | fromLatin1(), fromUtf8(), and fromLocal8Bit(). Other |
| 1610 | encodings are supported through the QTextCodec class. |
| 1611 | |
| 1612 | As mentioned above, QString provides a lot of functions and |
| 1613 | operators that make it easy to interoperate with \c{const char *} |
| 1614 | strings. But this functionality is a double-edged sword: It makes |
| 1615 | QString more convenient to use if all strings are US-ASCII or |
| 1616 | Latin-1, but there is always the risk that an implicit conversion |
| 1617 | from or to \c{const char *} is done using the wrong 8-bit |
| 1618 | encoding. To minimize these risks, you can turn off these implicit |
| 1619 | conversions by defining some of the following preprocessor symbols: |
| 1620 | |
| 1621 | \list |
| 1622 | \li \l QT_NO_CAST_FROM_ASCII disables automatic conversions from |
| 1623 | C string literals and pointers to Unicode. |
| 1624 | \li \l QT_RESTRICTED_CAST_FROM_ASCII allows automatic conversions |
| 1625 | from C characters and character arrays, but disables automatic |
| 1626 | conversions from character pointers to Unicode. |
| 1627 | \li \l QT_NO_CAST_TO_ASCII disables automatic conversion from QString |
| 1628 | to C strings. |
| 1629 | \endlist |
| 1630 | |
| 1631 | You then need to explicitly call fromUtf8(), fromLatin1(), |
| 1632 | or fromLocal8Bit() to construct a QString from an |
| 1633 | 8-bit string, or use the lightweight QLatin1String class, for |
| 1634 | example: |
| 1635 | |
| 1636 | \snippet code/src_corelib_tools_qstring.cpp 1 |
| 1637 | |
| 1638 | Similarly, you must call toLatin1(), toUtf8(), or |
| 1639 | toLocal8Bit() explicitly to convert the QString to an 8-bit |
| 1640 | string. (Other encodings are supported through the QTextCodec |
| 1641 | class.) |
| 1642 | |
| 1643 | \table 100 % |
| 1644 | \header |
| 1645 | \li Note for C Programmers |
| 1646 | |
| 1647 | \row |
| 1648 | \li |
| 1649 | Due to C++'s type system and the fact that QString is |
| 1650 | \l{implicitly shared}, QStrings may be treated like \c{int}s or |
| 1651 | other basic types. For example: |
| 1652 | |
| 1653 | \snippet qstring/main.cpp 7 |
| 1654 | |
| 1655 | The \c result variable, is a normal variable allocated on the |
| 1656 | stack. When \c return is called, and because we're returning by |
| 1657 | value, the copy constructor is called and a copy of the string is |
| 1658 | returned. No actual copying takes place thanks to the implicit |
| 1659 | sharing. |
| 1660 | |
| 1661 | \endtable |
| 1662 | |
| 1663 | \section1 Distinction Between Null and Empty Strings |
| 1664 | |
| 1665 | For historical reasons, QString distinguishes between a null |
| 1666 | string and an empty string. A \e null string is a string that is |
| 1667 | initialized using QString's default constructor or by passing |
| 1668 | (const char *)0 to the constructor. An \e empty string is any |
| 1669 | string with size 0. A null string is always empty, but an empty |
| 1670 | string isn't necessarily null: |
| 1671 | |
| 1672 | \snippet qstring/main.cpp 8 |
| 1673 | |
| 1674 | All functions except isNull() treat null strings the same as empty |
| 1675 | strings. For example, toUtf8().constData() returns a valid pointer |
| 1676 | (\e not nullptr) to a '\\0' character for a null string. We |
| 1677 | recommend that you always use the isEmpty() function and avoid isNull(). |
| 1678 | |
| 1679 | \section1 Argument Formats |
| 1680 | |
| 1681 | In member functions where an argument \e format can be specified |
| 1682 | (e.g., arg(), number()), the argument \e format can be one of the |
| 1683 | following: |
| 1684 | |
| 1685 | \table |
| 1686 | \header \li Format \li Meaning |
| 1687 | \row \li \c e \li format as [-]9.9e[+|-]999 |
| 1688 | \row \li \c E \li format as [-]9.9E[+|-]999 |
| 1689 | \row \li \c f \li format as [-]9.9 |
| 1690 | \row \li \c g \li use \c e or \c f format, whichever is the most concise |
| 1691 | \row \li \c G \li use \c E or \c f format, whichever is the most concise |
| 1692 | \endtable |
| 1693 | |
| 1694 | A \e precision is also specified with the argument \e format. For |
| 1695 | the 'e', 'E', and 'f' formats, the \e precision represents the |
| 1696 | number of digits \e after the decimal point. For the 'g' and 'G' |
| 1697 | formats, the \e precision represents the maximum number of |
| 1698 | significant digits (trailing zeroes are omitted). |
| 1699 | |
| 1700 | \section1 More Efficient String Construction |
| 1701 | |
| 1702 | Many strings are known at compile time. But the trivial |
| 1703 | constructor QString("Hello"), will copy the contents of the string, |
| 1704 | treating the contents as Latin-1. To avoid this one can use the |
| 1705 | QStringLiteral macro to directly create the required data at compile |
| 1706 | time. Constructing a QString out of the literal does then not cause |
| 1707 | any overhead at runtime. |
| 1708 | |
| 1709 | A slightly less efficient way is to use QLatin1String. This class wraps |
| 1710 | a C string literal, precalculates it length at compile time and can |
| 1711 | then be used for faster comparison with QStrings and conversion to |
| 1712 | QStrings than a regular C string literal. |
| 1713 | |
| 1714 | Using the QString \c{'+'} operator, it is easy to construct a |
| 1715 | complex string from multiple substrings. You will often write code |
| 1716 | like this: |
| 1717 | |
| 1718 | \snippet qstring/stringbuilder.cpp 0 |
| 1719 | |
| 1720 | There is nothing wrong with either of these string constructions, |
| 1721 | but there are a few hidden inefficiencies. Beginning with Qt 4.6, |
| 1722 | you can eliminate them. |
| 1723 | |
| 1724 | First, multiple uses of the \c{'+'} operator usually means |
| 1725 | multiple memory allocations. When concatenating \e{n} substrings, |
| 1726 | where \e{n > 2}, there can be as many as \e{n - 1} calls to the |
| 1727 | memory allocator. |
| 1728 | |
| 1729 | In 4.6, an internal template class \c{QStringBuilder} has been |
| 1730 | added along with a few helper functions. This class is marked |
| 1731 | internal and does not appear in the documentation, because you |
| 1732 | aren't meant to instantiate it in your code. Its use will be |
| 1733 | automatic, as described below. The class is found in |
| 1734 | \c {src/corelib/tools/qstringbuilder.cpp} if you want to have a |
| 1735 | look at it. |
| 1736 | |
| 1737 | \c{QStringBuilder} uses expression templates and reimplements the |
| 1738 | \c{'%'} operator so that when you use \c{'%'} for string |
| 1739 | concatenation instead of \c{'+'}, multiple substring |
| 1740 | concatenations will be postponed until the final result is about |
| 1741 | to be assigned to a QString. At this point, the amount of memory |
| 1742 | required for the final result is known. The memory allocator is |
| 1743 | then called \e{once} to get the required space, and the substrings |
| 1744 | are copied into it one by one. |
| 1745 | |
| 1746 | Additional efficiency is gained by inlining and reduced reference |
| 1747 | counting (the QString created from a \c{QStringBuilder} typically |
| 1748 | has a ref count of 1, whereas QString::append() needs an extra |
| 1749 | test). |
| 1750 | |
| 1751 | There are two ways you can access this improved method of string |
| 1752 | construction. The straightforward way is to include |
| 1753 | \c{QStringBuilder} wherever you want to use it, and use the |
| 1754 | \c{'%'} operator instead of \c{'+'} when concatenating strings: |
| 1755 | |
| 1756 | \snippet qstring/stringbuilder.cpp 5 |
| 1757 | |
| 1758 | A more global approach which is the most convenient but |
| 1759 | not entirely source compatible, is to this define in your |
| 1760 | .pro file: |
| 1761 | |
| 1762 | \snippet qstring/stringbuilder.cpp 3 |
| 1763 | |
| 1764 | and the \c{'+'} will automatically be performed as the |
| 1765 | \c{QStringBuilder} \c{'%'} everywhere. |
| 1766 | |
| 1767 | \section1 Maximum Size and Out-of-memory Conditions |
| 1768 | |
| 1769 | The current version of QString is limited to just under 2 GB (2^31 bytes) |
| 1770 | in size. The exact value is architecture-dependent, since it depends on the |
| 1771 | overhead required for managing the data block, but is no more than 32 |
| 1772 | bytes. Raw data blocks are also limited by the use of \c int type in the |
| 1773 | current version to 2 GB minus 1 byte. Since QString uses two bytes per |
| 1774 | character, that translates to just under 2^30 characters in one QString. |
| 1775 | |
| 1776 | In case memory allocation fails, QString will throw a \c std::bad_alloc |
| 1777 | exception. Out of memory conditions in the Qt containers are the only case |
| 1778 | where Qt will throw exceptions. |
| 1779 | |
| 1780 | Note that the operating system may impose further limits on applications |
| 1781 | holding a lot of allocated memory, especially large, contiguous blocks. |
| 1782 | Such considerations, the configuration of such behavior or any mitigation |
| 1783 | are outside the scope of the Qt API. |
| 1784 | |
| 1785 | \sa fromRawData(), QChar, QLatin1String, QByteArray, QStringRef |
| 1786 | */ |
| 1787 | |
| 1788 | /*! |
| 1789 | \enum QString::SplitBehavior |
| 1790 | |
| 1791 | \obsolete |
| 1792 | Use Qt::SplitBehavior instead. |
| 1793 | |
| 1794 | This enum specifies how the split() function should behave with |
| 1795 | respect to empty strings. |
| 1796 | |
| 1797 | \value KeepEmptyParts If a field is empty, keep it in the result. |
| 1798 | \value SkipEmptyParts If a field is empty, don't include it in the result. |
| 1799 | |
| 1800 | \sa split() |
| 1801 | */ |
| 1802 | |
| 1803 | /*! \typedef QString::ConstIterator |
| 1804 | |
| 1805 | Qt-style synonym for QString::const_iterator. |
| 1806 | */ |
| 1807 | |
| 1808 | /*! \typedef QString::Iterator |
| 1809 | |
| 1810 | Qt-style synonym for QString::iterator. |
| 1811 | */ |
| 1812 | |
| 1813 | /*! \typedef QString::const_iterator |
| 1814 | |
| 1815 | \sa QString::iterator |
| 1816 | */ |
| 1817 | |
| 1818 | /*! \typedef QString::iterator |
| 1819 | |
| 1820 | \sa QString::const_iterator |
| 1821 | */ |
| 1822 | |
| 1823 | /*! \typedef QString::const_reverse_iterator |
| 1824 | \since 5.6 |
| 1825 | |
| 1826 | \sa QString::reverse_iterator, QString::const_iterator |
| 1827 | */ |
| 1828 | |
| 1829 | /*! \typedef QString::reverse_iterator |
| 1830 | \since 5.6 |
| 1831 | |
| 1832 | \sa QString::const_reverse_iterator, QString::iterator |
| 1833 | */ |
| 1834 | |
| 1835 | /*! |
| 1836 | \typedef QString::size_type |
| 1837 | */ |
| 1838 | |
| 1839 | /*! |
| 1840 | \typedef QString::difference_type |
| 1841 | */ |
| 1842 | |
| 1843 | /*! |
| 1844 | \typedef QString::const_reference |
| 1845 | */ |
| 1846 | /*! |
| 1847 | \typedef QString::reference |
| 1848 | */ |
| 1849 | |
| 1850 | /*! |
| 1851 | \typedef QString::const_pointer |
| 1852 | |
| 1853 | The QString::const_pointer typedef provides an STL-style |
| 1854 | const pointer to a QString element (QChar). |
| 1855 | */ |
| 1856 | /*! |
| 1857 | \typedef QString::pointer |
| 1858 | |
| 1859 | The QString::const_pointer typedef provides an STL-style |
| 1860 | pointer to a QString element (QChar). |
| 1861 | */ |
| 1862 | |
| 1863 | /*! |
| 1864 | \typedef QString::value_type |
| 1865 | */ |
| 1866 | |
| 1867 | /*! \fn QString::iterator QString::begin() |
| 1868 | |
| 1869 | Returns an \l{STL-style iterators}{STL-style iterator} pointing to the first character in |
| 1870 | the string. |
| 1871 | |
| 1872 | \sa constBegin(), end() |
| 1873 | */ |
| 1874 | |
| 1875 | /*! \fn QString::const_iterator QString::begin() const |
| 1876 | |
| 1877 | \overload begin() |
| 1878 | */ |
| 1879 | |
| 1880 | /*! \fn QString::const_iterator QString::cbegin() const |
| 1881 | \since 5.0 |
| 1882 | |
| 1883 | Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first character |
| 1884 | in the string. |
| 1885 | |
| 1886 | \sa begin(), cend() |
| 1887 | */ |
| 1888 | |
| 1889 | /*! \fn QString::const_iterator QString::constBegin() const |
| 1890 | |
| 1891 | Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first character |
| 1892 | in the string. |
| 1893 | |
| 1894 | \sa begin(), constEnd() |
| 1895 | */ |
| 1896 | |
| 1897 | /*! \fn QString::iterator QString::end() |
| 1898 | |
| 1899 | Returns an \l{STL-style iterators}{STL-style iterator} pointing to the imaginary character |
| 1900 | after the last character in the string. |
| 1901 | |
| 1902 | \sa begin(), constEnd() |
| 1903 | */ |
| 1904 | |
| 1905 | /*! \fn QString::const_iterator QString::end() const |
| 1906 | |
| 1907 | \overload end() |
| 1908 | */ |
| 1909 | |
| 1910 | /*! \fn QString::const_iterator QString::cend() const |
| 1911 | \since 5.0 |
| 1912 | |
| 1913 | Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary |
| 1914 | character after the last character in the list. |
| 1915 | |
| 1916 | \sa cbegin(), end() |
| 1917 | */ |
| 1918 | |
| 1919 | /*! \fn QString::const_iterator QString::constEnd() const |
| 1920 | |
| 1921 | Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary |
| 1922 | character after the last character in the list. |
| 1923 | |
| 1924 | \sa constBegin(), end() |
| 1925 | */ |
| 1926 | |
| 1927 | /*! \fn QString::reverse_iterator QString::rbegin() |
| 1928 | \since 5.6 |
| 1929 | |
| 1930 | Returns a \l{STL-style iterators}{STL-style} reverse iterator pointing to the first |
| 1931 | character in the string, in reverse order. |
| 1932 | |
| 1933 | \sa begin(), crbegin(), rend() |
| 1934 | */ |
| 1935 | |
| 1936 | /*! \fn QString::const_reverse_iterator QString::rbegin() const |
| 1937 | \since 5.6 |
| 1938 | \overload |
| 1939 | */ |
| 1940 | |
| 1941 | /*! \fn QString::const_reverse_iterator QString::crbegin() const |
| 1942 | \since 5.6 |
| 1943 | |
| 1944 | Returns a const \l{STL-style iterators}{STL-style} reverse iterator pointing to the first |
| 1945 | character in the string, in reverse order. |
| 1946 | |
| 1947 | \sa begin(), rbegin(), rend() |
| 1948 | */ |
| 1949 | |
| 1950 | /*! \fn QString::reverse_iterator QString::rend() |
| 1951 | \since 5.6 |
| 1952 | |
| 1953 | Returns a \l{STL-style iterators}{STL-style} reverse iterator pointing to one past |
| 1954 | the last character in the string, in reverse order. |
| 1955 | |
| 1956 | \sa end(), crend(), rbegin() |
| 1957 | */ |
| 1958 | |
| 1959 | /*! \fn QString::const_reverse_iterator QString::rend() const |
| 1960 | \since 5.6 |
| 1961 | \overload |
| 1962 | */ |
| 1963 | |
| 1964 | /*! \fn QString::const_reverse_iterator QString::crend() const |
| 1965 | \since 5.6 |
| 1966 | |
| 1967 | Returns a const \l{STL-style iterators}{STL-style} reverse iterator pointing to one |
| 1968 | past the last character in the string, in reverse order. |
| 1969 | |
| 1970 | \sa end(), rend(), rbegin() |
| 1971 | */ |
| 1972 | |
| 1973 | /*! |
| 1974 | \fn QString::QString() |
| 1975 | |
| 1976 | Constructs a null string. Null strings are also empty. |
| 1977 | |
| 1978 | \sa isEmpty() |
| 1979 | */ |
| 1980 | |
| 1981 | /*! |
| 1982 | \fn QString::QString(QString &&other) |
| 1983 | |
| 1984 | Move-constructs a QString instance, making it point at the same |
| 1985 | object that \a other was pointing to. |
| 1986 | |
| 1987 | \since 5.2 |
| 1988 | */ |
| 1989 | |
| 1990 | /*! \fn QString::QString(const char *str) |
| 1991 | |
| 1992 | Constructs a string initialized with the 8-bit string \a str. The |
| 1993 | given const char pointer is converted to Unicode using the |
| 1994 | fromUtf8() function. |
| 1995 | |
| 1996 | You can disable this constructor by defining \c |
| 1997 | QT_NO_CAST_FROM_ASCII when you compile your applications. This |
| 1998 | can be useful if you want to ensure that all user-visible strings |
| 1999 | go through QObject::tr(), for example. |
| 2000 | |
| 2001 | \note Defining \c QT_RESTRICTED_CAST_FROM_ASCII also disables |
| 2002 | this constructor, but enables a \c{QString(const char (&ch)[N])} |
| 2003 | constructor instead. Using non-literal input, or input with |
| 2004 | embedded NUL characters, or non-7-bit characters is undefined |
| 2005 | in this case. |
| 2006 | |
| 2007 | \sa fromLatin1(), fromLocal8Bit(), fromUtf8(), QT_NO_CAST_FROM_ASCII, QT_RESTRICTED_CAST_FROM_ASCII |
| 2008 | */ |
| 2009 | |
| 2010 | /*! \fn QString QString::fromStdString(const std::string &str) |
| 2011 | |
| 2012 | Returns a copy of the \a str string. The given string is converted |
| 2013 | to Unicode using the fromUtf8() function. |
| 2014 | |
| 2015 | \sa fromLatin1(), fromLocal8Bit(), fromUtf8(), QByteArray::fromStdString() |
| 2016 | */ |
| 2017 | |
| 2018 | /*! \fn QString QString::fromStdWString(const std::wstring &str) |
| 2019 | |
| 2020 | Returns a copy of the \a str string. The given string is assumed |
| 2021 | to be encoded in utf16 if the size of wchar_t is 2 bytes (e.g. on |
| 2022 | windows) and ucs4 if the size of wchar_t is 4 bytes (most Unix |
| 2023 | systems). |
| 2024 | |
| 2025 | \sa fromUtf16(), fromLatin1(), fromLocal8Bit(), fromUtf8(), fromUcs4(), fromStdU16String(), fromStdU32String() |
| 2026 | */ |
| 2027 | |
| 2028 | /*! \fn QString QString::fromWCharArray(const wchar_t *string, int size) |
| 2029 | \since 4.2 |
| 2030 | |
| 2031 | Returns a copy of the \a string, where the encoding of \a string depends on |
| 2032 | the size of wchar. If wchar is 4 bytes, the \a string is interpreted as UCS-4, |
| 2033 | if wchar is 2 bytes it is interpreted as UTF-16. |
| 2034 | |
| 2035 | If \a size is -1 (default), the \a string has to be \\0'-terminated. |
| 2036 | |
| 2037 | \sa fromUtf16(), fromLatin1(), fromLocal8Bit(), fromUtf8(), fromUcs4(), fromStdWString() |
| 2038 | */ |
| 2039 | |
| 2040 | /*! \fn std::wstring QString::toStdWString() const |
| 2041 | |
| 2042 | Returns a std::wstring object with the data contained in this |
| 2043 | QString. The std::wstring is encoded in utf16 on platforms where |
| 2044 | wchar_t is 2 bytes wide (e.g. windows) and in ucs4 on platforms |
| 2045 | where wchar_t is 4 bytes wide (most Unix systems). |
| 2046 | |
| 2047 | This method is mostly useful to pass a QString to a function |
| 2048 | that accepts a std::wstring object. |
| 2049 | |
| 2050 | \sa utf16(), toLatin1(), toUtf8(), toLocal8Bit(), toStdU16String(), toStdU32String() |
| 2051 | */ |
| 2052 | |
| 2053 | int QString::toUcs4_helper(const ushort *uc, int length, uint *out) |
| 2054 | { |
| 2055 | int count = 0; |
| 2056 | |
| 2057 | QStringIterator i(QStringView(uc, length)); |
| 2058 | while (i.hasNext()) |
| 2059 | out[count++] = i.next(); |
| 2060 | |
| 2061 | return count; |
| 2062 | } |
| 2063 | |
| 2064 | /*! \fn int QString::toWCharArray(wchar_t *array) const |
| 2065 | \since 4.2 |
| 2066 | |
| 2067 | Fills the \a array with the data contained in this QString object. |
| 2068 | The array is encoded in UTF-16 on platforms where |
| 2069 | wchar_t is 2 bytes wide (e.g. windows) and in UCS-4 on platforms |
| 2070 | where wchar_t is 4 bytes wide (most Unix systems). |
| 2071 | |
| 2072 | \a array has to be allocated by the caller and contain enough space to |
| 2073 | hold the complete string (allocating the array with the same length as the |
| 2074 | string is always sufficient). |
| 2075 | |
| 2076 | This function returns the actual length of the string in \a array. |
| 2077 | |
| 2078 | \note This function does not append a null character to the array. |
| 2079 | |
| 2080 | \sa utf16(), toUcs4(), toLatin1(), toUtf8(), toLocal8Bit(), toStdWString(), QStringView::toWCharArray() |
| 2081 | */ |
| 2082 | |
| 2083 | /*! \fn QString::QString(const QString &other) |
| 2084 | |
| 2085 | Constructs a copy of \a other. |
| 2086 | |
| 2087 | This operation takes \l{constant time}, because QString is |
| 2088 | \l{implicitly shared}. This makes returning a QString from a |
| 2089 | function very fast. If a shared instance is modified, it will be |
| 2090 | copied (copy-on-write), and that takes \l{linear time}. |
| 2091 | |
| 2092 | \sa operator=() |
| 2093 | */ |
| 2094 | |
| 2095 | /*! |
| 2096 | Constructs a string initialized with the first \a size characters |
| 2097 | of the QChar array \a unicode. |
| 2098 | |
| 2099 | If \a unicode is 0, a null string is constructed. |
| 2100 | |
| 2101 | If \a size is negative, \a unicode is assumed to point to a \\0'-terminated |
| 2102 | array and its length is determined dynamically. The terminating |
| 2103 | null character is not considered part of the string. |
| 2104 | |
| 2105 | QString makes a deep copy of the string data. The unicode data is copied as |
| 2106 | is and the Byte Order Mark is preserved if present. |
| 2107 | |
| 2108 | \sa fromRawData() |
| 2109 | */ |
| 2110 | QString::QString(const QChar *unicode, int size) |
| 2111 | { |
| 2112 | if (!unicode) { |
| 2113 | d = Data::sharedNull(); |
| 2114 | } else { |
| 2115 | if (size < 0) { |
| 2116 | size = 0; |
| 2117 | while (!unicode[size].isNull()) |
| 2118 | ++size; |
| 2119 | } |
| 2120 | if (!size) { |
| 2121 | d = Data::allocate(capacity: 0); |
| 2122 | } else { |
| 2123 | d = Data::allocate(capacity: size + 1); |
| 2124 | Q_CHECK_PTR(d); |
| 2125 | d->size = size; |
| 2126 | memcpy(dest: d->data(), src: unicode, n: size * sizeof(QChar)); |
| 2127 | d->data()[size] = '\0'; |
| 2128 | } |
| 2129 | } |
| 2130 | } |
| 2131 | |
| 2132 | /*! |
| 2133 | Constructs a string of the given \a size with every character set |
| 2134 | to \a ch. |
| 2135 | |
| 2136 | \sa fill() |
| 2137 | */ |
| 2138 | QString::QString(int size, QChar ch) |
| 2139 | { |
| 2140 | if (size <= 0) { |
| 2141 | d = Data::allocate(capacity: 0); |
| 2142 | } else { |
| 2143 | d = Data::allocate(capacity: size + 1); |
| 2144 | Q_CHECK_PTR(d); |
| 2145 | d->size = size; |
| 2146 | d->data()[size] = '\0'; |
| 2147 | ushort *i = d->data() + size; |
| 2148 | ushort *b = d->data(); |
| 2149 | const ushort value = ch.unicode(); |
| 2150 | while (i != b) |
| 2151 | *--i = value; |
| 2152 | } |
| 2153 | } |
| 2154 | |
| 2155 | /*! \fn QString::QString(int size, Qt::Initialization) |
| 2156 | \internal |
| 2157 | |
| 2158 | Constructs a string of the given \a size without initializing the |
| 2159 | characters. This is only used in \c QStringBuilder::toString(). |
| 2160 | */ |
| 2161 | QString::QString(int size, Qt::Initialization) |
| 2162 | { |
| 2163 | d = Data::allocate(capacity: size + 1); |
| 2164 | Q_CHECK_PTR(d); |
| 2165 | d->size = size; |
| 2166 | d->data()[size] = '\0'; |
| 2167 | } |
| 2168 | |
| 2169 | /*! \fn QString::QString(QLatin1String str) |
| 2170 | |
| 2171 | Constructs a copy of the Latin-1 string \a str. |
| 2172 | |
| 2173 | \sa fromLatin1() |
| 2174 | */ |
| 2175 | |
| 2176 | /*! |
| 2177 | Constructs a string of size 1 containing the character \a ch. |
| 2178 | */ |
| 2179 | QString::QString(QChar ch) |
| 2180 | { |
| 2181 | d = Data::allocate(capacity: 2); |
| 2182 | Q_CHECK_PTR(d); |
| 2183 | d->size = 1; |
| 2184 | d->data()[0] = ch.unicode(); |
| 2185 | d->data()[1] = '\0'; |
| 2186 | } |
| 2187 | |
| 2188 | /*! \fn QString::QString(const QByteArray &ba) |
| 2189 | |
| 2190 | Constructs a string initialized with the byte array \a ba. The |
| 2191 | given byte array is converted to Unicode using fromUtf8(). Stops |
| 2192 | copying at the first 0 character, otherwise copies the entire byte |
| 2193 | array. |
| 2194 | |
| 2195 | You can disable this constructor by defining \c |
| 2196 | QT_NO_CAST_FROM_ASCII when you compile your applications. This |
| 2197 | can be useful if you want to ensure that all user-visible strings |
| 2198 | go through QObject::tr(), for example. |
| 2199 | |
| 2200 | \sa fromLatin1(), fromLocal8Bit(), fromUtf8(), QT_NO_CAST_FROM_ASCII |
| 2201 | */ |
| 2202 | |
| 2203 | /*! \fn QString::QString(const Null &) |
| 2204 | \internal |
| 2205 | */ |
| 2206 | |
| 2207 | /*! \fn QString::QString(QStringDataPtr) |
| 2208 | \internal |
| 2209 | */ |
| 2210 | |
| 2211 | /*! \fn QString &QString::operator=(const QString::Null &) |
| 2212 | \internal |
| 2213 | */ |
| 2214 | |
| 2215 | /*! |
| 2216 | \fn QString::~QString() |
| 2217 | |
| 2218 | Destroys the string. |
| 2219 | */ |
| 2220 | |
| 2221 | |
| 2222 | /*! \fn void QString::swap(QString &other) |
| 2223 | \since 4.8 |
| 2224 | |
| 2225 | Swaps string \a other with this string. This operation is very fast and |
| 2226 | never fails. |
| 2227 | */ |
| 2228 | |
| 2229 | /*! \fn void QString::detach() |
| 2230 | |
| 2231 | \internal |
| 2232 | */ |
| 2233 | |
| 2234 | /*! \fn bool QString::isDetached() const |
| 2235 | |
| 2236 | \internal |
| 2237 | */ |
| 2238 | |
| 2239 | /*! \fn bool QString::isSharedWith(const QString &other) const |
| 2240 | |
| 2241 | \internal |
| 2242 | */ |
| 2243 | |
| 2244 | /*! |
| 2245 | Sets the size of the string to \a size characters. |
| 2246 | |
| 2247 | If \a size is greater than the current size, the string is |
| 2248 | extended to make it \a size characters long with the extra |
| 2249 | characters added to the end. The new characters are uninitialized. |
| 2250 | |
| 2251 | If \a size is less than the current size, characters are removed |
| 2252 | from the end. |
| 2253 | |
| 2254 | Example: |
| 2255 | |
| 2256 | \snippet qstring/main.cpp 45 |
| 2257 | |
| 2258 | If you want to append a certain number of identical characters to |
| 2259 | the string, use the \l {QString::}{resize(int, QChar)} overload. |
| 2260 | |
| 2261 | If you want to expand the string so that it reaches a certain |
| 2262 | width and fill the new positions with a particular character, use |
| 2263 | the leftJustified() function: |
| 2264 | |
| 2265 | If \a size is negative, it is equivalent to passing zero. |
| 2266 | |
| 2267 | \snippet qstring/main.cpp 47 |
| 2268 | |
| 2269 | \sa truncate(), reserve() |
| 2270 | */ |
| 2271 | |
| 2272 | void QString::resize(int size) |
| 2273 | { |
| 2274 | if (size < 0) |
| 2275 | size = 0; |
| 2276 | |
| 2277 | if (IS_RAW_DATA(d) && !d->ref.isShared() && size < d->size) { |
| 2278 | d->size = size; |
| 2279 | return; |
| 2280 | } |
| 2281 | |
| 2282 | if (d->ref.isShared() || uint(size) + 1u > d->alloc) |
| 2283 | reallocData(alloc: uint(size) + 1u, grow: true); |
| 2284 | if (d->alloc) { |
| 2285 | d->size = size; |
| 2286 | d->data()[size] = '\0'; |
| 2287 | } |
| 2288 | } |
| 2289 | |
| 2290 | /*! |
| 2291 | \overload |
| 2292 | \since 5.7 |
| 2293 | |
| 2294 | Unlike \l {QString::}{resize(int)}, this overload |
| 2295 | initializes the new characters to \a fillChar: |
| 2296 | |
| 2297 | \snippet qstring/main.cpp 46 |
| 2298 | */ |
| 2299 | |
| 2300 | void QString::resize(int size, QChar fillChar) |
| 2301 | { |
| 2302 | const int oldSize = length(); |
| 2303 | resize(size); |
| 2304 | const int difference = length() - oldSize; |
| 2305 | if (difference > 0) |
| 2306 | std::fill_n(first: d->begin() + oldSize, n: difference, value: fillChar.unicode()); |
| 2307 | } |
| 2308 | |
| 2309 | /*! \fn int QString::capacity() const |
| 2310 | |
| 2311 | Returns the maximum number of characters that can be stored in |
| 2312 | the string without forcing a reallocation. |
| 2313 | |
| 2314 | The sole purpose of this function is to provide a means of fine |
| 2315 | tuning QString's memory usage. In general, you will rarely ever |
| 2316 | need to call this function. If you want to know how many |
| 2317 | characters are in the string, call size(). |
| 2318 | |
| 2319 | \sa reserve(), squeeze() |
| 2320 | */ |
| 2321 | |
| 2322 | /*! |
| 2323 | \fn void QString::reserve(int size) |
| 2324 | |
| 2325 | Attempts to allocate memory for at least \a size characters. If |
| 2326 | you know in advance how large the string will be, you can call |
| 2327 | this function, and if you resize the string often you are likely |
| 2328 | to get better performance. If \a size is an underestimate, the |
| 2329 | worst that will happen is that the QString will be a bit slower. |
| 2330 | |
| 2331 | The sole purpose of this function is to provide a means of fine |
| 2332 | tuning QString's memory usage. In general, you will rarely ever |
| 2333 | need to call this function. If you want to change the size of the |
| 2334 | string, call resize(). |
| 2335 | |
| 2336 | This function is useful for code that needs to build up a long |
| 2337 | string and wants to avoid repeated reallocation. In this example, |
| 2338 | we want to add to the string until some condition is \c true, and |
| 2339 | we're fairly sure that size is large enough to make a call to |
| 2340 | reserve() worthwhile: |
| 2341 | |
| 2342 | \snippet qstring/main.cpp 44 |
| 2343 | |
| 2344 | \sa squeeze(), capacity() |
| 2345 | */ |
| 2346 | |
| 2347 | /*! |
| 2348 | \fn void QString::squeeze() |
| 2349 | |
| 2350 | Releases any memory not required to store the character data. |
| 2351 | |
| 2352 | The sole purpose of this function is to provide a means of fine |
| 2353 | tuning QString's memory usage. In general, you will rarely ever |
| 2354 | need to call this function. |
| 2355 | |
| 2356 | \sa reserve(), capacity() |
| 2357 | */ |
| 2358 | |
| 2359 | void QString::reallocData(uint alloc, bool grow) |
| 2360 | { |
| 2361 | auto allocOptions = d->detachFlags(); |
| 2362 | if (grow) |
| 2363 | allocOptions |= QArrayData::Grow; |
| 2364 | |
| 2365 | if (d->ref.isShared() || IS_RAW_DATA(d)) { |
| 2366 | Data *x = Data::allocate(capacity: alloc, options: allocOptions); |
| 2367 | Q_CHECK_PTR(x); |
| 2368 | x->size = qMin(a: int(alloc) - 1, b: d->size); |
| 2369 | ::memcpy(dest: x->data(), src: d->data(), n: x->size * sizeof(QChar)); |
| 2370 | x->data()[x->size] = 0; |
| 2371 | if (!d->ref.deref()) |
| 2372 | Data::deallocate(data: d); |
| 2373 | d = x; |
| 2374 | } else { |
| 2375 | Data *p = Data::reallocateUnaligned(data: d, capacity: alloc, options: allocOptions); |
| 2376 | Q_CHECK_PTR(p); |
| 2377 | d = p; |
| 2378 | } |
| 2379 | } |
| 2380 | |
| 2381 | #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) |
| 2382 | void QString::expand(int i) |
| 2383 | { |
| 2384 | resize(size: qMax(a: i + 1, b: d->size), fillChar: QLatin1Char(' ')); |
| 2385 | } |
| 2386 | #endif |
| 2387 | |
| 2388 | /*! \fn void QString::clear() |
| 2389 | |
| 2390 | Clears the contents of the string and makes it null. |
| 2391 | |
| 2392 | \sa resize(), isNull() |
| 2393 | */ |
| 2394 | |
| 2395 | /*! \fn QString &QString::operator=(const QString &other) |
| 2396 | |
| 2397 | Assigns \a other to this string and returns a reference to this |
| 2398 | string. |
| 2399 | */ |
| 2400 | |
| 2401 | QString &QString::operator=(const QString &other) noexcept |
| 2402 | { |
| 2403 | other.d->ref.ref(); |
| 2404 | if (!d->ref.deref()) |
| 2405 | Data::deallocate(data: d); |
| 2406 | d = other.d; |
| 2407 | return *this; |
| 2408 | } |
| 2409 | |
| 2410 | /*! |
| 2411 | \fn QString &QString::operator=(QString &&other) |
| 2412 | |
| 2413 | Move-assigns \a other to this QString instance. |
| 2414 | |
| 2415 | \since 5.2 |
| 2416 | */ |
| 2417 | |
| 2418 | /*! \fn QString &QString::operator=(QLatin1String str) |
| 2419 | |
| 2420 | \overload operator=() |
| 2421 | |
| 2422 | Assigns the Latin-1 string \a str to this string. |
| 2423 | */ |
| 2424 | QString &QString::operator=(QLatin1String other) |
| 2425 | { |
| 2426 | if (isDetached() && other.size() <= capacity()) { // assumes d->alloc == 0 -> !isDetached() (sharedNull) |
| 2427 | d->size = other.size(); |
| 2428 | d->data()[other.size()] = 0; |
| 2429 | qt_from_latin1(dst: d->data(), str: other.latin1(), size: other.size()); |
| 2430 | } else { |
| 2431 | *this = fromLatin1(str: other.latin1(), size: other.size()); |
| 2432 | } |
| 2433 | return *this; |
| 2434 | } |
| 2435 | |
| 2436 | /*! \fn QString &QString::operator=(const QByteArray &ba) |
| 2437 | |
| 2438 | \overload operator=() |
| 2439 | |
| 2440 | Assigns \a ba to this string. The byte array is converted to Unicode |
| 2441 | using the fromUtf8() function. This function stops conversion at the |
| 2442 | first NUL character found, or the end of the \a ba byte array. |
| 2443 | |
| 2444 | You can disable this operator by defining \c |
| 2445 | QT_NO_CAST_FROM_ASCII when you compile your applications. This |
| 2446 | can be useful if you want to ensure that all user-visible strings |
| 2447 | go through QObject::tr(), for example. |
| 2448 | |
| 2449 | \sa QT_NO_CAST_FROM_ASCII |
| 2450 | */ |
| 2451 | |
| 2452 | /*! \fn QString &QString::operator=(const char *str) |
| 2453 | |
| 2454 | \overload operator=() |
| 2455 | |
| 2456 | Assigns \a str to this string. The const char pointer is converted |
| 2457 | to Unicode using the fromUtf8() function. |
| 2458 | |
| 2459 | You can disable this operator by defining \c QT_NO_CAST_FROM_ASCII |
| 2460 | or \c QT_RESTRICTED_CAST_FROM_ASCII when you compile your applications. |
| 2461 | This can be useful if you want to ensure that all user-visible strings |
| 2462 | go through QObject::tr(), for example. |
| 2463 | |
| 2464 | \sa QT_NO_CAST_FROM_ASCII, QT_RESTRICTED_CAST_FROM_ASCII |
| 2465 | */ |
| 2466 | |
| 2467 | /*! \fn QString &QString::operator=(char ch) |
| 2468 | |
| 2469 | \overload operator=() |
| 2470 | |
| 2471 | Assigns character \a ch to this string. Note that the character is |
| 2472 | converted to Unicode using the fromLatin1() function, unlike other 8-bit |
| 2473 | functions that operate on UTF-8 data. |
| 2474 | |
| 2475 | You can disable this operator by defining \c |
| 2476 | QT_NO_CAST_FROM_ASCII when you compile your applications. This |
| 2477 | can be useful if you want to ensure that all user-visible strings |
| 2478 | go through QObject::tr(), for example. |
| 2479 | |
| 2480 | \sa QT_NO_CAST_FROM_ASCII |
| 2481 | */ |
| 2482 | |
| 2483 | /*! |
| 2484 | \overload operator=() |
| 2485 | |
| 2486 | Sets the string to contain the single character \a ch. |
| 2487 | */ |
| 2488 | QString &QString::operator=(QChar ch) |
| 2489 | { |
| 2490 | if (isDetached() && capacity() >= 1) { // assumes d->alloc == 0 -> !isDetached() (sharedNull) |
| 2491 | // re-use existing capacity: |
| 2492 | ushort *dat = d->data(); |
| 2493 | dat[0] = ch.unicode(); |
| 2494 | dat[1] = 0; |
| 2495 | d->size = 1; |
| 2496 | } else { |
| 2497 | operator=(other: QString(ch)); |
| 2498 | } |
| 2499 | return *this; |
| 2500 | } |
| 2501 | |
| 2502 | /*! |
| 2503 | \fn QString& QString::insert(int position, const QString &str) |
| 2504 | |
| 2505 | Inserts the string \a str at the given index \a position and |
| 2506 | returns a reference to this string. |
| 2507 | |
| 2508 | Example: |
| 2509 | |
| 2510 | \snippet qstring/main.cpp 26 |
| 2511 | |
| 2512 | If the given \a position is greater than size(), the array is |
| 2513 | first extended using resize(). |
| 2514 | |
| 2515 | \sa append(), prepend(), replace(), remove() |
| 2516 | */ |
| 2517 | |
| 2518 | |
| 2519 | /*! |
| 2520 | \fn QString& QString::insert(int position, const QStringRef &str) |
| 2521 | \since 5.5 |
| 2522 | \overload insert() |
| 2523 | |
| 2524 | Inserts the string reference \a str at the given index \a position and |
| 2525 | returns a reference to this string. |
| 2526 | |
| 2527 | If the given \a position is greater than size(), the array is |
| 2528 | first extended using resize(). |
| 2529 | */ |
| 2530 | |
| 2531 | /*! |
| 2532 | \fn QString& QString::insert(int position, QStringView str) |
| 2533 | \since 5.15.2 |
| 2534 | \overload insert() |
| 2535 | |
| 2536 | Inserts the string reference \a str at the given index \a position and |
| 2537 | returns a reference to this string. |
| 2538 | |
| 2539 | If the given \a position is greater than size(), the array is |
| 2540 | first extended using resize(). |
| 2541 | |
| 2542 | \note This method has been added in 5.15.2 to simplify writing code that is portable |
| 2543 | between Qt 5.15 and Qt 6. |
| 2544 | */ |
| 2545 | |
| 2546 | |
| 2547 | /*! |
| 2548 | \fn QString& QString::insert(int position, const char *str) |
| 2549 | \since 5.5 |
| 2550 | \overload insert() |
| 2551 | |
| 2552 | Inserts the C string \a str at the given index \a position and |
| 2553 | returns a reference to this string. |
| 2554 | |
| 2555 | If the given \a position is greater than size(), the array is |
| 2556 | first extended using resize(). |
| 2557 | |
| 2558 | This function is not available when \c QT_NO_CAST_FROM_ASCII is |
| 2559 | defined. |
| 2560 | |
| 2561 | \sa QT_NO_CAST_FROM_ASCII |
| 2562 | */ |
| 2563 | |
| 2564 | |
| 2565 | /*! |
| 2566 | \fn QString& QString::insert(int position, const QByteArray &str) |
| 2567 | \since 5.5 |
| 2568 | \overload insert() |
| 2569 | |
| 2570 | Inserts the byte array \a str at the given index \a position and |
| 2571 | returns a reference to this string. |
| 2572 | |
| 2573 | If the given \a position is greater than size(), the array is |
| 2574 | first extended using resize(). |
| 2575 | |
| 2576 | This function is not available when \c QT_NO_CAST_FROM_ASCII is |
| 2577 | defined. |
| 2578 | |
| 2579 | \sa QT_NO_CAST_FROM_ASCII |
| 2580 | */ |
| 2581 | |
| 2582 | |
| 2583 | /*! |
| 2584 | \fn QString &QString::insert(int position, QLatin1String str) |
| 2585 | \overload insert() |
| 2586 | |
| 2587 | Inserts the Latin-1 string \a str at the given index \a position. |
| 2588 | */ |
| 2589 | QString &QString::insert(int i, QLatin1String str) |
| 2590 | { |
| 2591 | const char *s = str.latin1(); |
| 2592 | if (i < 0 || !s || !(*s)) |
| 2593 | return *this; |
| 2594 | |
| 2595 | int len = str.size(); |
| 2596 | if (Q_UNLIKELY(i > d->size)) |
| 2597 | resize(size: i + len, fillChar: QLatin1Char(' ')); |
| 2598 | else |
| 2599 | resize(size: d->size + len); |
| 2600 | |
| 2601 | ::memmove(dest: d->data() + i + len, src: d->data() + i, n: (d->size - i - len) * sizeof(QChar)); |
| 2602 | qt_from_latin1(dst: d->data() + i, str: s, size: uint(len)); |
| 2603 | return *this; |
| 2604 | } |
| 2605 | |
| 2606 | /*! |
| 2607 | \fn QString& QString::insert(int position, const QChar *unicode, int size) |
| 2608 | \overload insert() |
| 2609 | |
| 2610 | Inserts the first \a size characters of the QChar array \a unicode |
| 2611 | at the given index \a position in the string. |
| 2612 | */ |
| 2613 | QString& QString::insert(int i, const QChar *unicode, int size) |
| 2614 | { |
| 2615 | if (i < 0 || size <= 0) |
| 2616 | return *this; |
| 2617 | |
| 2618 | const ushort *s = (const ushort *)unicode; |
| 2619 | const std::less<const ushort*> less = {}; |
| 2620 | if (!less(s, d->data()) && less(s, d->data() + d->alloc)) { |
| 2621 | // Part of me - take a copy |
| 2622 | const QVarLengthArray<ushort> copy(s, s + size); |
| 2623 | insert(i, unicode: reinterpret_cast<const QChar *>(copy.data()), size); |
| 2624 | return *this; |
| 2625 | } |
| 2626 | |
| 2627 | if (Q_UNLIKELY(i > d->size)) |
| 2628 | resize(size: i + size, fillChar: QLatin1Char(' ')); |
| 2629 | else |
| 2630 | resize(size: d->size + size); |
| 2631 | |
| 2632 | ::memmove(dest: d->data() + i + size, src: d->data() + i, n: (d->size - i - size) * sizeof(QChar)); |
| 2633 | memcpy(dest: d->data() + i, src: s, n: size * sizeof(QChar)); |
| 2634 | return *this; |
| 2635 | } |
| 2636 | |
| 2637 | /*! |
| 2638 | \fn QString& QString::insert(int position, QChar ch) |
| 2639 | \overload insert() |
| 2640 | |
| 2641 | Inserts \a ch at the given index \a position in the string. |
| 2642 | */ |
| 2643 | |
| 2644 | QString& QString::insert(int i, QChar ch) |
| 2645 | { |
| 2646 | if (i < 0) |
| 2647 | i += d->size; |
| 2648 | if (i < 0) |
| 2649 | return *this; |
| 2650 | if (Q_UNLIKELY(i > d->size)) |
| 2651 | resize(size: i + 1, fillChar: QLatin1Char(' ')); |
| 2652 | else |
| 2653 | resize(size: d->size + 1); |
| 2654 | ::memmove(dest: d->data() + i + 1, src: d->data() + i, n: (d->size - i - 1) * sizeof(QChar)); |
| 2655 | d->data()[i] = ch.unicode(); |
| 2656 | return *this; |
| 2657 | } |
| 2658 | |
| 2659 | /*! |
| 2660 | Appends the string \a str onto the end of this string. |
| 2661 | |
| 2662 | Example: |
| 2663 | |
| 2664 | \snippet qstring/main.cpp 9 |
| 2665 | |
| 2666 | This is the same as using the insert() function: |
| 2667 | |
| 2668 | \snippet qstring/main.cpp 10 |
| 2669 | |
| 2670 | The append() function is typically very fast (\l{constant time}), |
| 2671 | because QString preallocates extra space at the end of the string |
| 2672 | data so it can grow without reallocating the entire string each |
| 2673 | time. |
| 2674 | |
| 2675 | \sa operator+=(), prepend(), insert() |
| 2676 | */ |
| 2677 | QString &QString::append(const QString &str) |
| 2678 | { |
| 2679 | if (str.d != Data::sharedNull()) { |
| 2680 | if (d == Data::sharedNull()) { |
| 2681 | operator=(other: str); |
| 2682 | } else { |
| 2683 | if (d->ref.isShared() || uint(d->size + str.d->size) + 1u > d->alloc) |
| 2684 | reallocData(alloc: uint(d->size + str.d->size) + 1u, grow: true); |
| 2685 | memcpy(dest: d->data() + d->size, src: str.d->data(), n: str.d->size * sizeof(QChar)); |
| 2686 | d->size += str.d->size; |
| 2687 | d->data()[d->size] = '\0'; |
| 2688 | } |
| 2689 | } |
| 2690 | return *this; |
| 2691 | } |
| 2692 | |
| 2693 | /*! |
| 2694 | \overload append() |
| 2695 | \since 5.0 |
| 2696 | |
| 2697 | Appends \a len characters from the QChar array \a str to this string. |
| 2698 | */ |
| 2699 | QString &QString::append(const QChar *str, int len) |
| 2700 | { |
| 2701 | if (str && len > 0) { |
| 2702 | if (d->ref.isShared() || uint(d->size + len) + 1u > d->alloc) |
| 2703 | reallocData(alloc: uint(d->size + len) + 1u, grow: true); |
| 2704 | memcpy(dest: d->data() + d->size, src: str, n: len * sizeof(QChar)); |
| 2705 | d->size += len; |
| 2706 | d->data()[d->size] = '\0'; |
| 2707 | } |
| 2708 | return *this; |
| 2709 | } |
| 2710 | |
| 2711 | /*! |
| 2712 | \overload append() |
| 2713 | |
| 2714 | Appends the Latin-1 string \a str to this string. |
| 2715 | */ |
| 2716 | QString &QString::append(QLatin1String str) |
| 2717 | { |
| 2718 | const char *s = str.latin1(); |
| 2719 | if (s) { |
| 2720 | int len = str.size(); |
| 2721 | if (d->ref.isShared() || uint(d->size + len) + 1u > d->alloc) |
| 2722 | reallocData(alloc: uint(d->size + len) + 1u, grow: true); |
| 2723 | ushort *i = d->data() + d->size; |
| 2724 | qt_from_latin1(dst: i, str: s, size: uint(len)); |
| 2725 | i[len] = '\0'; |
| 2726 | d->size += len; |
| 2727 | } |
| 2728 | return *this; |
| 2729 | } |
| 2730 | |
| 2731 | /*! \fn QString &QString::append(const QByteArray &ba) |
| 2732 | |
| 2733 | \overload append() |
| 2734 | |
| 2735 | Appends the byte array \a ba to this string. The given byte array |
| 2736 | is converted to Unicode using the fromUtf8() function. |
| 2737 | |
| 2738 | You can disable this function by defining \c QT_NO_CAST_FROM_ASCII |
| 2739 | when you compile your applications. This can be useful if you want |
| 2740 | to ensure that all user-visible strings go through QObject::tr(), |
| 2741 | for example. |
| 2742 | |
| 2743 | \sa QT_NO_CAST_FROM_ASCII |
| 2744 | */ |
| 2745 | |
| 2746 | /*! \fn QString &QString::append(const char *str) |
| 2747 | |
| 2748 | \overload append() |
| 2749 | |
| 2750 | Appends the string \a str to this string. The given const char |
| 2751 | pointer is converted to Unicode using the fromUtf8() function. |
| 2752 | |
| 2753 | You can disable this function by defining \c QT_NO_CAST_FROM_ASCII |
| 2754 | when you compile your applications. This can be useful if you want |
| 2755 | to ensure that all user-visible strings go through QObject::tr(), |
| 2756 | for example. |
| 2757 | |
| 2758 | \sa QT_NO_CAST_FROM_ASCII |
| 2759 | */ |
| 2760 | |
| 2761 | /*! |
| 2762 | \overload append() |
| 2763 | |
| 2764 | Appends the character \a ch to this string. |
| 2765 | */ |
| 2766 | QString &QString::append(QChar ch) |
| 2767 | { |
| 2768 | if (d->ref.isShared() || uint(d->size) + 2u > d->alloc) |
| 2769 | reallocData(alloc: uint(d->size) + 2u, grow: true); |
| 2770 | d->data()[d->size++] = ch.unicode(); |
| 2771 | d->data()[d->size] = '\0'; |
| 2772 | return *this; |
| 2773 | } |
| 2774 | |
| 2775 | /*! \fn QString &QString::prepend(const QString &str) |
| 2776 | |
| 2777 | Prepends the string \a str to the beginning of this string and |
| 2778 | returns a reference to this string. |
| 2779 | |
| 2780 | Example: |
| 2781 | |
| 2782 | \snippet qstring/main.cpp 36 |
| 2783 | |
| 2784 | \sa append(), insert() |
| 2785 | */ |
| 2786 | |
| 2787 | /*! \fn QString &QString::prepend(QLatin1String str) |
| 2788 | |
| 2789 | \overload prepend() |
| 2790 | |
| 2791 | Prepends the Latin-1 string \a str to this string. |
| 2792 | */ |
| 2793 | |
| 2794 | /*! \fn QString &QString::prepend(const QChar *str, int len) |
| 2795 | \since 5.5 |
| 2796 | \overload prepend() |
| 2797 | |
| 2798 | Prepends \a len characters from the QChar array \a str to this string and |
| 2799 | returns a reference to this string. |
| 2800 | */ |
| 2801 | |
| 2802 | /*! \fn QString &QString::prepend(const QStringRef &str) |
| 2803 | \since 5.5 |
| 2804 | \overload prepend() |
| 2805 | |
| 2806 | Prepends the string reference \a str to the beginning of this string and |
| 2807 | returns a reference to this string. |
| 2808 | */ |
| 2809 | |
| 2810 | /*! |
| 2811 | \fn QString &QString::prepend(QStringView str) |
| 2812 | \since 5.15.2 |
| 2813 | |
| 2814 | Prepends the given string \a str to this string and returns the result. |
| 2815 | |
| 2816 | \note This method has been added in 5.15.2 to simplify writing code that is portable |
| 2817 | between Qt 5.15 and Qt 6. |
| 2818 | */ |
| 2819 | |
| 2820 | /*! \fn QString &QString::prepend(const QByteArray &ba) |
| 2821 | |
| 2822 | \overload prepend() |
| 2823 | |
| 2824 | Prepends the byte array \a ba to this string. The byte array is |
| 2825 | converted to Unicode using the fromUtf8() function. |
| 2826 | |
| 2827 | You can disable this function by defining \c |
| 2828 | QT_NO_CAST_FROM_ASCII when you compile your applications. This |
| 2829 | can be useful if you want to ensure that all user-visible strings |
| 2830 | go through QObject::tr(), for example. |
| 2831 | |
| 2832 | \sa QT_NO_CAST_FROM_ASCII |
| 2833 | */ |
| 2834 | |
| 2835 | /*! \fn QString &QString::prepend(const char *str) |
| 2836 | |
| 2837 | \overload prepend() |
| 2838 | |
| 2839 | Prepends the string \a str to this string. The const char pointer |
| 2840 | is converted to Unicode using the fromUtf8() function. |
| 2841 | |
| 2842 | You can disable this function by defining \c |
| 2843 | QT_NO_CAST_FROM_ASCII when you compile your applications. This |
| 2844 | can be useful if you want to ensure that all user-visible strings |
| 2845 | go through QObject::tr(), for example. |
| 2846 | |
| 2847 | \sa QT_NO_CAST_FROM_ASCII |
| 2848 | */ |
| 2849 | |
| 2850 | /*! \fn QString &QString::prepend(QChar ch) |
| 2851 | |
| 2852 | \overload prepend() |
| 2853 | |
| 2854 | Prepends the character \a ch to this string. |
| 2855 | */ |
| 2856 | |
| 2857 | /*! |
| 2858 | \fn QString &QString::remove(int position, int n) |
| 2859 | |
| 2860 | Removes \a n characters from the string, starting at the given \a |
| 2861 | position index, and returns a reference to the string. |
| 2862 | |
| 2863 | If the specified \a position index is within the string, but \a |
| 2864 | position + \a n is beyond the end of the string, the string is |
| 2865 | truncated at the specified \a position. |
| 2866 | |
| 2867 | \snippet qstring/main.cpp 37 |
| 2868 | |
| 2869 | \sa insert(), replace() |
| 2870 | */ |
| 2871 | QString &QString::remove(int pos, int len) |
| 2872 | { |
| 2873 | if (pos < 0) // count from end of string |
| 2874 | pos += d->size; |
| 2875 | if (uint(pos) >= uint(d->size)) { |
| 2876 | // range problems |
| 2877 | } else if (len >= d->size - pos) { |
| 2878 | resize(size: pos); // truncate |
| 2879 | } else if (len > 0) { |
| 2880 | detach(); |
| 2881 | memmove(dest: d->data() + pos, src: d->data() + pos + len, |
| 2882 | n: (d->size - pos - len + 1) * sizeof(ushort)); |
| 2883 | d->size -= len; |
| 2884 | } |
| 2885 | return *this; |
| 2886 | } |
| 2887 | |
| 2888 | template<typename T> |
| 2889 | static void removeStringImpl(QString &s, const T &needle, Qt::CaseSensitivity cs) |
| 2890 | { |
| 2891 | const auto needleSize = needle.size(); |
| 2892 | if (!needleSize) |
| 2893 | return; |
| 2894 | |
| 2895 | // avoid detach if nothing to do: |
| 2896 | int i = s.indexOf(needle, 0, cs); |
| 2897 | if (i < 0) |
| 2898 | return; |
| 2899 | |
| 2900 | const auto beg = s.begin(); // detaches |
| 2901 | auto dst = beg + i; |
| 2902 | auto src = beg + i + needleSize; |
| 2903 | const auto end = s.end(); |
| 2904 | // loop invariant: [beg, dst[ is partial result |
| 2905 | // [src, end[ still to be checked for needles |
| 2906 | while (src < end) { |
| 2907 | const auto i = s.indexOf(needle, src - beg, cs); |
| 2908 | const auto hit = i == -1 ? end : beg + i; |
| 2909 | const auto skipped = hit - src; |
| 2910 | memmove(dst, src, skipped * sizeof(QChar)); |
| 2911 | dst += skipped; |
| 2912 | src = hit + needleSize; |
| 2913 | } |
| 2914 | s.truncate(pos: dst - beg); |
| 2915 | } |
| 2916 | |
| 2917 | /*! |
| 2918 | Removes every occurrence of the given \a str string in this |
| 2919 | string, and returns a reference to this string. |
| 2920 | |
| 2921 | If \a cs is Qt::CaseSensitive (default), the search is |
| 2922 | case sensitive; otherwise the search is case insensitive. |
| 2923 | |
| 2924 | This is the same as \c replace(str, "", cs). |
| 2925 | |
| 2926 | \sa replace() |
| 2927 | */ |
| 2928 | QString &QString::remove(const QString &str, Qt::CaseSensitivity cs) |
| 2929 | { |
| 2930 | const auto s = reinterpret_cast<const ushort *>(str.data()); |
| 2931 | const std::less<const ushort *> less = {}; |
| 2932 | if (!less(s, d->data()) && less(s, d->data() + d->alloc)) { |
| 2933 | // Part of me - take a copy |
| 2934 | const QVarLengthArray<ushort> copy(s, s + str.size()); |
| 2935 | removeStringImpl(s&: *this, needle: QStringView{copy.data(), copy.size()}, cs); |
| 2936 | } else { |
| 2937 | removeStringImpl(s&: *this, needle: qToStringViewIgnoringNull(s: str), cs); |
| 2938 | } |
| 2939 | return *this; |
| 2940 | } |
| 2941 | |
| 2942 | /*! |
| 2943 | \since 5.11 |
| 2944 | \overload |
| 2945 | |
| 2946 | Removes every occurrence of the given \a str string in this |
| 2947 | string, and returns a reference to this string. |
| 2948 | |
| 2949 | If \a cs is Qt::CaseSensitive (default), the search is |
| 2950 | case sensitive; otherwise the search is case insensitive. |
| 2951 | |
| 2952 | This is the same as \c replace(str, "", cs). |
| 2953 | |
| 2954 | \sa replace() |
| 2955 | */ |
| 2956 | QString &QString::remove(QLatin1String str, Qt::CaseSensitivity cs) |
| 2957 | { |
| 2958 | removeStringImpl(s&: *this, needle: str, cs); |
| 2959 | return *this; |
| 2960 | } |
| 2961 | |
| 2962 | /*! |
| 2963 | Removes every occurrence of the character \a ch in this string, and |
| 2964 | returns a reference to this string. |
| 2965 | |
| 2966 | If \a cs is Qt::CaseSensitive (default), the search is case |
| 2967 | sensitive; otherwise the search is case insensitive. |
| 2968 | |
| 2969 | Example: |
| 2970 | |
| 2971 | \snippet qstring/main.cpp 38 |
| 2972 | |
| 2973 | This is the same as \c replace(ch, "", cs). |
| 2974 | |
| 2975 | \sa replace() |
| 2976 | */ |
| 2977 | QString &QString::remove(QChar ch, Qt::CaseSensitivity cs) |
| 2978 | { |
| 2979 | const int idx = indexOf(c: ch, from: 0, cs); |
| 2980 | if (idx != -1) { |
| 2981 | const auto first = begin(); // implicit detach() |
| 2982 | auto last = end(); |
| 2983 | if (cs == Qt::CaseSensitive) { |
| 2984 | last = std::remove(first: first + idx, last: last, value: ch); |
| 2985 | } else { |
| 2986 | const QChar c = ch.toCaseFolded(); |
| 2987 | auto caseInsensEqual = [c](QChar x) { |
| 2988 | return c == x.toCaseFolded(); |
| 2989 | }; |
| 2990 | last = std::remove_if(first: first + idx, last: last, pred: caseInsensEqual); |
| 2991 | } |
| 2992 | resize(size: last - first); |
| 2993 | } |
| 2994 | return *this; |
| 2995 | } |
| 2996 | |
| 2997 | /*! |
| 2998 | \fn QString &QString::remove(const QRegExp &rx) |
| 2999 | |
| 3000 | Removes every occurrence of the regular expression \a rx in the |
| 3001 | string, and returns a reference to the string. For example: |
| 3002 | |
| 3003 | \snippet qstring/main.cpp 39 |
| 3004 | |
| 3005 | \sa indexOf(), lastIndexOf(), replace() |
| 3006 | */ |
| 3007 | |
| 3008 | /*! |
| 3009 | \fn QString &QString::remove(const QRegularExpression &re) |
| 3010 | \since 5.0 |
| 3011 | |
| 3012 | Removes every occurrence of the regular expression \a re in the |
| 3013 | string, and returns a reference to the string. For example: |
| 3014 | |
| 3015 | \snippet qstring/main.cpp 96 |
| 3016 | |
| 3017 | \sa indexOf(), lastIndexOf(), replace() |
| 3018 | */ |
| 3019 | |
| 3020 | /*! |
| 3021 | \fn QString &QString::replace(int position, int n, const QString &after) |
| 3022 | |
| 3023 | Replaces \a n characters beginning at index \a position with |
| 3024 | the string \a after and returns a reference to this string. |
| 3025 | |
| 3026 | \note If the specified \a position index is within the string, |
| 3027 | but \a position + \a n goes outside the strings range, |
| 3028 | then \a n will be adjusted to stop at the end of the string. |
| 3029 | |
| 3030 | Example: |
| 3031 | |
| 3032 | \snippet qstring/main.cpp 40 |
| 3033 | |
| 3034 | \sa insert(), remove() |
| 3035 | */ |
| 3036 | QString &QString::replace(int pos, int len, const QString &after) |
| 3037 | { |
| 3038 | return replace(i: pos, len, s: after.constData(), slen: after.length()); |
| 3039 | } |
| 3040 | |
| 3041 | /*! |
| 3042 | \fn QString &QString::replace(int position, int n, const QChar *unicode, int size) |
| 3043 | \overload replace() |
| 3044 | Replaces \a n characters beginning at index \a position with the |
| 3045 | first \a size characters of the QChar array \a unicode and returns a |
| 3046 | reference to this string. |
| 3047 | */ |
| 3048 | QString &QString::replace(int pos, int len, const QChar *unicode, int size) |
| 3049 | { |
| 3050 | if (uint(pos) > uint(d->size)) |
| 3051 | return *this; |
| 3052 | if (len > d->size - pos) |
| 3053 | len = d->size - pos; |
| 3054 | |
| 3055 | uint index = pos; |
| 3056 | replace_helper(indices: &index, nIndices: 1, blen: len, after: unicode, alen: size); |
| 3057 | return *this; |
| 3058 | } |
| 3059 | |
| 3060 | /*! |
| 3061 | \fn QString &QString::replace(int position, int n, QChar after) |
| 3062 | \overload replace() |
| 3063 | |
| 3064 | Replaces \a n characters beginning at index \a position with the |
| 3065 | character \a after and returns a reference to this string. |
| 3066 | */ |
| 3067 | QString &QString::replace(int pos, int len, QChar after) |
| 3068 | { |
| 3069 | return replace(pos, len, unicode: &after, size: 1); |
| 3070 | } |
| 3071 | |
| 3072 | /*! |
| 3073 | \overload replace() |
| 3074 | Replaces every occurrence of the string \a before with the string \a |
| 3075 | after and returns a reference to this string. |
| 3076 | |
| 3077 | If \a cs is Qt::CaseSensitive (default), the search is case |
| 3078 | sensitive; otherwise the search is case insensitive. |
| 3079 | |
| 3080 | Example: |
| 3081 | |
| 3082 | \snippet qstring/main.cpp 41 |
| 3083 | |
| 3084 | \note The replacement text is not rescanned after it is inserted. |
| 3085 | |
| 3086 | Example: |
| 3087 | |
| 3088 | \snippet qstring/main.cpp 86 |
| 3089 | */ |
| 3090 | QString &QString::replace(const QString &before, const QString &after, Qt::CaseSensitivity cs) |
| 3091 | { |
| 3092 | return replace(before: before.constData(), blen: before.size(), after: after.constData(), alen: after.size(), cs); |
| 3093 | } |
| 3094 | |
| 3095 | namespace { // helpers for replace and its helper: |
| 3096 | QChar *textCopy(const QChar *start, int len) |
| 3097 | { |
| 3098 | const size_t size = len * sizeof(QChar); |
| 3099 | QChar *const copy = static_cast<QChar *>(::malloc(size: size)); |
| 3100 | Q_CHECK_PTR(copy); |
| 3101 | ::memcpy(dest: copy, src: start, n: size); |
| 3102 | return copy; |
| 3103 | } |
| 3104 | |
| 3105 | bool pointsIntoRange(const QChar *ptr, const ushort *base, int len) |
| 3106 | { |
| 3107 | const QChar *const start = reinterpret_cast<const QChar *>(base); |
| 3108 | const std::less<const QChar *> less = {}; |
| 3109 | return !less(ptr, start) && less(ptr, start + len); |
| 3110 | } |
| 3111 | } // end namespace |
| 3112 | |
| 3113 | /*! |
| 3114 | \internal |
| 3115 | */ |
| 3116 | void QString::replace_helper(uint *indices, int nIndices, int blen, const QChar *after, int alen) |
| 3117 | { |
| 3118 | // Copy after if it lies inside our own d->data() area (which we could |
| 3119 | // possibly invalidate via a realloc or modify by replacement). |
| 3120 | QChar *afterBuffer = nullptr; |
| 3121 | if (pointsIntoRange(ptr: after, base: d->data(), len: d->size)) // Use copy in place of vulnerable original: |
| 3122 | after = afterBuffer = textCopy(start: after, len: alen); |
| 3123 | |
| 3124 | QT_TRY { |
| 3125 | if (blen == alen) { |
| 3126 | // replace in place |
| 3127 | detach(); |
| 3128 | for (int i = 0; i < nIndices; ++i) |
| 3129 | memcpy(dest: d->data() + indices[i], src: after, n: alen * sizeof(QChar)); |
| 3130 | } else if (alen < blen) { |
| 3131 | // replace from front |
| 3132 | detach(); |
| 3133 | uint to = indices[0]; |
| 3134 | if (alen) |
| 3135 | memcpy(dest: d->data()+to, src: after, n: alen*sizeof(QChar)); |
| 3136 | to += alen; |
| 3137 | uint movestart = indices[0] + blen; |
| 3138 | for (int i = 1; i < nIndices; ++i) { |
| 3139 | int msize = indices[i] - movestart; |
| 3140 | if (msize > 0) { |
| 3141 | memmove(dest: d->data() + to, src: d->data() + movestart, n: msize * sizeof(QChar)); |
| 3142 | to += msize; |
| 3143 | } |
| 3144 | if (alen) { |
| 3145 | memcpy(dest: d->data() + to, src: after, n: alen * sizeof(QChar)); |
| 3146 | to += alen; |
| 3147 | } |
| 3148 | movestart = indices[i] + blen; |
| 3149 | } |
| 3150 | int msize = d->size - movestart; |
| 3151 | if (msize > 0) |
| 3152 | memmove(dest: d->data() + to, src: d->data() + movestart, n: msize * sizeof(QChar)); |
| 3153 | resize(size: d->size - nIndices*(blen-alen)); |
| 3154 | } else { |
| 3155 | // replace from back |
| 3156 | int adjust = nIndices*(alen-blen); |
| 3157 | int newLen = d->size + adjust; |
| 3158 | int moveend = d->size; |
| 3159 | resize(size: newLen); |
| 3160 | |
| 3161 | while (nIndices) { |
| 3162 | --nIndices; |
| 3163 | int movestart = indices[nIndices] + blen; |
| 3164 | int insertstart = indices[nIndices] + nIndices*(alen-blen); |
| 3165 | int moveto = insertstart + alen; |
| 3166 | memmove(dest: d->data() + moveto, src: d->data() + movestart, |
| 3167 | n: (moveend - movestart)*sizeof(QChar)); |
| 3168 | memcpy(dest: d->data() + insertstart, src: after, n: alen * sizeof(QChar)); |
| 3169 | moveend = movestart-blen; |
| 3170 | } |
| 3171 | } |
| 3172 | } QT_CATCH(const std::bad_alloc &) { |
| 3173 | ::free(ptr: afterBuffer); |
| 3174 | QT_RETHROW; |
| 3175 | } |
| 3176 | ::free(ptr: afterBuffer); |
| 3177 | } |
| 3178 | |
| 3179 | /*! |
| 3180 | \since 4.5 |
| 3181 | \overload replace() |
| 3182 | |
| 3183 | Replaces each occurrence in this string of the first \a blen |
| 3184 | characters of \a before with the first \a alen characters of \a |
| 3185 | after and returns a reference to this string. |
| 3186 | |
| 3187 | If \a cs is Qt::CaseSensitive (default), the search is case |
| 3188 | sensitive; otherwise the search is case insensitive. |
| 3189 | */ |
| 3190 | QString &QString::replace(const QChar *before, int blen, |
| 3191 | const QChar *after, int alen, |
| 3192 | Qt::CaseSensitivity cs) |
| 3193 | { |
| 3194 | if (d->size == 0) { |
| 3195 | if (blen) |
| 3196 | return *this; |
| 3197 | } else { |
| 3198 | if (cs == Qt::CaseSensitive && before == after && blen == alen) |
| 3199 | return *this; |
| 3200 | } |
| 3201 | if (alen == 0 && blen == 0) |
| 3202 | return *this; |
| 3203 | |
| 3204 | QStringMatcher matcher(before, blen, cs); |
| 3205 | QChar *beforeBuffer = nullptr, *afterBuffer = nullptr; |
| 3206 | |
| 3207 | int index = 0; |
| 3208 | while (1) { |
| 3209 | uint indices[1024]; |
| 3210 | uint pos = 0; |
| 3211 | while (pos < 1024) { |
| 3212 | index = matcher.indexIn(str: *this, from: index); |
| 3213 | if (index == -1) |
| 3214 | break; |
| 3215 | indices[pos++] = index; |
| 3216 | if (blen) // Step over before: |
| 3217 | index += blen; |
| 3218 | else // Only count one instance of empty between any two characters: |
| 3219 | index++; |
| 3220 | } |
| 3221 | if (!pos) // Nothing to replace |
| 3222 | break; |
| 3223 | |
| 3224 | if (Q_UNLIKELY(index != -1)) { |
| 3225 | /* |
| 3226 | We're about to change data, that before and after might point |
| 3227 | into, and we'll need that data for our next batch of indices. |
| 3228 | */ |
| 3229 | if (!afterBuffer && pointsIntoRange(ptr: after, base: d->data(), len: d->size)) |
| 3230 | after = afterBuffer = textCopy(start: after, len: alen); |
| 3231 | |
| 3232 | if (!beforeBuffer && pointsIntoRange(ptr: before, base: d->data(), len: d->size)) { |
| 3233 | beforeBuffer = textCopy(start: before, len: blen); |
| 3234 | matcher = QStringMatcher(beforeBuffer, blen, cs); |
| 3235 | } |
| 3236 | } |
| 3237 | |
| 3238 | replace_helper(indices, nIndices: pos, blen, after, alen); |
| 3239 | |
| 3240 | if (Q_LIKELY(index == -1)) // Nothing left to replace |
| 3241 | break; |
| 3242 | // The call to replace_helper just moved what index points at: |
| 3243 | index += pos*(alen-blen); |
| 3244 | } |
| 3245 | ::free(ptr: afterBuffer); |
| 3246 | ::free(ptr: beforeBuffer); |
| 3247 | |
| 3248 | return *this; |
| 3249 | } |
| 3250 | |
| 3251 | /*! |
| 3252 | \overload replace() |
| 3253 | Replaces every occurrence of the character \a ch in the string with |
| 3254 | \a after and returns a reference to this string. |
| 3255 | |
| 3256 | If \a cs is Qt::CaseSensitive (default), the search is case |
| 3257 | sensitive; otherwise the search is case insensitive. |
| 3258 | */ |
| 3259 | QString& QString::replace(QChar ch, const QString &after, Qt::CaseSensitivity cs) |
| 3260 | { |
| 3261 | if (after.d->size == 0) |
| 3262 | return remove(ch, cs); |
| 3263 | |
| 3264 | if (after.d->size == 1) |
| 3265 | return replace(before: ch, after: after.front(), cs); |
| 3266 | |
| 3267 | if (d->size == 0) |
| 3268 | return *this; |
| 3269 | |
| 3270 | ushort cc = (cs == Qt::CaseSensitive ? ch.unicode() : ch.toCaseFolded().unicode()); |
| 3271 | |
| 3272 | int index = 0; |
| 3273 | while (1) { |
| 3274 | uint indices[1024]; |
| 3275 | uint pos = 0; |
| 3276 | if (cs == Qt::CaseSensitive) { |
| 3277 | while (pos < 1024 && index < d->size) { |
| 3278 | if (d->data()[index] == cc) |
| 3279 | indices[pos++] = index; |
| 3280 | index++; |
| 3281 | } |
| 3282 | } else { |
| 3283 | while (pos < 1024 && index < d->size) { |
| 3284 | if (QChar::toCaseFolded(ucs4: d->data()[index]) == cc) |
| 3285 | indices[pos++] = index; |
| 3286 | index++; |
| 3287 | } |
| 3288 | } |
| 3289 | if (!pos) // Nothing to replace |
| 3290 | break; |
| 3291 | |
| 3292 | replace_helper(indices, nIndices: pos, blen: 1, after: after.constData(), alen: after.d->size); |
| 3293 | |
| 3294 | if (Q_LIKELY(index == size())) // Nothing left to replace |
| 3295 | break; |
| 3296 | // The call to replace_helper just moved what index points at: |
| 3297 | index += pos*(after.d->size - 1); |
| 3298 | } |
| 3299 | return *this; |
| 3300 | } |
| 3301 | |
| 3302 | /*! |
| 3303 | \overload replace() |
| 3304 | Replaces every occurrence of the character \a before with the |
| 3305 | character \a after and returns a reference to this string. |
| 3306 | |
| 3307 | If \a cs is Qt::CaseSensitive (default), the search is case |
| 3308 | sensitive; otherwise the search is case insensitive. |
| 3309 | */ |
| 3310 | QString& QString::replace(QChar before, QChar after, Qt::CaseSensitivity cs) |
| 3311 | { |
| 3312 | if (d->size) { |
| 3313 | const int idx = indexOf(c: before, from: 0, cs); |
| 3314 | if (idx != -1) { |
| 3315 | detach(); |
| 3316 | const ushort a = after.unicode(); |
| 3317 | ushort *i = d->data(); |
| 3318 | const ushort *e = i + d->size; |
| 3319 | i += idx; |
| 3320 | *i = a; |
| 3321 | if (cs == Qt::CaseSensitive) { |
| 3322 | const ushort b = before.unicode(); |
| 3323 | while (++i != e) { |
| 3324 | if (*i == b) |
| 3325 | *i = a; |
| 3326 | } |
| 3327 | } else { |
| 3328 | const ushort b = foldCase(ch: before.unicode()); |
| 3329 | while (++i != e) { |
| 3330 | if (foldCase(ch: *i) == b) |
| 3331 | *i = a; |
| 3332 | } |
| 3333 | } |
| 3334 | } |
| 3335 | } |
| 3336 | return *this; |
| 3337 | } |
| 3338 | |
| 3339 | /*! |
| 3340 | \since 4.5 |
| 3341 | \overload replace() |
| 3342 | |
| 3343 | Replaces every occurrence of the string \a before with the string \a |
| 3344 | after and returns a reference to this string. |
| 3345 | |
| 3346 | If \a cs is Qt::CaseSensitive (default), the search is case |
| 3347 | sensitive; otherwise the search is case insensitive. |
| 3348 | |
| 3349 | \note The text is not rescanned after a replacement. |
| 3350 | */ |
| 3351 | QString &QString::replace(QLatin1String before, QLatin1String after, Qt::CaseSensitivity cs) |
| 3352 | { |
| 3353 | int alen = after.size(); |
| 3354 | int blen = before.size(); |
| 3355 | QVarLengthArray<ushort> a(alen); |
| 3356 | QVarLengthArray<ushort> b(blen); |
| 3357 | qt_from_latin1(dst: a.data(), str: after.latin1(), size: alen); |
| 3358 | qt_from_latin1(dst: b.data(), str: before.latin1(), size: blen); |
| 3359 | return replace(before: (const QChar *)b.data(), blen, after: (const QChar *)a.data(), alen, cs); |
| 3360 | } |
| 3361 | |
| 3362 | /*! |
| 3363 | \since 4.5 |
| 3364 | \overload replace() |
| 3365 | |
| 3366 | Replaces every occurrence of the string \a before with the string \a |
| 3367 | after and returns a reference to this string. |
| 3368 | |
| 3369 | If \a cs is Qt::CaseSensitive (default), the search is case |
| 3370 | sensitive; otherwise the search is case insensitive. |
| 3371 | |
| 3372 | \note The text is not rescanned after a replacement. |
| 3373 | */ |
| 3374 | QString &QString::replace(QLatin1String before, const QString &after, Qt::CaseSensitivity cs) |
| 3375 | { |
| 3376 | int blen = before.size(); |
| 3377 | QVarLengthArray<ushort> b(blen); |
| 3378 | qt_from_latin1(dst: b.data(), str: before.latin1(), size: blen); |
| 3379 | return replace(before: (const QChar *)b.data(), blen, after: after.constData(), alen: after.d->size, cs); |
| 3380 | } |
| 3381 | |
| 3382 | /*! |
| 3383 | \since 4.5 |
| 3384 | \overload replace() |
| 3385 | |
| 3386 | Replaces every occurrence of the string \a before with the string \a |
| 3387 | after and returns a reference to this string. |
| 3388 | |
| 3389 | If \a cs is Qt::CaseSensitive (default), the search is case |
| 3390 | sensitive; otherwise the search is case insensitive. |
| 3391 | |
| 3392 | \note The text is not rescanned after a replacement. |
| 3393 | */ |
| 3394 | QString &QString::replace(const QString &before, QLatin1String after, Qt::CaseSensitivity cs) |
| 3395 | { |
| 3396 | int alen = after.size(); |
| 3397 | QVarLengthArray<ushort> a(alen); |
| 3398 | qt_from_latin1(dst: a.data(), str: after.latin1(), size: alen); |
| 3399 | return replace(before: before.constData(), blen: before.d->size, after: (const QChar *)a.data(), alen, cs); |
| 3400 | } |
| 3401 | |
| 3402 | /*! |
| 3403 | \since 4.5 |
| 3404 | \overload replace() |
| 3405 | |
| 3406 | Replaces every occurrence of the character \a c with the string \a |
| 3407 | after and returns a reference to this string. |
| 3408 | |
| 3409 | If \a cs is Qt::CaseSensitive (default), the search is case |
| 3410 | sensitive; otherwise the search is case insensitive. |
| 3411 | |
| 3412 | \note The text is not rescanned after a replacement. |
| 3413 | */ |
| 3414 | QString &QString::replace(QChar c, QLatin1String after, Qt::CaseSensitivity cs) |
| 3415 | { |
| 3416 | int alen = after.size(); |
| 3417 | QVarLengthArray<ushort> a(alen); |
| 3418 | qt_from_latin1(dst: a.data(), str: after.latin1(), size: alen); |
| 3419 | return replace(before: &c, blen: 1, after: (const QChar *)a.data(), alen, cs); |
| 3420 | } |
| 3421 | |
| 3422 | |
| 3423 | /*! |
| 3424 | \relates QString |
| 3425 | Returns \c true if string \a s1 is equal to string \a s2; otherwise |
| 3426 | returns \c false. |
| 3427 | |
| 3428 | \sa {Comparing Strings} |
| 3429 | */ |
| 3430 | bool operator==(const QString &s1, const QString &s2) noexcept |
| 3431 | { |
| 3432 | if (s1.d->size != s2.d->size) |
| 3433 | return false; |
| 3434 | |
| 3435 | return qt_compare_strings(lhs: s1, rhs: s2, cs: Qt::CaseSensitive) == 0; |
| 3436 | } |
| 3437 | |
| 3438 | /*! |
| 3439 | \overload operator==() |
| 3440 | Returns \c true if this string is equal to \a other; otherwise |
| 3441 | returns \c false. |
| 3442 | */ |
| 3443 | bool QString::operator==(QLatin1String other) const noexcept |
| 3444 | { |
| 3445 | if (d->size != other.size()) |
| 3446 | return false; |
| 3447 | |
| 3448 | return qt_compare_strings(lhs: *this, rhs: other, cs: Qt::CaseSensitive) == 0; |
| 3449 | } |
| 3450 | |
| 3451 | /*! \fn bool QString::operator==(const QByteArray &other) const |
| 3452 | |
| 3453 | \overload operator==() |
| 3454 | |
| 3455 | The \a other byte array is converted to a QString using the |
| 3456 | fromUtf8() function. This function stops conversion at the |
| 3457 | first NUL character found, or the end of the byte array. |
| 3458 | |
| 3459 | You can disable this operator by defining \c |
| 3460 | QT_NO_CAST_FROM_ASCII when you compile your applications. This |
| 3461 | can be useful if you want to ensure that all user-visible strings |
| 3462 | go through QObject::tr(), for example. |
| 3463 | |
| 3464 | Returns \c true if this string is lexically equal to the parameter |
| 3465 | string \a other. Otherwise returns \c false. |
| 3466 | |
| 3467 | \sa QT_NO_CAST_FROM_ASCII |
| 3468 | */ |
| 3469 | |
| 3470 | /*! \fn bool QString::operator==(const char *other) const |
| 3471 | |
| 3472 | \overload operator==() |
| 3473 | |
| 3474 | The \a other const char pointer is converted to a QString using |
| 3475 | the fromUtf8() function. |
| 3476 | |
| 3477 | You can disable this operator by defining \c |
| 3478 | QT_NO_CAST_FROM_ASCII when you compile your applications. This |
| 3479 | can be useful if you want to ensure that all user-visible strings |
| 3480 | go through QObject::tr(), for example. |
| 3481 | |
| 3482 | \sa QT_NO_CAST_FROM_ASCII |
| 3483 | */ |
| 3484 | |
| 3485 | /*! |
| 3486 | \relates QString |
| 3487 | Returns \c true if string \a s1 is lexically less than string |
| 3488 | \a s2; otherwise returns \c false. |
| 3489 | |
| 3490 | \sa {Comparing Strings} |
| 3491 | */ |
| 3492 | bool operator<(const QString &s1, const QString &s2) noexcept |
| 3493 | { |
| 3494 | return qt_compare_strings(lhs: s1, rhs: s2, cs: Qt::CaseSensitive) < 0; |
| 3495 | } |
| 3496 | |
| 3497 | /*! |
| 3498 | \overload operator<() |
| 3499 | |
| 3500 | Returns \c true if this string is lexically less than the parameter |
| 3501 | string called \a other; otherwise returns \c false. |
| 3502 | */ |
| 3503 | bool QString::operator<(QLatin1String other) const noexcept |
| 3504 | { |
| 3505 | return qt_compare_strings(lhs: *this, rhs: other, cs: Qt::CaseSensitive) < 0; |
| 3506 | } |
| 3507 | |
| 3508 | /*! \fn bool QString::operator<(const QByteArray &other) const |
| 3509 | |
| 3510 | \overload operator<() |
| 3511 | |
| 3512 | The \a other byte array is converted to a QString using the |
| 3513 | fromUtf8() function. If any NUL characters ('\\0') are embedded |
| 3514 | in the byte array, they will be included in the transformation. |
| 3515 | |
| 3516 | You can disable this operator by defining \c |
| 3517 | QT_NO_CAST_FROM_ASCII when you compile your applications. This |
| 3518 | can be useful if you want to ensure that all user-visible strings |
| 3519 | go through QObject::tr(), for example. |
| 3520 | |
| 3521 | \sa QT_NO_CAST_FROM_ASCII |
| 3522 | */ |
| 3523 | |
| 3524 | /*! \fn bool QString::operator<(const char *other) const |
| 3525 | |
| 3526 | Returns \c true if this string is lexically less than string \a other. |
| 3527 | Otherwise returns \c false. |
| 3528 | |
| 3529 | \overload operator<() |
| 3530 | |
| 3531 | The \a other const char pointer is converted to a QString using |
| 3532 | the fromUtf8() function. |
| 3533 | |
| 3534 | You can disable this operator by defining \c |
| 3535 | QT_NO_CAST_FROM_ASCII when you compile your applications. This |
| 3536 | can be useful if you want to ensure that all user-visible strings |
| 3537 | go through QObject::tr(), for example. |
| 3538 | |
| 3539 | \sa QT_NO_CAST_FROM_ASCII |
| 3540 | */ |
| 3541 | |
| 3542 | /*! \fn bool operator<=(const QString &s1, const QString &s2) |
| 3543 | |
| 3544 | \relates QString |
| 3545 | |
| 3546 | Returns \c true if string \a s1 is lexically less than or equal to |
| 3547 | string \a s2; otherwise returns \c false. |
| 3548 | |
| 3549 | \sa {Comparing Strings} |
| 3550 | */ |
| 3551 | |
| 3552 | /*! \fn bool QString::operator<=(QLatin1String other) const |
| 3553 | |
| 3554 | Returns \c true if this string is lexically less than or equal to |
| 3555 | parameter string \a other. Otherwise returns \c false. |
| 3556 | |
| 3557 | \overload operator<=() |
| 3558 | */ |
| 3559 | |
| 3560 | /*! \fn bool QString::operator<=(const QByteArray &other) const |
| 3561 | |
| 3562 | \overload operator<=() |
| 3563 | |
| 3564 | The \a other byte array is converted to a QString using the |
| 3565 | fromUtf8() function. If any NUL characters ('\\0') are embedded |
| 3566 | in the byte array, they will be included in the transformation. |
| 3567 | |
| 3568 | You can disable this operator by defining \c |
| 3569 | QT_NO_CAST_FROM_ASCII when you compile your applications. This |
| 3570 | can be useful if you want to ensure that all user-visible strings |
| 3571 | go through QObject::tr(), for example. |
| 3572 | |
| 3573 | \sa QT_NO_CAST_FROM_ASCII |
| 3574 | */ |
| 3575 | |
| 3576 | /*! \fn bool QString::operator<=(const char *other) const |
| 3577 | |
| 3578 | \overload operator<=() |
| 3579 | |
| 3580 | The \a other const char pointer is converted to a QString using |
| 3581 | the fromUtf8() function. |
| 3582 | |
| 3583 | You can disable this operator by defining \c |
| 3584 | QT_NO_CAST_FROM_ASCII when you compile your applications. This |
| 3585 | can be useful if you want to ensure that all user-visible strings |
| 3586 | go through QObject::tr(), for example. |
| 3587 | |
| 3588 | \sa QT_NO_CAST_FROM_ASCII |
| 3589 | */ |
| 3590 | |
| 3591 | /*! \fn bool operator>(const QString &s1, const QString &s2) |
| 3592 | \relates QString |
| 3593 | |
| 3594 | Returns \c true if string \a s1 is lexically greater than string \a s2; |
| 3595 | otherwise returns \c false. |
| 3596 | |
| 3597 | \sa {Comparing Strings} |
| 3598 | */ |
| 3599 | |
| 3600 | /*! |
| 3601 | \overload operator>() |
| 3602 | |
| 3603 | Returns \c true if this string is lexically greater than the parameter |
| 3604 | string \a other; otherwise returns \c false. |
| 3605 | */ |
| 3606 | bool QString::operator>(QLatin1String other) const noexcept |
| 3607 | { |
| 3608 | return qt_compare_strings(lhs: *this, rhs: other, cs: Qt::CaseSensitive) > 0; |
| 3609 | } |
| 3610 | |
| 3611 | /*! \fn bool QString::operator>(const QByteArray &other) const |
| 3612 | |
| 3613 | \overload operator>() |
| 3614 | |
| 3615 | The \a other byte array is converted to a QString using the |
| 3616 | fromUtf8() function. If any NUL characters ('\\0') are embedded |
| 3617 | in the byte array, they will be included in the transformation. |
| 3618 | |
| 3619 | You can disable this operator by defining \c |
| 3620 | QT_NO_CAST_FROM_ASCII when you compile your applications. This |
| 3621 | can be useful if you want to ensure that all user-visible strings |
| 3622 | go through QObject::tr(), for example. |
| 3623 | |
| 3624 | \sa QT_NO_CAST_FROM_ASCII |
| 3625 | */ |
| 3626 | |
| 3627 | /*! \fn bool QString::operator>(const char *other) const |
| 3628 | |
| 3629 | \overload operator>() |
| 3630 | |
| 3631 | The \a other const char pointer is converted to a QString using |
| 3632 | the fromUtf8() function. |
| 3633 | |
| 3634 | You can disable this operator by defining \c QT_NO_CAST_FROM_ASCII |
| 3635 | when you compile your applications. This can be useful if you want |
| 3636 | to ensure that all user-visible strings go through QObject::tr(), |
| 3637 | for example. |
| 3638 | |
| 3639 | \sa QT_NO_CAST_FROM_ASCII |
| 3640 | */ |
| 3641 | |
| 3642 | /*! \fn bool operator>=(const QString &s1, const QString &s2) |
| 3643 | \relates QString |
| 3644 | |
| 3645 | Returns \c true if string \a s1 is lexically greater than or equal to |
| 3646 | string \a s2; otherwise returns \c false. |
| 3647 | |
| 3648 | \sa {Comparing Strings} |
| 3649 | */ |
| 3650 | |
| 3651 | /*! \fn bool QString::operator>=(QLatin1String other) const |
| 3652 | |
| 3653 | Returns \c true if this string is lexically greater than or equal to parameter |
| 3654 | string \a other. Otherwise returns \c false. |
| 3655 | |
| 3656 | \overload operator>=() |
| 3657 | */ |
| 3658 | |
| 3659 | /*! \fn bool QString::operator>=(const QByteArray &other) const |
| 3660 | |
| 3661 | \overload operator>=() |
| 3662 | |
| 3663 | The \a other byte array is converted to a QString using the |
| 3664 | fromUtf8() function. If any NUL characters ('\\0') are embedded in |
| 3665 | the byte array, they will be included in the transformation. |
| 3666 | |
| 3667 | You can disable this operator by defining \c QT_NO_CAST_FROM_ASCII |
| 3668 | when you compile your applications. This can be useful if you want |
| 3669 | to ensure that all user-visible strings go through QObject::tr(), |
| 3670 | for example. |
| 3671 | |
| 3672 | \sa QT_NO_CAST_FROM_ASCII |
| 3673 | */ |
| 3674 | |
| 3675 | /*! \fn bool QString::operator>=(const char *other) const |
| 3676 | |
| 3677 | \overload operator>=() |
| 3678 | |
| 3679 | The \a other const char pointer is converted to a QString using |
| 3680 | the fromUtf8() function. |
| 3681 | |
| 3682 | You can disable this operator by defining \c QT_NO_CAST_FROM_ASCII |
| 3683 | when you compile your applications. This can be useful if you want |
| 3684 | to ensure that all user-visible strings go through QObject::tr(), |
| 3685 | for example. |
| 3686 | |
| 3687 | \sa QT_NO_CAST_FROM_ASCII |
| 3688 | */ |
| 3689 | |
| 3690 | /*! \fn bool operator!=(const QString &s1, const QString &s2) |
| 3691 | \relates QString |
| 3692 | |
| 3693 | Returns \c true if string \a s1 is not equal to string \a s2; |
| 3694 | otherwise returns \c false. |
| 3695 | |
| 3696 | \sa {Comparing Strings} |
| 3697 | */ |
| 3698 | |
| 3699 | /*! \fn bool QString::operator!=(QLatin1String other) const |
| 3700 | |
| 3701 | Returns \c true if this string is not equal to parameter string \a other. |
| 3702 | Otherwise returns \c false. |
| 3703 | |
| 3704 | \overload operator!=() |
| 3705 | */ |
| 3706 | |
| 3707 | /*! \fn bool QString::operator!=(const QByteArray &other) const |
| 3708 | |
| 3709 | \overload operator!=() |
| 3710 | |
| 3711 | The \a other byte array is converted to a QString using the |
| 3712 | fromUtf8() function. If any NUL characters ('\\0') are embedded |
| 3713 | in the byte array, they will be included in the transformation. |
| 3714 | |
| 3715 | You can disable this operator by defining \c QT_NO_CAST_FROM_ASCII |
| 3716 | when you compile your applications. This can be useful if you want |
| 3717 | to ensure that all user-visible strings go through QObject::tr(), |
| 3718 | for example. |
| 3719 | |
| 3720 | \sa QT_NO_CAST_FROM_ASCII |
| 3721 | */ |
| 3722 | |
| 3723 | /*! \fn bool QString::operator!=(const char *other) const |
| 3724 | |
| 3725 | \overload operator!=() |
| 3726 | |
| 3727 | The \a other const char pointer is converted to a QString using |
| 3728 | the fromUtf8() function. |
| 3729 | |
| 3730 | You can disable this operator by defining \c |
| 3731 | QT_NO_CAST_FROM_ASCII when you compile your applications. This |
| 3732 | can be useful if you want to ensure that all user-visible strings |
| 3733 | go through QObject::tr(), for example. |
| 3734 | |
| 3735 | \sa QT_NO_CAST_FROM_ASCII |
| 3736 | */ |
| 3737 | |
| 3738 | #if QT_STRINGVIEW_LEVEL < 2 |
| 3739 | /*! |
| 3740 | Returns the index position of the first occurrence of the string \a |
| 3741 | str in this string, searching forward from index position \a |
| 3742 | from. Returns -1 if \a str is not found. |
| 3743 | |
| 3744 | If \a cs is Qt::CaseSensitive (default), the search is case |
| 3745 | sensitive; otherwise the search is case insensitive. |
| 3746 | |
| 3747 | Example: |
| 3748 | |
| 3749 | \snippet qstring/main.cpp 24 |
| 3750 | |
| 3751 | If \a from is -1, the search starts at the last character; if it is |
| 3752 | -2, at the next to last character and so on. |
| 3753 | |
| 3754 | \sa lastIndexOf(), contains(), count() |
| 3755 | */ |
| 3756 | int QString::indexOf(const QString &str, int from, Qt::CaseSensitivity cs) const |
| 3757 | { |
| 3758 | // ### Qt6: qsizetype |
| 3759 | return int(QtPrivate::findString(haystack: QStringView(unicode(), length()), from, needle: QStringView(str.unicode(), str.length()), cs)); |
| 3760 | } |
| 3761 | #endif // QT_STRINGVIEW_LEVEL < 2 |
| 3762 | |
| 3763 | /*! |
| 3764 | \fn int QString::indexOf(QStringView str, int from, Qt::CaseSensitivity cs) const |
| 3765 | \since 5.14 |
| 3766 | \overload indexOf() |
| 3767 | |
| 3768 | Returns the index position of the first occurrence of the string view \a str |
| 3769 | in this string, searching forward from index position \a from. |
| 3770 | Returns -1 if \a str is not found. |
| 3771 | |
| 3772 | If \a cs is Qt::CaseSensitive (default), the search is case |
| 3773 | sensitive; otherwise the search is case insensitive. |
| 3774 | |
| 3775 | If \a from is -1, the search starts at the last character; if it is |
| 3776 | -2, at the next to last character and so on. |
| 3777 | |
| 3778 | \sa QStringView::indexOf(), lastIndexOf(), contains(), count() |
| 3779 | */ |
| 3780 | |
| 3781 | /*! |
| 3782 | \since 4.5 |
| 3783 | Returns the index position of the first occurrence of the string \a |
| 3784 | str in this string, searching forward from index position \a |
| 3785 | from. Returns -1 if \a str is not found. |
| 3786 | |
| 3787 | If \a cs is Qt::CaseSensitive (default), the search is case |
| 3788 | sensitive; otherwise the search is case insensitive. |
| 3789 | |
| 3790 | Example: |
| 3791 | |
| 3792 | \snippet qstring/main.cpp 24 |
| 3793 | |
| 3794 | If \a from is -1, the search starts at the last character; if it is |
| 3795 | -2, at the next to last character and so on. |
| 3796 | |
| 3797 | \sa lastIndexOf(), contains(), count() |
| 3798 | */ |
| 3799 | |
| 3800 | int QString::indexOf(QLatin1String str, int from, Qt::CaseSensitivity cs) const |
| 3801 | { |
| 3802 | // ### Qt6: qsizetype |
| 3803 | return int(QtPrivate::findString(haystack: QStringView(unicode(), size()), from, needle: str, cs)); |
| 3804 | } |
| 3805 | |
| 3806 | /*! |
| 3807 | \overload indexOf() |
| 3808 | |
| 3809 | Returns the index position of the first occurrence of the |
| 3810 | character \a ch in the string, searching forward from index |
| 3811 | position \a from. Returns -1 if \a ch could not be found. |
| 3812 | */ |
| 3813 | int QString::indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const |
| 3814 | { |
| 3815 | // ### Qt6: qsizetype |
| 3816 | return int(qFindChar(str: QStringView(unicode(), length()), ch, from, cs)); |
| 3817 | } |
| 3818 | |
| 3819 | #if QT_STRINGVIEW_LEVEL < 2 |
| 3820 | /*! |
| 3821 | \since 4.8 |
| 3822 | |
| 3823 | \overload indexOf() |
| 3824 | |
| 3825 | Returns the index position of the first occurrence of the string |
| 3826 | reference \a str in this string, searching forward from index |
| 3827 | position \a from. Returns -1 if \a str is not found. |
| 3828 | |
| 3829 | If \a cs is Qt::CaseSensitive (default), the search is case |
| 3830 | sensitive; otherwise the search is case insensitive. |
| 3831 | */ |
| 3832 | int QString::indexOf(const QStringRef &str, int from, Qt::CaseSensitivity cs) const |
| 3833 | { |
| 3834 | // ### Qt6: qsizetype |
| 3835 | return int(QtPrivate::findString(haystack: QStringView(unicode(), length()), from, needle: QStringView(str.unicode(), str.length()), cs)); |
| 3836 | } |
| 3837 | |
| 3838 | /*! |
| 3839 | Returns the index position of the last occurrence of the string \a |
| 3840 | str in this string, searching backward from index position \a |
| 3841 | from. If \a from is -1 (default), the search starts at the last |
| 3842 | character; if \a from is -2, at the next to last character and so |
| 3843 | on. Returns -1 if \a str is not found. |
| 3844 | |
| 3845 | If \a cs is Qt::CaseSensitive (default), the search is case |
| 3846 | sensitive; otherwise the search is case insensitive. |
| 3847 | |
| 3848 | Example: |
| 3849 | |
| 3850 | \snippet qstring/main.cpp 29 |
| 3851 | |
| 3852 | \sa indexOf(), contains(), count() |
| 3853 | */ |
| 3854 | int QString::lastIndexOf(const QString &str, int from, Qt::CaseSensitivity cs) const |
| 3855 | { |
| 3856 | // ### Qt6: qsizetype |
| 3857 | return int(QtPrivate::lastIndexOf(haystack: QStringView(*this), from, needle: str, cs)); |
| 3858 | } |
| 3859 | |
| 3860 | #endif // QT_STRINGVIEW_LEVEL < 2 |
| 3861 | |
| 3862 | /*! |
| 3863 | \since 4.5 |
| 3864 | \overload lastIndexOf() |
| 3865 | |
| 3866 | Returns the index position of the last occurrence of the string \a |
| 3867 | str in this string, searching backward from index position \a |
| 3868 | from. If \a from is -1 (default), the search starts at the last |
| 3869 | character; if \a from is -2, at the next to last character and so |
| 3870 | on. Returns -1 if \a str is not found. |
| 3871 | |
| 3872 | If \a cs is Qt::CaseSensitive (default), the search is case |
| 3873 | sensitive; otherwise the search is case insensitive. |
| 3874 | |
| 3875 | Example: |
| 3876 | |
| 3877 | \snippet qstring/main.cpp 29 |
| 3878 | |
| 3879 | \sa indexOf(), contains(), count() |
| 3880 | */ |
| 3881 | int QString::lastIndexOf(QLatin1String str, int from, Qt::CaseSensitivity cs) const |
| 3882 | { |
| 3883 | // ### Qt6: qsizetype |
| 3884 | return int(QtPrivate::lastIndexOf(haystack: *this, from, needle: str, cs)); |
| 3885 | } |
| 3886 | |
| 3887 | /*! |
| 3888 | \overload lastIndexOf() |
| 3889 | |
| 3890 | Returns the index position of the last occurrence of the character |
| 3891 | \a ch, searching backward from position \a from. |
| 3892 | */ |
| 3893 | int QString::lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) const |
| 3894 | { |
| 3895 | // ### Qt6: qsizetype |
| 3896 | return int(qLastIndexOf(haystack: QStringView(*this), needle: ch, from, cs)); |
| 3897 | } |
| 3898 | |
| 3899 | #if QT_STRINGVIEW_LEVEL < 2 |
| 3900 | /*! |
| 3901 | \since 4.8 |
| 3902 | \overload lastIndexOf() |
| 3903 | |
| 3904 | Returns the index position of the last occurrence of the string |
| 3905 | reference \a str in this string, searching backward from index |
| 3906 | position \a from. If \a from is -1 (default), the search starts at |
| 3907 | the last character; if \a from is -2, at the next to last character |
| 3908 | and so on. Returns -1 if \a str is not found. |
| 3909 | |
| 3910 | If \a cs is Qt::CaseSensitive (default), the search is case |
| 3911 | sensitive; otherwise the search is case insensitive. |
| 3912 | |
| 3913 | \sa indexOf(), contains(), count() |
| 3914 | */ |
| 3915 | int QString::lastIndexOf(const QStringRef &str, int from, Qt::CaseSensitivity cs) const |
| 3916 | { |
| 3917 | // ### Qt6: qsizetype |
| 3918 | return int(QtPrivate::lastIndexOf(haystack: *this, from, needle: str, cs)); |
| 3919 | } |
| 3920 | #endif // QT_STRINGVIEW_LEVEL < 2 |
| 3921 | |
| 3922 | /*! |
| 3923 | \fn int QString::lastIndexOf(QStringView str, int from, Qt::CaseSensitivity cs) const |
| 3924 | \since 5.14 |
| 3925 | \overload lastIndexOf() |
| 3926 | |
| 3927 | Returns the index position of the last occurrence of the string view \a |
| 3928 | str in this string, searching backward from index position \a |
| 3929 | from. If \a from is -1 (default), the search starts at the last |
| 3930 | character; if \a from is -2, at the next to last character and so |
| 3931 | on. Returns -1 if \a str is not found. |
| 3932 | |
| 3933 | If \a cs is Qt::CaseSensitive (default), the search is case |
| 3934 | sensitive; otherwise the search is case insensitive. |
| 3935 | |
| 3936 | \sa indexOf(), contains(), count() |
| 3937 | */ |
| 3938 | |
| 3939 | |
| 3940 | #if !(defined(QT_NO_REGEXP) && !QT_CONFIG(regularexpression)) |
| 3941 | struct QStringCapture |
| 3942 | { |
| 3943 | int pos; |
| 3944 | int len; |
| 3945 | int no; |
| 3946 | }; |
| 3947 | Q_DECLARE_TYPEINFO(QStringCapture, Q_PRIMITIVE_TYPE); |
| 3948 | #endif |
| 3949 | |
| 3950 | #ifndef QT_NO_REGEXP |
| 3951 | |
| 3952 | /*! |
| 3953 | \overload replace() |
| 3954 | |
| 3955 | Replaces every occurrence of the regular expression \a rx in the |
| 3956 | string with \a after. Returns a reference to the string. For |
| 3957 | example: |
| 3958 | |
| 3959 | \snippet qstring/main.cpp 42 |
| 3960 | |
| 3961 | For regular expressions containing \l{capturing parentheses}, |
| 3962 | occurrences of \b{\\1}, \b{\\2}, ..., in \a after are replaced |
| 3963 | with \a{rx}.cap(1), cap(2), ... |
| 3964 | |
| 3965 | \snippet qstring/main.cpp 43 |
| 3966 | |
| 3967 | \sa indexOf(), lastIndexOf(), remove(), QRegExp::cap() |
| 3968 | */ |
| 3969 | QString& QString::replace(const QRegExp &rx, const QString &after) |
| 3970 | { |
| 3971 | QRegExp rx2(rx); |
| 3972 | |
| 3973 | if (isEmpty() && rx2.indexIn(str: *this) == -1) |
| 3974 | return *this; |
| 3975 | |
| 3976 | reallocData(alloc: uint(d->size) + 1u); |
| 3977 | |
| 3978 | int index = 0; |
| 3979 | int numCaptures = rx2.captureCount(); |
| 3980 | int al = after.length(); |
| 3981 | QRegExp::CaretMode caretMode = QRegExp::CaretAtZero; |
| 3982 | |
| 3983 | if (numCaptures > 0) { |
| 3984 | const QChar *uc = after.unicode(); |
| 3985 | int numBackRefs = 0; |
| 3986 | |
| 3987 | for (int i = 0; i < al - 1; i++) { |
| 3988 | if (uc[i] == QLatin1Char('\\')) { |
| 3989 | int no = uc[i + 1].digitValue(); |
| 3990 | if (no > 0 && no <= numCaptures) |
| 3991 | numBackRefs++; |
| 3992 | } |
| 3993 | } |
| 3994 | |
| 3995 | /* |
| 3996 | This is the harder case where we have back-references. |
| 3997 | */ |
| 3998 | if (numBackRefs > 0) { |
| 3999 | QVarLengthArray<QStringCapture, 16> captures(numBackRefs); |
| 4000 | int j = 0; |
| 4001 | |
| 4002 | for (int i = 0; i < al - 1; i++) { |
| 4003 | if (uc[i] == QLatin1Char('\\')) { |
| 4004 | int no = uc[i + 1].digitValue(); |
| 4005 | if (no > 0 && no <= numCaptures) { |
| 4006 | QStringCapture capture; |
| 4007 | capture.pos = i; |
| 4008 | capture.len = 2; |
| 4009 | |
| 4010 | if (i < al - 2) { |
| 4011 | int secondDigit = uc[i + 2].digitValue(); |
| 4012 | if (secondDigit != -1 && ((no * 10) + secondDigit) <= numCaptures) { |
| 4013 | no = (no * 10) + secondDigit; |
| 4014 | ++capture.len; |
| 4015 | } |
| 4016 | } |
| 4017 | |
| 4018 | capture.no = no; |
| 4019 | captures[j++] = capture; |
| 4020 | } |
| 4021 | } |
| 4022 | } |
| 4023 | |
| 4024 | while (index <= length()) { |
| 4025 | index = rx2.indexIn(str: *this, offset: index, caretMode); |
| 4026 | if (index == -1) |
| 4027 | break; |
| 4028 | |
| 4029 | QString after2(after); |
| 4030 | for (j = numBackRefs - 1; j >= 0; j--) { |
| 4031 | const QStringCapture &capture = captures[j]; |
| 4032 | after2.replace(pos: capture.pos, len: capture.len, after: rx2.cap(nth: capture.no)); |
| 4033 | } |
| 4034 | |
| 4035 | replace(pos: index, len: rx2.matchedLength(), after: after2); |
| 4036 | index += after2.length(); |
| 4037 | |
| 4038 | // avoid infinite loop on 0-length matches (e.g., QRegExp("[a-z]*")) |
| 4039 | if (rx2.matchedLength() == 0) |
| 4040 | ++index; |
| 4041 | |
| 4042 | caretMode = QRegExp::CaretWontMatch; |
| 4043 | } |
| 4044 | return *this; |
| 4045 | } |
| 4046 | } |
| 4047 | |
| 4048 | /* |
| 4049 | This is the simple and optimized case where we don't have |
| 4050 | back-references. |
| 4051 | */ |
| 4052 | while (index != -1) { |
| 4053 | struct { |
| 4054 | int pos; |
| 4055 | int length; |
| 4056 | } replacements[2048]; |
| 4057 | |
| 4058 | int pos = 0; |
| 4059 | int adjust = 0; |
| 4060 | while (pos < 2047) { |
| 4061 | index = rx2.indexIn(str: *this, offset: index, caretMode); |
| 4062 | if (index == -1) |
| 4063 | break; |
| 4064 | int ml = rx2.matchedLength(); |
| 4065 | replacements[pos].pos = index; |
| 4066 | replacements[pos++].length = ml; |
| 4067 | index += ml; |
| 4068 | adjust += al - ml; |
| 4069 | // avoid infinite loop |
| 4070 | if (!ml) |
| 4071 | index++; |
| 4072 | } |
| 4073 | if (!pos) |
| 4074 | break; |
| 4075 | replacements[pos].pos = d->size; |
| 4076 | int newlen = d->size + adjust; |
| 4077 | |
| 4078 | // to continue searching at the right position after we did |
| 4079 | // the first round of replacements |
| 4080 | if (index != -1) |
| 4081 | index += adjust; |
| 4082 | QString newstring; |
| 4083 | newstring.reserve(asize: newlen + 1); |
| 4084 | QChar *newuc = newstring.data(); |
| 4085 | QChar *uc = newuc; |
| 4086 | int copystart = 0; |
| 4087 | int i = 0; |
| 4088 | while (i < pos) { |
| 4089 | int copyend = replacements[i].pos; |
| 4090 | int size = copyend - copystart; |
| 4091 | memcpy(dest: static_cast<void*>(uc), src: static_cast<const void *>(d->data() + copystart), n: size * sizeof(QChar)); |
| 4092 | uc += size; |
| 4093 | memcpy(dest: static_cast<void *>(uc), src: static_cast<const void *>(after.d->data()), n: al * sizeof(QChar)); |
| 4094 | uc += al; |
| 4095 | copystart = copyend + replacements[i].length; |
| 4096 | i++; |
| 4097 | } |
| 4098 | memcpy(dest: static_cast<void *>(uc), src: static_cast<const void *>(d->data() + copystart), n: (d->size - copystart) * sizeof(QChar)); |
| 4099 | newstring.resize(size: newlen); |
| 4100 | *this = newstring; |
| 4101 | caretMode = QRegExp::CaretWontMatch; |
| 4102 | } |
| 4103 | return *this; |
| 4104 | } |
| 4105 | #endif |
| 4106 | |
| 4107 | #if QT_CONFIG(regularexpression) |
| 4108 | /*! |
| 4109 | \overload replace() |
| 4110 | \since 5.0 |
| 4111 | |
| 4112 | Replaces every occurrence of the regular expression \a re in the |
| 4113 | string with \a after. Returns a reference to the string. For |
| 4114 | example: |
| 4115 | |
| 4116 | \snippet qstring/main.cpp 87 |
| 4117 | |
| 4118 | For regular expressions containing capturing groups, |
| 4119 | occurrences of \b{\\1}, \b{\\2}, ..., in \a after are replaced |
| 4120 | with the string captured by the corresponding capturing group. |
| 4121 | |
| 4122 | \snippet qstring/main.cpp 88 |
| 4123 | |
| 4124 | \sa indexOf(), lastIndexOf(), remove(), QRegularExpression, QRegularExpressionMatch |
| 4125 | */ |
| 4126 | QString &QString::replace(const QRegularExpression &re, const QString &after) |
| 4127 | { |
| 4128 | if (!re.isValid()) { |
| 4129 | qWarning(msg: "QString::replace: invalid QRegularExpression object" ); |
| 4130 | return *this; |
| 4131 | } |
| 4132 | |
| 4133 | const QString copy(*this); |
| 4134 | QRegularExpressionMatchIterator iterator = re.globalMatch(subject: copy); |
| 4135 | if (!iterator.hasNext()) // no matches at all |
| 4136 | return *this; |
| 4137 | |
| 4138 | reallocData(alloc: uint(d->size) + 1u); |
| 4139 | |
| 4140 | int numCaptures = re.captureCount(); |
| 4141 | |
| 4142 | // 1. build the backreferences vector, holding where the backreferences |
| 4143 | // are in the replacement string |
| 4144 | QVector<QStringCapture> backReferences; |
| 4145 | const int al = after.length(); |
| 4146 | const QChar *ac = after.unicode(); |
| 4147 | |
| 4148 | for (int i = 0; i < al - 1; i++) { |
| 4149 | if (ac[i] == QLatin1Char('\\')) { |
| 4150 | int no = ac[i + 1].digitValue(); |
| 4151 | if (no > 0 && no <= numCaptures) { |
| 4152 | QStringCapture backReference; |
| 4153 | backReference.pos = i; |
| 4154 | backReference.len = 2; |
| 4155 | |
| 4156 | if (i < al - 2) { |
| 4157 | int secondDigit = ac[i + 2].digitValue(); |
| 4158 | if (secondDigit != -1 && ((no * 10) + secondDigit) <= numCaptures) { |
| 4159 | no = (no * 10) + secondDigit; |
| 4160 | ++backReference.len; |
| 4161 | } |
| 4162 | } |
| 4163 | |
| 4164 | backReference.no = no; |
| 4165 | backReferences.append(t: backReference); |
| 4166 | } |
| 4167 | } |
| 4168 | } |
| 4169 | |
| 4170 | // 2. iterate on the matches. For every match, copy in chunks |
| 4171 | // - the part before the match |
| 4172 | // - the after string, with the proper replacements for the backreferences |
| 4173 | |
| 4174 | int newLength = 0; // length of the new string, with all the replacements |
| 4175 | int lastEnd = 0; |
| 4176 | QVector<QStringRef> chunks; |
| 4177 | while (iterator.hasNext()) { |
| 4178 | QRegularExpressionMatch match = iterator.next(); |
| 4179 | int len; |
| 4180 | // add the part before the match |
| 4181 | len = match.capturedStart() - lastEnd; |
| 4182 | if (len > 0) { |
| 4183 | chunks << copy.midRef(position: lastEnd, n: len); |
| 4184 | newLength += len; |
| 4185 | } |
| 4186 | |
| 4187 | lastEnd = 0; |
| 4188 | // add the after string, with replacements for the backreferences |
| 4189 | for (const QStringCapture &backReference : qAsConst(t&: backReferences)) { |
| 4190 | // part of "after" before the backreference |
| 4191 | len = backReference.pos - lastEnd; |
| 4192 | if (len > 0) { |
| 4193 | chunks << after.midRef(position: lastEnd, n: len); |
| 4194 | newLength += len; |
| 4195 | } |
| 4196 | |
| 4197 | // backreference itself |
| 4198 | len = match.capturedLength(nth: backReference.no); |
| 4199 | if (len > 0) { |
| 4200 | chunks << copy.midRef(position: match.capturedStart(nth: backReference.no), n: len); |
| 4201 | newLength += len; |
| 4202 | } |
| 4203 | |
| 4204 | lastEnd = backReference.pos + backReference.len; |
| 4205 | } |
| 4206 | |
| 4207 | // add the last part of the after string |
| 4208 | len = after.length() - lastEnd; |
| 4209 | if (len > 0) { |
| 4210 | chunks << after.midRef(position: lastEnd, n: len); |
| 4211 | newLength += len; |
| 4212 | } |
| 4213 | |
| 4214 | lastEnd = match.capturedEnd(); |
| 4215 | } |
| 4216 | |
| 4217 | // 3. trailing string after the last match |
| 4218 | if (copy.length() > lastEnd) { |
| 4219 | chunks << copy.midRef(position: lastEnd); |
| 4220 | newLength += copy.length() - lastEnd; |
| 4221 | } |
| 4222 | |
| 4223 | // 4. assemble the chunks together |
| 4224 | resize(size: newLength); |
| 4225 | int i = 0; |
| 4226 | QChar *uc = data(); |
| 4227 | for (const QStringRef &chunk : qAsConst(t&: chunks)) { |
| 4228 | int len = chunk.length(); |
| 4229 | memcpy(dest: uc + i, src: chunk.unicode(), n: len * sizeof(QChar)); |
| 4230 | i += len; |
| 4231 | } |
| 4232 | |
| 4233 | return *this; |
| 4234 | } |
| 4235 | #endif // QT_CONFIG(regularexpression) |
| 4236 | |
| 4237 | /*! |
| 4238 | Returns the number of (potentially overlapping) occurrences of |
| 4239 | the string \a str in this string. |
| 4240 | |
| 4241 | If \a cs is Qt::CaseSensitive (default), the search is |
| 4242 | case sensitive; otherwise the search is case insensitive. |
| 4243 | |
| 4244 | \sa contains(), indexOf() |
| 4245 | */ |
| 4246 | |
| 4247 | int QString::count(const QString &str, Qt::CaseSensitivity cs) const |
| 4248 | { |
| 4249 | // ### Qt6: qsizetype |
| 4250 | return int(qt_string_count(haystack: QStringView(unicode(), size()), needle: QStringView(str.unicode(), str.size()), cs)); |
| 4251 | } |
| 4252 | |
| 4253 | /*! |
| 4254 | \overload count() |
| 4255 | |
| 4256 | Returns the number of occurrences of character \a ch in the string. |
| 4257 | |
| 4258 | If \a cs is Qt::CaseSensitive (default), the search is |
| 4259 | case sensitive; otherwise the search is case insensitive. |
| 4260 | |
| 4261 | \sa contains(), indexOf() |
| 4262 | */ |
| 4263 | |
| 4264 | int QString::count(QChar ch, Qt::CaseSensitivity cs) const |
| 4265 | { |
| 4266 | // ### Qt6: qsizetype |
| 4267 | return int(qt_string_count(haystack: QStringView(unicode(), size()), needle: ch, cs)); |
| 4268 | } |
| 4269 | |
| 4270 | /*! |
| 4271 | \since 4.8 |
| 4272 | \overload count() |
| 4273 | Returns the number of (potentially overlapping) occurrences of the |
| 4274 | string reference \a str in this string. |
| 4275 | |
| 4276 | If \a cs is Qt::CaseSensitive (default), the search is |
| 4277 | case sensitive; otherwise the search is case insensitive. |
| 4278 | |
| 4279 | \sa contains(), indexOf() |
| 4280 | */ |
| 4281 | int QString::count(const QStringRef &str, Qt::CaseSensitivity cs) const |
| 4282 | { |
| 4283 | // ### Qt6: qsizetype |
| 4284 | return int(qt_string_count(haystack: QStringView(unicode(), size()), needle: QStringView(str.unicode(), str.size()), cs)); |
| 4285 | } |
| 4286 | |
| 4287 | #if QT_STRINGVIEW_LEVEL < 2 |
| 4288 | /*! \fn bool QString::contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
| 4289 | |
| 4290 | Returns \c true if this string contains an occurrence of the string |
| 4291 | \a str; otherwise returns \c false. |
| 4292 | |
| 4293 | If \a cs is Qt::CaseSensitive (default), the search is |
| 4294 | case sensitive; otherwise the search is case insensitive. |
| 4295 | |
| 4296 | Example: |
| 4297 | \snippet qstring/main.cpp 17 |
| 4298 | |
| 4299 | \sa indexOf(), count() |
| 4300 | */ |
| 4301 | #endif // QT_STRINGVIEW_LEVEL < 2 |
| 4302 | |
| 4303 | /*! \fn bool QString::contains(QLatin1String str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
| 4304 | \since 5.3 |
| 4305 | |
| 4306 | \overload contains() |
| 4307 | |
| 4308 | Returns \c true if this string contains an occurrence of the latin-1 string |
| 4309 | \a str; otherwise returns \c false. |
| 4310 | */ |
| 4311 | |
| 4312 | /*! \fn bool QString::contains(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
| 4313 | |
| 4314 | \overload contains() |
| 4315 | |
| 4316 | Returns \c true if this string contains an occurrence of the |
| 4317 | character \a ch; otherwise returns \c false. |
| 4318 | */ |
| 4319 | |
| 4320 | #if QT_STRINGVIEW_LEVEL < 2 |
| 4321 | /*! \fn bool QString::contains(const QStringRef &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
| 4322 | \since 4.8 |
| 4323 | |
| 4324 | Returns \c true if this string contains an occurrence of the string |
| 4325 | reference \a str; otherwise returns \c false. |
| 4326 | |
| 4327 | If \a cs is Qt::CaseSensitive (default), the search is |
| 4328 | case sensitive; otherwise the search is case insensitive. |
| 4329 | |
| 4330 | \sa indexOf(), count() |
| 4331 | */ |
| 4332 | #endif // QT_STRINGVIEW_LEVEL < 2 |
| 4333 | |
| 4334 | /*! \fn bool QString::contains(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
| 4335 | \since 5.14 |
| 4336 | \overload contains() |
| 4337 | |
| 4338 | Returns \c true if this string contains an occurrence of the string view |
| 4339 | \a str; otherwise returns \c false. |
| 4340 | |
| 4341 | If \a cs is Qt::CaseSensitive (default), the search is |
| 4342 | case sensitive; otherwise the search is case insensitive. |
| 4343 | |
| 4344 | \sa indexOf(), count() |
| 4345 | */ |
| 4346 | |
| 4347 | /*! \fn bool QString::contains(const QRegExp &rx) const |
| 4348 | |
| 4349 | \overload contains() |
| 4350 | |
| 4351 | Returns \c true if the regular expression \a rx matches somewhere in |
| 4352 | this string; otherwise returns \c false. |
| 4353 | */ |
| 4354 | |
| 4355 | /*! \fn bool QString::contains(QRegExp &rx) const |
| 4356 | \overload contains() |
| 4357 | \since 4.5 |
| 4358 | |
| 4359 | Returns \c true if the regular expression \a rx matches somewhere in |
| 4360 | this string; otherwise returns \c false. |
| 4361 | |
| 4362 | If there is a match, the \a rx regular expression will contain the |
| 4363 | matched captures (see QRegExp::matchedLength, QRegExp::cap). |
| 4364 | */ |
| 4365 | |
| 4366 | #ifndef QT_NO_REGEXP |
| 4367 | /*! |
| 4368 | \overload indexOf() |
| 4369 | |
| 4370 | Returns the index position of the first match of the regular |
| 4371 | expression \a rx in the string, searching forward from index |
| 4372 | position \a from. Returns -1 if \a rx didn't match anywhere. |
| 4373 | |
| 4374 | Example: |
| 4375 | |
| 4376 | \snippet qstring/main.cpp 25 |
| 4377 | */ |
| 4378 | int QString::indexOf(const QRegExp& rx, int from) const |
| 4379 | { |
| 4380 | QRegExp rx2(rx); |
| 4381 | return rx2.indexIn(str: *this, offset: from); |
| 4382 | } |
| 4383 | |
| 4384 | /*! |
| 4385 | \overload indexOf() |
| 4386 | \since 4.5 |
| 4387 | |
| 4388 | Returns the index position of the first match of the regular |
| 4389 | expression \a rx in the string, searching forward from index |
| 4390 | position \a from. Returns -1 if \a rx didn't match anywhere. |
| 4391 | |
| 4392 | If there is a match, the \a rx regular expression will contain the |
| 4393 | matched captures (see QRegExp::matchedLength, QRegExp::cap). |
| 4394 | |
| 4395 | Example: |
| 4396 | |
| 4397 | \snippet qstring/main.cpp 25 |
| 4398 | */ |
| 4399 | int QString::indexOf(QRegExp& rx, int from) const |
| 4400 | { |
| 4401 | return rx.indexIn(str: *this, offset: from); |
| 4402 | } |
| 4403 | |
| 4404 | /*! |
| 4405 | \overload lastIndexOf() |
| 4406 | |
| 4407 | Returns the index position of the last match of the regular |
| 4408 | expression \a rx in the string, searching backward from index |
| 4409 | position \a from. Returns -1 if \a rx didn't match anywhere. |
| 4410 | |
| 4411 | Example: |
| 4412 | |
| 4413 | \snippet qstring/main.cpp 30 |
| 4414 | */ |
| 4415 | int QString::lastIndexOf(const QRegExp& rx, int from) const |
| 4416 | { |
| 4417 | QRegExp rx2(rx); |
| 4418 | return rx2.lastIndexIn(str: *this, offset: from); |
| 4419 | } |
| 4420 | |
| 4421 | /*! |
| 4422 | \overload lastIndexOf() |
| 4423 | \since 4.5 |
| 4424 | |
| 4425 | Returns the index position of the last match of the regular |
| 4426 | expression \a rx in the string, searching backward from index |
| 4427 | position \a from. Returns -1 if \a rx didn't match anywhere. |
| 4428 | |
| 4429 | If there is a match, the \a rx regular expression will contain the |
| 4430 | matched captures (see QRegExp::matchedLength, QRegExp::cap). |
| 4431 | |
| 4432 | Example: |
| 4433 | |
| 4434 | \snippet qstring/main.cpp 30 |
| 4435 | */ |
| 4436 | int QString::lastIndexOf(QRegExp& rx, int from) const |
| 4437 | { |
| 4438 | return rx.lastIndexIn(str: *this, offset: from); |
| 4439 | } |
| 4440 | |
| 4441 | /*! |
| 4442 | \overload count() |
| 4443 | |
| 4444 | Returns the number of times the regular expression \a rx matches |
| 4445 | in the string. |
| 4446 | |
| 4447 | This function counts overlapping matches, so in the example |
| 4448 | below, there are four instances of "ana" or "ama": |
| 4449 | |
| 4450 | \snippet qstring/main.cpp 18 |
| 4451 | |
| 4452 | */ |
| 4453 | int QString::count(const QRegExp& rx) const |
| 4454 | { |
| 4455 | QRegExp rx2(rx); |
| 4456 | int count = 0; |
| 4457 | int index = -1; |
| 4458 | int len = length(); |
| 4459 | while (index < len - 1) { // count overlapping matches |
| 4460 | index = rx2.indexIn(str: *this, offset: index + 1); |
| 4461 | if (index == -1) |
| 4462 | break; |
| 4463 | count++; |
| 4464 | } |
| 4465 | return count; |
| 4466 | } |
| 4467 | #endif // QT_NO_REGEXP |
| 4468 | |
| 4469 | #if QT_CONFIG(regularexpression) |
| 4470 | /*! |
| 4471 | \overload indexOf() |
| 4472 | \since 5.0 |
| 4473 | |
| 4474 | Returns the index position of the first match of the regular |
| 4475 | expression \a re in the string, searching forward from index |
| 4476 | position \a from. Returns -1 if \a re didn't match anywhere. |
| 4477 | |
| 4478 | Example: |
| 4479 | |
| 4480 | \snippet qstring/main.cpp 93 |
| 4481 | */ |
| 4482 | int QString::indexOf(const QRegularExpression& re, int from) const |
| 4483 | { |
| 4484 | return indexOf(re, from, rmatch: nullptr); |
| 4485 | } |
| 4486 | |
| 4487 | /*! |
| 4488 | \overload |
| 4489 | \since 5.5 |
| 4490 | |
| 4491 | Returns the index position of the first match of the regular |
| 4492 | expression \a re in the string, searching forward from index |
| 4493 | position \a from. Returns -1 if \a re didn't match anywhere. |
| 4494 | |
| 4495 | If the match is successful and \a rmatch is not \nullptr, it also |
| 4496 | writes the results of the match into the QRegularExpressionMatch object |
| 4497 | pointed to by \a rmatch. |
| 4498 | |
| 4499 | Example: |
| 4500 | |
| 4501 | \snippet qstring/main.cpp 99 |
| 4502 | */ |
| 4503 | int QString::indexOf(const QRegularExpression &re, int from, QRegularExpressionMatch *rmatch) const |
| 4504 | { |
| 4505 | if (!re.isValid()) { |
| 4506 | qWarning(msg: "QString::indexOf: invalid QRegularExpression object" ); |
| 4507 | return -1; |
| 4508 | } |
| 4509 | |
| 4510 | QRegularExpressionMatch match = re.match(subject: *this, offset: from); |
| 4511 | if (match.hasMatch()) { |
| 4512 | const int ret = match.capturedStart(); |
| 4513 | if (rmatch) |
| 4514 | *rmatch = std::move(match); |
| 4515 | return ret; |
| 4516 | } |
| 4517 | |
| 4518 | return -1; |
| 4519 | } |
| 4520 | |
| 4521 | /*! |
| 4522 | \overload lastIndexOf() |
| 4523 | \since 5.0 |
| 4524 | |
| 4525 | Returns the index position of the last match of the regular |
| 4526 | expression \a re in the string, which starts before the index |
| 4527 | position \a from. Returns -1 if \a re didn't match anywhere. |
| 4528 | |
| 4529 | Example: |
| 4530 | |
| 4531 | \snippet qstring/main.cpp 94 |
| 4532 | */ |
| 4533 | int QString::lastIndexOf(const QRegularExpression &re, int from) const |
| 4534 | { |
| 4535 | return lastIndexOf(re, from, rmatch: nullptr); |
| 4536 | } |
| 4537 | |
| 4538 | /*! |
| 4539 | \overload |
| 4540 | \since 5.5 |
| 4541 | |
| 4542 | Returns the index position of the last match of the regular |
| 4543 | expression \a re in the string, which starts before the index |
| 4544 | position \a from. Returns -1 if \a re didn't match anywhere. |
| 4545 | |
| 4546 | If the match is successful and \a rmatch is not \nullptr, it also |
| 4547 | writes the results of the match into the QRegularExpressionMatch object |
| 4548 | pointed to by \a rmatch. |
| 4549 | |
| 4550 | Example: |
| 4551 | |
| 4552 | \snippet qstring/main.cpp 100 |
| 4553 | |
| 4554 | \note Due to how the regular expression matching algorithm works, |
| 4555 | this function will actually match repeatedly from the beginning of |
| 4556 | the string until the position \a from is reached. |
| 4557 | */ |
| 4558 | int QString::lastIndexOf(const QRegularExpression &re, int from, QRegularExpressionMatch *rmatch) const |
| 4559 | { |
| 4560 | if (!re.isValid()) { |
| 4561 | qWarning(msg: "QString::lastIndexOf: invalid QRegularExpression object" ); |
| 4562 | return -1; |
| 4563 | } |
| 4564 | |
| 4565 | int endpos = (from < 0) ? (size() + from + 1) : (from + 1); |
| 4566 | QRegularExpressionMatchIterator iterator = re.globalMatch(subject: *this); |
| 4567 | int lastIndex = -1; |
| 4568 | while (iterator.hasNext()) { |
| 4569 | QRegularExpressionMatch match = iterator.next(); |
| 4570 | int start = match.capturedStart(); |
| 4571 | if (start < endpos) { |
| 4572 | lastIndex = start; |
| 4573 | if (rmatch) |
| 4574 | *rmatch = std::move(match); |
| 4575 | } else { |
| 4576 | break; |
| 4577 | } |
| 4578 | } |
| 4579 | |
| 4580 | return lastIndex; |
| 4581 | } |
| 4582 | |
| 4583 | /*! \overload contains() |
| 4584 | \since 5.0 |
| 4585 | |
| 4586 | Returns \c true if the regular expression \a re matches somewhere in |
| 4587 | this string; otherwise returns \c false. |
| 4588 | */ |
| 4589 | bool QString::contains(const QRegularExpression &re) const |
| 4590 | { |
| 4591 | return contains(re, rmatch: nullptr); |
| 4592 | } |
| 4593 | |
| 4594 | /*! |
| 4595 | \overload contains() |
| 4596 | \since 5.1 |
| 4597 | |
| 4598 | Returns \c true if the regular expression \a re matches somewhere in this |
| 4599 | string; otherwise returns \c false. |
| 4600 | |
| 4601 | If the match is successful and \a rmatch is not \nullptr, it also |
| 4602 | writes the results of the match into the QRegularExpressionMatch object |
| 4603 | pointed to by \a rmatch. |
| 4604 | |
| 4605 | \sa QRegularExpression::match() |
| 4606 | */ |
| 4607 | |
| 4608 | bool QString::contains(const QRegularExpression &re, QRegularExpressionMatch *rmatch) const |
| 4609 | { |
| 4610 | if (!re.isValid()) { |
| 4611 | qWarning(msg: "QString::contains: invalid QRegularExpression object" ); |
| 4612 | return false; |
| 4613 | } |
| 4614 | QRegularExpressionMatch m = re.match(subject: *this); |
| 4615 | bool hasMatch = m.hasMatch(); |
| 4616 | if (hasMatch && rmatch) |
| 4617 | *rmatch = std::move(m); |
| 4618 | return hasMatch; |
| 4619 | } |
| 4620 | |
| 4621 | /*! |
| 4622 | \overload count() |
| 4623 | \since 5.0 |
| 4624 | |
| 4625 | Returns the number of times the regular expression \a re matches |
| 4626 | in the string. |
| 4627 | |
| 4628 | For historical reasons, this function counts overlapping matches, |
| 4629 | so in the example below, there are four instances of "ana" or |
| 4630 | "ama": |
| 4631 | |
| 4632 | \snippet qstring/main.cpp 95 |
| 4633 | |
| 4634 | This behavior is different from simply iterating over the matches |
| 4635 | in the string using QRegularExpressionMatchIterator. |
| 4636 | |
| 4637 | \sa QRegularExpression::globalMatch() |
| 4638 | */ |
| 4639 | int QString::count(const QRegularExpression &re) const |
| 4640 | { |
| 4641 | if (!re.isValid()) { |
| 4642 | qWarning(msg: "QString::count: invalid QRegularExpression object" ); |
| 4643 | return 0; |
| 4644 | } |
| 4645 | int count = 0; |
| 4646 | int index = -1; |
| 4647 | int len = length(); |
| 4648 | while (index <= len - 1) { |
| 4649 | QRegularExpressionMatch match = re.match(subject: *this, offset: index + 1); |
| 4650 | if (!match.hasMatch()) |
| 4651 | break; |
| 4652 | index = match.capturedStart(); |
| 4653 | count++; |
| 4654 | } |
| 4655 | return count; |
| 4656 | } |
| 4657 | #endif // QT_CONFIG(regularexpression) |
| 4658 | |
| 4659 | /*! \fn int QString::count() const |
| 4660 | |
| 4661 | \overload count() |
| 4662 | |
| 4663 | Same as size(). |
| 4664 | */ |
| 4665 | |
| 4666 | |
| 4667 | /*! |
| 4668 | \enum QString::SectionFlag |
| 4669 | |
| 4670 | This enum specifies flags that can be used to affect various |
| 4671 | aspects of the section() function's behavior with respect to |
| 4672 | separators and empty fields. |
| 4673 | |
| 4674 | \value SectionDefault Empty fields are counted, leading and |
| 4675 | trailing separators are not included, and the separator is |
| 4676 | compared case sensitively. |
| 4677 | |
| 4678 | \value SectionSkipEmpty Treat empty fields as if they don't exist, |
| 4679 | i.e. they are not considered as far as \e start and \e end are |
| 4680 | concerned. |
| 4681 | |
| 4682 | \value SectionIncludeLeadingSep Include the leading separator (if |
| 4683 | any) in the result string. |
| 4684 | |
| 4685 | \value SectionIncludeTrailingSep Include the trailing separator |
| 4686 | (if any) in the result string. |
| 4687 | |
| 4688 | \value SectionCaseInsensitiveSeps Compare the separator |
| 4689 | case-insensitively. |
| 4690 | |
| 4691 | \sa section() |
| 4692 | */ |
| 4693 | |
| 4694 | /*! |
| 4695 | \fn QString QString::section(QChar sep, int start, int end = -1, SectionFlags flags) const |
| 4696 | |
| 4697 | This function returns a section of the string. |
| 4698 | |
| 4699 | This string is treated as a sequence of fields separated by the |
| 4700 | character, \a sep. The returned string consists of the fields from |
| 4701 | position \a start to position \a end inclusive. If \a end is not |
| 4702 | specified, all fields from position \a start to the end of the |
| 4703 | string are included. Fields are numbered 0, 1, 2, etc., counting |
| 4704 | from the left, and -1, -2, etc., counting from right to left. |
| 4705 | |
| 4706 | The \a flags argument can be used to affect some aspects of the |
| 4707 | function's behavior, e.g. whether to be case sensitive, whether |
| 4708 | to skip empty fields and how to deal with leading and trailing |
| 4709 | separators; see \l{SectionFlags}. |
| 4710 | |
| 4711 | \snippet qstring/main.cpp 52 |
| 4712 | |
| 4713 | If \a start or \a end is negative, we count fields from the right |
| 4714 | of the string, the right-most field being -1, the one from |
| 4715 | right-most field being -2, and so on. |
| 4716 | |
| 4717 | \snippet qstring/main.cpp 53 |
| 4718 | |
| 4719 | \sa split() |
| 4720 | */ |
| 4721 | |
| 4722 | /*! |
| 4723 | \overload section() |
| 4724 | |
| 4725 | \snippet qstring/main.cpp 51 |
| 4726 | \snippet qstring/main.cpp 54 |
| 4727 | |
| 4728 | \sa split() |
| 4729 | */ |
| 4730 | |
| 4731 | QString QString::section(const QString &sep, int start, int end, SectionFlags flags) const |
| 4732 | { |
| 4733 | const QVector<QStringRef> sections = splitRef(sep, behavior: Qt::KeepEmptyParts, |
| 4734 | cs: (flags & SectionCaseInsensitiveSeps) ? Qt::CaseInsensitive : Qt::CaseSensitive); |
| 4735 | const int sectionsSize = sections.size(); |
| 4736 | if (!(flags & SectionSkipEmpty)) { |
| 4737 | if (start < 0) |
| 4738 | start += sectionsSize; |
| 4739 | if (end < 0) |
| 4740 | end += sectionsSize; |
| 4741 | } else { |
| 4742 | int skip = 0; |
| 4743 | for (int k = 0; k < sectionsSize; ++k) { |
| 4744 | if (sections.at(i: k).isEmpty()) |
| 4745 | skip++; |
| 4746 | } |
| 4747 | if (start < 0) |
| 4748 | start += sectionsSize - skip; |
| 4749 | if (end < 0) |
| 4750 | end += sectionsSize - skip; |
| 4751 | } |
| 4752 | if (start >= sectionsSize || end < 0 || start > end) |
| 4753 | return QString(); |
| 4754 | |
| 4755 | QString ret; |
| 4756 | int first_i = start, last_i = end; |
| 4757 | for (int x = 0, i = 0; x <= end && i < sectionsSize; ++i) { |
| 4758 | const QStringRef §ion = sections.at(i); |
| 4759 | const bool empty = section.isEmpty(); |
| 4760 | if (x >= start) { |
| 4761 | if(x == start) |
| 4762 | first_i = i; |
| 4763 | if(x == end) |
| 4764 | last_i = i; |
| 4765 | if (x > start && i > 0) |
| 4766 | ret += sep; |
| 4767 | ret += section; |
| 4768 | } |
| 4769 | if (!empty || !(flags & SectionSkipEmpty)) |
| 4770 | x++; |
| 4771 | } |
| 4772 | if ((flags & SectionIncludeLeadingSep) && first_i > 0) |
| 4773 | ret.prepend(s: sep); |
| 4774 | if ((flags & SectionIncludeTrailingSep) && last_i < sectionsSize - 1) |
| 4775 | ret += sep; |
| 4776 | return ret; |
| 4777 | } |
| 4778 | |
| 4779 | #if !(defined(QT_NO_REGEXP) && !QT_CONFIG(regularexpression)) |
| 4780 | class qt_section_chunk { |
| 4781 | public: |
| 4782 | qt_section_chunk() {} |
| 4783 | qt_section_chunk(int l, QStringRef s) : length(l), string(std::move(s)) {} |
| 4784 | int length; |
| 4785 | QStringRef string; |
| 4786 | }; |
| 4787 | Q_DECLARE_TYPEINFO(qt_section_chunk, Q_MOVABLE_TYPE); |
| 4788 | |
| 4789 | static QString (const QVector<qt_section_chunk> §ions, |
| 4790 | int start, |
| 4791 | int end, |
| 4792 | QString::SectionFlags flags) |
| 4793 | { |
| 4794 | const int sectionsSize = sections.size(); |
| 4795 | |
| 4796 | if (!(flags & QString::SectionSkipEmpty)) { |
| 4797 | if (start < 0) |
| 4798 | start += sectionsSize; |
| 4799 | if (end < 0) |
| 4800 | end += sectionsSize; |
| 4801 | } else { |
| 4802 | int skip = 0; |
| 4803 | for (int k = 0; k < sectionsSize; ++k) { |
| 4804 | const qt_section_chunk §ion = sections.at(i: k); |
| 4805 | if (section.length == section.string.length()) |
| 4806 | skip++; |
| 4807 | } |
| 4808 | if (start < 0) |
| 4809 | start += sectionsSize - skip; |
| 4810 | if (end < 0) |
| 4811 | end += sectionsSize - skip; |
| 4812 | } |
| 4813 | if (start >= sectionsSize || end < 0 || start > end) |
| 4814 | return QString(); |
| 4815 | |
| 4816 | QString ret; |
| 4817 | int x = 0; |
| 4818 | int first_i = start, last_i = end; |
| 4819 | for (int i = 0; x <= end && i < sectionsSize; ++i) { |
| 4820 | const qt_section_chunk §ion = sections.at(i); |
| 4821 | const bool empty = (section.length == section.string.length()); |
| 4822 | if (x >= start) { |
| 4823 | if (x == start) |
| 4824 | first_i = i; |
| 4825 | if (x == end) |
| 4826 | last_i = i; |
| 4827 | if (x != start) |
| 4828 | ret += section.string; |
| 4829 | else |
| 4830 | ret += section.string.mid(pos: section.length); |
| 4831 | } |
| 4832 | if (!empty || !(flags & QString::SectionSkipEmpty)) |
| 4833 | x++; |
| 4834 | } |
| 4835 | |
| 4836 | if ((flags & QString::SectionIncludeLeadingSep) && first_i >= 0) { |
| 4837 | const qt_section_chunk §ion = sections.at(i: first_i); |
| 4838 | ret.prepend(s: section.string.left(n: section.length)); |
| 4839 | } |
| 4840 | |
| 4841 | if ((flags & QString::SectionIncludeTrailingSep) |
| 4842 | && last_i < sectionsSize - 1) { |
| 4843 | const qt_section_chunk §ion = sections.at(i: last_i+1); |
| 4844 | ret += section.string.left(n: section.length); |
| 4845 | } |
| 4846 | |
| 4847 | return ret; |
| 4848 | } |
| 4849 | #endif |
| 4850 | |
| 4851 | #ifndef QT_NO_REGEXP |
| 4852 | /*! |
| 4853 | \overload section() |
| 4854 | |
| 4855 | This string is treated as a sequence of fields separated by the |
| 4856 | regular expression, \a reg. |
| 4857 | |
| 4858 | \snippet qstring/main.cpp 55 |
| 4859 | |
| 4860 | \warning Using this QRegExp version is much more expensive than |
| 4861 | the overloaded string and character versions. |
| 4862 | |
| 4863 | \sa split(), simplified() |
| 4864 | */ |
| 4865 | QString QString::section(const QRegExp ®, int start, int end, SectionFlags flags) const |
| 4866 | { |
| 4867 | const QChar *uc = unicode(); |
| 4868 | if(!uc) |
| 4869 | return QString(); |
| 4870 | |
| 4871 | QRegExp sep(reg); |
| 4872 | sep.setCaseSensitivity((flags & SectionCaseInsensitiveSeps) ? Qt::CaseInsensitive |
| 4873 | : Qt::CaseSensitive); |
| 4874 | |
| 4875 | QVector<qt_section_chunk> sections; |
| 4876 | int n = length(), m = 0, last_m = 0, last_len = 0; |
| 4877 | while ((m = sep.indexIn(str: *this, offset: m)) != -1) { |
| 4878 | sections.append(t: qt_section_chunk(last_len, QStringRef(this, last_m, m - last_m))); |
| 4879 | last_m = m; |
| 4880 | last_len = sep.matchedLength(); |
| 4881 | m += qMax(a: sep.matchedLength(), b: 1); |
| 4882 | } |
| 4883 | sections.append(t: qt_section_chunk(last_len, QStringRef(this, last_m, n - last_m))); |
| 4884 | |
| 4885 | return extractSections(sections, start, end, flags); |
| 4886 | } |
| 4887 | #endif |
| 4888 | |
| 4889 | #if QT_CONFIG(regularexpression) |
| 4890 | /*! |
| 4891 | \overload section() |
| 4892 | \since 5.0 |
| 4893 | |
| 4894 | This string is treated as a sequence of fields separated by the |
| 4895 | regular expression, \a re. |
| 4896 | |
| 4897 | \snippet qstring/main.cpp 89 |
| 4898 | |
| 4899 | \warning Using this QRegularExpression version is much more expensive than |
| 4900 | the overloaded string and character versions. |
| 4901 | |
| 4902 | \sa split(), simplified() |
| 4903 | */ |
| 4904 | QString QString::section(const QRegularExpression &re, int start, int end, SectionFlags flags) const |
| 4905 | { |
| 4906 | if (!re.isValid()) { |
| 4907 | qWarning(msg: "QString::section: invalid QRegularExpression object" ); |
| 4908 | return QString(); |
| 4909 | } |
| 4910 | |
| 4911 | const QChar *uc = unicode(); |
| 4912 | if (!uc) |
| 4913 | return QString(); |
| 4914 | |
| 4915 | QRegularExpression sep(re); |
| 4916 | if (flags & SectionCaseInsensitiveSeps) |
| 4917 | sep.setPatternOptions(sep.patternOptions() | QRegularExpression::CaseInsensitiveOption); |
| 4918 | |
| 4919 | QVector<qt_section_chunk> sections; |
| 4920 | int n = length(), m = 0, last_m = 0, last_len = 0; |
| 4921 | QRegularExpressionMatchIterator iterator = sep.globalMatch(subject: *this); |
| 4922 | while (iterator.hasNext()) { |
| 4923 | QRegularExpressionMatch match = iterator.next(); |
| 4924 | m = match.capturedStart(); |
| 4925 | sections.append(t: qt_section_chunk(last_len, QStringRef(this, last_m, m - last_m))); |
| 4926 | last_m = m; |
| 4927 | last_len = match.capturedLength(); |
| 4928 | } |
| 4929 | sections.append(t: qt_section_chunk(last_len, QStringRef(this, last_m, n - last_m))); |
| 4930 | |
| 4931 | return extractSections(sections, start, end, flags); |
| 4932 | } |
| 4933 | #endif // QT_CONFIG(regularexpression) |
| 4934 | |
| 4935 | /*! |
| 4936 | Returns a substring that contains the \a n leftmost characters |
| 4937 | of the string. |
| 4938 | |
| 4939 | The entire string is returned if \a n is greater than or equal |
| 4940 | to size(), or less than zero. |
| 4941 | |
| 4942 | \snippet qstring/main.cpp 31 |
| 4943 | |
| 4944 | \sa right(), mid(), startsWith(), chopped(), chop(), truncate() |
| 4945 | */ |
| 4946 | QString QString::left(int n) const |
| 4947 | { |
| 4948 | if (uint(n) >= uint(d->size)) |
| 4949 | return *this; |
| 4950 | return QString((const QChar*) d->data(), n); |
| 4951 | } |
| 4952 | |
| 4953 | /*! |
| 4954 | Returns a substring that contains the \a n rightmost characters |
| 4955 | of the string. |
| 4956 | |
| 4957 | The entire string is returned if \a n is greater than or equal |
| 4958 | to size(), or less than zero. |
| 4959 | |
| 4960 | \snippet qstring/main.cpp 48 |
| 4961 | |
| 4962 | \sa left(), mid(), endsWith(), chopped(), chop(), truncate() |
| 4963 | */ |
| 4964 | QString QString::right(int n) const |
| 4965 | { |
| 4966 | if (uint(n) >= uint(d->size)) |
| 4967 | return *this; |
| 4968 | return QString((const QChar*) d->data() + d->size - n, n); |
| 4969 | } |
| 4970 | |
| 4971 | /*! |
| 4972 | Returns a string that contains \a n characters of this string, |
| 4973 | starting at the specified \a position index. |
| 4974 | |
| 4975 | Returns a null string if the \a position index exceeds the |
| 4976 | length of the string. If there are less than \a n characters |
| 4977 | available in the string starting at the given \a position, or if |
| 4978 | \a n is -1 (default), the function returns all characters that |
| 4979 | are available from the specified \a position. |
| 4980 | |
| 4981 | Example: |
| 4982 | |
| 4983 | \snippet qstring/main.cpp 34 |
| 4984 | |
| 4985 | \sa left(), right(), chopped(), chop(), truncate() |
| 4986 | */ |
| 4987 | |
| 4988 | QString QString::mid(int position, int n) const |
| 4989 | { |
| 4990 | using namespace QtPrivate; |
| 4991 | switch (QContainerImplHelper::mid(originalLength: d->size, position: &position, length: &n)) { |
| 4992 | case QContainerImplHelper::Null: |
| 4993 | return QString(); |
| 4994 | case QContainerImplHelper::Empty: |
| 4995 | { |
| 4996 | QStringDataPtr empty = { .ptr: Data::allocate(capacity: 0) }; |
| 4997 | return QString(empty); |
| 4998 | } |
| 4999 | case QContainerImplHelper::Full: |
| 5000 | return *this; |
| 5001 | case QContainerImplHelper::Subset: |
| 5002 | return QString((const QChar*)d->data() + position, n); |
| 5003 | } |
| 5004 | Q_UNREACHABLE(); |
| 5005 | return QString(); |
| 5006 | } |
| 5007 | |
| 5008 | /*! |
| 5009 | \fn QString QString::chopped(int len) const |
| 5010 | \since 5.10 |
| 5011 | |
| 5012 | Returns a substring that contains the size() - \a len leftmost characters |
| 5013 | of this string. |
| 5014 | |
| 5015 | \note The behavior is undefined if \a len is negative or greater than size(). |
| 5016 | |
| 5017 | \sa endsWith(), left(), right(), mid(), chop(), truncate() |
| 5018 | */ |
| 5019 | |
| 5020 | #if QT_STRINGVIEW_LEVEL < 2 |
| 5021 | /*! |
| 5022 | Returns \c true if the string starts with \a s; otherwise returns |
| 5023 | \c false. |
| 5024 | |
| 5025 | If \a cs is Qt::CaseSensitive (default), the search is |
| 5026 | case sensitive; otherwise the search is case insensitive. |
| 5027 | |
| 5028 | \snippet qstring/main.cpp 65 |
| 5029 | |
| 5030 | \sa endsWith() |
| 5031 | */ |
| 5032 | bool QString::startsWith(const QString& s, Qt::CaseSensitivity cs) const |
| 5033 | { |
| 5034 | return qt_starts_with(haystack: *this, needle: s, cs); |
| 5035 | } |
| 5036 | #endif |
| 5037 | |
| 5038 | /*! |
| 5039 | \overload startsWith() |
| 5040 | */ |
| 5041 | bool QString::startsWith(QLatin1String s, Qt::CaseSensitivity cs) const |
| 5042 | { |
| 5043 | return qt_starts_with(haystack: *this, needle: s, cs); |
| 5044 | } |
| 5045 | |
| 5046 | /*! |
| 5047 | \overload startsWith() |
| 5048 | |
| 5049 | Returns \c true if the string starts with \a c; otherwise returns |
| 5050 | \c false. |
| 5051 | */ |
| 5052 | bool QString::startsWith(QChar c, Qt::CaseSensitivity cs) const |
| 5053 | { |
| 5054 | return qt_starts_with(haystack: *this, needle: c, cs); |
| 5055 | } |
| 5056 | |
| 5057 | #if QT_STRINGVIEW_LEVEL < 2 |
| 5058 | /*! |
| 5059 | \since 4.8 |
| 5060 | \overload |
| 5061 | Returns \c true if the string starts with the string reference \a s; |
| 5062 | otherwise returns \c false. |
| 5063 | |
| 5064 | If \a cs is Qt::CaseSensitive (default), the search is case |
| 5065 | sensitive; otherwise the search is case insensitive. |
| 5066 | |
| 5067 | \sa endsWith() |
| 5068 | */ |
| 5069 | bool QString::startsWith(const QStringRef &s, Qt::CaseSensitivity cs) const |
| 5070 | { |
| 5071 | return qt_starts_with(haystack: *this, needle: s, cs); |
| 5072 | } |
| 5073 | #endif |
| 5074 | |
| 5075 | /*! |
| 5076 | \fn bool QString::startsWith(QStringView str, Qt::CaseSensitivity cs) const |
| 5077 | \since 5.10 |
| 5078 | \overload |
| 5079 | |
| 5080 | Returns \c true if the string starts with the string-view \a str; |
| 5081 | otherwise returns \c false. |
| 5082 | |
| 5083 | If \a cs is Qt::CaseSensitive (default), the search is case-sensitive; |
| 5084 | otherwise the search is case insensitive. |
| 5085 | |
| 5086 | \sa endsWith() |
| 5087 | */ |
| 5088 | |
| 5089 | #if QT_STRINGVIEW_LEVEL < 2 |
| 5090 | /*! |
| 5091 | Returns \c true if the string ends with \a s; otherwise returns |
| 5092 | \c false. |
| 5093 | |
| 5094 | If \a cs is Qt::CaseSensitive (default), the search is case |
| 5095 | sensitive; otherwise the search is case insensitive. |
| 5096 | |
| 5097 | \snippet qstring/main.cpp 20 |
| 5098 | |
| 5099 | \sa startsWith() |
| 5100 | */ |
| 5101 | bool QString::endsWith(const QString &s, Qt::CaseSensitivity cs) const |
| 5102 | { |
| 5103 | return qt_ends_with(haystack: *this, needle: s, cs); |
| 5104 | } |
| 5105 | |
| 5106 | /*! |
| 5107 | \since 4.8 |
| 5108 | \overload endsWith() |
| 5109 | Returns \c true if the string ends with the string reference \a s; |
| 5110 | otherwise returns \c false. |
| 5111 | |
| 5112 | If \a cs is Qt::CaseSensitive (default), the search is case |
| 5113 | sensitive; otherwise the search is case insensitive. |
| 5114 | |
| 5115 | \sa startsWith() |
| 5116 | */ |
| 5117 | bool QString::endsWith(const QStringRef &s, Qt::CaseSensitivity cs) const |
| 5118 | { |
| 5119 | return qt_ends_with(haystack: *this, needle: s, cs); |
| 5120 | } |
| 5121 | #endif // QT_STRINGVIEW_LEVEL < 2 |
| 5122 | |
| 5123 | /*! |
| 5124 | \fn bool QString::endsWith(QStringView str, Qt::CaseSensitivity cs) const |
| 5125 | \since 5.10 |
| 5126 | \overload endsWith() |
| 5127 | Returns \c true if the string ends with the string view \a str; |
| 5128 | otherwise returns \c false. |
| 5129 | |
| 5130 | If \a cs is Qt::CaseSensitive (default), the search is case |
| 5131 | sensitive; otherwise the search is case insensitive. |
| 5132 | |
| 5133 | \sa startsWith() |
| 5134 | */ |
| 5135 | |
| 5136 | /*! |
| 5137 | \overload endsWith() |
| 5138 | */ |
| 5139 | bool QString::endsWith(QLatin1String s, Qt::CaseSensitivity cs) const |
| 5140 | { |
| 5141 | return qt_ends_with(haystack: *this, needle: s, cs); |
| 5142 | } |
| 5143 | |
| 5144 | /*! |
| 5145 | Returns \c true if the string ends with \a c; otherwise returns |
| 5146 | \c false. |
| 5147 | |
| 5148 | \overload endsWith() |
| 5149 | */ |
| 5150 | bool QString::endsWith(QChar c, Qt::CaseSensitivity cs) const |
| 5151 | { |
| 5152 | return qt_ends_with(haystack: *this, needle: c, cs); |
| 5153 | } |
| 5154 | |
| 5155 | /*! |
| 5156 | Returns \c true if the string is uppercase, that is, it's identical |
| 5157 | to its toUpper() folding. |
| 5158 | |
| 5159 | Note that this does \e not mean that the string does not contain |
| 5160 | lowercase letters (some lowercase letters do not have a uppercase |
| 5161 | folding; they are left unchanged by toUpper()). |
| 5162 | For more information, refer to the Unicode standard, section 3.13. |
| 5163 | |
| 5164 | \since 5.12 |
| 5165 | |
| 5166 | \sa QChar::toUpper(), isLower() |
| 5167 | */ |
| 5168 | bool QString::isUpper() const |
| 5169 | { |
| 5170 | QStringIterator it(*this); |
| 5171 | |
| 5172 | while (it.hasNext()) { |
| 5173 | uint uc = it.nextUnchecked(); |
| 5174 | if (qGetProp(ucs4: uc)->cases[QUnicodeTables::UpperCase].diff) |
| 5175 | return false; |
| 5176 | } |
| 5177 | |
| 5178 | return true; |
| 5179 | } |
| 5180 | |
| 5181 | /*! |
| 5182 | Returns \c true if the string is lowercase, that is, it's identical |
| 5183 | to its toLower() folding. |
| 5184 | |
| 5185 | Note that this does \e not mean that the string does not contain |
| 5186 | uppercase letters (some uppercase letters do not have a lowercase |
| 5187 | folding; they are left unchanged by toLower()). |
| 5188 | For more information, refer to the Unicode standard, section 3.13. |
| 5189 | |
| 5190 | \since 5.12 |
| 5191 | |
| 5192 | \sa QChar::toLower(), isUpper() |
| 5193 | */ |
| 5194 | bool QString::isLower() const |
| 5195 | { |
| 5196 | QStringIterator it(*this); |
| 5197 | |
| 5198 | while (it.hasNext()) { |
| 5199 | uint uc = it.nextUnchecked(); |
| 5200 | if (qGetProp(ucs4: uc)->cases[QUnicodeTables::LowerCase].diff) |
| 5201 | return false; |
| 5202 | } |
| 5203 | |
| 5204 | return true; |
| 5205 | } |
| 5206 | |
| 5207 | static QByteArray qt_convert_to_latin1(QStringView string); |
| 5208 | |
| 5209 | QByteArray QString::toLatin1_helper(const QString &string) |
| 5210 | { |
| 5211 | return qt_convert_to_latin1(string); |
| 5212 | } |
| 5213 | |
| 5214 | /*! |
| 5215 | \since 5.10 |
| 5216 | \internal |
| 5217 | \relates QStringView |
| 5218 | |
| 5219 | Returns a Latin-1 representation of \a string as a QByteArray. |
| 5220 | |
| 5221 | The behavior is undefined if \a string contains non-Latin1 characters. |
| 5222 | |
| 5223 | \sa QString::toLatin1(), QStringView::toLatin1(), QtPrivate::convertToUtf8(), |
| 5224 | QtPrivate::convertToLocal8Bit(), QtPrivate::convertToUcs4() |
| 5225 | */ |
| 5226 | QByteArray QtPrivate::convertToLatin1(QStringView string) |
| 5227 | { |
| 5228 | return qt_convert_to_latin1(string); |
| 5229 | } |
| 5230 | |
| 5231 | static QByteArray qt_convert_to_latin1(QStringView string) |
| 5232 | { |
| 5233 | if (Q_UNLIKELY(string.isNull())) |
| 5234 | return QByteArray(); |
| 5235 | |
| 5236 | QByteArray ba(string.length(), Qt::Uninitialized); |
| 5237 | |
| 5238 | // since we own the only copy, we're going to const_cast the constData; |
| 5239 | // that avoids an unnecessary call to detach() and expansion code that will never get used |
| 5240 | qt_to_latin1(dst: reinterpret_cast<uchar *>(const_cast<char *>(ba.constData())), |
| 5241 | src: reinterpret_cast<const ushort *>(string.data()), length: string.length()); |
| 5242 | return ba; |
| 5243 | } |
| 5244 | |
| 5245 | QByteArray QString::toLatin1_helper_inplace(QString &s) |
| 5246 | { |
| 5247 | if (!s.isDetached()) |
| 5248 | return qt_convert_to_latin1(string: s); |
| 5249 | |
| 5250 | // We can return our own buffer to the caller. |
| 5251 | // Conversion to Latin-1 always shrinks the buffer by half. |
| 5252 | const ushort *data = reinterpret_cast<const ushort *>(s.constData()); |
| 5253 | uint length = s.size(); |
| 5254 | |
| 5255 | // Swap the d pointers. |
| 5256 | // Kids, avert your eyes. Don't try this at home. |
| 5257 | QArrayData *ba_d = s.d; |
| 5258 | |
| 5259 | // multiply the allocated capacity by sizeof(ushort) |
| 5260 | ba_d->alloc *= sizeof(ushort); |
| 5261 | |
| 5262 | // reset ourselves to QString() |
| 5263 | s.d = QString().d; |
| 5264 | |
| 5265 | // do the in-place conversion |
| 5266 | uchar *dst = reinterpret_cast<uchar *>(ba_d->data()); |
| 5267 | qt_to_latin1(dst, src: data, length); |
| 5268 | dst[length] = '\0'; |
| 5269 | |
| 5270 | QByteArrayDataPtr badptr = { .ptr: ba_d }; |
| 5271 | return QByteArray(badptr); |
| 5272 | } |
| 5273 | |
| 5274 | |
| 5275 | /*! |
| 5276 | \fn QByteArray QString::toLatin1() const |
| 5277 | |
| 5278 | Returns a Latin-1 representation of the string as a QByteArray. |
| 5279 | |
| 5280 | The returned byte array is undefined if the string contains non-Latin1 |
| 5281 | characters. Those characters may be suppressed or replaced with a |
| 5282 | question mark. |
| 5283 | |
| 5284 | \sa fromLatin1(), toUtf8(), toLocal8Bit(), QTextCodec |
| 5285 | */ |
| 5286 | |
| 5287 | /*! |
| 5288 | \fn QByteArray QString::toAscii() const |
| 5289 | \deprecated |
| 5290 | Returns an 8-bit representation of the string as a QByteArray. |
| 5291 | |
| 5292 | This function does the same as toLatin1(). |
| 5293 | |
| 5294 | Note that, despite the name, this function does not necessarily return an US-ASCII |
| 5295 | (ANSI X3.4-1986) string and its result may not be US-ASCII compatible. |
| 5296 | |
| 5297 | \sa fromAscii(), toLatin1(), toUtf8(), toLocal8Bit(), QTextCodec |
| 5298 | */ |
| 5299 | |
| 5300 | static QByteArray qt_convert_to_local_8bit(QStringView string); |
| 5301 | |
| 5302 | /*! |
| 5303 | \fn QByteArray QString::toLocal8Bit() const |
| 5304 | |
| 5305 | Returns the local 8-bit representation of the string as a |
| 5306 | QByteArray. The returned byte array is undefined if the string |
| 5307 | contains characters not supported by the local 8-bit encoding. |
| 5308 | |
| 5309 | QTextCodec::codecForLocale() is used to perform the conversion from |
| 5310 | Unicode. If the locale encoding could not be determined, this function |
| 5311 | does the same as toLatin1(). |
| 5312 | |
| 5313 | If this string contains any characters that cannot be encoded in the |
| 5314 | locale, the returned byte array is undefined. Those characters may be |
| 5315 | suppressed or replaced by another. |
| 5316 | |
| 5317 | \sa fromLocal8Bit(), toLatin1(), toUtf8(), QTextCodec |
| 5318 | */ |
| 5319 | |
| 5320 | QByteArray QString::toLocal8Bit_helper(const QChar *data, int size) |
| 5321 | { |
| 5322 | return qt_convert_to_local_8bit(string: QStringView(data, size)); |
| 5323 | } |
| 5324 | |
| 5325 | static QByteArray qt_convert_to_local_8bit(QStringView string) |
| 5326 | { |
| 5327 | if (string.isNull()) |
| 5328 | return QByteArray(); |
| 5329 | #if QT_CONFIG(textcodec) |
| 5330 | QTextCodec *localeCodec = QTextCodec::codecForLocale(); |
| 5331 | if (localeCodec) |
| 5332 | return localeCodec->fromUnicode(uc: string); |
| 5333 | #endif // textcodec |
| 5334 | return qt_convert_to_latin1(string); |
| 5335 | } |
| 5336 | |
| 5337 | /*! |
| 5338 | \since 5.10 |
| 5339 | \internal |
| 5340 | \relates QStringView |
| 5341 | |
| 5342 | Returns a local 8-bit representation of \a string as a QByteArray. |
| 5343 | |
| 5344 | QTextCodec::codecForLocale() is used to perform the conversion from |
| 5345 | Unicode. |
| 5346 | |
| 5347 | The behavior is undefined if \a string contains characters not |
| 5348 | supported by the locale's 8-bit encoding. |
| 5349 | |
| 5350 | \sa QString::toLocal8Bit(), QStringView::toLocal8Bit() |
| 5351 | */ |
| 5352 | QByteArray QtPrivate::convertToLocal8Bit(QStringView string) |
| 5353 | { |
| 5354 | return qt_convert_to_local_8bit(string); |
| 5355 | } |
| 5356 | |
| 5357 | static QByteArray qt_convert_to_utf8(QStringView str); |
| 5358 | |
| 5359 | /*! |
| 5360 | \fn QByteArray QString::toUtf8() const |
| 5361 | |
| 5362 | Returns a UTF-8 representation of the string as a QByteArray. |
| 5363 | |
| 5364 | UTF-8 is a Unicode codec and can represent all characters in a Unicode |
| 5365 | string like QString. |
| 5366 | |
| 5367 | \sa fromUtf8(), toLatin1(), toLocal8Bit(), QTextCodec |
| 5368 | */ |
| 5369 | |
| 5370 | QByteArray QString::toUtf8_helper(const QString &str) |
| 5371 | { |
| 5372 | return qt_convert_to_utf8(str); |
| 5373 | } |
| 5374 | |
| 5375 | static QByteArray qt_convert_to_utf8(QStringView str) |
| 5376 | { |
| 5377 | if (str.isNull()) |
| 5378 | return QByteArray(); |
| 5379 | |
| 5380 | return QUtf8::convertFromUnicode(str.data(), str.length()); |
| 5381 | } |
| 5382 | |
| 5383 | /*! |
| 5384 | \since 5.10 |
| 5385 | \internal |
| 5386 | \relates QStringView |
| 5387 | |
| 5388 | Returns a UTF-8 representation of \a string as a QByteArray. |
| 5389 | |
| 5390 | UTF-8 is a Unicode codec and can represent all characters in a Unicode |
| 5391 | string like QStringView. |
| 5392 | |
| 5393 | \sa QString::toUtf8(), QStringView::toUtf8() |
| 5394 | */ |
| 5395 | QByteArray QtPrivate::convertToUtf8(QStringView string) |
| 5396 | { |
| 5397 | return qt_convert_to_utf8(str: string); |
| 5398 | } |
| 5399 | |
| 5400 | static QVector<uint> qt_convert_to_ucs4(QStringView string); |
| 5401 | |
| 5402 | /*! |
| 5403 | \since 4.2 |
| 5404 | |
| 5405 | Returns a UCS-4/UTF-32 representation of the string as a QVector<uint>. |
| 5406 | |
| 5407 | UCS-4 is a Unicode codec and therefore it is lossless. All characters from |
| 5408 | this string will be encoded in UCS-4. Any invalid sequence of code units in |
| 5409 | this string is replaced by the Unicode's replacement character |
| 5410 | (QChar::ReplacementCharacter, which corresponds to \c{U+FFFD}). |
| 5411 | |
| 5412 | The returned vector is not \\0'-terminated. |
| 5413 | |
| 5414 | \sa fromUtf8(), toUtf8(), toLatin1(), toLocal8Bit(), QTextCodec, fromUcs4(), toWCharArray() |
| 5415 | */ |
| 5416 | QVector<uint> QString::toUcs4() const |
| 5417 | { |
| 5418 | return qt_convert_to_ucs4(string: *this); |
| 5419 | } |
| 5420 | |
| 5421 | static QVector<uint> qt_convert_to_ucs4(QStringView string) |
| 5422 | { |
| 5423 | QVector<uint> v(string.length()); |
| 5424 | uint *a = const_cast<uint*>(v.constData()); |
| 5425 | QStringIterator it(string); |
| 5426 | while (it.hasNext()) |
| 5427 | *a++ = it.next(); |
| 5428 | v.resize(asize: a - v.constData()); |
| 5429 | return v; |
| 5430 | } |
| 5431 | |
| 5432 | /*! |
| 5433 | \since 5.10 |
| 5434 | \internal |
| 5435 | \relates QStringView |
| 5436 | |
| 5437 | Returns a UCS-4/UTF-32 representation of \a string as a QVector<uint>. |
| 5438 | |
| 5439 | UCS-4 is a Unicode codec and therefore it is lossless. All characters from |
| 5440 | this string will be encoded in UCS-4. Any invalid sequence of code units in |
| 5441 | this string is replaced by the Unicode's replacement character |
| 5442 | (QChar::ReplacementCharacter, which corresponds to \c{U+FFFD}). |
| 5443 | |
| 5444 | The returned vector is not \\0'-terminated. |
| 5445 | |
| 5446 | \sa QString::toUcs4(), QStringView::toUcs4(), QtPrivate::convertToLatin1(), |
| 5447 | QtPrivate::convertToLocal8Bit(), QtPrivate::convertToUtf8() |
| 5448 | */ |
| 5449 | QVector<uint> QtPrivate::convertToUcs4(QStringView string) |
| 5450 | { |
| 5451 | return qt_convert_to_ucs4(string); |
| 5452 | } |
| 5453 | |
| 5454 | QString::Data *QString::fromLatin1_helper(const char *str, int size) |
| 5455 | { |
| 5456 | Data *d; |
| 5457 | if (!str) { |
| 5458 | d = Data::sharedNull(); |
| 5459 | } else if (size == 0 || (!*str && size < 0)) { |
| 5460 | d = Data::allocate(capacity: 0); |
| 5461 | } else { |
| 5462 | if (size < 0) |
| 5463 | size = qstrlen(str); |
| 5464 | d = Data::allocate(capacity: size + 1); |
| 5465 | Q_CHECK_PTR(d); |
| 5466 | d->size = size; |
| 5467 | d->data()[size] = '\0'; |
| 5468 | ushort *dst = d->data(); |
| 5469 | |
| 5470 | qt_from_latin1(dst, str, size: uint(size)); |
| 5471 | } |
| 5472 | return d; |
| 5473 | } |
| 5474 | |
| 5475 | QString::Data *QString::fromAscii_helper(const char *str, int size) |
| 5476 | { |
| 5477 | QString s = fromUtf8(str, size); |
| 5478 | s.d->ref.ref(); |
| 5479 | return s.d; |
| 5480 | } |
| 5481 | |
| 5482 | /*! \fn QString QString::fromLatin1(const char *str, int size) |
| 5483 | Returns a QString initialized with the first \a size characters |
| 5484 | of the Latin-1 string \a str. |
| 5485 | |
| 5486 | If \a size is -1 (default), it is taken to be strlen(\a |
| 5487 | str). |
| 5488 | |
| 5489 | \sa toLatin1(), fromUtf8(), fromLocal8Bit() |
| 5490 | */ |
| 5491 | |
| 5492 | /*! |
| 5493 | \fn QString QString::fromLatin1(const QByteArray &str) |
| 5494 | \overload |
| 5495 | \since 5.0 |
| 5496 | |
| 5497 | Returns a QString initialized with the Latin-1 string \a str. |
| 5498 | */ |
| 5499 | |
| 5500 | /*! \fn QString QString::fromLocal8Bit(const char *str, int size) |
| 5501 | Returns a QString initialized with the first \a size characters |
| 5502 | of the 8-bit string \a str. |
| 5503 | |
| 5504 | If \a size is -1 (default), it is taken to be strlen(\a |
| 5505 | str). |
| 5506 | |
| 5507 | QTextCodec::codecForLocale() is used to perform the conversion. |
| 5508 | |
| 5509 | \sa toLocal8Bit(), fromLatin1(), fromUtf8() |
| 5510 | */ |
| 5511 | |
| 5512 | /*! |
| 5513 | \fn QString QString::fromLocal8Bit(const QByteArray &str) |
| 5514 | \overload |
| 5515 | \since 5.0 |
| 5516 | |
| 5517 | Returns a QString initialized with the 8-bit string \a str. |
| 5518 | */ |
| 5519 | QString QString::fromLocal8Bit_helper(const char *str, int size) |
| 5520 | { |
| 5521 | if (!str) |
| 5522 | return QString(); |
| 5523 | if (size == 0 || (!*str && size < 0)) { |
| 5524 | QStringDataPtr empty = { .ptr: Data::allocate(capacity: 0) }; |
| 5525 | return QString(empty); |
| 5526 | } |
| 5527 | #if QT_CONFIG(textcodec) |
| 5528 | if (size < 0) |
| 5529 | size = qstrlen(str); |
| 5530 | QTextCodec *codec = QTextCodec::codecForLocale(); |
| 5531 | if (codec) |
| 5532 | return codec->toUnicode(in: str, length: size); |
| 5533 | #endif // textcodec |
| 5534 | return fromLatin1(str, size); |
| 5535 | } |
| 5536 | |
| 5537 | /*! \fn QString QString::fromAscii(const char *, int size); |
| 5538 | \deprecated |
| 5539 | |
| 5540 | Returns a QString initialized with the first \a size characters |
| 5541 | from the string \a str. |
| 5542 | |
| 5543 | If \a size is -1 (default), it is taken to be strlen(\a |
| 5544 | str). |
| 5545 | |
| 5546 | This function does the same as fromLatin1(). |
| 5547 | |
| 5548 | \sa toAscii(), fromLatin1(), fromUtf8(), fromLocal8Bit() |
| 5549 | */ |
| 5550 | |
| 5551 | /*! |
| 5552 | \fn QString QString::fromAscii(const QByteArray &str) |
| 5553 | \deprecated |
| 5554 | \overload |
| 5555 | \since 5.0 |
| 5556 | |
| 5557 | Returns a QString initialized with the string \a str. |
| 5558 | */ |
| 5559 | |
| 5560 | /*! \fn QString QString::fromUtf8(const char *str, int size) |
| 5561 | Returns a QString initialized with the first \a size bytes |
| 5562 | of the UTF-8 string \a str. |
| 5563 | |
| 5564 | If \a size is -1 (default), it is taken to be strlen(\a |
| 5565 | str). |
| 5566 | |
| 5567 | UTF-8 is a Unicode codec and can represent all characters in a Unicode |
| 5568 | string like QString. However, invalid sequences are possible with UTF-8 |
| 5569 | and, if any such are found, they will be replaced with one or more |
| 5570 | "replacement characters", or suppressed. These include non-Unicode |
| 5571 | sequences, non-characters, overlong sequences or surrogate codepoints |
| 5572 | encoded into UTF-8. |
| 5573 | |
| 5574 | This function can be used to process incoming data incrementally as long as |
| 5575 | all UTF-8 characters are terminated within the incoming data. Any |
| 5576 | unterminated characters at the end of the string will be replaced or |
| 5577 | suppressed. In order to do stateful decoding, please use \l QTextDecoder. |
| 5578 | |
| 5579 | \sa toUtf8(), fromLatin1(), fromLocal8Bit() |
| 5580 | */ |
| 5581 | |
| 5582 | /*! |
| 5583 | \fn QString QString::fromUtf8(const QByteArray &str) |
| 5584 | \overload |
| 5585 | \since 5.0 |
| 5586 | |
| 5587 | Returns a QString initialized with the UTF-8 string \a str. |
| 5588 | */ |
| 5589 | QString QString::fromUtf8_helper(const char *str, int size) |
| 5590 | { |
| 5591 | if (!str) |
| 5592 | return QString(); |
| 5593 | |
| 5594 | Q_ASSERT(size != -1); |
| 5595 | return QUtf8::convertToUnicode(str, size); |
| 5596 | } |
| 5597 | |
| 5598 | /*! |
| 5599 | Returns a QString initialized with the first \a size characters |
| 5600 | of the Unicode string \a unicode (ISO-10646-UTF-16 encoded). |
| 5601 | |
| 5602 | If \a size is -1 (default), \a unicode must be \\0'-terminated. |
| 5603 | |
| 5604 | This function checks for a Byte Order Mark (BOM). If it is missing, |
| 5605 | host byte order is assumed. |
| 5606 | |
| 5607 | This function is slow compared to the other Unicode conversions. |
| 5608 | Use QString(const QChar *, int) or QString(const QChar *) if possible. |
| 5609 | |
| 5610 | QString makes a deep copy of the Unicode data. |
| 5611 | |
| 5612 | \sa utf16(), setUtf16(), fromStdU16String() |
| 5613 | */ |
| 5614 | QString QString::fromUtf16(const ushort *unicode, int size) |
| 5615 | { |
| 5616 | if (!unicode) |
| 5617 | return QString(); |
| 5618 | if (size < 0) { |
| 5619 | size = 0; |
| 5620 | while (unicode[size] != 0) |
| 5621 | ++size; |
| 5622 | } |
| 5623 | return QUtf16::convertToUnicode((const char *)unicode, size*2, nullptr); |
| 5624 | } |
| 5625 | |
| 5626 | /*! |
| 5627 | \fn QString QString::fromUtf16(const char16_t *str, int size) |
| 5628 | \since 5.3 |
| 5629 | |
| 5630 | Returns a QString initialized with the first \a size characters |
| 5631 | of the Unicode string \a str (ISO-10646-UTF-16 encoded). |
| 5632 | |
| 5633 | If \a size is -1 (default), \a str must be \\0'-terminated. |
| 5634 | |
| 5635 | This function checks for a Byte Order Mark (BOM). If it is missing, |
| 5636 | host byte order is assumed. |
| 5637 | |
| 5638 | This function is slow compared to the other Unicode conversions. |
| 5639 | Use QString(const QChar *, int) or QString(const QChar *) if possible. |
| 5640 | |
| 5641 | QString makes a deep copy of the Unicode data. |
| 5642 | |
| 5643 | \sa utf16(), setUtf16(), fromStdU16String() |
| 5644 | */ |
| 5645 | |
| 5646 | /*! |
| 5647 | \fn QString QString::fromUcs4(const char32_t *str, int size) |
| 5648 | \since 5.3 |
| 5649 | |
| 5650 | Returns a QString initialized with the first \a size characters |
| 5651 | of the Unicode string \a str (ISO-10646-UCS-4 encoded). |
| 5652 | |
| 5653 | If \a size is -1 (default), \a str must be \\0'-terminated. |
| 5654 | |
| 5655 | \sa toUcs4(), fromUtf16(), utf16(), setUtf16(), fromWCharArray(), fromStdU32String() |
| 5656 | */ |
| 5657 | |
| 5658 | /*! |
| 5659 | \since 4.2 |
| 5660 | |
| 5661 | Returns a QString initialized with the first \a size characters |
| 5662 | of the Unicode string \a unicode (ISO-10646-UCS-4 encoded). |
| 5663 | |
| 5664 | If \a size is -1 (default), \a unicode must be \\0'-terminated. |
| 5665 | |
| 5666 | \sa toUcs4(), fromUtf16(), utf16(), setUtf16(), fromWCharArray(), fromStdU32String() |
| 5667 | */ |
| 5668 | QString QString::fromUcs4(const uint *unicode, int size) |
| 5669 | { |
| 5670 | if (!unicode) |
| 5671 | return QString(); |
| 5672 | if (size < 0) { |
| 5673 | size = 0; |
| 5674 | while (unicode[size] != 0) |
| 5675 | ++size; |
| 5676 | } |
| 5677 | return QUtf32::convertToUnicode((const char *)unicode, size*4, nullptr); |
| 5678 | } |
| 5679 | |
| 5680 | |
| 5681 | /*! |
| 5682 | Resizes the string to \a size characters and copies \a unicode |
| 5683 | into the string. |
| 5684 | |
| 5685 | If \a unicode is 0, nothing is copied, but the string is still |
| 5686 | resized to \a size. |
| 5687 | |
| 5688 | \sa unicode(), setUtf16() |
| 5689 | */ |
| 5690 | QString& QString::setUnicode(const QChar *unicode, int size) |
| 5691 | { |
| 5692 | resize(size); |
| 5693 | if (unicode && size) |
| 5694 | memcpy(dest: d->data(), src: unicode, n: size * sizeof(QChar)); |
| 5695 | return *this; |
| 5696 | } |
| 5697 | |
| 5698 | /*! |
| 5699 | \fn QString &QString::setUtf16(const ushort *unicode, int size) |
| 5700 | |
| 5701 | Resizes the string to \a size characters and copies \a unicode |
| 5702 | into the string. |
| 5703 | |
| 5704 | If \a unicode is 0, nothing is copied, but the string is still |
| 5705 | resized to \a size. |
| 5706 | |
| 5707 | Note that unlike fromUtf16(), this function does not consider BOMs and |
| 5708 | possibly differing byte ordering. |
| 5709 | |
| 5710 | \sa utf16(), setUnicode() |
| 5711 | */ |
| 5712 | |
| 5713 | /*! |
| 5714 | \fn QString QString::simplified() const |
| 5715 | |
| 5716 | Returns a string that has whitespace removed from the start |
| 5717 | and the end, and that has each sequence of internal whitespace |
| 5718 | replaced with a single space. |
| 5719 | |
| 5720 | Whitespace means any character for which QChar::isSpace() returns |
| 5721 | \c true. This includes the ASCII characters '\\t', '\\n', '\\v', |
| 5722 | '\\f', '\\r', and ' '. |
| 5723 | |
| 5724 | Example: |
| 5725 | |
| 5726 | \snippet qstring/main.cpp 57 |
| 5727 | |
| 5728 | \sa trimmed() |
| 5729 | */ |
| 5730 | QString QString::simplified_helper(const QString &str) |
| 5731 | { |
| 5732 | return QStringAlgorithms<const QString>::simplified_helper(str); |
| 5733 | } |
| 5734 | |
| 5735 | QString QString::simplified_helper(QString &str) |
| 5736 | { |
| 5737 | return QStringAlgorithms<QString>::simplified_helper(str); |
| 5738 | } |
| 5739 | |
| 5740 | namespace { |
| 5741 | template <typename StringView> |
| 5742 | StringView qt_trimmed(StringView s) noexcept |
| 5743 | { |
| 5744 | auto begin = s.begin(); |
| 5745 | auto end = s.end(); |
| 5746 | QStringAlgorithms<const StringView>::trimmed_helper_positions(begin, end); |
| 5747 | return StringView{begin, end}; |
| 5748 | } |
| 5749 | } |
| 5750 | |
| 5751 | /*! |
| 5752 | \fn QStringView QtPrivate::trimmed(QStringView s) |
| 5753 | \fn QLatin1String QtPrivate::trimmed(QLatin1String s) |
| 5754 | \internal |
| 5755 | \relates QStringView |
| 5756 | \since 5.10 |
| 5757 | |
| 5758 | Returns \a s with whitespace removed from the start and the end. |
| 5759 | |
| 5760 | Whitespace means any character for which QChar::isSpace() returns |
| 5761 | \c true. This includes the ASCII characters '\\t', '\\n', '\\v', |
| 5762 | '\\f', '\\r', and ' '. |
| 5763 | |
| 5764 | \sa QString::trimmed(), QStringView::trimmed(), QLatin1String::trimmed() |
| 5765 | */ |
| 5766 | QStringView QtPrivate::trimmed(QStringView s) noexcept |
| 5767 | { |
| 5768 | return qt_trimmed(s); |
| 5769 | } |
| 5770 | |
| 5771 | QLatin1String QtPrivate::trimmed(QLatin1String s) noexcept |
| 5772 | { |
| 5773 | return qt_trimmed(s); |
| 5774 | } |
| 5775 | |
| 5776 | /*! |
| 5777 | \fn QString QString::trimmed() const |
| 5778 | |
| 5779 | Returns a string that has whitespace removed from the start and |
| 5780 | the end. |
| 5781 | |
| 5782 | Whitespace means any character for which QChar::isSpace() returns |
| 5783 | \c true. This includes the ASCII characters '\\t', '\\n', '\\v', |
| 5784 | '\\f', '\\r', and ' '. |
| 5785 | |
| 5786 | Example: |
| 5787 | |
| 5788 | \snippet qstring/main.cpp 82 |
| 5789 | |
| 5790 | Unlike simplified(), trimmed() leaves internal whitespace alone. |
| 5791 | |
| 5792 | \sa simplified() |
| 5793 | */ |
| 5794 | QString QString::trimmed_helper(const QString &str) |
| 5795 | { |
| 5796 | return QStringAlgorithms<const QString>::trimmed_helper(str); |
| 5797 | } |
| 5798 | |
| 5799 | QString QString::trimmed_helper(QString &str) |
| 5800 | { |
| 5801 | return QStringAlgorithms<QString>::trimmed_helper(str); |
| 5802 | } |
| 5803 | |
| 5804 | /*! \fn const QChar QString::at(int position) const |
| 5805 | |
| 5806 | Returns the character at the given index \a position in the |
| 5807 | string. |
| 5808 | |
| 5809 | The \a position must be a valid index position in the string |
| 5810 | (i.e., 0 <= \a position < size()). |
| 5811 | |
| 5812 | \sa operator[]() |
| 5813 | */ |
| 5814 | |
| 5815 | /*! |
| 5816 | \fn QCharRef QString::operator[](int position) |
| 5817 | |
| 5818 | Returns the character at the specified \a position in the string as a |
| 5819 | modifiable reference. |
| 5820 | |
| 5821 | Example: |
| 5822 | |
| 5823 | \snippet qstring/main.cpp 85 |
| 5824 | |
| 5825 | The return value is of type QCharRef, a helper class for QString. |
| 5826 | When you get an object of type QCharRef, you can use it as if it |
| 5827 | were a reference to a QChar. If you assign to it, the assignment will apply to |
| 5828 | the character in the QString from which you got the reference. |
| 5829 | |
| 5830 | \note Before Qt 5.14 it was possible to use this operator to access |
| 5831 | a character at an out-of-bounds position in the string, and |
| 5832 | then assign to such a position, causing the string to be |
| 5833 | automatically resized. Furthermore, assigning a value to the |
| 5834 | returned QCharRef would cause a detach of the string, even if the |
| 5835 | string has been copied in the meanwhile (and the QCharRef kept |
| 5836 | alive while the copy was taken). These behaviors are deprecated, |
| 5837 | and will be changed in a future version of Qt. |
| 5838 | |
| 5839 | \sa at() |
| 5840 | */ |
| 5841 | |
| 5842 | /*! |
| 5843 | \fn const QChar QString::operator[](int position) const |
| 5844 | |
| 5845 | \overload operator[]() |
| 5846 | */ |
| 5847 | |
| 5848 | /*! \fn QCharRef QString::operator[](uint position) |
| 5849 | |
| 5850 | \overload operator[]() |
| 5851 | |
| 5852 | Returns the character at the specified \a position in the string as a |
| 5853 | modifiable reference. |
| 5854 | */ |
| 5855 | |
| 5856 | /*! \fn const QChar QString::operator[](uint position) const |
| 5857 | Equivalent to \c at(position). |
| 5858 | \overload operator[]() |
| 5859 | */ |
| 5860 | |
| 5861 | /*! |
| 5862 | \fn QChar QString::front() const |
| 5863 | \since 5.10 |
| 5864 | |
| 5865 | Returns the first character in the string. |
| 5866 | Same as \c{at(0)}. |
| 5867 | |
| 5868 | This function is provided for STL compatibility. |
| 5869 | |
| 5870 | \warning Calling this function on an empty string constitutes |
| 5871 | undefined behavior. |
| 5872 | |
| 5873 | \sa back(), at(), operator[]() |
| 5874 | */ |
| 5875 | |
| 5876 | /*! |
| 5877 | \fn QChar QString::back() const |
| 5878 | \since 5.10 |
| 5879 | |
| 5880 | Returns the last character in the string. |
| 5881 | Same as \c{at(size() - 1)}. |
| 5882 | |
| 5883 | This function is provided for STL compatibility. |
| 5884 | |
| 5885 | \warning Calling this function on an empty string constitutes |
| 5886 | undefined behavior. |
| 5887 | |
| 5888 | \sa front(), at(), operator[]() |
| 5889 | */ |
| 5890 | |
| 5891 | /*! |
| 5892 | \fn QCharRef QString::front() |
| 5893 | \since 5.10 |
| 5894 | |
| 5895 | Returns a reference to the first character in the string. |
| 5896 | Same as \c{operator[](0)}. |
| 5897 | |
| 5898 | This function is provided for STL compatibility. |
| 5899 | |
| 5900 | \warning Calling this function on an empty string constitutes |
| 5901 | undefined behavior. |
| 5902 | |
| 5903 | \sa back(), at(), operator[]() |
| 5904 | */ |
| 5905 | |
| 5906 | /*! |
| 5907 | \fn QCharRef QString::back() |
| 5908 | \since 5.10 |
| 5909 | |
| 5910 | Returns a reference to the last character in the string. |
| 5911 | Same as \c{operator[](size() - 1)}. |
| 5912 | |
| 5913 | This function is provided for STL compatibility. |
| 5914 | |
| 5915 | \warning Calling this function on an empty string constitutes |
| 5916 | undefined behavior. |
| 5917 | |
| 5918 | \sa front(), at(), operator[]() |
| 5919 | */ |
| 5920 | |
| 5921 | /*! |
| 5922 | \fn void QString::truncate(int position) |
| 5923 | |
| 5924 | Truncates the string at the given \a position index. |
| 5925 | |
| 5926 | If the specified \a position index is beyond the end of the |
| 5927 | string, nothing happens. |
| 5928 | |
| 5929 | Example: |
| 5930 | |
| 5931 | \snippet qstring/main.cpp 83 |
| 5932 | |
| 5933 | If \a position is negative, it is equivalent to passing zero. |
| 5934 | |
| 5935 | \sa chop(), resize(), left(), QStringRef::truncate() |
| 5936 | */ |
| 5937 | |
| 5938 | void QString::truncate(int pos) |
| 5939 | { |
| 5940 | if (pos < d->size) |
| 5941 | resize(size: pos); |
| 5942 | } |
| 5943 | |
| 5944 | |
| 5945 | /*! |
| 5946 | Removes \a n characters from the end of the string. |
| 5947 | |
| 5948 | If \a n is greater than or equal to size(), the result is an |
| 5949 | empty string; if \a n is negative, it is equivalent to passing zero. |
| 5950 | |
| 5951 | Example: |
| 5952 | \snippet qstring/main.cpp 15 |
| 5953 | |
| 5954 | If you want to remove characters from the \e beginning of the |
| 5955 | string, use remove() instead. |
| 5956 | |
| 5957 | \sa truncate(), resize(), remove(), QStringRef::chop() |
| 5958 | */ |
| 5959 | void QString::chop(int n) |
| 5960 | { |
| 5961 | if (n > 0) |
| 5962 | resize(size: d->size - n); |
| 5963 | } |
| 5964 | |
| 5965 | /*! |
| 5966 | Sets every character in the string to character \a ch. If \a size |
| 5967 | is different from -1 (default), the string is resized to \a |
| 5968 | size beforehand. |
| 5969 | |
| 5970 | Example: |
| 5971 | |
| 5972 | \snippet qstring/main.cpp 21 |
| 5973 | |
| 5974 | \sa resize() |
| 5975 | */ |
| 5976 | |
| 5977 | QString& QString::fill(QChar ch, int size) |
| 5978 | { |
| 5979 | resize(size: size < 0 ? d->size : size); |
| 5980 | if (d->size) { |
| 5981 | QChar *i = (QChar*)d->data() + d->size; |
| 5982 | QChar *b = (QChar*)d->data(); |
| 5983 | while (i != b) |
| 5984 | *--i = ch; |
| 5985 | } |
| 5986 | return *this; |
| 5987 | } |
| 5988 | |
| 5989 | /*! |
| 5990 | \fn int QString::length() const |
| 5991 | |
| 5992 | Returns the number of characters in this string. Equivalent to |
| 5993 | size(). |
| 5994 | |
| 5995 | \sa resize() |
| 5996 | */ |
| 5997 | |
| 5998 | /*! |
| 5999 | \fn int QString::size() const |
| 6000 | |
| 6001 | Returns the number of characters in this string. |
| 6002 | |
| 6003 | The last character in the string is at position size() - 1. |
| 6004 | |
| 6005 | Example: |
| 6006 | \snippet qstring/main.cpp 58 |
| 6007 | |
| 6008 | \sa isEmpty(), resize() |
| 6009 | */ |
| 6010 | |
| 6011 | /*! \fn bool QString::isNull() const |
| 6012 | |
| 6013 | Returns \c true if this string is null; otherwise returns \c false. |
| 6014 | |
| 6015 | Example: |
| 6016 | |
| 6017 | \snippet qstring/main.cpp 28 |
| 6018 | |
| 6019 | Qt makes a distinction between null strings and empty strings for |
| 6020 | historical reasons. For most applications, what matters is |
| 6021 | whether or not a string contains any data, and this can be |
| 6022 | determined using the isEmpty() function. |
| 6023 | |
| 6024 | \sa isEmpty() |
| 6025 | */ |
| 6026 | |
| 6027 | /*! \fn bool QString::isEmpty() const |
| 6028 | |
| 6029 | Returns \c true if the string has no characters; otherwise returns |
| 6030 | \c false. |
| 6031 | |
| 6032 | Example: |
| 6033 | |
| 6034 | \snippet qstring/main.cpp 27 |
| 6035 | |
| 6036 | \sa size() |
| 6037 | */ |
| 6038 | |
| 6039 | /*! \fn QString &QString::operator+=(const QString &other) |
| 6040 | |
| 6041 | Appends the string \a other onto the end of this string and |
| 6042 | returns a reference to this string. |
| 6043 | |
| 6044 | Example: |
| 6045 | |
| 6046 | \snippet qstring/main.cpp 84 |
| 6047 | |
| 6048 | This operation is typically very fast (\l{constant time}), |
| 6049 | because QString preallocates extra space at the end of the string |
| 6050 | data so it can grow without reallocating the entire string each |
| 6051 | time. |
| 6052 | |
| 6053 | \sa append(), prepend() |
| 6054 | */ |
| 6055 | |
| 6056 | /*! \fn QString &QString::operator+=(QLatin1String str) |
| 6057 | |
| 6058 | \overload operator+=() |
| 6059 | |
| 6060 | Appends the Latin-1 string \a str to this string. |
| 6061 | */ |
| 6062 | |
| 6063 | /*! \fn QString &QString::operator+=(const QByteArray &ba) |
| 6064 | |
| 6065 | \overload operator+=() |
| 6066 | |
| 6067 | Appends the byte array \a ba to this string. The byte array is converted |
| 6068 | to Unicode using the fromUtf8() function. If any NUL characters ('\\0') |
| 6069 | are embedded in the \a ba byte array, they will be included in the |
| 6070 | transformation. |
| 6071 | |
| 6072 | You can disable this function by defining \c |
| 6073 | QT_NO_CAST_FROM_ASCII when you compile your applications. This |
| 6074 | can be useful if you want to ensure that all user-visible strings |
| 6075 | go through QObject::tr(), for example. |
| 6076 | |
| 6077 | \sa QT_NO_CAST_FROM_ASCII |
| 6078 | */ |
| 6079 | |
| 6080 | /*! \fn QString &QString::operator+=(const char *str) |
| 6081 | |
| 6082 | \overload operator+=() |
| 6083 | |
| 6084 | Appends the string \a str to this string. The const char pointer |
| 6085 | is converted to Unicode using the fromUtf8() function. |
| 6086 | |
| 6087 | You can disable this function by defining \c QT_NO_CAST_FROM_ASCII |
| 6088 | when you compile your applications. This can be useful if you want |
| 6089 | to ensure that all user-visible strings go through QObject::tr(), |
| 6090 | for example. |
| 6091 | |
| 6092 | \sa QT_NO_CAST_FROM_ASCII |
| 6093 | */ |
| 6094 | |
| 6095 | /*! \fn QString &QString::operator+=(const QStringRef &str) |
| 6096 | |
| 6097 | \overload operator+=() |
| 6098 | |
| 6099 | Appends the string section referenced by \a str to this string. |
| 6100 | */ |
| 6101 | |
| 6102 | /*! \fn QString &QString::operator+=(QStringView str) |
| 6103 | \since 5.15.2 |
| 6104 | \overload operator+=() |
| 6105 | |
| 6106 | Appends the string section referenced by \a str to this string. |
| 6107 | |
| 6108 | \note This method has been added in 5.15.2 to simplify writing code that is portable |
| 6109 | between Qt 5.15 and Qt 6. |
| 6110 | */ |
| 6111 | |
| 6112 | /*! \fn QString &QString::operator+=(char ch) |
| 6113 | |
| 6114 | \overload operator+=() |
| 6115 | |
| 6116 | Appends the character \a ch to this string. Note that the character is |
| 6117 | converted to Unicode using the fromLatin1() function, unlike other 8-bit |
| 6118 | functions that operate on UTF-8 data. |
| 6119 | |
| 6120 | You can disable this function by defining \c QT_NO_CAST_FROM_ASCII |
| 6121 | when you compile your applications. This can be useful if you want |
| 6122 | to ensure that all user-visible strings go through QObject::tr(), |
| 6123 | for example. |
| 6124 | |
| 6125 | \sa QT_NO_CAST_FROM_ASCII |
| 6126 | */ |
| 6127 | |
| 6128 | /*! \fn QString &QString::operator+=(QChar ch) |
| 6129 | |
| 6130 | \overload operator+=() |
| 6131 | |
| 6132 | Appends the character \a ch to the string. |
| 6133 | */ |
| 6134 | |
| 6135 | /*! \fn QString &QString::operator+=(QChar::SpecialCharacter c) |
| 6136 | |
| 6137 | \overload operator+=() |
| 6138 | |
| 6139 | \internal |
| 6140 | */ |
| 6141 | |
| 6142 | /*! |
| 6143 | \fn bool operator==(const char *s1, const QString &s2) |
| 6144 | |
| 6145 | \overload operator==() |
| 6146 | \relates QString |
| 6147 | |
| 6148 | Returns \c true if \a s1 is equal to \a s2; otherwise returns \c false. |
| 6149 | Note that no string is equal to \a s1 being 0. |
| 6150 | |
| 6151 | Equivalent to \c {s1 != 0 && compare(s1, s2) == 0}. |
| 6152 | */ |
| 6153 | |
| 6154 | /*! |
| 6155 | \fn bool operator!=(const char *s1, const QString &s2) |
| 6156 | \relates QString |
| 6157 | |
| 6158 | Returns \c true if \a s1 is not equal to \a s2; otherwise returns |
| 6159 | \c false. |
| 6160 | |
| 6161 | For \a s1 != 0, this is equivalent to \c {compare(} \a s1, \a s2 |
| 6162 | \c {) != 0}. Note that no string is equal to \a s1 being 0. |
| 6163 | */ |
| 6164 | |
| 6165 | /*! |
| 6166 | \fn bool operator<(const char *s1, const QString &s2) |
| 6167 | \relates QString |
| 6168 | |
| 6169 | Returns \c true if \a s1 is lexically less than \a s2; otherwise |
| 6170 | returns \c false. For \a s1 != 0, this is equivalent to \c |
| 6171 | {compare(s1, s2) < 0}. |
| 6172 | |
| 6173 | \sa {Comparing Strings} |
| 6174 | */ |
| 6175 | |
| 6176 | /*! |
| 6177 | \fn bool operator<=(const char *s1, const QString &s2) |
| 6178 | \relates QString |
| 6179 | |
| 6180 | Returns \c true if \a s1 is lexically less than or equal to \a s2; |
| 6181 | otherwise returns \c false. For \a s1 != 0, this is equivalent to \c |
| 6182 | {compare(s1, s2) <= 0}. |
| 6183 | |
| 6184 | \sa {Comparing Strings} |
| 6185 | */ |
| 6186 | |
| 6187 | /*! |
| 6188 | \fn bool operator>(const char *s1, const QString &s2) |
| 6189 | \relates QString |
| 6190 | |
| 6191 | Returns \c true if \a s1 is lexically greater than \a s2; otherwise |
| 6192 | returns \c false. Equivalent to \c {compare(s1, s2) > 0}. |
| 6193 | |
| 6194 | \sa {Comparing Strings} |
| 6195 | */ |
| 6196 | |
| 6197 | /*! |
| 6198 | \fn bool operator>=(const char *s1, const QString &s2) |
| 6199 | \relates QString |
| 6200 | |
| 6201 | Returns \c true if \a s1 is lexically greater than or equal to \a s2; |
| 6202 | otherwise returns \c false. For \a s1 != 0, this is equivalent to \c |
| 6203 | {compare(s1, s2) >= 0}. |
| 6204 | |
| 6205 | \sa {Comparing Strings} |
| 6206 | */ |
| 6207 | |
| 6208 | /*! |
| 6209 | \fn const QString operator+(const QString &s1, const QString &s2) |
| 6210 | \relates QString |
| 6211 | |
| 6212 | Returns a string which is the result of concatenating \a s1 and \a |
| 6213 | s2. |
| 6214 | */ |
| 6215 | |
| 6216 | /*! |
| 6217 | \fn const QString operator+(const QString &s1, const char *s2) |
| 6218 | \relates QString |
| 6219 | |
| 6220 | Returns a string which is the result of concatenating \a s1 and \a |
| 6221 | s2 (\a s2 is converted to Unicode using the QString::fromUtf8() |
| 6222 | function). |
| 6223 | |
| 6224 | \sa QString::fromUtf8() |
| 6225 | */ |
| 6226 | |
| 6227 | /*! |
| 6228 | \fn const QString operator+(const char *s1, const QString &s2) |
| 6229 | \relates QString |
| 6230 | |
| 6231 | Returns a string which is the result of concatenating \a s1 and \a |
| 6232 | s2 (\a s1 is converted to Unicode using the QString::fromUtf8() |
| 6233 | function). |
| 6234 | |
| 6235 | \sa QString::fromUtf8() |
| 6236 | */ |
| 6237 | |
| 6238 | /*! |
| 6239 | \fn const QString operator+(const QString &s, char ch) |
| 6240 | \relates QString |
| 6241 | |
| 6242 | Returns a string which is the result of concatenating the string |
| 6243 | \a s and the character \a ch. |
| 6244 | */ |
| 6245 | |
| 6246 | /*! |
| 6247 | \fn const QString operator+(char ch, const QString &s) |
| 6248 | \relates QString |
| 6249 | |
| 6250 | Returns a string which is the result of concatenating the |
| 6251 | character \a ch and the string \a s. |
| 6252 | */ |
| 6253 | |
| 6254 | /*! |
| 6255 | \fn int QString::compare(const QString &s1, const QString &s2, Qt::CaseSensitivity cs) |
| 6256 | \since 4.2 |
| 6257 | |
| 6258 | Compares \a s1 with \a s2 and returns an integer less than, equal |
| 6259 | to, or greater than zero if \a s1 is less than, equal to, or |
| 6260 | greater than \a s2. |
| 6261 | |
| 6262 | If \a cs is Qt::CaseSensitive, the comparison is case sensitive; |
| 6263 | otherwise the comparison is case insensitive. |
| 6264 | |
| 6265 | Case sensitive comparison is based exclusively on the numeric |
| 6266 | Unicode values of the characters and is very fast, but is not what |
| 6267 | a human would expect. Consider sorting user-visible strings with |
| 6268 | localeAwareCompare(). |
| 6269 | |
| 6270 | \snippet qstring/main.cpp 16 |
| 6271 | |
| 6272 | \sa operator==(), operator<(), operator>(), {Comparing Strings} |
| 6273 | */ |
| 6274 | |
| 6275 | /*! |
| 6276 | \fn int QString::compare(const QString &s1, QLatin1String s2, Qt::CaseSensitivity cs) |
| 6277 | \since 4.2 |
| 6278 | \overload compare() |
| 6279 | |
| 6280 | Performs a comparison of \a s1 and \a s2, using the case |
| 6281 | sensitivity setting \a cs. |
| 6282 | */ |
| 6283 | |
| 6284 | /*! |
| 6285 | \fn int QString::compare(QLatin1String s1, const QString &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive) |
| 6286 | |
| 6287 | \since 4.2 |
| 6288 | \overload compare() |
| 6289 | |
| 6290 | Performs a comparison of \a s1 and \a s2, using the case |
| 6291 | sensitivity setting \a cs. |
| 6292 | */ |
| 6293 | |
| 6294 | /*! |
| 6295 | \fn int QString::compare(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
| 6296 | |
| 6297 | \since 5.12 |
| 6298 | \overload compare() |
| 6299 | |
| 6300 | Performs a comparison of this with \a s, using the case |
| 6301 | sensitivity setting \a cs. |
| 6302 | */ |
| 6303 | |
| 6304 | /*! |
| 6305 | \fn int QString::compare(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
| 6306 | |
| 6307 | \since 5.14 |
| 6308 | \overload compare() |
| 6309 | |
| 6310 | Performs a comparison of this with \a ch, using the case |
| 6311 | sensitivity setting \a cs. |
| 6312 | */ |
| 6313 | |
| 6314 | #if QT_STRINGVIEW_LEVEL < 2 |
| 6315 | /*! |
| 6316 | \overload compare() |
| 6317 | \since 4.2 |
| 6318 | |
| 6319 | Lexically compares this string with the \a other string and |
| 6320 | returns an integer less than, equal to, or greater than zero if |
| 6321 | this string is less than, equal to, or greater than the other |
| 6322 | string. |
| 6323 | |
| 6324 | Same as compare(*this, \a other, \a cs). |
| 6325 | */ |
| 6326 | int QString::compare(const QString &other, Qt::CaseSensitivity cs) const noexcept |
| 6327 | { |
| 6328 | return qt_compare_strings(lhs: *this, rhs: other, cs); |
| 6329 | } |
| 6330 | #endif |
| 6331 | |
| 6332 | /*! |
| 6333 | \internal |
| 6334 | \since 4.5 |
| 6335 | */ |
| 6336 | int QString::compare_helper(const QChar *data1, int length1, const QChar *data2, int length2, |
| 6337 | Qt::CaseSensitivity cs) noexcept |
| 6338 | { |
| 6339 | Q_ASSERT(length1 >= 0); |
| 6340 | Q_ASSERT(length2 >= 0); |
| 6341 | Q_ASSERT(data1 || length1 == 0); |
| 6342 | Q_ASSERT(data2 || length2 == 0); |
| 6343 | return qt_compare_strings(lhs: QStringView(data1, length1), rhs: QStringView(data2, length2), cs); |
| 6344 | } |
| 6345 | |
| 6346 | /*! |
| 6347 | \overload compare() |
| 6348 | \since 4.2 |
| 6349 | |
| 6350 | Same as compare(*this, \a other, \a cs). |
| 6351 | */ |
| 6352 | int QString::compare(QLatin1String other, Qt::CaseSensitivity cs) const noexcept |
| 6353 | { |
| 6354 | return qt_compare_strings(lhs: *this, rhs: other, cs); |
| 6355 | } |
| 6356 | |
| 6357 | #if QT_STRINGVIEW_LEVEL < 2 |
| 6358 | /*! |
| 6359 | \fn int QString::compare(const QStringRef &ref, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
| 6360 | \overload compare() |
| 6361 | |
| 6362 | Compares the string reference, \a ref, with the string and returns |
| 6363 | an integer less than, equal to, or greater than zero if the string |
| 6364 | is less than, equal to, or greater than \a ref. |
| 6365 | */ |
| 6366 | #endif |
| 6367 | |
| 6368 | /*! |
| 6369 | \internal |
| 6370 | \since 5.0 |
| 6371 | */ |
| 6372 | int QString::compare_helper(const QChar *data1, int length1, const char *data2, int length2, |
| 6373 | Qt::CaseSensitivity cs) |
| 6374 | { |
| 6375 | Q_ASSERT(length1 >= 0); |
| 6376 | Q_ASSERT(data1 || length1 == 0); |
| 6377 | if (!data2) |
| 6378 | return length1; |
| 6379 | if (Q_UNLIKELY(length2 < 0)) |
| 6380 | length2 = int(strlen(s: data2)); |
| 6381 | // ### make me nothrow in all cases |
| 6382 | QVarLengthArray<ushort> s2(length2); |
| 6383 | const auto beg = reinterpret_cast<QChar *>(s2.data()); |
| 6384 | const auto end = QUtf8::convertToUnicode(beg, data2, length2); |
| 6385 | return qt_compare_strings(lhs: QStringView(data1, length1), rhs: QStringView(beg, end - beg), cs); |
| 6386 | } |
| 6387 | |
| 6388 | /*! |
| 6389 | \fn int QString::compare(const QString &s1, const QStringRef &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive) |
| 6390 | \overload compare() |
| 6391 | */ |
| 6392 | |
| 6393 | /*! |
| 6394 | \internal |
| 6395 | \since 4.5 |
| 6396 | */ |
| 6397 | int QString::compare_helper(const QChar *data1, int length1, QLatin1String s2, |
| 6398 | Qt::CaseSensitivity cs) noexcept |
| 6399 | { |
| 6400 | Q_ASSERT(length1 >= 0); |
| 6401 | Q_ASSERT(data1 || length1 == 0); |
| 6402 | return qt_compare_strings(lhs: QStringView(data1, length1), rhs: s2, cs); |
| 6403 | } |
| 6404 | |
| 6405 | /*! |
| 6406 | \fn int QString::localeAwareCompare(const QString & s1, const QString & s2) |
| 6407 | |
| 6408 | Compares \a s1 with \a s2 and returns an integer less than, equal |
| 6409 | to, or greater than zero if \a s1 is less than, equal to, or |
| 6410 | greater than \a s2. |
| 6411 | |
| 6412 | The comparison is performed in a locale- and also |
| 6413 | platform-dependent manner. Use this function to present sorted |
| 6414 | lists of strings to the user. |
| 6415 | |
| 6416 | \sa compare(), QLocale, {Comparing Strings} |
| 6417 | */ |
| 6418 | |
| 6419 | /*! |
| 6420 | \fn int QString::localeAwareCompare(const QStringRef &other) const |
| 6421 | \since 4.5 |
| 6422 | \overload localeAwareCompare() |
| 6423 | |
| 6424 | Compares this string with the \a other string and returns an |
| 6425 | integer less than, equal to, or greater than zero if this string |
| 6426 | is less than, equal to, or greater than the \a other string. |
| 6427 | |
| 6428 | The comparison is performed in a locale- and also |
| 6429 | platform-dependent manner. Use this function to present sorted |
| 6430 | lists of strings to the user. |
| 6431 | |
| 6432 | Same as \c {localeAwareCompare(*this, other)}. |
| 6433 | |
| 6434 | \sa {Comparing Strings} |
| 6435 | */ |
| 6436 | |
| 6437 | /*! |
| 6438 | \fn int QString::localeAwareCompare(QStringView other) const |
| 6439 | \since 5.15.2 |
| 6440 | \overload localeAwareCompare() |
| 6441 | |
| 6442 | Compares this string with the \a other string and returns an |
| 6443 | integer less than, equal to, or greater than zero if this string |
| 6444 | is less than, equal to, or greater than the \a other string. |
| 6445 | |
| 6446 | The comparison is performed in a locale- and also |
| 6447 | platform-dependent manner. Use this function to present sorted |
| 6448 | lists of strings to the user. |
| 6449 | |
| 6450 | Same as \c {localeAwareCompare(*this, other)}. |
| 6451 | |
| 6452 | \note This method has been added in 5.15.2 to simplify writing code that is portable |
| 6453 | between Qt 5.15 and Qt 6. |
| 6454 | |
| 6455 | \sa {Comparing Strings} |
| 6456 | */ |
| 6457 | |
| 6458 | /*! |
| 6459 | \fn int QString::localeAwareCompare(const QString &s1, const QStringRef &s2) |
| 6460 | \since 4.5 |
| 6461 | \overload localeAwareCompare() |
| 6462 | |
| 6463 | Compares \a s1 with \a s2 and returns an integer less than, equal |
| 6464 | to, or greater than zero if \a s1 is less than, equal to, or |
| 6465 | greater than \a s2. |
| 6466 | |
| 6467 | The comparison is performed in a locale- and also |
| 6468 | platform-dependent manner. Use this function to present sorted |
| 6469 | lists of strings to the user. |
| 6470 | |
| 6471 | \sa {Comparing Strings} |
| 6472 | */ |
| 6473 | |
| 6474 | /*! |
| 6475 | \fn int QString::localeAwareCompare(QStringView s1, QStringView s2) |
| 6476 | \since 5.15.2 |
| 6477 | \overload localeAwareCompare() |
| 6478 | |
| 6479 | Compares \a s1 with \a s2 and returns an integer less than, equal |
| 6480 | to, or greater than zero if \a s1 is less than, equal to, or |
| 6481 | greater than \a s2. |
| 6482 | |
| 6483 | The comparison is performed in a locale- and also |
| 6484 | platform-dependent manner. Use this function to present sorted |
| 6485 | lists of strings to the user. |
| 6486 | |
| 6487 | \note This method has been added in 5.15.2 to simplify writing code that is portable |
| 6488 | between Qt 5.15 and Qt 6. |
| 6489 | |
| 6490 | \sa {Comparing Strings} |
| 6491 | */ |
| 6492 | |
| 6493 | #if !defined(CSTR_LESS_THAN) |
| 6494 | #define CSTR_LESS_THAN 1 |
| 6495 | #define CSTR_EQUAL 2 |
| 6496 | #define CSTR_GREATER_THAN 3 |
| 6497 | #endif |
| 6498 | |
| 6499 | /*! |
| 6500 | \overload localeAwareCompare() |
| 6501 | |
| 6502 | Compares this string with the \a other string and returns an |
| 6503 | integer less than, equal to, or greater than zero if this string |
| 6504 | is less than, equal to, or greater than the \a other string. |
| 6505 | |
| 6506 | The comparison is performed in a locale- and also |
| 6507 | platform-dependent manner. Use this function to present sorted |
| 6508 | lists of strings to the user. |
| 6509 | |
| 6510 | Same as \c {localeAwareCompare(*this, other)}. |
| 6511 | |
| 6512 | \sa {Comparing Strings} |
| 6513 | */ |
| 6514 | int QString::localeAwareCompare(const QString &other) const |
| 6515 | { |
| 6516 | return localeAwareCompare_helper(data1: constData(), length1: length(), data2: other.constData(), length2: other.length()); |
| 6517 | } |
| 6518 | |
| 6519 | #if QT_CONFIG(icu) |
| 6520 | Q_GLOBAL_STATIC(QThreadStorage<QCollator>, defaultCollator) |
| 6521 | #endif |
| 6522 | |
| 6523 | /*! |
| 6524 | \internal |
| 6525 | \since 4.5 |
| 6526 | */ |
| 6527 | int QString::localeAwareCompare_helper(const QChar *data1, int length1, |
| 6528 | const QChar *data2, int length2) |
| 6529 | { |
| 6530 | Q_ASSERT(length1 >= 0); |
| 6531 | Q_ASSERT(data1 || length1 == 0); |
| 6532 | Q_ASSERT(length2 >= 0); |
| 6533 | Q_ASSERT(data2 || length2 == 0); |
| 6534 | |
| 6535 | // do the right thing for null and empty |
| 6536 | if (length1 == 0 || length2 == 0) |
| 6537 | return qt_compare_strings(lhs: QStringView(data1, length1), rhs: QStringView(data2, length2), |
| 6538 | cs: Qt::CaseSensitive); |
| 6539 | |
| 6540 | #if QT_CONFIG(icu) |
| 6541 | if (!defaultCollator()->hasLocalData()) |
| 6542 | defaultCollator()->setLocalData(QCollator()); |
| 6543 | return defaultCollator()->localData().compare(s1: data1, len1: length1, s2: data2, len2: length2); |
| 6544 | #else |
| 6545 | const QString lhs = QString::fromRawData(data1, length1).normalized(QString::NormalizationForm_C); |
| 6546 | const QString rhs = QString::fromRawData(data2, length2).normalized(QString::NormalizationForm_C); |
| 6547 | # if defined(Q_OS_WIN) |
| 6548 | int res = CompareStringEx(LOCALE_NAME_USER_DEFAULT, 0, (LPWSTR)lhs.constData(), lhs.length(), (LPWSTR)rhs.constData(), rhs.length(), NULL, NULL, 0); |
| 6549 | |
| 6550 | switch (res) { |
| 6551 | case CSTR_LESS_THAN: |
| 6552 | return -1; |
| 6553 | case CSTR_GREATER_THAN: |
| 6554 | return 1; |
| 6555 | default: |
| 6556 | return 0; |
| 6557 | } |
| 6558 | # elif defined (Q_OS_DARWIN) |
| 6559 | // Use CFStringCompare for comparing strings on Mac. This makes Qt order |
| 6560 | // strings the same way as native applications do, and also respects |
| 6561 | // the "Order for sorted lists" setting in the International preferences |
| 6562 | // panel. |
| 6563 | const CFStringRef thisString = |
| 6564 | CFStringCreateWithCharactersNoCopy(kCFAllocatorDefault, |
| 6565 | reinterpret_cast<const UniChar *>(lhs.constData()), lhs.length(), kCFAllocatorNull); |
| 6566 | const CFStringRef otherString = |
| 6567 | CFStringCreateWithCharactersNoCopy(kCFAllocatorDefault, |
| 6568 | reinterpret_cast<const UniChar *>(rhs.constData()), rhs.length(), kCFAllocatorNull); |
| 6569 | |
| 6570 | const int result = CFStringCompare(thisString, otherString, kCFCompareLocalized); |
| 6571 | CFRelease(thisString); |
| 6572 | CFRelease(otherString); |
| 6573 | return result; |
| 6574 | # elif defined(Q_OS_UNIX) |
| 6575 | // declared in <string.h> |
| 6576 | int delta = strcoll(lhs.toLocal8Bit().constData(), rhs.toLocal8Bit().constData()); |
| 6577 | if (delta == 0) |
| 6578 | delta = qt_compare_strings(lhs, rhs, Qt::CaseSensitive); |
| 6579 | return delta; |
| 6580 | # else |
| 6581 | # error "This case shouldn't happen" |
| 6582 | return qt_compare_strings(lhs, rhs, Qt::CaseSensitive); |
| 6583 | # endif |
| 6584 | #endif // !QT_CONFIG(icu) |
| 6585 | } |
| 6586 | |
| 6587 | |
| 6588 | /*! |
| 6589 | \fn const QChar *QString::unicode() const |
| 6590 | |
| 6591 | Returns a Unicode representation of the string. |
| 6592 | The result remains valid until the string is modified. |
| 6593 | |
| 6594 | \note The returned string may not be '\\0'-terminated. |
| 6595 | Use size() to determine the length of the array. |
| 6596 | |
| 6597 | \sa utf16(), fromRawData() |
| 6598 | */ |
| 6599 | |
| 6600 | /*! |
| 6601 | \fn const ushort *QString::utf16() const |
| 6602 | |
| 6603 | Returns the QString as a '\\0\'-terminated array of unsigned |
| 6604 | shorts. The result remains valid until the string is modified. |
| 6605 | |
| 6606 | The returned string is in host byte order. |
| 6607 | |
| 6608 | \sa unicode() |
| 6609 | */ |
| 6610 | |
| 6611 | const ushort *QString::utf16() const |
| 6612 | { |
| 6613 | if (IS_RAW_DATA(d)) { |
| 6614 | // ensure '\0'-termination for ::fromRawData strings |
| 6615 | const_cast<QString*>(this)->reallocData(alloc: uint(d->size) + 1u); |
| 6616 | } |
| 6617 | return d->data(); |
| 6618 | } |
| 6619 | |
| 6620 | /*! |
| 6621 | Returns a string of size \a width that contains this string |
| 6622 | padded by the \a fill character. |
| 6623 | |
| 6624 | If \a truncate is \c false and the size() of the string is more than |
| 6625 | \a width, then the returned string is a copy of the string. |
| 6626 | |
| 6627 | \snippet qstring/main.cpp 32 |
| 6628 | |
| 6629 | If \a truncate is \c true and the size() of the string is more than |
| 6630 | \a width, then any characters in a copy of the string after |
| 6631 | position \a width are removed, and the copy is returned. |
| 6632 | |
| 6633 | \snippet qstring/main.cpp 33 |
| 6634 | |
| 6635 | \sa rightJustified() |
| 6636 | */ |
| 6637 | |
| 6638 | QString QString::leftJustified(int width, QChar fill, bool truncate) const |
| 6639 | { |
| 6640 | QString result; |
| 6641 | int len = length(); |
| 6642 | int padlen = width - len; |
| 6643 | if (padlen > 0) { |
| 6644 | result.resize(size: len+padlen); |
| 6645 | if (len) |
| 6646 | memcpy(dest: result.d->data(), src: d->data(), n: sizeof(QChar)*len); |
| 6647 | QChar *uc = (QChar*)result.d->data() + len; |
| 6648 | while (padlen--) |
| 6649 | * uc++ = fill; |
| 6650 | } else { |
| 6651 | if (truncate) |
| 6652 | result = left(n: width); |
| 6653 | else |
| 6654 | result = *this; |
| 6655 | } |
| 6656 | return result; |
| 6657 | } |
| 6658 | |
| 6659 | /*! |
| 6660 | Returns a string of size() \a width that contains the \a fill |
| 6661 | character followed by the string. For example: |
| 6662 | |
| 6663 | \snippet qstring/main.cpp 49 |
| 6664 | |
| 6665 | If \a truncate is \c false and the size() of the string is more than |
| 6666 | \a width, then the returned string is a copy of the string. |
| 6667 | |
| 6668 | If \a truncate is true and the size() of the string is more than |
| 6669 | \a width, then the resulting string is truncated at position \a |
| 6670 | width. |
| 6671 | |
| 6672 | \snippet qstring/main.cpp 50 |
| 6673 | |
| 6674 | \sa leftJustified() |
| 6675 | */ |
| 6676 | |
| 6677 | QString QString::rightJustified(int width, QChar fill, bool truncate) const |
| 6678 | { |
| 6679 | QString result; |
| 6680 | int len = length(); |
| 6681 | int padlen = width - len; |
| 6682 | if (padlen > 0) { |
| 6683 | result.resize(size: len+padlen); |
| 6684 | QChar *uc = (QChar*)result.d->data(); |
| 6685 | while (padlen--) |
| 6686 | * uc++ = fill; |
| 6687 | if (len) |
| 6688 | memcpy(dest: static_cast<void *>(uc), src: static_cast<const void *>(d->data()), n: sizeof(QChar)*len); |
| 6689 | } else { |
| 6690 | if (truncate) |
| 6691 | result = left(n: width); |
| 6692 | else |
| 6693 | result = *this; |
| 6694 | } |
| 6695 | return result; |
| 6696 | } |
| 6697 | |
| 6698 | /*! |
| 6699 | \fn QString QString::toLower() const |
| 6700 | |
| 6701 | Returns a lowercase copy of the string. |
| 6702 | |
| 6703 | \snippet qstring/main.cpp 75 |
| 6704 | |
| 6705 | The case conversion will always happen in the 'C' locale. For locale dependent |
| 6706 | case folding use QLocale::toLower() |
| 6707 | |
| 6708 | \sa toUpper(), QLocale::toLower() |
| 6709 | */ |
| 6710 | |
| 6711 | namespace QUnicodeTables { |
| 6712 | /* |
| 6713 | \internal |
| 6714 | Converts the \a str string starting from the position pointed to by the \a |
| 6715 | it iterator, using the Unicode case traits \c Traits, and returns the |
| 6716 | result. The input string must not be empty (the convertCase function below |
| 6717 | guarantees that). |
| 6718 | |
| 6719 | The string type \c{T} is also a template and is either \c{const QString} or |
| 6720 | \c{QString}. This function can do both copy-conversion and in-place |
| 6721 | conversion depending on the state of the \a str parameter: |
| 6722 | \list |
| 6723 | \li \c{T} is \c{const QString}: copy-convert |
| 6724 | \li \c{T} is \c{QString} and its refcount != 1: copy-convert |
| 6725 | \li \c{T} is \c{QString} and its refcount == 1: in-place convert |
| 6726 | \endlist |
| 6727 | |
| 6728 | In copy-convert mode, the local variable \c{s} is detached from the input |
| 6729 | \a str. In the in-place convert mode, \a str is in moved-from state (which |
| 6730 | this function requires to be a valid, empty string) and \c{s} contains the |
| 6731 | only copy of the string, without reallocation (thus, \a it is still valid). |
| 6732 | |
| 6733 | There is one pathological case left: when the in-place conversion needs to |
| 6734 | reallocate memory to grow the buffer. In that case, we need to adjust the \a |
| 6735 | it pointer. |
| 6736 | */ |
| 6737 | template <typename T> |
| 6738 | Q_NEVER_INLINE |
| 6739 | static QString detachAndConvertCase(T &str, QStringIterator it, QUnicodeTables::Case which) |
| 6740 | { |
| 6741 | Q_ASSERT(!str.isEmpty()); |
| 6742 | QString s = std::move(str); // will copy if T is const QString |
| 6743 | QChar *pp = s.begin() + it.index(); // will detach if necessary |
| 6744 | |
| 6745 | do { |
| 6746 | uint uc = it.nextUnchecked(); |
| 6747 | |
| 6748 | const auto fold = qGetProp(ucs4: uc)->cases[which]; |
| 6749 | signed short caseDiff = fold.diff; |
| 6750 | |
| 6751 | if (Q_UNLIKELY(fold.special)) { |
| 6752 | const ushort *specialCase = specialCaseMap + caseDiff; |
| 6753 | ushort length = *specialCase++; |
| 6754 | |
| 6755 | if (Q_LIKELY(length == 1)) { |
| 6756 | *pp++ = QChar(*specialCase); |
| 6757 | } else { |
| 6758 | // slow path: the string is growing |
| 6759 | int inpos = it.index() - 1; |
| 6760 | int outpos = pp - s.constBegin(); |
| 6761 | |
| 6762 | s.replace(pos: outpos, len: 1, unicode: reinterpret_cast<const QChar *>(specialCase), size: length); |
| 6763 | pp = const_cast<QChar *>(s.constBegin()) + outpos + length; |
| 6764 | |
| 6765 | // do we need to adjust the input iterator too? |
| 6766 | // if it is pointing to s's data, str is empty |
| 6767 | if (str.isEmpty()) |
| 6768 | it = QStringIterator(s.constBegin(), inpos + length, s.constEnd()); |
| 6769 | } |
| 6770 | } else if (Q_UNLIKELY(QChar::requiresSurrogates(uc))) { |
| 6771 | // so far, case convertion never changes planes (guaranteed by the qunicodetables generator) |
| 6772 | pp++; |
| 6773 | *pp++ = QChar(QChar::lowSurrogate(ucs4: uc + caseDiff)); |
| 6774 | } else { |
| 6775 | *pp++ = QChar(uc + caseDiff); |
| 6776 | } |
| 6777 | } while (it.hasNext()); |
| 6778 | |
| 6779 | return s; |
| 6780 | } |
| 6781 | |
| 6782 | template <typename T> |
| 6783 | static QString convertCase(T &str, QUnicodeTables::Case which) |
| 6784 | { |
| 6785 | const QChar *p = str.constBegin(); |
| 6786 | const QChar *e = p + str.size(); |
| 6787 | |
| 6788 | // this avoids out of bounds check in the loop |
| 6789 | while (e != p && e[-1].isHighSurrogate()) |
| 6790 | --e; |
| 6791 | |
| 6792 | QStringIterator it(p, e); |
| 6793 | while (it.hasNext()) { |
| 6794 | uint uc = it.nextUnchecked(); |
| 6795 | if (qGetProp(ucs4: uc)->cases[which].diff) { |
| 6796 | it.recedeUnchecked(); |
| 6797 | return detachAndConvertCase(str, it, which); |
| 6798 | } |
| 6799 | } |
| 6800 | return std::move(str); |
| 6801 | } |
| 6802 | } // namespace QUnicodeTables |
| 6803 | |
| 6804 | QString QString::toLower_helper(const QString &str) |
| 6805 | { |
| 6806 | return QUnicodeTables::convertCase(str, which: QUnicodeTables::LowerCase); |
| 6807 | } |
| 6808 | |
| 6809 | QString QString::toLower_helper(QString &str) |
| 6810 | { |
| 6811 | return QUnicodeTables::convertCase(str, which: QUnicodeTables::LowerCase); |
| 6812 | } |
| 6813 | |
| 6814 | /*! |
| 6815 | \fn QString QString::toCaseFolded() const |
| 6816 | |
| 6817 | Returns the case folded equivalent of the string. For most Unicode |
| 6818 | characters this is the same as toLower(). |
| 6819 | */ |
| 6820 | |
| 6821 | QString QString::toCaseFolded_helper(const QString &str) |
| 6822 | { |
| 6823 | return QUnicodeTables::convertCase(str, which: QUnicodeTables::CaseFold); |
| 6824 | } |
| 6825 | |
| 6826 | QString QString::toCaseFolded_helper(QString &str) |
| 6827 | { |
| 6828 | return QUnicodeTables::convertCase(str, which: QUnicodeTables::CaseFold); |
| 6829 | } |
| 6830 | |
| 6831 | /*! |
| 6832 | \fn QString QString::toUpper() const |
| 6833 | |
| 6834 | Returns an uppercase copy of the string. |
| 6835 | |
| 6836 | \snippet qstring/main.cpp 81 |
| 6837 | |
| 6838 | The case conversion will always happen in the 'C' locale. For locale dependent |
| 6839 | case folding use QLocale::toUpper() |
| 6840 | |
| 6841 | \sa toLower(), QLocale::toLower() |
| 6842 | */ |
| 6843 | |
| 6844 | QString QString::toUpper_helper(const QString &str) |
| 6845 | { |
| 6846 | return QUnicodeTables::convertCase(str, which: QUnicodeTables::UpperCase); |
| 6847 | } |
| 6848 | |
| 6849 | QString QString::toUpper_helper(QString &str) |
| 6850 | { |
| 6851 | return QUnicodeTables::convertCase(str, which: QUnicodeTables::UpperCase); |
| 6852 | } |
| 6853 | |
| 6854 | #if QT_DEPRECATED_SINCE(5, 14) |
| 6855 | /*! |
| 6856 | \obsolete |
| 6857 | |
| 6858 | Use asprintf(), arg() or QTextStream instead. |
| 6859 | */ |
| 6860 | QString &QString::sprintf(const char *cformat, ...) |
| 6861 | { |
| 6862 | va_list ap; |
| 6863 | va_start(ap, cformat); |
| 6864 | *this = vasprintf(format: cformat, ap); |
| 6865 | va_end(ap); |
| 6866 | return *this; |
| 6867 | } |
| 6868 | #endif |
| 6869 | |
| 6870 | // ### Qt 6: Consider whether this function shouldn't be removed See task 202871. |
| 6871 | /*! |
| 6872 | \since 5.5 |
| 6873 | |
| 6874 | Safely builds a formatted string from the format string \a cformat |
| 6875 | and an arbitrary list of arguments. |
| 6876 | |
| 6877 | The format string supports the conversion specifiers, length modifiers, |
| 6878 | and flags provided by printf() in the standard C++ library. The \a cformat |
| 6879 | string and \c{%s} arguments must be UTF-8 encoded. |
| 6880 | |
| 6881 | \note The \c{%lc} escape sequence expects a unicode character of type |
| 6882 | \c char16_t, or \c ushort (as returned by QChar::unicode()). |
| 6883 | The \c{%ls} escape sequence expects a pointer to a zero-terminated array |
| 6884 | of unicode characters of type \c char16_t, or ushort (as returned by |
| 6885 | QString::utf16()). This is at odds with the printf() in the standard C++ |
| 6886 | library, which defines \c {%lc} to print a wchar_t and \c{%ls} to print |
| 6887 | a \c{wchar_t*}, and might also produce compiler warnings on platforms |
| 6888 | where the size of \c {wchar_t} is not 16 bits. |
| 6889 | |
| 6890 | \warning We do not recommend using QString::asprintf() in new Qt |
| 6891 | code. Instead, consider using QTextStream or arg(), both of |
| 6892 | which support Unicode strings seamlessly and are type-safe. |
| 6893 | Here is an example that uses QTextStream: |
| 6894 | |
| 6895 | \snippet qstring/main.cpp 64 |
| 6896 | |
| 6897 | For \l {QObject::tr()}{translations}, especially if the strings |
| 6898 | contains more than one escape sequence, you should consider using |
| 6899 | the arg() function instead. This allows the order of the |
| 6900 | replacements to be controlled by the translator. |
| 6901 | |
| 6902 | \sa arg() |
| 6903 | */ |
| 6904 | |
| 6905 | QString QString::asprintf(const char *cformat, ...) |
| 6906 | { |
| 6907 | va_list ap; |
| 6908 | va_start(ap, cformat); |
| 6909 | const QString s = vasprintf(format: cformat, ap); |
| 6910 | va_end(ap); |
| 6911 | return s; |
| 6912 | } |
| 6913 | |
| 6914 | #if QT_DEPRECATED_SINCE(5, 14) |
| 6915 | /*! |
| 6916 | \obsolete |
| 6917 | |
| 6918 | Use vasprintf(), arg() or QTextStream instead. |
| 6919 | */ |
| 6920 | QString &QString::vsprintf(const char *cformat, va_list ap) |
| 6921 | { |
| 6922 | return *this = vasprintf(format: cformat, ap); |
| 6923 | } |
| 6924 | #endif |
| 6925 | |
| 6926 | static void append_utf8(QString &qs, const char *cs, int len) |
| 6927 | { |
| 6928 | const int oldSize = qs.size(); |
| 6929 | qs.resize(size: oldSize + len); |
| 6930 | const QChar *newEnd = QUtf8::convertToUnicode(qs.data() + oldSize, cs, len); |
| 6931 | qs.resize(size: newEnd - qs.constData()); |
| 6932 | } |
| 6933 | |
| 6934 | static uint parse_flag_characters(const char * &c) noexcept |
| 6935 | { |
| 6936 | uint flags = QLocaleData::ZeroPadExponent; |
| 6937 | while (true) { |
| 6938 | switch (*c) { |
| 6939 | case '#': |
| 6940 | flags |= QLocaleData::ShowBase | QLocaleData::AddTrailingZeroes |
| 6941 | | QLocaleData::ForcePoint; |
| 6942 | break; |
| 6943 | case '0': flags |= QLocaleData::ZeroPadded; break; |
| 6944 | case '-': flags |= QLocaleData::LeftAdjusted; break; |
| 6945 | case ' ': flags |= QLocaleData::BlankBeforePositive; break; |
| 6946 | case '+': flags |= QLocaleData::AlwaysShowSign; break; |
| 6947 | case '\'': flags |= QLocaleData::ThousandsGroup; break; |
| 6948 | default: return flags; |
| 6949 | } |
| 6950 | ++c; |
| 6951 | } |
| 6952 | } |
| 6953 | |
| 6954 | static int parse_field_width(const char * &c) |
| 6955 | { |
| 6956 | Q_ASSERT(qIsDigit(*c)); |
| 6957 | |
| 6958 | // can't be negative - started with a digit |
| 6959 | // contains at least one digit |
| 6960 | const char *endp; |
| 6961 | bool ok; |
| 6962 | const qulonglong result = qstrtoull(nptr: c, endptr: &endp, base: 10, ok: &ok); |
| 6963 | c = endp; |
| 6964 | while (qIsDigit(ch: *c)) // preserve Qt 5.5 behavior of consuming all digits, no matter how many |
| 6965 | ++c; |
| 6966 | return ok && result < qulonglong(std::numeric_limits<int>::max()) ? int(result) : 0; |
| 6967 | } |
| 6968 | |
| 6969 | enum LengthMod { lm_none, lm_hh, lm_h, lm_l, lm_ll, lm_L, lm_j, lm_z, lm_t }; |
| 6970 | |
| 6971 | static inline bool can_consume(const char * &c, char ch) noexcept |
| 6972 | { |
| 6973 | if (*c == ch) { |
| 6974 | ++c; |
| 6975 | return true; |
| 6976 | } |
| 6977 | return false; |
| 6978 | } |
| 6979 | |
| 6980 | static LengthMod parse_length_modifier(const char * &c) noexcept |
| 6981 | { |
| 6982 | switch (*c++) { |
| 6983 | case 'h': return can_consume(c, ch: 'h') ? lm_hh : lm_h; |
| 6984 | case 'l': return can_consume(c, ch: 'l') ? lm_ll : lm_l; |
| 6985 | case 'L': return lm_L; |
| 6986 | case 'j': return lm_j; |
| 6987 | case 'z': |
| 6988 | case 'Z': return lm_z; |
| 6989 | case 't': return lm_t; |
| 6990 | } |
| 6991 | --c; // don't consume *c - it wasn't a flag |
| 6992 | return lm_none; |
| 6993 | } |
| 6994 | |
| 6995 | /*! |
| 6996 | \fn QString QString::vasprintf(const char *cformat, va_list ap) |
| 6997 | \since 5.5 |
| 6998 | |
| 6999 | Equivalent method to asprintf(), but takes a va_list \a ap |
| 7000 | instead a list of variable arguments. See the asprintf() |
| 7001 | documentation for an explanation of \a cformat. |
| 7002 | |
| 7003 | This method does not call the va_end macro, the caller |
| 7004 | is responsible to call va_end on \a ap. |
| 7005 | |
| 7006 | \sa asprintf() |
| 7007 | */ |
| 7008 | |
| 7009 | QString QString::vasprintf(const char *cformat, va_list ap) |
| 7010 | { |
| 7011 | if (!cformat || !*cformat) { |
| 7012 | // Qt 1.x compat |
| 7013 | return fromLatin1(str: "" ); |
| 7014 | } |
| 7015 | |
| 7016 | // Parse cformat |
| 7017 | |
| 7018 | QString result; |
| 7019 | const char *c = cformat; |
| 7020 | for (;;) { |
| 7021 | // Copy non-escape chars to result |
| 7022 | const char *cb = c; |
| 7023 | while (*c != '\0' && *c != '%') |
| 7024 | c++; |
| 7025 | append_utf8(qs&: result, cs: cb, len: int(c - cb)); |
| 7026 | |
| 7027 | if (*c == '\0') |
| 7028 | break; |
| 7029 | |
| 7030 | // Found '%' |
| 7031 | const char *escape_start = c; |
| 7032 | ++c; |
| 7033 | |
| 7034 | if (*c == '\0') { |
| 7035 | result.append(ch: QLatin1Char('%')); // a % at the end of the string - treat as non-escape text |
| 7036 | break; |
| 7037 | } |
| 7038 | if (*c == '%') { |
| 7039 | result.append(ch: QLatin1Char('%')); // %% |
| 7040 | ++c; |
| 7041 | continue; |
| 7042 | } |
| 7043 | |
| 7044 | uint flags = parse_flag_characters(c); |
| 7045 | |
| 7046 | if (*c == '\0') { |
| 7047 | result.append(str: QLatin1String(escape_start)); // incomplete escape, treat as non-escape text |
| 7048 | break; |
| 7049 | } |
| 7050 | |
| 7051 | // Parse field width |
| 7052 | int width = -1; // -1 means unspecified |
| 7053 | if (qIsDigit(ch: *c)) { |
| 7054 | width = parse_field_width(c); |
| 7055 | } else if (*c == '*') { // can't parse this in another function, not portably, at least |
| 7056 | width = va_arg(ap, int); |
| 7057 | if (width < 0) |
| 7058 | width = -1; // treat all negative numbers as unspecified |
| 7059 | ++c; |
| 7060 | } |
| 7061 | |
| 7062 | if (*c == '\0') { |
| 7063 | result.append(str: QLatin1String(escape_start)); // incomplete escape, treat as non-escape text |
| 7064 | break; |
| 7065 | } |
| 7066 | |
| 7067 | // Parse precision |
| 7068 | int precision = -1; // -1 means unspecified |
| 7069 | if (*c == '.') { |
| 7070 | ++c; |
| 7071 | if (qIsDigit(ch: *c)) { |
| 7072 | precision = parse_field_width(c); |
| 7073 | } else if (*c == '*') { // can't parse this in another function, not portably, at least |
| 7074 | precision = va_arg(ap, int); |
| 7075 | if (precision < 0) |
| 7076 | precision = -1; // treat all negative numbers as unspecified |
| 7077 | ++c; |
| 7078 | } |
| 7079 | } |
| 7080 | |
| 7081 | if (*c == '\0') { |
| 7082 | result.append(str: QLatin1String(escape_start)); // incomplete escape, treat as non-escape text |
| 7083 | break; |
| 7084 | } |
| 7085 | |
| 7086 | const LengthMod length_mod = parse_length_modifier(c); |
| 7087 | |
| 7088 | if (*c == '\0') { |
| 7089 | result.append(str: QLatin1String(escape_start)); // incomplete escape, treat as non-escape text |
| 7090 | break; |
| 7091 | } |
| 7092 | |
| 7093 | // Parse the conversion specifier and do the conversion |
| 7094 | QString subst; |
| 7095 | switch (*c) { |
| 7096 | case 'd': |
| 7097 | case 'i': { |
| 7098 | qint64 i; |
| 7099 | switch (length_mod) { |
| 7100 | case lm_none: i = va_arg(ap, int); break; |
| 7101 | case lm_hh: i = va_arg(ap, int); break; |
| 7102 | case lm_h: i = va_arg(ap, int); break; |
| 7103 | case lm_l: i = va_arg(ap, long int); break; |
| 7104 | case lm_ll: i = va_arg(ap, qint64); break; |
| 7105 | case lm_j: i = va_arg(ap, long int); break; |
| 7106 | case lm_z: i = va_arg(ap, size_t); break; |
| 7107 | case lm_t: i = va_arg(ap, int); break; |
| 7108 | default: i = 0; break; |
| 7109 | } |
| 7110 | subst = QLocaleData::c()->longLongToString(l: i, precision, base: 10, width, flags); |
| 7111 | ++c; |
| 7112 | break; |
| 7113 | } |
| 7114 | case 'o': |
| 7115 | case 'u': |
| 7116 | case 'x': |
| 7117 | case 'X': { |
| 7118 | quint64 u; |
| 7119 | switch (length_mod) { |
| 7120 | case lm_none: u = va_arg(ap, uint); break; |
| 7121 | case lm_hh: u = va_arg(ap, uint); break; |
| 7122 | case lm_h: u = va_arg(ap, uint); break; |
| 7123 | case lm_l: u = va_arg(ap, ulong); break; |
| 7124 | case lm_ll: u = va_arg(ap, quint64); break; |
| 7125 | case lm_z: u = va_arg(ap, size_t); break; |
| 7126 | default: u = 0; break; |
| 7127 | } |
| 7128 | |
| 7129 | if (qIsUpper(ch: *c)) |
| 7130 | flags |= QLocaleData::CapitalEorX; |
| 7131 | |
| 7132 | int base = 10; |
| 7133 | switch (qToLower(ch: *c)) { |
| 7134 | case 'o': |
| 7135 | base = 8; break; |
| 7136 | case 'u': |
| 7137 | base = 10; break; |
| 7138 | case 'x': |
| 7139 | base = 16; break; |
| 7140 | default: break; |
| 7141 | } |
| 7142 | subst = QLocaleData::c()->unsLongLongToString(l: u, precision, base, width, flags); |
| 7143 | ++c; |
| 7144 | break; |
| 7145 | } |
| 7146 | case 'E': |
| 7147 | case 'e': |
| 7148 | case 'F': |
| 7149 | case 'f': |
| 7150 | case 'G': |
| 7151 | case 'g': |
| 7152 | case 'A': |
| 7153 | case 'a': { |
| 7154 | double d; |
| 7155 | if (length_mod == lm_L) |
| 7156 | d = va_arg(ap, long double); // not supported - converted to a double |
| 7157 | else |
| 7158 | d = va_arg(ap, double); |
| 7159 | |
| 7160 | if (qIsUpper(ch: *c)) |
| 7161 | flags |= QLocaleData::CapitalEorX; |
| 7162 | |
| 7163 | QLocaleData::DoubleForm form = QLocaleData::DFDecimal; |
| 7164 | switch (qToLower(ch: *c)) { |
| 7165 | case 'e': form = QLocaleData::DFExponent; break; |
| 7166 | case 'a': // not supported - decimal form used instead |
| 7167 | case 'f': form = QLocaleData::DFDecimal; break; |
| 7168 | case 'g': form = QLocaleData::DFSignificantDigits; break; |
| 7169 | default: break; |
| 7170 | } |
| 7171 | subst = QLocaleData::c()->doubleToString(d, precision, form, width, flags); |
| 7172 | ++c; |
| 7173 | break; |
| 7174 | } |
| 7175 | case 'c': { |
| 7176 | if (length_mod == lm_l) |
| 7177 | subst = QChar((ushort) va_arg(ap, int)); |
| 7178 | else |
| 7179 | subst = QLatin1Char((uchar) va_arg(ap, int)); |
| 7180 | ++c; |
| 7181 | break; |
| 7182 | } |
| 7183 | case 's': { |
| 7184 | if (length_mod == lm_l) { |
| 7185 | const ushort *buff = va_arg(ap, const ushort*); |
| 7186 | const ushort *ch = buff; |
| 7187 | while (precision != 0 && *ch != 0) { |
| 7188 | ++ch; |
| 7189 | --precision; |
| 7190 | } |
| 7191 | subst.setUtf16(autf16: buff, asize: ch - buff); |
| 7192 | } else if (precision == -1) { |
| 7193 | subst = QString::fromUtf8(va_arg(ap, const char*)); |
| 7194 | } else { |
| 7195 | const char *buff = va_arg(ap, const char*); |
| 7196 | subst = QString::fromUtf8(str: buff, size: qstrnlen(str: buff, maxlen: precision)); |
| 7197 | } |
| 7198 | ++c; |
| 7199 | break; |
| 7200 | } |
| 7201 | case 'p': { |
| 7202 | void *arg = va_arg(ap, void*); |
| 7203 | const quint64 i = reinterpret_cast<quintptr>(arg); |
| 7204 | flags |= QLocaleData::ShowBase; |
| 7205 | subst = QLocaleData::c()->unsLongLongToString(l: i, precision, base: 16, width, flags); |
| 7206 | ++c; |
| 7207 | break; |
| 7208 | } |
| 7209 | case 'n': |
| 7210 | switch (length_mod) { |
| 7211 | case lm_hh: { |
| 7212 | signed char *n = va_arg(ap, signed char*); |
| 7213 | *n = result.length(); |
| 7214 | break; |
| 7215 | } |
| 7216 | case lm_h: { |
| 7217 | short int *n = va_arg(ap, short int*); |
| 7218 | *n = result.length(); |
| 7219 | break; |
| 7220 | } |
| 7221 | case lm_l: { |
| 7222 | long int *n = va_arg(ap, long int*); |
| 7223 | *n = result.length(); |
| 7224 | break; |
| 7225 | } |
| 7226 | case lm_ll: { |
| 7227 | qint64 *n = va_arg(ap, qint64*); |
| 7228 | *n = result.length(); |
| 7229 | break; |
| 7230 | } |
| 7231 | default: { |
| 7232 | int *n = va_arg(ap, int*); |
| 7233 | *n = result.length(); |
| 7234 | break; |
| 7235 | } |
| 7236 | } |
| 7237 | ++c; |
| 7238 | break; |
| 7239 | |
| 7240 | default: // bad escape, treat as non-escape text |
| 7241 | for (const char *cc = escape_start; cc != c; ++cc) |
| 7242 | result.append(ch: QLatin1Char(*cc)); |
| 7243 | continue; |
| 7244 | } |
| 7245 | |
| 7246 | if (flags & QLocaleData::LeftAdjusted) |
| 7247 | result.append(str: subst.leftJustified(width)); |
| 7248 | else |
| 7249 | result.append(str: subst.rightJustified(width)); |
| 7250 | } |
| 7251 | |
| 7252 | return result; |
| 7253 | } |
| 7254 | |
| 7255 | /*! |
| 7256 | Returns the string converted to a \c{long long} using base \a |
| 7257 | base, which is 10 by default and must be between 2 and 36, or 0. |
| 7258 | Returns 0 if the conversion fails. |
| 7259 | |
| 7260 | If \a ok is not \nullptr, failure is reported by setting *\a{ok} |
| 7261 | to \c false, and success by setting *\a{ok} to \c true. |
| 7262 | |
| 7263 | If \a base is 0, the C language convention is used: If the string |
| 7264 | begins with "0x", base 16 is used; if the string begins with "0", |
| 7265 | base 8 is used; otherwise, base 10 is used. |
| 7266 | |
| 7267 | The string conversion will always happen in the 'C' locale. For locale |
| 7268 | dependent conversion use QLocale::toLongLong() |
| 7269 | |
| 7270 | Example: |
| 7271 | |
| 7272 | \snippet qstring/main.cpp 74 |
| 7273 | |
| 7274 | This function ignores leading and trailing whitespace. |
| 7275 | |
| 7276 | \sa number(), toULongLong(), toInt(), QLocale::toLongLong() |
| 7277 | */ |
| 7278 | |
| 7279 | qint64 QString::toLongLong(bool *ok, int base) const |
| 7280 | { |
| 7281 | return toIntegral_helper<qlonglong>(data: constData(), len: size(), ok, base); |
| 7282 | } |
| 7283 | |
| 7284 | qlonglong QString::toIntegral_helper(const QChar *data, int len, bool *ok, int base) |
| 7285 | { |
| 7286 | #if defined(QT_CHECK_RANGE) |
| 7287 | if (base != 0 && (base < 2 || base > 36)) { |
| 7288 | qWarning("QString::toULongLong: Invalid base (%d)" , base); |
| 7289 | base = 10; |
| 7290 | } |
| 7291 | #endif |
| 7292 | |
| 7293 | return QLocaleData::c()->stringToLongLong(str: QStringView(data, len), base, ok, options: QLocale::RejectGroupSeparator); |
| 7294 | } |
| 7295 | |
| 7296 | |
| 7297 | /*! |
| 7298 | Returns the string converted to an \c{unsigned long long} using base \a |
| 7299 | base, which is 10 by default and must be between 2 and 36, or 0. |
| 7300 | Returns 0 if the conversion fails. |
| 7301 | |
| 7302 | If \a ok is not \nullptr, failure is reported by setting *\a{ok} |
| 7303 | to \c false, and success by setting *\a{ok} to \c true. |
| 7304 | |
| 7305 | If \a base is 0, the C language convention is used: If the string |
| 7306 | begins with "0x", base 16 is used; if the string begins with "0", |
| 7307 | base 8 is used; otherwise, base 10 is used. |
| 7308 | |
| 7309 | The string conversion will always happen in the 'C' locale. For locale |
| 7310 | dependent conversion use QLocale::toULongLong() |
| 7311 | |
| 7312 | Example: |
| 7313 | |
| 7314 | \snippet qstring/main.cpp 79 |
| 7315 | |
| 7316 | This function ignores leading and trailing whitespace. |
| 7317 | |
| 7318 | \sa number(), toLongLong(), QLocale::toULongLong() |
| 7319 | */ |
| 7320 | |
| 7321 | quint64 QString::toULongLong(bool *ok, int base) const |
| 7322 | { |
| 7323 | return toIntegral_helper<qulonglong>(data: constData(), len: size(), ok, base); |
| 7324 | } |
| 7325 | |
| 7326 | qulonglong QString::toIntegral_helper(const QChar *data, uint len, bool *ok, int base) |
| 7327 | { |
| 7328 | #if defined(QT_CHECK_RANGE) |
| 7329 | if (base != 0 && (base < 2 || base > 36)) { |
| 7330 | qWarning("QString::toULongLong: Invalid base (%d)" , base); |
| 7331 | base = 10; |
| 7332 | } |
| 7333 | #endif |
| 7334 | |
| 7335 | return QLocaleData::c()->stringToUnsLongLong(str: QStringView(data, len), base, ok, |
| 7336 | options: QLocale::RejectGroupSeparator); |
| 7337 | } |
| 7338 | |
| 7339 | /*! |
| 7340 | \fn long QString::toLong(bool *ok, int base) const |
| 7341 | |
| 7342 | Returns the string converted to a \c long using base \a |
| 7343 | base, which is 10 by default and must be between 2 and 36, or 0. |
| 7344 | Returns 0 if the conversion fails. |
| 7345 | |
| 7346 | If \a ok is not \nullptr, failure is reported by setting *\a{ok} |
| 7347 | to \c false, and success by setting *\a{ok} to \c true. |
| 7348 | |
| 7349 | If \a base is 0, the C language convention is used: If the string |
| 7350 | begins with "0x", base 16 is used; if the string begins with "0", |
| 7351 | base 8 is used; otherwise, base 10 is used. |
| 7352 | |
| 7353 | The string conversion will always happen in the 'C' locale. For locale |
| 7354 | dependent conversion use QLocale::toLongLong() |
| 7355 | |
| 7356 | Example: |
| 7357 | |
| 7358 | \snippet qstring/main.cpp 73 |
| 7359 | |
| 7360 | This function ignores leading and trailing whitespace. |
| 7361 | |
| 7362 | \sa number(), toULong(), toInt(), QLocale::toInt() |
| 7363 | */ |
| 7364 | |
| 7365 | long QString::toLong(bool *ok, int base) const |
| 7366 | { |
| 7367 | return toIntegral_helper<long>(data: constData(), len: size(), ok, base); |
| 7368 | } |
| 7369 | |
| 7370 | /*! |
| 7371 | \fn ulong QString::toULong(bool *ok, int base) const |
| 7372 | |
| 7373 | Returns the string converted to an \c{unsigned long} using base \a |
| 7374 | base, which is 10 by default and must be between 2 and 36, or 0. |
| 7375 | Returns 0 if the conversion fails. |
| 7376 | |
| 7377 | If \a ok is not \nullptr, failure is reported by setting *\a{ok} |
| 7378 | to \c false, and success by setting *\a{ok} to \c true. |
| 7379 | |
| 7380 | If \a base is 0, the C language convention is used: If the string |
| 7381 | begins with "0x", base 16 is used; if the string begins with "0", |
| 7382 | base 8 is used; otherwise, base 10 is used. |
| 7383 | |
| 7384 | The string conversion will always happen in the 'C' locale. For locale |
| 7385 | dependent conversion use QLocale::toULongLong() |
| 7386 | |
| 7387 | Example: |
| 7388 | |
| 7389 | \snippet qstring/main.cpp 78 |
| 7390 | |
| 7391 | This function ignores leading and trailing whitespace. |
| 7392 | |
| 7393 | \sa number(), QLocale::toUInt() |
| 7394 | */ |
| 7395 | |
| 7396 | ulong QString::toULong(bool *ok, int base) const |
| 7397 | { |
| 7398 | return toIntegral_helper<ulong>(data: constData(), len: size(), ok, base); |
| 7399 | } |
| 7400 | |
| 7401 | |
| 7402 | /*! |
| 7403 | Returns the string converted to an \c int using base \a |
| 7404 | base, which is 10 by default and must be between 2 and 36, or 0. |
| 7405 | Returns 0 if the conversion fails. |
| 7406 | |
| 7407 | If \a ok is not \nullptr, failure is reported by setting *\a{ok} |
| 7408 | to \c false, and success by setting *\a{ok} to \c true. |
| 7409 | |
| 7410 | If \a base is 0, the C language convention is used: If the string |
| 7411 | begins with "0x", base 16 is used; if the string begins with "0", |
| 7412 | base 8 is used; otherwise, base 10 is used. |
| 7413 | |
| 7414 | The string conversion will always happen in the 'C' locale. For locale |
| 7415 | dependent conversion use QLocale::toInt() |
| 7416 | |
| 7417 | Example: |
| 7418 | |
| 7419 | \snippet qstring/main.cpp 72 |
| 7420 | |
| 7421 | This function ignores leading and trailing whitespace. |
| 7422 | |
| 7423 | \sa number(), toUInt(), toDouble(), QLocale::toInt() |
| 7424 | */ |
| 7425 | |
| 7426 | int QString::toInt(bool *ok, int base) const |
| 7427 | { |
| 7428 | return toIntegral_helper<int>(data: constData(), len: size(), ok, base); |
| 7429 | } |
| 7430 | |
| 7431 | /*! |
| 7432 | Returns the string converted to an \c{unsigned int} using base \a |
| 7433 | base, which is 10 by default and must be between 2 and 36, or 0. |
| 7434 | Returns 0 if the conversion fails. |
| 7435 | |
| 7436 | If \a ok is not \nullptr, failure is reported by setting *\a{ok} |
| 7437 | to \c false, and success by setting *\a{ok} to \c true. |
| 7438 | |
| 7439 | If \a base is 0, the C language convention is used: If the string |
| 7440 | begins with "0x", base 16 is used; if the string begins with "0", |
| 7441 | base 8 is used; otherwise, base 10 is used. |
| 7442 | |
| 7443 | The string conversion will always happen in the 'C' locale. For locale |
| 7444 | dependent conversion use QLocale::toUInt() |
| 7445 | |
| 7446 | Example: |
| 7447 | |
| 7448 | \snippet qstring/main.cpp 77 |
| 7449 | |
| 7450 | This function ignores leading and trailing whitespace. |
| 7451 | |
| 7452 | \sa number(), toInt(), QLocale::toUInt() |
| 7453 | */ |
| 7454 | |
| 7455 | uint QString::toUInt(bool *ok, int base) const |
| 7456 | { |
| 7457 | return toIntegral_helper<uint>(data: constData(), len: size(), ok, base); |
| 7458 | } |
| 7459 | |
| 7460 | /*! |
| 7461 | Returns the string converted to a \c short using base \a |
| 7462 | base, which is 10 by default and must be between 2 and 36, or 0. |
| 7463 | Returns 0 if the conversion fails. |
| 7464 | |
| 7465 | If \a ok is not \nullptr, failure is reported by setting *\a{ok} |
| 7466 | to \c false, and success by setting *\a{ok} to \c true. |
| 7467 | |
| 7468 | If \a base is 0, the C language convention is used: If the string |
| 7469 | begins with "0x", base 16 is used; if the string begins with "0", |
| 7470 | base 8 is used; otherwise, base 10 is used. |
| 7471 | |
| 7472 | The string conversion will always happen in the 'C' locale. For locale |
| 7473 | dependent conversion use QLocale::toShort() |
| 7474 | |
| 7475 | Example: |
| 7476 | |
| 7477 | \snippet qstring/main.cpp 76 |
| 7478 | |
| 7479 | This function ignores leading and trailing whitespace. |
| 7480 | |
| 7481 | \sa number(), toUShort(), toInt(), QLocale::toShort() |
| 7482 | */ |
| 7483 | |
| 7484 | short QString::toShort(bool *ok, int base) const |
| 7485 | { |
| 7486 | return toIntegral_helper<short>(data: constData(), len: size(), ok, base); |
| 7487 | } |
| 7488 | |
| 7489 | /*! |
| 7490 | Returns the string converted to an \c{unsigned short} using base \a |
| 7491 | base, which is 10 by default and must be between 2 and 36, or 0. |
| 7492 | Returns 0 if the conversion fails. |
| 7493 | |
| 7494 | If \a ok is not \nullptr, failure is reported by setting *\a{ok} |
| 7495 | to \c false, and success by setting *\a{ok} to \c true. |
| 7496 | |
| 7497 | If \a base is 0, the C language convention is used: If the string |
| 7498 | begins with "0x", base 16 is used; if the string begins with "0", |
| 7499 | base 8 is used; otherwise, base 10 is used. |
| 7500 | |
| 7501 | The string conversion will always happen in the 'C' locale. For locale |
| 7502 | dependent conversion use QLocale::toUShort() |
| 7503 | |
| 7504 | Example: |
| 7505 | |
| 7506 | \snippet qstring/main.cpp 80 |
| 7507 | |
| 7508 | This function ignores leading and trailing whitespace. |
| 7509 | |
| 7510 | \sa number(), toShort(), QLocale::toUShort() |
| 7511 | */ |
| 7512 | |
| 7513 | ushort QString::toUShort(bool *ok, int base) const |
| 7514 | { |
| 7515 | return toIntegral_helper<ushort>(data: constData(), len: size(), ok, base); |
| 7516 | } |
| 7517 | |
| 7518 | |
| 7519 | /*! |
| 7520 | Returns the string converted to a \c double value. |
| 7521 | |
| 7522 | Returns an infinity if the conversion overflows or 0.0 if the |
| 7523 | conversion fails for other reasons (e.g. underflow). |
| 7524 | |
| 7525 | If \a ok is not \nullptr, failure is reported by setting *\a{ok} |
| 7526 | to \c false, and success by setting *\a{ok} to \c true. |
| 7527 | |
| 7528 | \snippet qstring/main.cpp 66 |
| 7529 | |
| 7530 | \warning The QString content may only contain valid numerical characters |
| 7531 | which includes the plus/minus sign, the character e used in scientific |
| 7532 | notation, and the decimal point. Including the unit or additional characters |
| 7533 | leads to a conversion error. |
| 7534 | |
| 7535 | \snippet qstring/main.cpp 67 |
| 7536 | |
| 7537 | The string conversion will always happen in the 'C' locale. For locale |
| 7538 | dependent conversion use QLocale::toDouble() |
| 7539 | |
| 7540 | \snippet qstring/main.cpp 68 |
| 7541 | |
| 7542 | For historical reasons, this function does not handle |
| 7543 | thousands group separators. If you need to convert such numbers, |
| 7544 | use QLocale::toDouble(). |
| 7545 | |
| 7546 | \snippet qstring/main.cpp 69 |
| 7547 | |
| 7548 | This function ignores leading and trailing whitespace. |
| 7549 | |
| 7550 | \sa number(), QLocale::setDefault(), QLocale::toDouble(), trimmed() |
| 7551 | */ |
| 7552 | |
| 7553 | double QString::toDouble(bool *ok) const |
| 7554 | { |
| 7555 | return QLocaleData::c()->stringToDouble(str: *this, ok, options: QLocale::RejectGroupSeparator); |
| 7556 | } |
| 7557 | |
| 7558 | /*! |
| 7559 | Returns the string converted to a \c float value. |
| 7560 | |
| 7561 | Returns an infinity if the conversion overflows or 0.0 if the |
| 7562 | conversion fails for other reasons (e.g. underflow). |
| 7563 | |
| 7564 | If \a ok is not \nullptr, failure is reported by setting *\a{ok} |
| 7565 | to \c false, and success by setting *\a{ok} to \c true. |
| 7566 | |
| 7567 | \warning The QString content may only contain valid numerical characters |
| 7568 | which includes the plus/minus sign, the character e used in scientific |
| 7569 | notation, and the decimal point. Including the unit or additional characters |
| 7570 | leads to a conversion error. |
| 7571 | |
| 7572 | The string conversion will always happen in the 'C' locale. For locale |
| 7573 | dependent conversion use QLocale::toFloat() |
| 7574 | |
| 7575 | For historical reasons, this function does not handle |
| 7576 | thousands group separators. If you need to convert such numbers, |
| 7577 | use QLocale::toFloat(). |
| 7578 | |
| 7579 | Example: |
| 7580 | |
| 7581 | \snippet qstring/main.cpp 71 |
| 7582 | |
| 7583 | This function ignores leading and trailing whitespace. |
| 7584 | |
| 7585 | \sa number(), toDouble(), toInt(), QLocale::toFloat(), trimmed() |
| 7586 | */ |
| 7587 | |
| 7588 | float QString::toFloat(bool *ok) const |
| 7589 | { |
| 7590 | return QLocaleData::convertDoubleToFloat(d: toDouble(ok), ok); |
| 7591 | } |
| 7592 | |
| 7593 | /*! \fn QString &QString::setNum(int n, int base) |
| 7594 | |
| 7595 | Sets the string to the printed value of \a n in the specified \a |
| 7596 | base, and returns a reference to the string. |
| 7597 | |
| 7598 | The base is 10 by default and must be between 2 and 36. For bases |
| 7599 | other than 10, \a n is treated as an unsigned integer. |
| 7600 | |
| 7601 | \snippet qstring/main.cpp 56 |
| 7602 | |
| 7603 | The formatting always uses QLocale::C, i.e., English/UnitedStates. |
| 7604 | To get a localized string representation of a number, use |
| 7605 | QLocale::toString() with the appropriate locale. |
| 7606 | |
| 7607 | \sa number() |
| 7608 | */ |
| 7609 | |
| 7610 | /*! \fn QString &QString::setNum(uint n, int base) |
| 7611 | |
| 7612 | \overload |
| 7613 | */ |
| 7614 | |
| 7615 | /*! \fn QString &QString::setNum(long n, int base) |
| 7616 | |
| 7617 | \overload |
| 7618 | */ |
| 7619 | |
| 7620 | /*! \fn QString &QString::setNum(ulong n, int base) |
| 7621 | |
| 7622 | \overload |
| 7623 | */ |
| 7624 | |
| 7625 | /*! |
| 7626 | \overload |
| 7627 | */ |
| 7628 | QString &QString::setNum(qlonglong n, int base) |
| 7629 | { |
| 7630 | return *this = number(n, base); |
| 7631 | } |
| 7632 | |
| 7633 | /*! |
| 7634 | \overload |
| 7635 | */ |
| 7636 | QString &QString::setNum(qulonglong n, int base) |
| 7637 | { |
| 7638 | return *this = number(n, base); |
| 7639 | } |
| 7640 | |
| 7641 | /*! \fn QString &QString::setNum(short n, int base) |
| 7642 | |
| 7643 | \overload |
| 7644 | */ |
| 7645 | |
| 7646 | /*! \fn QString &QString::setNum(ushort n, int base) |
| 7647 | |
| 7648 | \overload |
| 7649 | */ |
| 7650 | |
| 7651 | /*! |
| 7652 | \fn QString &QString::setNum(double n, char format, int precision) |
| 7653 | \overload |
| 7654 | |
| 7655 | Sets the string to the printed value of \a n, formatted according |
| 7656 | to the given \a format and \a precision, and returns a reference |
| 7657 | to the string. |
| 7658 | |
| 7659 | The \a format can be 'e', 'E', 'f', 'g' or 'G' (see |
| 7660 | \l{Argument Formats} for an explanation of the formats). |
| 7661 | |
| 7662 | The formatting always uses QLocale::C, i.e., English/UnitedStates. |
| 7663 | To get a localized string representation of a number, use |
| 7664 | QLocale::toString() with the appropriate locale. |
| 7665 | |
| 7666 | \sa number() |
| 7667 | */ |
| 7668 | |
| 7669 | QString &QString::setNum(double n, char f, int prec) |
| 7670 | { |
| 7671 | return *this = number(n, f, prec); |
| 7672 | } |
| 7673 | |
| 7674 | /*! |
| 7675 | \fn QString &QString::setNum(float n, char format, int precision) |
| 7676 | \overload |
| 7677 | |
| 7678 | Sets the string to the printed value of \a n, formatted according |
| 7679 | to the given \a format and \a precision, and returns a reference |
| 7680 | to the string. |
| 7681 | |
| 7682 | The formatting always uses QLocale::C, i.e., English/UnitedStates. |
| 7683 | To get a localized string representation of a number, use |
| 7684 | QLocale::toString() with the appropriate locale. |
| 7685 | |
| 7686 | \sa number() |
| 7687 | */ |
| 7688 | |
| 7689 | |
| 7690 | /*! |
| 7691 | \fn QString QString::number(long n, int base) |
| 7692 | |
| 7693 | Returns a string equivalent of the number \a n according to the |
| 7694 | specified \a base. |
| 7695 | |
| 7696 | The base is 10 by default and must be between 2 |
| 7697 | and 36. For bases other than 10, \a n is treated as an |
| 7698 | unsigned integer. |
| 7699 | |
| 7700 | The formatting always uses QLocale::C, i.e., English/UnitedStates. |
| 7701 | To get a localized string representation of a number, use |
| 7702 | QLocale::toString() with the appropriate locale. |
| 7703 | |
| 7704 | \snippet qstring/main.cpp 35 |
| 7705 | |
| 7706 | \sa setNum() |
| 7707 | */ |
| 7708 | |
| 7709 | QString QString::number(long n, int base) |
| 7710 | { |
| 7711 | return number(qlonglong(n), base); |
| 7712 | } |
| 7713 | |
| 7714 | /*! |
| 7715 | \fn QString QString::number(ulong n, int base) |
| 7716 | |
| 7717 | \overload |
| 7718 | */ |
| 7719 | QString QString::number(ulong n, int base) |
| 7720 | { |
| 7721 | return number(qulonglong(n), base); |
| 7722 | } |
| 7723 | |
| 7724 | /*! |
| 7725 | \overload |
| 7726 | */ |
| 7727 | QString QString::number(int n, int base) |
| 7728 | { |
| 7729 | return number(qlonglong(n), base); |
| 7730 | } |
| 7731 | |
| 7732 | /*! |
| 7733 | \overload |
| 7734 | */ |
| 7735 | QString QString::number(uint n, int base) |
| 7736 | { |
| 7737 | return number(qulonglong(n), base); |
| 7738 | } |
| 7739 | |
| 7740 | /*! |
| 7741 | \overload |
| 7742 | */ |
| 7743 | QString QString::number(qlonglong n, int base) |
| 7744 | { |
| 7745 | #if defined(QT_CHECK_RANGE) |
| 7746 | if (base < 2 || base > 36) { |
| 7747 | qWarning("QString::setNum: Invalid base (%d)" , base); |
| 7748 | base = 10; |
| 7749 | } |
| 7750 | #endif |
| 7751 | return QLocaleData::c()->longLongToString(l: n, precision: -1, base); |
| 7752 | } |
| 7753 | |
| 7754 | /*! |
| 7755 | \overload |
| 7756 | */ |
| 7757 | QString QString::number(qulonglong n, int base) |
| 7758 | { |
| 7759 | #if defined(QT_CHECK_RANGE) |
| 7760 | if (base < 2 || base > 36) { |
| 7761 | qWarning("QString::setNum: Invalid base (%d)" , base); |
| 7762 | base = 10; |
| 7763 | } |
| 7764 | #endif |
| 7765 | return QLocaleData::c()->unsLongLongToString(l: n, precision: -1, base); |
| 7766 | } |
| 7767 | |
| 7768 | |
| 7769 | /*! |
| 7770 | \fn QString QString::number(double n, char format, int precision) |
| 7771 | |
| 7772 | Returns a string equivalent of the number \a n, formatted |
| 7773 | according to the specified \a format and \a precision. See |
| 7774 | \l{Argument Formats} for details. |
| 7775 | |
| 7776 | Unlike QLocale::toString(), this function does not honor the |
| 7777 | user's locale settings. |
| 7778 | |
| 7779 | \sa setNum(), QLocale::toString() |
| 7780 | */ |
| 7781 | QString QString::number(double n, char f, int prec) |
| 7782 | { |
| 7783 | QLocaleData::DoubleForm form = QLocaleData::DFDecimal; |
| 7784 | uint flags = QLocaleData::ZeroPadExponent; |
| 7785 | |
| 7786 | if (qIsUpper(ch: f)) |
| 7787 | flags |= QLocaleData::CapitalEorX; |
| 7788 | |
| 7789 | switch (qToLower(ch: f)) { |
| 7790 | case 'f': |
| 7791 | form = QLocaleData::DFDecimal; |
| 7792 | break; |
| 7793 | case 'e': |
| 7794 | form = QLocaleData::DFExponent; |
| 7795 | break; |
| 7796 | case 'g': |
| 7797 | form = QLocaleData::DFSignificantDigits; |
| 7798 | break; |
| 7799 | default: |
| 7800 | #if defined(QT_CHECK_RANGE) |
| 7801 | qWarning("QString::setNum: Invalid format char '%c'" , f); |
| 7802 | #endif |
| 7803 | break; |
| 7804 | } |
| 7805 | |
| 7806 | return QLocaleData::c()->doubleToString(d: n, precision: prec, form, width: -1, flags); |
| 7807 | } |
| 7808 | |
| 7809 | namespace { |
| 7810 | template<class ResultList, class StringSource> |
| 7811 | static ResultList splitString(const StringSource &source, const QChar *sep, |
| 7812 | Qt::SplitBehavior behavior, Qt::CaseSensitivity cs, const int separatorSize) |
| 7813 | { |
| 7814 | ResultList list; |
| 7815 | typename StringSource::size_type start = 0; |
| 7816 | typename StringSource::size_type end; |
| 7817 | typename StringSource::size_type = 0; |
| 7818 | while ((end = QtPrivate::findString(QStringView(source.constData(), source.size()), start + extra, QStringView(sep, separatorSize), cs)) != -1) { |
| 7819 | if (start != end || behavior == Qt::KeepEmptyParts) |
| 7820 | list.append(source.mid(start, end - start)); |
| 7821 | start = end + separatorSize; |
| 7822 | extra = (separatorSize == 0 ? 1 : 0); |
| 7823 | } |
| 7824 | if (start != source.size() || behavior == Qt::KeepEmptyParts) |
| 7825 | list.append(source.mid(start, -1)); |
| 7826 | return list; |
| 7827 | } |
| 7828 | |
| 7829 | #if QT_DEPRECATED_SINCE(5, 15) |
| 7830 | Qt::SplitBehavior mapSplitBehavior(QString::SplitBehavior sb) |
| 7831 | { |
| 7832 | QT_WARNING_PUSH |
| 7833 | QT_WARNING_DISABLE_DEPRECATED |
| 7834 | return sb & QString::SkipEmptyParts ? Qt::SkipEmptyParts : Qt::KeepEmptyParts; |
| 7835 | QT_WARNING_POP |
| 7836 | } |
| 7837 | #endif |
| 7838 | |
| 7839 | } // namespace |
| 7840 | |
| 7841 | /*! |
| 7842 | Splits the string into substrings wherever \a sep occurs, and |
| 7843 | returns the list of those strings. If \a sep does not match |
| 7844 | anywhere in the string, split() returns a single-element list |
| 7845 | containing this string. |
| 7846 | |
| 7847 | \a cs specifies whether \a sep should be matched case |
| 7848 | sensitively or case insensitively. |
| 7849 | |
| 7850 | If \a behavior is Qt::SkipEmptyParts, empty entries don't |
| 7851 | appear in the result. By default, empty entries are kept. |
| 7852 | |
| 7853 | Example: |
| 7854 | |
| 7855 | \snippet qstring/main.cpp 62 |
| 7856 | |
| 7857 | If \a sep is empty, split() returns an empty string, followed |
| 7858 | by each of the string's characters, followed by another empty string: |
| 7859 | |
| 7860 | \snippet qstring/main.cpp 62-empty |
| 7861 | |
| 7862 | To understand this behavior, recall that the empty string matches |
| 7863 | everywhere, so the above is qualitatively the same as: |
| 7864 | |
| 7865 | \snippet qstring/main.cpp 62-slashes |
| 7866 | |
| 7867 | \sa QStringList::join(), section() |
| 7868 | |
| 7869 | \since 5.14 |
| 7870 | */ |
| 7871 | QStringList QString::split(const QString &sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const |
| 7872 | { |
| 7873 | return splitString<QStringList>(source: *this, sep: sep.constData(), behavior, cs, separatorSize: sep.size()); |
| 7874 | } |
| 7875 | |
| 7876 | #if QT_DEPRECATED_SINCE(5, 15) |
| 7877 | /*! |
| 7878 | \overload |
| 7879 | Use QString::split(const QString &sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) instead. |
| 7880 | |
| 7881 | \obsolete |
| 7882 | */ |
| 7883 | QStringList QString::split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const |
| 7884 | { |
| 7885 | return split(sep, behavior: mapSplitBehavior(sb: behavior), cs); |
| 7886 | } |
| 7887 | #endif |
| 7888 | |
| 7889 | /*! |
| 7890 | Splits the string into substring references wherever \a sep occurs, and |
| 7891 | returns the list of those strings. |
| 7892 | |
| 7893 | See QString::split() for how \a sep, \a behavior and \a cs interact to form |
| 7894 | the result. |
| 7895 | |
| 7896 | \note All references are valid as long this string is alive. Destroying this |
| 7897 | string will cause all references to be dangling pointers. |
| 7898 | |
| 7899 | \since 5.14 |
| 7900 | \sa QStringRef split() |
| 7901 | */ |
| 7902 | QVector<QStringRef> QString::splitRef(const QString &sep, Qt::SplitBehavior behavior, |
| 7903 | Qt::CaseSensitivity cs) const |
| 7904 | { |
| 7905 | return splitString<QVector<QStringRef>>(source: QStringRef(this), sep: sep.constData(), behavior, |
| 7906 | cs, separatorSize: sep.size()); |
| 7907 | } |
| 7908 | |
| 7909 | #if QT_DEPRECATED_SINCE(5, 15) |
| 7910 | /*! |
| 7911 | \overload |
| 7912 | \obsolete |
| 7913 | Use QString::splitRef(const QString &sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) instead. |
| 7914 | |
| 7915 | \since 5.4 |
| 7916 | */ |
| 7917 | QVector<QStringRef> QString::splitRef(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const |
| 7918 | { |
| 7919 | return splitRef(sep, behavior: mapSplitBehavior(sb: behavior), cs); |
| 7920 | } |
| 7921 | #endif |
| 7922 | |
| 7923 | /*! |
| 7924 | \overload |
| 7925 | \since 5.14 |
| 7926 | */ |
| 7927 | QStringList QString::split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const |
| 7928 | { |
| 7929 | return splitString<QStringList>(source: *this, sep: &sep, behavior, cs, separatorSize: 1); |
| 7930 | } |
| 7931 | |
| 7932 | #if QT_DEPRECATED_SINCE(5, 15) |
| 7933 | /*! |
| 7934 | \overload |
| 7935 | \obsolete |
| 7936 | Use QString::split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) instead. |
| 7937 | |
| 7938 | */ |
| 7939 | QStringList QString::split(QChar sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const |
| 7940 | { |
| 7941 | return split(sep, behavior: mapSplitBehavior(sb: behavior), cs); |
| 7942 | } |
| 7943 | #endif |
| 7944 | |
| 7945 | /*! |
| 7946 | \overload |
| 7947 | \since 5.14 |
| 7948 | */ |
| 7949 | QVector<QStringRef> QString::splitRef(QChar sep, Qt::SplitBehavior behavior, |
| 7950 | Qt::CaseSensitivity cs) const |
| 7951 | { |
| 7952 | return splitString<QVector<QStringRef> >(source: QStringRef(this), sep: &sep, behavior, cs, separatorSize: 1); |
| 7953 | } |
| 7954 | |
| 7955 | #if QT_DEPRECATED_SINCE(5, 15) |
| 7956 | /*! |
| 7957 | \overload |
| 7958 | \since 5.4 |
| 7959 | */ |
| 7960 | QVector<QStringRef> QString::splitRef(QChar sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const |
| 7961 | { |
| 7962 | return splitRef(sep, behavior: mapSplitBehavior(sb: behavior), cs); |
| 7963 | } |
| 7964 | #endif |
| 7965 | |
| 7966 | /*! |
| 7967 | Splits the string into substrings references wherever \a sep occurs, and |
| 7968 | returns the list of those strings. |
| 7969 | |
| 7970 | See QString::split() for how \a sep, \a behavior and \a cs interact to form |
| 7971 | the result. |
| 7972 | |
| 7973 | \note All references are valid as long this string is alive. Destroying this |
| 7974 | string will cause all references to be dangling pointers. |
| 7975 | |
| 7976 | \since 5.14 |
| 7977 | */ |
| 7978 | QVector<QStringRef> QStringRef::split(const QString &sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const |
| 7979 | { |
| 7980 | return splitString<QVector<QStringRef> >(source: *this, sep: sep.constData(), behavior, cs, separatorSize: sep.size()); |
| 7981 | } |
| 7982 | |
| 7983 | #if QT_DEPRECATED_SINCE(5, 15) |
| 7984 | /*! |
| 7985 | \overload |
| 7986 | \since 5.4 |
| 7987 | \obsolete |
| 7988 | Use QString::split(const QString &sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) instead. |
| 7989 | */ |
| 7990 | QVector<QStringRef> QStringRef::split(const QString &sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const |
| 7991 | { |
| 7992 | return split(sep, behavior: mapSplitBehavior(sb: behavior), cs); |
| 7993 | } |
| 7994 | #endif |
| 7995 | |
| 7996 | /*! |
| 7997 | \overload |
| 7998 | \since 5.14 |
| 7999 | */ |
| 8000 | QVector<QStringRef> QStringRef::split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const |
| 8001 | { |
| 8002 | return splitString<QVector<QStringRef> >(source: *this, sep: &sep, behavior, cs, separatorSize: 1); |
| 8003 | } |
| 8004 | |
| 8005 | #if QT_DEPRECATED_SINCE(5, 15) |
| 8006 | /*! |
| 8007 | \overload |
| 8008 | \since 5.4 |
| 8009 | \obsolete |
| 8010 | Use QString::split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) instead. |
| 8011 | */ |
| 8012 | QVector<QStringRef> QStringRef::split(QChar sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const |
| 8013 | { |
| 8014 | return split(sep, behavior: mapSplitBehavior(sb: behavior), cs); |
| 8015 | } |
| 8016 | #endif |
| 8017 | |
| 8018 | #ifndef QT_NO_REGEXP |
| 8019 | namespace { |
| 8020 | template<class ResultList, typename MidMethod> |
| 8021 | static ResultList splitString(const QString &source, MidMethod mid, const QRegExp &rx, Qt::SplitBehavior behavior) |
| 8022 | { |
| 8023 | QRegExp rx2(rx); |
| 8024 | ResultList list; |
| 8025 | int start = 0; |
| 8026 | int = 0; |
| 8027 | int end; |
| 8028 | while ((end = rx2.indexIn(str: source, offset: start + extra)) != -1) { |
| 8029 | int matchedLen = rx2.matchedLength(); |
| 8030 | if (start != end || behavior == Qt::KeepEmptyParts) |
| 8031 | list.append((source.*mid)(start, end - start)); |
| 8032 | start = end + matchedLen; |
| 8033 | extra = (matchedLen == 0) ? 1 : 0; |
| 8034 | } |
| 8035 | if (start != source.size() || behavior == Qt::KeepEmptyParts) |
| 8036 | list.append((source.*mid)(start, -1)); |
| 8037 | return list; |
| 8038 | } |
| 8039 | } // namespace |
| 8040 | |
| 8041 | /*! |
| 8042 | \overload |
| 8043 | \since 5.14 |
| 8044 | |
| 8045 | Splits the string into substrings wherever the regular expression |
| 8046 | \a rx matches, and returns the list of those strings. If \a rx |
| 8047 | does not match anywhere in the string, split() returns a |
| 8048 | single-element list containing this string. |
| 8049 | |
| 8050 | Here is an example where we extract the words in a sentence |
| 8051 | using one or more whitespace characters as the separator: |
| 8052 | |
| 8053 | \snippet qstring/main.cpp 59 |
| 8054 | |
| 8055 | Here is a similar example, but this time we use any sequence of |
| 8056 | non-word characters as the separator: |
| 8057 | |
| 8058 | \snippet qstring/main.cpp 60 |
| 8059 | |
| 8060 | Here is a third example where we use a zero-length assertion, |
| 8061 | \b{\\b} (word boundary), to split the string into an |
| 8062 | alternating sequence of non-word and word tokens: |
| 8063 | |
| 8064 | \snippet qstring/main.cpp 61 |
| 8065 | |
| 8066 | \sa QStringList::join(), section() |
| 8067 | */ |
| 8068 | QStringList QString::split(const QRegExp &rx, Qt::SplitBehavior behavior) const |
| 8069 | { |
| 8070 | return splitString<QStringList>(source: *this, mid: &QString::mid, rx, behavior); |
| 8071 | } |
| 8072 | |
| 8073 | # if QT_DEPRECATED_SINCE(5, 15) |
| 8074 | /*! |
| 8075 | \overload |
| 8076 | \obsolete |
| 8077 | Use QString::split(const QRegularExpression &sep, Qt::SplitBehavior behavior) instead. |
| 8078 | */ |
| 8079 | QStringList QString::split(const QRegExp &rx, SplitBehavior behavior) const |
| 8080 | { |
| 8081 | return split(rx, behavior: mapSplitBehavior(sb: behavior)); |
| 8082 | } |
| 8083 | # endif |
| 8084 | |
| 8085 | /*! |
| 8086 | \overload |
| 8087 | \since 5.14 |
| 8088 | |
| 8089 | Splits the string into substring references wherever the regular expression |
| 8090 | \a rx matches, and returns the list of those strings. If \a rx |
| 8091 | does not match anywhere in the string, splitRef() returns a |
| 8092 | single-element vector containing this string reference. |
| 8093 | |
| 8094 | \note All references are valid as long this string is alive. Destroying this |
| 8095 | string will cause all references to be dangling pointers. |
| 8096 | |
| 8097 | \sa QStringRef split() |
| 8098 | */ |
| 8099 | QVector<QStringRef> QString::splitRef(const QRegExp &rx, Qt::SplitBehavior behavior) const |
| 8100 | { |
| 8101 | return splitString<QVector<QStringRef> >(source: *this, mid: &QString::midRef, rx, behavior); |
| 8102 | } |
| 8103 | |
| 8104 | # if QT_DEPRECATED_SINCE(5, 15) |
| 8105 | /*! |
| 8106 | \overload |
| 8107 | \since 5.4 |
| 8108 | \obsolete |
| 8109 | Use QString::splitRef(const QRegularExpression &sep, Qt::SplitBehavior behavior) instead. |
| 8110 | */ |
| 8111 | QVector<QStringRef> QString::splitRef(const QRegExp &rx, SplitBehavior behavior) const |
| 8112 | { |
| 8113 | return splitRef(rx, behavior: mapSplitBehavior(sb: behavior)); |
| 8114 | } |
| 8115 | # endif |
| 8116 | #endif // QT_NO_REGEXP |
| 8117 | |
| 8118 | #if QT_CONFIG(regularexpression) |
| 8119 | namespace { |
| 8120 | template<class ResultList, typename MidMethod> |
| 8121 | static ResultList splitString(const QString &source, MidMethod mid, const QRegularExpression &re, |
| 8122 | Qt::SplitBehavior behavior) |
| 8123 | { |
| 8124 | ResultList list; |
| 8125 | if (!re.isValid()) { |
| 8126 | qWarning(msg: "QString::split: invalid QRegularExpression object" ); |
| 8127 | return list; |
| 8128 | } |
| 8129 | |
| 8130 | int start = 0; |
| 8131 | int end = 0; |
| 8132 | QRegularExpressionMatchIterator iterator = re.globalMatch(subject: source); |
| 8133 | while (iterator.hasNext()) { |
| 8134 | QRegularExpressionMatch match = iterator.next(); |
| 8135 | end = match.capturedStart(); |
| 8136 | if (start != end || behavior == Qt::KeepEmptyParts) |
| 8137 | list.append((source.*mid)(start, end - start)); |
| 8138 | start = match.capturedEnd(); |
| 8139 | } |
| 8140 | |
| 8141 | if (start != source.size() || behavior == Qt::KeepEmptyParts) |
| 8142 | list.append((source.*mid)(start, -1)); |
| 8143 | |
| 8144 | return list; |
| 8145 | } |
| 8146 | } // namespace |
| 8147 | |
| 8148 | /*! |
| 8149 | \overload |
| 8150 | \since 5.14 |
| 8151 | |
| 8152 | Splits the string into substrings wherever the regular expression |
| 8153 | \a re matches, and returns the list of those strings. If \a re |
| 8154 | does not match anywhere in the string, split() returns a |
| 8155 | single-element list containing this string. |
| 8156 | |
| 8157 | Here is an example where we extract the words in a sentence |
| 8158 | using one or more whitespace characters as the separator: |
| 8159 | |
| 8160 | \snippet qstring/main.cpp 90 |
| 8161 | |
| 8162 | Here is a similar example, but this time we use any sequence of |
| 8163 | non-word characters as the separator: |
| 8164 | |
| 8165 | \snippet qstring/main.cpp 91 |
| 8166 | |
| 8167 | Here is a third example where we use a zero-length assertion, |
| 8168 | \b{\\b} (word boundary), to split the string into an |
| 8169 | alternating sequence of non-word and word tokens: |
| 8170 | |
| 8171 | \snippet qstring/main.cpp 92 |
| 8172 | |
| 8173 | \sa QStringList::join(), section() |
| 8174 | */ |
| 8175 | QStringList QString::split(const QRegularExpression &re, Qt::SplitBehavior behavior) const |
| 8176 | { |
| 8177 | return splitString<QStringList>(source: *this, mid: &QString::mid, re, behavior); |
| 8178 | } |
| 8179 | |
| 8180 | # if QT_DEPRECATED_SINCE(5, 15) |
| 8181 | /*! |
| 8182 | \overload |
| 8183 | \since 5.0 |
| 8184 | \obsolete |
| 8185 | Use QString::split(const QRegularExpression &sep, Qt::SplitBehavior behavior) instead. |
| 8186 | */ |
| 8187 | QStringList QString::split(const QRegularExpression &re, SplitBehavior behavior) const |
| 8188 | { |
| 8189 | return split(re, behavior: mapSplitBehavior(sb: behavior)); |
| 8190 | } |
| 8191 | # endif |
| 8192 | |
| 8193 | /*! |
| 8194 | \overload |
| 8195 | \since 5.14 |
| 8196 | |
| 8197 | Splits the string into substring references wherever the regular expression |
| 8198 | \a re matches, and returns the list of those strings. If \a re |
| 8199 | does not match anywhere in the string, splitRef() returns a |
| 8200 | single-element vector containing this string reference. |
| 8201 | |
| 8202 | \note All references are valid as long this string is alive. Destroying this |
| 8203 | string will cause all references to be dangling pointers. |
| 8204 | |
| 8205 | \sa split() QStringRef |
| 8206 | */ |
| 8207 | QVector<QStringRef> QString::splitRef(const QRegularExpression &re, Qt::SplitBehavior behavior) const |
| 8208 | { |
| 8209 | return splitString<QVector<QStringRef> >(source: *this, mid: &QString::midRef, re, behavior); |
| 8210 | } |
| 8211 | |
| 8212 | # if QT_DEPRECATED_SINCE(5, 15) |
| 8213 | /*! |
| 8214 | \overload |
| 8215 | \since 5.4 |
| 8216 | \obsolete |
| 8217 | Use QString::splitRef(const QRegularExpression &sep, Qt::SplitBehavior behavior) instead. |
| 8218 | |
| 8219 | */ |
| 8220 | QVector<QStringRef> QString::splitRef(const QRegularExpression &re, SplitBehavior behavior) const |
| 8221 | { |
| 8222 | return splitRef(re, behavior: mapSplitBehavior(sb: behavior)); |
| 8223 | } |
| 8224 | # endif |
| 8225 | #endif // QT_CONFIG(regularexpression) |
| 8226 | |
| 8227 | /*! |
| 8228 | \enum QString::NormalizationForm |
| 8229 | |
| 8230 | This enum describes the various normalized forms of Unicode text. |
| 8231 | |
| 8232 | \value NormalizationForm_D Canonical Decomposition |
| 8233 | \value NormalizationForm_C Canonical Decomposition followed by Canonical Composition |
| 8234 | \value NormalizationForm_KD Compatibility Decomposition |
| 8235 | \value NormalizationForm_KC Compatibility Decomposition followed by Canonical Composition |
| 8236 | |
| 8237 | \sa normalized(), |
| 8238 | {https://www.unicode.org/reports/tr15/}{Unicode Standard Annex #15} |
| 8239 | */ |
| 8240 | |
| 8241 | /*! |
| 8242 | \since 4.5 |
| 8243 | |
| 8244 | Returns a copy of this string repeated the specified number of \a times. |
| 8245 | |
| 8246 | If \a times is less than 1, an empty string is returned. |
| 8247 | |
| 8248 | Example: |
| 8249 | |
| 8250 | \snippet code/src_corelib_tools_qstring.cpp 8 |
| 8251 | */ |
| 8252 | QString QString::repeated(int times) const |
| 8253 | { |
| 8254 | if (d->size == 0) |
| 8255 | return *this; |
| 8256 | |
| 8257 | if (times <= 1) { |
| 8258 | if (times == 1) |
| 8259 | return *this; |
| 8260 | return QString(); |
| 8261 | } |
| 8262 | |
| 8263 | const int resultSize = times * d->size; |
| 8264 | |
| 8265 | QString result; |
| 8266 | result.reserve(asize: resultSize); |
| 8267 | if (result.d->alloc != uint(resultSize) + 1u) |
| 8268 | return QString(); // not enough memory |
| 8269 | |
| 8270 | memcpy(dest: result.d->data(), src: d->data(), n: d->size * sizeof(ushort)); |
| 8271 | |
| 8272 | int sizeSoFar = d->size; |
| 8273 | ushort *end = result.d->data() + sizeSoFar; |
| 8274 | |
| 8275 | const int halfResultSize = resultSize >> 1; |
| 8276 | while (sizeSoFar <= halfResultSize) { |
| 8277 | memcpy(dest: end, src: result.d->data(), n: sizeSoFar * sizeof(ushort)); |
| 8278 | end += sizeSoFar; |
| 8279 | sizeSoFar <<= 1; |
| 8280 | } |
| 8281 | memcpy(dest: end, src: result.d->data(), n: (resultSize - sizeSoFar) * sizeof(ushort)); |
| 8282 | result.d->data()[resultSize] = '\0'; |
| 8283 | result.d->size = resultSize; |
| 8284 | return result; |
| 8285 | } |
| 8286 | |
| 8287 | void qt_string_normalize(QString *data, QString::NormalizationForm mode, QChar::UnicodeVersion version, int from) |
| 8288 | { |
| 8289 | const QChar *p = data->constData() + from; |
| 8290 | if (isAscii(ptr&: p, end: p + data->length() - from)) |
| 8291 | return; |
| 8292 | if (p > data->constData() + from) |
| 8293 | from = p - data->constData() - 1; // need one before the non-ASCII to perform NFC |
| 8294 | |
| 8295 | if (version == QChar::Unicode_Unassigned) { |
| 8296 | version = QChar::currentUnicodeVersion(); |
| 8297 | } else if (int(version) <= NormalizationCorrectionsVersionMax) { |
| 8298 | const QString &s = *data; |
| 8299 | QChar *d = nullptr; |
| 8300 | for (int i = 0; i < NumNormalizationCorrections; ++i) { |
| 8301 | const NormalizationCorrection &n = uc_normalization_corrections[i]; |
| 8302 | if (n.version > version) { |
| 8303 | int pos = from; |
| 8304 | if (QChar::requiresSurrogates(ucs4: n.ucs4)) { |
| 8305 | ushort ucs4High = QChar::highSurrogate(ucs4: n.ucs4); |
| 8306 | ushort ucs4Low = QChar::lowSurrogate(ucs4: n.ucs4); |
| 8307 | ushort oldHigh = QChar::highSurrogate(ucs4: n.old_mapping); |
| 8308 | ushort oldLow = QChar::lowSurrogate(ucs4: n.old_mapping); |
| 8309 | while (pos < s.length() - 1) { |
| 8310 | if (s.at(i: pos).unicode() == ucs4High && s.at(i: pos + 1).unicode() == ucs4Low) { |
| 8311 | if (!d) |
| 8312 | d = data->data(); |
| 8313 | d[pos] = QChar(oldHigh); |
| 8314 | d[++pos] = QChar(oldLow); |
| 8315 | } |
| 8316 | ++pos; |
| 8317 | } |
| 8318 | } else { |
| 8319 | while (pos < s.length()) { |
| 8320 | if (s.at(i: pos).unicode() == n.ucs4) { |
| 8321 | if (!d) |
| 8322 | d = data->data(); |
| 8323 | d[pos] = QChar(n.old_mapping); |
| 8324 | } |
| 8325 | ++pos; |
| 8326 | } |
| 8327 | } |
| 8328 | } |
| 8329 | } |
| 8330 | } |
| 8331 | |
| 8332 | if (normalizationQuickCheckHelper(str: data, mode, from, lastStable: &from)) |
| 8333 | return; |
| 8334 | |
| 8335 | decomposeHelper(str: data, canonical: mode < QString::NormalizationForm_KD, version, from); |
| 8336 | |
| 8337 | canonicalOrderHelper(str: data, version, from); |
| 8338 | |
| 8339 | if (mode == QString::NormalizationForm_D || mode == QString::NormalizationForm_KD) |
| 8340 | return; |
| 8341 | |
| 8342 | composeHelper(str: data, version, from); |
| 8343 | } |
| 8344 | |
| 8345 | /*! |
| 8346 | Returns the string in the given Unicode normalization \a mode, |
| 8347 | according to the given \a version of the Unicode standard. |
| 8348 | */ |
| 8349 | QString QString::normalized(QString::NormalizationForm mode, QChar::UnicodeVersion version) const |
| 8350 | { |
| 8351 | QString copy = *this; |
| 8352 | qt_string_normalize(data: ©, mode, version, from: 0); |
| 8353 | return copy; |
| 8354 | } |
| 8355 | |
| 8356 | |
| 8357 | struct ArgEscapeData |
| 8358 | { |
| 8359 | int min_escape; // lowest escape sequence number |
| 8360 | int occurrences; // number of occurrences of the lowest escape sequence number |
| 8361 | int locale_occurrences; // number of occurrences of the lowest escape sequence number that |
| 8362 | // contain 'L' |
| 8363 | int escape_len; // total length of escape sequences which will be replaced |
| 8364 | }; |
| 8365 | |
| 8366 | static ArgEscapeData findArgEscapes(QStringView s) |
| 8367 | { |
| 8368 | const QChar *uc_begin = s.begin(); |
| 8369 | const QChar *uc_end = s.end(); |
| 8370 | |
| 8371 | ArgEscapeData d; |
| 8372 | |
| 8373 | d.min_escape = INT_MAX; |
| 8374 | d.occurrences = 0; |
| 8375 | d.escape_len = 0; |
| 8376 | d.locale_occurrences = 0; |
| 8377 | |
| 8378 | const QChar *c = uc_begin; |
| 8379 | while (c != uc_end) { |
| 8380 | while (c != uc_end && c->unicode() != '%') |
| 8381 | ++c; |
| 8382 | |
| 8383 | if (c == uc_end) |
| 8384 | break; |
| 8385 | const QChar *escape_start = c; |
| 8386 | if (++c == uc_end) |
| 8387 | break; |
| 8388 | |
| 8389 | bool locale_arg = false; |
| 8390 | if (c->unicode() == 'L') { |
| 8391 | locale_arg = true; |
| 8392 | if (++c == uc_end) |
| 8393 | break; |
| 8394 | } |
| 8395 | |
| 8396 | int escape = c->digitValue(); |
| 8397 | if (escape == -1) |
| 8398 | continue; |
| 8399 | |
| 8400 | ++c; |
| 8401 | |
| 8402 | if (c != uc_end) { |
| 8403 | int next_escape = c->digitValue(); |
| 8404 | if (next_escape != -1) { |
| 8405 | escape = (10 * escape) + next_escape; |
| 8406 | ++c; |
| 8407 | } |
| 8408 | } |
| 8409 | |
| 8410 | if (escape > d.min_escape) |
| 8411 | continue; |
| 8412 | |
| 8413 | if (escape < d.min_escape) { |
| 8414 | d.min_escape = escape; |
| 8415 | d.occurrences = 0; |
| 8416 | d.escape_len = 0; |
| 8417 | d.locale_occurrences = 0; |
| 8418 | } |
| 8419 | |
| 8420 | ++d.occurrences; |
| 8421 | if (locale_arg) |
| 8422 | ++d.locale_occurrences; |
| 8423 | d.escape_len += c - escape_start; |
| 8424 | } |
| 8425 | return d; |
| 8426 | } |
| 8427 | |
| 8428 | static QString replaceArgEscapes(QStringView s, const ArgEscapeData &d, int field_width, |
| 8429 | QStringView arg, QStringView larg, QChar fillChar) |
| 8430 | { |
| 8431 | const QChar *uc_begin = s.begin(); |
| 8432 | const QChar *uc_end = s.end(); |
| 8433 | |
| 8434 | int abs_field_width = qAbs(t: field_width); |
| 8435 | int result_len = s.length() |
| 8436 | - d.escape_len |
| 8437 | + (d.occurrences - d.locale_occurrences) |
| 8438 | *qMax(a: abs_field_width, b: arg.length()) |
| 8439 | + d.locale_occurrences |
| 8440 | *qMax(a: abs_field_width, b: larg.length()); |
| 8441 | |
| 8442 | QString result(result_len, Qt::Uninitialized); |
| 8443 | QChar *result_buff = (QChar*) result.unicode(); |
| 8444 | |
| 8445 | QChar *rc = result_buff; |
| 8446 | const QChar *c = uc_begin; |
| 8447 | int repl_cnt = 0; |
| 8448 | while (c != uc_end) { |
| 8449 | /* We don't have to check if we run off the end of the string with c, |
| 8450 | because as long as d.occurrences > 0 we KNOW there are valid escape |
| 8451 | sequences. */ |
| 8452 | |
| 8453 | const QChar *text_start = c; |
| 8454 | |
| 8455 | while (c->unicode() != '%') |
| 8456 | ++c; |
| 8457 | |
| 8458 | const QChar *escape_start = c++; |
| 8459 | |
| 8460 | bool locale_arg = false; |
| 8461 | if (c->unicode() == 'L') { |
| 8462 | locale_arg = true; |
| 8463 | ++c; |
| 8464 | } |
| 8465 | |
| 8466 | int escape = c->digitValue(); |
| 8467 | if (escape != -1) { |
| 8468 | if (c + 1 != uc_end && (c + 1)->digitValue() != -1) { |
| 8469 | escape = (10 * escape) + (c + 1)->digitValue(); |
| 8470 | ++c; |
| 8471 | } |
| 8472 | } |
| 8473 | |
| 8474 | if (escape != d.min_escape) { |
| 8475 | memcpy(dest: rc, src: text_start, n: (c - text_start)*sizeof(QChar)); |
| 8476 | rc += c - text_start; |
| 8477 | } |
| 8478 | else { |
| 8479 | ++c; |
| 8480 | |
| 8481 | memcpy(dest: rc, src: text_start, n: (escape_start - text_start)*sizeof(QChar)); |
| 8482 | rc += escape_start - text_start; |
| 8483 | |
| 8484 | uint pad_chars; |
| 8485 | if (locale_arg) |
| 8486 | pad_chars = qMax(a: abs_field_width, b: larg.length()) - larg.length(); |
| 8487 | else |
| 8488 | pad_chars = qMax(a: abs_field_width, b: arg.length()) - arg.length(); |
| 8489 | |
| 8490 | if (field_width > 0) { // left padded |
| 8491 | for (uint i = 0; i < pad_chars; ++i) |
| 8492 | (rc++)->unicode() = fillChar.unicode(); |
| 8493 | } |
| 8494 | |
| 8495 | if (locale_arg) { |
| 8496 | memcpy(dest: rc, src: larg.data(), n: larg.length()*sizeof(QChar)); |
| 8497 | rc += larg.length(); |
| 8498 | } |
| 8499 | else { |
| 8500 | memcpy(dest: rc, src: arg.data(), n: arg.length()*sizeof(QChar)); |
| 8501 | rc += arg.length(); |
| 8502 | } |
| 8503 | |
| 8504 | if (field_width < 0) { // right padded |
| 8505 | for (uint i = 0; i < pad_chars; ++i) |
| 8506 | (rc++)->unicode() = fillChar.unicode(); |
| 8507 | } |
| 8508 | |
| 8509 | if (++repl_cnt == d.occurrences) { |
| 8510 | memcpy(dest: rc, src: c, n: (uc_end - c)*sizeof(QChar)); |
| 8511 | rc += uc_end - c; |
| 8512 | Q_ASSERT(rc - result_buff == result_len); |
| 8513 | c = uc_end; |
| 8514 | } |
| 8515 | } |
| 8516 | } |
| 8517 | Q_ASSERT(rc == result_buff + result_len); |
| 8518 | |
| 8519 | return result; |
| 8520 | } |
| 8521 | |
| 8522 | #if QT_STRINGVIEW_LEVEL < 2 |
| 8523 | /*! |
| 8524 | Returns a copy of this string with the lowest numbered place marker |
| 8525 | replaced by string \a a, i.e., \c %1, \c %2, ..., \c %99. |
| 8526 | |
| 8527 | \a fieldWidth specifies the minimum amount of space that argument \a |
| 8528 | a shall occupy. If \a a requires less space than \a fieldWidth, it |
| 8529 | is padded to \a fieldWidth with character \a fillChar. A positive |
| 8530 | \a fieldWidth produces right-aligned text. A negative \a fieldWidth |
| 8531 | produces left-aligned text. |
| 8532 | |
| 8533 | This example shows how we might create a \c status string for |
| 8534 | reporting progress while processing a list of files: |
| 8535 | |
| 8536 | \snippet qstring/main.cpp 11 |
| 8537 | |
| 8538 | First, \c arg(i) replaces \c %1. Then \c arg(total) replaces \c |
| 8539 | %2. Finally, \c arg(fileName) replaces \c %3. |
| 8540 | |
| 8541 | One advantage of using arg() over asprintf() is that the order of the |
| 8542 | numbered place markers can change, if the application's strings are |
| 8543 | translated into other languages, but each arg() will still replace |
| 8544 | the lowest numbered unreplaced place marker, no matter where it |
| 8545 | appears. Also, if place marker \c %i appears more than once in the |
| 8546 | string, the arg() replaces all of them. |
| 8547 | |
| 8548 | If there is no unreplaced place marker remaining, a warning message |
| 8549 | is output and the result is undefined. Place marker numbers must be |
| 8550 | in the range 1 to 99. |
| 8551 | */ |
| 8552 | QString QString::arg(const QString &a, int fieldWidth, QChar fillChar) const |
| 8553 | { |
| 8554 | return arg(a: qToStringViewIgnoringNull(s: a), fieldWidth, fillChar); |
| 8555 | } |
| 8556 | #endif // QT_STRINGVIEW_LEVEL < 2 |
| 8557 | |
| 8558 | /*! |
| 8559 | \overload |
| 8560 | \since 5.10 |
| 8561 | |
| 8562 | Returns a copy of this string with the lowest-numbered place-marker |
| 8563 | replaced by string \a a, i.e., \c %1, \c %2, ..., \c %99. |
| 8564 | |
| 8565 | \a fieldWidth specifies the minimum amount of space that \a a |
| 8566 | shall occupy. If \a a requires less space than \a fieldWidth, it |
| 8567 | is padded to \a fieldWidth with character \a fillChar. A positive |
| 8568 | \a fieldWidth produces right-aligned text. A negative \a fieldWidth |
| 8569 | produces left-aligned text. |
| 8570 | |
| 8571 | This example shows how we might create a \c status string for |
| 8572 | reporting progress while processing a list of files: |
| 8573 | |
| 8574 | \snippet qstring/main.cpp 11-qstringview |
| 8575 | |
| 8576 | First, \c arg(i) replaces \c %1. Then \c arg(total) replaces \c |
| 8577 | %2. Finally, \c arg(fileName) replaces \c %3. |
| 8578 | |
| 8579 | One advantage of using arg() over asprintf() is that the order of the |
| 8580 | numbered place markers can change, if the application's strings are |
| 8581 | translated into other languages, but each arg() will still replace |
| 8582 | the lowest-numbered unreplaced place-marker, no matter where it |
| 8583 | appears. Also, if place-marker \c %i appears more than once in the |
| 8584 | string, arg() replaces all of them. |
| 8585 | |
| 8586 | If there is no unreplaced place-marker remaining, a warning message |
| 8587 | is printed and the result is undefined. Place-marker numbers must be |
| 8588 | in the range 1 to 99. |
| 8589 | */ |
| 8590 | QString QString::arg(QStringView a, int fieldWidth, QChar fillChar) const |
| 8591 | { |
| 8592 | ArgEscapeData d = findArgEscapes(s: *this); |
| 8593 | |
| 8594 | if (Q_UNLIKELY(d.occurrences == 0)) { |
| 8595 | qWarning(msg: "QString::arg: Argument missing: %ls, %ls" , qUtf16Printable(*this), |
| 8596 | qUtf16Printable(a.toString())); |
| 8597 | return *this; |
| 8598 | } |
| 8599 | return replaceArgEscapes(s: *this, d, field_width: fieldWidth, arg: a, larg: a, fillChar); |
| 8600 | } |
| 8601 | |
| 8602 | /*! |
| 8603 | \overload |
| 8604 | \since 5.10 |
| 8605 | |
| 8606 | Returns a copy of this string with the lowest-numbered place-marker |
| 8607 | replaced by string \a a, i.e., \c %1, \c %2, ..., \c %99. |
| 8608 | |
| 8609 | \a fieldWidth specifies the minimum amount of space that \a a |
| 8610 | shall occupy. If \a a requires less space than \a fieldWidth, it |
| 8611 | is padded to \a fieldWidth with character \a fillChar. A positive |
| 8612 | \a fieldWidth produces right-aligned text. A negative \a fieldWidth |
| 8613 | produces left-aligned text. |
| 8614 | |
| 8615 | One advantage of using arg() over asprintf() is that the order of the |
| 8616 | numbered place markers can change, if the application's strings are |
| 8617 | translated into other languages, but each arg() will still replace |
| 8618 | the lowest-numbered unreplaced place-marker, no matter where it |
| 8619 | appears. Also, if place-marker \c %i appears more than once in the |
| 8620 | string, arg() replaces all of them. |
| 8621 | |
| 8622 | If there is no unreplaced place-marker remaining, a warning message |
| 8623 | is printed and the result is undefined. Place-marker numbers must be |
| 8624 | in the range 1 to 99. |
| 8625 | */ |
| 8626 | QString QString::arg(QLatin1String a, int fieldWidth, QChar fillChar) const |
| 8627 | { |
| 8628 | QVarLengthArray<ushort> utf16(a.size()); |
| 8629 | qt_from_latin1(dst: utf16.data(), str: a.data(), size: a.size()); |
| 8630 | return arg(a: QStringView(utf16.data(), utf16.size()), fieldWidth, fillChar); |
| 8631 | } |
| 8632 | |
| 8633 | /*! |
| 8634 | \fn QString QString::arg(const QString& a1, const QString& a2) const |
| 8635 | \overload arg() |
| 8636 | |
| 8637 | This is the same as \c {str.arg(a1).arg(a2)}, except that the |
| 8638 | strings \a a1 and \a a2 are replaced in one pass. This can make a |
| 8639 | difference if \a a1 contains e.g. \c{%1}: |
| 8640 | |
| 8641 | \snippet qstring/main.cpp 13 |
| 8642 | |
| 8643 | A similar problem occurs when the numbered place markers are not |
| 8644 | white space separated: |
| 8645 | |
| 8646 | \snippet qstring/main.cpp 12 |
| 8647 | \snippet qstring/main.cpp 97 |
| 8648 | |
| 8649 | Let's look at the substitutions: |
| 8650 | \list |
| 8651 | \li First, \c Hello replaces \c {%1} so the string becomes \c {"Hello%3%2"}. |
| 8652 | \li Then, \c 20 replaces \c {%2} so the string becomes \c {"Hello%320"}. |
| 8653 | \li Since the maximum numbered place marker value is 99, \c 50 replaces \c {%32}. |
| 8654 | \endlist |
| 8655 | Thus the string finally becomes \c {"Hello500"}. |
| 8656 | |
| 8657 | In such cases, the following yields the expected results: |
| 8658 | |
| 8659 | \snippet qstring/main.cpp 12 |
| 8660 | \snippet qstring/main.cpp 98 |
| 8661 | */ |
| 8662 | |
| 8663 | /*! |
| 8664 | \fn QString QString::arg(const QString& a1, const QString& a2, const QString& a3) const |
| 8665 | \overload arg() |
| 8666 | |
| 8667 | This is the same as calling \c str.arg(a1).arg(a2).arg(a3), except |
| 8668 | that the strings \a a1, \a a2 and \a a3 are replaced in one pass. |
| 8669 | */ |
| 8670 | |
| 8671 | /*! |
| 8672 | \fn QString QString::arg(const QString& a1, const QString& a2, const QString& a3, const QString& a4) const |
| 8673 | \overload arg() |
| 8674 | |
| 8675 | This is the same as calling \c |
| 8676 | {str.arg(a1).arg(a2).arg(a3).arg(a4)}, except that the strings \a |
| 8677 | a1, \a a2, \a a3 and \a a4 are replaced in one pass. |
| 8678 | */ |
| 8679 | |
| 8680 | /*! |
| 8681 | \fn QString QString::arg(const QString& a1, const QString& a2, const QString& a3, const QString& a4, const QString& a5) const |
| 8682 | \overload arg() |
| 8683 | |
| 8684 | This is the same as calling \c |
| 8685 | {str.arg(a1).arg(a2).arg(a3).arg(a4).arg(a5)}, except that the strings |
| 8686 | \a a1, \a a2, \a a3, \a a4, and \a a5 are replaced in one pass. |
| 8687 | */ |
| 8688 | |
| 8689 | /*! |
| 8690 | \fn QString QString::arg(const QString& a1, const QString& a2, const QString& a3, const QString& a4, const QString& a5, const QString& a6) const |
| 8691 | \overload arg() |
| 8692 | |
| 8693 | This is the same as calling \c |
| 8694 | {str.arg(a1).arg(a2).arg(a3).arg(a4).arg(a5).arg(a6))}, except that |
| 8695 | the strings \a a1, \a a2, \a a3, \a a4, \a a5, and \a a6 are |
| 8696 | replaced in one pass. |
| 8697 | */ |
| 8698 | |
| 8699 | /*! |
| 8700 | \fn QString QString::arg(const QString& a1, const QString& a2, const QString& a3, const QString& a4, const QString& a5, const QString& a6, const QString& a7) const |
| 8701 | \overload arg() |
| 8702 | |
| 8703 | This is the same as calling \c |
| 8704 | {str.arg(a1).arg(a2).arg(a3).arg(a4).arg(a5).arg(a6).arg(a7)}, |
| 8705 | except that the strings \a a1, \a a2, \a a3, \a a4, \a a5, \a a6, |
| 8706 | and \a a7 are replaced in one pass. |
| 8707 | */ |
| 8708 | |
| 8709 | /*! |
| 8710 | \fn QString QString::arg(const QString& a1, const QString& a2, const QString& a3, const QString& a4, const QString& a5, const QString& a6, const QString& a7, const QString& a8) const |
| 8711 | \overload arg() |
| 8712 | |
| 8713 | This is the same as calling \c |
| 8714 | {str.arg(a1).arg(a2).arg(a3).arg(a4).arg(a5).arg(a6).arg(a7).arg(a8)}, |
| 8715 | except that the strings \a a1, \a a2, \a a3, \a a4, \a a5, \a a6, \a |
| 8716 | a7, and \a a8 are replaced in one pass. |
| 8717 | */ |
| 8718 | |
| 8719 | /*! |
| 8720 | \fn QString QString::arg(const QString& a1, const QString& a2, const QString& a3, const QString& a4, const QString& a5, const QString& a6, const QString& a7, const QString& a8, const QString& a9) const |
| 8721 | \overload arg() |
| 8722 | |
| 8723 | This is the same as calling \c |
| 8724 | {str.arg(a1).arg(a2).arg(a3).arg(a4).arg(a5).arg(a6).arg(a7).arg(a8).arg(a9)}, |
| 8725 | except that the strings \a a1, \a a2, \a a3, \a a4, \a a5, \a a6, \a |
| 8726 | a7, \a a8, and \a a9 are replaced in one pass. |
| 8727 | */ |
| 8728 | |
| 8729 | /*! \fn QString QString::arg(int a, int fieldWidth, int base, QChar fillChar) const |
| 8730 | \overload arg() |
| 8731 | |
| 8732 | The \a a argument is expressed in base \a base, which is 10 by |
| 8733 | default and must be between 2 and 36. For bases other than 10, \a a |
| 8734 | is treated as an unsigned integer. |
| 8735 | |
| 8736 | \a fieldWidth specifies the minimum amount of space that \a a is |
| 8737 | padded to and filled with the character \a fillChar. A positive |
| 8738 | value produces right-aligned text; a negative value produces |
| 8739 | left-aligned text. |
| 8740 | |
| 8741 | The '%' can be followed by an 'L', in which case the sequence is |
| 8742 | replaced with a localized representation of \a a. The conversion |
| 8743 | uses the default locale, set by QLocale::setDefault(). If no default |
| 8744 | locale was specified, the "C" locale is used. The 'L' flag is |
| 8745 | ignored if \a base is not 10. |
| 8746 | |
| 8747 | \snippet qstring/main.cpp 12 |
| 8748 | \snippet qstring/main.cpp 14 |
| 8749 | |
| 8750 | If \a fillChar is '0' (the number 0, ASCII 48), the locale's zero is |
| 8751 | used. For negative numbers, zero padding might appear before the |
| 8752 | minus sign. |
| 8753 | */ |
| 8754 | |
| 8755 | /*! \fn QString QString::arg(uint a, int fieldWidth, int base, QChar fillChar) const |
| 8756 | \overload arg() |
| 8757 | |
| 8758 | The \a base argument specifies the base to use when converting the |
| 8759 | integer \a a into a string. The base must be between 2 and 36. |
| 8760 | |
| 8761 | If \a fillChar is '0' (the number 0, ASCII 48), the locale's zero is |
| 8762 | used. For negative numbers, zero padding might appear before the |
| 8763 | minus sign. |
| 8764 | */ |
| 8765 | |
| 8766 | /*! \fn QString QString::arg(long a, int fieldWidth, int base, QChar fillChar) const |
| 8767 | \overload arg() |
| 8768 | |
| 8769 | \a fieldWidth specifies the minimum amount of space that \a a is |
| 8770 | padded to and filled with the character \a fillChar. A positive |
| 8771 | value produces right-aligned text; a negative value produces |
| 8772 | left-aligned text. |
| 8773 | |
| 8774 | The \a a argument is expressed in the given \a base, which is 10 by |
| 8775 | default and must be between 2 and 36. |
| 8776 | |
| 8777 | The '%' can be followed by an 'L', in which case the sequence is |
| 8778 | replaced with a localized representation of \a a. The conversion |
| 8779 | uses the default locale. The default locale is determined from the |
| 8780 | system's locale settings at application startup. It can be changed |
| 8781 | using QLocale::setDefault(). The 'L' flag is ignored if \a base is |
| 8782 | not 10. |
| 8783 | |
| 8784 | \snippet qstring/main.cpp 12 |
| 8785 | \snippet qstring/main.cpp 14 |
| 8786 | |
| 8787 | If \a fillChar is '0' (the number 0, ASCII 48), the locale's zero is |
| 8788 | used. For negative numbers, zero padding might appear before the |
| 8789 | minus sign. |
| 8790 | */ |
| 8791 | |
| 8792 | /*! \fn QString QString::arg(ulong a, int fieldWidth, int base, QChar fillChar) const |
| 8793 | \overload arg() |
| 8794 | |
| 8795 | \a fieldWidth specifies the minimum amount of space that \a a is |
| 8796 | padded to and filled with the character \a fillChar. A positive |
| 8797 | value produces right-aligned text; a negative value produces |
| 8798 | left-aligned text. |
| 8799 | |
| 8800 | The \a base argument specifies the base to use when converting the |
| 8801 | integer \a a to a string. The base must be between 2 and 36, with 8 |
| 8802 | giving octal, 10 decimal, and 16 hexadecimal numbers. |
| 8803 | |
| 8804 | If \a fillChar is '0' (the number 0, ASCII 48), the locale's zero is |
| 8805 | used. For negative numbers, zero padding might appear before the |
| 8806 | minus sign. |
| 8807 | */ |
| 8808 | |
| 8809 | /*! |
| 8810 | \overload arg() |
| 8811 | |
| 8812 | \a fieldWidth specifies the minimum amount of space that \a a is |
| 8813 | padded to and filled with the character \a fillChar. A positive |
| 8814 | value produces right-aligned text; a negative value produces |
| 8815 | left-aligned text. |
| 8816 | |
| 8817 | The \a base argument specifies the base to use when converting the |
| 8818 | integer \a a into a string. The base must be between 2 and 36, with |
| 8819 | 8 giving octal, 10 decimal, and 16 hexadecimal numbers. |
| 8820 | |
| 8821 | If \a fillChar is '0' (the number 0, ASCII 48), the locale's zero is |
| 8822 | used. For negative numbers, zero padding might appear before the |
| 8823 | minus sign. |
| 8824 | */ |
| 8825 | QString QString::arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const |
| 8826 | { |
| 8827 | ArgEscapeData d = findArgEscapes(s: *this); |
| 8828 | |
| 8829 | if (d.occurrences == 0) { |
| 8830 | qWarning() << "QString::arg: Argument missing:" << *this << ',' << a; |
| 8831 | return *this; |
| 8832 | } |
| 8833 | |
| 8834 | unsigned flags = QLocaleData::NoFlags; |
| 8835 | if (fillChar == QLatin1Char('0')) |
| 8836 | flags = QLocaleData::ZeroPadded; |
| 8837 | |
| 8838 | QString arg; |
| 8839 | if (d.occurrences > d.locale_occurrences) |
| 8840 | arg = QLocaleData::c()->longLongToString(l: a, precision: -1, base, width: fieldWidth, flags); |
| 8841 | |
| 8842 | QString locale_arg; |
| 8843 | if (d.locale_occurrences > 0) { |
| 8844 | QLocale locale; |
| 8845 | if (!(locale.numberOptions() & QLocale::OmitGroupSeparator)) |
| 8846 | flags |= QLocaleData::ThousandsGroup; |
| 8847 | locale_arg = locale.d->m_data->longLongToString(l: a, precision: -1, base, width: fieldWidth, flags); |
| 8848 | } |
| 8849 | |
| 8850 | return replaceArgEscapes(s: *this, d, field_width: fieldWidth, arg, larg: locale_arg, fillChar); |
| 8851 | } |
| 8852 | |
| 8853 | /*! |
| 8854 | \overload arg() |
| 8855 | |
| 8856 | \a fieldWidth specifies the minimum amount of space that \a a is |
| 8857 | padded to and filled with the character \a fillChar. A positive |
| 8858 | value produces right-aligned text; a negative value produces |
| 8859 | left-aligned text. |
| 8860 | |
| 8861 | The \a base argument specifies the base to use when converting the |
| 8862 | integer \a a into a string. \a base must be between 2 and 36, with 8 |
| 8863 | giving octal, 10 decimal, and 16 hexadecimal numbers. |
| 8864 | |
| 8865 | If \a fillChar is '0' (the number 0, ASCII 48), the locale's zero is |
| 8866 | used. For negative numbers, zero padding might appear before the |
| 8867 | minus sign. |
| 8868 | */ |
| 8869 | QString QString::arg(qulonglong a, int fieldWidth, int base, QChar fillChar) const |
| 8870 | { |
| 8871 | ArgEscapeData d = findArgEscapes(s: *this); |
| 8872 | |
| 8873 | if (d.occurrences == 0) { |
| 8874 | qWarning() << "QString::arg: Argument missing:" << *this << ',' << a; |
| 8875 | return *this; |
| 8876 | } |
| 8877 | |
| 8878 | unsigned flags = QLocaleData::NoFlags; |
| 8879 | if (fillChar == QLatin1Char('0')) |
| 8880 | flags = QLocaleData::ZeroPadded; |
| 8881 | |
| 8882 | QString arg; |
| 8883 | if (d.occurrences > d.locale_occurrences) |
| 8884 | arg = QLocaleData::c()->unsLongLongToString(l: a, precision: -1, base, width: fieldWidth, flags); |
| 8885 | |
| 8886 | QString locale_arg; |
| 8887 | if (d.locale_occurrences > 0) { |
| 8888 | QLocale locale; |
| 8889 | if (!(locale.numberOptions() & QLocale::OmitGroupSeparator)) |
| 8890 | flags |= QLocaleData::ThousandsGroup; |
| 8891 | locale_arg = locale.d->m_data->unsLongLongToString(l: a, precision: -1, base, width: fieldWidth, flags); |
| 8892 | } |
| 8893 | |
| 8894 | return replaceArgEscapes(s: *this, d, field_width: fieldWidth, arg, larg: locale_arg, fillChar); |
| 8895 | } |
| 8896 | |
| 8897 | /*! |
| 8898 | \overload arg() |
| 8899 | |
| 8900 | \fn QString QString::arg(short a, int fieldWidth, int base, QChar fillChar) const |
| 8901 | |
| 8902 | \a fieldWidth specifies the minimum amount of space that \a a is |
| 8903 | padded to and filled with the character \a fillChar. A positive |
| 8904 | value produces right-aligned text; a negative value produces |
| 8905 | left-aligned text. |
| 8906 | |
| 8907 | The \a base argument specifies the base to use when converting the |
| 8908 | integer \a a into a string. The base must be between 2 and 36, with |
| 8909 | 8 giving octal, 10 decimal, and 16 hexadecimal numbers. |
| 8910 | |
| 8911 | If \a fillChar is '0' (the number 0, ASCII 48), the locale's zero is |
| 8912 | used. For negative numbers, zero padding might appear before the |
| 8913 | minus sign. |
| 8914 | */ |
| 8915 | |
| 8916 | /*! |
| 8917 | \fn QString QString::arg(ushort a, int fieldWidth, int base, QChar fillChar) const |
| 8918 | \overload arg() |
| 8919 | |
| 8920 | \a fieldWidth specifies the minimum amount of space that \a a is |
| 8921 | padded to and filled with the character \a fillChar. A positive |
| 8922 | value produces right-aligned text; a negative value produces |
| 8923 | left-aligned text. |
| 8924 | |
| 8925 | The \a base argument specifies the base to use when converting the |
| 8926 | integer \a a into a string. The base must be between 2 and 36, with |
| 8927 | 8 giving octal, 10 decimal, and 16 hexadecimal numbers. |
| 8928 | |
| 8929 | If \a fillChar is '0' (the number 0, ASCII 48), the locale's zero is |
| 8930 | used. For negative numbers, zero padding might appear before the |
| 8931 | minus sign. |
| 8932 | */ |
| 8933 | |
| 8934 | /*! |
| 8935 | \overload arg() |
| 8936 | */ |
| 8937 | QString QString::arg(QChar a, int fieldWidth, QChar fillChar) const |
| 8938 | { |
| 8939 | return arg(a: QStringView{&a, 1}, fieldWidth, fillChar); |
| 8940 | } |
| 8941 | |
| 8942 | /*! |
| 8943 | \overload arg() |
| 8944 | |
| 8945 | The \a a argument is interpreted as a Latin-1 character. |
| 8946 | */ |
| 8947 | QString QString::arg(char a, int fieldWidth, QChar fillChar) const |
| 8948 | { |
| 8949 | return arg(a: QLatin1Char(a), fieldWidth, fillChar); |
| 8950 | } |
| 8951 | |
| 8952 | /*! |
| 8953 | \fn QString QString::arg(double a, int fieldWidth, char format, int precision, QChar fillChar) const |
| 8954 | \overload arg() |
| 8955 | |
| 8956 | Argument \a a is formatted according to the specified \a format and |
| 8957 | \a precision. See \l{Argument Formats} for details. |
| 8958 | |
| 8959 | \a fieldWidth specifies the minimum amount of space that \a a is |
| 8960 | padded to and filled with the character \a fillChar. A positive |
| 8961 | value produces right-aligned text; a negative value produces |
| 8962 | left-aligned text. |
| 8963 | |
| 8964 | \snippet code/src_corelib_tools_qstring.cpp 2 |
| 8965 | |
| 8966 | The '%' can be followed by an 'L', in which case the sequence is |
| 8967 | replaced with a localized representation of \a a. The conversion |
| 8968 | uses the default locale, set by QLocale::setDefault(). If no |
| 8969 | default locale was specified, the "C" locale is used. |
| 8970 | |
| 8971 | If \a fillChar is '0' (the number 0, ASCII 48), this function will |
| 8972 | use the locale's zero to pad. For negative numbers, the zero padding |
| 8973 | will probably appear before the minus sign. |
| 8974 | |
| 8975 | \sa QLocale::toString() |
| 8976 | */ |
| 8977 | QString QString::arg(double a, int fieldWidth, char fmt, int prec, QChar fillChar) const |
| 8978 | { |
| 8979 | ArgEscapeData d = findArgEscapes(s: *this); |
| 8980 | |
| 8981 | if (d.occurrences == 0) { |
| 8982 | qWarning(msg: "QString::arg: Argument missing: %s, %g" , toLocal8Bit().data(), a); |
| 8983 | return *this; |
| 8984 | } |
| 8985 | |
| 8986 | unsigned flags = QLocaleData::NoFlags; |
| 8987 | if (fillChar == QLatin1Char('0')) |
| 8988 | flags |= QLocaleData::ZeroPadded; |
| 8989 | |
| 8990 | if (qIsUpper(ch: fmt)) |
| 8991 | flags |= QLocaleData::CapitalEorX; |
| 8992 | |
| 8993 | QLocaleData::DoubleForm form = QLocaleData::DFDecimal; |
| 8994 | switch (qToLower(ch: fmt)) { |
| 8995 | case 'f': |
| 8996 | form = QLocaleData::DFDecimal; |
| 8997 | break; |
| 8998 | case 'e': |
| 8999 | form = QLocaleData::DFExponent; |
| 9000 | break; |
| 9001 | case 'g': |
| 9002 | form = QLocaleData::DFSignificantDigits; |
| 9003 | break; |
| 9004 | default: |
| 9005 | #if defined(QT_CHECK_RANGE) |
| 9006 | qWarning("QString::arg: Invalid format char '%c'" , fmt); |
| 9007 | #endif |
| 9008 | break; |
| 9009 | } |
| 9010 | |
| 9011 | QString arg; |
| 9012 | if (d.occurrences > d.locale_occurrences) |
| 9013 | arg = QLocaleData::c()->doubleToString(d: a, precision: prec, form, width: fieldWidth, flags: flags | QLocaleData::ZeroPadExponent); |
| 9014 | |
| 9015 | QString locale_arg; |
| 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::ThousandsGroup; |
| 9022 | if (!(numberOptions & QLocale::OmitLeadingZeroInExponent)) |
| 9023 | flags |= QLocaleData::ZeroPadExponent; |
| 9024 | if (numberOptions & QLocale::IncludeTrailingZeroesAfterDot) |
| 9025 | flags |= QLocaleData::AddTrailingZeroes; |
| 9026 | locale_arg = locale.d->m_data->doubleToString(d: a, precision: prec, form, width: fieldWidth, flags); |
| 9027 | } |
| 9028 | |
| 9029 | return replaceArgEscapes(s: *this, d, field_width: fieldWidth, arg, larg: locale_arg, fillChar); |
| 9030 | } |
| 9031 | |
| 9032 | static inline ushort to_unicode(const QChar c) { return c.unicode(); } |
| 9033 | static inline ushort to_unicode(const char c) { return QLatin1Char{c}.unicode(); } |
| 9034 | |
| 9035 | template <typename Char> |
| 9036 | static int getEscape(const Char *uc, qsizetype *pos, qsizetype len, int maxNumber = 999) |
| 9037 | { |
| 9038 | int i = *pos; |
| 9039 | ++i; |
| 9040 | if (i < len && uc[i] == QLatin1Char('L')) |
| 9041 | ++i; |
| 9042 | if (i < len) { |
| 9043 | int escape = to_unicode(uc[i]) - '0'; |
| 9044 | if (uint(escape) >= 10U) |
| 9045 | return -1; |
| 9046 | ++i; |
| 9047 | while (i < len) { |
| 9048 | int digit = to_unicode(uc[i]) - '0'; |
| 9049 | if (uint(digit) >= 10U) |
| 9050 | break; |
| 9051 | escape = (escape * 10) + digit; |
| 9052 | ++i; |
| 9053 | } |
| 9054 | if (escape <= maxNumber) { |
| 9055 | *pos = i; |
| 9056 | return escape; |
| 9057 | } |
| 9058 | } |
| 9059 | return -1; |
| 9060 | } |
| 9061 | |
| 9062 | /* |
| 9063 | Algorithm for multiArg: |
| 9064 | |
| 9065 | 1. Parse the string as a sequence of verbatim text and placeholders (%L?\d{,3}). |
| 9066 | The L is parsed and accepted for compatibility with non-multi-arg, but since |
| 9067 | multiArg only accepts strings as replacements, the localization request can |
| 9068 | be safely ignored. |
| 9069 | 2. The result of step (1) is a list of (string-ref,int)-tuples. The string-ref |
| 9070 | either points at text to be copied verbatim (in which case the int is -1), |
| 9071 | or, initially, at the textual representation of the placeholder. In that case, |
| 9072 | the int contains the numerical number as parsed from the placeholder. |
| 9073 | 3. Next, collect all the non-negative ints found, sort them in ascending order and |
| 9074 | remove duplicates. |
| 9075 | 3a. If the result has more entires than multiArg() was given replacement strings, |
| 9076 | we have found placeholders we can't satisfy with replacement strings. That is |
| 9077 | fine (there could be another .arg() call coming after this one), so just |
| 9078 | truncate the result to the number of actual multiArg() replacement strings. |
| 9079 | 3b. If the result has less entries than multiArg() was given replacement strings, |
| 9080 | the string is missing placeholders. This is an error that the user should be |
| 9081 | warned about. |
| 9082 | 4. The result of step (3) is a mapping from the index of any replacement string to |
| 9083 | placeholder number. This is the wrong way around, but since placeholder |
| 9084 | numbers could get as large as 999, while we typically don't have more than 9 |
| 9085 | replacement strings, we trade 4K of sparsely-used memory for doing a reverse lookup |
| 9086 | each time we need to map a placeholder number to a replacement string index |
| 9087 | (that's a linear search; but still *much* faster than using an associative container). |
| 9088 | 5. Next, for each of the tuples found in step (1), do the following: |
| 9089 | 5a. If the int is negative, do nothing. |
| 9090 | 5b. Otherwise, if the int is found in the result of step (3) at index I, replace |
| 9091 | the string-ref with a string-ref for the (complete) I'th replacement string. |
| 9092 | 5c. Otherwise, do nothing. |
| 9093 | 6. Concatenate all string refs into a single result string. |
| 9094 | */ |
| 9095 | |
| 9096 | namespace { |
| 9097 | struct Part |
| 9098 | { |
| 9099 | Part() = default; // for QVarLengthArray; do not use |
| 9100 | Q_DECL_CONSTEXPR Part(QStringView s, int num = -1) |
| 9101 | : tag{QtPrivate::ArgBase::U16}, number{num}, data{s.utf16()}, size{s.size()} {} |
| 9102 | Q_DECL_CONSTEXPR Part(QLatin1String s, int num = -1) |
| 9103 | : tag{QtPrivate::ArgBase::L1}, number{num}, data{s.data()}, size{s.size()} {} |
| 9104 | |
| 9105 | void reset(QStringView s) noexcept { *this = {s, number}; } |
| 9106 | void reset(QLatin1String s) noexcept { *this = {s, number}; } |
| 9107 | |
| 9108 | QtPrivate::ArgBase::Tag tag; |
| 9109 | int number; |
| 9110 | const void *data; |
| 9111 | qsizetype size; |
| 9112 | }; |
| 9113 | } // unnamed namespace |
| 9114 | |
| 9115 | Q_DECLARE_TYPEINFO(Part, Q_PRIMITIVE_TYPE); |
| 9116 | |
| 9117 | namespace { |
| 9118 | |
| 9119 | enum { ExpectedParts = 32 }; |
| 9120 | |
| 9121 | typedef QVarLengthArray<Part, ExpectedParts> ParseResult; |
| 9122 | typedef QVarLengthArray<int, ExpectedParts/2> ArgIndexToPlaceholderMap; |
| 9123 | |
| 9124 | template <typename StringView> |
| 9125 | static ParseResult parseMultiArgFormatString(StringView s) |
| 9126 | { |
| 9127 | ParseResult result; |
| 9128 | |
| 9129 | const auto uc = s.data(); |
| 9130 | const auto len = s.size(); |
| 9131 | const auto end = len - 1; |
| 9132 | qsizetype i = 0; |
| 9133 | qsizetype last = 0; |
| 9134 | |
| 9135 | while (i < end) { |
| 9136 | if (uc[i] == QLatin1Char('%')) { |
| 9137 | qsizetype percent = i; |
| 9138 | int number = getEscape(uc, &i, len); |
| 9139 | if (number != -1) { |
| 9140 | if (last != percent) |
| 9141 | result.push_back(t: Part{s.mid(last, percent - last)}); // literal text (incl. failed placeholders) |
| 9142 | result.push_back(t: Part{s.mid(percent, i - percent), number}); // parsed placeholder |
| 9143 | last = i; |
| 9144 | continue; |
| 9145 | } |
| 9146 | } |
| 9147 | ++i; |
| 9148 | } |
| 9149 | |
| 9150 | if (last < len) |
| 9151 | result.push_back(t: Part{s.mid(last, len - last)}); // trailing literal text |
| 9152 | |
| 9153 | return result; |
| 9154 | } |
| 9155 | |
| 9156 | static ArgIndexToPlaceholderMap makeArgIndexToPlaceholderMap(const ParseResult &parts) |
| 9157 | { |
| 9158 | ArgIndexToPlaceholderMap result; |
| 9159 | |
| 9160 | for (Part part : parts) { |
| 9161 | if (part.number >= 0) |
| 9162 | result.push_back(t: part.number); |
| 9163 | } |
| 9164 | |
| 9165 | std::sort(first: result.begin(), last: result.end()); |
| 9166 | result.erase(abegin: std::unique(first: result.begin(), last: result.end()), |
| 9167 | aend: result.end()); |
| 9168 | |
| 9169 | return result; |
| 9170 | } |
| 9171 | |
| 9172 | static qsizetype resolveStringRefsAndReturnTotalSize(ParseResult &parts, const ArgIndexToPlaceholderMap &argIndexToPlaceholderMap, const QtPrivate::ArgBase *args[]) |
| 9173 | { |
| 9174 | using namespace QtPrivate; |
| 9175 | qsizetype totalSize = 0; |
| 9176 | for (Part &part : parts) { |
| 9177 | if (part.number != -1) { |
| 9178 | const auto it = std::find(first: argIndexToPlaceholderMap.begin(), last: argIndexToPlaceholderMap.end(), val: part.number); |
| 9179 | if (it != argIndexToPlaceholderMap.end()) { |
| 9180 | const auto &arg = *args[it - argIndexToPlaceholderMap.begin()]; |
| 9181 | switch (arg.tag) { |
| 9182 | case ArgBase::L1: |
| 9183 | part.reset(s: static_cast<const QLatin1StringArg&>(arg).string); |
| 9184 | break; |
| 9185 | case ArgBase::U8: |
| 9186 | Q_UNREACHABLE(); // waiting for QUtf8String... |
| 9187 | break; |
| 9188 | case ArgBase::U16: |
| 9189 | part.reset(s: static_cast<const QStringViewArg&>(arg).string); |
| 9190 | break; |
| 9191 | } |
| 9192 | } |
| 9193 | } |
| 9194 | totalSize += part.size; |
| 9195 | } |
| 9196 | return totalSize; |
| 9197 | } |
| 9198 | |
| 9199 | } // unnamed namespace |
| 9200 | |
| 9201 | #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) |
| 9202 | QString QString::multiArg(int numArgs, const QString **args) const |
| 9203 | { |
| 9204 | QVarLengthArray<QtPrivate::QStringViewArg, 9> sva; |
| 9205 | sva.reserve(asize: numArgs); |
| 9206 | QVarLengthArray<const QtPrivate::ArgBase *, 9> pointers; |
| 9207 | pointers.reserve(asize: numArgs); |
| 9208 | for (int i = 0; i < numArgs; ++i) { |
| 9209 | sva.push_back(t: QtPrivate::qStringLikeToArg(s: *args[i])); |
| 9210 | pointers.push_back(t: &sva.back()); |
| 9211 | } |
| 9212 | return QtPrivate::argToQString(pattern: qToStringViewIgnoringNull(s: *this), n: static_cast<size_t>(numArgs), args: pointers.data()); |
| 9213 | } |
| 9214 | #endif |
| 9215 | |
| 9216 | Q_ALWAYS_INLINE QString to_string(QLatin1String s) noexcept { return s; } |
| 9217 | Q_ALWAYS_INLINE QString to_string(QStringView s) noexcept { return s.toString(); } |
| 9218 | |
| 9219 | template <typename StringView> |
| 9220 | static QString argToQStringImpl(StringView pattern, size_t numArgs, const QtPrivate::ArgBase **args) |
| 9221 | { |
| 9222 | // Step 1-2 above |
| 9223 | ParseResult parts = parseMultiArgFormatString(pattern); |
| 9224 | |
| 9225 | // 3-4 |
| 9226 | ArgIndexToPlaceholderMap argIndexToPlaceholderMap = makeArgIndexToPlaceholderMap(parts); |
| 9227 | |
| 9228 | if (static_cast<size_t>(argIndexToPlaceholderMap.size()) > numArgs) // 3a |
| 9229 | argIndexToPlaceholderMap.resize(asize: int(numArgs)); |
| 9230 | else if (Q_UNLIKELY(static_cast<size_t>(argIndexToPlaceholderMap.size()) < numArgs)) // 3b |
| 9231 | qWarning(msg: "QString::arg: %d argument(s) missing in %ls" , |
| 9232 | int(numArgs - argIndexToPlaceholderMap.size()), qUtf16Printable(to_string(pattern))); |
| 9233 | |
| 9234 | // 5 |
| 9235 | const qsizetype totalSize = resolveStringRefsAndReturnTotalSize(parts, argIndexToPlaceholderMap, args); |
| 9236 | |
| 9237 | // 6: |
| 9238 | QString result(totalSize, Qt::Uninitialized); |
| 9239 | auto out = const_cast<QChar*>(result.constData()); |
| 9240 | |
| 9241 | for (Part part : parts) { |
| 9242 | switch (part.tag) { |
| 9243 | case QtPrivate::ArgBase::L1: |
| 9244 | if (part.size) { |
| 9245 | qt_from_latin1(dst: reinterpret_cast<ushort*>(out), |
| 9246 | str: reinterpret_cast<const char*>(part.data), size: part.size); |
| 9247 | } |
| 9248 | break; |
| 9249 | case QtPrivate::ArgBase::U8: |
| 9250 | Q_UNREACHABLE(); // waiting for QUtf8String |
| 9251 | break; |
| 9252 | case QtPrivate::ArgBase::U16: |
| 9253 | if (part.size) |
| 9254 | memcpy(dest: out, src: part.data, n: part.size * sizeof(QChar)); |
| 9255 | break; |
| 9256 | } |
| 9257 | out += part.size; |
| 9258 | } |
| 9259 | |
| 9260 | return result; |
| 9261 | } |
| 9262 | |
| 9263 | QString QtPrivate::argToQString(QStringView pattern, size_t n, const ArgBase **args) |
| 9264 | { |
| 9265 | return argToQStringImpl(pattern, numArgs: n, args); |
| 9266 | } |
| 9267 | |
| 9268 | QString QtPrivate::argToQString(QLatin1String pattern, size_t n, const ArgBase **args) |
| 9269 | { |
| 9270 | return argToQStringImpl(pattern, numArgs: n, args); |
| 9271 | } |
| 9272 | |
| 9273 | /*! \fn bool QString::isSimpleText() const |
| 9274 | |
| 9275 | \internal |
| 9276 | */ |
| 9277 | bool QString::isSimpleText() const |
| 9278 | { |
| 9279 | const ushort *p = d->data(); |
| 9280 | const ushort * const end = p + d->size; |
| 9281 | while (p < end) { |
| 9282 | ushort uc = *p; |
| 9283 | // sort out regions of complex text formatting |
| 9284 | if (uc > 0x058f && (uc < 0x1100 || uc > 0xfb0f)) { |
| 9285 | return false; |
| 9286 | } |
| 9287 | p++; |
| 9288 | } |
| 9289 | |
| 9290 | return true; |
| 9291 | } |
| 9292 | |
| 9293 | /*! \fn bool QString::isRightToLeft() const |
| 9294 | |
| 9295 | Returns \c true if the string is read right to left. |
| 9296 | |
| 9297 | \sa QStringRef::isRightToLeft() |
| 9298 | */ |
| 9299 | bool QString::isRightToLeft() const |
| 9300 | { |
| 9301 | return QtPrivate::isRightToLeft(string: QStringView(*this)); |
| 9302 | } |
| 9303 | |
| 9304 | /*! |
| 9305 | \fn bool QString::isValidUtf16() const noexcept |
| 9306 | \since 5.15 |
| 9307 | |
| 9308 | Returns \c true if the string contains valid UTF-16 encoded data, |
| 9309 | or \c false otherwise. |
| 9310 | |
| 9311 | Note that this function does not perform any special validation of the |
| 9312 | data; it merely checks if it can be successfully decoded from UTF-16. |
| 9313 | The data is assumed to be in host byte order; the presence of a BOM |
| 9314 | is meaningless. |
| 9315 | |
| 9316 | \sa QStringView::isValidUtf16() |
| 9317 | */ |
| 9318 | |
| 9319 | /*! \fn QChar *QString::data() |
| 9320 | |
| 9321 | Returns a pointer to the data stored in the QString. The pointer |
| 9322 | can be used to access and modify the characters that compose the |
| 9323 | string. |
| 9324 | |
| 9325 | Unlike constData() and unicode(), the returned data is always |
| 9326 | '\\0'-terminated. |
| 9327 | |
| 9328 | Example: |
| 9329 | |
| 9330 | \snippet qstring/main.cpp 19 |
| 9331 | |
| 9332 | Note that the pointer remains valid only as long as the string is |
| 9333 | not modified by other means. For read-only access, constData() is |
| 9334 | faster because it never causes a \l{deep copy} to occur. |
| 9335 | |
| 9336 | \sa constData(), operator[]() |
| 9337 | */ |
| 9338 | |
| 9339 | /*! \fn const QChar *QString::data() const |
| 9340 | |
| 9341 | \overload |
| 9342 | |
| 9343 | \note The returned string may not be '\\0'-terminated. |
| 9344 | Use size() to determine the length of the array. |
| 9345 | |
| 9346 | \sa fromRawData() |
| 9347 | */ |
| 9348 | |
| 9349 | /*! \fn const QChar *QString::constData() const |
| 9350 | |
| 9351 | Returns a pointer to the data stored in the QString. The pointer |
| 9352 | can be used to access the characters that compose the string. |
| 9353 | |
| 9354 | Note that the pointer remains valid only as long as the string is |
| 9355 | not modified. |
| 9356 | |
| 9357 | \note The returned string may not be '\\0'-terminated. |
| 9358 | Use size() to determine the length of the array. |
| 9359 | |
| 9360 | \sa data(), operator[](), fromRawData() |
| 9361 | */ |
| 9362 | |
| 9363 | /*! \fn void QString::push_front(const QString &other) |
| 9364 | |
| 9365 | This function is provided for STL compatibility, prepending the |
| 9366 | given \a other string to the beginning of this string. It is |
| 9367 | equivalent to \c prepend(other). |
| 9368 | |
| 9369 | \sa prepend() |
| 9370 | */ |
| 9371 | |
| 9372 | /*! \fn void QString::push_front(QChar ch) |
| 9373 | |
| 9374 | \overload |
| 9375 | |
| 9376 | Prepends the given \a ch character to the beginning of this string. |
| 9377 | */ |
| 9378 | |
| 9379 | /*! \fn void QString::push_back(const QString &other) |
| 9380 | |
| 9381 | This function is provided for STL compatibility, appending the |
| 9382 | given \a other string onto the end of this string. It is |
| 9383 | equivalent to \c append(other). |
| 9384 | |
| 9385 | \sa append() |
| 9386 | */ |
| 9387 | |
| 9388 | /*! \fn void QString::push_back(QChar ch) |
| 9389 | |
| 9390 | \overload |
| 9391 | |
| 9392 | Appends the given \a ch character onto the end of this string. |
| 9393 | */ |
| 9394 | |
| 9395 | /*! \fn void QString::shrink_to_fit() |
| 9396 | \since 5.10 |
| 9397 | |
| 9398 | This function is provided for STL compatibility. It is |
| 9399 | equivalent to squeeze(). |
| 9400 | |
| 9401 | \sa squeeze() |
| 9402 | */ |
| 9403 | |
| 9404 | /*! |
| 9405 | \fn std::string QString::toStdString() const |
| 9406 | |
| 9407 | Returns a std::string object with the data contained in this |
| 9408 | QString. The Unicode data is converted into 8-bit characters using |
| 9409 | the toUtf8() function. |
| 9410 | |
| 9411 | This method is mostly useful to pass a QString to a function |
| 9412 | that accepts a std::string object. |
| 9413 | |
| 9414 | \sa toLatin1(), toUtf8(), toLocal8Bit(), QByteArray::toStdString() |
| 9415 | */ |
| 9416 | |
| 9417 | /*! |
| 9418 | Constructs a QString that uses the first \a size Unicode characters |
| 9419 | in the array \a unicode. The data in \a unicode is \e not |
| 9420 | copied. The caller must be able to guarantee that \a unicode will |
| 9421 | not be deleted or modified as long as the QString (or an |
| 9422 | unmodified copy of it) exists. |
| 9423 | |
| 9424 | Any attempts to modify the QString or copies of it will cause it |
| 9425 | to create a deep copy of the data, ensuring that the raw data |
| 9426 | isn't modified. |
| 9427 | |
| 9428 | Here is an example of how we can use a QRegularExpression on raw data in |
| 9429 | memory without requiring to copy the data into a QString: |
| 9430 | |
| 9431 | \snippet qstring/main.cpp 22 |
| 9432 | \snippet qstring/main.cpp 23 |
| 9433 | |
| 9434 | \warning A string created with fromRawData() is \e not |
| 9435 | '\\0'-terminated, unless the raw data contains a '\\0' character |
| 9436 | at position \a size. This means unicode() will \e not return a |
| 9437 | '\\0'-terminated string (although utf16() does, at the cost of |
| 9438 | copying the raw data). |
| 9439 | |
| 9440 | \sa fromUtf16(), setRawData() |
| 9441 | */ |
| 9442 | QString QString::fromRawData(const QChar *unicode, int size) |
| 9443 | { |
| 9444 | Data *x; |
| 9445 | if (!unicode) { |
| 9446 | x = Data::sharedNull(); |
| 9447 | } else if (!size) { |
| 9448 | x = Data::allocate(capacity: 0); |
| 9449 | } else { |
| 9450 | x = Data::fromRawData(data: reinterpret_cast<const ushort *>(unicode), n: size); |
| 9451 | Q_CHECK_PTR(x); |
| 9452 | } |
| 9453 | QStringDataPtr dataPtr = { .ptr: x }; |
| 9454 | return QString(dataPtr); |
| 9455 | } |
| 9456 | |
| 9457 | /*! |
| 9458 | \since 4.7 |
| 9459 | |
| 9460 | Resets the QString to use the first \a size Unicode characters |
| 9461 | in the array \a unicode. The data in \a unicode is \e not |
| 9462 | copied. The caller must be able to guarantee that \a unicode will |
| 9463 | not be deleted or modified as long as the QString (or an |
| 9464 | unmodified copy of it) exists. |
| 9465 | |
| 9466 | This function can be used instead of fromRawData() to re-use |
| 9467 | existings QString objects to save memory re-allocations. |
| 9468 | |
| 9469 | \sa fromRawData() |
| 9470 | */ |
| 9471 | QString &QString::setRawData(const QChar *unicode, int size) |
| 9472 | { |
| 9473 | if (d->ref.isShared() || d->alloc) { |
| 9474 | *this = fromRawData(unicode, size); |
| 9475 | } else { |
| 9476 | if (unicode) { |
| 9477 | d->size = size; |
| 9478 | d->offset = reinterpret_cast<const char *>(unicode) - reinterpret_cast<char *>(d); |
| 9479 | } else { |
| 9480 | d->offset = sizeof(QStringData); |
| 9481 | d->size = 0; |
| 9482 | } |
| 9483 | } |
| 9484 | return *this; |
| 9485 | } |
| 9486 | |
| 9487 | /*! \fn QString QString::fromStdU16String(const std::u16string &str) |
| 9488 | \since 5.5 |
| 9489 | |
| 9490 | Returns a copy of the \a str string. The given string is assumed |
| 9491 | to be encoded in UTF-16. |
| 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 | Returns a copy of the \a str string. The given string is assumed |
| 9511 | to be encoded in UCS-4. |
| 9512 | |
| 9513 | \sa fromUcs4(), fromStdWString(), fromStdU16String() |
| 9514 | */ |
| 9515 | |
| 9516 | /*! |
| 9517 | \fn std::u32string QString::toStdU32String() const |
| 9518 | \since 5.5 |
| 9519 | |
| 9520 | Returns a std::u32string object with the data contained in this |
| 9521 | QString. The Unicode data is the same as returned by the toUcs4() |
| 9522 | method. |
| 9523 | |
| 9524 | \sa toUcs4(), toStdWString(), toStdU16String() |
| 9525 | */ |
| 9526 | |
| 9527 | /*! \class QLatin1String |
| 9528 | \inmodule QtCore |
| 9529 | \brief The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal. |
| 9530 | |
| 9531 | \ingroup string-processing |
| 9532 | \reentrant |
| 9533 | |
| 9534 | Many of QString's member functions are overloaded to accept |
| 9535 | \c{const char *} instead of QString. This includes the copy |
| 9536 | constructor, the assignment operator, the comparison operators, |
| 9537 | and various other functions such as \l{QString::insert()}{insert()}, \l{QString::replace()}{replace()}, |
| 9538 | and \l{QString::indexOf()}{indexOf()}. These functions |
| 9539 | are usually optimized to avoid constructing a QString object for |
| 9540 | the \c{const char *} data. For example, assuming \c str is a |
| 9541 | QString, |
| 9542 | |
| 9543 | \snippet code/src_corelib_tools_qstring.cpp 3 |
| 9544 | |
| 9545 | is much faster than |
| 9546 | |
| 9547 | \snippet code/src_corelib_tools_qstring.cpp 4 |
| 9548 | |
| 9549 | because it doesn't construct four temporary QString objects and |
| 9550 | make a deep copy of the character data. |
| 9551 | |
| 9552 | Applications that define \c QT_NO_CAST_FROM_ASCII (as explained |
| 9553 | in the QString documentation) don't have access to QString's |
| 9554 | \c{const char *} API. To provide an efficient way of specifying |
| 9555 | constant Latin-1 strings, Qt provides the QLatin1String, which is |
| 9556 | just a very thin wrapper around a \c{const char *}. Using |
| 9557 | QLatin1String, the example code above becomes |
| 9558 | |
| 9559 | \snippet code/src_corelib_tools_qstring.cpp 5 |
| 9560 | |
| 9561 | This is a bit longer to type, but it provides exactly the same |
| 9562 | benefits as the first version of the code, and is faster than |
| 9563 | converting the Latin-1 strings using QString::fromLatin1(). |
| 9564 | |
| 9565 | Thanks to the QString(QLatin1String) constructor, |
| 9566 | QLatin1String can be used everywhere a QString is expected. For |
| 9567 | example: |
| 9568 | |
| 9569 | \snippet code/src_corelib_tools_qstring.cpp 6 |
| 9570 | |
| 9571 | \note If the function you're calling with a QLatin1String |
| 9572 | argument isn't actually overloaded to take QLatin1String, the |
| 9573 | implicit conversion to QString will trigger a memory allocation, |
| 9574 | which is usually what you want to avoid by using QLatin1String |
| 9575 | in the first place. In those cases, using QStringLiteral may be |
| 9576 | the better option. |
| 9577 | |
| 9578 | \sa QString, QLatin1Char, {QStringLiteral()}{QStringLiteral}, QT_NO_CAST_FROM_ASCII |
| 9579 | */ |
| 9580 | |
| 9581 | /*! |
| 9582 | \typedef QLatin1String::value_type |
| 9583 | \since 5.10 |
| 9584 | |
| 9585 | Alias for \c{const char}. Provided for compatibility with the STL. |
| 9586 | */ |
| 9587 | |
| 9588 | /*! |
| 9589 | \typedef QLatin1String::difference_type |
| 9590 | \since 5.10 |
| 9591 | |
| 9592 | Alias for \c{int}. Provided for compatibility with the STL. |
| 9593 | */ |
| 9594 | |
| 9595 | /*! |
| 9596 | \typedef QLatin1String::size_type |
| 9597 | \since 5.10 |
| 9598 | |
| 9599 | Alias for \c{int}. Provided for compatibility with the STL. |
| 9600 | */ |
| 9601 | |
| 9602 | /*! |
| 9603 | \typedef QLatin1String::reference |
| 9604 | \since 5.10 |
| 9605 | |
| 9606 | Alias for \c{value_type &}. Provided for compatibility with the STL. |
| 9607 | */ |
| 9608 | |
| 9609 | /*! |
| 9610 | \typedef QLatin1String::const_reference |
| 9611 | \since 5.11 |
| 9612 | |
| 9613 | Alias for \c{reference}. Provided for compatibility with the STL. |
| 9614 | */ |
| 9615 | |
| 9616 | /*! |
| 9617 | \typedef QLatin1String::iterator |
| 9618 | \since 5.10 |
| 9619 | |
| 9620 | QLatin1String does not support mutable iterators, so this is the same |
| 9621 | as const_iterator. |
| 9622 | |
| 9623 | \sa const_iterator, reverse_iterator |
| 9624 | */ |
| 9625 | |
| 9626 | /*! |
| 9627 | \typedef QLatin1String::const_iterator |
| 9628 | \since 5.10 |
| 9629 | |
| 9630 | \sa iterator, const_reverse_iterator |
| 9631 | */ |
| 9632 | |
| 9633 | /*! |
| 9634 | \typedef QLatin1String::reverse_iterator |
| 9635 | \since 5.10 |
| 9636 | |
| 9637 | QLatin1String does not support mutable reverse iterators, so this is the |
| 9638 | same as const_reverse_iterator. |
| 9639 | |
| 9640 | \sa const_reverse_iterator, iterator |
| 9641 | */ |
| 9642 | |
| 9643 | /*! |
| 9644 | \typedef QLatin1String::const_reverse_iterator |
| 9645 | \since 5.10 |
| 9646 | |
| 9647 | \sa reverse_iterator, const_iterator |
| 9648 | */ |
| 9649 | |
| 9650 | /*! \fn QLatin1String::QLatin1String() |
| 9651 | \since 5.6 |
| 9652 | |
| 9653 | Constructs a QLatin1String object that stores a nullptr. |
| 9654 | */ |
| 9655 | |
| 9656 | /*! \fn QLatin1String::QLatin1String(const char *str) |
| 9657 | |
| 9658 | Constructs a QLatin1String object that stores \a str. |
| 9659 | |
| 9660 | The string data is \e not copied. The caller must be able to |
| 9661 | guarantee that \a str will not be deleted or modified as long as |
| 9662 | the QLatin1String object exists. |
| 9663 | |
| 9664 | \sa latin1() |
| 9665 | */ |
| 9666 | |
| 9667 | /*! \fn QLatin1String::QLatin1String(const char *str, int size) |
| 9668 | |
| 9669 | Constructs a QLatin1String object that stores \a str with \a size. |
| 9670 | |
| 9671 | The string data is \e not copied. The caller must be able to |
| 9672 | guarantee that \a str will not be deleted or modified as long as |
| 9673 | the QLatin1String object exists. |
| 9674 | |
| 9675 | \sa latin1() |
| 9676 | */ |
| 9677 | |
| 9678 | /*! |
| 9679 | \fn QLatin1String::QLatin1String(const char *first, const char *last) |
| 9680 | \since 5.10 |
| 9681 | |
| 9682 | Constructs a QLatin1String object that stores \a first with length |
| 9683 | (\a last - \a first). |
| 9684 | |
| 9685 | The range \c{[first,last)} must remain valid for the lifetime of |
| 9686 | this Latin-1 string object. |
| 9687 | |
| 9688 | Passing \nullptr as \a first is safe if \a last is \nullptr, |
| 9689 | too, and results in a null Latin-1 string. |
| 9690 | |
| 9691 | The behavior is undefined if \a last precedes \a first, \a first |
| 9692 | is \nullptr and \a last is not, or if \c{last - first > |
| 9693 | INT_MAX}. |
| 9694 | */ |
| 9695 | |
| 9696 | /*! \fn QLatin1String::QLatin1String(const QByteArray &str) |
| 9697 | |
| 9698 | Constructs a QLatin1String object that stores \a str. |
| 9699 | |
| 9700 | The string data is \e not copied. The caller must be able to |
| 9701 | guarantee that \a str will not be deleted or modified as long as |
| 9702 | the QLatin1String object exists. |
| 9703 | |
| 9704 | \sa latin1() |
| 9705 | */ |
| 9706 | |
| 9707 | /*! \fn const char *QLatin1String::latin1() const |
| 9708 | |
| 9709 | Returns the Latin-1 string stored in this object. |
| 9710 | */ |
| 9711 | |
| 9712 | /*! \fn const char *QLatin1String::data() const |
| 9713 | |
| 9714 | Returns the Latin-1 string stored in this object. |
| 9715 | */ |
| 9716 | |
| 9717 | /*! \fn int QLatin1String::size() const |
| 9718 | |
| 9719 | Returns the size of the Latin-1 string stored in this object. |
| 9720 | */ |
| 9721 | |
| 9722 | /*! \fn bool QLatin1String::isNull() const |
| 9723 | \since 5.10 |
| 9724 | |
| 9725 | Returns whether the Latin-1 string stored in this object is null |
| 9726 | (\c{data() == nullptr}) or not. |
| 9727 | |
| 9728 | \sa isEmpty(), data() |
| 9729 | */ |
| 9730 | |
| 9731 | /*! \fn bool QLatin1String::isEmpty() const |
| 9732 | \since 5.10 |
| 9733 | |
| 9734 | Returns whether the Latin-1 string stored in this object is empty |
| 9735 | (\c{size() == 0}) or not. |
| 9736 | |
| 9737 | \sa isNull(), size() |
| 9738 | */ |
| 9739 | |
| 9740 | /*! \fn QLatin1Char QLatin1String::at(int pos) const |
| 9741 | \since 5.8 |
| 9742 | |
| 9743 | Returns the character at position \a pos in this object. |
| 9744 | |
| 9745 | \note This function performs no error checking. |
| 9746 | The behavior is undefined when \a pos < 0 or \a pos >= size(). |
| 9747 | |
| 9748 | \sa operator[]() |
| 9749 | */ |
| 9750 | |
| 9751 | /*! \fn QLatin1Char QLatin1String::operator[](int pos) const |
| 9752 | \since 5.8 |
| 9753 | |
| 9754 | Returns the character at position \a pos in this object. |
| 9755 | |
| 9756 | \note This function performs no error checking. |
| 9757 | The behavior is undefined when \a pos < 0 or \a pos >= size(). |
| 9758 | |
| 9759 | \sa at() |
| 9760 | */ |
| 9761 | |
| 9762 | /*! |
| 9763 | \fn QLatin1Char QLatin1String::front() const |
| 9764 | \since 5.10 |
| 9765 | |
| 9766 | Returns the first character in the string. |
| 9767 | Same as \c{at(0)}. |
| 9768 | |
| 9769 | This function is provided for STL compatibility. |
| 9770 | |
| 9771 | \warning Calling this function on an empty string constitutes |
| 9772 | undefined behavior. |
| 9773 | |
| 9774 | \sa back(), at(), operator[]() |
| 9775 | */ |
| 9776 | |
| 9777 | /*! |
| 9778 | \fn QLatin1Char QLatin1String::back() const |
| 9779 | \since 5.10 |
| 9780 | |
| 9781 | Returns the last character in the string. |
| 9782 | Same as \c{at(size() - 1)}. |
| 9783 | |
| 9784 | This function is provided for STL compatibility. |
| 9785 | |
| 9786 | \warning Calling this function on an empty string constitutes |
| 9787 | undefined behavior. |
| 9788 | |
| 9789 | \sa front(), at(), operator[]() |
| 9790 | */ |
| 9791 | |
| 9792 | /*! |
| 9793 | \fn int QLatin1String::compare(QStringView str, Qt::CaseSensitivity cs) const |
| 9794 | \fn int QLatin1String::compare(QLatin1String l1, Qt::CaseSensitivity cs) const |
| 9795 | \fn int QLatin1String::compare(QChar ch) const |
| 9796 | \fn int QLatin1String::compare(QChar ch, Qt::CaseSensitivity cs) const |
| 9797 | \since 5.14 |
| 9798 | |
| 9799 | Returns an integer that compares to zero as this Latin-1 string compares to the |
| 9800 | string-view \a str, Latin-1 string \a l1, or character \a ch, respectively. |
| 9801 | |
| 9802 | If \a cs is Qt::CaseSensitive (the default), the comparison is case sensitive; |
| 9803 | otherwise the comparison is case-insensitive. |
| 9804 | |
| 9805 | \sa operator==(), operator<(), operator>() |
| 9806 | */ |
| 9807 | |
| 9808 | |
| 9809 | /*! |
| 9810 | \fn bool QLatin1String::startsWith(QStringView str, Qt::CaseSensitivity cs) const |
| 9811 | \since 5.10 |
| 9812 | \fn bool QLatin1String::startsWith(QLatin1String l1, Qt::CaseSensitivity cs) const |
| 9813 | \since 5.10 |
| 9814 | \fn bool QLatin1String::startsWith(QChar ch) const |
| 9815 | \since 5.10 |
| 9816 | \fn bool QLatin1String::startsWith(QChar ch, Qt::CaseSensitivity cs) const |
| 9817 | \since 5.10 |
| 9818 | |
| 9819 | Returns \c true if this Latin-1 string starts with string-view \a str, |
| 9820 | Latin-1 string \a l1, or character \a ch, respectively; |
| 9821 | otherwise returns \c false. |
| 9822 | |
| 9823 | If \a cs is Qt::CaseSensitive (the default), the search is case-sensitive; |
| 9824 | otherwise the search is case-insensitive. |
| 9825 | |
| 9826 | \sa endsWith() |
| 9827 | */ |
| 9828 | |
| 9829 | /*! |
| 9830 | \fn bool QLatin1String::endsWith(QStringView str, Qt::CaseSensitivity cs) const |
| 9831 | \since 5.10 |
| 9832 | \fn bool QLatin1String::endsWith(QLatin1String l1, Qt::CaseSensitivity cs) const |
| 9833 | \since 5.10 |
| 9834 | \fn bool QLatin1String::endsWith(QChar ch) const |
| 9835 | \since 5.10 |
| 9836 | \fn bool QLatin1String::endsWith(QChar ch, Qt::CaseSensitivity cs) const |
| 9837 | \since 5.10 |
| 9838 | |
| 9839 | Returns \c true if this Latin-1 string ends with string-view \a str, |
| 9840 | Latin-1 string \a l1, or character \a ch, respectively; |
| 9841 | otherwise returns \c false. |
| 9842 | |
| 9843 | If \a cs is Qt::CaseSensitive (the default), the search is case-sensitive; |
| 9844 | otherwise the search is case-insensitive. |
| 9845 | |
| 9846 | \sa startsWith() |
| 9847 | */ |
| 9848 | |
| 9849 | /*! |
| 9850 | \fn int QLatin1String::indexOf(QStringView str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
| 9851 | \fn int QLatin1String::indexOf(QLatin1String l1, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
| 9852 | \fn int QLatin1String::indexOf(QChar c, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
| 9853 | \since 5.14 |
| 9854 | |
| 9855 | Returns the index position of the first occurrence of the string-view \a str, |
| 9856 | Latin-1 string \a l1, or character \a ch, respectively, in this Latin-1 string, |
| 9857 | searching forward from index position \a from. Returns -1 if \a str is not found. |
| 9858 | |
| 9859 | If \a cs is Qt::CaseSensitive (default), the search is case |
| 9860 | sensitive; otherwise the search is case insensitive. |
| 9861 | |
| 9862 | If \a from is -1, the search starts at the last character; if it is |
| 9863 | -2, at the next to last character and so on. |
| 9864 | |
| 9865 | \sa QString::indexOf() |
| 9866 | */ |
| 9867 | |
| 9868 | /*! |
| 9869 | \fn bool QLatin1String::contains(QStringView str, Qt::CaseSensitivity cs) const |
| 9870 | \fn bool QLatin1String::contains(QLatin1String l1, Qt::CaseSensitivity cs) const |
| 9871 | \fn bool QLatin1String::contains(QChar c, Qt::CaseSensitivity cs) const |
| 9872 | \since 5.14 |
| 9873 | |
| 9874 | Returns \c true if this Latin-1 string contains an occurrence of the string-view |
| 9875 | \a str, Latin-1 string \a l1, or character \a ch; otherwise returns \c false. |
| 9876 | |
| 9877 | If \a cs is Qt::CaseSensitive (the default), the search is |
| 9878 | case-sensitive; otherwise the search is case-insensitive. |
| 9879 | |
| 9880 | \sa indexOf(), QStringView::contains(), QStringView::indexOf(), QString::indexOf() |
| 9881 | */ |
| 9882 | |
| 9883 | /*! |
| 9884 | \fn int QLatin1String::lastIndexOf(QStringView str, int from, Qt::CaseSensitivity cs) const |
| 9885 | \fn int QLatin1String::lastIndexOf(QLatin1String l1, int from, Qt::CaseSensitivity cs) const |
| 9886 | \fn int QLatin1String::lastIndexOf(QChar c, int from, Qt::CaseSensitivity cs) const |
| 9887 | \since 5.14 |
| 9888 | |
| 9889 | Returns the index position of the last occurrence of the string-view \a str, |
| 9890 | Latin-1 string \a l1, or character \a ch, respectively, in this Latin-1 string, |
| 9891 | searching backward from index position \a from. If \a from is -1 (default), |
| 9892 | the search starts at the last character; if \a from is -2, at the next to last |
| 9893 | character and so on. Returns -1 if \a str is not found. |
| 9894 | |
| 9895 | If \a cs is Qt::CaseSensitive (default), the search is case |
| 9896 | sensitive; otherwise the search is case insensitive. |
| 9897 | |
| 9898 | \sa indexOf(), QStringView::lastIndexOf(), QStringView::indexOf(), QString::indexOf() |
| 9899 | */ |
| 9900 | |
| 9901 | /*! |
| 9902 | \fn QLatin1String::const_iterator QLatin1String::begin() const |
| 9903 | \since 5.10 |
| 9904 | |
| 9905 | Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first character in |
| 9906 | the string. |
| 9907 | |
| 9908 | This function is provided for STL compatibility. |
| 9909 | |
| 9910 | \sa end(), cbegin(), rbegin(), data() |
| 9911 | */ |
| 9912 | |
| 9913 | /*! |
| 9914 | \fn QLatin1String::const_iterator QLatin1String::cbegin() const |
| 9915 | \since 5.10 |
| 9916 | |
| 9917 | Same as begin(). |
| 9918 | |
| 9919 | This function is provided for STL compatibility. |
| 9920 | |
| 9921 | \sa cend(), begin(), crbegin(), data() |
| 9922 | */ |
| 9923 | |
| 9924 | /*! |
| 9925 | \fn QLatin1String::const_iterator QLatin1String::end() const |
| 9926 | \since 5.10 |
| 9927 | |
| 9928 | Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary |
| 9929 | character after the last character in the list. |
| 9930 | |
| 9931 | This function is provided for STL compatibility. |
| 9932 | |
| 9933 | \sa begin(), cend(), rend() |
| 9934 | */ |
| 9935 | |
| 9936 | /*! \fn QLatin1String::const_iterator QLatin1String::cend() const |
| 9937 | \since 5.10 |
| 9938 | |
| 9939 | Same as end(). |
| 9940 | |
| 9941 | This function is provided for STL compatibility. |
| 9942 | |
| 9943 | \sa cbegin(), end(), crend() |
| 9944 | */ |
| 9945 | |
| 9946 | /*! |
| 9947 | \fn QLatin1String::const_reverse_iterator QLatin1String::rbegin() const |
| 9948 | \since 5.10 |
| 9949 | |
| 9950 | Returns a const \l{STL-style iterators}{STL-style} reverse iterator pointing to the first |
| 9951 | character in the string, in reverse order. |
| 9952 | |
| 9953 | This function is provided for STL compatibility. |
| 9954 | |
| 9955 | \sa rend(), crbegin(), begin() |
| 9956 | */ |
| 9957 | |
| 9958 | /*! |
| 9959 | \fn QLatin1String::const_reverse_iterator QLatin1String::crbegin() const |
| 9960 | \since 5.10 |
| 9961 | |
| 9962 | Same as rbegin(). |
| 9963 | |
| 9964 | This function is provided for STL compatibility. |
| 9965 | |
| 9966 | \sa crend(), rbegin(), cbegin() |
| 9967 | */ |
| 9968 | |
| 9969 | /*! |
| 9970 | \fn QLatin1String::const_reverse_iterator QLatin1String::rend() const |
| 9971 | \since 5.10 |
| 9972 | |
| 9973 | Returns a \l{STL-style iterators}{STL-style} reverse iterator pointing to one past |
| 9974 | the last character in the string, in reverse order. |
| 9975 | |
| 9976 | This function is provided for STL compatibility. |
| 9977 | |
| 9978 | \sa rbegin(), crend(), end() |
| 9979 | */ |
| 9980 | |
| 9981 | /*! |
| 9982 | \fn QLatin1String::const_reverse_iterator QLatin1String::crend() const |
| 9983 | \since 5.10 |
| 9984 | |
| 9985 | Same as rend(). |
| 9986 | |
| 9987 | This function is provided for STL compatibility. |
| 9988 | |
| 9989 | \sa crbegin(), rend(), cend() |
| 9990 | */ |
| 9991 | |
| 9992 | /*! \fn QLatin1String QLatin1String::mid(int start) const |
| 9993 | \since 5.8 |
| 9994 | |
| 9995 | Returns the substring starting at position \a start in this object, |
| 9996 | and extending to the end of the string. |
| 9997 | |
| 9998 | \note This function performs no error checking. |
| 9999 | The behavior is undefined when \a start < 0 or \a start > size(). |
| 10000 | |
| 10001 | \sa left(), right(), chopped(), chop(), truncate() |
| 10002 | */ |
| 10003 | |
| 10004 | /*! \fn QLatin1String QLatin1String::mid(int start, int length) const |
| 10005 | \since 5.8 |
| 10006 | \overload |
| 10007 | |
| 10008 | Returns the substring of length \a length starting at position |
| 10009 | \a start in this object. |
| 10010 | |
| 10011 | \note This function performs no error checking. |
| 10012 | The behavior is undefined when \a start < 0, \a length < 0, |
| 10013 | or \a start + \a length > size(). |
| 10014 | |
| 10015 | \sa left(), right(), chopped(), chop(), truncate() |
| 10016 | */ |
| 10017 | |
| 10018 | /*! \fn QLatin1String QLatin1String::left(int length) const |
| 10019 | \since 5.8 |
| 10020 | |
| 10021 | Returns the substring of length \a length starting at position |
| 10022 | 0 in this object. |
| 10023 | |
| 10024 | \note This function performs no error checking. |
| 10025 | The behavior is undefined when \a length < 0 or \a length > size(). |
| 10026 | |
| 10027 | \sa mid(), right(), chopped(), chop(), truncate() |
| 10028 | */ |
| 10029 | |
| 10030 | /*! \fn QLatin1String QLatin1String::right(int length) const |
| 10031 | \since 5.8 |
| 10032 | |
| 10033 | Returns the substring of length \a length starting at position |
| 10034 | size() - \a length in this object. |
| 10035 | |
| 10036 | \note This function performs no error checking. |
| 10037 | The behavior is undefined when \a length < 0 or \a length > size(). |
| 10038 | |
| 10039 | \sa mid(), left(), chopped(), chop(), truncate() |
| 10040 | */ |
| 10041 | |
| 10042 | /*! |
| 10043 | \fn QLatin1String QLatin1String::chopped(int length) const |
| 10044 | \since 5.10 |
| 10045 | |
| 10046 | Returns the substring of length size() - \a length starting at the |
| 10047 | beginning of this object. |
| 10048 | |
| 10049 | Same as \c{left(size() - length)}. |
| 10050 | |
| 10051 | \note The behavior is undefined when \a length < 0 or \a length > size(). |
| 10052 | |
| 10053 | \sa mid(), left(), right(), chop(), truncate() |
| 10054 | */ |
| 10055 | |
| 10056 | /*! |
| 10057 | \fn void QLatin1String::truncate(int length) |
| 10058 | \since 5.10 |
| 10059 | |
| 10060 | Truncates this string to length \a length. |
| 10061 | |
| 10062 | Same as \c{*this = left(length)}. |
| 10063 | |
| 10064 | \note The behavior is undefined when \a length < 0 or \a length > size(). |
| 10065 | |
| 10066 | \sa mid(), left(), right(), chopped(), chop() |
| 10067 | */ |
| 10068 | |
| 10069 | /*! |
| 10070 | \fn void QLatin1String::chop(int length) |
| 10071 | \since 5.10 |
| 10072 | |
| 10073 | Truncates this string by \a length characters. |
| 10074 | |
| 10075 | Same as \c{*this = left(size() - length)}. |
| 10076 | |
| 10077 | \note The behavior is undefined when \a length < 0 or \a length > size(). |
| 10078 | |
| 10079 | \sa mid(), left(), right(), chopped(), truncate() |
| 10080 | */ |
| 10081 | |
| 10082 | /*! |
| 10083 | \fn QLatin1String QLatin1String::trimmed() const |
| 10084 | \since 5.10 |
| 10085 | |
| 10086 | Strips leading and trailing whitespace and returns the result. |
| 10087 | |
| 10088 | Whitespace means any character for which QChar::isSpace() returns |
| 10089 | \c true. This includes the ASCII characters '\\t', '\\n', '\\v', |
| 10090 | '\\f', '\\r', and ' '. |
| 10091 | */ |
| 10092 | |
| 10093 | /*! \fn bool QLatin1String::operator==(const QString &other) const |
| 10094 | |
| 10095 | Returns \c true if this string is equal to string \a other; |
| 10096 | otherwise returns \c false. |
| 10097 | |
| 10098 | \sa {Comparing Strings} |
| 10099 | */ |
| 10100 | |
| 10101 | /*! |
| 10102 | \fn bool QLatin1String::operator==(const char *other) const |
| 10103 | \since 4.3 |
| 10104 | \overload |
| 10105 | |
| 10106 | The \a other const char pointer is converted to a QString using |
| 10107 | the QString::fromUtf8() function. |
| 10108 | |
| 10109 | You can disable this operator by defining \c |
| 10110 | QT_NO_CAST_FROM_ASCII when you compile your applications. This |
| 10111 | can be useful if you want to ensure that all user-visible strings |
| 10112 | go through QObject::tr(), for example. |
| 10113 | |
| 10114 | \sa QT_NO_CAST_FROM_ASCII |
| 10115 | */ |
| 10116 | |
| 10117 | /*! |
| 10118 | \fn bool QLatin1String::operator==(const QByteArray &other) const |
| 10119 | \since 5.0 |
| 10120 | \overload |
| 10121 | |
| 10122 | The \a other byte array is converted to a QString using |
| 10123 | the QString::fromUtf8() function. |
| 10124 | |
| 10125 | You can disable this operator by defining \c |
| 10126 | QT_NO_CAST_FROM_ASCII when you compile your applications. This |
| 10127 | can be useful if you want to ensure that all user-visible strings |
| 10128 | go through QObject::tr(), for example. |
| 10129 | |
| 10130 | \sa QT_NO_CAST_FROM_ASCII |
| 10131 | */ |
| 10132 | |
| 10133 | /*! \fn bool QLatin1String::operator!=(const QString &other) const |
| 10134 | |
| 10135 | Returns \c true if this string is not equal to string \a other; |
| 10136 | otherwise returns \c false. |
| 10137 | |
| 10138 | \sa {Comparing Strings} |
| 10139 | */ |
| 10140 | |
| 10141 | /*! |
| 10142 | \fn bool QLatin1String::operator!=(const char *other) const |
| 10143 | \since 4.3 |
| 10144 | \overload operator!=() |
| 10145 | |
| 10146 | The \a other const char pointer is converted to a QString using |
| 10147 | the QString::fromUtf8() function. |
| 10148 | |
| 10149 | You can disable this operator by defining \c |
| 10150 | QT_NO_CAST_FROM_ASCII when you compile your applications. This |
| 10151 | can be useful if you want to ensure that all user-visible strings |
| 10152 | go through QObject::tr(), for example. |
| 10153 | |
| 10154 | \sa QT_NO_CAST_FROM_ASCII |
| 10155 | */ |
| 10156 | |
| 10157 | /*! |
| 10158 | \fn bool QLatin1String::operator!=(const QByteArray &other) const |
| 10159 | \since 5.0 |
| 10160 | \overload operator!=() |
| 10161 | |
| 10162 | The \a other byte array is converted to a QString using |
| 10163 | the QString::fromUtf8() function. |
| 10164 | |
| 10165 | You can disable this operator by defining \c |
| 10166 | QT_NO_CAST_FROM_ASCII when you compile your applications. This |
| 10167 | can be useful if you want to ensure that all user-visible strings |
| 10168 | go through QObject::tr(), for example. |
| 10169 | |
| 10170 | \sa QT_NO_CAST_FROM_ASCII |
| 10171 | */ |
| 10172 | |
| 10173 | /*! |
| 10174 | \fn bool QLatin1String::operator>(const QString &other) const |
| 10175 | |
| 10176 | Returns \c true if this string is lexically greater than string \a |
| 10177 | other; otherwise returns \c false. |
| 10178 | |
| 10179 | \sa {Comparing Strings} |
| 10180 | */ |
| 10181 | |
| 10182 | /*! |
| 10183 | \fn bool QLatin1String::operator>(const char *other) const |
| 10184 | \since 4.3 |
| 10185 | \overload |
| 10186 | |
| 10187 | The \a other const char pointer is converted to a QString using |
| 10188 | the QString::fromUtf8() function. |
| 10189 | |
| 10190 | You can disable this operator by defining \c QT_NO_CAST_FROM_ASCII |
| 10191 | when you compile your applications. This can be useful if you want |
| 10192 | to ensure that all user-visible strings go through QObject::tr(), |
| 10193 | for example. |
| 10194 | |
| 10195 | \sa QT_NO_CAST_FROM_ASCII |
| 10196 | */ |
| 10197 | |
| 10198 | /*! |
| 10199 | \fn bool QLatin1String::operator>(const QByteArray &other) const |
| 10200 | \since 5.0 |
| 10201 | \overload |
| 10202 | |
| 10203 | The \a other const char pointer is converted to a QString using |
| 10204 | the QString::fromUtf8() function. |
| 10205 | |
| 10206 | You can disable this operator by defining \c QT_NO_CAST_FROM_ASCII |
| 10207 | when you compile your applications. This can be useful if you want |
| 10208 | to ensure that all user-visible strings go through QObject::tr(), |
| 10209 | for example. |
| 10210 | |
| 10211 | \sa QT_NO_CAST_FROM_ASCII |
| 10212 | */ |
| 10213 | |
| 10214 | /*! |
| 10215 | \fn bool QLatin1String::operator<(const QString &other) const |
| 10216 | |
| 10217 | Returns \c true if this string is lexically less than the \a other |
| 10218 | string; otherwise returns \c false. |
| 10219 | |
| 10220 | \sa {Comparing Strings} |
| 10221 | */ |
| 10222 | |
| 10223 | /*! |
| 10224 | \fn bool QLatin1String::operator<(const char *other) const |
| 10225 | \since 4.3 |
| 10226 | \overload |
| 10227 | |
| 10228 | The \a other const char pointer is converted to a QString using |
| 10229 | the QString::fromUtf8() function. |
| 10230 | |
| 10231 | You can disable this operator by defining \c |
| 10232 | QT_NO_CAST_FROM_ASCII when you compile your applications. This |
| 10233 | can be useful if you want to ensure that all user-visible strings |
| 10234 | go through QObject::tr(), for example. |
| 10235 | |
| 10236 | \sa QT_NO_CAST_FROM_ASCII |
| 10237 | */ |
| 10238 | |
| 10239 | /*! |
| 10240 | \fn bool QLatin1String::operator<(const QByteArray &other) const |
| 10241 | \since 5.0 |
| 10242 | \overload |
| 10243 | |
| 10244 | The \a other const char pointer is converted to a QString using |
| 10245 | the QString::fromUtf8() function. |
| 10246 | |
| 10247 | You can disable this operator by defining \c |
| 10248 | QT_NO_CAST_FROM_ASCII when you compile your applications. This |
| 10249 | can be useful if you want to ensure that all user-visible strings |
| 10250 | go through QObject::tr(), for example. |
| 10251 | |
| 10252 | \sa QT_NO_CAST_FROM_ASCII |
| 10253 | */ |
| 10254 | |
| 10255 | /*! |
| 10256 | \fn bool QLatin1String::operator>=(const QString &other) const |
| 10257 | |
| 10258 | Returns \c true if this string is lexically greater than or equal |
| 10259 | to string \a other; otherwise returns \c false. |
| 10260 | |
| 10261 | \sa {Comparing Strings} |
| 10262 | */ |
| 10263 | |
| 10264 | /*! |
| 10265 | \fn bool QLatin1String::operator>=(const char *other) const |
| 10266 | \since 4.3 |
| 10267 | \overload |
| 10268 | |
| 10269 | The \a other const char pointer is converted to a QString using |
| 10270 | the QString::fromUtf8() function. |
| 10271 | |
| 10272 | You can disable this operator by defining \c |
| 10273 | QT_NO_CAST_FROM_ASCII when you compile your applications. This |
| 10274 | can be useful if you want to ensure that all user-visible strings |
| 10275 | go through QObject::tr(), for example. |
| 10276 | |
| 10277 | \sa QT_NO_CAST_FROM_ASCII |
| 10278 | */ |
| 10279 | |
| 10280 | /*! |
| 10281 | \fn bool QLatin1String::operator>=(const QByteArray &other) const |
| 10282 | \since 5.0 |
| 10283 | \overload |
| 10284 | |
| 10285 | The \a other array is converted to a QString using |
| 10286 | the QString::fromUtf8() function. |
| 10287 | |
| 10288 | You can disable this operator by defining \c |
| 10289 | QT_NO_CAST_FROM_ASCII when you compile your applications. This |
| 10290 | can be useful if you want to ensure that all user-visible strings |
| 10291 | go through QObject::tr(), for example. |
| 10292 | |
| 10293 | \sa QT_NO_CAST_FROM_ASCII |
| 10294 | */ |
| 10295 | |
| 10296 | /*! \fn bool QLatin1String::operator<=(const QString &other) const |
| 10297 | |
| 10298 | Returns \c true if this string is lexically less than or equal |
| 10299 | to string \a other; otherwise returns \c false. |
| 10300 | |
| 10301 | \sa {Comparing Strings} |
| 10302 | */ |
| 10303 | |
| 10304 | /*! |
| 10305 | \fn bool QLatin1String::operator<=(const char *other) const |
| 10306 | \since 4.3 |
| 10307 | \overload |
| 10308 | |
| 10309 | The \a other const char pointer is converted to a QString using |
| 10310 | the QString::fromUtf8() function. |
| 10311 | |
| 10312 | You can disable this operator by defining \c |
| 10313 | QT_NO_CAST_FROM_ASCII when you compile your applications. This |
| 10314 | can be useful if you want to ensure that all user-visible strings |
| 10315 | go through QObject::tr(), for example. |
| 10316 | |
| 10317 | \sa QT_NO_CAST_FROM_ASCII |
| 10318 | */ |
| 10319 | |
| 10320 | /*! |
| 10321 | \fn bool QLatin1String::operator<=(const QByteArray &other) const |
| 10322 | \since 5.0 |
| 10323 | \overload |
| 10324 | |
| 10325 | The \a other array is converted to a QString using |
| 10326 | the QString::fromUtf8() function. |
| 10327 | |
| 10328 | You can disable this operator by defining \c |
| 10329 | QT_NO_CAST_FROM_ASCII when you compile your applications. This |
| 10330 | can be useful if you want to ensure that all user-visible strings |
| 10331 | go through QObject::tr(), for example. |
| 10332 | |
| 10333 | \sa QT_NO_CAST_FROM_ASCII |
| 10334 | */ |
| 10335 | |
| 10336 | |
| 10337 | /*! \fn bool operator==(QLatin1String s1, QLatin1String s2) |
| 10338 | \relates QLatin1String |
| 10339 | |
| 10340 | Returns \c true if string \a s1 is lexically equal to string \a s2; otherwise |
| 10341 | returns \c false. |
| 10342 | */ |
| 10343 | /*! \fn bool operator!=(QLatin1String s1, QLatin1String s2) |
| 10344 | \relates QLatin1String |
| 10345 | |
| 10346 | Returns \c true if string \a s1 is lexically unequal to string \a s2; otherwise |
| 10347 | returns \c false. |
| 10348 | */ |
| 10349 | /*! \fn bool operator<(QLatin1String s1, QLatin1String s2) |
| 10350 | \relates QLatin1String |
| 10351 | |
| 10352 | Returns \c true if string \a s1 is lexically smaller than string \a s2; otherwise |
| 10353 | returns \c false. |
| 10354 | */ |
| 10355 | /*! \fn bool operator<=(QLatin1String s1, QLatin1String s2) |
| 10356 | \relates QLatin1String |
| 10357 | |
| 10358 | Returns \c true if string \a s1 is lexically smaller than or equal to string \a s2; otherwise |
| 10359 | returns \c false. |
| 10360 | */ |
| 10361 | /*! \fn bool operator>(QLatin1String s1, QLatin1String s2) |
| 10362 | \relates QLatin1String |
| 10363 | |
| 10364 | Returns \c true if string \a s1 is lexically greater than string \a s2; otherwise |
| 10365 | returns \c false. |
| 10366 | */ |
| 10367 | /*! \fn bool operator>=(QLatin1String s1, QLatin1String s2) |
| 10368 | \relates QLatin1String |
| 10369 | |
| 10370 | Returns \c true if string \a s1 is lexically greater than or equal to |
| 10371 | string \a s2; otherwise returns \c false. |
| 10372 | */ |
| 10373 | |
| 10374 | |
| 10375 | #if !defined(QT_NO_DATASTREAM) || (defined(QT_BOOTSTRAPPED) && !defined(QT_BUILD_QMAKE)) |
| 10376 | /*! |
| 10377 | \fn QDataStream &operator<<(QDataStream &stream, const QString &string) |
| 10378 | \relates QString |
| 10379 | |
| 10380 | Writes the given \a string to the specified \a stream. |
| 10381 | |
| 10382 | \sa {Serializing Qt Data Types} |
| 10383 | */ |
| 10384 | |
| 10385 | QDataStream &operator<<(QDataStream &out, const QString &str) |
| 10386 | { |
| 10387 | if (out.version() == 1) { |
| 10388 | out << str.toLatin1(); |
| 10389 | } else { |
| 10390 | if (!str.isNull() || out.version() < 3) { |
| 10391 | if ((out.byteOrder() == QDataStream::BigEndian) == (QSysInfo::ByteOrder == QSysInfo::BigEndian)) { |
| 10392 | out.writeBytes(reinterpret_cast<const char *>(str.unicode()), len: sizeof(QChar) * str.length()); |
| 10393 | } else { |
| 10394 | QVarLengthArray<ushort> buffer(str.length()); |
| 10395 | qbswap<sizeof(ushort)>(source: str.constData(), count: str.length(), dest: buffer.data()); |
| 10396 | out.writeBytes(reinterpret_cast<const char *>(buffer.data()), len: sizeof(ushort) * buffer.size()); |
| 10397 | } |
| 10398 | } else { |
| 10399 | // write null marker |
| 10400 | out << (quint32)0xffffffff; |
| 10401 | } |
| 10402 | } |
| 10403 | return out; |
| 10404 | } |
| 10405 | |
| 10406 | /*! |
| 10407 | \fn QDataStream &operator>>(QDataStream &stream, QString &string) |
| 10408 | \relates QString |
| 10409 | |
| 10410 | Reads a string from the specified \a stream into the given \a string. |
| 10411 | |
| 10412 | \sa {Serializing Qt Data Types} |
| 10413 | */ |
| 10414 | |
| 10415 | QDataStream &operator>>(QDataStream &in, QString &str) |
| 10416 | { |
| 10417 | if (in.version() == 1) { |
| 10418 | QByteArray l; |
| 10419 | in >> l; |
| 10420 | str = QString::fromLatin1(str: l); |
| 10421 | } else { |
| 10422 | quint32 bytes = 0; |
| 10423 | in >> bytes; // read size of string |
| 10424 | if (bytes == 0xffffffff) { // null string |
| 10425 | str.clear(); |
| 10426 | } else if (bytes > 0) { // not empty |
| 10427 | if (bytes & 0x1) { |
| 10428 | str.clear(); |
| 10429 | in.setStatus(QDataStream::ReadCorruptData); |
| 10430 | return in; |
| 10431 | } |
| 10432 | |
| 10433 | const quint32 Step = 1024 * 1024; |
| 10434 | quint32 len = bytes / 2; |
| 10435 | quint32 allocated = 0; |
| 10436 | |
| 10437 | while (allocated < len) { |
| 10438 | int blockSize = qMin(a: Step, b: len - allocated); |
| 10439 | str.resize(size: allocated + blockSize); |
| 10440 | if (in.readRawData(reinterpret_cast<char *>(str.data()) + allocated * 2, |
| 10441 | len: blockSize * 2) != blockSize * 2) { |
| 10442 | str.clear(); |
| 10443 | in.setStatus(QDataStream::ReadPastEnd); |
| 10444 | return in; |
| 10445 | } |
| 10446 | allocated += blockSize; |
| 10447 | } |
| 10448 | |
| 10449 | if ((in.byteOrder() == QDataStream::BigEndian) |
| 10450 | != (QSysInfo::ByteOrder == QSysInfo::BigEndian)) { |
| 10451 | ushort *data = reinterpret_cast<ushort *>(str.data()); |
| 10452 | qbswap<sizeof(*data)>(source: data, count: len, dest: data); |
| 10453 | } |
| 10454 | } else { |
| 10455 | str = QString(QLatin1String("" )); |
| 10456 | } |
| 10457 | } |
| 10458 | return in; |
| 10459 | } |
| 10460 | #endif // QT_NO_DATASTREAM |
| 10461 | |
| 10462 | |
| 10463 | |
| 10464 | |
| 10465 | /*! |
| 10466 | \class QStringRef |
| 10467 | \inmodule QtCore |
| 10468 | \since 4.3 |
| 10469 | \brief The QStringRef class provides a thin wrapper around QString substrings. |
| 10470 | \reentrant |
| 10471 | \ingroup tools |
| 10472 | \ingroup string-processing |
| 10473 | |
| 10474 | QStringRef provides a read-only subset of the QString API. |
| 10475 | |
| 10476 | A string reference explicitly references a portion of a string() |
| 10477 | with a given size(), starting at a specific position(). Calling |
| 10478 | toString() returns a copy of the data as a real QString instance. |
| 10479 | |
| 10480 | This class is designed to improve the performance of substring |
| 10481 | handling when manipulating substrings obtained from existing QString |
| 10482 | instances. QStringRef avoids the memory allocation and reference |
| 10483 | counting overhead of a standard QString by simply referencing a |
| 10484 | part of the original string. This can prove to be advantageous in |
| 10485 | low level code, such as that used in a parser, at the expense of |
| 10486 | potentially more complex code. |
| 10487 | |
| 10488 | For most users, there are no semantic benefits to using QStringRef |
| 10489 | instead of QString since QStringRef requires attention to be paid |
| 10490 | to memory management issues, potentially making code more complex |
| 10491 | to write and maintain. |
| 10492 | |
| 10493 | \warning A QStringRef is only valid as long as the referenced |
| 10494 | string exists. If the original string is deleted, the string |
| 10495 | reference points to an invalid memory location. |
| 10496 | |
| 10497 | We suggest that you only use this class in stable code where profiling |
| 10498 | has clearly identified that performance improvements can be made by |
| 10499 | replacing standard string operations with the optimized substring |
| 10500 | handling provided by this class. |
| 10501 | |
| 10502 | \sa {Implicitly Shared Classes} |
| 10503 | */ |
| 10504 | |
| 10505 | /*! |
| 10506 | \typedef QStringRef::size_type |
| 10507 | \internal |
| 10508 | */ |
| 10509 | |
| 10510 | /*! |
| 10511 | \typedef QStringRef::value_type |
| 10512 | \internal |
| 10513 | */ |
| 10514 | |
| 10515 | /*! |
| 10516 | \typedef QStringRef::const_pointer |
| 10517 | \internal |
| 10518 | */ |
| 10519 | |
| 10520 | /*! |
| 10521 | \typedef QStringRef::const_reference |
| 10522 | \internal |
| 10523 | */ |
| 10524 | |
| 10525 | /*! |
| 10526 | \typedef QStringRef::const_iterator |
| 10527 | \since 5.4 |
| 10528 | |
| 10529 | \sa QStringRef::const_reverse_iterator |
| 10530 | */ |
| 10531 | |
| 10532 | /*! |
| 10533 | \typedef QStringRef::const_reverse_iterator |
| 10534 | \since 5.7 |
| 10535 | |
| 10536 | \sa QStringRef::const_iterator |
| 10537 | */ |
| 10538 | |
| 10539 | /*! |
| 10540 | \fn QStringRef::QStringRef() |
| 10541 | |
| 10542 | Constructs an empty string reference. |
| 10543 | */ |
| 10544 | |
| 10545 | /*! \fn QStringRef::QStringRef(const QString *string, int position, int length) |
| 10546 | |
| 10547 | Constructs a string reference to the range of characters in the given |
| 10548 | \a string specified by the starting \a position and \a length in characters. |
| 10549 | |
| 10550 | \warning This function exists to improve performance as much as possible, |
| 10551 | and performs no bounds checking. For program correctness, \a position and |
| 10552 | \a length must describe a valid substring of \a string. |
| 10553 | |
| 10554 | This means that the starting \a position must be positive or 0 and smaller |
| 10555 | than \a string's length, and \a length must be positive or 0 but smaller than |
| 10556 | the string's length minus the starting \a position; |
| 10557 | i.e, 0 <= position < string->length() and |
| 10558 | 0 <= length <= string->length() - position must both be satisfied. |
| 10559 | */ |
| 10560 | |
| 10561 | /*! \fn QStringRef::QStringRef(const QString *string) |
| 10562 | |
| 10563 | Constructs a string reference to the given \a string. |
| 10564 | */ |
| 10565 | |
| 10566 | /*! \fn QStringRef::QStringRef(const QStringRef &other) |
| 10567 | |
| 10568 | Constructs a copy of the \a other string reference. |
| 10569 | */ |
| 10570 | /*! |
| 10571 | \fn QStringRef::~QStringRef() |
| 10572 | |
| 10573 | Destroys the string reference. |
| 10574 | |
| 10575 | Since this class is only used to refer to string data, and does not take |
| 10576 | ownership of it, no memory is freed when instances are destroyed. |
| 10577 | */ |
| 10578 | |
| 10579 | /*! |
| 10580 | \fn int QStringRef::position() const |
| 10581 | |
| 10582 | Returns the starting position in the referenced string that is referred to |
| 10583 | by the string reference. |
| 10584 | |
| 10585 | \sa size(), string() |
| 10586 | */ |
| 10587 | |
| 10588 | /*! |
| 10589 | \fn int QStringRef::size() const |
| 10590 | |
| 10591 | Returns the number of characters referred to by the string reference. |
| 10592 | Equivalent to length() and count(). |
| 10593 | |
| 10594 | \sa position(), string() |
| 10595 | */ |
| 10596 | /*! |
| 10597 | \fn int QStringRef::count() const |
| 10598 | Returns the number of characters referred to by the string reference. |
| 10599 | Equivalent to size() and length(). |
| 10600 | |
| 10601 | \sa position(), string() |
| 10602 | */ |
| 10603 | /*! |
| 10604 | \fn int QStringRef::length() const |
| 10605 | Returns the number of characters referred to by the string reference. |
| 10606 | Equivalent to size() and count(). |
| 10607 | |
| 10608 | \sa position(), string() |
| 10609 | */ |
| 10610 | |
| 10611 | |
| 10612 | /*! |
| 10613 | \fn bool QStringRef::isEmpty() const |
| 10614 | |
| 10615 | Returns \c true if the string reference has no characters; otherwise returns |
| 10616 | \c false. |
| 10617 | |
| 10618 | A string reference is empty if its size is zero. |
| 10619 | |
| 10620 | \sa size() |
| 10621 | */ |
| 10622 | |
| 10623 | /*! |
| 10624 | \fn bool QStringRef::isNull() const |
| 10625 | |
| 10626 | Returns \c true if this string reference does not reference a string or if |
| 10627 | the string it references is null (i.e. QString::isNull() is true). |
| 10628 | |
| 10629 | \sa size() |
| 10630 | */ |
| 10631 | |
| 10632 | /*! |
| 10633 | \fn const QString *QStringRef::string() const |
| 10634 | |
| 10635 | Returns a pointer to the string referred to by the string reference, or |
| 10636 | 0 if it does not reference a string. |
| 10637 | |
| 10638 | \sa unicode() |
| 10639 | */ |
| 10640 | |
| 10641 | |
| 10642 | /*! |
| 10643 | \fn const QChar *QStringRef::unicode() const |
| 10644 | |
| 10645 | Returns a Unicode representation of the string reference. Since |
| 10646 | the data stems directly from the referenced string, it is not |
| 10647 | \\0'-terminated unless the string reference includes the string's |
| 10648 | null terminator. |
| 10649 | |
| 10650 | \sa string() |
| 10651 | */ |
| 10652 | |
| 10653 | /*! |
| 10654 | \fn const QChar *QStringRef::data() const |
| 10655 | |
| 10656 | Same as unicode(). |
| 10657 | */ |
| 10658 | |
| 10659 | /*! |
| 10660 | \fn const QChar *QStringRef::constData() const |
| 10661 | |
| 10662 | Same as unicode(). |
| 10663 | */ |
| 10664 | |
| 10665 | /*! |
| 10666 | \fn QStringRef::const_iterator QStringRef::begin() const |
| 10667 | \since 5.4 |
| 10668 | |
| 10669 | Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first character in |
| 10670 | the string. |
| 10671 | |
| 10672 | \sa cbegin(), constBegin(), end(), constEnd(), rbegin(), rend() |
| 10673 | */ |
| 10674 | |
| 10675 | /*! |
| 10676 | \fn QStringRef::const_iterator QStringRef::cbegin() const |
| 10677 | \since 5.4 |
| 10678 | |
| 10679 | Same as begin(). |
| 10680 | |
| 10681 | \sa begin(), constBegin(), cend(), constEnd(), rbegin(), rend() |
| 10682 | */ |
| 10683 | |
| 10684 | /*! |
| 10685 | \fn QStringRef::const_iterator QStringRef::constBegin() const |
| 10686 | \since 5.9 |
| 10687 | |
| 10688 | Same as begin(). |
| 10689 | |
| 10690 | \sa begin(), cend(), constEnd(), rbegin(), rend() |
| 10691 | */ |
| 10692 | |
| 10693 | /*! |
| 10694 | \fn QStringRef::const_iterator QStringRef::end() const |
| 10695 | \since 5.4 |
| 10696 | |
| 10697 | Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary |
| 10698 | character after the last character in the list. |
| 10699 | |
| 10700 | \sa cbegin(), constBegin(), end(), constEnd(), rbegin(), rend() |
| 10701 | */ |
| 10702 | |
| 10703 | /*! \fn QStringRef::const_iterator QStringRef::cend() const |
| 10704 | \since 5.4 |
| 10705 | |
| 10706 | Same as end(). |
| 10707 | |
| 10708 | \sa end(), constEnd(), cbegin(), constBegin(), rbegin(), rend() |
| 10709 | */ |
| 10710 | |
| 10711 | /*! \fn QStringRef::const_iterator QStringRef::constEnd() const |
| 10712 | \since 5.9 |
| 10713 | |
| 10714 | Same as end(). |
| 10715 | |
| 10716 | \sa end(), cend(), cbegin(), constBegin(), rbegin(), rend() |
| 10717 | */ |
| 10718 | |
| 10719 | /*! |
| 10720 | \fn QStringRef::const_reverse_iterator QStringRef::rbegin() const |
| 10721 | \since 5.7 |
| 10722 | |
| 10723 | Returns a const \l{STL-style iterators}{STL-style} reverse iterator pointing to the first |
| 10724 | character in the string, in reverse order. |
| 10725 | |
| 10726 | \sa begin(), crbegin(), rend() |
| 10727 | */ |
| 10728 | |
| 10729 | /*! |
| 10730 | \fn QStringRef::const_reverse_iterator QStringRef::crbegin() const |
| 10731 | \since 5.7 |
| 10732 | |
| 10733 | Same as rbegin(). |
| 10734 | |
| 10735 | \sa begin(), rbegin(), rend() |
| 10736 | */ |
| 10737 | |
| 10738 | /*! |
| 10739 | \fn QStringRef::const_reverse_iterator QStringRef::rend() const |
| 10740 | \since 5.7 |
| 10741 | |
| 10742 | Returns a \l{STL-style iterators}{STL-style} reverse iterator pointing to one past |
| 10743 | the last character in the string, in reverse order. |
| 10744 | |
| 10745 | \sa end(), crend(), rbegin() |
| 10746 | */ |
| 10747 | |
| 10748 | |
| 10749 | /*! |
| 10750 | \fn QStringRef::const_reverse_iterator QStringRef::crend() const |
| 10751 | \since 5.7 |
| 10752 | |
| 10753 | Same as rend(). |
| 10754 | |
| 10755 | \sa end(), rend(), rbegin() |
| 10756 | */ |
| 10757 | |
| 10758 | /*! |
| 10759 | Returns a copy of the string reference as a QString object. |
| 10760 | |
| 10761 | If the string reference is not a complete reference of the string |
| 10762 | (meaning that position() is 0 and size() equals string()->size()), |
| 10763 | this function will allocate a new string to return. |
| 10764 | |
| 10765 | \sa string() |
| 10766 | */ |
| 10767 | |
| 10768 | QString QStringRef::toString() const { |
| 10769 | if (!m_string) |
| 10770 | return QString(); |
| 10771 | if (m_size && m_position == 0 && m_size == m_string->size()) |
| 10772 | return *m_string; |
| 10773 | return QString(m_string->unicode() + m_position, m_size); |
| 10774 | } |
| 10775 | |
| 10776 | |
| 10777 | /*! \relates QStringRef |
| 10778 | |
| 10779 | Returns \c true if string reference \a s1 is lexically equal to string reference \a s2; otherwise |
| 10780 | returns \c false. |
| 10781 | */ |
| 10782 | bool operator==(const QStringRef &s1,const QStringRef &s2) noexcept |
| 10783 | { |
| 10784 | return s1.size() == s2.size() && qt_compare_strings(lhs: s1, rhs: s2, cs: Qt::CaseSensitive) == 0; |
| 10785 | } |
| 10786 | |
| 10787 | /*! \relates QStringRef |
| 10788 | |
| 10789 | Returns \c true if string \a s1 is lexically equal to string reference \a s2; otherwise |
| 10790 | returns \c false. |
| 10791 | */ |
| 10792 | bool operator==(const QString &s1,const QStringRef &s2) noexcept |
| 10793 | { |
| 10794 | return s1.size() == s2.size() && qt_compare_strings(lhs: s1, rhs: s2, cs: Qt::CaseSensitive) == 0; |
| 10795 | } |
| 10796 | |
| 10797 | /*! \relates QStringRef |
| 10798 | |
| 10799 | Returns \c true if string \a s1 is lexically equal to string reference \a s2; otherwise |
| 10800 | returns \c false. |
| 10801 | */ |
| 10802 | bool operator==(QLatin1String s1, const QStringRef &s2) noexcept |
| 10803 | { |
| 10804 | if (s1.size() != s2.size()) |
| 10805 | return false; |
| 10806 | |
| 10807 | return qt_compare_strings(lhs: s2, rhs: s1, cs: Qt::CaseSensitive) == 0; |
| 10808 | } |
| 10809 | |
| 10810 | /*! |
| 10811 | \relates QStringRef |
| 10812 | |
| 10813 | Returns \c true if string reference \a s1 is lexically less than |
| 10814 | string reference \a s2; otherwise returns \c false. |
| 10815 | |
| 10816 | \sa {Comparing Strings} |
| 10817 | */ |
| 10818 | bool operator<(const QStringRef &s1,const QStringRef &s2) noexcept |
| 10819 | { |
| 10820 | return qt_compare_strings(lhs: s1, rhs: s2, cs: Qt::CaseSensitive) < 0; |
| 10821 | } |
| 10822 | |
| 10823 | /*!\fn bool operator<=(const QStringRef &s1,const QStringRef &s2) |
| 10824 | |
| 10825 | \relates QStringRef |
| 10826 | |
| 10827 | Returns \c true if string reference \a s1 is lexically less than |
| 10828 | or equal to string reference \a s2; otherwise returns \c false. |
| 10829 | |
| 10830 | \sa {Comparing Strings} |
| 10831 | */ |
| 10832 | |
| 10833 | /*!\fn bool operator>=(const QStringRef &s1,const QStringRef &s2) |
| 10834 | |
| 10835 | \relates QStringRef |
| 10836 | |
| 10837 | Returns \c true if string reference \a s1 is lexically greater than |
| 10838 | or equal to string reference \a s2; otherwise returns \c false. |
| 10839 | |
| 10840 | \sa {Comparing Strings} |
| 10841 | */ |
| 10842 | |
| 10843 | /*!\fn bool operator>(const QStringRef &s1,const QStringRef &s2) |
| 10844 | |
| 10845 | \relates QStringRef |
| 10846 | |
| 10847 | Returns \c true if string reference \a s1 is lexically greater than |
| 10848 | string reference \a s2; otherwise returns \c false. |
| 10849 | |
| 10850 | \sa {Comparing Strings} |
| 10851 | */ |
| 10852 | |
| 10853 | |
| 10854 | /*! |
| 10855 | \fn const QChar QStringRef::at(int position) const |
| 10856 | |
| 10857 | Returns the character at the given index \a position in the |
| 10858 | string reference. |
| 10859 | |
| 10860 | The \a position must be a valid index position in the string |
| 10861 | (i.e., 0 <= \a position < size()). |
| 10862 | */ |
| 10863 | |
| 10864 | /*! |
| 10865 | \fn QChar QStringRef::operator[](int position) const |
| 10866 | \since 5.7 |
| 10867 | |
| 10868 | Returns the character at the given index \a position in the |
| 10869 | string reference. |
| 10870 | |
| 10871 | The \a position must be a valid index position in the string |
| 10872 | reference (i.e., 0 <= \a position < size()). |
| 10873 | |
| 10874 | \sa at() |
| 10875 | */ |
| 10876 | |
| 10877 | /*! |
| 10878 | \fn QChar QStringRef::front() const |
| 10879 | \since 5.10 |
| 10880 | |
| 10881 | Returns the first character in the string. |
| 10882 | Same as \c{at(0)}. |
| 10883 | |
| 10884 | This function is provided for STL compatibility. |
| 10885 | |
| 10886 | \warning Calling this function on an empty string constitutes |
| 10887 | undefined behavior. |
| 10888 | |
| 10889 | \sa back(), at(), operator[]() |
| 10890 | */ |
| 10891 | |
| 10892 | /*! |
| 10893 | \fn QChar QStringRef::back() const |
| 10894 | \since 5.10 |
| 10895 | |
| 10896 | Returns the last character in the string. |
| 10897 | Same as \c{at(size() - 1)}. |
| 10898 | |
| 10899 | This function is provided for STL compatibility. |
| 10900 | |
| 10901 | \warning Calling this function on an empty string constitutes |
| 10902 | undefined behavior. |
| 10903 | |
| 10904 | \sa front(), at(), operator[]() |
| 10905 | */ |
| 10906 | |
| 10907 | /*! |
| 10908 | \fn void QStringRef::clear() |
| 10909 | |
| 10910 | Clears the contents of the string reference by making it null and empty. |
| 10911 | |
| 10912 | \sa isEmpty(), isNull() |
| 10913 | */ |
| 10914 | |
| 10915 | /*! |
| 10916 | \fn QStringRef &QStringRef::operator=(const QStringRef &other) |
| 10917 | |
| 10918 | Assigns the \a other string reference to this string reference, and |
| 10919 | returns the result. |
| 10920 | */ |
| 10921 | |
| 10922 | /*! |
| 10923 | \fn QStringRef &QStringRef::operator=(const QString *string) |
| 10924 | |
| 10925 | Constructs a string reference to the given \a string and assigns it to |
| 10926 | this string reference, returning the result. |
| 10927 | */ |
| 10928 | |
| 10929 | /*! |
| 10930 | \fn bool QStringRef::operator==(const char * s) const |
| 10931 | |
| 10932 | \overload operator==() |
| 10933 | |
| 10934 | The \a s byte array is converted to a QStringRef using the |
| 10935 | fromUtf8() function. This function stops conversion at the |
| 10936 | first NUL character found, or the end of the byte array. |
| 10937 | |
| 10938 | You can disable this operator by defining \c |
| 10939 | QT_NO_CAST_FROM_ASCII when you compile your applications. This |
| 10940 | can be useful if you want to ensure that all user-visible strings |
| 10941 | go through QObject::tr(), for example. |
| 10942 | |
| 10943 | Returns \c true if this string is lexically equal to the parameter |
| 10944 | string \a s. Otherwise returns \c false. |
| 10945 | |
| 10946 | \sa QT_NO_CAST_FROM_ASCII |
| 10947 | */ |
| 10948 | |
| 10949 | /*! |
| 10950 | \fn bool QStringRef::operator!=(const char * s) const |
| 10951 | |
| 10952 | \overload operator!=() |
| 10953 | |
| 10954 | The \a s const char pointer is converted to a QStringRef using |
| 10955 | the fromUtf8() function. |
| 10956 | |
| 10957 | You can disable this operator by defining \c |
| 10958 | QT_NO_CAST_FROM_ASCII when you compile your applications. This |
| 10959 | can be useful if you want to ensure that all user-visible strings |
| 10960 | go through QObject::tr(), for example. |
| 10961 | |
| 10962 | Returns \c true if this string is not lexically equal to the parameter |
| 10963 | string \a s. Otherwise returns \c false. |
| 10964 | |
| 10965 | \sa QT_NO_CAST_FROM_ASCII |
| 10966 | */ |
| 10967 | |
| 10968 | /*! |
| 10969 | \fn bool QStringRef::operator<(const char * s) const |
| 10970 | |
| 10971 | \overload operator<() |
| 10972 | |
| 10973 | The \a s const char pointer is converted to a QStringRef using |
| 10974 | the fromUtf8() function. |
| 10975 | |
| 10976 | You can disable this operator by defining \c |
| 10977 | QT_NO_CAST_FROM_ASCII when you compile your applications. This |
| 10978 | can be useful if you want to ensure that all user-visible strings |
| 10979 | go through QObject::tr(), for example. |
| 10980 | |
| 10981 | Returns \c true if this string is lexically smaller than the parameter |
| 10982 | string \a s. Otherwise returns \c false. |
| 10983 | |
| 10984 | \sa QT_NO_CAST_FROM_ASCII |
| 10985 | */ |
| 10986 | |
| 10987 | /*! |
| 10988 | \fn bool QStringRef::operator<=(const char * s) const |
| 10989 | |
| 10990 | \overload operator<=() |
| 10991 | |
| 10992 | The \a s const char pointer is converted to a QStringRef using |
| 10993 | the fromUtf8() function. |
| 10994 | |
| 10995 | You can disable this operator by defining \c |
| 10996 | QT_NO_CAST_FROM_ASCII when you compile your applications. This |
| 10997 | can be useful if you want to ensure that all user-visible strings |
| 10998 | go through QObject::tr(), for example. |
| 10999 | |
| 11000 | Returns \c true if this string is lexically smaller than or equal to the parameter |
| 11001 | string \a s. Otherwise returns \c false. |
| 11002 | |
| 11003 | \sa QT_NO_CAST_FROM_ASCII |
| 11004 | */ |
| 11005 | |
| 11006 | /*! |
| 11007 | \fn bool QStringRef::operator>(const char * s) const |
| 11008 | |
| 11009 | |
| 11010 | \overload operator>() |
| 11011 | |
| 11012 | The \a s const char pointer is converted to a QStringRef using |
| 11013 | the fromUtf8() function. |
| 11014 | |
| 11015 | You can disable this operator by defining \c |
| 11016 | QT_NO_CAST_FROM_ASCII when you compile your applications. This |
| 11017 | can be useful if you want to ensure that all user-visible strings |
| 11018 | go through QObject::tr(), for example. |
| 11019 | |
| 11020 | Returns \c true if this string is lexically greater than the parameter |
| 11021 | string \a s. Otherwise returns \c false. |
| 11022 | |
| 11023 | \sa QT_NO_CAST_FROM_ASCII |
| 11024 | */ |
| 11025 | |
| 11026 | /*! |
| 11027 | \fn bool QStringRef::operator>= (const char * s) const |
| 11028 | |
| 11029 | \overload operator>=() |
| 11030 | |
| 11031 | The \a s const char pointer is converted to a QStringRef using |
| 11032 | the fromUtf8() function. |
| 11033 | |
| 11034 | You can disable this operator by defining \c |
| 11035 | QT_NO_CAST_FROM_ASCII when you compile your applications. This |
| 11036 | can be useful if you want to ensure that all user-visible strings |
| 11037 | go through QObject::tr(), for example. |
| 11038 | |
| 11039 | Returns \c true if this string is lexically greater than or equal to the |
| 11040 | parameter string \a s. Otherwise returns \c false. |
| 11041 | |
| 11042 | \sa QT_NO_CAST_FROM_ASCII |
| 11043 | */ |
| 11044 | /*! |
| 11045 | \typedef QString::Data |
| 11046 | \internal |
| 11047 | */ |
| 11048 | |
| 11049 | /*! |
| 11050 | \typedef QString::DataPtr |
| 11051 | \internal |
| 11052 | */ |
| 11053 | |
| 11054 | /*! |
| 11055 | \fn DataPtr & QString::data_ptr() |
| 11056 | \internal |
| 11057 | */ |
| 11058 | |
| 11059 | |
| 11060 | |
| 11061 | /*! Appends the string reference to \a string, and returns a new |
| 11062 | reference to the combined string data. |
| 11063 | */ |
| 11064 | QStringRef QStringRef::appendTo(QString *string) const |
| 11065 | { |
| 11066 | if (!string) |
| 11067 | return QStringRef(); |
| 11068 | int pos = string->size(); |
| 11069 | string->insert(i: pos, unicode: unicode(), size: size()); |
| 11070 | return QStringRef(string, pos, size()); |
| 11071 | } |
| 11072 | |
| 11073 | /*! |
| 11074 | \fn int QStringRef::compare(const QStringRef &s1, const QString &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive) |
| 11075 | \since 4.5 |
| 11076 | |
| 11077 | Compares the string \a s1 with the string \a s2 and returns an |
| 11078 | integer less than, equal to, or greater than zero if \a s1 |
| 11079 | is less than, equal to, or greater than \a s2. |
| 11080 | |
| 11081 | If \a cs is Qt::CaseSensitive, the comparison is case sensitive; |
| 11082 | otherwise the comparison is case insensitive. |
| 11083 | */ |
| 11084 | |
| 11085 | /*! |
| 11086 | \fn int QStringRef::compare(const QStringRef &s1, const QStringRef &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive) |
| 11087 | \since 4.5 |
| 11088 | \overload |
| 11089 | |
| 11090 | Compares the string \a s1 with the string \a s2 and returns an |
| 11091 | integer less than, equal to, or greater than zero if \a s1 |
| 11092 | is less than, equal to, or greater than \a s2. |
| 11093 | |
| 11094 | If \a cs is Qt::CaseSensitive, the comparison is case sensitive; |
| 11095 | otherwise the comparison is case insensitive. |
| 11096 | */ |
| 11097 | |
| 11098 | /*! |
| 11099 | \fn int QStringRef::compare(const QStringRef &s1, QLatin1String s2, Qt::CaseSensitivity cs = Qt::CaseSensitive) |
| 11100 | \since 4.5 |
| 11101 | \overload |
| 11102 | |
| 11103 | Compares the string \a s1 with the string \a s2 and returns an |
| 11104 | integer less than, equal to, or greater than zero if \a s1 |
| 11105 | is less than, equal to, or greater than \a s2. |
| 11106 | |
| 11107 | If \a cs is Qt::CaseSensitive, the comparison is case sensitive; |
| 11108 | otherwise the comparison is case insensitive. |
| 11109 | */ |
| 11110 | |
| 11111 | /*! |
| 11112 | \overload |
| 11113 | \fn int QStringRef::compare(const QString &other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
| 11114 | \since 4.5 |
| 11115 | |
| 11116 | Compares this string with the \a other string and returns an |
| 11117 | integer less than, equal to, or greater than zero if this string |
| 11118 | is less than, equal to, or greater than the \a other string. |
| 11119 | |
| 11120 | If \a cs is Qt::CaseSensitive, the comparison is case sensitive; |
| 11121 | otherwise the comparison is case insensitive. |
| 11122 | |
| 11123 | Equivalent to \c {compare(*this, other, cs)}. |
| 11124 | */ |
| 11125 | |
| 11126 | /*! |
| 11127 | \overload |
| 11128 | \fn int QStringRef::compare(const QStringRef &other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
| 11129 | \since 4.5 |
| 11130 | |
| 11131 | Compares this string with the \a other string and returns an |
| 11132 | integer less than, equal to, or greater than zero if this string |
| 11133 | is less than, equal to, or greater than the \a other string. |
| 11134 | |
| 11135 | If \a cs is Qt::CaseSensitive, the comparison is case sensitive; |
| 11136 | otherwise the comparison is case insensitive. |
| 11137 | |
| 11138 | Equivalent to \c {compare(*this, other, cs)}. |
| 11139 | */ |
| 11140 | |
| 11141 | /*! |
| 11142 | \overload |
| 11143 | \fn int QStringRef::compare(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
| 11144 | \since 5.14 |
| 11145 | |
| 11146 | Compares this string with \a ch and returns an |
| 11147 | integer less than, equal to, or greater than zero if this string |
| 11148 | is less than, equal to, or greater than \a ch, interpreted as a string of length one. |
| 11149 | |
| 11150 | If \a cs is Qt::CaseSensitive, the comparison is case sensitive; |
| 11151 | otherwise the comparison is case insensitive. |
| 11152 | */ |
| 11153 | |
| 11154 | /*! |
| 11155 | \overload |
| 11156 | \fn int QStringRef::compare(QLatin1String other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
| 11157 | \since 4.5 |
| 11158 | |
| 11159 | Compares this string with the \a other string and returns an |
| 11160 | integer less than, equal to, or greater than zero if this string |
| 11161 | is less than, equal to, or greater than the \a other string. |
| 11162 | |
| 11163 | If \a cs is Qt::CaseSensitive, the comparison is case sensitive; |
| 11164 | otherwise the comparison is case insensitive. |
| 11165 | |
| 11166 | Equivalent to \c {compare(*this, other, cs)}. |
| 11167 | */ |
| 11168 | |
| 11169 | /*! |
| 11170 | \overload |
| 11171 | \fn int QStringRef::compare(const QByteArray &other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
| 11172 | \since 5.8 |
| 11173 | |
| 11174 | Compares this string with \a other and returns an |
| 11175 | integer less than, equal to, or greater than zero if this string |
| 11176 | is less than, equal to, or greater than the \a other byte array, |
| 11177 | interpreted as a UTF-8 sequence. |
| 11178 | |
| 11179 | If \a cs is Qt::CaseSensitive, the comparison is case sensitive; |
| 11180 | otherwise the comparison is case insensitive. |
| 11181 | |
| 11182 | Equivalent to \c {compare(*this, other, cs)}. |
| 11183 | */ |
| 11184 | |
| 11185 | /*! |
| 11186 | \fn int QStringRef::localeAwareCompare(const QStringRef &s1, const QString & s2) |
| 11187 | \since 4.5 |
| 11188 | |
| 11189 | Compares \a s1 with \a s2 and returns an integer less than, equal |
| 11190 | to, or greater than zero if \a s1 is less than, equal to, or |
| 11191 | greater than \a s2. |
| 11192 | |
| 11193 | The comparison is performed in a locale- and also |
| 11194 | platform-dependent manner. Use this function to present sorted |
| 11195 | lists of strings to the user. |
| 11196 | |
| 11197 | \sa compare(), QLocale, {Comparing Strings} |
| 11198 | */ |
| 11199 | |
| 11200 | /*! |
| 11201 | \fn int QStringRef::localeAwareCompare(const QStringRef &s1, const QStringRef & s2) |
| 11202 | \since 4.5 |
| 11203 | \overload |
| 11204 | |
| 11205 | Compares \a s1 with \a s2 and returns an integer less than, equal |
| 11206 | to, or greater than zero if \a s1 is less than, equal to, or |
| 11207 | greater than \a s2. |
| 11208 | |
| 11209 | The comparison is performed in a locale- and also |
| 11210 | platform-dependent manner. Use this function to present sorted |
| 11211 | lists of strings to the user. |
| 11212 | |
| 11213 | \sa {Comparing Strings} |
| 11214 | */ |
| 11215 | |
| 11216 | /*! |
| 11217 | \fn int QStringRef::localeAwareCompare(const QString &other) const |
| 11218 | \since 4.5 |
| 11219 | \overload |
| 11220 | |
| 11221 | Compares this string with the \a other string and returns an |
| 11222 | integer less than, equal to, or greater than zero if this string |
| 11223 | is less than, equal to, or greater than the \a other string. |
| 11224 | |
| 11225 | The comparison is performed in a locale- and also |
| 11226 | platform-dependent manner. Use this function to present sorted |
| 11227 | lists of strings to the user. |
| 11228 | |
| 11229 | \sa {Comparing Strings} |
| 11230 | */ |
| 11231 | |
| 11232 | /*! |
| 11233 | \fn int QStringRef::localeAwareCompare(const QStringRef &other) const |
| 11234 | \since 4.5 |
| 11235 | \overload |
| 11236 | |
| 11237 | Compares this string with the \a other string and returns an |
| 11238 | integer less than, equal to, or greater than zero if this string |
| 11239 | is less than, equal to, or greater than the \a other string. |
| 11240 | |
| 11241 | The comparison is performed in a locale- and also |
| 11242 | platform-dependent manner. Use this function to present sorted |
| 11243 | lists of strings to the user. |
| 11244 | |
| 11245 | \sa {Comparing Strings} |
| 11246 | */ |
| 11247 | |
| 11248 | /*! |
| 11249 | \fn QString &QString::append(const QStringRef &reference) |
| 11250 | \since 4.4 |
| 11251 | |
| 11252 | Appends the given string \a reference to this string and returns the result. |
| 11253 | */ |
| 11254 | QString &QString::append(const QStringRef &str) |
| 11255 | { |
| 11256 | if (str.string() == this) { |
| 11257 | str.appendTo(string: this); |
| 11258 | } else if (!str.isNull()) { |
| 11259 | int oldSize = size(); |
| 11260 | resize(size: oldSize + str.size()); |
| 11261 | memcpy(dest: data() + oldSize, src: str.unicode(), n: str.size() * sizeof(QChar)); |
| 11262 | } |
| 11263 | return *this; |
| 11264 | } |
| 11265 | |
| 11266 | /*! |
| 11267 | \fn QString &QString::append(QStringView str) |
| 11268 | \since 5.15.2 |
| 11269 | |
| 11270 | Appends the given string \a str to this string and returns the result. |
| 11271 | |
| 11272 | \note This method has been added in 5.15.2 to simplify writing code that is portable |
| 11273 | between Qt 5.15 and Qt 6. |
| 11274 | */ |
| 11275 | |
| 11276 | /*! |
| 11277 | \fn QStringRef::left(int n) const |
| 11278 | \since 5.2 |
| 11279 | |
| 11280 | Returns a substring reference to the \a n leftmost characters |
| 11281 | of the string. |
| 11282 | |
| 11283 | If \a n is greater than or equal to size(), or less than zero, |
| 11284 | a reference to the entire string is returned. |
| 11285 | |
| 11286 | \sa right(), mid(), startsWith(), chopped(), chop(), truncate() |
| 11287 | */ |
| 11288 | QStringRef QStringRef::left(int n) const |
| 11289 | { |
| 11290 | if (uint(n) >= uint(m_size)) |
| 11291 | return *this; |
| 11292 | return QStringRef(m_string, m_position, n); |
| 11293 | } |
| 11294 | |
| 11295 | /*! |
| 11296 | \since 4.4 |
| 11297 | |
| 11298 | Returns a substring reference to the \a n leftmost characters |
| 11299 | of the string. |
| 11300 | |
| 11301 | If \a n is greater than or equal to size(), or less than zero, |
| 11302 | a reference to the entire string is returned. |
| 11303 | |
| 11304 | \snippet qstring/main.cpp leftRef |
| 11305 | |
| 11306 | \sa left(), rightRef(), midRef(), startsWith() |
| 11307 | */ |
| 11308 | QStringRef QString::leftRef(int n) const |
| 11309 | { |
| 11310 | return QStringRef(this).left(n); |
| 11311 | } |
| 11312 | |
| 11313 | /*! |
| 11314 | \fn QStringRef::right(int n) const |
| 11315 | \since 5.2 |
| 11316 | |
| 11317 | Returns a substring reference to the \a n rightmost characters |
| 11318 | of the string. |
| 11319 | |
| 11320 | If \a n is greater than or equal to size(), or less than zero, |
| 11321 | a reference to the entire string is returned. |
| 11322 | |
| 11323 | \sa left(), mid(), endsWith(), chopped(), chop(), truncate() |
| 11324 | */ |
| 11325 | QStringRef QStringRef::right(int n) const |
| 11326 | { |
| 11327 | if (uint(n) >= uint(m_size)) |
| 11328 | return *this; |
| 11329 | return QStringRef(m_string, m_size - n + m_position, n); |
| 11330 | } |
| 11331 | |
| 11332 | /*! |
| 11333 | \since 4.4 |
| 11334 | |
| 11335 | Returns a substring reference to the \a n rightmost characters |
| 11336 | of the string. |
| 11337 | |
| 11338 | If \a n is greater than or equal to size(), or less than zero, |
| 11339 | a reference to the entire string is returned. |
| 11340 | |
| 11341 | \snippet qstring/main.cpp rightRef |
| 11342 | |
| 11343 | \sa right(), leftRef(), midRef(), endsWith() |
| 11344 | */ |
| 11345 | QStringRef QString::rightRef(int n) const |
| 11346 | { |
| 11347 | return QStringRef(this).right(n); |
| 11348 | } |
| 11349 | |
| 11350 | /*! |
| 11351 | \fn QStringRef QStringRef::mid(int position, int n = -1) const |
| 11352 | \since 5.2 |
| 11353 | |
| 11354 | Returns a substring reference to \a n characters of this string, |
| 11355 | starting at the specified \a position. |
| 11356 | |
| 11357 | If the \a position exceeds the length of the string, a null |
| 11358 | reference is returned. |
| 11359 | |
| 11360 | If there are less than \a n characters available in the string, |
| 11361 | starting at the given \a position, or if \a n is -1 (default), the |
| 11362 | function returns all characters from the specified \a position |
| 11363 | onwards. |
| 11364 | |
| 11365 | \sa left(), right(), chopped(), chop(), truncate() |
| 11366 | */ |
| 11367 | QStringRef QStringRef::mid(int pos, int n) const |
| 11368 | { |
| 11369 | using namespace QtPrivate; |
| 11370 | switch (QContainerImplHelper::mid(originalLength: m_size, position: &pos, length: &n)) { |
| 11371 | case QContainerImplHelper::Null: |
| 11372 | return QStringRef(); |
| 11373 | case QContainerImplHelper::Empty: |
| 11374 | return QStringRef(m_string, 0, 0); |
| 11375 | case QContainerImplHelper::Full: |
| 11376 | return *this; |
| 11377 | case QContainerImplHelper::Subset: |
| 11378 | return QStringRef(m_string, pos + m_position, n); |
| 11379 | } |
| 11380 | Q_UNREACHABLE(); |
| 11381 | return QStringRef(); |
| 11382 | } |
| 11383 | |
| 11384 | /*! |
| 11385 | \fn QStringRef QStringRef::chopped(int len) const |
| 11386 | \since 5.10 |
| 11387 | |
| 11388 | Returns a substring reference to the size() - \a len leftmost characters |
| 11389 | of this string. |
| 11390 | |
| 11391 | \note The behavior is undefined if \a len is negative or greater than size(). |
| 11392 | |
| 11393 | \sa endsWith(), left(), right(), mid(), chop(), truncate() |
| 11394 | */ |
| 11395 | |
| 11396 | /*! |
| 11397 | \since 4.4 |
| 11398 | |
| 11399 | Returns a substring reference to \a n characters of this string, |
| 11400 | starting at the specified \a position. |
| 11401 | |
| 11402 | If the \a position exceeds the length of the string, a null |
| 11403 | reference is returned. |
| 11404 | |
| 11405 | If there are less than \a n characters available in the string, |
| 11406 | starting at the given \a position, or if \a n is -1 (default), the |
| 11407 | function returns all characters from the specified \a position |
| 11408 | onwards. |
| 11409 | |
| 11410 | Example: |
| 11411 | |
| 11412 | \snippet qstring/main.cpp midRef |
| 11413 | |
| 11414 | \sa mid(), leftRef(), rightRef() |
| 11415 | */ |
| 11416 | QStringRef QString::midRef(int position, int n) const |
| 11417 | { |
| 11418 | return QStringRef(this).mid(pos: position, n); |
| 11419 | } |
| 11420 | |
| 11421 | /*! |
| 11422 | \fn void QStringRef::truncate(int position) |
| 11423 | \since 5.6 |
| 11424 | |
| 11425 | Truncates the string at the given \a position index. |
| 11426 | |
| 11427 | If the specified \a position index is beyond the end of the |
| 11428 | string, nothing happens. |
| 11429 | |
| 11430 | If \a position is negative, it is equivalent to passing zero. |
| 11431 | |
| 11432 | \sa QString::truncate() |
| 11433 | */ |
| 11434 | |
| 11435 | /*! |
| 11436 | \fn void QStringRef::chop(int n) |
| 11437 | \since 5.8 |
| 11438 | |
| 11439 | Removes \a n characters from the end of the string. |
| 11440 | |
| 11441 | If \a n is greater than or equal to size(), the result is an |
| 11442 | empty string; if \a n is negative, it is equivalent to passing zero. |
| 11443 | |
| 11444 | \sa QString::chop(), truncate() |
| 11445 | */ |
| 11446 | |
| 11447 | #if QT_STRINGVIEW_LEVEL < 2 |
| 11448 | /*! |
| 11449 | \since 4.8 |
| 11450 | |
| 11451 | Returns the index position of the first occurrence of the string \a |
| 11452 | str in this string reference, searching forward from index position |
| 11453 | \a from. Returns -1 if \a str is not found. |
| 11454 | |
| 11455 | If \a cs is Qt::CaseSensitive (default), the search is case |
| 11456 | sensitive; otherwise the search is case insensitive. |
| 11457 | |
| 11458 | If \a from is -1, the search starts at the last character; if it is |
| 11459 | -2, at the next to last character and so on. |
| 11460 | |
| 11461 | \sa QString::indexOf(), lastIndexOf(), contains(), count() |
| 11462 | */ |
| 11463 | int QStringRef::indexOf(const QString &str, int from, Qt::CaseSensitivity cs) const |
| 11464 | { |
| 11465 | // ### Qt6: qsizetype |
| 11466 | return int(QtPrivate::findString(haystack: QStringView(unicode(), length()), from, needle: QStringView(str.unicode(), str.length()), cs)); |
| 11467 | } |
| 11468 | #endif // QT_STRINGVIEW_LEVEL < 2 |
| 11469 | |
| 11470 | /*! |
| 11471 | \fn int QStringRef::indexOf(QStringView str, int from, Qt::CaseSensitivity cs) const |
| 11472 | \since 5.14 |
| 11473 | \overload indexOf() |
| 11474 | |
| 11475 | Returns the index position of the first occurrence of the string view \a str |
| 11476 | in this string reference, searching forward from index position \a from. |
| 11477 | Returns -1 if \a str is not found. |
| 11478 | |
| 11479 | If \a cs is Qt::CaseSensitive (default), the search is case |
| 11480 | sensitive; otherwise the search is case insensitive. |
| 11481 | |
| 11482 | If \a from is -1, the search starts at the last character; if it is |
| 11483 | -2, at the next to last character and so on. |
| 11484 | |
| 11485 | \sa QString::indexOf(), QStringView::indexOf(), lastIndexOf(), contains(), count() |
| 11486 | */ |
| 11487 | |
| 11488 | /*! |
| 11489 | \since 4.8 |
| 11490 | \overload indexOf() |
| 11491 | |
| 11492 | Returns the index position of the first occurrence of the |
| 11493 | character \a ch in the string reference, searching forward from |
| 11494 | index position \a from. Returns -1 if \a ch could not be found. |
| 11495 | |
| 11496 | \sa QString::indexOf(), lastIndexOf(), contains(), count() |
| 11497 | */ |
| 11498 | int QStringRef::indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const |
| 11499 | { |
| 11500 | // ### Qt6: qsizetype |
| 11501 | return int(qFindChar(str: QStringView(unicode(), length()), ch, from, cs)); |
| 11502 | } |
| 11503 | |
| 11504 | /*! |
| 11505 | \since 4.8 |
| 11506 | |
| 11507 | Returns the index position of the first occurrence of the string \a |
| 11508 | str in this string reference, searching forward from index position |
| 11509 | \a from. Returns -1 if \a str is not found. |
| 11510 | |
| 11511 | If \a cs is Qt::CaseSensitive (default), the search is case |
| 11512 | sensitive; otherwise the search is case insensitive. |
| 11513 | |
| 11514 | If \a from is -1, the search starts at the last character; if it is |
| 11515 | -2, at the next to last character and so on. |
| 11516 | |
| 11517 | \sa QString::indexOf(), lastIndexOf(), contains(), count() |
| 11518 | */ |
| 11519 | int QStringRef::indexOf(QLatin1String str, int from, Qt::CaseSensitivity cs) const |
| 11520 | { |
| 11521 | // ### Qt6: qsizetype |
| 11522 | return int(QtPrivate::findString(haystack: QStringView(unicode(), size()), from, needle: str, cs)); |
| 11523 | } |
| 11524 | |
| 11525 | #if QT_STRINGVIEW_LEVEL < 2 |
| 11526 | /*! |
| 11527 | \since 4.8 |
| 11528 | |
| 11529 | \overload indexOf() |
| 11530 | |
| 11531 | Returns the index position of the first occurrence of the string |
| 11532 | reference \a str in this string reference, searching forward from |
| 11533 | index position \a from. Returns -1 if \a str is not found. |
| 11534 | |
| 11535 | If \a cs is Qt::CaseSensitive (default), the search is case |
| 11536 | sensitive; otherwise the search is case insensitive. |
| 11537 | |
| 11538 | \sa QString::indexOf(), lastIndexOf(), contains(), count() |
| 11539 | */ |
| 11540 | int QStringRef::indexOf(const QStringRef &str, int from, Qt::CaseSensitivity cs) const |
| 11541 | { |
| 11542 | // ### Qt6: qsizetype |
| 11543 | return int(QtPrivate::findString(haystack: QStringView(unicode(), size()), from, needle: QStringView(str.unicode(), str.size()), cs)); |
| 11544 | } |
| 11545 | #endif // QT_STRINGVIEW_LEVEL < 2 |
| 11546 | |
| 11547 | /*! |
| 11548 | \since 4.8 |
| 11549 | |
| 11550 | Returns the index position of the last occurrence of the string \a |
| 11551 | str in this string reference, searching backward from index position |
| 11552 | \a from. If \a from is -1 (default), the search starts at the last |
| 11553 | character; if \a from is -2, at the next to last character and so |
| 11554 | on. Returns -1 if \a str is not found. |
| 11555 | |
| 11556 | If \a cs is Qt::CaseSensitive (default), the search is case |
| 11557 | sensitive; otherwise the search is case insensitive. |
| 11558 | |
| 11559 | \sa QString::lastIndexOf(), indexOf(), contains(), count() |
| 11560 | */ |
| 11561 | int QStringRef::lastIndexOf(const QString &str, int from, Qt::CaseSensitivity cs) const |
| 11562 | { |
| 11563 | // ### Qt6: qsizetype |
| 11564 | return int(QtPrivate::lastIndexOf(haystack: *this, from, needle: str, cs)); |
| 11565 | } |
| 11566 | |
| 11567 | /*! |
| 11568 | \since 4.8 |
| 11569 | \overload lastIndexOf() |
| 11570 | |
| 11571 | Returns the index position of the last occurrence of the character |
| 11572 | \a ch, searching backward from position \a from. |
| 11573 | |
| 11574 | \sa QString::lastIndexOf(), indexOf(), contains(), count() |
| 11575 | */ |
| 11576 | int QStringRef::lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) const |
| 11577 | { |
| 11578 | // ### Qt6: qsizetype |
| 11579 | return int(qLastIndexOf(haystack: *this, needle: ch, from, cs)); |
| 11580 | } |
| 11581 | |
| 11582 | /*! |
| 11583 | \since 4.8 |
| 11584 | \overload lastIndexOf() |
| 11585 | |
| 11586 | Returns the index position of the last occurrence of the string \a |
| 11587 | str in this string reference, searching backward from index position |
| 11588 | \a from. If \a from is -1 (default), the search starts at the last |
| 11589 | character; if \a from is -2, at the next to last character and so |
| 11590 | on. Returns -1 if \a str is not found. |
| 11591 | |
| 11592 | If \a cs is Qt::CaseSensitive (default), the search is case |
| 11593 | sensitive; otherwise the search is case insensitive. |
| 11594 | |
| 11595 | \sa QString::lastIndexOf(), indexOf(), contains(), count() |
| 11596 | */ |
| 11597 | int QStringRef::lastIndexOf(QLatin1String str, int from, Qt::CaseSensitivity cs) const |
| 11598 | { |
| 11599 | // ### Qt6: qsizetype |
| 11600 | return int(QtPrivate::lastIndexOf(haystack: *this, from, needle: str, cs)); |
| 11601 | } |
| 11602 | |
| 11603 | /*! |
| 11604 | \since 4.8 |
| 11605 | \overload lastIndexOf() |
| 11606 | |
| 11607 | Returns the index position of the last occurrence of the string |
| 11608 | reference \a str in this string reference, searching backward from |
| 11609 | index position \a from. If \a from is -1 (default), the search |
| 11610 | starts at the last character; if \a from is -2, at the next to last |
| 11611 | character and so on. Returns -1 if \a str is not found. |
| 11612 | |
| 11613 | If \a cs is Qt::CaseSensitive (default), the search is case |
| 11614 | sensitive; otherwise the search is case insensitive. |
| 11615 | |
| 11616 | \sa QString::lastIndexOf(), indexOf(), contains(), count() |
| 11617 | */ |
| 11618 | int QStringRef::lastIndexOf(const QStringRef &str, int from, Qt::CaseSensitivity cs) const |
| 11619 | { |
| 11620 | // ### Qt6: qsizetype |
| 11621 | return int(QtPrivate::lastIndexOf(haystack: *this, from, needle: str, cs)); |
| 11622 | } |
| 11623 | |
| 11624 | /*! |
| 11625 | \fn int QStringRef::lastIndexOf(QStringView str, int from, Qt::CaseSensitivity cs) const |
| 11626 | \since 5.14 |
| 11627 | \overload lastIndexOf() |
| 11628 | |
| 11629 | Returns the index position of the last occurrence of the string view \a |
| 11630 | str in this string, searching backward from index position \a |
| 11631 | from. If \a from is -1 (default), the search starts at the last |
| 11632 | character; if \a from is -2, at the next to last character and so |
| 11633 | on. Returns -1 if \a str is not found. |
| 11634 | |
| 11635 | If \a cs is Qt::CaseSensitive (default), the search is case |
| 11636 | sensitive; otherwise the search is case insensitive. |
| 11637 | |
| 11638 | \sa indexOf(), contains(), count() |
| 11639 | */ |
| 11640 | |
| 11641 | /*! |
| 11642 | \since 4.8 |
| 11643 | Returns the number of (potentially overlapping) occurrences of |
| 11644 | the string \a str in this string reference. |
| 11645 | |
| 11646 | If \a cs is Qt::CaseSensitive (default), the search is |
| 11647 | case sensitive; otherwise the search is case insensitive. |
| 11648 | |
| 11649 | \sa QString::count(), contains(), indexOf() |
| 11650 | */ |
| 11651 | int QStringRef::count(const QString &str, Qt::CaseSensitivity cs) const |
| 11652 | { |
| 11653 | // ### Qt6: qsizetype |
| 11654 | return int(qt_string_count(haystack: QStringView(unicode(), size()), needle: QStringView(str.unicode(), str.size()), cs)); |
| 11655 | } |
| 11656 | |
| 11657 | /*! |
| 11658 | \since 4.8 |
| 11659 | \overload count() |
| 11660 | |
| 11661 | Returns the number of occurrences of the character \a ch in the |
| 11662 | string reference. |
| 11663 | |
| 11664 | If \a cs is Qt::CaseSensitive (default), the search is |
| 11665 | case sensitive; otherwise the search is case insensitive. |
| 11666 | |
| 11667 | \sa QString::count(), contains(), indexOf() |
| 11668 | */ |
| 11669 | int QStringRef::count(QChar ch, Qt::CaseSensitivity cs) const |
| 11670 | { |
| 11671 | // ### Qt6: qsizetype |
| 11672 | return int(qt_string_count(haystack: QStringView(unicode(), size()), needle: ch, cs)); |
| 11673 | } |
| 11674 | |
| 11675 | /*! |
| 11676 | \since 4.8 |
| 11677 | \overload count() |
| 11678 | |
| 11679 | Returns the number of (potentially overlapping) occurrences of the |
| 11680 | string reference \a str in this string reference. |
| 11681 | |
| 11682 | If \a cs is Qt::CaseSensitive (default), the search is |
| 11683 | case sensitive; otherwise the search is case insensitive. |
| 11684 | |
| 11685 | \sa QString::count(), contains(), indexOf() |
| 11686 | */ |
| 11687 | int QStringRef::count(const QStringRef &str, Qt::CaseSensitivity cs) const |
| 11688 | { |
| 11689 | // ### Qt6: qsizetype |
| 11690 | return int(qt_string_count(haystack: QStringView(unicode(), size()), needle: QStringView(str.unicode(), str.size()), cs)); |
| 11691 | } |
| 11692 | |
| 11693 | /*! |
| 11694 | \since 5.9 |
| 11695 | |
| 11696 | Returns \c true if the string is read right to left. |
| 11697 | |
| 11698 | \sa QString::isRightToLeft() |
| 11699 | */ |
| 11700 | bool QStringRef::isRightToLeft() const |
| 11701 | { |
| 11702 | return QtPrivate::isRightToLeft(string: QStringView(unicode(), size())); |
| 11703 | } |
| 11704 | |
| 11705 | /*! |
| 11706 | \since 5.11 |
| 11707 | \internal |
| 11708 | \relates QStringView |
| 11709 | |
| 11710 | Returns \c true if the string is read right to left. |
| 11711 | |
| 11712 | \sa QString::isRightToLeft() |
| 11713 | */ |
| 11714 | bool QtPrivate::isRightToLeft(QStringView string) noexcept |
| 11715 | { |
| 11716 | const ushort *p = reinterpret_cast<const ushort*>(string.data()); |
| 11717 | const ushort * const end = p + string.size(); |
| 11718 | int isolateLevel = 0; |
| 11719 | while (p < end) { |
| 11720 | uint ucs4 = *p; |
| 11721 | if (QChar::isHighSurrogate(ucs4) && p < end - 1) { |
| 11722 | ushort low = p[1]; |
| 11723 | if (QChar::isLowSurrogate(ucs4: low)) { |
| 11724 | ucs4 = QChar::surrogateToUcs4(high: ucs4, low); |
| 11725 | ++p; |
| 11726 | } |
| 11727 | } |
| 11728 | switch (QChar::direction(ucs4)) |
| 11729 | { |
| 11730 | case QChar::DirRLI: |
| 11731 | case QChar::DirLRI: |
| 11732 | case QChar::DirFSI: |
| 11733 | ++isolateLevel; |
| 11734 | break; |
| 11735 | case QChar::DirPDI: |
| 11736 | if (isolateLevel) |
| 11737 | --isolateLevel; |
| 11738 | break; |
| 11739 | case QChar::DirL: |
| 11740 | if (isolateLevel) |
| 11741 | break; |
| 11742 | return false; |
| 11743 | case QChar::DirR: |
| 11744 | case QChar::DirAL: |
| 11745 | if (isolateLevel) |
| 11746 | break; |
| 11747 | return true; |
| 11748 | default: |
| 11749 | break; |
| 11750 | } |
| 11751 | ++p; |
| 11752 | } |
| 11753 | return false; |
| 11754 | } |
| 11755 | |
| 11756 | /*! |
| 11757 | \since 4.8 |
| 11758 | |
| 11759 | Returns \c true if the string reference starts with \a str; otherwise |
| 11760 | returns \c false. |
| 11761 | |
| 11762 | If \a cs is Qt::CaseSensitive (default), the search is |
| 11763 | case sensitive; otherwise the search is case insensitive. |
| 11764 | |
| 11765 | \sa QString::startsWith(), endsWith() |
| 11766 | */ |
| 11767 | bool QStringRef::startsWith(const QString &str, Qt::CaseSensitivity cs) const |
| 11768 | { |
| 11769 | return qt_starts_with(haystack: *this, needle: str, cs); |
| 11770 | } |
| 11771 | |
| 11772 | /*! |
| 11773 | \since 4.8 |
| 11774 | \overload startsWith() |
| 11775 | \sa QString::startsWith(), endsWith() |
| 11776 | */ |
| 11777 | bool QStringRef::startsWith(QLatin1String str, Qt::CaseSensitivity cs) const |
| 11778 | { |
| 11779 | return qt_starts_with(haystack: *this, needle: str, cs); |
| 11780 | } |
| 11781 | |
| 11782 | /*! |
| 11783 | \fn bool QStringRef::startsWith(QStringView str, Qt::CaseSensitivity cs) const |
| 11784 | \since 5.10 |
| 11785 | \overload startsWith() |
| 11786 | \sa QString::startsWith(), endsWith() |
| 11787 | */ |
| 11788 | |
| 11789 | /*! |
| 11790 | \since 4.8 |
| 11791 | \overload startsWith() |
| 11792 | \sa QString::startsWith(), endsWith() |
| 11793 | */ |
| 11794 | bool QStringRef::startsWith(const QStringRef &str, Qt::CaseSensitivity cs) const |
| 11795 | { |
| 11796 | return qt_starts_with(haystack: *this, needle: str, cs); |
| 11797 | } |
| 11798 | |
| 11799 | /*! |
| 11800 | \since 4.8 |
| 11801 | \overload startsWith() |
| 11802 | |
| 11803 | Returns \c true if the string reference starts with \a ch; otherwise |
| 11804 | returns \c false. |
| 11805 | |
| 11806 | If \a cs is Qt::CaseSensitive (default), the search is case |
| 11807 | sensitive; otherwise the search is case insensitive. |
| 11808 | |
| 11809 | \sa QString::startsWith(), endsWith() |
| 11810 | */ |
| 11811 | bool QStringRef::startsWith(QChar ch, Qt::CaseSensitivity cs) const |
| 11812 | { |
| 11813 | return qt_starts_with(haystack: *this, needle: ch, cs); |
| 11814 | } |
| 11815 | |
| 11816 | /*! |
| 11817 | \since 4.8 |
| 11818 | Returns \c true if the string reference ends with \a str; otherwise |
| 11819 | returns \c false. |
| 11820 | |
| 11821 | If \a cs is Qt::CaseSensitive (default), the search is case |
| 11822 | sensitive; otherwise the search is case insensitive. |
| 11823 | |
| 11824 | \sa QString::endsWith(), startsWith() |
| 11825 | */ |
| 11826 | bool QStringRef::endsWith(const QString &str, Qt::CaseSensitivity cs) const |
| 11827 | { |
| 11828 | return qt_ends_with(haystack: *this, needle: str, cs); |
| 11829 | } |
| 11830 | |
| 11831 | /*! |
| 11832 | \since 4.8 |
| 11833 | \overload endsWith() |
| 11834 | |
| 11835 | Returns \c true if the string reference ends with \a ch; otherwise |
| 11836 | returns \c false. |
| 11837 | |
| 11838 | If \a cs is Qt::CaseSensitive (default), the search is case |
| 11839 | sensitive; otherwise the search is case insensitive. |
| 11840 | |
| 11841 | \sa QString::endsWith(), endsWith() |
| 11842 | */ |
| 11843 | bool QStringRef::endsWith(QChar ch, Qt::CaseSensitivity cs) const |
| 11844 | { |
| 11845 | return qt_ends_with(haystack: *this, needle: ch, cs); |
| 11846 | } |
| 11847 | |
| 11848 | /*! |
| 11849 | \since 4.8 |
| 11850 | \overload endsWith() |
| 11851 | \sa QString::endsWith(), endsWith() |
| 11852 | */ |
| 11853 | bool QStringRef::endsWith(QLatin1String str, Qt::CaseSensitivity cs) const |
| 11854 | { |
| 11855 | return qt_ends_with(haystack: *this, needle: str, cs); |
| 11856 | } |
| 11857 | |
| 11858 | /*! |
| 11859 | \fn bool QStringRef::endsWith(QStringView str, Qt::CaseSensitivity cs) const |
| 11860 | \since 5.10 |
| 11861 | \overload endsWith() |
| 11862 | \sa QString::endsWith(), startsWith() |
| 11863 | */ |
| 11864 | |
| 11865 | /*! |
| 11866 | \since 4.8 |
| 11867 | \overload endsWith() |
| 11868 | \sa QString::endsWith(), endsWith() |
| 11869 | */ |
| 11870 | bool QStringRef::endsWith(const QStringRef &str, Qt::CaseSensitivity cs) const |
| 11871 | { |
| 11872 | return qt_ends_with(haystack: *this, needle: str, cs); |
| 11873 | } |
| 11874 | |
| 11875 | #if QT_STRINGVIEW_LEVEL < 2 |
| 11876 | /*! \fn bool QStringRef::contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
| 11877 | |
| 11878 | \since 4.8 |
| 11879 | Returns \c true if this string reference contains an occurrence of |
| 11880 | the string \a str; otherwise returns \c false. |
| 11881 | |
| 11882 | If \a cs is Qt::CaseSensitive (default), the search is |
| 11883 | case sensitive; otherwise the search is case insensitive. |
| 11884 | |
| 11885 | \sa indexOf(), count() |
| 11886 | */ |
| 11887 | #endif // QT_STRINGVIEW_LEVEL < 2 |
| 11888 | |
| 11889 | /*! \fn bool QStringRef::contains(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
| 11890 | |
| 11891 | \overload contains() |
| 11892 | \since 4.8 |
| 11893 | |
| 11894 | Returns \c true if this string contains an occurrence of the |
| 11895 | character \a ch; otherwise returns \c false. |
| 11896 | |
| 11897 | If \a cs is Qt::CaseSensitive (default), the search is |
| 11898 | case sensitive; otherwise the search is case insensitive. |
| 11899 | |
| 11900 | */ |
| 11901 | |
| 11902 | #if QT_STRINGVIEW_LEVEL < 2 |
| 11903 | /*! \fn bool QStringRef::contains(const QStringRef &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
| 11904 | \overload contains() |
| 11905 | \since 4.8 |
| 11906 | |
| 11907 | Returns \c true if this string reference contains an occurrence of |
| 11908 | the string reference \a str; otherwise returns \c false. |
| 11909 | |
| 11910 | If \a cs is Qt::CaseSensitive (default), the search is |
| 11911 | case sensitive; otherwise the search is case insensitive. |
| 11912 | |
| 11913 | \sa indexOf(), count() |
| 11914 | */ |
| 11915 | #endif // QT_STRINGVIEW_LEVEL < 2 |
| 11916 | |
| 11917 | /*! \fn bool QStringRef::contains(QLatin1String str, Qt::CaseSensitivity cs) const |
| 11918 | \since 4.8 |
| 11919 | \overload contains() |
| 11920 | |
| 11921 | Returns \c true if this string reference contains an occurrence of |
| 11922 | the string \a str; otherwise returns \c false. |
| 11923 | |
| 11924 | If \a cs is Qt::CaseSensitive (default), the search is |
| 11925 | case sensitive; otherwise the search is case insensitive. |
| 11926 | |
| 11927 | \sa indexOf(), count() |
| 11928 | */ |
| 11929 | |
| 11930 | /*! \fn bool QStringRef::contains(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
| 11931 | \since 5.14 |
| 11932 | \overload contains() |
| 11933 | |
| 11934 | Returns \c true if this string reference contains an occurrence of |
| 11935 | the string view \a str; otherwise returns \c false. |
| 11936 | |
| 11937 | If \a cs is Qt::CaseSensitive (default), the search is |
| 11938 | case sensitive; otherwise the search is case insensitive. |
| 11939 | |
| 11940 | \sa indexOf(), count() |
| 11941 | */ |
| 11942 | |
| 11943 | static inline qsizetype qt_string_count(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs) |
| 11944 | { |
| 11945 | qsizetype num = 0; |
| 11946 | qsizetype i = -1; |
| 11947 | if (haystack.size() > 500 && needle.size() > 5) { |
| 11948 | QStringMatcher matcher(needle, cs); |
| 11949 | while ((i = matcher.indexIn(str: haystack, from: i + 1)) != -1) |
| 11950 | ++num; |
| 11951 | } else { |
| 11952 | while ((i = QtPrivate::findString(haystack, from: i + 1, needle, cs)) != -1) |
| 11953 | ++num; |
| 11954 | } |
| 11955 | return num; |
| 11956 | } |
| 11957 | |
| 11958 | static inline qsizetype qt_string_count(QStringView haystack, QChar ch, |
| 11959 | Qt::CaseSensitivity cs) |
| 11960 | { |
| 11961 | ushort c = ch.unicode(); |
| 11962 | qsizetype num = 0; |
| 11963 | const ushort *b = reinterpret_cast<const ushort*>(haystack.data()); |
| 11964 | const ushort *i = b + haystack.size(); |
| 11965 | if (cs == Qt::CaseSensitive) { |
| 11966 | while (i != b) |
| 11967 | if (*--i == c) |
| 11968 | ++num; |
| 11969 | } else { |
| 11970 | c = foldCase(ch: c); |
| 11971 | while (i != b) |
| 11972 | if (foldCase(ch: *(--i)) == c) |
| 11973 | ++num; |
| 11974 | } |
| 11975 | return num; |
| 11976 | } |
| 11977 | |
| 11978 | template <typename Haystack, typename Needle> |
| 11979 | bool qt_starts_with_impl(Haystack haystack, Needle needle, Qt::CaseSensitivity cs) noexcept |
| 11980 | { |
| 11981 | if (haystack.isNull()) |
| 11982 | return needle.isNull(); // historical behavior, consider changing in ### Qt 6. |
| 11983 | const auto haystackLen = haystack.size(); |
| 11984 | const auto needleLen = needle.size(); |
| 11985 | if (haystackLen == 0) |
| 11986 | return needleLen == 0; |
| 11987 | if (needleLen > haystackLen) |
| 11988 | return false; |
| 11989 | |
| 11990 | return qt_compare_strings(haystack.left(needleLen), needle, cs) == 0; |
| 11991 | } |
| 11992 | |
| 11993 | static inline bool qt_starts_with(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs) |
| 11994 | { |
| 11995 | return qt_starts_with_impl(haystack, needle, cs); |
| 11996 | } |
| 11997 | |
| 11998 | static inline bool qt_starts_with(QStringView haystack, QLatin1String needle, Qt::CaseSensitivity cs) |
| 11999 | { |
| 12000 | return qt_starts_with_impl(haystack, needle, cs); |
| 12001 | } |
| 12002 | |
| 12003 | static inline bool qt_starts_with(QStringView haystack, QChar needle, Qt::CaseSensitivity cs) |
| 12004 | { |
| 12005 | return haystack.size() |
| 12006 | && (cs == Qt::CaseSensitive ? haystack.front() == needle |
| 12007 | : foldCase(ch: haystack.front()) == foldCase(ch: needle)); |
| 12008 | } |
| 12009 | |
| 12010 | /*! |
| 12011 | \fn bool QtPrivate::startsWith(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs) |
| 12012 | \since 5.10 |
| 12013 | \fn bool QtPrivate::startsWith(QStringView haystack, QLatin1String needle, Qt::CaseSensitivity cs) |
| 12014 | \since 5.10 |
| 12015 | \fn bool QtPrivate::startsWith(QLatin1String haystack, QStringView needle, Qt::CaseSensitivity cs) |
| 12016 | \since 5.10 |
| 12017 | \fn bool QtPrivate::startsWith(QLatin1String haystack, QLatin1String needle, Qt::CaseSensitivity cs) |
| 12018 | \since 5.10 |
| 12019 | \internal |
| 12020 | \relates QStringView |
| 12021 | |
| 12022 | Returns \c true if \a haystack starts with \a needle, |
| 12023 | otherwise returns \c false. |
| 12024 | |
| 12025 | If \a cs is Qt::CaseSensitive (the default), the search is case-sensitive; |
| 12026 | otherwise the search is case-insensitive. |
| 12027 | |
| 12028 | \sa QtPrivate::endsWith(), QString::endsWith(), QStringView::endsWith(), QLatin1String::endsWith() |
| 12029 | */ |
| 12030 | |
| 12031 | bool QtPrivate::startsWith(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs) noexcept |
| 12032 | { |
| 12033 | return qt_starts_with_impl(haystack, needle, cs); |
| 12034 | } |
| 12035 | |
| 12036 | bool QtPrivate::startsWith(QStringView haystack, QLatin1String needle, Qt::CaseSensitivity cs) noexcept |
| 12037 | { |
| 12038 | return qt_starts_with_impl(haystack, needle, cs); |
| 12039 | } |
| 12040 | |
| 12041 | bool QtPrivate::startsWith(QLatin1String haystack, QStringView needle, Qt::CaseSensitivity cs) noexcept |
| 12042 | { |
| 12043 | return qt_starts_with_impl(haystack, needle, cs); |
| 12044 | } |
| 12045 | |
| 12046 | bool QtPrivate::startsWith(QLatin1String haystack, QLatin1String needle, Qt::CaseSensitivity cs) noexcept |
| 12047 | { |
| 12048 | return qt_starts_with_impl(haystack, needle, cs); |
| 12049 | } |
| 12050 | |
| 12051 | template <typename Haystack, typename Needle> |
| 12052 | bool qt_ends_with_impl(Haystack haystack, Needle needle, Qt::CaseSensitivity cs) noexcept |
| 12053 | { |
| 12054 | if (haystack.isNull()) |
| 12055 | return needle.isNull(); // historical behavior, consider changing in ### Qt 6. |
| 12056 | const auto haystackLen = haystack.size(); |
| 12057 | const auto needleLen = needle.size(); |
| 12058 | if (haystackLen == 0) |
| 12059 | return needleLen == 0; |
| 12060 | if (haystackLen < needleLen) |
| 12061 | return false; |
| 12062 | |
| 12063 | return qt_compare_strings(haystack.right(needleLen), needle, cs) == 0; |
| 12064 | } |
| 12065 | |
| 12066 | static inline bool qt_ends_with(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs) |
| 12067 | { |
| 12068 | return qt_ends_with_impl(haystack, needle, cs); |
| 12069 | } |
| 12070 | |
| 12071 | static inline bool qt_ends_with(QStringView haystack, QLatin1String needle, Qt::CaseSensitivity cs) |
| 12072 | { |
| 12073 | return qt_ends_with_impl(haystack, needle, cs); |
| 12074 | } |
| 12075 | |
| 12076 | static inline bool qt_ends_with(QStringView haystack, QChar needle, Qt::CaseSensitivity cs) |
| 12077 | { |
| 12078 | return haystack.size() |
| 12079 | && (cs == Qt::CaseSensitive ? haystack.back() == needle |
| 12080 | : foldCase(ch: haystack.back()) == foldCase(ch: needle)); |
| 12081 | } |
| 12082 | |
| 12083 | /*! |
| 12084 | \fn bool QtPrivate::endsWith(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs) |
| 12085 | \since 5.10 |
| 12086 | \fn bool QtPrivate::endsWith(QStringView haystack, QLatin1String needle, Qt::CaseSensitivity cs) |
| 12087 | \since 5.10 |
| 12088 | \fn bool QtPrivate::endsWith(QLatin1String haystack, QStringView needle, Qt::CaseSensitivity cs) |
| 12089 | \since 5.10 |
| 12090 | \fn bool QtPrivate::endsWith(QLatin1String haystack, QLatin1String needle, Qt::CaseSensitivity cs) |
| 12091 | \since 5.10 |
| 12092 | \internal |
| 12093 | \relates QStringView |
| 12094 | |
| 12095 | Returns \c true if \a haystack ends with \a needle, |
| 12096 | otherwise returns \c false. |
| 12097 | |
| 12098 | If \a cs is Qt::CaseSensitive (the default), the search is case-sensitive; |
| 12099 | otherwise the search is case-insensitive. |
| 12100 | |
| 12101 | \sa QtPrivate::startsWith(), QString::endsWith(), QStringView::endsWith(), QLatin1String::endsWith() |
| 12102 | */ |
| 12103 | |
| 12104 | bool QtPrivate::endsWith(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs) noexcept |
| 12105 | { |
| 12106 | return qt_ends_with_impl(haystack, needle, cs); |
| 12107 | } |
| 12108 | |
| 12109 | bool QtPrivate::endsWith(QStringView haystack, QLatin1String needle, Qt::CaseSensitivity cs) noexcept |
| 12110 | { |
| 12111 | return qt_ends_with_impl(haystack, needle, cs); |
| 12112 | } |
| 12113 | |
| 12114 | bool QtPrivate::endsWith(QLatin1String haystack, QStringView needle, Qt::CaseSensitivity cs) noexcept |
| 12115 | { |
| 12116 | return qt_ends_with_impl(haystack, needle, cs); |
| 12117 | } |
| 12118 | |
| 12119 | bool QtPrivate::endsWith(QLatin1String haystack, QLatin1String needle, Qt::CaseSensitivity cs) noexcept |
| 12120 | { |
| 12121 | return qt_ends_with_impl(haystack, needle, cs); |
| 12122 | } |
| 12123 | |
| 12124 | namespace { |
| 12125 | template <typename Pointer> |
| 12126 | uint foldCaseHelper(Pointer ch, Pointer start) = delete; |
| 12127 | |
| 12128 | template <> |
| 12129 | uint foldCaseHelper<const QChar*>(const QChar* ch, const QChar* start) |
| 12130 | { |
| 12131 | return foldCase(ch: reinterpret_cast<const ushort*>(ch), start: reinterpret_cast<const ushort*>(start)); |
| 12132 | } |
| 12133 | |
| 12134 | template <> |
| 12135 | uint foldCaseHelper<const char*>(const char* ch, const char*) |
| 12136 | { |
| 12137 | return foldCase(ch: ushort(uchar(*ch))); |
| 12138 | } |
| 12139 | |
| 12140 | template <typename T> |
| 12141 | ushort valueTypeToUtf16(T t) = delete; |
| 12142 | |
| 12143 | template <> |
| 12144 | ushort valueTypeToUtf16<QChar>(QChar t) |
| 12145 | { |
| 12146 | return t.unicode(); |
| 12147 | } |
| 12148 | |
| 12149 | template <> |
| 12150 | ushort valueTypeToUtf16<char>(char t) |
| 12151 | { |
| 12152 | return ushort(uchar(t)); |
| 12153 | } |
| 12154 | } |
| 12155 | |
| 12156 | /*! |
| 12157 | \internal |
| 12158 | |
| 12159 | Returns the index position of the first occurrence of the |
| 12160 | character \a ch in the string given by \a str and \a len, |
| 12161 | searching forward from index |
| 12162 | position \a from. Returns -1 if \a ch could not be found. |
| 12163 | */ |
| 12164 | |
| 12165 | static inline qsizetype qFindChar(QStringView str, QChar ch, qsizetype from, Qt::CaseSensitivity cs) noexcept |
| 12166 | { |
| 12167 | if (from < 0) |
| 12168 | from = qMax(a: from + str.size(), b: qsizetype(0)); |
| 12169 | if (from < str.size()) { |
| 12170 | const ushort *s = (const ushort *)str.data(); |
| 12171 | ushort c = ch.unicode(); |
| 12172 | const ushort *n = s + from; |
| 12173 | const ushort *e = s + str.size(); |
| 12174 | if (cs == Qt::CaseSensitive) { |
| 12175 | n = QtPrivate::qustrchr(str: QStringView(n, e), c); |
| 12176 | if (n != e) |
| 12177 | return n - s; |
| 12178 | } else { |
| 12179 | c = foldCase(ch: c); |
| 12180 | --n; |
| 12181 | while (++n != e) |
| 12182 | if (foldCase(ch: *n) == c) |
| 12183 | return n - s; |
| 12184 | } |
| 12185 | } |
| 12186 | return -1; |
| 12187 | } |
| 12188 | |
| 12189 | qsizetype QtPrivate::findString(QStringView haystack0, qsizetype from, QStringView needle0, Qt::CaseSensitivity cs) noexcept |
| 12190 | { |
| 12191 | const qsizetype l = haystack0.size(); |
| 12192 | const qsizetype sl = needle0.size(); |
| 12193 | if (from < 0) |
| 12194 | from += l; |
| 12195 | if (std::size_t(sl + from) > std::size_t(l)) |
| 12196 | return -1; |
| 12197 | if (!sl) |
| 12198 | return from; |
| 12199 | if (!l) |
| 12200 | return -1; |
| 12201 | |
| 12202 | if (sl == 1) |
| 12203 | return qFindChar(str: haystack0, ch: needle0[0], from, cs); |
| 12204 | |
| 12205 | /* |
| 12206 | We use the Boyer-Moore algorithm in cases where the overhead |
| 12207 | for the skip table should pay off, otherwise we use a simple |
| 12208 | hash function. |
| 12209 | */ |
| 12210 | if (l > 500 && sl > 5) |
| 12211 | return qFindStringBoyerMoore(haystack: haystack0, from, needle: needle0, cs); |
| 12212 | |
| 12213 | auto sv = [sl](const ushort *v) { return QStringView(v, sl); }; |
| 12214 | /* |
| 12215 | We use some hashing for efficiency's sake. Instead of |
| 12216 | comparing strings, we compare the hash value of str with that |
| 12217 | of a part of this QString. Only if that matches, we call |
| 12218 | qt_string_compare(). |
| 12219 | */ |
| 12220 | const ushort *needle = (const ushort *)needle0.data(); |
| 12221 | const ushort *haystack = (const ushort *)(haystack0.data()) + from; |
| 12222 | const ushort *end = (const ushort *)(haystack0.data()) + (l - sl); |
| 12223 | const std::size_t sl_minus_1 = sl - 1; |
| 12224 | std::size_t hashNeedle = 0, hashHaystack = 0; |
| 12225 | qsizetype idx; |
| 12226 | |
| 12227 | if (cs == Qt::CaseSensitive) { |
| 12228 | for (idx = 0; idx < sl; ++idx) { |
| 12229 | hashNeedle = ((hashNeedle<<1) + needle[idx]); |
| 12230 | hashHaystack = ((hashHaystack<<1) + haystack[idx]); |
| 12231 | } |
| 12232 | hashHaystack -= haystack[sl_minus_1]; |
| 12233 | |
| 12234 | while (haystack <= end) { |
| 12235 | hashHaystack += haystack[sl_minus_1]; |
| 12236 | if (hashHaystack == hashNeedle |
| 12237 | && qt_compare_strings(lhs: needle0, rhs: sv(haystack), cs: Qt::CaseSensitive) == 0) |
| 12238 | return haystack - (const ushort *)haystack0.data(); |
| 12239 | |
| 12240 | REHASH(*haystack); |
| 12241 | ++haystack; |
| 12242 | } |
| 12243 | } else { |
| 12244 | const ushort *haystack_start = (const ushort *)haystack0.data(); |
| 12245 | for (idx = 0; idx < sl; ++idx) { |
| 12246 | hashNeedle = (hashNeedle<<1) + foldCase(ch: needle + idx, start: needle); |
| 12247 | hashHaystack = (hashHaystack<<1) + foldCase(ch: haystack + idx, start: haystack_start); |
| 12248 | } |
| 12249 | hashHaystack -= foldCase(ch: haystack + sl_minus_1, start: haystack_start); |
| 12250 | |
| 12251 | while (haystack <= end) { |
| 12252 | hashHaystack += foldCase(ch: haystack + sl_minus_1, start: haystack_start); |
| 12253 | if (hashHaystack == hashNeedle |
| 12254 | && qt_compare_strings(lhs: needle0, rhs: sv(haystack), cs: Qt::CaseInsensitive) == 0) |
| 12255 | return haystack - (const ushort *)haystack0.data(); |
| 12256 | |
| 12257 | REHASH(foldCase(haystack, haystack_start)); |
| 12258 | ++haystack; |
| 12259 | } |
| 12260 | } |
| 12261 | return -1; |
| 12262 | } |
| 12263 | |
| 12264 | template <typename Haystack> |
| 12265 | static inline qsizetype qLastIndexOf(Haystack haystack, QChar needle, |
| 12266 | qsizetype from, Qt::CaseSensitivity cs) noexcept |
| 12267 | { |
| 12268 | if (from < 0) |
| 12269 | from += haystack.size(); |
| 12270 | if (std::size_t(from) >= std::size_t(haystack.size())) |
| 12271 | return -1; |
| 12272 | if (from >= 0) { |
| 12273 | ushort c = needle.unicode(); |
| 12274 | const auto b = haystack.data(); |
| 12275 | auto n = b + from; |
| 12276 | if (cs == Qt::CaseSensitive) { |
| 12277 | for (; n >= b; --n) |
| 12278 | if (valueTypeToUtf16(*n) == c) |
| 12279 | return n - b; |
| 12280 | } else { |
| 12281 | c = foldCase(ch: c); |
| 12282 | for (; n >= b; --n) |
| 12283 | if (foldCase(valueTypeToUtf16(*n)) == c) |
| 12284 | return n - b; |
| 12285 | } |
| 12286 | } |
| 12287 | return -1; |
| 12288 | } |
| 12289 | |
| 12290 | template<typename Haystack, typename Needle> |
| 12291 | static qsizetype qLastIndexOf(Haystack haystack0, qsizetype from, |
| 12292 | Needle needle0, Qt::CaseSensitivity cs) noexcept |
| 12293 | { |
| 12294 | const qsizetype sl = needle0.size(); |
| 12295 | if (sl == 1) |
| 12296 | return qLastIndexOf(haystack0, needle0.front(), from, cs); |
| 12297 | |
| 12298 | const qsizetype l = haystack0.size(); |
| 12299 | if (from < 0) |
| 12300 | from += l; |
| 12301 | if (from == l && sl == 0) |
| 12302 | return from; |
| 12303 | const qsizetype delta = l - sl; |
| 12304 | if (std::size_t(from) >= std::size_t(l) || delta < 0) |
| 12305 | return -1; |
| 12306 | if (from > delta) |
| 12307 | from = delta; |
| 12308 | |
| 12309 | auto sv = [sl](const typename Haystack::value_type *v) { return Haystack(v, sl); }; |
| 12310 | |
| 12311 | auto haystack = haystack0.data(); |
| 12312 | const auto needle = needle0.data(); |
| 12313 | const auto *end = haystack; |
| 12314 | haystack += from; |
| 12315 | const std::size_t sl_minus_1 = sl ? sl - 1 : 0; |
| 12316 | const auto *n = needle + sl_minus_1; |
| 12317 | const auto *h = haystack + sl_minus_1; |
| 12318 | std::size_t hashNeedle = 0, hashHaystack = 0; |
| 12319 | qsizetype idx; |
| 12320 | |
| 12321 | if (cs == Qt::CaseSensitive) { |
| 12322 | for (idx = 0; idx < sl; ++idx) { |
| 12323 | hashNeedle = (hashNeedle << 1) + valueTypeToUtf16(*(n - idx)); |
| 12324 | hashHaystack = (hashHaystack << 1) + valueTypeToUtf16(*(h - idx)); |
| 12325 | } |
| 12326 | hashHaystack -= valueTypeToUtf16(*haystack); |
| 12327 | |
| 12328 | while (haystack >= end) { |
| 12329 | hashHaystack += valueTypeToUtf16(*haystack); |
| 12330 | if (hashHaystack == hashNeedle |
| 12331 | && qt_compare_strings(needle0, sv(haystack), Qt::CaseSensitive) == 0) |
| 12332 | return haystack - end; |
| 12333 | --haystack; |
| 12334 | REHASH(valueTypeToUtf16(haystack[sl])); |
| 12335 | } |
| 12336 | } else { |
| 12337 | for (idx = 0; idx < sl; ++idx) { |
| 12338 | hashNeedle = (hashNeedle << 1) + foldCaseHelper(n - idx, needle); |
| 12339 | hashHaystack = (hashHaystack << 1) + foldCaseHelper(h - idx, end); |
| 12340 | } |
| 12341 | hashHaystack -= foldCaseHelper(haystack, end); |
| 12342 | |
| 12343 | while (haystack >= end) { |
| 12344 | hashHaystack += foldCaseHelper(haystack, end); |
| 12345 | if (hashHaystack == hashNeedle |
| 12346 | && qt_compare_strings(sv(haystack), needle0, Qt::CaseInsensitive) == 0) |
| 12347 | return haystack - end; |
| 12348 | --haystack; |
| 12349 | REHASH(foldCaseHelper(haystack + sl, end)); |
| 12350 | } |
| 12351 | } |
| 12352 | return -1; |
| 12353 | } |
| 12354 | |
| 12355 | qsizetype QtPrivate::findString(QStringView haystack, qsizetype from, QLatin1String needle, Qt::CaseSensitivity cs) noexcept |
| 12356 | { |
| 12357 | if (haystack.size() < needle.size()) |
| 12358 | return -1; |
| 12359 | |
| 12360 | QVarLengthArray<ushort> s(needle.size()); |
| 12361 | qt_from_latin1(dst: s.data(), str: needle.latin1(), size: needle.size()); |
| 12362 | return QtPrivate::findString(haystack0: haystack, from, needle0: QStringView(reinterpret_cast<const QChar*>(s.constData()), s.size()), cs); |
| 12363 | } |
| 12364 | |
| 12365 | qsizetype QtPrivate::findString(QLatin1String haystack, qsizetype from, QStringView needle, Qt::CaseSensitivity cs) noexcept |
| 12366 | { |
| 12367 | if (haystack.size() < needle.size()) |
| 12368 | return -1; |
| 12369 | |
| 12370 | QVarLengthArray<ushort> s(haystack.size()); |
| 12371 | qt_from_latin1(dst: s.data(), str: haystack.latin1(), size: haystack.size()); |
| 12372 | return QtPrivate::findString(haystack0: QStringView(reinterpret_cast<const QChar*>(s.constData()), s.size()), from, needle0: needle, cs); |
| 12373 | } |
| 12374 | |
| 12375 | qsizetype QtPrivate::findString(QLatin1String haystack, qsizetype from, QLatin1String needle, Qt::CaseSensitivity cs) noexcept |
| 12376 | { |
| 12377 | if (haystack.size() < needle.size()) |
| 12378 | return -1; |
| 12379 | |
| 12380 | QVarLengthArray<ushort> h(haystack.size()); |
| 12381 | qt_from_latin1(dst: h.data(), str: haystack.latin1(), size: haystack.size()); |
| 12382 | QVarLengthArray<ushort> n(needle.size()); |
| 12383 | qt_from_latin1(dst: n.data(), str: needle.latin1(), size: needle.size()); |
| 12384 | return QtPrivate::findString(haystack0: QStringView(reinterpret_cast<const QChar*>(h.constData()), h.size()), from, |
| 12385 | needle0: QStringView(reinterpret_cast<const QChar*>(n.constData()), n.size()), cs); |
| 12386 | } |
| 12387 | |
| 12388 | qsizetype QtPrivate::lastIndexOf(QStringView haystack, qsizetype from, QStringView needle, Qt::CaseSensitivity cs) noexcept |
| 12389 | { |
| 12390 | return qLastIndexOf(haystack0: haystack, from, needle0: needle, cs); |
| 12391 | } |
| 12392 | |
| 12393 | qsizetype QtPrivate::lastIndexOf(QStringView haystack, qsizetype from, QLatin1String needle, Qt::CaseSensitivity cs) noexcept |
| 12394 | { |
| 12395 | return qLastIndexOf(haystack0: haystack, from, needle0: needle, cs); |
| 12396 | } |
| 12397 | |
| 12398 | qsizetype QtPrivate::lastIndexOf(QLatin1String haystack, qsizetype from, QStringView needle, Qt::CaseSensitivity cs) noexcept |
| 12399 | { |
| 12400 | return qLastIndexOf(haystack0: haystack, from, needle0: needle, cs); |
| 12401 | } |
| 12402 | |
| 12403 | qsizetype QtPrivate::lastIndexOf(QLatin1String haystack, qsizetype from, QLatin1String needle, Qt::CaseSensitivity cs) noexcept |
| 12404 | { |
| 12405 | return qLastIndexOf(haystack0: haystack, from, needle0: needle, cs); |
| 12406 | } |
| 12407 | |
| 12408 | /*! |
| 12409 | \since 4.8 |
| 12410 | |
| 12411 | Returns a Latin-1 representation of the string as a QByteArray. |
| 12412 | |
| 12413 | The returned byte array is undefined if the string contains non-Latin1 |
| 12414 | characters. Those characters may be suppressed or replaced with a |
| 12415 | question mark. |
| 12416 | |
| 12417 | \sa toUtf8(), toLocal8Bit(), QTextCodec |
| 12418 | */ |
| 12419 | QByteArray QStringRef::toLatin1() const |
| 12420 | { |
| 12421 | return qt_convert_to_latin1(string: *this); |
| 12422 | } |
| 12423 | |
| 12424 | /*! |
| 12425 | \fn QByteArray QStringRef::toAscii() const |
| 12426 | \since 4.8 |
| 12427 | \deprecated |
| 12428 | |
| 12429 | Returns an 8-bit representation of the string as a QByteArray. |
| 12430 | |
| 12431 | This function does the same as toLatin1(). |
| 12432 | |
| 12433 | Note that, despite the name, this function does not necessarily return an US-ASCII |
| 12434 | (ANSI X3.4-1986) string and its result may not be US-ASCII compatible. |
| 12435 | |
| 12436 | \sa toLatin1(), toUtf8(), toLocal8Bit(), QTextCodec |
| 12437 | */ |
| 12438 | |
| 12439 | /*! |
| 12440 | \since 4.8 |
| 12441 | |
| 12442 | Returns the local 8-bit representation of the string as a |
| 12443 | QByteArray. The returned byte array is undefined if the string |
| 12444 | contains characters not supported by the local 8-bit encoding. |
| 12445 | |
| 12446 | QTextCodec::codecForLocale() is used to perform the conversion from |
| 12447 | Unicode. If the locale encoding could not be determined, this function |
| 12448 | does the same as toLatin1(). |
| 12449 | |
| 12450 | If this string contains any characters that cannot be encoded in the |
| 12451 | locale, the returned byte array is undefined. Those characters may be |
| 12452 | suppressed or replaced by another. |
| 12453 | |
| 12454 | \sa toLatin1(), toUtf8(), QTextCodec |
| 12455 | */ |
| 12456 | QByteArray QStringRef::toLocal8Bit() const |
| 12457 | { |
| 12458 | return qt_convert_to_local_8bit(string: *this); |
| 12459 | } |
| 12460 | |
| 12461 | /*! |
| 12462 | \since 4.8 |
| 12463 | |
| 12464 | Returns a UTF-8 representation of the string as a QByteArray. |
| 12465 | |
| 12466 | UTF-8 is a Unicode codec and can represent all characters in a Unicode |
| 12467 | string like QString. |
| 12468 | |
| 12469 | \sa toLatin1(), toLocal8Bit(), QTextCodec |
| 12470 | */ |
| 12471 | QByteArray QStringRef::toUtf8() const |
| 12472 | { |
| 12473 | return qt_convert_to_utf8(str: *this); |
| 12474 | } |
| 12475 | |
| 12476 | /*! |
| 12477 | \since 4.8 |
| 12478 | |
| 12479 | Returns a UCS-4/UTF-32 representation of the string as a QVector<uint>. |
| 12480 | |
| 12481 | UCS-4 is a Unicode codec and therefore it is lossless. All characters from |
| 12482 | this string will be encoded in UCS-4. Any invalid sequence of code units in |
| 12483 | this string is replaced by the Unicode's replacement character |
| 12484 | (QChar::ReplacementCharacter, which corresponds to \c{U+FFFD}). |
| 12485 | |
| 12486 | The returned vector is not \\0'-terminated. |
| 12487 | |
| 12488 | \sa toUtf8(), toLatin1(), toLocal8Bit(), QTextCodec |
| 12489 | */ |
| 12490 | QVector<uint> QStringRef::toUcs4() const |
| 12491 | { |
| 12492 | return qt_convert_to_ucs4(string: *this); |
| 12493 | } |
| 12494 | |
| 12495 | /*! |
| 12496 | Returns a string that has whitespace removed from the start and |
| 12497 | the end. |
| 12498 | |
| 12499 | Whitespace means any character for which QChar::isSpace() returns |
| 12500 | \c true. This includes the ASCII characters '\\t', '\\n', '\\v', |
| 12501 | '\\f', '\\r', and ' '. |
| 12502 | |
| 12503 | Unlike QString::simplified(), trimmed() leaves internal whitespace alone. |
| 12504 | |
| 12505 | \since 5.1 |
| 12506 | |
| 12507 | \sa QString::trimmed() |
| 12508 | */ |
| 12509 | QStringRef QStringRef::trimmed() const |
| 12510 | { |
| 12511 | const QChar *begin = cbegin(); |
| 12512 | const QChar *end = cend(); |
| 12513 | QStringAlgorithms<const QStringRef>::trimmed_helper_positions(begin, end); |
| 12514 | if (begin == cbegin() && end == cend()) |
| 12515 | return *this; |
| 12516 | int position = m_position + (begin - cbegin()); |
| 12517 | return QStringRef(m_string, position, end - begin); |
| 12518 | } |
| 12519 | |
| 12520 | /*! |
| 12521 | Returns the string converted to a \c{long long} using base \a |
| 12522 | base, which is 10 by default and must be between 2 and 36, or 0. |
| 12523 | Returns 0 if the conversion fails. |
| 12524 | |
| 12525 | If \a ok is not \nullptr, failure is reported by setting *\a{ok} |
| 12526 | to \c false, and success by setting *\a{ok} to \c true. |
| 12527 | |
| 12528 | If \a base is 0, the C language convention is used: If the string |
| 12529 | begins with "0x", base 16 is used; if the string begins with "0", |
| 12530 | base 8 is used; otherwise, base 10 is used. |
| 12531 | |
| 12532 | The string conversion will always happen in the 'C' locale. For locale |
| 12533 | dependent conversion use QLocale::toLongLong() |
| 12534 | |
| 12535 | \sa QString::toLongLong() |
| 12536 | |
| 12537 | \since 5.1 |
| 12538 | */ |
| 12539 | |
| 12540 | qint64 QStringRef::toLongLong(bool *ok, int base) const |
| 12541 | { |
| 12542 | return QString::toIntegral_helper<qint64>(data: constData(), len: size(), ok, base); |
| 12543 | } |
| 12544 | |
| 12545 | /*! |
| 12546 | Returns the string converted to an \c{unsigned long long} using base \a |
| 12547 | base, which is 10 by default and must be between 2 and 36, or 0. |
| 12548 | Returns 0 if the conversion fails. |
| 12549 | |
| 12550 | If \a ok is not \nullptr, failure is reported by setting *\a{ok} |
| 12551 | to \c false, and success by setting *\a{ok} to \c true. |
| 12552 | |
| 12553 | If \a base is 0, the C language convention is used: If the string |
| 12554 | begins with "0x", base 16 is used; if the string begins with "0", |
| 12555 | base 8 is used; otherwise, base 10 is used. |
| 12556 | |
| 12557 | The string conversion will always happen in the 'C' locale. For locale |
| 12558 | dependent conversion use QLocale::toULongLong() |
| 12559 | |
| 12560 | \sa QString::toULongLong() |
| 12561 | |
| 12562 | \since 5.1 |
| 12563 | */ |
| 12564 | |
| 12565 | quint64 QStringRef::toULongLong(bool *ok, int base) const |
| 12566 | { |
| 12567 | return QString::toIntegral_helper<quint64>(data: constData(), len: size(), ok, base); |
| 12568 | } |
| 12569 | |
| 12570 | /*! |
| 12571 | \fn long QStringRef::toLong(bool *ok, int base) const |
| 12572 | |
| 12573 | Returns the string converted to a \c long using base \a |
| 12574 | base, which is 10 by default and must be between 2 and 36, or 0. |
| 12575 | Returns 0 if the conversion fails. |
| 12576 | |
| 12577 | If \a ok is not \nullptr, failure is reported by setting *\a{ok} |
| 12578 | to \c false, and success by setting *\a{ok} to \c true. |
| 12579 | |
| 12580 | If \a base is 0, the C language convention is used: If the string |
| 12581 | begins with "0x", base 16 is used; if the string begins with "0", |
| 12582 | base 8 is used; otherwise, base 10 is used. |
| 12583 | |
| 12584 | The string conversion will always happen in the 'C' locale. For locale |
| 12585 | dependent conversion use QLocale::toLong() |
| 12586 | |
| 12587 | \sa QString::toLong() |
| 12588 | |
| 12589 | \since 5.1 |
| 12590 | */ |
| 12591 | |
| 12592 | long QStringRef::toLong(bool *ok, int base) const |
| 12593 | { |
| 12594 | return QString::toIntegral_helper<long>(data: constData(), len: size(), ok, base); |
| 12595 | } |
| 12596 | |
| 12597 | /*! |
| 12598 | \fn ulong QStringRef::toULong(bool *ok, int base) const |
| 12599 | |
| 12600 | Returns the string converted to an \c{unsigned long} using base \a |
| 12601 | base, which is 10 by default and must be between 2 and 36, or 0. |
| 12602 | Returns 0 if the conversion fails. |
| 12603 | |
| 12604 | If \a ok is not \nullptr, failure is reported by setting *\a{ok} |
| 12605 | to \c false, and success by setting *\a{ok} to \c true. |
| 12606 | |
| 12607 | If \a base is 0, the C language convention is used: If the string |
| 12608 | begins with "0x", base 16 is used; if the string begins with "0", |
| 12609 | base 8 is used; otherwise, base 10 is used. |
| 12610 | |
| 12611 | The string conversion will always happen in the 'C' locale. For locale |
| 12612 | dependent conversion use QLocale::toULongLong() |
| 12613 | |
| 12614 | \sa QString::toULong() |
| 12615 | |
| 12616 | \since 5.1 |
| 12617 | */ |
| 12618 | |
| 12619 | ulong QStringRef::toULong(bool *ok, int base) const |
| 12620 | { |
| 12621 | return QString::toIntegral_helper<ulong>(data: constData(), len: size(), ok, base); |
| 12622 | } |
| 12623 | |
| 12624 | |
| 12625 | /*! |
| 12626 | Returns the string converted to an \c int using base \a |
| 12627 | base, which is 10 by default and must be between 2 and 36, or 0. |
| 12628 | Returns 0 if the conversion fails. |
| 12629 | |
| 12630 | If \a ok is not \nullptr, failure is reported by setting *\a{ok} |
| 12631 | to \c false, and success by setting *\a{ok} to \c true. |
| 12632 | |
| 12633 | If \a base is 0, the C language convention is used: If the string |
| 12634 | begins with "0x", base 16 is used; if the string begins with "0", |
| 12635 | base 8 is used; otherwise, base 10 is used. |
| 12636 | |
| 12637 | The string conversion will always happen in the 'C' locale. For locale |
| 12638 | dependent conversion use QLocale::toInt() |
| 12639 | |
| 12640 | \sa QString::toInt() |
| 12641 | |
| 12642 | \since 5.1 |
| 12643 | */ |
| 12644 | |
| 12645 | int QStringRef::toInt(bool *ok, int base) const |
| 12646 | { |
| 12647 | return QString::toIntegral_helper<int>(data: constData(), len: size(), ok, base); |
| 12648 | } |
| 12649 | |
| 12650 | /*! |
| 12651 | Returns the string converted to an \c{unsigned int} using base \a |
| 12652 | base, which is 10 by default and must be between 2 and 36, or 0. |
| 12653 | Returns 0 if the conversion fails. |
| 12654 | |
| 12655 | If \a ok is not \nullptr, failure is reported by setting *\a{ok} |
| 12656 | to \c false, and success by setting *\a{ok} to \c true. |
| 12657 | |
| 12658 | If \a base is 0, the C language convention is used: If the string |
| 12659 | begins with "0x", base 16 is used; if the string begins with "0", |
| 12660 | base 8 is used; otherwise, base 10 is used. |
| 12661 | |
| 12662 | The string conversion will always happen in the 'C' locale. For locale |
| 12663 | dependent conversion use QLocale::toUInt() |
| 12664 | |
| 12665 | \sa QString::toUInt() |
| 12666 | |
| 12667 | \since 5.1 |
| 12668 | */ |
| 12669 | |
| 12670 | uint QStringRef::toUInt(bool *ok, int base) const |
| 12671 | { |
| 12672 | return QString::toIntegral_helper<uint>(data: constData(), len: size(), ok, base); |
| 12673 | } |
| 12674 | |
| 12675 | /*! |
| 12676 | Returns the string converted to a \c short using base \a |
| 12677 | base, which is 10 by default and must be between 2 and 36, or 0. |
| 12678 | Returns 0 if the conversion fails. |
| 12679 | |
| 12680 | If \a ok is not \nullptr, failure is reported by setting *\a{ok} |
| 12681 | to \c false, and success by setting *\a{ok} to \c true. |
| 12682 | |
| 12683 | If \a base is 0, the C language convention is used: If the string |
| 12684 | begins with "0x", base 16 is used; if the string begins with "0", |
| 12685 | base 8 is used; otherwise, base 10 is used. |
| 12686 | |
| 12687 | The string conversion will always happen in the 'C' locale. For locale |
| 12688 | dependent conversion use QLocale::toShort() |
| 12689 | |
| 12690 | \sa QString::toShort() |
| 12691 | |
| 12692 | \since 5.1 |
| 12693 | */ |
| 12694 | |
| 12695 | short QStringRef::toShort(bool *ok, int base) const |
| 12696 | { |
| 12697 | return QString::toIntegral_helper<short>(data: constData(), len: size(), ok, base); |
| 12698 | } |
| 12699 | |
| 12700 | /*! |
| 12701 | Returns the string converted to an \c{unsigned short} using base \a |
| 12702 | base, which is 10 by default and must be between 2 and 36, or 0. |
| 12703 | Returns 0 if the conversion fails. |
| 12704 | |
| 12705 | If \a ok is not \nullptr, failure is reported by setting *\a{ok} |
| 12706 | to \c false, and success by setting *\a{ok} to \c true. |
| 12707 | |
| 12708 | If \a base is 0, the C language convention is used: If the string |
| 12709 | begins with "0x", base 16 is used; if the string begins with "0", |
| 12710 | base 8 is used; otherwise, base 10 is used. |
| 12711 | |
| 12712 | The string conversion will always happen in the 'C' locale. For locale |
| 12713 | dependent conversion use QLocale::toUShort() |
| 12714 | |
| 12715 | \sa QString::toUShort() |
| 12716 | |
| 12717 | \since 5.1 |
| 12718 | */ |
| 12719 | |
| 12720 | ushort QStringRef::toUShort(bool *ok, int base) const |
| 12721 | { |
| 12722 | return QString::toIntegral_helper<ushort>(data: constData(), len: size(), ok, base); |
| 12723 | } |
| 12724 | |
| 12725 | |
| 12726 | /*! |
| 12727 | Returns the string converted to a \c double value. |
| 12728 | |
| 12729 | Returns an infinity if the conversion overflows or 0.0 if the |
| 12730 | conversion fails for other reasons (e.g. underflow). |
| 12731 | |
| 12732 | If \a ok is not \nullptr, failure is reported by setting *\a{ok} |
| 12733 | to \c false, and success by setting *\a{ok} to \c true. |
| 12734 | |
| 12735 | The string conversion will always happen in the 'C' locale. For locale |
| 12736 | dependent conversion use QLocale::toDouble() |
| 12737 | |
| 12738 | For historic reasons, this function does not handle |
| 12739 | thousands group separators. If you need to convert such numbers, |
| 12740 | use QLocale::toDouble(). |
| 12741 | |
| 12742 | \sa QString::toDouble() |
| 12743 | |
| 12744 | \since 5.1 |
| 12745 | */ |
| 12746 | |
| 12747 | double QStringRef::toDouble(bool *ok) const |
| 12748 | { |
| 12749 | return QLocaleData::c()->stringToDouble(str: *this, ok, options: QLocale::RejectGroupSeparator); |
| 12750 | } |
| 12751 | |
| 12752 | /*! |
| 12753 | Returns the string converted to a \c float value. |
| 12754 | |
| 12755 | Returns an infinity if the conversion overflows or 0.0 if the |
| 12756 | conversion fails for other reasons (e.g. underflow). |
| 12757 | |
| 12758 | If \a ok is not \nullptr, failure is reported by setting *\a{ok} |
| 12759 | to \c false, and success by setting *\a{ok} to \c true. |
| 12760 | |
| 12761 | The string conversion will always happen in the 'C' locale. For locale |
| 12762 | dependent conversion use QLocale::toFloat() |
| 12763 | |
| 12764 | \sa QString::toFloat() |
| 12765 | |
| 12766 | \since 5.1 |
| 12767 | */ |
| 12768 | |
| 12769 | float QStringRef::toFloat(bool *ok) const |
| 12770 | { |
| 12771 | return QLocaleData::convertDoubleToFloat(d: toDouble(ok), ok); |
| 12772 | } |
| 12773 | |
| 12774 | /*! |
| 12775 | \obsolete |
| 12776 | \fn QString Qt::escape(const QString &plain) |
| 12777 | |
| 12778 | Use QString::toHtmlEscaped() instead. |
| 12779 | */ |
| 12780 | |
| 12781 | /*! |
| 12782 | \since 5.0 |
| 12783 | |
| 12784 | Converts a plain text string to an HTML string with |
| 12785 | HTML metacharacters \c{<}, \c{>}, \c{&}, and \c{"} replaced by HTML |
| 12786 | entities. |
| 12787 | |
| 12788 | Example: |
| 12789 | |
| 12790 | \snippet code/src_corelib_tools_qstring.cpp 7 |
| 12791 | */ |
| 12792 | QString QString::toHtmlEscaped() const |
| 12793 | { |
| 12794 | QString rich; |
| 12795 | const int len = length(); |
| 12796 | rich.reserve(asize: int(len * 1.1)); |
| 12797 | for (int i = 0; i < len; ++i) { |
| 12798 | if (at(i) == QLatin1Char('<')) |
| 12799 | rich += QLatin1String("<" ); |
| 12800 | else if (at(i) == QLatin1Char('>')) |
| 12801 | rich += QLatin1String(">" ); |
| 12802 | else if (at(i) == QLatin1Char('&')) |
| 12803 | rich += QLatin1String("&" ); |
| 12804 | else if (at(i) == QLatin1Char('"')) |
| 12805 | rich += QLatin1String(""" ); |
| 12806 | else |
| 12807 | rich += at(i); |
| 12808 | } |
| 12809 | rich.squeeze(); |
| 12810 | return rich; |
| 12811 | } |
| 12812 | |
| 12813 | /*! |
| 12814 | \macro QStringLiteral(str) |
| 12815 | \relates QString |
| 12816 | |
| 12817 | The macro generates the data for a QString out of the string literal \a str |
| 12818 | at compile time. Creating a QString from it is free in this case, and the |
| 12819 | generated string data is stored in the read-only segment of the compiled |
| 12820 | object file. |
| 12821 | |
| 12822 | If you have code that looks like this: |
| 12823 | |
| 12824 | \snippet code/src_corelib_tools_qstring.cpp 9 |
| 12825 | |
| 12826 | then a temporary QString will be created to be passed as the \c{hasAttribute} |
| 12827 | function parameter. This can be quite expensive, as it involves a memory |
| 12828 | allocation and the copy/conversion of the data into QString's internal |
| 12829 | encoding. |
| 12830 | |
| 12831 | This cost can be avoided by using QStringLiteral instead: |
| 12832 | |
| 12833 | \snippet code/src_corelib_tools_qstring.cpp 10 |
| 12834 | |
| 12835 | In this case, QString's internal data will be generated at compile time; no |
| 12836 | conversion or allocation will occur at runtime. |
| 12837 | |
| 12838 | Using QStringLiteral instead of a double quoted plain C++ string literal can |
| 12839 | significantly speed up creation of QString instances from data known at |
| 12840 | compile time. |
| 12841 | |
| 12842 | \note QLatin1String can still be more efficient than QStringLiteral |
| 12843 | when the string is passed to a function that has an overload taking |
| 12844 | QLatin1String and this overload avoids conversion to QString. For |
| 12845 | instance, QString::operator==() can compare to a QLatin1String |
| 12846 | directly: |
| 12847 | |
| 12848 | \snippet code/src_corelib_tools_qstring.cpp 11 |
| 12849 | |
| 12850 | \note Some compilers have bugs encoding strings containing characters outside |
| 12851 | the US-ASCII character set. Make sure you prefix your string with \c{u} in |
| 12852 | those cases. It is optional otherwise. |
| 12853 | |
| 12854 | \sa QByteArrayLiteral |
| 12855 | */ |
| 12856 | |
| 12857 | /*! |
| 12858 | \internal |
| 12859 | */ |
| 12860 | void QAbstractConcatenable::appendLatin1To(const char *a, int len, QChar *out) noexcept |
| 12861 | { |
| 12862 | qt_from_latin1(dst: reinterpret_cast<ushort *>(out), str: a, size: uint(len)); |
| 12863 | } |
| 12864 | |
| 12865 | QT_END_NAMESPACE |
| 12866 | |