6.0.0-rc4Source

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>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt Core C++ Classes

List of all members, including inherited members

Public Types

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)
QByteArraytoByteArray() const
const char *data() const
const char *constData() const
qsizetypesize() const
qsizetypelength() const
boolisNull() const
boolisEmpty() const
boolempty() const
charat(qsizetype i) const
charoperator[](qsizetype i) const
charfront() const
charback() const
const_iteratorbegin() const
const_iteratorend() const
QByteArrayViewmid(qsizetype pos, qsizetype len = -1) const
QByteArrayViewleft(qsizetype c) const
QByteArrayViewright(qsizetype c) const
QByteArrayViewfirst(qsizetype c) const
QByteArrayViewlast(qsizetype c) const
QByteArrayViewsliced(qsizetype pos) const
QByteArrayViewsliced(qsizetype pos, qsizetype c) const
QByteArrayViewchopped(qsizetype c) const
boolstartsWith(QByteArrayView s) const
boolendsWith(QByteArrayView s) const
boolstartsWith(char c) const
boolendsWith(char c) const
qsizetypeindexOf(QByteArrayView s, qsizetype from = 0) const
qsizetypeindexOf(char c, qsizetype from = 0) const
qsizetypelastIndexOf(QByteArrayView s, qsizetype from = -1) const
qsizetypelastIndexOf(char c, qsizetype from = -1) const
boolcontains(QByteArrayView s) const
boolcontains(char c) const
intcompare(QByteArrayView s) const
const_iteratorcbegin() const
const_iteratorcend() const
const_reverse_iteratorrbegin() const
const_reverse_iteratorrend() const
const_reverse_iteratorcrbegin() const
const_reverse_iteratorcrend() const
qsizetypecount(QByteArrayView s) const
qsizetypecount(char c) const
QByteArrayViewtrimmed() const
voidtruncate(qsizetype len)
voidchop(qsizetype len)
QByteArrayView &slice(qsizetype pos)
QByteArrayView &slice(qsizetype pos, qsizetype len)
boolisValidUtf8() const
intcompare(QByteArrayView s, Qt::CaseSensitivity cs) const
shorttoShort(bool *ok = nullptr, int base = 10) const
ushorttoUShort(bool *ok = nullptr, int base = 10) const
inttoInt(bool *ok = nullptr, int base = 10) const
uinttoUInt(bool *ok = nullptr, int base = 10) const
longtoLong(bool *ok = nullptr, int base = 10) const
ulongtoULong(bool *ok = nullptr, int base = 10) const
qlonglongtoLongLong(bool *ok = nullptr, int base = 10) const
qulonglongtoULongLong(bool *ok = nullptr, int base = 10) const
floattoFloat(bool *ok = nullptr) const
doubletoDouble(bool *ok = nullptr) const

Static Public Members

QByteArrayViewfromArray(const char (&data)[N])
qsizetypemax_size()
qsizetypemaxSize()

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

[constexpr] QByteArrayView::QByteArrayView()

Constructs a null view.

See also isNull().

[constexpr] QByteArrayView::QByteArrayView(std::nullptr_t)

This is an overloaded function.

Constructs a null view.

[constexpr] QByteArrayView::QByteArrayView(const char *data, qsizetype len)

This is an overloaded function.

Constructs a view of the len bytes starting at data.

[constexpr] 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.

[constexpr] 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>[constexpr] 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.

[constexpr] char QByteArrayView::at(qsizetype i) const

Returns the byte at index position i. i must be a valid index; no bounds checking is done.

See also front() and back().

[constexpr] char QByteArrayView::back() const

Returns the last byte of the view. The view must not be empty.

See also front().

[constexpr] 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().

[constexpr] 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().

[constexpr] 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.

[constexpr] 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().

[constexpr] 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().

[constexpr] bool QByteArrayView::empty() const

Returns true if the view has no bytes. This is the same as isEmpty(), provided for STL compatibility.

[constexpr] 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.

[constexpr] QByteArrayView QByteArrayView::first(qsizetype c) const

Returns a view of the first c bytes. c must not exceed size(); no bounds checking is done.

See also last() and left().

template <std::size_t N>[static, constexpr] 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.

[constexpr] 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.

[constexpr] bool QByteArrayView::isEmpty() const

Returns true if the view has no bytes; otherwise returns false.

See also isNull() and size().

[constexpr] 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.

[constexpr] QByteArrayView QByteArrayView::last(qsizetype c) const

Returns a view of the last c bytes. c must not exceed size(); no bounds checking is done.

See also first() and right().

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().

[constexpr] qsizetype QByteArrayView::length() const

Returns the number of bytes in the view. This is the same as size().

[static, constexpr] qsizetype QByteArrayView::max_size()

Returns the largest number of bytes a view can refer to.

This function was introduced in 6.8.

[static, constexpr] 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().

[constexpr] 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().

Returns the last c bytes of the view, or the whole view if c is larger than size().

See also left(), mid() and last().

[constexpr] qsizetype QByteArrayView::size() const

Returns the number of bytes in the view.

See also isEmpty().

[constexpr] 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().

[constexpr] 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.

[constexpr] 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.

See also mid() and chopped().

[constexpr] 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.

[constexpr] 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().

[constexpr] void QByteArrayView::truncate(qsizetype len)

Shortens the view to its first len bytes. len must not exceed size(); no bounds checking is done.

See also chop() and first().