| 1 | // Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@kdab.com> |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | // Qt-Security score:significant reason:trivial-impl-only |
| 4 | |
| 5 | #include "qanystringview.h" |
| 6 | #include "qdebug.h" |
| 7 | #include "qttypetraits.h" |
| 8 | |
| 9 | QT_BEGIN_NAMESPACE |
| 10 | |
| 11 | /*! |
| 12 | \class QAnyStringView |
| 13 | \inmodule QtCore |
| 14 | \since 6.0 |
| 15 | \brief The QAnyStringView class provides a unified view on Latin-1, UTF-8, |
| 16 | or UTF-16 strings with a read-only subset of the QString API. |
| 17 | \reentrant |
| 18 | \ingroup tools |
| 19 | \ingroup string-processing |
| 20 | |
| 21 | \compares strong |
| 22 | \compareswith strong char16_t QChar {const char16_t *} {const char *} \ |
| 23 | QByteArray QByteArrayView QString QStringView QUtf8StringView \ |
| 24 | QLatin1StringView |
| 25 | \endcompareswith |
| 26 | |
| 27 | A QAnyStringView references a contiguous portion of a string it does |
| 28 | not own. It acts as an interface type to all kinds of strings, |
| 29 | without the need to construct a QString first. |
| 30 | |
| 31 | Unlike QStringView and QUtf8StringView, QAnyStringView can hold |
| 32 | strings of any of the following encodings: UTF-8, UTF-16, and |
| 33 | Latin-1. The latter is supported because Latin-1, unlike UTF-8, |
| 34 | can be efficiently compared to UTF-16 data: a length mismatch |
| 35 | already means the strings cannot be equal. This is not true for |
| 36 | UTF-8/UTF-16 comparisons, because UTF-8 is a variable-length |
| 37 | encoding. |
| 38 | |
| 39 | The string may be represented as an array (or an array-compatible |
| 40 | data-structure such as QString, std::basic_string, etc.) of \c |
| 41 | char, \c char8_t, QChar, \c ushort, \c char16_t or (on platforms, |
| 42 | such as Windows, where it is a 16-bit type) \c wchar_t. |
| 43 | |
| 44 | QAnyStringView is designed as an interface type; its main use-case |
| 45 | is as a function parameter type. When QAnyStringViews are used as |
| 46 | automatic variables or data members, care must be taken to ensure |
| 47 | that the referenced string data (for example, owned by a QString) |
| 48 | outlives the QAnyStringView on all code paths, lest the string |
| 49 | view ends up referencing deleted data. |
| 50 | |
| 51 | For example, |
| 52 | |
| 53 | \code |
| 54 | QAnyStringView str = funcReturningQString(); // return value is a temp |
| 55 | \endcode |
| 56 | |
| 57 | would leave \c{str} referencing the deleted temporary (which constitutes |
| 58 | undefined behavior). This is particularly true for the single-character |
| 59 | constructors: |
| 60 | |
| 61 | \code |
| 62 | QAnyStringView ch = u' '; // u' ' is a temporary |
| 63 | // oops, ch references deleted temporary |
| 64 | \endcode |
| 65 | |
| 66 | In both cases, the solution is to "pin" the temporary to an lvalue and only |
| 67 | then create a QAnyStringView from it: |
| 68 | |
| 69 | \code |
| 70 | const auto r = funcReturningQString(); |
| 71 | QAnyStringView str = r; // ok, `r` outlives `str` |
| 72 | const auto sp = u' '; |
| 73 | QAnyStringView ch = sp; // ok, `sp` outlives `ch` |
| 74 | \endcode |
| 75 | |
| 76 | However, using QAnyStringView as the interface type that it is intended to |
| 77 | be is \e{always} safe, provided the called function's documentation is not |
| 78 | asking for a longer lifetime: |
| 79 | |
| 80 | \code |
| 81 | void func(QAnyStringView s); |
| 82 | func(u' '); |
| 83 | func(functionReturningQString()); |
| 84 | \endcode |
| 85 | |
| 86 | This is why QAnyStringView supports these conversions in the first place. |
| 87 | |
| 88 | When used as an interface type, QAnyStringView allows a single |
| 89 | function to accept a wide variety of string data sources. One |
| 90 | function accepting QAnyStringView thus replaces five function |
| 91 | overloads (taking QString, \c{(const QChar*, qsizetype)}, |
| 92 | QUtf8StringView, QLatin1StringView (but see above), and QChar), while |
| 93 | at the same time enabling even more string data sources to be |
| 94 | passed to the function, such as \c{u8"Hello World"}, a \c char8_t |
| 95 | string literal. |
| 96 | |
| 97 | Like elsewhere in Qt, QAnyStringView assumes \c char data is encoded |
| 98 | in UTF-8, unless it is presented as a QLatin1StringView. |
| 99 | |
| 100 | Since Qt 6.4, however, UTF-8 string literals that are pure US-ASCII are |
| 101 | automatically stored as Latin-1. This is a compile-time check with no |
| 102 | runtime overhead. The feature requires compiling in C++20, or with a recent |
| 103 | GCC. |
| 104 | |
| 105 | QAnyStringViews should be passed by value, not by reference-to-const: |
| 106 | \snippet code/src_corelib_text_qanystringview.cpp 0 |
| 107 | |
| 108 | QAnyStringView can also be used as the return value of a function, |
| 109 | but this is not recommended. QUtf8StringView or QStringView are |
| 110 | better suited as function return values. If you call a function |
| 111 | returning QAnyStringView, take extra care to not keep the |
| 112 | QAnyStringView around longer than the function promises to keep |
| 113 | the referenced string data alive. If in doubt, obtain a strong |
| 114 | reference to the data by calling toString() to convert the |
| 115 | QAnyStringView into a QString. |
| 116 | |
| 117 | QAnyStringView is a \e{Literal Type}. |
| 118 | |
| 119 | \section2 Compatible Character Types |
| 120 | |
| 121 | QAnyStringView accepts strings over a variety of character types: |
| 122 | |
| 123 | \list |
| 124 | \li \c char (both signed and unsigned) |
| 125 | \li \c char8_t (C++20 only) |
| 126 | \li \c char16_t |
| 127 | \li \c wchar_t (where it's a 16-bit type, e.g. Windows) |
| 128 | \li \c ushort |
| 129 | \li \c QChar |
| 130 | \endlist |
| 131 | |
| 132 | The 8-bit character types are interpreted as UTF-8 data (except when |
| 133 | presented as a QLatin1StringView) while the 16-bit character types are |
| 134 | interpreted as UTF-16 data in host byte order (the same as QString). |
| 135 | |
| 136 | The following character types are only supported by the single-character |
| 137 | constructor: |
| 138 | |
| 139 | \list |
| 140 | \li \c QLatin1Char |
| 141 | \li \c QChar::SpecialCharacter |
| 142 | \li \c wchar_t (where it's a 32-bit type, i.e. Unix) (since 6.10) |
| 143 | \li \c char32_t |
| 144 | \endlist |
| 145 | |
| 146 | These character types are internally decomposed into a UTF-16 |
| 147 | sequence (using QChar::fromUcs4() for the last). |
| 148 | |
| 149 | \section2 Sizes and Sub-Strings |
| 150 | |
| 151 | All sizes and positions in QAnyStringView functions are in the |
| 152 | encoding's code units (that is, UTF-16 surrogate pairs count as |
| 153 | two for the purposes of these functions, the same as in QString, |
| 154 | and UTF-8 multibyte sequences count as two, three or four, |
| 155 | depending on their length). |
| 156 | |
| 157 | \sa {Which string class to use?}, QUtf8StringView, QStringView |
| 158 | */ |
| 159 | |
| 160 | /*! |
| 161 | \typedef QAnyStringView::difference_type |
| 162 | |
| 163 | Alias for \c{std::ptrdiff_t}. Provided for compatibility with the STL. |
| 164 | */ |
| 165 | |
| 166 | /*! |
| 167 | \typedef QAnyStringView::size_type |
| 168 | |
| 169 | Alias for qsizetype. Provided for compatibility with the STL. |
| 170 | */ |
| 171 | |
| 172 | /*! |
| 173 | \fn QAnyStringView::QAnyStringView() |
| 174 | |
| 175 | Constructs a null string view. |
| 176 | |
| 177 | \sa isNull() |
| 178 | */ |
| 179 | |
| 180 | /*! |
| 181 | \fn QAnyStringView::QAnyStringView(std::nullptr_t) |
| 182 | |
| 183 | Constructs a null string view. |
| 184 | |
| 185 | \sa isNull() |
| 186 | */ |
| 187 | |
| 188 | /*! |
| 189 | \fn template <typename Char, QAnyStringView::if_compatible_char<Char> = true> QAnyStringView::QAnyStringView(const Char *str, qsizetype len) |
| 190 | |
| 191 | Constructs a string view on \a str with length \a len. |
| 192 | |
| 193 | The range \c{[str,len)} must remain valid for the lifetime of this string view object. |
| 194 | |
| 195 | Passing \nullptr as \a str is safe if \a len is 0, too, and results in a null string view. |
| 196 | |
| 197 | The behavior is undefined if \a len is negative or, when positive, if \a str is \nullptr. |
| 198 | |
| 199 | \constraints \c Char is a compatible character type. |
| 200 | |
| 201 | \sa isNull(), {Compatible Character Types} |
| 202 | */ |
| 203 | |
| 204 | /*! |
| 205 | \fn template <typename Char, QAnyStringView::if_compatible_char<Char> = true> QAnyStringView::QAnyStringView(const Char *first, const Char *last) |
| 206 | |
| 207 | Constructs a string view on \a first with length (\a last - \a first). |
| 208 | |
| 209 | The range \c{[first,last)} must remain valid for the lifetime of |
| 210 | this string view object. |
| 211 | |
| 212 | Passing \nullptr as \a first is safe if \a last is \nullptr, too, |
| 213 | and results in a null string view. |
| 214 | |
| 215 | The behavior is undefined if \a last precedes \a first, or \a first |
| 216 | is \nullptr and \a last is not. |
| 217 | |
| 218 | \constraints \c Char is a compatible character type. |
| 219 | |
| 220 | \sa isNull(), {Compatible Character Types} |
| 221 | */ |
| 222 | |
| 223 | /*! |
| 224 | \fn template <typename Char> QAnyStringView::QAnyStringView(const Char *str) |
| 225 | |
| 226 | Constructs a string view on \a str. The length is determined |
| 227 | by scanning for the first \c{Char(0)}. |
| 228 | |
| 229 | \a str must remain valid for the lifetime of this string view object. |
| 230 | |
| 231 | Passing \nullptr as \a str is safe and results in a null string view. |
| 232 | |
| 233 | \constraints \a str is not an array and \c Char is a |
| 234 | compatible character type. |
| 235 | |
| 236 | \sa isNull(), {Compatible Character Types} |
| 237 | */ |
| 238 | |
| 239 | /*! |
| 240 | \fn template <typename Char, QAnyStringView::if_compatible_char<Char>> QAnyStringView::QAnyStringView(const Char &ch) |
| 241 | |
| 242 | Constructs a string view on the single character \a ch. The length is usually |
| 243 | \c{1} (but see below). |
| 244 | |
| 245 | In general, you must assume that a QAnyStringView thus created will start |
| 246 | to reference stale data at the end of the |
| 247 | \l{https://en.cppreference.com/w/cpp/language/expressions#Full-expressions}{full-expression}, |
| 248 | when temporaries are deleted. That means that using it to pass a single |
| 249 | character to a QAnyStringView-taking function is ok and safe (as long as |
| 250 | the function documentation doesn't ask for a lifetime longer than the |
| 251 | initial call): |
| 252 | |
| 253 | \code |
| 254 | int to_int(QAnyStringView); |
| 255 | int res = to_int(u'9'); // OK, data stays around for the duration of the call |
| 256 | \endcode |
| 257 | |
| 258 | But keeping the object around longer is undefined behavior: |
| 259 | |
| 260 | \code |
| 261 | QAnyStringView ch = u'9'; |
| 262 | int res = to_int(ch); // (silent) ERROR: ch references deleted data |
| 263 | \endcode |
| 264 | |
| 265 | If you need this, prefer |
| 266 | |
| 267 | \code |
| 268 | const auto nine = u'9'; |
| 269 | QAnyStringView ch(nine); // ok, references `nine`, which outlives `ch` |
| 270 | int res = to_int(ch); // 9 |
| 271 | \endcode |
| 272 | |
| 273 | The above is true for all directly supported \l{Compatible Character Types}. |
| 274 | |
| 275 | If \a ch is not one of these types, but merely converts to QChar, e.g. |
| 276 | QChar::SpecialCharacter or QLatin1Char, the QAnyStringView will bind to a |
| 277 | temporary object that will have been deleted at the end of the full |
| 278 | expression, just like in the second example. |
| 279 | |
| 280 | If \a ch cannot be represented in a single UTF-16 code unit (e.g. because |
| 281 | it's a \c{char32_t} value), this constructor decomposes \a ch into two |
| 282 | UFT-16 code units. The resulting QAnyStringView will have a size() of \c{2} |
| 283 | in that case, and the temporary buffer in which the decomposition is stored |
| 284 | is deleted at the end of the full-expression, similar to |
| 285 | |
| 286 | \code |
| 287 | [](char32_t ch, auto &&tmp = QChar::fromUcs4(ch)) { |
| 288 | return QAnyStringView(tmp); |
| 289 | } |
| 290 | \endcode |
| 291 | |
| 292 | The equivalent safe version in this case would be |
| 293 | |
| 294 | \code |
| 295 | const auto decomposed = QChar::fromUcs4(ch); |
| 296 | QAnyStringView ch(decomposed); |
| 297 | \endcode |
| 298 | |
| 299 | \sa QChar::fromUcs4(), {Compatible Character Types} |
| 300 | */ |
| 301 | |
| 302 | /*! |
| 303 | \fn template <typename Char, size_t N> QAnyStringView::QAnyStringView(const Char (&string)[N]) |
| 304 | |
| 305 | Constructs a string view on the character string literal \a string. |
| 306 | The view covers the array until the first \c{Char(0)} is encountered, |
| 307 | or \c N, whichever comes first. |
| 308 | If you need the full array, use fromArray() instead. |
| 309 | |
| 310 | \a string must remain valid for the lifetime of this string view |
| 311 | object. |
| 312 | |
| 313 | \constraints \a |
| 314 | string is an actual array and \c Char is a compatible character |
| 315 | type. |
| 316 | |
| 317 | \sa {Compatible Character Types} |
| 318 | */ |
| 319 | |
| 320 | /*! |
| 321 | \fn QAnyStringView::QAnyStringView(const QString &str) |
| 322 | |
| 323 | Constructs a string view on \a str. |
| 324 | |
| 325 | \c{str.data()} must remain valid for the lifetime of this string view object. |
| 326 | |
| 327 | The string view will be null if and only if \c{str.isNull()}. |
| 328 | */ |
| 329 | |
| 330 | /*! |
| 331 | \fn QAnyStringView::QAnyStringView(const QByteArray &str) |
| 332 | |
| 333 | Constructs a string view on \a str. The data in \a str is interpreted as UTF-8. |
| 334 | |
| 335 | \c{str.data()} must remain valid for the lifetime of this string view object. |
| 336 | |
| 337 | The string view will be null if and only if \c{str.isNull()}. |
| 338 | */ |
| 339 | |
| 340 | /*! |
| 341 | \fn template <typename Container, QAnyStringView::if_compatible_container<Container>> QAnyStringView::QAnyStringView(const Container &str) |
| 342 | |
| 343 | Constructs a string view on \a str. The length is taken from \c{std::size(str)}. |
| 344 | |
| 345 | \c{std::data(str)} must remain valid for the lifetime of this string view object. |
| 346 | |
| 347 | The string view will be empty if and only if \c{std::size(str) == 0}. It is unspecified |
| 348 | whether this constructor can result in a null string view (\c{std::data(str)} would |
| 349 | have to return \nullptr for this). |
| 350 | |
| 351 | \constraints \c Container is a |
| 352 | container with a compatible character type as \c{value_type}. |
| 353 | |
| 354 | \sa isNull(), isEmpty() |
| 355 | */ |
| 356 | |
| 357 | // confirm we don't make an accidental copy constructor: |
| 358 | static_assert(QtPrivate::IsContainerCompatibleWithQStringView<QAnyStringView>::value == false); |
| 359 | static_assert(QtPrivate::IsContainerCompatibleWithQUtf8StringView<QAnyStringView>::value == false); |
| 360 | |
| 361 | /*! |
| 362 | \fn template <typename Char, size_t Size> static QAnyStringView fromArray(const Char (&string)[Size]) noexcept |
| 363 | |
| 364 | Constructs a string view on the full character string literal \a string, |
| 365 | including any trailing \c{Char(0)}. If you don't want the |
| 366 | null-terminator included in the view then you can use the constructor |
| 367 | overload taking a pointer and a size: |
| 368 | |
| 369 | \snippet code/src_corelib_text_qanystringview.cpp 2 |
| 370 | |
| 371 | Alternatively you can use the constructor overload taking an |
| 372 | array literal which will create a view up to, but not including, |
| 373 | the first null-terminator in the data. |
| 374 | |
| 375 | \a string must remain valid for the lifetime of this string view |
| 376 | object. |
| 377 | |
| 378 | This function will work with any array literal if \c Char is a |
| 379 | compatible character type. |
| 380 | */ |
| 381 | |
| 382 | /*! |
| 383 | \fn QString QAnyStringView::toString() const |
| 384 | |
| 385 | Returns a deep copy of this string view's data as a QString. |
| 386 | |
| 387 | The return value will be a null QString if and only if this string view is null. |
| 388 | */ |
| 389 | |
| 390 | /*! |
| 391 | \fn const void *QAnyStringView::data() const |
| 392 | |
| 393 | Returns a const pointer to the first character in the string view. |
| 394 | |
| 395 | \note The character array represented by the return value is \e not null-terminated. |
| 396 | |
| 397 | \sa size_bytes() |
| 398 | */ |
| 399 | |
| 400 | /*! |
| 401 | \fn bool QAnyStringView::empty() const |
| 402 | |
| 403 | Returns whether this string view is empty - that is, whether \c{size() == 0}. |
| 404 | |
| 405 | This function is provided for STL compatibility. |
| 406 | |
| 407 | \sa isEmpty(), isNull(), size() |
| 408 | */ |
| 409 | |
| 410 | /*! |
| 411 | \fn bool QAnyStringView::isEmpty() const |
| 412 | |
| 413 | Returns whether this string view is empty - that is, whether \c{size() == 0}. |
| 414 | |
| 415 | This function is provided for compatibility with other Qt containers. |
| 416 | |
| 417 | \sa empty(), isNull(), size() |
| 418 | */ |
| 419 | |
| 420 | /*! |
| 421 | \fn bool QAnyStringView::isNull() const |
| 422 | |
| 423 | Returns whether this string view is null - that is, whether \c{data() == nullptr}. |
| 424 | |
| 425 | This functions is provided for compatibility with other Qt containers. |
| 426 | |
| 427 | \sa empty(), isEmpty(), size() |
| 428 | */ |
| 429 | |
| 430 | /*! |
| 431 | \fn qsizetype QAnyStringView::size() const |
| 432 | |
| 433 | Returns the size of this string view, in the encoding's code points. |
| 434 | |
| 435 | \sa empty(), isEmpty(), isNull(), size_bytes(), {Sizes and Sub-Strings} |
| 436 | */ |
| 437 | |
| 438 | /*! |
| 439 | \fn QAnyStringView::size_bytes() const |
| 440 | |
| 441 | Returns the size of this string view, but in bytes, not code-points. |
| 442 | |
| 443 | You can use this function together with data() for hashing or serialization. |
| 444 | |
| 445 | This function is provided for STL compatibility. |
| 446 | |
| 447 | \sa size(), data() |
| 448 | */ |
| 449 | |
| 450 | /*! |
| 451 | \fn QAnyStringView::length() const |
| 452 | |
| 453 | Same as size(). |
| 454 | |
| 455 | This function is provided for compatibility with other Qt containers. |
| 456 | |
| 457 | \sa size() |
| 458 | */ |
| 459 | |
| 460 | /*! |
| 461 | \fn QChar QAnyStringView::front() const |
| 462 | |
| 463 | Returns the first character in the string view. |
| 464 | |
| 465 | This function is provided for STL compatibility. |
| 466 | |
| 467 | \warning Calling this function on an empty string view constitutes |
| 468 | undefined behavior. |
| 469 | |
| 470 | \sa back(), {Sizes and Sub-Strings} |
| 471 | */ |
| 472 | |
| 473 | /*! |
| 474 | \fn QChar QAnyStringView::back() const |
| 475 | |
| 476 | Returns the last character in the string view. |
| 477 | |
| 478 | This function is provided for STL compatibility. |
| 479 | |
| 480 | \warning Calling this function on an empty string view constitutes |
| 481 | undefined behavior. |
| 482 | |
| 483 | \sa front(), {Sizes and Sub-Strings} |
| 484 | */ |
| 485 | |
| 486 | /*! |
| 487 | \fn QAnyStringView::mid(qsizetype pos, qsizetype n) const |
| 488 | \since 6.5 |
| 489 | |
| 490 | Returns the substring of length \a n starting at position |
| 491 | \a pos in this object. |
| 492 | |
| 493 | \deprecated Use sliced() instead in new code. |
| 494 | |
| 495 | Returns an empty string view if \a n exceeds the |
| 496 | length of the string view. If there are less than \a n code points |
| 497 | available in the string view starting at \a pos, or if |
| 498 | \a n is negative (default), the function returns all code points that |
| 499 | are available from \a pos. |
| 500 | |
| 501 | \sa first(), last(), sliced(), chopped(), chop(), truncate(), slice(), {Sizes and Sub-Strings} |
| 502 | */ |
| 503 | |
| 504 | /*! |
| 505 | \fn QAnyStringView::left(qsizetype n) const |
| 506 | \since 6.5 |
| 507 | |
| 508 | \deprecated Use first() instead in new code. |
| 509 | |
| 510 | Returns the substring of length \a n starting at position |
| 511 | 0 in this object. |
| 512 | |
| 513 | The entire string view is returned if \a n is greater than or equal |
| 514 | to size(), or less than zero. |
| 515 | |
| 516 | \sa first(), last(), sliced(), chopped(), chop(), truncate(), slice(), {Sizes and Sub-Strings} |
| 517 | */ |
| 518 | |
| 519 | /*! |
| 520 | \fn QAnyStringView::right(qsizetype n) const |
| 521 | \since 6.5 |
| 522 | |
| 523 | \deprecated Use last() instead in new code. |
| 524 | |
| 525 | Returns the substring of length \a n starting at position |
| 526 | size() - \a n in this object. |
| 527 | |
| 528 | The entire string view is returned if \a n is greater than or equal |
| 529 | to size(), or less than zero. |
| 530 | |
| 531 | \sa first(), last(), sliced(), chopped(), chop(), truncate(), slice(), {Sizes and Sub-Strings} |
| 532 | */ |
| 533 | |
| 534 | /*! |
| 535 | \fn QAnyStringView::first(qsizetype n) const |
| 536 | \since 6.5 |
| 537 | |
| 538 | Returns a string view that contains the first \a n code points |
| 539 | of this string view. |
| 540 | |
| 541 | \note The behavior is undefined when \a n < 0 or \a n > size(). |
| 542 | |
| 543 | \sa last(), sliced(), chopped(), chop(), truncate(), slice(), {Sizes and Sub-Strings} |
| 544 | */ |
| 545 | |
| 546 | /*! |
| 547 | \fn QAnyStringView::last(qsizetype n) const |
| 548 | \since 6.5 |
| 549 | |
| 550 | Returns a string view that contains the last \a n code points of this string view. |
| 551 | |
| 552 | \note The behavior is undefined when \a n < 0 or \a n > size(). |
| 553 | |
| 554 | \sa first(), sliced(), chopped(), chop(), truncate(), slice(), {Sizes and Sub-Strings} |
| 555 | */ |
| 556 | |
| 557 | /*! |
| 558 | \fn QAnyStringView::sliced(qsizetype pos, qsizetype n) const |
| 559 | \since 6.5 |
| 560 | |
| 561 | Returns a string view containing \a n code points of this string view, |
| 562 | starting at position \a pos. |
| 563 | |
| 564 | //! [UB-sliced-index-length] |
| 565 | \note The behavior is undefined when \a pos < 0, \a n < 0, |
| 566 | or \a pos + \a n > size(). |
| 567 | //! [UB-sliced-index-length] |
| 568 | |
| 569 | \sa first(), last(), chopped(), chop(), truncate(), slice(), {Sizes and Sub-Strings} |
| 570 | */ |
| 571 | |
| 572 | /*! |
| 573 | \fn QAnyStringView::sliced(qsizetype pos) const |
| 574 | \since 6.5 |
| 575 | |
| 576 | Returns a string view starting at position \a pos in this object, |
| 577 | and extending to its end. |
| 578 | |
| 579 | //! [UB-sliced-index-only] |
| 580 | \note The behavior is undefined when \a pos < 0 or \a pos > size(). |
| 581 | //! [UB-sliced-index-only] |
| 582 | |
| 583 | \sa first(), last(), chopped(), chop(), truncate(), slice(), {Sizes and Sub-Strings} |
| 584 | */ |
| 585 | |
| 586 | /*! |
| 587 | \fn QAnyStringView &QAnyStringView::slice(qsizetype pos, qsizetype n) |
| 588 | \since 6.8 |
| 589 | |
| 590 | Modifies this string view to start at position \a pos, extending for |
| 591 | \a n code points. |
| 592 | |
| 593 | \include qanystringview.cpp UB-sliced-index-length |
| 594 | |
| 595 | \sa sliced(), first(), last(), chopped(), chop(), truncate(), {Sizes and Sub-Strings} |
| 596 | */ |
| 597 | |
| 598 | /*! |
| 599 | \fn QAnyStringView &QAnyStringView::slice(qsizetype pos) |
| 600 | \since 6.8 |
| 601 | \overload |
| 602 | |
| 603 | Modifies this string view to start at position \a pos, extending to |
| 604 | its end. |
| 605 | |
| 606 | \include qanystringview.cpp UB-sliced-index-only |
| 607 | |
| 608 | \sa sliced(), first(), last(), chopped(), chop(), truncate(), {Sizes and Sub-Strings} |
| 609 | */ |
| 610 | |
| 611 | /*! |
| 612 | \fn QAnyStringView::chopped(qsizetype n) const |
| 613 | \since 6.5 |
| 614 | |
| 615 | Returns the substring of length size() - \a n starting at the |
| 616 | beginning of this object. |
| 617 | |
| 618 | Same as \c{first(size() - n)}. |
| 619 | |
| 620 | \note The behavior is undefined when \a n < 0 or \a n > size(). |
| 621 | |
| 622 | \sa sliced(), first(), last(), chop(), truncate(), slice(), {Sizes and Sub-Strings} |
| 623 | */ |
| 624 | |
| 625 | /*! |
| 626 | \fn QAnyStringView::truncate(qsizetype n) |
| 627 | \since 6.5 |
| 628 | |
| 629 | Truncates this string view to \a n code points. |
| 630 | |
| 631 | Same as \c{*this = first(n)}. |
| 632 | |
| 633 | \note The behavior is undefined when \a n < 0 or \a n > size(). |
| 634 | |
| 635 | \sa sliced(), first(), last(), chopped(), chop(), {Sizes and Sub-Strings} |
| 636 | */ |
| 637 | |
| 638 | /*! |
| 639 | \fn QAnyStringView::chop(qsizetype n) |
| 640 | \since 6.5 |
| 641 | |
| 642 | Truncates this string view by \a n code points. |
| 643 | |
| 644 | Same as \c{*this = first(size() - n)}. |
| 645 | |
| 646 | \note The behavior is undefined when \a n < 0 or \a n > size(). |
| 647 | |
| 648 | \sa sliced(), first(), last(), chopped(), truncate(), slice(), {Sizes and Sub-Strings} |
| 649 | */ |
| 650 | |
| 651 | /*! \fn template <typename Visitor> decltype(auto) QAnyStringView::visit(Visitor &&v) const |
| 652 | |
| 653 | Calls \a v with either a QUtf8StringView, QLatin1String, or QStringView, depending |
| 654 | on the encoding of the string data this string-view references. |
| 655 | |
| 656 | This is how most functions taking QAnyStringView fork off into per-encoding |
| 657 | functions: |
| 658 | |
| 659 | \code |
| 660 | void processImpl(QLatin1String s) { ~~~ } |
| 661 | void processImpl(QUtf8StringView s) { ~~~ } |
| 662 | void processImpl(QStringView s) { ~~~ } |
| 663 | |
| 664 | void process(QAnyStringView s) |
| 665 | { |
| 666 | s.visit([](auto s) { processImpl(s); }); |
| 667 | } |
| 668 | \endcode |
| 669 | |
| 670 | Here, we're reusing the same name, \c s, for both the QAnyStringView |
| 671 | object, as well as the lambda's parameter. This is idiomatic code and helps |
| 672 | track the identity of the objects through visit() calls, for example in more |
| 673 | complex situations such as |
| 674 | |
| 675 | \code |
| 676 | bool equal(QAnyStringView lhs, QAnyStringView rhs) |
| 677 | { |
| 678 | // assuming operator==(QAnyStringView, QAnyStringView) didn't, yet, exist: |
| 679 | return lhs.visit([rhs](auto lhs) { |
| 680 | rhs.visit([lhs](auto rhs) { |
| 681 | return lhs == rhs; |
| 682 | }); |
| 683 | }); |
| 684 | } |
| 685 | \endcode |
| 686 | |
| 687 | visit() requires that all lambda instantiations have the same return type. |
| 688 | If they differ, you get a compile error, even if there is a common type. To |
| 689 | fix, you can use explicit return types on the lambda, or cast in the return |
| 690 | statements: |
| 691 | |
| 692 | \code |
| 693 | // wrong: |
| 694 | QAnyStringView firstHalf(QAnyStringView input) |
| 695 | { |
| 696 | return input.visit([](auto input) { // ERROR: lambdas return different types |
| 697 | return input.sliced(0, input.size() / 2); |
| 698 | }); |
| 699 | } |
| 700 | // correct: |
| 701 | QAnyStringView firstHalf(QAnyStringView input) |
| 702 | { |
| 703 | return input.visit([](auto input) -> QAnyStringView { // OK, explicit return type |
| 704 | return input.sliced(0, input.size() / 2); |
| 705 | }); |
| 706 | } |
| 707 | // also correct: |
| 708 | QAnyStringView firstHalf(QAnyStringView input) |
| 709 | { |
| 710 | return input.visit([](auto input) { |
| 711 | return QAnyStringView(input.sliced(0, input.size() / 2)); // OK, cast to common type |
| 712 | }); |
| 713 | } |
| 714 | \endcode |
| 715 | */ |
| 716 | |
| 717 | /*! |
| 718 | \fn QAnyStringView::compare(QAnyStringView lhs, QAnyStringView rhs, Qt::CaseSensitivity cs) |
| 719 | |
| 720 | Compares the string view \a lhs with the string view \a rhs and returns a |
| 721 | negative integer if \a lhs is less than \a rhs, a positive integer if it is |
| 722 | greater than \a rhs, and zero if they are equal. |
| 723 | |
| 724 | If \a cs is Qt::CaseSensitive (the default), the comparison is case sensitive; |
| 725 | otherwise the comparison is case-insensitive. |
| 726 | |
| 727 | \sa operator==(), operator<(), operator>() |
| 728 | */ |
| 729 | |
| 730 | /*! |
| 731 | \fn bool QAnyStringView::operator==(const QAnyStringView &lhs, const QAnyStringView & rhs) |
| 732 | \fn bool QAnyStringView::operator!=(const QAnyStringView & lhs, const QAnyStringView & rhs) |
| 733 | \fn bool QAnyStringView::operator<=(const QAnyStringView & lhs, const QAnyStringView & rhs) |
| 734 | \fn bool QAnyStringView::operator>=(const QAnyStringView & lhs, const QAnyStringView & rhs) |
| 735 | \fn bool QAnyStringView::operator<(const QAnyStringView & lhs, const QAnyStringView & rhs) |
| 736 | \fn bool QAnyStringView::operator>(const QAnyStringView & lhs, const QAnyStringView & rhs) |
| 737 | |
| 738 | Operators that compare \a lhs to \a rhs. |
| 739 | |
| 740 | \sa compare() |
| 741 | */ |
| 742 | |
| 743 | /*! |
| 744 | \fn template <typename QStringLike> qToAnyStringViewIgnoringNull(const QStringLike &s); |
| 745 | \since 6.0 |
| 746 | \internal |
| 747 | |
| 748 | Convert \a s to a QAnyStringView ignoring \c{s.isNull()}. |
| 749 | |
| 750 | Returns a string view that references \a{s}'s data, but is never null. |
| 751 | |
| 752 | This is a faster way to convert a QString or QByteArray to a QAnyStringView, |
| 753 | if null QStrings or QByteArrays can legitimately be treated as empty ones. |
| 754 | |
| 755 | \sa QString::isNull(), QAnyStringView |
| 756 | */ |
| 757 | |
| 758 | /*! |
| 759 | \fn QAnyStringView::max_size() const |
| 760 | \since 6.8 |
| 761 | |
| 762 | This function is provided for STL compatibility. |
| 763 | |
| 764 | It returns the maximum number of elements that the string view can |
| 765 | theoretically represent. In practice, the number can be much smaller, |
| 766 | limited by the amount of memory available to the system. |
| 767 | |
| 768 | \note The returned value is calculated based on the currently used character |
| 769 | type, so calling this function on two different views may return different |
| 770 | results. |
| 771 | */ |
| 772 | |
| 773 | /*! |
| 774 | \fn QAnyStringView::operator<<(QDebug d, QAnyStringView s) |
| 775 | \since 6.7 |
| 776 | \relates QDebug |
| 777 | |
| 778 | Outputs \a s to debug stream \a d. |
| 779 | |
| 780 | If \c{d.quotedString()} is \c true, indicates which encoding the string is |
| 781 | in. If you just want the string data, use visit() like this: |
| 782 | |
| 783 | \code |
| 784 | s.visit([&d) (auto s) { d << s; }); |
| 785 | \endcode |
| 786 | |
| 787 | \sa QAnyStringView::visit() |
| 788 | */ |
| 789 | QDebug operator<<(QDebug d, QAnyStringView s) |
| 790 | { |
| 791 | struct S { const char *prefix, *suffix; }; |
| 792 | const auto affixes = s.visit(v: [](auto s) { |
| 793 | using View = decltype(s); |
| 794 | if constexpr (std::is_same_v<View, QLatin1StringView>) { |
| 795 | return S{.prefix: "" , .suffix: "_L1" }; |
| 796 | } else if constexpr (std::is_same_v<View, QUtf8StringView>) { |
| 797 | return S{.prefix: "u8" , .suffix: "" }; |
| 798 | } else if constexpr (std::is_same_v<View, QStringView>) { |
| 799 | return S{.prefix: "u" , .suffix: "" }; |
| 800 | } else { |
| 801 | static_assert(QtPrivate::type_dependent_false<View>()); |
| 802 | } |
| 803 | }); |
| 804 | const QDebugStateSaver saver(d); |
| 805 | d.nospace(); |
| 806 | if (d.quoteStrings()) |
| 807 | d << affixes.prefix; |
| 808 | s.visit(v: [&d](auto s) { d << s; }); |
| 809 | if (d.quoteStrings()) |
| 810 | d << affixes.suffix; |
| 811 | return d; |
| 812 | } |
| 813 | |
| 814 | /*! |
| 815 | \fn template <typename...Args> QString QAnyStringView::arg(Args &&...args) const |
| 816 | \since 6.9 |
| 817 | |
| 818 | \include qstringview.cpp qstring-multi-arg |
| 819 | |
| 820 | \sa QString::arg(Args&&...) |
| 821 | */ |
| 822 | |
| 823 | /*! |
| 824 | \fn template <typename Char, size_t Size, QAnyStringView::if_compatible_char<Char>> QAnyStringView QAnyStringView::fromArray(const Char (&string)[Size]) |
| 825 | |
| 826 | Constructs a string view on the full character string literal \a string, |
| 827 | including any trailing \c{Char(0)}. If you don't want the |
| 828 | null-terminator included in the view then you can chop() it off |
| 829 | when you are certain it is at the end. Alternatively you can use |
| 830 | the constructor overload taking an array literal which will create |
| 831 | a view up to, but not including, the first null-terminator in the data. |
| 832 | |
| 833 | \a string must remain valid for the lifetime of this string view |
| 834 | object. |
| 835 | |
| 836 | This function will work with any array literal if \c Char is a |
| 837 | compatible character type. The compatible character types are: \c QChar, \c ushort, \c |
| 838 | char16_t and (on platforms, such as Windows, where it is a 16-bit |
| 839 | type) \c wchar_t. |
| 840 | */ |
| 841 | |
| 842 | QT_END_NAMESPACE |
| 843 | |