QAnyStringView Class
The QAnyStringView class provides a unified view on Latin-1, UTF-8 or UTF-16 strings. More…
| Header | #include <QAnyStringView> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Core) |
| Module | Nt Core C++ Classes |
| Since | 6.0 |
List of all members, including inherited members
Public Types
| typedef | size_type |
Public Functions
| QAnyStringView() | |
| QAnyStringView(std::nullptr_t) | |
| QAnyStringView(QStringView str) | |
| QAnyStringView(const QString &str) | |
| QAnyStringView(QLatin1StringView str) | |
| QAnyStringView(QUtf8StringView str) | |
| QAnyStringView(const char *str) | |
| QAnyStringView(const char *str, qsizetype len) | |
| QAnyStringView(const QByteArray &str) | |
| QAnyStringView(const std::string &str) | |
| QAnyStringView(const char16_t *str) | |
| QAnyStringView(const char16_t *str, qsizetype len) | |
| QAnyStringView(std::u16string_view str) | |
| QAnyStringView(const QChar &c) | |
| decltype(auto) | visit(Visitor &&v) const |
| QString | toString() const |
| const void * | data() const |
| qsizetype | size() const |
| qsizetype | length() const |
| qsizetype | size_bytes() const |
| bool | isNull() const |
| bool | isEmpty() const |
| bool | empty() const |
| QChar | front() const |
| QChar | back() const |
| QAnyStringView | first(qsizetype count) const |
| QAnyStringView | last(qsizetype count) const |
| QAnyStringView | sliced(qsizetype pos) const |
| QAnyStringView | sliced(qsizetype pos, qsizetype count) const |
| QAnyStringView | chopped(qsizetype count) const |
| QAnyStringView | left(qsizetype count) const |
| QAnyStringView | right(qsizetype count) const |
| QAnyStringView | mid(qsizetype pos, qsizetype count = -1) const |
| void | truncate(qsizetype count) |
| void | chop(qsizetype count) |
Static Public Members
| qsizetype | max_size() |
| int | compare(QAnyStringView lhs, QAnyStringView rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive) |
| bool | equal(QAnyStringView lhs, QAnyStringView rhs) |
Detailed Description
A QAnyStringView refers to text in one of three encodings without owning or converting it: a QLatin1StringView, a QUtf8StringView (including plain C strings and QByteArray) or a QStringView (including QString). It suits function parameters that only read text and accept all of them:
void setName(QAnyStringView name) { m_name = name.toString(); }
setName("ascii");
setName(u"utf-16");
setName(QLatin1StringView("latin-1"));
Use visit() to work on the underlying view directly. Sizes count code units of the viewed encoding.
See also QStringView, QUtf8StringView and QLatin1StringView.
Differences from Qt
The text classes of Nt take QString arguments, so QAnyStringView is mainly for user code. Comparisons convert both sides to QString, so they allocate. There is no constructor taking a callable or a char8_t string.
Member Type Documentation
typedef QAnyStringView::size_type
Alias for qsizetype.
Member Function Documentation
QAnyStringView::QAnyStringView()
Constructs a null view.
QAnyStringView::QAnyStringView(std::nullptr_t)
Constructs a null view.
QAnyStringView::QAnyStringView(QStringView str)
Constructs a view of the UTF-16 view str.
QAnyStringView::QAnyStringView(const QString &str)
Constructs a view of the characters of str.
QAnyStringView::QAnyStringView(QLatin1StringView str)
Constructs a view of the Latin-1 view str.
QAnyStringView::QAnyStringView(QUtf8StringView str)
Constructs a view of the UTF-8 view str.
QAnyStringView::QAnyStringView(const char *str)
Constructs a view of the null-terminated UTF-8 string str.
QAnyStringView::QAnyStringView(const char *str, qsizetype len)
Constructs a view of the len UTF-8 bytes at str.
QAnyStringView::QAnyStringView(const QByteArray &str)
Constructs a view of the UTF-8 bytes of str.
QAnyStringView::QAnyStringView(const std::string &str)
Constructs a view of the UTF-8 bytes of str.
QAnyStringView::QAnyStringView(const char16_t *str)
Constructs a view of the null-terminated UTF-16 string str.
QAnyStringView::QAnyStringView(const char16_t *str, qsizetype len)
Constructs a view of the len UTF-16 code units at str.
QAnyStringView::QAnyStringView(std::u16string_view str)
Constructs a view of the UTF-16 code units of str.
QAnyStringView::QAnyStringView(const QChar &c)
Constructs a view of the single character c. The view refers to c, which must outlive it.
QChar QAnyStringView::back() const
Returns the last character. The view must not be empty.
void QAnyStringView::chop(qsizetype count)
Removes count code units from the end of the view.
This function was introduced in 6.5.
QAnyStringView QAnyStringView::chopped(qsizetype count) const
Returns a view without the last count code units.
This function was introduced in 6.5.
int QAnyStringView::compare(QAnyStringView lhs, QAnyStringView rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive)
Compares lhs with rhs, using the case sensitivity cs, and returns a negative number, zero or a positive number if lhs is less than, equal to or greater than rhs.
See also equal().
const void * QAnyStringView::data() const
Returns a pointer to the first code unit of the view.
bool QAnyStringView::empty() const
Same as isEmpty(). Provided for STL compatibility.
bool QAnyStringView::equal(QAnyStringView lhs, QAnyStringView rhs)
Returns true if lhs and rhs hold the same text, whatever their encodings; otherwise returns false.
See also compare().
QAnyStringView QAnyStringView::first(qsizetype count) const
Returns a view of the first count code units.
This function was introduced in 6.5.
QChar QAnyStringView::front() const
Returns the first character. For a UTF-8 view of a non-ASCII text this is the first character decoded. The view must not be empty.
bool QAnyStringView::isEmpty() const
Returns true if the view is empty; otherwise returns false.
bool QAnyStringView::isNull() const
Returns true if the view is null; otherwise returns false.
QAnyStringView QAnyStringView::last(qsizetype count) const
Returns a view of the last count code units.
This function was introduced in 6.5.
QAnyStringView QAnyStringView::left(qsizetype count) const
Returns a view of the first count code units, or the whole view if count is negative or larger than size().
See also first(), right() and mid().
qsizetype QAnyStringView::length() const
Same as size().
qsizetype QAnyStringView::max_size()
Returns the largest number of code units a view can refer to.
This function was introduced in 6.8.
QAnyStringView QAnyStringView::mid(qsizetype pos, qsizetype count = -1) const
Returns a view of count code units from index pos, or of the code units from pos to the end if count is negative or reaches past the end. Returns an empty view if pos is out of range.
See also sliced(), left() and right().
QAnyStringView QAnyStringView::right(qsizetype count) const
Returns a view of the last count code units, or the whole view if count is negative or larger than size().
See also last(), left() and mid().
qsizetype QAnyStringView::size() const
Returns the number of code units in the view.
qsizetype QAnyStringView::size_bytes() const
Returns the size of the view in bytes.
QAnyStringView QAnyStringView::sliced(qsizetype pos) const
Returns a view of the code units from pos to the end.
This function was introduced in 6.5.
QAnyStringView QAnyStringView::sliced(qsizetype pos, qsizetype count) const
This is an overloaded function.
Returns a view of count code units from pos.
This function was introduced in 6.5.
QString QAnyStringView::toString() const
Returns the viewed text converted to a QString.
void QAnyStringView::truncate(qsizetype count)
Shortens the view to count code units.
This function was introduced in 6.5.
template <typename Visitor>decltype(auto) QAnyStringView::visit(Visitor &&v) const
Calls v with the underlying view, a QLatin1StringView, a QUtf8StringView or a QStringView, and returns its result.