1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
7 \brief The QByteArrayView class provides a view on an array of bytes with a read-only
8 subset of the QByteArray API.
13 \ingroup string-processing
17 A QByteArrayView references a contiguous portion of raw bytes it does
18 not own. It acts as an interface type to all kinds of byte-array-like data,
19 without the need to construct a QByteArray first.
21 The byte array data may be represented as an array (or an array-compatible
22 data-structure such as QByteArray, std::basic_string, etc.) of \c char,
23 \c{signed char}, \c{unsigned char} or \c std::byte.
25 QByteArrayView is designed as an interface type; its main use-case is
26 as a function parameter type. When QByteArrayViews are used as automatic
27 variables or data members, care must be taken to ensure that the referenced
28 data (for example, owned by a QByteArray) outlives the QByteArrayView on all
29 code paths, lest the byte array view ends up referencing deleted data.
31 When used as an interface type, QByteArrayView allows a single function to accept
32 a wide variety of byte-array-like data sources. One function accepting QByteArrayView
33 thus replaces several function overloads (taking, for example, QByteArray, const char *,
34 etc.) while at the same time enabling even more byte array data sources to be passed
37 QByteArrayView should be passed by value, not by reference-to-const:
38 \snippet code/src_corelib_text_qbytearrayview.cpp 0
40 If you want to give your users maximum freedom in what type of data they
41 can pass to your function, accompany the QByteArrayView overload with
45 \li \e char: this overload can delegate to the QByteArrayView version:
46 \snippet code/src_corelib_text_qbytearrayview.cpp 1
47 even though, for technical reasons, QByteArrayView cannot provide a
48 char constructor by itself.
49 \li \e QByteArray: if you store an unmodified copy of the byte array and
50 thus would like to take advantage of QByteArray's implicit sharing.
53 QByteArrayView can also be used as the return value of a function. If you call a
54 function returning QByteArrayView, take extra care to not keep the QByteArrayView
55 around longer than the function promises to keep the referenced data alive.
56 If in doubt, obtain a strong reference to the data by calling toByteArray() to convert
57 the QByteArrayView into a QByteArray.
59 The methods supported by QByteArrayView reflect those of \l QByteArray. In
60 particular, to the limited degree that it ascribes semantics (such as
61 character case, spacing, digits of numbers) to the character data viewed, it
62 uses the C locale and ASCII encoding. See \l {C locale and ASCII functions}
63 for details and the limitations on these methods.
65 \section1 Compatible Byte Types
67 QByteArrayView can be constructed on any container of bytes, where the byte type
71 \li \c char (both signed and unsigned)
75 \sa QByteArray, QStringView
79 \typedef QByteArrayView::storage_type
85 \typedef QByteArrayView::value_type
87 Alias for \c{const char}. Provided for compatibility with the STL.
91 \typedef QByteArrayView::difference_type
93 Alias for \c{std::ptrdiff_t}. Provided for compatibility with the STL.
97 \typedef QByteArrayView::size_type
99 Alias for qsizetype. Provided for compatibility with the STL.
103 \typedef QByteArrayView::reference
105 Alias for \c{value_type &}. Provided for compatibility with the STL.
107 QByteArrayView does not support mutable references, so this is the same
112 \typedef QByteArrayView::const_reference
114 Alias for \c{value_type &}. Provided for compatibility with the STL.
118 \typedef QByteArrayView::pointer
120 Alias for \c{value_type *}. Provided for compatibility with the STL.
122 QByteArrayView does not support mutable pointers, so this is the same
127 \typedef QByteArrayView::const_pointer
129 Alias for \c{value_type *}. Provided for compatibility with the STL.
133 \typedef QByteArrayView::iterator
135 This typedef provides an STL-style const iterator for QByteArrayView.
137 QByteArrayView does not support mutable iterators, so this is the same
140 \sa const_iterator, reverse_iterator
144 \typedef QByteArrayView::const_iterator
146 This typedef provides an STL-style const iterator for QByteArrayView.
148 \sa iterator, const_reverse_iterator
152 \typedef QByteArrayView::reverse_iterator
154 This typedef provides an STL-style const reverse iterator for QByteArrayView.
156 QByteArrayView does not support mutable reverse iterators, so this is the
157 same as const_reverse_iterator.
159 \sa const_reverse_iterator, iterator
163 \typedef QByteArrayView::const_reverse_iterator
165 This typedef provides an STL-style const reverse iterator for QByteArrayView.
167 \sa reverse_iterator, const_iterator
171 \fn QByteArrayView::QByteArrayView()
173 Constructs a null byte array view.
179 \fn QByteArrayView::QByteArrayView(std::nullptr_t)
181 Constructs a null byte array view.
187 \fn template <typename Byte> QByteArrayView::QByteArrayView(const Byte *data, qsizetype len)
189 Constructs a byte array view on \a data with length \a len.
191 The range \c{[data,len)} must remain valid for the lifetime of this QByteArrayView.
193 Passing \nullptr as \a data is safe if \a len is 0, too, and results in a null
196 The behavior is undefined if \a len is negative or, when positive, if \a data is \nullptr.
198 This constructor only participates in overload resolution if \c Byte is a compatible
201 \sa {Compatible Byte Types}
205 \fn template <typename Byte> QByteArrayView::QByteArrayView(const Byte *first, const Byte *last)
207 Constructs a byte array view on \a first with length (\a last - \a first).
209 The range \c{[first,last)} must remain valid for the lifetime of
212 Passing \c \nullptr as \a first is safe if \a last is \nullptr, too,
213 and results in a null byte array view.
215 The behavior is undefined if \a last precedes \a first, or \a first
216 is \nullptr and \a last is not.
218 This constructor only participates in overload resolution if \c Byte is
219 a compatible byte type.
221 \sa {Compatible Byte Types}
225 \fn template <typename Byte> QByteArrayView::QByteArrayView(const Byte *data)
227 Constructs a byte array view on \a data. The length is determined
228 by scanning for the first \c{Byte(0)}.
230 \a data must remain valid for the lifetime of this byte array view object.
232 Passing \nullptr as \a data is safe and results in a null byte array view.
234 This constructor only participates in overload resolution if \a data is not
235 an array and if \c Byte is a compatible byte type.
237 \sa {Compatible Byte Types}
241 \fn template <size_t Size> QByteArrayView::QByteArrayView(const char (&data)[Size])
243 Constructs a byte array view on the char array \a data.
244 The view covers the array until the first \c{'\0'} is encountered,
245 or \c Size, whichever comes first.
246 If you need the full array, use fromArray() instead.
248 \a data must remain valid for the lifetime of this byte array view
251 \note This constructor is only available for char array literals.
252 The reasoning behind that is for compatibility with C-libraries
253 which predefine "large-enough" arrays, but only use some of the
254 preallocated space. To support this in an intuitive way in an
255 implicit constructor overload, we need to stop at the first
256 \c{char(0)}. This is logical for a char array, but not
257 for a \c{std::byte} array.
263 \fn QByteArrayView::QByteArrayView(const QByteArray &byteArray)
265 Constructs a byte array view on \a byteArray.
267 \c{byteArray.data()} must remain valid for the lifetime of this byte array view object.
269 The byte array view will be null if and only if \c{byteArray.isNull()}.
273 \fn template <typename Container> QByteArrayView::QByteArrayView(const Container &c)
275 Constructs a byte array view on the array-like container \a c. The length and data
276 are set via \c{std::size(c)} and \c{std::data(c)} respectively.
278 The container's data must remain valid for the lifetime of this byte array view object.
280 This constructor participates in overload resolution if \a c is any contiguous
281 container with elements of a compatible byte type.
283 \sa {Compatible Byte Types}
287 \fn template <typename Byte, size_t Size> QByteArrayView QByteArrayView::fromArray(const Byte (&data)[Size])
289 Constructs a byte array view on the array literal \a data. The view covers the full
290 array. That includes the trailing null-terminator of \c{char} array literals.
291 If you don't want the null-terminator included in the view, you can chop() it off
292 when you are certain it is at the end. Alternatively you can use the constructor
293 overload taking a char array literal which will create a view up to, but not including,
294 the first null-terminator in the data.
296 This function will work with any array literal of a compatible byte type.
298 \sa {Compatible Byte Types}, QByteArrayView
302 \fn QByteArray QByteArrayView::toByteArray() const
304 Returns a deep copy of this byte array view's data as a QByteArray.
306 The return value will be a null QByteArray if and only if this byte array
311 \fn const char *QByteArrayView::data() const
313 Returns a const \c char pointer to the first byte in the byte array view.
315 \note The character array represented by the return value is \e not guaranteed
316 to be null-terminated. The returned pointer is only safe to use for accessing
317 bytes at indices that are less than this byte array view's size().
323 \fn const char *QByteArrayView::constData() const
325 Returns a const \c char pointer to the first byte in the byte array view.
327 \note The character array represented by the return value is \e not guaranteed
328 to be null-terminated. The returned pointer is only safe to use for accessing
329 bytes at indices that are less than this byte array view's size().
331 \sa data(), begin(), end()
335 \fn int QByteArrayView::operator==(QByteArrayView lhs, QByteArrayView rhs)
336 \fn int QByteArrayView::operator!=(QByteArrayView lhs, QByteArrayView rhs)
337 \fn int QByteArrayView::operator< (QByteArrayView lhs, QByteArrayView rhs)
338 \fn int QByteArrayView::operator<=(QByteArrayView lhs, QByteArrayView rhs)
339 \fn int QByteArrayView::operator> (QByteArrayView lhs, QByteArrayView rhs)
340 \fn int QByteArrayView::operator>=(QByteArrayView lhs, QByteArrayView rhs)
342 Comparison operators for QByteArrayView.
346 \fn int QByteArrayView::compare(QByteArrayView bv, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
349 Returns an integer less than, equal to, or greater than zero depending on
350 whether this QByteArrayView sorts before, at the same position as, or after
351 the QByteArrayView \a bv. The comparison is performed according to case
358 \fn QByteArrayView::isValidUtf8() const
360 Returns \c true if this byte array view contains valid UTF-8 encoded data,
361 or \c false otherwise.
367 \fn QByteArrayView::const_iterator QByteArrayView::begin() const
369 Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the
370 first byte in the byte array view.
372 This function is provided for STL compatibility.
374 \sa end(), cbegin(), rbegin(), data()
378 \fn QByteArrayView::const_iterator QByteArrayView::cbegin() const
382 This function is provided for STL compatibility.
384 \sa cend(), begin(), crbegin(), data()
388 \fn QByteArrayView::const_iterator QByteArrayView::end() const
390 Returns a const \l{STL-style iterators}{STL-style iterator} pointing
391 just after the last byte in the byte array view.
393 This function is provided for STL compatibility.
395 \sa begin(), cend(), rend()
398/*! \fn QByteArrayView::const_iterator QByteArrayView::cend() const
402 This function is provided for STL compatibility.
404 \sa cbegin(), end(), crend()
408 \fn QByteArrayView::const_reverse_iterator QByteArrayView::rbegin() const
410 Returns a const \l{STL-style iterators}{STL-style} reverse iterator pointing
411 to the first byte in the byte array view, in reverse order.
413 This function is provided for STL compatibility.
415 \sa rend(), crbegin(), begin()
419 \fn QByteArrayView::const_reverse_iterator QByteArrayView::crbegin() const
423 This function is provided for STL compatibility.
425 \sa crend(), rbegin(), cbegin()
429 \fn QByteArrayView::const_reverse_iterator QByteArrayView::rend() const
431 Returns a \l{STL-style iterators}{STL-style} reverse iterator pointing to one past
432 the last byte in the byte array view, in reverse order.
434 This function is provided for STL compatibility.
436 \sa rbegin(), crend(), end()
440 \fn QByteArrayView::const_reverse_iterator QByteArrayView::crend() const
444 This function is provided for STL compatibility.
446 \sa crbegin(), rend(), cend()
450 \fn bool QByteArrayView::empty() const
452 Returns \c true if this byte array view is empty - that is, \c{size() == 0}.
454 This function is provided for STL compatibility.
456 \sa isEmpty(), isNull(), size()
460 \fn bool QByteArrayView::isEmpty() const
462 Returns \c true if this byte array view is empty - that is, \c{size() == 0}.
464 \sa empty(), isNull(), size()
468 \fn bool QByteArrayView::isNull() const
470 Returns \c true if this byte array view is null - that is, \c{data() == nullptr}.
472 \sa empty(), isEmpty(), size()
476 \fn qsizetype QByteArrayView::size() const
478 Returns the number of bytes in this byte array view.
480 \sa empty(), isEmpty(), isNull()
484 \fn QByteArrayView::length() const
488 \sa empty(), isEmpty(), isNull(), size()
492 \fn char QByteArrayView::operator[](qsizetype n) const
494 Returns the character at position \a n in this byte array view.
496 The behavior is undefined if \a n is negative or not less than size().
498 \sa at(), front(), back()
502 \fn char QByteArrayView::at(qsizetype n) const
504 Returns the character at position \a n in this byte array view.
506 The behavior is undefined if \a n is negative or not less than size().
508 \sa operator[](), front(), back()
512 \fn char QByteArrayView::front() const
514 Returns the first byte in the byte array view.
516 This function is provided for STL compatibility.
518 \warning Calling this function on an empty byte array view constitutes
525 \fn char QByteArrayView::back() const
527 Returns the last byte in the byte array view.
529 This function is provided for STL compatibility.
531 \warning Calling this function on an empty byte array view constitutes
538 \fn QByteArrayView QByteArrayView::first(qsizetype n) const
540 Returns a byte array view that points to the first \a n bytes
541 of this byte array view. Equivalent to \c{sliced(0, n)}.
543 \note The behavior is undefined when \a n < 0 or \a n > size().
545 \sa last(), startsWith(), chopped(), chop(), truncate()
549 \fn QByteArrayView QByteArrayView::last(qsizetype n) const
551 Returns a byte array view that points to the last \a n bytes
552 of this byte array view.
554 \note The behavior is undefined when \a n < 0 or \a n > size().
556 \sa first(), endsWith(), chopped(), chop(), truncate()
560 \fn QByteArrayView QByteArrayView::sliced(qsizetype pos, qsizetype n) const
562 Returns a byte array view that points to \a n bytes of this byte array
563 view, starting at position \a pos.
565 \note The behavior is undefined when \a pos < 0, \a n < 0,
566 or \a pos + \a n > size().
568 \sa first(), last(), chopped(), chop(), truncate()
572 \fn QByteArrayView QByteArrayView::sliced(qsizetype pos) const
574 Returns a byte array view starting at position \a pos in this object,
575 and extending to its end.
577 \note The behavior is undefined when \a pos < 0 or \a pos > size().
579 \sa first(), last(), chopped(), chop(), truncate()
583 \fn QByteArrayView QByteArrayView::chopped(qsizetype length) const
585 Returns a copy of this byte array view that omits its last \a length bytes.
586 In other words, returns a byte array view of length size() - \a length starting
587 at the beginning of this object.
589 Same as \c{first(size() - length)}.
591 \note The behavior is undefined when \a length < 0 or \a length > size().
593 \sa first(), last(), sliced(), chop(), truncate()
597 \fn void QByteArrayView::truncate(qsizetype length)
599 Truncates this byte array view to length \a length.
601 Same as \c{*this = first(length)}.
603 \note The behavior is undefined when \a length < 0 or \a length > size().
605 \sa first(), last(), sliced(), chopped(), chop()
609 \fn void QByteArrayView::chop(qsizetype length)
611 Truncates this byte array view by \a length characters.
613 Same as \c{*this = first(size() - length)}.
615 \note The behavior is undefined when \a length < 0 or \a length > size().
617 \sa sliced(), first(), last(), chopped(), truncate()
621 \fn QByteArrayView QByteArrayView::mid(qsizetype start, qsizetype length) const
624 \deprecated Use sliced() instead in new code.
626 Returns the subarray of length \a length starting at position
627 \a start in this object.
629 Returns an empty byte array view if \a start exceeds the
630 length of the byte array view. If there are less than \a length characters
631 available in the byte array view starting at \a start, or if
632 \a length is negative (default), the function returns all characters that
633 are available from \a start.
635 \sa first(), last(), sliced(), chopped(), chop(), truncate()
639 \fn QByteArrayView QByteArrayView::left(qsizetype length) const
642 \deprecated Use first() instead in new code.
644 Returns the subarray of length \a length starting at position
647 The entire byte array view is returned if \a length is greater than or equal
648 to size(), or less than zero.
650 \sa first(), last(), sliced(), startsWith(), chopped(), chop(), truncate()
654 \fn QByteArrayView QByteArrayView::right(qsizetype length) const
657 \deprecated Use last() instead in new code.
659 Returns the subarray of length \a length starting at position
660 size() - \a length in this object.
662 The entire byte array view is returned if \a length is greater than or equal
663 to size(), or less than zero.
665 \sa first(), last(), sliced(), endsWith(), chopped(), chop(), truncate()
669 \fn QByteArrayView QByteArrayView::trimmed() const noexcept
672 Returns a copy of this byte array view with spacing characters
673 removed from the start and end.
675 The spacing characters are those for which the standard C++ \c isspace()
676 function returns \c true in the C locale; these are the ASCII characters
677 tabulation '\\t', line feed '\\n', carriage return '\\r', vertical
678 tabulation '\\v', form feed '\\f', and space ' '.
680 \sa QChar::SpecialCharacter, {QByteArray#Spacing Characters}{Spacing Characters}
684 \fn qlonglong QByteArrayView::toLongLong(bool *ok, int base) const
687 Returns this byte array view converted to a \c {long long} using base \a
688 base, which is ten by default. Bases 0 and 2 through 36 are supported, using
689 letters for digits beyond 9; A is ten, B is eleven and so on.
691 If \a base is 0, the base is determined automatically using the following
692 rules: if the byte array view begins with "0x", the rest of it is read as
693 hexadecimal (base 16); otherwise, if it begins with "0", the rest of it is
694 read as octal (base 8); otherwise it is read as decimal.
696 Returns 0 if the conversion fails.
698 If \a ok is not \nullptr, failure is reported by setting *\a{ok}
699 to \c false, and success by setting *\a{ok} to \c true.
701 \note The conversion of the number is performed in the default C locale,
702 regardless of the user's locale. Use QLocale to perform locale-aware
703 conversions between numbers and strings.
707 \fn qulonglong QByteArrayView::toULongLong(bool *ok, int base) const
710 Returns this byte array view converted to an \c {unsigned long long} using
711 base \a base, which is ten by default. Bases 0 and 2 through 36 are
712 supported, using letters for digits beyond 9; A is ten, B is eleven and so
715 If \a base is 0, the base is determined automatically using the following
716 rules: if the byte array view begins with "0x", the rest of it is read as
717 hexadecimal (base 16); otherwise, if it begins with "0", the rest of it is
718 read as octal (base 8); otherwise it is read as decimal.
720 Returns 0 if the conversion fails.
722 If \a ok is not \nullptr, failure is reported by setting *\a{ok}
723 to \c false, and success by setting *\a{ok} to \c true.
725 \note The conversion of the number is performed in the default C locale,
726 regardless of the user's locale. Use QLocale to perform locale-aware
727 conversions between numbers and strings.
731 \fn int QByteArrayView::toInt(bool *ok, int base) const
734 Returns this byte array view converted to an \c int using base \a base,
735 which is ten by default. Bases 0 and 2 through 36 are supported, using
736 letters for digits beyond 9; A is ten, B is eleven and so on.
738 If \a base is 0, the base is determined automatically using the following
739 rules: if the byte array view begins with "0x", the rest of it is read as
740 hexadecimal (base 16); otherwise, if it begins with "0", the rest of it is
741 read as octal (base 8); otherwise it is read as decimal.
743 Returns 0 if the conversion fails.
745 If \a ok is not \nullptr, failure is reported by setting *\a{ok}
746 to \c false, and success by setting *\a{ok} to \c true.
748 \snippet code/src_corelib_text_qbytearrayview.cpp 2
750 \note The conversion of the number is performed in the default C locale,
751 regardless of the user's locale. Use QLocale to perform locale-aware
752 conversions between numbers and strings.
756 \fn uint QByteArrayView::toUInt(bool *ok, int base) const
759 Returns this byte array view converted to an \c {unsigned int} using base \a
760 base, which is ten by default. Bases 0 and 2 through 36 are supported, using
761 letters for digits beyond 9; A is ten, B is eleven and so on.
763 If \a base is 0, the base is determined automatically using the following
764 rules: if the byte array view begins with "0x", the rest of it is read as
765 hexadecimal (base 16); otherwise, if it begins with "0", the rest of it is
766 read as octal (base 8); otherwise it is read as decimal.
768 Returns 0 if the conversion fails.
770 If \a ok is not \nullptr, failure is reported by setting *\a{ok}
771 to \c false, and success by setting *\a{ok} to \c true.
773 \note The conversion of the number is performed in the default C locale,
774 regardless of the user's locale. Use QLocale to perform locale-aware
775 conversions between numbers and strings.
779 \fn long QByteArrayView::toLong(bool *ok, int base) const
782 Returns this byte array view converted to a \c long int using base \a base,
783 which is ten by default. Bases 0 and 2 through 36 are supported, using
784 letters for digits beyond 9; A is ten, B is eleven and so on.
786 If \a base is 0, the base is determined automatically using the following
787 rules: if the byte array view begins with "0x", the rest of it is read as
788 hexadecimal (base 16); otherwise, if it begins with "0", the rest of it is
789 read as octal (base 8); otherwise it is read as decimal.
791 Returns 0 if the conversion fails.
793 If \a ok is not \nullptr, failure is reported by setting *\a{ok}
794 to \c false, and success by setting *\a{ok} to \c true.
796 \snippet code/src_corelib_text_qbytearrayview.cpp 3
798 \note The conversion of the number is performed in the default C locale,
799 regardless of the user's locale. Use QLocale to perform locale-aware
800 conversions between numbers and strings.
804 \fn ulong QByteArrayView::toULong(bool *ok, int base) const
807 Returns this byte array view converted to an \c {unsigned long int} using
808 base \a base, which is ten by default. Bases 0 and 2 through 36 are
809 supported, using letters for digits beyond 9; A is ten, B is eleven and so
812 If \a base is 0, the base is determined automatically using the following
813 rules: if the byte array view begins with "0x", the rest of it is read as
814 hexadecimal (base 16); otherwise, if it begins with "0", the rest of it is
815 read as octal (base 8); otherwise it is read as decimal.
817 Returns 0 if the conversion fails.
819 If \a ok is not \nullptr, failure is reported by setting *\a{ok}
820 to \c false, and success by setting *\a{ok} to \c true.
822 \note The conversion of the number is performed in the default C locale,
823 regardless of the user's locale. Use QLocale to perform locale-aware
824 conversions between numbers and strings.
828 \fn short QByteArrayView::toShort(bool *ok, int base) const
831 Returns this byte array view converted to a \c short using base \a base,
832 which is ten by default. Bases 0 and 2 through 36 are supported, using
833 letters for digits beyond 9; A is ten, B is eleven and so on.
835 If \a base is 0, the base is determined automatically using the following
836 rules: if the byte array view begins with "0x", the rest of it is read as
837 hexadecimal (base 16); otherwise, if it begins with "0", the rest of it is
838 read as octal (base 8); otherwise it is read as decimal.
840 Returns 0 if the conversion fails.
842 If \a ok is not \nullptr, failure is reported by setting *\a{ok}
843 to \c false, and success by setting *\a{ok} to \c true.
845 \note The conversion of the number is performed in the default C locale,
846 regardless of the user's locale. Use QLocale to perform locale-aware
847 conversions between numbers and strings.
851 \fn ushort QByteArrayView::toUShort(bool *ok, int base) const
854 Returns this byte array view converted to an \c {unsigned short} using base
855 \a base, which is ten by default. Bases 0 and 2 through 36 are supported,
856 using letters for digits beyond 9; A is ten, B is eleven and so on.
858 If \a base is 0, the base is determined automatically using the following
859 rules: if the byte array view begins with "0x", the rest of it is read as
860 hexadecimal (base 16); otherwise, if it begins with "0", the rest of it is
861 read as octal (base 8); otherwise it is read as decimal.
863 Returns 0 if the conversion fails.
865 If \a ok is not \nullptr, failure is reported by setting *\a{ok}
866 to \c false, and success by setting *\a{ok} to \c true.
868 \note The conversion of the number is performed in the default C locale,
869 regardless of the user's locale. Use QLocale to perform locale-aware
870 conversions between numbers and strings.
874 \fn double QByteArrayView::toDouble(bool *ok) const
877 Returns this byte array view converted to a \c double value.
879 Returns an infinity if the conversion overflows or 0.0 if the
880 conversion fails for other reasons (e.g. underflow).
882 If \a ok is not \nullptr, failure is reported by setting *\a{ok}
883 to \c false, and success by setting *\a{ok} to \c true.
885 \warning The QByteArrayView content may only contain valid numerical
886 characters which includes the plus/minus sign, the character e used in
887 scientific notation, and the decimal point. Including the unit or additional
888 characters leads to a conversion error.
890 \note The conversion of the number is performed in the default C locale,
891 regardless of the user's locale. Use QLocale to perform locale-aware
892 conversions between numbers and strings.
894 This function ignores leading and trailing spacing characters.
898 \fn float QByteArrayView::toFloat(bool *ok) const
901 Returns this byte array view converted to a \c float value.
903 Returns an infinity if the conversion overflows or 0.0 if the
904 conversion fails for other reasons (e.g. underflow).
906 If \a ok is not \nullptr, failure is reported by setting *\a{ok}
907 to \c false, and success by setting *\a{ok} to \c true.
909 \snippet code/src_corelib_text_qbytearrayview.cpp 4
911 \warning The QByteArrayView content may only contain valid numerical
912 characters which includes the plus/minus sign, the character e used in
913 scientific notation, and the decimal point. Including the unit or additional
914 characters leads to a conversion error.
916 \note The conversion of the number is performed in the default C locale,
917 regardless of the user's locale. Use QLocale to perform locale-aware
918 conversions between numbers and strings.
920 This function ignores leading and trailing whitespace.
924 \fn bool QByteArrayView::startsWith(QByteArrayView bv) const
925 \fn bool QByteArrayView::startsWith(char ch) const
927 Returns \c true if this byte array view starts with byte array view \a bv
928 or character \a ch, respectively; otherwise returns \c false.
934 \fn bool QByteArrayView::endsWith(QByteArrayView bv) const
935 \fn bool QByteArrayView::endsWith(char ch) const
937 Returns \c true if this byte array view ends with byte array view \a bv
938 or character \a ch, respectively; otherwise returns \c false.
944 \fn qsizetype QByteArrayView::indexOf(QByteArrayView bv, qsizetype from = 0) const
945 \fn qsizetype QByteArrayView::indexOf(char ch, qsizetype from = 0) const
947 Returns the index position of either the start of the first occurrence of
948 the sequence of bytes viewed by \a bv or the first occurrence of byte \a ch,
949 respectively, in this byte array view, searching forward from index position
950 \a from.Returns -1 if no match is found.
952 \include qstring.qdocinc negative-index-start-search-from-end
954 \sa lastIndexOf(), contains()
958 \fn bool QByteArrayView::contains(QByteArrayView bv) const
959 \fn bool QByteArrayView::contains(char ch) const
961 Returns \c true if this byte array view contains an occurrence of the sequence
962 of bytes viewed by \a bv or character \a ch, respectively; otherwise returns
965 \sa indexOf(), lastIndexOf()
969 \fn qsizetype QByteArrayView::lastIndexOf(QByteArrayView bv, qsizetype from) const
970 \fn qsizetype QByteArrayView::lastIndexOf(char ch, qsizetype from = -1) const
972 Returns the index position of either the start of the last occurrence of
973 the sequence of bytes viewed by \a bv or the last occurrence of byte \a ch,
974 respectively, in this byte array view, searching backward from index position
977 \include qstring.qdocinc negative-index-start-search-from-end
979 Returns -1 if no match is found.
981 \note When searching for a 0-length \a bv, the match at the end of
982 the data is excluded from the search by a negative \a from, even
983 though \c{-1} is normally thought of as searching from the end of
984 the view: the match at the end is \e after the last character, so
985 it is excluded. To include such a final empty match, either give a
986 positive value for \a from or omit the \a from parameter entirely.
988 \sa indexOf(), contains()
992 \fn qsizetype QByteArrayView::lastIndexOf(QByteArrayView bv) const
996 Returns the index position of the start of the last
997 occurrence of the sequence of bytes viewed by \a bv in this byte
998 array view, searching backward from the end of this byte array
999 view. Returns -1 if no match is found.
1001 \sa indexOf(), contains()
1005 \fn qsizetype QByteArrayView::count(QByteArrayView bv) const
1007 Returns the number of (potentially overlapping) occurrences of the
1008 sequence of bytes viewed by \a bv in this byte array view.
1010 \sa contains(), indexOf()
1014 \fn bool QByteArrayView::count(char ch) const
1017 Returns the number of occurrences of byte \a ch in this byte array view.
1019 \sa contains(), indexOf()
1023 \fn QByteArrayView qToByteArrayViewIgnoringNull(const QByteArray &b);
1026 Convert \a b to a QByteArrayView ignoring \c{b.isNull()}.
1028 Returns a byte array view that references \a{b}'s data, but is never null.
1030 This is a faster way to convert a QByteArray to a QByteArrayView,
1031 if null QByteArray can legitimately be treated as empty ones.
1033 \sa QByteArray::isNull(), QByteArrayView