QByteArrayView Class
The QByteArrayView class provides a view on an array of bytes with a read-only subset of the QByteArray API. More…
| Header | #include <QByteArrayView> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Core) |
| Module | Nt Core C++ Classes |
List of all members, including inherited members
Public Types
| typedef | const_iterator |
| typedef | iterator |
| typedef | value_type |
| typedef | const_reverse_iterator |
| typedef | reverse_iterator |
| typedef | size_type |
| typedef | difference_type |
Public Functions
| QByteArrayView() | |
| QByteArrayView(std::nullptr_t) | |
| QByteArrayView(const char *data, qsizetype len) | |
| QByteArrayView(const char *data) | |
| QByteArrayView(const QByteArray &b) | |
| QByteArrayView(std::string_view s) | |
| QByteArrayView(const Container &c) | |
| QByteArray | toByteArray() const |
| const char * | data() const |
| const char * | constData() const |
| qsizetype | size() const |
| qsizetype | length() const |
| bool | isNull() const |
| bool | isEmpty() const |
| bool | empty() const |
| char | at(qsizetype i) const |
| char | operator[](qsizetype i) const |
| char | front() const |
| char | back() const |
| const_iterator | begin() const |
| const_iterator | end() const |
| QByteArrayView | mid(qsizetype pos, qsizetype len = -1) const |
| QByteArrayView | left(qsizetype c) const |
| QByteArrayView | right(qsizetype c) const |
| QByteArrayView | first(qsizetype c) const |
| QByteArrayView | last(qsizetype c) const |
| QByteArrayView | sliced(qsizetype pos) const |
| QByteArrayView | sliced(qsizetype pos, qsizetype c) const |
| QByteArrayView | chopped(qsizetype c) const |
| bool | startsWith(QByteArrayView s) const |
| bool | endsWith(QByteArrayView s) const |
| bool | startsWith(char c) const |
| bool | endsWith(char c) const |
| qsizetype | indexOf(QByteArrayView s, qsizetype from = 0) const |
| qsizetype | indexOf(char c, qsizetype from = 0) const |
| qsizetype | lastIndexOf(QByteArrayView s, qsizetype from = -1) const |
| qsizetype | lastIndexOf(char c, qsizetype from = -1) const |
| bool | contains(QByteArrayView s) const |
| bool | contains(char c) const |
| int | compare(QByteArrayView s) const |
| const_iterator | cbegin() const |
| const_iterator | cend() const |
| const_reverse_iterator | rbegin() const |
| const_reverse_iterator | rend() const |
| const_reverse_iterator | crbegin() const |
| const_reverse_iterator | crend() const |
| qsizetype | count(QByteArrayView s) const |
| qsizetype | count(char c) const |
| QByteArrayView | trimmed() const |
| void | truncate(qsizetype len) |
| void | chop(qsizetype len) |
| QByteArrayView & | slice(qsizetype pos) |
| QByteArrayView & | slice(qsizetype pos, qsizetype len) |
| bool | isValidUtf8() const |
| int | compare(QByteArrayView s, Qt::CaseSensitivity cs) const |
| short | toShort(bool *ok = nullptr, int base = 10) const |
| ushort | toUShort(bool *ok = nullptr, int base = 10) const |
| int | toInt(bool *ok = nullptr, int base = 10) const |
| uint | toUInt(bool *ok = nullptr, int base = 10) const |
| long | toLong(bool *ok = nullptr, int base = 10) const |
| ulong | toULong(bool *ok = nullptr, int base = 10) const |
| qlonglong | toLongLong(bool *ok = nullptr, int base = 10) const |
| qulonglong | toULongLong(bool *ok = nullptr, int base = 10) const |
| float | toFloat(bool *ok = nullptr) const |
| double | toDouble(bool *ok = nullptr) const |
Static Public Members
Detailed Description
A QByteArrayView refers to a contiguous range of bytes without owning or copying it, so the viewed data must outlive the view. It is cheap to copy and is meant for function parameters that only read bytes, accepting a QByteArray, a string literal or a std::string_view alike.
A view is null if it was constructed without data (or from a null QByteArray), and empty if its size is 0. A null view is also empty.
See also QByteArray and QStringView.
Differences from Qt
The number conversions copy the bytes into a QByteArray first, so they allocate. There is no constructor from a container of bytes other than QByteArray and std::string_view; use fromArray() for a literal array. first(), last(), sliced(), chopped(), slice(), truncate() and chop() do not check their arguments.
Member Type Documentation
typedef QByteArrayView::const_iterator
A read-only iterator over the bytes of the view. It is a plain const char *.
typedef QByteArrayView::const_reverse_iterator
A read-only reverse iterator over the bytes of the view.
typedef QByteArrayView::difference_type
The type of the distance between two iterators, for STL compatibility.
typedef QByteArrayView::iterator
The iterator type, for STL compatibility. It is the same as const_iterator.
typedef QByteArrayView::reverse_iterator
The reverse iterator type, for STL compatibility. It is the same as const_reverse_iterator.
typedef QByteArrayView::size_type
The type of sizes and indexes, for STL compatibility.
typedef QByteArrayView::value_type
The element type of the view, for STL compatibility.
Member Function Documentation
QByteArrayView::QByteArrayView()
Constructs a null view.
See also isNull().
QByteArrayView::QByteArrayView(std::nullptr_t)
This is an overloaded function.
Constructs a null view.
QByteArrayView::QByteArrayView(const char *data, qsizetype len)
This is an overloaded function.
Constructs a view of the len bytes starting at data.
QByteArrayView::QByteArrayView(const char *data)
This is an overloaded function.
Constructs a view of the null-terminated string data, not including the terminator. The view is null if data is nullptr.
QByteArrayView::QByteArrayView(const QByteArray &b)
This is an overloaded function.
Constructs a view of the contents of the byte array b. The view is null if b is null. It is invalidated when b is modified or destroyed.
QByteArrayView::QByteArrayView(std::string_view s)
This is an overloaded function.
Constructs a view of the characters of the string view s.
template <typename Container, if_compatible_container<Container> = 0> QByteArrayView::QByteArrayView(const Container &c)
This is an overloaded function.
Constructs a view of the bytes of the container c: anything that has data() returning a pointer to char and size(), such as a QLatin1StringView, a std::string, a std::vector<char> or a std::array<char, N>. The view is invalidated when c is modified or destroyed.
char QByteArrayView::at(qsizetype i) const
Returns the byte at index position i. i must be a valid index; no bounds checking is done.
char QByteArrayView::back() const
Returns the last byte of the view. The view must not be empty.
See also front().
const_iterator QByteArrayView::begin() const
Returns an iterator pointing to the first byte of the view.
See also end().
const_iterator QByteArrayView::cbegin() const
Returns a const iterator pointing to the first byte of the view. This is the same as begin().
See also cend().
const_iterator QByteArrayView::cend() const
Returns a const iterator pointing just after the last byte of the view. This is the same as end().
See also cbegin().
void QByteArrayView::chop(qsizetype len)
Removes len bytes from the end of the view. len must not exceed size(); no bounds checking is done.
See also truncate() and chopped().
QByteArrayView QByteArrayView::chopped(qsizetype c) const
Returns the view with the last c bytes removed. c must not exceed size(); no bounds checking is done.
See also sliced().
int QByteArrayView::compare(QByteArrayView s) const
Compares the view with s byte by byte and returns a negative integer, zero or a positive integer if the view is less than, equal to or greater than s.
int QByteArrayView::compare(QByteArrayView s, Qt::CaseSensitivity cs) const
This is an overloaded function.
Compares the view with s and returns a negative integer, zero or a positive integer if the view is less than, equal to or greater than s. With Qt::CaseInsensitive as cs, the ASCII letters compare equal whatever their case.
const char * QByteArrayView::constData() const
Returns a pointer to the first byte of the view. This is the same as data().
bool QByteArrayView::contains(QByteArrayView s) const
Returns true if the view contains s; otherwise returns false.
See also indexOf().
bool QByteArrayView::contains(char c) const
This is an overloaded function.
Returns true if the view contains the byte c; otherwise returns false.
qsizetype QByteArrayView::count(QByteArrayView s) const
Returns the number of occurrences of s in the view, which may overlap. An empty s is found size() + 1 times.
See also indexOf() and contains().
qsizetype QByteArrayView::count(char c) const
This is an overloaded function.
Returns the number of bytes in the view that equal c.
const_reverse_iterator QByteArrayView::crbegin() const
Returns a reverse iterator pointing to the last byte of the view. This is the same as rbegin().
const_reverse_iterator QByteArrayView::crend() const
Returns a reverse iterator pointing just before the first byte of the view. This is the same as rend().
const char * QByteArrayView::data() const
Returns a pointer to the first byte of the view. The data is not necessarily null-terminated.
See also constData().
bool QByteArrayView::empty() const
Returns true if the view has no bytes. This is the same as isEmpty(), provided for STL compatibility.
const_iterator QByteArrayView::end() const
Returns an iterator pointing just after the last byte of the view.
See also begin().
bool QByteArrayView::endsWith(QByteArrayView s) const
Returns true if the view ends with s; otherwise returns false.
See also startsWith().
bool QByteArrayView::endsWith(char c) const
This is an overloaded function.
Returns true if the view ends with the byte c; otherwise returns false.
QByteArrayView QByteArrayView::first(qsizetype c) const
Returns a view of the first c bytes. c must not exceed size(); no bounds checking is done.
template <std::size_t N> QByteArrayView QByteArrayView::fromArray(const char (&data)[N])
Returns a view of the whole array data, including a terminating null if there is one: fromArray("ab") has three bytes, where QByteArrayView("ab") has two.
char QByteArrayView::front() const
Returns the first byte of the view. The view must not be empty.
See also back().
qsizetype QByteArrayView::indexOf(QByteArrayView s, qsizetype from = 0) const
Returns the index position of the first occurrence of s in the view, searching forward from index position from, or -1 if it is not found.
See also lastIndexOf() and contains().
qsizetype QByteArrayView::indexOf(char c, qsizetype from = 0) const
This is an overloaded function.
Returns the index position of the first occurrence of the byte c in the view, searching forward from index position from, or -1 if it is not found.
bool QByteArrayView::isEmpty() const
Returns true if the view has no bytes; otherwise returns false.
bool QByteArrayView::isNull() const
Returns true if the view has no data (it was default-constructed or built from a null array or pointer); otherwise returns false.
See also isEmpty().
bool QByteArrayView::isValidUtf8() const
Returns true if the view holds well-formed UTF-8; otherwise returns false. An empty view is valid.
This function was introduced in 6.3.
QByteArrayView QByteArrayView::last(qsizetype c) const
Returns a view of the last c bytes. c must not exceed size(); no bounds checking is done.
qsizetype QByteArrayView::lastIndexOf(QByteArrayView s, qsizetype from = -1) const
Returns the index position of the last occurrence of s in the view, searching backward from index position from, or -1 if it is not found. A negative from searches from the end of the view; the match must begin at or before from.
See also indexOf() and contains().
qsizetype QByteArrayView::lastIndexOf(char c, qsizetype from = -1) const
This is an overloaded function.
Returns the index position of the last occurrence of the byte c in the view, searching backward from index position from, or -1 if it is not found.
QByteArrayView QByteArrayView::left(qsizetype c) const
Returns the first c bytes of the view, or the whole view if c is larger than size().
See also right(), mid() and first().
qsizetype QByteArrayView::length() const
Returns the number of bytes in the view. This is the same as size().
qsizetype QByteArrayView::max_size()
Returns the largest number of bytes a view can refer to.
This function was introduced in 6.8.
qsizetype QByteArrayView::maxSize()
Returns the largest number of bytes a view can refer to. This is the same as max_size().
This function was introduced in 6.8.
QByteArrayView QByteArrayView::mid(qsizetype pos, qsizetype len = -1) const
Returns a view of len bytes starting at index position pos. If len is negative or extends past the end, the view extends to the end. A negative pos is clamped to 0 and reduces len accordingly. If pos is past the end, an empty view is returned.
See also left(), right() and sliced().
char QByteArrayView::operator[](qsizetype i) const
Returns the byte at index position i. This is the same as at().
const_reverse_iterator QByteArrayView::rbegin() const
Returns a reverse iterator pointing to the last byte of the view.
See also rend().
const_reverse_iterator QByteArrayView::rend() const
Returns a reverse iterator pointing just before the first byte of the view.
See also rbegin().
QByteArrayView QByteArrayView::right(qsizetype c) const
Returns the last c bytes of the view, or the whole view if c is larger than size().
See also left(), mid() and last().
qsizetype QByteArrayView::size() const
Returns the number of bytes in the view.
See also isEmpty().
QByteArrayView & QByteArrayView::slice(qsizetype pos)
Changes the view to start at index position pos and to extend to its end, and returns a reference to it. pos must not exceed size().
This function was introduced in 6.8.
See also sliced().
QByteArrayView & QByteArrayView::slice(qsizetype pos, qsizetype len)
This is an overloaded function.
Changes the view to the len bytes that start at index position pos, and returns a reference to it. The range must lie within the view.
This function was introduced in 6.8.
QByteArrayView QByteArrayView::sliced(qsizetype pos) const
Returns a view of the part starting at index position pos and extending to the end. pos must not exceed size(); no bounds checking is done.
QByteArrayView QByteArrayView::sliced(qsizetype pos, qsizetype c) const
This is an overloaded function.
Returns a view of c bytes starting at index position pos. The range must lie within the view; no bounds checking is done.
bool QByteArrayView::startsWith(QByteArrayView s) const
Returns true if the view starts with s; otherwise returns false.
See also endsWith().
bool QByteArrayView::startsWith(char c) const
This is an overloaded function.
Returns true if the view starts with the byte c; otherwise returns false.
QByteArray QByteArrayView::toByteArray() const
Returns a copy of the viewed bytes as a QByteArray. The result is null if the view is null.
double QByteArrayView::toDouble(bool *ok = nullptr) const
Returns the view converted to a double, or 0 if it is not a number. The text is read in the C locale. If ok is not nullptr, *ok is set to whether the conversion succeeded.
See also QByteArray::toDouble().
float QByteArrayView::toFloat(bool *ok = nullptr) const
Returns the view converted to a float, or 0 if it is not a number. If ok is not nullptr, *ok is set to whether the conversion succeeded.
See also toDouble().
int QByteArrayView::toInt(bool *ok = nullptr, int base = 10) const
Returns the view converted to an int in base base, as toShort() does, and sets *ok if ok is not nullptr.
See also QByteArray::toInt().
long QByteArrayView::toLong(bool *ok = nullptr, int base = 10) const
Returns the view converted to a long in base base, as toShort() does, and sets *ok if ok is not nullptr.
qlonglong QByteArrayView::toLongLong(bool *ok = nullptr, int base = 10) const
Returns the view converted to a long long in base base, as toShort() does, and sets *ok if ok is not nullptr.
short QByteArrayView::toShort(bool *ok = nullptr, int base = 10) const
Returns the view converted to a short in base base, which is between 2 and 36, or 0 to detect the base from a 0x, 0b or 0 prefix. Returns 0 if the conversion fails or the value does not fit. If ok is not nullptr, *ok is set to whether the conversion succeeded.
See also QByteArray::toShort().
uint QByteArrayView::toUInt(bool *ok = nullptr, int base = 10) const
Returns the view converted to an unsigned int in base base, as toShort() does, and sets *ok if ok is not nullptr.
ulong QByteArrayView::toULong(bool *ok = nullptr, int base = 10) const
Returns the view converted to an unsigned long in base base, as toShort() does, and sets *ok if ok is not nullptr.
qulonglong QByteArrayView::toULongLong(bool *ok = nullptr, int base = 10) const
Returns the view converted to an unsigned long long in base base, as toShort() does, and sets *ok if ok is not nullptr.
ushort QByteArrayView::toUShort(bool *ok = nullptr, int base = 10) const
Returns the view converted to an unsigned short in base base, as toShort() does, and sets *ok if ok is not nullptr.
QByteArrayView QByteArrayView::trimmed() const
Returns the view without the whitespace at its start and its end. Whitespace means the ASCII characters space, '\t', '\n', '\v', '\f' and '\r', as for QByteArray::trimmed().
void QByteArrayView::truncate(qsizetype len)
Shortens the view to its first len bytes. len must not exceed size(); no bounds checking is done.