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
qutf8stringview.qdoc
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 GFDL-1.3-no-invariants-only
3// Qt-Security score:significant reason:docs
4
5/*!
6 \class QUtf8StringView
7 \inmodule QtCore
8 \since 6.0
9 \brief The QUtf8StringView class provides a unified view on UTF-8 strings
10 with a read-only subset of the QString API.
11 \reentrant
12 \ingroup tools
13 \ingroup string-processing
14
15 \compares strong
16 \compareswith strong char16_t QChar {const char16_t *} QString QStringView \
17 QLatin1StringView
18 \endcompareswith
19 \compareswith strong {const char *} QByteArray QByteArrayView
20 The contents of byte arrays is interpreted as UTF-8.
21 \endcompareswith
22
23 A QUtf8StringView references a contiguous portion of a UTF-8
24 string it does not own. It acts as an interface type to all kinds
25 of UTF-8 string, without the need to construct a QString or
26 QByteArray first.
27
28 The UTF-8 string may be represented as an array (or an
29 array-compatible data-structure such as std::basic_string, etc.)
30 of \c char8_t, \c char, \c{signed char} or \c{unsigned char}.
31
32 QUtf8StringView is designed as an interface type; its main
33 use-case is as a function parameter type. When QUtf8StringViews
34 are used as automatic variables or data members, care must be
35 taken to ensure that the referenced string data (for example,
36 owned by a std::u8string) outlives the QUtf8StringView on all code
37 paths, lest the string view ends up referencing deleted data.
38
39 When used as an interface type, QUtf8StringView allows a single
40 function to accept a wide variety of UTF-8 string data
41 sources. One function accepting QUtf8StringView thus replaces
42 several function overloads (taking e.g. QByteArray), while at the
43 same time enabling even more string data sources to be passed to
44 the function, such as \c{u8"Hello World"}, a \c char8_t (C++20) or
45 \c char (C++17) string literal. The \c char8_t incompatibility
46 between C++17 and C++20 goes away when using QUtf8StringView.
47
48 Like all views, QUtf8StringViews should be passed by value, not by
49 reference-to-const:
50 \snippet code/src_corelib_text_qutf8stringview.cpp 0
51
52 If you want to give your users maximum freedom in what strings
53 they can pass to your function, consider using QAnyStringView
54 instead.
55
56 QUtf8StringView can also be used as the return value of a
57 function. If you call a function returning QUtf8StringView, take
58 extra care to not keep the QUtf8StringView around longer than the
59 function promises to keep the referenced string data alive. If in
60 doubt, obtain a strong reference to the data by calling toString()
61 to convert the QUtf8StringView into a QString.
62
63 QUtf8StringView is a \e{Literal Type}.
64
65 \section2 Compatible Character Types
66
67 QUtf8StringView accepts strings over a variety of character types:
68
69 \list
70 \li \c char (both signed and unsigned)
71 \li \c char8_t (C++20 only)
72 \endlist
73
74 \section2 Sizes and Sub-Strings
75
76 All sizes and positions in QUtf8StringView functions are in
77 UTF-8 code points (that is, UTF-8 multibyte sequences count as
78 two, three or four, depending on their length). QUtf8StringView
79 does not an attempt to detect or prevent slicing right through
80 UTF-8 multibyte sequences. This is similar to the situation with
81 QStringView and surrogate pairs.
82
83 \section2 C++20, char8_t, and QUtf8StringView
84
85 In C++20, \c{u8""} string literals changed their type from
86 \c{const char[]} to \c{const char8_t[]}. If Qt 6 could have depended
87 on C++20, QUtf8StringView would store \c char8_t natively, and the
88 following functions and aliases would use (pointers to) \c char8_t:
89
90 \list
91 \li storage_type, value_type, etc
92 \li begin(), end(), data(), etc
93 \li front(), back(), at(), operator[]()
94 \endlist
95
96 This is what QUtf8StringView is expected to look like in Qt 7, but for
97 Qt 6, this was not possible. Instead of locking users into a C++17-era
98 interface for the next decade, Qt provides two QUtf8StringView classes,
99 in different (inline) namespaces. The first, in namespace \c{q_no_char8_t},
100 has a value_type of \c{const char} and is universally available.
101 The second, in namespace \c{q_has_char8_t}, has a value_type of
102 \c{const char8_t} and is only available when compiling in C++20 mode.
103
104 \c{q_no_char8_t} is an inline namespace regardless of C++ edition, to avoid
105 accidental binary incompatibilities. To use the \c{char8_t} version, you
106 need to name it explicitly with \c{q_has_char8_t::QUtf8StringView}.
107
108 Internally, both are instantiations of the same template class,
109 QBasicUtf8StringView. Please do not use the template class's name in your
110 source code.
111
112 \sa QAnyStringView, QStringView, QLatin1StringView, QString
113*/
114
115/*!
116 \typedef QUtf8StringView::storage_type
117
118 Alias for \c{char}.
119*/
120
121/*!
122 \typedef QUtf8StringView::value_type
123
124 Alias for \c{const char}. Provided for compatibility with the STL.
125*/
126
127/*!
128 \typedef QUtf8StringView::difference_type
129
130 Alias for \c{std::ptrdiff_t}. Provided for compatibility with the STL.
131*/
132
133/*!
134 \typedef QUtf8StringView::size_type
135
136 Alias for qsizetype. Provided for compatibility with the STL.
137*/
138
139/*!
140 \typedef QUtf8StringView::reference
141
142 Alias for \c{value_type &}. Provided for compatibility with the STL.
143
144 QUtf8StringView does not support mutable references, so this is the same
145 as const_reference.
146*/
147
148/*!
149 \typedef QUtf8StringView::const_reference
150
151 Alias for \c{value_type &}. Provided for compatibility with the STL.
152*/
153
154/*!
155 \typedef QUtf8StringView::pointer
156
157 Alias for \c{value_type *}. Provided for compatibility with the STL.
158
159 QUtf8StringView does not support mutable pointers, so this is the same
160 as const_pointer.
161*/
162
163/*!
164 \typedef QUtf8StringView::const_pointer
165
166 Alias for \c{value_type *}. Provided for compatibility with the STL.
167*/
168
169/*!
170 \typedef QUtf8StringView::iterator
171
172 This typedef provides an STL-style const iterator for QUtf8StringView.
173
174 QUtf8StringView does not support mutable iterators, so this is the same
175 as const_iterator.
176
177 \sa const_iterator, reverse_iterator
178*/
179
180/*!
181 \typedef QUtf8StringView::const_iterator
182
183 This typedef provides an STL-style const iterator for QUtf8StringView.
184
185 \sa iterator, const_reverse_iterator
186*/
187
188/*!
189 \typedef QUtf8StringView::reverse_iterator
190
191 This typedef provides an STL-style const reverse iterator for QUtf8StringView.
192
193 QUtf8StringView does not support mutable reverse iterators, so this is the
194 same as const_reverse_iterator.
195
196 \sa const_reverse_iterator, iterator
197*/
198
199/*!
200 \typedef QUtf8StringView::const_reverse_iterator
201
202 This typedef provides an STL-style const reverse iterator for QUtf8StringView.
203
204 \sa reverse_iterator, const_iterator
205*/
206
207/*!
208 \fn QUtf8StringView::QUtf8StringView()
209
210 Constructs a null string view.
211
212 \sa isNull()
213*/
214
215/*!
216 \fn QUtf8StringView::QUtf8StringView(const storage_type *d, qsizetype n)
217 \internal
218*/
219
220/*!
221 \fn QUtf8StringView::QUtf8StringView(std::nullptr_t)
222
223 Constructs a null string view.
224
225 \sa isNull()
226*/
227
228/*!
229 \fn template <typename Char, QUtf8StringView::if_compatible_char<Char> = true> QUtf8StringView::QUtf8StringView(const Char *str, qsizetype len)
230
231 Constructs a string view on \a str with length \a len.
232
233 The range \c{[str,len)} must remain valid for the lifetime of this string view object.
234
235 Passing \nullptr as \a str is safe if \a len is 0, too, and results in a null string view.
236
237 The behavior is undefined if \a len is negative or, when positive, if \a str is \nullptr.
238
239 \constraints \c Char is a compatible
240 character type. The compatible character types are: \c char8_t, \c char, \c{signed char} and
241 \c{unsigned char}.
242*/
243
244/*!
245 \fn template <typename Char, QUtf8StringView::if_compatible_char<Char> = true> QUtf8StringView::QUtf8StringView(const Char *first, const Char *last)
246
247 Constructs a string view on \a first with length (\a last - \a first).
248
249 The range \c{[first,last)} must remain valid for the lifetime of
250 this string view object.
251
252 Passing \c \nullptr as \a first is safe if \a last is \nullptr, too,
253 and results in a null string view.
254
255 The behavior is undefined if \a last precedes \a first, or \a first
256 is \nullptr and \a last is not.
257
258 \constraints \c Char is a compatible
259 character type. The compatible character types are: \c char8_t, \c char, \c{signed char} and
260 \c{unsigned char}.
261*/
262
263/*!
264 \fn template <typename Char> QUtf8StringView::QUtf8StringView(const Char *str)
265
266 Constructs a string view on \a str. The length is determined
267 by scanning for the first \c{Char(0)}.
268
269 \a str must remain valid for the lifetime of this string view object.
270
271 Passing \nullptr as \a str is safe and results in a null string view.
272
273 \constraints \a str
274 is not an array and if \c Char is a compatible character type. The
275 compatible character types are: \c char8_t, \c char, \c{signed char} and
276 \c{unsigned char}.
277*/
278
279/*!
280 \fn template <typename Char, size_t N> QUtf8StringView::QUtf8StringView(const Char (&string)[N])
281
282 Constructs a string view on the character string literal \a string.
283 The view covers the array until the first \c{Char(0)} is encountered,
284 or \c N, whichever comes first.
285 If you need the full array, use fromArray() instead.
286
287 \a string must remain valid for the lifetime of this string view
288 object.
289
290 \constraints \a string
291 is an actual array and if \c Char is a compatible character type. The
292 compatible character types are: \c char8_t, \c char, \c{signed char} and
293 \c{unsigned char}.
294
295 \sa fromArray()
296*/
297
298/*!
299 \fn template <typename Container, QUtf8StringView::if_compatible_container<Container>> QUtf8StringView::QUtf8StringView(const Container &str)
300
301 Constructs a string view on \a str. The length is taken from \c{std::size(str)}.
302
303 \c{std::data(str)} must remain valid for the lifetime of this string view object.
304
305 The string view will be empty if and only if \c{std::size(str) == 0}. It is unspecified
306 whether this constructor can result in a null string view (\c{std::data(str)} would
307 have to return \nullptr for this).
308
309 \constraints \c Container is a
310 container with a compatible character type as \c{value_type}. The
311 compatible character types are: \c char8_t, \c char, \c{signed char} and
312 \c{unsigned char}.
313
314 \sa isNull(), isEmpty()
315*/
316
317/*!
318 \fn template <typename Char, size_t Size, QUtf8StringView::if_compatible_char<Char>> QUtf8StringView::fromArray(const Char (&string)[Size])
319
320 Constructs a string view on the full character string literal \a string,
321 including any trailing \c{Char(0)}. If you don't want the
322 null-terminator included in the view then you can chop() it off
323 when you are certain it is at the end. Alternatively you can use
324 the constructor overload taking an array literal which will create
325 a view up to, but not including, the first null-terminator in the data.
326
327 \a string must remain valid for the lifetime of this string view
328 object.
329
330 This function will work with any array literal if \c Char is a
331 compatible character type. The compatible character types
332 are: \c char8_t, \c char, \c{signed char} and \c{unsigned char}.
333*/
334
335/*!
336 \fn QString QUtf8StringView::toString() const
337
338 Returns a deep copy of this string view's data as a QString.
339
340 The return value will be a null QString if and only if this string view is null.
341*/
342
343/*!
344 \fn QUtf8StringView::data() const
345
346 Returns a const pointer to the first code point in the string view.
347
348 \note The character array represented by the return value is \e not null-terminated.
349
350 \sa begin(), end(), utf8()
351*/
352
353/*!
354 \fn QUtf8StringView::utf8() const
355
356 Returns a const pointer to the first code point in the string view.
357
358 The result is returned as a \c{const char8_t*}, so this function is only available when
359 compiling in C++20 mode.
360
361 \note The character array represented by the return value is \e not null-terminated.
362
363 \sa begin(), end(), data()
364*/
365
366/*!
367 \fn QUtf8StringView::const_iterator QUtf8StringView::begin() const
368
369 Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first code point in
370 the string view.
371
372 This function is provided for STL compatibility.
373
374 \sa end(), cbegin(), rbegin(), data()
375*/
376
377/*!
378 \fn QUtf8StringView::const_iterator QUtf8StringView::cbegin() const
379
380 Same as begin().
381
382 This function is provided for STL compatibility.
383
384 \sa cend(), begin(), crbegin(), data()
385*/
386
387/*!
388 \fn QUtf8StringView::const_iterator QUtf8StringView::end() const
389
390 Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary
391 code point after the last code point in the list.
392
393 This function is provided for STL compatibility.
394
395 \sa begin(), cend(), rend()
396*/
397
398/*! \fn QUtf8StringView::const_iterator QUtf8StringView::cend() const
399
400 Same as end().
401
402 This function is provided for STL compatibility.
403
404 \sa cbegin(), end(), crend()
405*/
406
407/*!
408 \fn QUtf8StringView::const_reverse_iterator QUtf8StringView::rbegin() const
409
410 Returns a const \l{STL-style iterators}{STL-style} reverse iterator pointing to the first
411 code point in the string view, in reverse order.
412
413 This function is provided for STL compatibility.
414
415 \sa rend(), crbegin(), begin()
416*/
417
418/*!
419 \fn QUtf8StringView::const_reverse_iterator QUtf8StringView::crbegin() const
420
421 Same as rbegin().
422
423 This function is provided for STL compatibility.
424
425 \sa crend(), rbegin(), cbegin()
426*/
427
428/*!
429 \fn QUtf8StringView::const_reverse_iterator QUtf8StringView::rend() const
430
431 Returns a \l{STL-style iterators}{STL-style} reverse iterator pointing to one past
432 the last code point in the string view, in reverse order.
433
434 This function is provided for STL compatibility.
435
436 \sa rbegin(), crend(), end()
437*/
438
439/*!
440 \fn QUtf8StringView::const_reverse_iterator QUtf8StringView::crend() const
441
442 Same as rend().
443
444 This function is provided for STL compatibility.
445
446 \sa crbegin(), rend(), cend()
447*/
448
449/*!
450 \fn bool QUtf8StringView::empty() const
451
452 Returns whether this string view is empty - that is, whether \c{size() == 0}.
453
454 This function is provided for STL compatibility.
455
456 \sa isEmpty(), isNull(), size(), length()
457*/
458
459/*!
460 \fn bool QUtf8StringView::isEmpty() const
461
462 Returns whether this string view is empty - that is, whether \c{size() == 0}.
463
464 This function is provided for compatibility with other Qt containers.
465
466 \sa empty(), isNull(), size(), length()
467*/
468
469/*!
470 \fn bool QUtf8StringView::isNull() const
471
472 Returns whether this string view is null - that is, whether \c{data() == nullptr}.
473
474 This functions is provided for compatibility with other Qt containers.
475
476 \sa empty(), isEmpty(), size(), length()
477*/
478
479/*!
480 \fn qsizetype QUtf8StringView::size() const
481
482 Returns the size of this string view, in UTF-8 code points (that is,
483 multi-byte sequences count as more than one for the purposes of this function, the same
484 as surrogate pairs in QString and QStringView).
485
486 \sa empty(), isEmpty(), isNull(), length()
487*/
488
489/*!
490 \fn QUtf8StringView::length() const
491
492 Same as size().
493
494 This function is provided for compatibility with other Qt containers.
495
496 \sa empty(), isEmpty(), isNull(), size()
497*/
498
499/*!
500 \fn QUtf8StringView::operator[](qsizetype n) const
501
502 Returns the code point at position \a n in this string view.
503
504 The behavior is undefined if \a n is negative or not less than size().
505
506 \sa at(), front(), back()
507*/
508
509/*!
510 \fn QUtf8StringView::at(qsizetype n) const
511
512 Returns the code point at position \a n in this string view.
513
514 The behavior is undefined if \a n is negative or not less than size().
515
516 \sa operator[](), front(), back()
517*/
518
519/*!
520 \fn QUtf8StringView::front() const
521
522 Returns the first code point in the string view. Same as first().
523
524 This function is provided for STL compatibility.
525
526 \warning Calling this function on an empty string view constitutes
527 undefined behavior.
528
529 \sa back()
530*/
531
532/*!
533 \fn QUtf8StringView::back() const
534
535 Returns the last code point in the string view. Same as last().
536
537 This function is provided for STL compatibility.
538
539 \warning Calling this function on an empty string view constitutes
540 undefined behavior.
541
542 \sa front()
543*/
544
545/*!
546 \fn QUtf8StringView::mid(qsizetype pos, qsizetype n) const
547
548 Returns the substring of length \a n starting at position
549 \a pos in this object.
550
551 \deprecated Use sliced() instead in new code.
552
553 Returns an empty string view if \a n exceeds the
554 length of the string view. If there are less than \a n code points
555 available in the string view starting at \a pos, or if
556 \a n is negative (default), the function returns all code points that
557 are available from \a pos.
558
559 \sa first(), last(), sliced(), chopped(), chop(), truncate(), slice()
560*/
561
562/*!
563 \fn QUtf8StringView::left(qsizetype n) const
564
565 \deprecated Use first() instead in new code.
566
567 Returns the substring of length \a n starting at position
568 0 in this object.
569
570 The entire string view is returned if \a n is greater than or equal
571 to size(), or less than zero.
572
573 \sa first(), last(), sliced(), chopped(), chop(), truncate(), slice()
574*/
575
576/*!
577 \fn QUtf8StringView::right(qsizetype n) const
578
579 \deprecated Use last() instead in new code.
580
581 Returns the substring of length \a n starting at position
582 size() - \a n in this object.
583
584 The entire string view is returned if \a n is greater than or equal
585 to size(), or less than zero.
586
587 \sa first(), last(), sliced(), chopped(), chop(), truncate(), slice()
588*/
589
590/*!
591 \fn QUtf8StringView::first(qsizetype n) const
592
593 Returns a string view that contains the first \a n code points
594 of this string view.
595
596 \note The behavior is undefined when \a n < 0 or \a n > size().
597
598 \sa last(), sliced(), chopped(), chop(), truncate(), slice()
599*/
600
601/*!
602 \fn QUtf8StringView::last(qsizetype n) const
603
604 Returns a string view that contains the last \a n code points of this string view.
605
606 \note The behavior is undefined when \a n < 0 or \a n > size().
607
608 \sa first(), sliced(), chopped(), chop(), truncate(), slice()
609*/
610
611/*!
612 \fn QUtf8StringView::sliced(qsizetype pos, qsizetype n) const
613
614 Returns a string view containing \a n code points of this string view,
615 starting at position \a pos.
616
617//! [UB-sliced-index-length]
618 \note The behavior is undefined when \a pos < 0, \a n < 0,
619 or \a pos + \a n > size().
620//! [UB-sliced-index-length]
621
622 \sa first(), last(), chopped(), chop(), truncate(), slice()
623*/
624
625/*!
626 \fn QUtf8StringView::sliced(qsizetype pos) const
627
628 Returns a string view starting at position \a pos in this object,
629 and extending to its end.
630
631//! [UB-sliced-index-only]
632 \note The behavior is undefined when \a pos < 0 or \a pos > size().
633//! [UB-sliced-index-only]
634
635 \sa first(), last(), chopped(), chop(), truncate(), slice()
636*/
637
638/*!
639 \fn QUtf8StringView &QUtf8StringView::slice(qsizetype pos, qsizetype n)
640 \since 6.8
641
642 Modifies this string view to start at position \a pos, extending for
643 \a n code points.
644
645 \include qutf8stringview.qdoc UB-sliced-index-length
646
647 \sa sliced(), first(), last(), chopped(), chop(), truncate()
648*/
649
650/*!
651 \fn QUtf8StringView &QUtf8StringView::slice(qsizetype pos)
652 \since 6.8
653 \overload
654
655 Modifies this string view to start at position \a pos, extending
656 to its end.
657
658 \include qutf8stringview.qdoc UB-sliced-index-only
659
660 \sa sliced(), first(), last(), chopped(), chop(), truncate()
661*/
662
663/*!
664 \fn QUtf8StringView::chopped(qsizetype n) const
665
666 Returns the substring of length size() - \a n starting at the
667 beginning of this object.
668
669 Same as \c{first(size() - n)}.
670
671 \note The behavior is undefined when \a n < 0 or \a n > size().
672
673 \sa sliced(), first(), last(), chop(), truncate(), slice()
674*/
675
676/*!
677 \fn QUtf8StringView::truncate(qsizetype n)
678
679 Truncates this string view to \a n code points.
680
681 Same as \c{*this = first(n)}.
682
683 \note The behavior is undefined when \a n < 0 or \a n > size().
684
685 \sa sliced(), first(), last(), chopped(), chop()
686*/
687
688/*!
689 \fn QUtf8StringView::chop(qsizetype n)
690
691 Truncates this string view by \a n code points.
692
693 Same as \c{*this = first(size() - n)}.
694
695 \note The behavior is undefined when \a n < 0 or \a n > size().
696
697 \sa sliced(), first(), last(), chopped(), truncate()
698*/
699
700/*!
701 \fn int QUtf8StringView::compare(QLatin1StringView str, Qt::CaseSensitivity cs) const
702 \fn int QUtf8StringView::compare(QUtf8StringView str, Qt::CaseSensitivity cs) const
703 \fn int QUtf8StringView::compare(QStringView str, Qt::CaseSensitivity cs) const
704 \since 6.5
705
706 Compares this string view with \a str and returns a negative integer if
707 this string view is less than \a str, a positive integer if it is greater than
708 \a str, and zero if they are equal.
709
710 \include qstring.qdocinc {search-comparison-case-sensitivity} {comparison}
711*/
712
713
714/*!
715 \fn QUtf8StringView::isValidUtf8() const
716
717 Returns \c true if this string contains valid UTF-8 encoded data,
718 or \c false otherwise.
719
720 \since 6.3
721*/
722
723/*!
724 \fn template <typename QStringLike> qToUtf8StringViewIgnoringNull(const QStringLike &s);
725 \relates QUtf8StringView
726 \internal
727
728 Convert \a s to a QUtf8StringView ignoring \c{s.isNull()}.
729
730 Returns a string view that references \a{s}'s data, but is never null.
731
732 This is a faster way to convert a QByteArray to a QUtf8StringView,
733 if null QByteArrays can legitimately be treated as empty ones.
734
735 \sa QByteArray::isNull(), QUtf8StringView
736*/
737
738
739/*!
740 \fn QUtf8StringView::operator std::string_view() const
741 \since 6.7
742
743 Converts this QUtf8StringView object to a
744 \c{std::string_view} object. The returned view will have the
745 same data pointer and length as this view.
746*/
747
748/*!
749 \fn QUtf8StringView::operator std::u8string_view() const
750 \since 6.10
751
752 Converts this QUtf8StringView object to a \c{std::u8string_view}
753 object. The returned view will have the same data pointer and length
754 as this view.
755
756 This function is only available when compiling in C++20 mode.
757*/
758
759/*!
760 \fn QUtf8StringView::maxSize()
761 \since 6.8
762
763 It returns the maximum number of elements that the view can
764 theoretically represent. In practice, the number can be much smaller,
765 limited by the amount of memory available to the system.
766*/
767
768/*!
769 \fn QUtf8StringView::max_size() const
770 \since 6.8
771
772 This function is provided for STL compatibility.
773
774 Returns maxSize().
775*/
776
777/*!
778 \fn template <typename...Args> QString QUtf8StringView::arg(Args &&...args) const
779 \since 6.9
780
781 \include qstringview.cpp qstring-multi-arg
782
783 \sa QString::arg(Args&&...)
784*/