Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qanystringview.cpp
Go to the documentation of this file.
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
6#include "qdebug.h"
7#include "qttypetraits.h"
8
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:
358static_assert(QtPrivate::IsContainerCompatibleWithQStringView<QAnyStringView>::value == false);
359static_assert(QtPrivate::IsContainerCompatibleWithQUtf8StringView<QAnyStringView>::value == false);
360
361/*!
362 \fn QString QAnyStringView::toString() const
363
364 Returns a deep copy of this string view's data as a QString.
365
366 The return value will be a null QString if and only if this string view is null.
367*/
368
369/*!
370 \fn const void *QAnyStringView::data() const
371
372 Returns a const pointer to the first character in the string view.
373
374 \note The character array represented by the return value is \e not null-terminated.
375
376 \sa size_bytes()
377*/
378
379/*!
380 \fn bool QAnyStringView::empty() const
381
382 Returns whether this string view is empty - that is, whether \c{size() == 0}.
383
384 This function is provided for STL compatibility.
385
386 \sa isEmpty(), isNull(), size()
387*/
388
389/*!
390 \fn bool QAnyStringView::isEmpty() const
391
392 Returns whether this string view is empty - that is, whether \c{size() == 0}.
393
394 This function is provided for compatibility with other Qt containers.
395
396 \sa empty(), isNull(), size()
397*/
398
399/*!
400 \fn bool QAnyStringView::isNull() const
401
402 Returns whether this string view is null - that is, whether \c{data() == nullptr}.
403
404 This functions is provided for compatibility with other Qt containers.
405
406 \sa empty(), isEmpty(), size()
407*/
408
409/*!
410 \fn qsizetype QAnyStringView::size() const
411
412 Returns the size of this string view, in the encoding's code points.
413
414 \sa empty(), isEmpty(), isNull(), size_bytes(), {Sizes and Sub-Strings}
415*/
416
417/*!
418 \fn QAnyStringView::size_bytes() const
419
420 Returns the size of this string view, but in bytes, not code-points.
421
422 You can use this function together with data() for hashing or serialization.
423
424 This function is provided for STL compatibility.
425
426 \sa size(), data()
427*/
428
429/*!
430 \fn QAnyStringView::length() const
431
432 Same as size().
433
434 This function is provided for compatibility with other Qt containers.
435
436 \sa size()
437*/
438
439/*!
440 \fn QChar QAnyStringView::front() const
441
442 Returns the first character in the string view.
443
444 This function is provided for STL compatibility.
445
446 \warning Calling this function on an empty string view constitutes
447 undefined behavior.
448
449 \sa back(), {Sizes and Sub-Strings}
450*/
451
452/*!
453 \fn QChar QAnyStringView::back() const
454
455 Returns the last character in the string view.
456
457 This function is provided for STL compatibility.
458
459 \warning Calling this function on an empty string view constitutes
460 undefined behavior.
461
462 \sa front(), {Sizes and Sub-Strings}
463*/
464
465/*!
466 \fn QAnyStringView::mid(qsizetype pos, qsizetype n) const
467 \since 6.5
468
469 Returns the substring of length \a n starting at position
470 \a pos in this object.
471
472 \deprecated [6.5] Use sliced() instead in new code.
473
474 Returns an empty string view if \a n exceeds the
475 length of the string view. If there are less than \a n code points
476 available in the string view starting at \a pos, or if
477 \a n is negative (default), the function returns all code points that
478 are available from \a pos.
479
480 \sa first(), last(), sliced(), chopped(), chop(), truncate(), slice(), {Sizes and Sub-Strings}
481*/
482
483/*!
484 \fn QAnyStringView::left(qsizetype n) const
485 \since 6.5
486
487 \deprecated [6.5] Use first() instead in new code.
488
489 Returns the substring of length \a n starting at position
490 0 in this object.
491
492 The entire string view is returned if \a n is greater than or equal
493 to size(), or less than zero.
494
495 \sa first(), last(), sliced(), chopped(), chop(), truncate(), slice(), {Sizes and Sub-Strings}
496*/
497
498/*!
499 \fn QAnyStringView::right(qsizetype n) const
500 \since 6.5
501
502 \deprecated [6.5] Use last() instead in new code.
503
504 Returns the substring of length \a n starting at position
505 size() - \a n in this object.
506
507 The entire string view is returned if \a n is greater than or equal
508 to size(), or less than zero.
509
510 \sa first(), last(), sliced(), chopped(), chop(), truncate(), slice(), {Sizes and Sub-Strings}
511*/
512
513/*!
514 \fn QAnyStringView::first(qsizetype n) const
515 \since 6.5
516
517 Returns a string view that contains the first \a n code points
518 of this string view.
519
520 \note The behavior is undefined when \a n < 0 or \a n > size().
521
522 \sa last(), sliced(), chopped(), chop(), truncate(), slice(), {Sizes and Sub-Strings}
523*/
524
525/*!
526 \fn QAnyStringView::last(qsizetype n) const
527 \since 6.5
528
529 Returns a string view that contains the last \a n code points of this string view.
530
531 \note The behavior is undefined when \a n < 0 or \a n > size().
532
533 \sa first(), sliced(), chopped(), chop(), truncate(), slice(), {Sizes and Sub-Strings}
534*/
535
536/*!
537 \fn QAnyStringView::sliced(qsizetype pos, qsizetype n) const
538 \since 6.5
539
540 Returns a string view containing \a n code points of this string view,
541 starting at position \a pos.
542
543//! [UB-sliced-index-length]
544 \note The behavior is undefined when \a pos < 0, \a n < 0,
545 or \a pos + \a n > size().
546//! [UB-sliced-index-length]
547
548 \sa first(), last(), chopped(), chop(), truncate(), slice(), {Sizes and Sub-Strings}
549*/
550
551/*!
552 \fn QAnyStringView::sliced(qsizetype pos) const
553 \since 6.5
554
555 Returns a string view starting at position \a pos in this object,
556 and extending to its end.
557
558//! [UB-sliced-index-only]
559 \note The behavior is undefined when \a pos < 0 or \a pos > size().
560//! [UB-sliced-index-only]
561
562 \sa first(), last(), chopped(), chop(), truncate(), slice(), {Sizes and Sub-Strings}
563*/
564
565/*!
566 \fn QAnyStringView &QAnyStringView::slice(qsizetype pos, qsizetype n)
567 \since 6.8
568
569 Modifies this string view to start at position \a pos, extending for
570 \a n code points.
571
572 \include qanystringview.cpp UB-sliced-index-length
573
574 \sa sliced(), first(), last(), chopped(), chop(), truncate(), {Sizes and Sub-Strings}
575*/
576
577/*!
578 \fn QAnyStringView &QAnyStringView::slice(qsizetype pos)
579 \since 6.8
580 \overload
581
582 Modifies this string view to start at position \a pos, extending to
583 its end.
584
585 \include qanystringview.cpp UB-sliced-index-only
586
587 \sa sliced(), first(), last(), chopped(), chop(), truncate(), {Sizes and Sub-Strings}
588*/
589
590/*!
591 \fn QAnyStringView::chopped(qsizetype n) const
592 \since 6.5
593
594 Returns the substring of length size() - \a n starting at the
595 beginning of this object.
596
597 Same as \c{first(size() - n)}.
598
599 \note The behavior is undefined when \a n < 0 or \a n > size().
600
601 \sa sliced(), first(), last(), chop(), truncate(), slice(), {Sizes and Sub-Strings}
602*/
603
604/*!
605 \fn QAnyStringView::truncate(qsizetype n)
606 \since 6.5
607
608 Truncates this string view to \a n code points.
609
610 Same as \c{*this = first(n)}.
611
612 \note The behavior is undefined when \a n < 0 or \a n > size().
613
614 \sa sliced(), first(), last(), chopped(), chop(), {Sizes and Sub-Strings}
615*/
616
617/*!
618 \fn QAnyStringView::chop(qsizetype n)
619 \since 6.5
620
621 Truncates this string view by \a n code points.
622
623 Same as \c{*this = first(size() - n)}.
624
625 \note The behavior is undefined when \a n < 0 or \a n > size().
626
627 \sa sliced(), first(), last(), chopped(), truncate(), slice(), {Sizes and Sub-Strings}
628*/
629
630/*! \fn template <typename Visitor> decltype(auto) QAnyStringView::visit(Visitor &&v) const
631
632 Calls \a v with either a QUtf8StringView, QLatin1String, or QStringView, depending
633 on the encoding of the string data this string-view references.
634
635 This is how most functions taking QAnyStringView fork off into per-encoding
636 functions:
637
638 \code
639 void processImpl(QLatin1String s) { ~~~ }
640 void processImpl(QUtf8StringView s) { ~~~ }
641 void processImpl(QStringView s) { ~~~ }
642
643 void process(QAnyStringView s)
644 {
645 s.visit([](auto s) { processImpl(s); });
646 }
647 \endcode
648
649 Here, we're reusing the same name, \c s, for both the QAnyStringView
650 object, as well as the lambda's parameter. This is idiomatic code and helps
651 track the identity of the objects through visit() calls, for example in more
652 complex situations such as
653
654 \code
655 bool equal(QAnyStringView lhs, QAnyStringView rhs)
656 {
657 // assuming operator==(QAnyStringView, QAnyStringView) didn't, yet, exist:
658 return lhs.visit([rhs](auto lhs) {
659 rhs.visit([lhs](auto rhs) {
660 return lhs == rhs;
661 });
662 });
663 }
664 \endcode
665
666 visit() requires that all lambda instantiations have the same return type.
667 If they differ, you get a compile error, even if there is a common type. To
668 fix, you can use explicit return types on the lambda, or cast in the return
669 statements:
670
671 \code
672 // wrong:
673 QAnyStringView firstHalf(QAnyStringView input)
674 {
675 return input.visit([](auto input) { // ERROR: lambdas return different types
676 return input.sliced(0, input.size() / 2);
677 });
678 }
679 // correct:
680 QAnyStringView firstHalf(QAnyStringView input)
681 {
682 return input.visit([](auto input) -> QAnyStringView { // OK, explicit return type
683 return input.sliced(0, input.size() / 2);
684 });
685 }
686 // also correct:
687 QAnyStringView firstHalf(QAnyStringView input)
688 {
689 return input.visit([](auto input) {
690 return QAnyStringView(input.sliced(0, input.size() / 2)); // OK, cast to common type
691 });
692 }
693 \endcode
694*/
695
696/*!
697 \fn QAnyStringView::compare(QAnyStringView lhs, QAnyStringView rhs, Qt::CaseSensitivity cs)
698
699 Compares the string view \a lhs with the string view \a rhs and returns a
700 negative integer if \a lhs is less than \a rhs, a positive integer if it is
701 greater than \a rhs, and zero if they are equal.
702
703 If \a cs is Qt::CaseSensitive (the default), the comparison is case sensitive;
704 otherwise the comparison is case-insensitive.
705
706 \sa operator==(), operator<(), operator>()
707*/
708
709/*!
710 \fn bool QAnyStringView::operator==(const QAnyStringView &lhs, const QAnyStringView & rhs)
711 \fn bool QAnyStringView::operator!=(const QAnyStringView & lhs, const QAnyStringView & rhs)
712 \fn bool QAnyStringView::operator<=(const QAnyStringView & lhs, const QAnyStringView & rhs)
713 \fn bool QAnyStringView::operator>=(const QAnyStringView & lhs, const QAnyStringView & rhs)
714 \fn bool QAnyStringView::operator<(const QAnyStringView & lhs, const QAnyStringView & rhs)
715 \fn bool QAnyStringView::operator>(const QAnyStringView & lhs, const QAnyStringView & rhs)
716
717 Operators that compare \a lhs to \a rhs.
718
719 \sa compare()
720*/
721
722/*!
723 \fn template <typename QStringLike> qToAnyStringViewIgnoringNull(const QStringLike &s);
724 \since 6.0
725 \internal
726
727 Convert \a s to a QAnyStringView ignoring \c{s.isNull()}.
728
729 Returns a string view that references \a{s}'s data, but is never null.
730
731 This is a faster way to convert a QString or QByteArray to a QAnyStringView,
732 if null QStrings or QByteArrays can legitimately be treated as empty ones.
733
734 \sa QString::isNull(), QAnyStringView
735*/
736
737/*!
738 \fn QAnyStringView::max_size() const
739 \since 6.8
740
741 This function is provided for STL compatibility.
742
743 It returns the maximum number of elements that the string view can
744 theoretically represent. In practice, the number can be much smaller,
745 limited by the amount of memory available to the system.
746
747 \note The returned value is calculated based on the currently used character
748 type, so calling this function on two different views may return different
749 results.
750*/
751
752/*!
753 \fn QAnyStringView::operator<<(QDebug d, QAnyStringView s)
754 \since 6.7
755 \relates QDebug
756
757 Outputs \a s to debug stream \a d.
758
759 If \c{d.quotedString()} is \c true, indicates which encoding the string is
760 in. If you just want the string data, use \l{QAnyStringView::visit()}{visit()} like this:
761
762 \code
763 s.visit([&d) (auto s) { d << s; });
764 \endcode
765
766 \sa QAnyStringView::visit()
767*/
769{
770 struct S { const char *prefix, *suffix; };
771 const auto affixes = s.visit([](auto s) {
772 using View = decltype(s);
773 if constexpr (std::is_same_v<View, QLatin1StringView>) {
774 return S{"", "_L1"};
775 } else if constexpr (std::is_same_v<View, QUtf8StringView>) {
776 return S{"u8", ""};
777 } else if constexpr (std::is_same_v<View, QStringView>) {
778 return S{"u", ""};
779 } else {
780 static_assert(QtPrivate::type_dependent_false<View>());
781 }
782 });
783 const QDebugStateSaver saver(d);
784 d.nospace();
785 if (d.quoteStrings())
786 d << affixes.prefix;
787 s.visit([&d](auto s) { d << s; });
788 if (d.quoteStrings())
789 d << affixes.suffix;
790 return d;
791}
792
793/*!
794 \fn template <typename...Args> QString QAnyStringView::arg(Args &&...args) const
795 \since 6.9
796
797 \include qstringview.cpp qstring-multi-arg
798
799 \sa QString::arg(Args&&...)
800*/
801
802/*!
803 \fn template <typename Char, size_t Size, QAnyStringView::if_compatible_char<Char>> QAnyStringView QAnyStringView::fromArray(const Char (&string)[Size])
804
805 Constructs a string view on the full character string literal \a string
806 of \a Size elements, including any trailing \c{Char(0)}. If you don't want the
807 null-terminator included in the view then you can chop() it off
808 when you are certain it is at the end. Alternatively you can use
809 the constructor overload taking an array literal which will create
810 a view up to, but not including, the first null-terminator in the data.
811
812 \a string must remain valid for the lifetime of this string view
813 object.
814
815 This function will work with any array literal if \c Char is a
816 compatible character type. The compatible character types are: \c QChar, \c ushort, \c
817 char16_t and (on platforms, such as Windows, where it is a 16-bit
818 type) \c wchar_t.
819*/
820
821QT_END_NAMESPACE
\inmodule QtCore
Combined button and popup list for selecting options.
QDebug operator<<(QDebug d, QAnyStringView s)