6.0.0-rc4Source

QUtf8StringView Class

The QUtf8StringView class provides a unified view on UTF-8 strings with a read-only subset of the QString API. More…

Header#include <QUtf8StringView>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt Core C++ Classes
Since6.0

List of all members, including inherited members

Public Types

Public Functions

QUtf8StringView()
QUtf8StringView(std::nullptr_t)
QUtf8StringView(const char *str, qsizetype len)
QUtf8StringView(const char *str)
QUtf8StringView(const QByteArray &ba)
QUtf8StringView(std::string_view s)
QUtf8StringView(const std::string &s)
QStringtoString() const
const char *data() const
const char *utf8() const
qsizetypesize() const
qsizetypelength() const
qsizetypesize_bytes() const
boolisNull() const
boolisEmpty() const
boolempty() const
charat(qsizetype i) const
charoperator[](qsizetype i) const
charfront() const
charback() const
QUtf8StringViewfirst(qsizetype count) const
QUtf8StringViewlast(qsizetype count) const
QUtf8StringViewsliced(qsizetype pos) const
QUtf8StringViewsliced(qsizetype pos, qsizetype count) const
QUtf8StringViewchopped(qsizetype count) const
QUtf8StringViewleft(qsizetype count) const
QUtf8StringViewright(qsizetype count) const
QUtf8StringViewmid(qsizetype pos, qsizetype count = -1) const
QUtf8StringView &slice(qsizetype pos)
QUtf8StringView &slice(qsizetype pos, qsizetype count)
voidtruncate(qsizetype count)
voidchop(qsizetype count)
boolisValidUtf8() const
intcompare(QUtf8StringView other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
const_iteratorbegin() const
const_iteratorend() const
const_iteratorcbegin() const
const_iteratorcend() const
const_reverse_iteratorrbegin() const
const_reverse_iteratorrend() const
const_reverse_iteratorcrbegin() const
const_reverse_iteratorcrend() const

Static Public Members

qsizetypemax_size()
qsizetypemaxSize()
QUtf8StringViewfromArray(const char (&str)[Size])

Detailed Description

A QUtf8StringView refers to a range of UTF-8 bytes without owning or copying them, so the viewed data must outlive the view. Sizes and indexes count bytes (code units), not characters.

QUtf8StringView name = "Grüße";
qsizetype bytes = name.size();   // 7
QString text = name.toString(); // 5 characters

See also QStringView, QLatin1StringView and QAnyStringView.

Differences from Qt

QUtf8StringView is a class, not an alias of the QBasicUtf8StringView template, and there is no char8_t support because Nt is built as C++17. Comparisons compare the bytes, which orders the strings by code point.

Member Type Documentation

typedef QUtf8StringView::const_iterator

A random access iterator over the bytes of the view.

typedef QUtf8StringView::const_reverse_iterator

The reverse iterator type, for STL compatibility.

typedef QUtf8StringView::iterator

Same as const_iterator; the view is read-only.

typedef QUtf8StringView::reverse_iterator

Same as const_reverse_iterator; the view is read-only.

typedef QUtf8StringView::size_type

Alias for qsizetype.

typedef QUtf8StringView::storage_type

The type of a code unit, char.

typedef QUtf8StringView::value_type

Alias for const char.

Member Function Documentation

[constexpr] QUtf8StringView::QUtf8StringView()

Constructs a null view.

See also isNull().

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

Constructs a null view.

[constexpr] QUtf8StringView::QUtf8StringView(const char *str, qsizetype len)

Constructs a view of the len bytes at str.

[constexpr] QUtf8StringView::QUtf8StringView(const char *str)

Constructs a view of the null-terminated string str. A null str gives a null view.

QUtf8StringView::QUtf8StringView(const QByteArray &ba)

Constructs a view of the bytes of ba. A null byte array gives a null view.

[constexpr] QUtf8StringView::QUtf8StringView(std::string_view s)

Constructs a view of the bytes of s.

QUtf8StringView::QUtf8StringView(const std::string &s)

Constructs a view of the bytes of s.

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

Returns the byte at index i, which must be valid.

[constexpr] char QUtf8StringView::back() const

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

[constexpr] const_iterator QUtf8StringView::begin() const

Returns an iterator to the first byte.

[constexpr] const_iterator QUtf8StringView::cbegin() const

Same as begin().

[constexpr] const_iterator QUtf8StringView::cend() const

Same as end().

[constexpr] void QUtf8StringView::chop(qsizetype count)

Removes count bytes from the end of the view.

[constexpr] QUtf8StringView QUtf8StringView::chopped(qsizetype count) const

Returns a view without the last count bytes.

int QUtf8StringView::compare(QUtf8StringView other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

Compares this view with other and returns a negative number, zero or a positive number if it is less than, equal to or greater than other. With Qt::CaseInsensitive (cs), the texts are compared after case folding.

This function was introduced in 6.5.

const_reverse_iterator QUtf8StringView::crbegin() const

Same as rbegin().

const_reverse_iterator QUtf8StringView::crend() const

Same as rend().

[constexpr] const char * QUtf8StringView::data() const

Returns a pointer to the first byte of the view. The data is not necessarily null-terminated.

[constexpr] bool QUtf8StringView::empty() const

Same as isEmpty(). Provided for STL compatibility.

[constexpr] const_iterator QUtf8StringView::end() const

Returns an iterator just past the last byte.

[constexpr] QUtf8StringView QUtf8StringView::first(qsizetype count) const

Returns a view of the first count bytes.

template <std::size_t Size>[static, constexpr] QUtf8StringView QUtf8StringView::fromArray(const char (&str)[Size])

Returns a view of the whole character array str, including a trailing '\0' if it has one.

[constexpr] char QUtf8StringView::front() const

Returns the first byte. The view must not be empty.

[constexpr] bool QUtf8StringView::isEmpty() const

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

[constexpr] bool QUtf8StringView::isNull() const

Returns true if the view is null; otherwise returns false.

bool QUtf8StringView::isValidUtf8() const

Returns true if the view holds well-formed UTF-8; otherwise returns false.

This function was introduced in 6.3.

[constexpr] QUtf8StringView QUtf8StringView::last(qsizetype count) const

Returns a view of the last count bytes.

[constexpr] QUtf8StringView QUtf8StringView::left(qsizetype count) const

Returns a view of the first count bytes, or the whole view if count is negative or larger than size().

See also first(), right() and mid().

[constexpr] qsizetype QUtf8StringView::length() const

Same as size().

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

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

This function was introduced in 6.8.

See also maxSize().

[static, constexpr] qsizetype QUtf8StringView::maxSize()

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

This function was introduced in 6.8.

See also max_size().

[constexpr] QUtf8StringView QUtf8StringView::mid(qsizetype pos, qsizetype count = -1) const

Returns a view of count bytes from index pos, or of the bytes from pos to the end if count is negative or reaches past the end. Returns an empty view if pos is past the end.

See also sliced(), left() and right().

[constexpr] char QUtf8StringView::operator[](qsizetype i) const

Same as at(i).

const_reverse_iterator QUtf8StringView::rbegin() const

Returns a reverse iterator to the last byte.

const_reverse_iterator QUtf8StringView::rend() const

Returns a reverse iterator just before the first byte.

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

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

[constexpr] qsizetype QUtf8StringView::size() const

Returns the number of bytes in the view.

[constexpr] qsizetype QUtf8StringView::size_bytes() const

Returns the size of the view in bytes, which is size().

[constexpr] QUtf8StringView & QUtf8StringView::slice(qsizetype pos)

Removes the bytes before index pos from the view and returns a reference to it.

This function was introduced in 6.8.

See also sliced().

[constexpr] QUtf8StringView & QUtf8StringView::slice(qsizetype pos, qsizetype count)

This is an overloaded function.

Makes the view refer to count bytes from index pos and returns a reference to it.

This function was introduced in 6.8.

[constexpr] QUtf8StringView QUtf8StringView::sliced(qsizetype pos) const

Returns a view of the bytes from index pos to the end.

[constexpr] QUtf8StringView QUtf8StringView::sliced(qsizetype pos, qsizetype count) const

This is an overloaded function.

Returns a view of count bytes from index pos.

QString QUtf8StringView::toString() const

Returns the viewed text converted to a QString.

[constexpr] void QUtf8StringView::truncate(qsizetype count)

Shortens the view to count bytes.

[constexpr] const char * QUtf8StringView::utf8() const

Same as data().