Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
QAnyStringView Class Reference

\inmodule QtCore More...

#include <qanystringview.h>

+ Collaboration diagram for QAnyStringView:

Public Types

typedef qptrdiff difference_type
 Alias for {std::ptrdiff_t}.
 
typedef qsizetype size_type
 Alias for qsizetype.
 

Public Member Functions

constexpr QAnyStringView () noexcept
 Constructs a null string view.
 
constexpr QAnyStringView (std::nullptr_t) noexcept
 Constructs a null string view.
 
template<typename Char , if_compatible_char< Char > = true>
constexpr QAnyStringView (const Char *str, qsizetype len)
 Constructs a string view on str with length len.
 
template<typename Char , if_compatible_char< Char > = true>
constexpr QAnyStringView (const Char *f, const Char *l)
 Constructs a string view on first with length (last - first).
 
template<typename Pointer , if_compatible_pointer< Pointer > = true>
constexpr QAnyStringView (const Pointer &str) noexcept
 
 QAnyStringView (const QByteArray &str) noexcept
 Constructs a string view on str.
 
 QAnyStringView (const QString &str) noexcept
 Constructs a string view on str.
 
constexpr QAnyStringView (QLatin1StringView str) noexcept
 
template<typename Container , if_compatible_container< Container > = true>
constexpr Q_ALWAYS_INLINE QAnyStringView (const Container &c) noexcept
 
template<typename Container , if_convertible_to< QString, Container > = true>
constexpr QAnyStringView (Container &&c, QtPrivate::wrapped_t< Container, QString > &&capacity={})
 
template<typename Container , if_convertible_to< QByteArray, Container > = true>
constexpr QAnyStringView (Container &&c, QtPrivate::wrapped_t< Container, QByteArray > &&capacity={})
 
template<typename Char , if_compatible_char< Char > = true>
constexpr QAnyStringView (const Char &c) noexcept
 
constexpr QAnyStringView (const QChar &c) noexcept
 
template<typename Char , typename Container = decltype(QChar::fromUcs4(U'x')), std::enable_if_t< std::is_same_v< Char, char32_t >, bool > = true>
constexpr QAnyStringView (Char c, Container &&capacity={})
 
constexpr QAnyStringView (QStringView v) noexcept
 
template<bool UseChar8T>
constexpr QAnyStringView (QBasicUtf8StringView< UseChar8T > v) noexcept
 
template<typename Visitor >
constexpr decltype(auto) visit (Visitor &&v) const
 Calls v with either a QUtf8StringView, QLatin1String, or QStringView, depending on the encoding of the string data this string-view references.
 
constexpr QAnyStringView mid (qsizetype pos, qsizetype n=-1) const
 
constexpr QAnyStringView left (qsizetype n) const
 
constexpr QAnyStringView right (qsizetype n) const
 
constexpr QAnyStringView sliced (qsizetype pos) const
 
constexpr QAnyStringView sliced (qsizetype pos, qsizetype n) const
 
constexpr QAnyStringView first (qsizetype n) const
 
constexpr QAnyStringView last (qsizetype n) const
 
constexpr QAnyStringView chopped (qsizetype n) const
 
constexpr void truncate (qsizetype n)
 
constexpr void chop (qsizetype n)
 
QString toString () const
 Returns a deep copy of this string view's data as a QString.
 
constexpr qsizetype size () const noexcept
 Returns the size of this string view, in the encoding's code points.
 
constexpr const voiddata () const noexcept
 Returns a const pointer to the first character in the string view.
 
constexpr QChar front () const
 Returns the first character in the string view.
 
constexpr QChar back () const
 Returns the last character in the string view.
 
constexpr bool empty () const noexcept
 Returns whether this string view is empty - that is, whether {size() == 0}.
 
constexpr qsizetype size_bytes () const noexcept
 Returns the size of this string view, but in bytes, not code-points.
 
constexpr bool isNull () const noexcept
 Returns whether this string view is null - that is, whether {data() == nullptr}.
 
constexpr bool isEmpty () const noexcept
 Returns whether this string view is empty - that is, whether {size() == 0}.
 
constexpr qsizetype length () const noexcept
 Same as size().
 
template<typename Visitor >
constexpr decltype(auto) visit (Visitor &&v) const
 

Static Public Member Functions

template<typename Char , size_t Size, if_compatible_char< Char > = true>
static constexpr QAnyStringView fromArray (const Char(&string)[Size]) noexcept
 
static Q_CORE_EXPORT int compare (QAnyStringView lhs, QAnyStringView rhs, Qt::CaseSensitivity cs=Qt::CaseSensitive) noexcept
 Compares the string view lhs with the string view rhs and returns a negative integer if lhs is less than rhs, a positive integer if it is greater than rhs, and zero if they are equal.
 
static Q_CORE_EXPORT bool equal (QAnyStringView lhs, QAnyStringView rhs) noexcept
 

Static Public Attributes

static constexpr bool detects_US_ASCII_at_compile_time
 

Friends

class ::tst_QAnyStringView
 
bool comparesEqual (const QAnyStringView &lhs, const QAnyStringView &rhs) noexcept
 
Qt::strong_ordering compareThreeWay (const QAnyStringView &lhs, const QAnyStringView &rhs) noexcept
 
Q_CORE_EXPORT friend QDebug operator<< (QDebug d, QAnyStringView s)
 

Related Symbols

(Note that these are not member symbols.)

QString convertToQString (QAnyStringView string)
 

Detailed Description

\inmodule QtCore

Since
6.0

The QAnyStringView class provides a unified view on Latin-1, UTF-8, or UTF-16 strings with a read-only subset of the QString API. \reentrant

\compares strong \compareswith strong char16_t QChar {const char16_t *} {const char *} \ QByteArray QByteArrayView QString QStringView QUtf8StringView \ QLatin1StringView \endcompareswith

A QAnyStringView references a contiguous portion of a string it does not own. It acts as an interface type to all kinds of strings, without the need to construct a QString first.

Unlike QStringView and QUtf8StringView, QAnyStringView can hold strings of any of the following encodings: UTF-8, UTF-16, and Latin-1. The latter is supported because Latin-1, unlike UTF-8, can be efficiently compared to UTF-16 data: a length mismatch already means the strings cannot be equal. This is not true for UTF-8/UTF-16 comparisons, because UTF-8 is a variable-length encoding.

The string may be represented as an array (or an array-compatible data-structure such as QString, std::basic_string, etc.) of char, char8_t, QChar, ushort, char16_t or (on platforms, such as Windows, where it is a 16-bit type) wchar_t.

QAnyStringView is designed as an interface type; its main use-case is as a function parameter type. When QAnyStringViews are used as automatic variables or data members, care must be taken to ensure that the referenced string data (for example, owned by a QString) outlives the QAnyStringView on all code paths, lest the string view ends up referencing deleted data.

When used as an interface type, QAnyStringView allows a single function to accept a wide variety of string data sources. One function accepting QAnyStringView thus replaces five function overloads (taking QString, {(const QChar*, qsizetype)}, QUtf8StringView, QLatin1StringView (but see above), and QChar), while at the same time enabling even more string data sources to be passed to the function, such as {u8"Hello World"}, a char8_t string literal.

Like elsewhere in Qt, QAnyStringView assumes char data is encoded in UTF-8, unless it is presented as a QLatin1StringView.

Since Qt 6.4, however, UTF-8 string literals that are pure US-ASCII are automatically stored as Latin-1. This is a compile-time check with no runtime overhead. The feature requires compiling in C++20, or with a recent GCC.

QAnyStringViews should be passed by value, not by reference-to-const:

void myfun1(QAnyStringView sv); // preferred
void myfun2(const QAnyStringView &sv); // compiles and works, but slower

QAnyStringView can also be used as the return value of a function, but this is not recommended. QUtf8StringView or QStringView are better suited as function return values. If you call a function returning QAnyStringView, take extra care to not keep the QAnyStringView around longer than the function promises to keep the referenced string data alive. If in doubt, obtain a strong reference to the data by calling toString() to convert the QAnyStringView into a QString.

QAnyStringView is a {Literal Type}.

Definition at line 32 of file qanystringview.h.

Member Typedef Documentation

◆ difference_type

Alias for {std::ptrdiff_t}.

Provided for compatibility with the STL.

Definition at line 35 of file qanystringview.h.

◆ size_type

Alias for qsizetype.

Provided for compatibility with the STL.

Definition at line 36 of file qanystringview.h.

Constructor & Destructor Documentation

◆ QAnyStringView() [1/16]

QAnyStringView::QAnyStringView ( )
inlineconstexprnoexcept

Constructs a null string view.

See also
isNull()

Definition at line 150 of file qanystringview.h.

Referenced by fromArray(), and mid().

+ Here is the caller graph for this function:

◆ QAnyStringView() [2/16]

QAnyStringView::QAnyStringView ( std::nullptr_t )
inlineconstexprnoexcept

Constructs a null string view.

See also
isNull()

Definition at line 152 of file qanystringview.h.

◆ QAnyStringView() [3/16]

template<typename Char , if_compatible_char< Char > = true>
template< typename Char, QAnyStringView::if_compatible_char< Char >=true > QAnyStringView::QAnyStringView ( const Char * str,
qsizetype len )
inlineconstexpr

Constructs a string view on str with length len.

The range {[str,len)} must remain valid for the lifetime of this string view object.

Passing \nullptr as str is safe if len is 0, too, and results in a null string view.

The behavior is undefined if len is negative or, when positive, if str is \nullptr.

This constructor only participates in overload resolution if Char is a compatible character type.

See also
isNull(), {Compatible Character Types}

Definition at line 156 of file qanystringview.h.

◆ QAnyStringView() [4/16]

template<typename Char , if_compatible_char< Char > = true>
template< typename Char, QAnyStringView::if_compatible_char< Char >=true > QAnyStringView::QAnyStringView ( const Char * first,
const Char * last )
inlineconstexpr

Constructs a string view on first with length (last - first).

The range {[first,last)} must remain valid for the lifetime of this string view object.

Passing \nullptr as first is safe if last is \nullptr, too, and results in a null string view.

The behavior is undefined if last precedes first, or first is \nullptr and last is not.

This constructor only participates in overload resolution if Char is a compatible character type.

See also
isNull(), {Compatible Character Types}

Definition at line 162 of file qanystringview.h.

◆ QAnyStringView() [5/16]

template<typename Pointer , if_compatible_pointer< Pointer > = true>
constexpr QAnyStringView::QAnyStringView ( const Pointer & str)
inlineconstexprnoexcept

Definition at line 174 of file qanystringview.h.

References str.

◆ QAnyStringView() [6/16]

QAnyStringView::QAnyStringView ( const QByteArray & str)
inlinenoexcept

Constructs a string view on str.

The data in str is interpreted as UTF-8.

{str.data()} must remain valid for the lifetime of this string view object.

The string view will be null if and only if {str.isNull()}.

Definition at line 1213 of file qstring.h.

References QString::data(), QString::isNull(), nullptr, QString::size(), and str.

+ Here is the call graph for this function:

◆ QAnyStringView() [7/16]

QAnyStringView::QAnyStringView ( const QString & str)
inlinenoexcept

Constructs a string view on str.

{str.data()} must remain valid for the lifetime of this string view object.

The string view will be null if and only if {str.isNull()}.

Definition at line 1215 of file qstring.h.

References QString::data(), QString::isNull(), nullptr, QString::size(), and str.

+ Here is the call graph for this function:

◆ QAnyStringView() [8/16]

constexpr QAnyStringView::QAnyStringView ( QLatin1StringView str)
inlineconstexprnoexcept

Definition at line 93 of file qstring.h.

References QString::data(), and str.

+ Here is the call graph for this function:

◆ QAnyStringView() [9/16]

template<typename Container , if_compatible_container< Container > = true>
constexpr Q_ALWAYS_INLINE QAnyStringView::QAnyStringView ( const Container & c)
inlineconstexprnoexcept

Definition at line 184 of file qanystringview.h.

◆ QAnyStringView() [10/16]

template<typename Container , if_convertible_to< QString, Container > = true>
constexpr QAnyStringView::QAnyStringView ( Container && c,
QtPrivate::wrapped_t< Container, QString > && capacity = {} )
inlineconstexpr

Definition at line 188 of file qanystringview.h.

◆ QAnyStringView() [11/16]

template<typename Container , if_convertible_to< QByteArray, Container > = true>
constexpr QAnyStringView::QAnyStringView ( Container && c,
QtPrivate::wrapped_t< Container, QByteArray > && capacity = {} )
inlineconstexpr

Definition at line 193 of file qanystringview.h.

◆ QAnyStringView() [12/16]

template<typename Char , if_compatible_char< Char > = true>
constexpr QAnyStringView::QAnyStringView ( const Char & c)
inlineconstexprnoexcept

Definition at line 198 of file qanystringview.h.

◆ QAnyStringView() [13/16]

constexpr QAnyStringView::QAnyStringView ( const QChar & c)
inlineconstexprnoexcept

Definition at line 200 of file qanystringview.h.

◆ QAnyStringView() [14/16]

template<typename Char , typename Container = decltype(QChar::fromUcs4(U'x')), std::enable_if_t< std::is_same_v< Char, char32_t >, bool > = true>
constexpr QAnyStringView::QAnyStringView ( Char c,
Container && capacity = {} )
inlineconstexpr

Definition at line 205 of file qanystringview.h.

◆ QAnyStringView() [15/16]

constexpr QAnyStringView::QAnyStringView ( QStringView v)
inlineconstexprnoexcept

Definition at line 208 of file qanystringview.h.

◆ QAnyStringView() [16/16]

template<bool UseChar8T>
constexpr QAnyStringView::QAnyStringView ( QBasicUtf8StringView< UseChar8T > v)
inlineconstexprnoexcept

Definition at line 212 of file qanystringview.h.

Member Function Documentation

◆ back()

constexpr QChar QAnyStringView::back ( ) const
constexpr

Returns the last character in the string view.

This function is provided for STL compatibility.

Warning
Calling this function on an empty string view constitutes undefined behavior.
See also
front(), {Sizes and Sub-Strings}

Definition at line 122 of file qstring.h.

References visit().

Referenced by ResolvedTypeAlias::ResolvedTypeAlias().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ chop()

QAnyStringView::chop ( qsizetype n)
inlineconstexpr
Since
6.5

Truncates this string view by n code points.

Same as {*this = first(size() - n)}.

Note
The behavior is undefined when n < 0 or n > size().
See also
sliced(), first(), last(), chopped(), truncate(), {Sizes and Sub-Strings}

Definition at line 258 of file qanystringview.h.

◆ chopped()

QAnyStringView::chopped ( qsizetype n) const
inlineconstexpr
\fn QAnyStringView::sliced(qsizetype pos, qsizetype n) const
\since 6.5

Returns a string view containing \a n code points of this string view,
starting at position \a pos.

! [UB-sliced-index-length]

Note
The behavior is undefined when pos < 0, n < 0, or pos + n > size(). ! [UB-sliced-index-length]
\sa first(), last(), chopped(), chop(), truncate(), {Sizes and Sub-Strings}
\fn QAnyStringView::sliced(qsizetype pos) const
\since 6.5

Returns a string view starting at position \a pos in this object,
and extending to its end.

! [UB-sliced-index-only]

Note
The behavior is undefined when pos < 0 or pos > size(). ! [UB-sliced-index-only]
\sa first(), last(), chopped(), chop(), truncate(), {Sizes and Sub-Strings}
Since
6.5

Returns the substring of length size() - n starting at the beginning of this object.

Same as {first(size() - n)}.

Note
The behavior is undefined when n < 0 or n > size().
See also
sliced(), first(), last(), chop(), truncate(), {Sizes and Sub-Strings}

Definition at line 253 of file qanystringview.h.

References sliced().

Referenced by ResolvedTypeAlias::ResolvedTypeAlias().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ compare()

int QAnyStringView::compare ( QAnyStringView lhs,
QAnyStringView rhs,
Qt::CaseSensitivity cs = Qt::CaseSensitive )
staticnoexcept

Compares the string view lhs with the string view rhs and returns a negative integer if lhs is less than rhs, a positive integer if it is greater than rhs, and zero if they are equal.

If cs is Qt::CaseSensitive (the default), the comparison is case sensitive; otherwise the comparison is case-insensitive.

See also
operator==(), operator<(), operator>()

Definition at line 1599 of file qstring.cpp.

References QtPrivate::compareStrings().

Referenced by QNetworkHeadersPrivate::findRawHeader(), and QCalendarRegistryCaseInsensitiveAnyStringViewLessThan::operator()().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ data()

const void * QAnyStringView::data ( ) const
inlineconstexprnoexcept

Returns a const pointer to the first character in the string view.

Note
The character array represented by the return value is not null-terminated.
See also
size_bytes()

Definition at line 266 of file qanystringview.h.

References m_data.

◆ empty()

bool QAnyStringView::empty ( ) const
inlineconstexprnoexcept

Returns whether this string view is empty - that is, whether {size() == 0}.

This function is provided for STL compatibility.

See also
isEmpty(), isNull(), size()

Definition at line 284 of file qanystringview.h.

Referenced by isEmpty().

+ Here is the caller graph for this function:

◆ equal()

bool QAnyStringView::equal ( QAnyStringView lhs,
QAnyStringView rhs )
staticnoexcept

Definition at line 1448 of file qstring.cpp.

References QtPrivate::equalStrings().

+ Here is the call graph for this function:

◆ first()

QAnyStringView::first ( qsizetype n) const
inlineconstexpr
Since
6.5

Returns a string view that contains the first n code points of this string view.

Note
The behavior is undefined when n < 0 or n > size().
See also
last(), sliced(), chopped(), chop(), truncate(), {Sizes and Sub-Strings}

Definition at line 249 of file qanystringview.h.

References sliced().

Referenced by QQmlJSRegisterContent::descriptiveName().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fromArray()

template<typename Char , size_t Size, if_compatible_char< Char > = true>
static constexpr QAnyStringView QAnyStringView::fromArray ( const Char(&) string[Size])
inlinestaticconstexprnoexcept

Definition at line 216 of file qanystringview.h.

References QAnyStringView().

+ Here is the call graph for this function:

◆ front()

constexpr QChar QAnyStringView::front ( ) const
constexpr

Returns the first character in the string view.

This function is provided for STL compatibility.

Warning
Calling this function on an empty string view constitutes undefined behavior.
See also
back(), {Sizes and Sub-Strings}

Definition at line 118 of file qstring.h.

References visit().

+ Here is the call graph for this function:

◆ isEmpty()

bool QAnyStringView::isEmpty ( ) const
inlineconstexprnoexcept

Returns whether this string view is empty - that is, whether {size() == 0}.

This function is provided for compatibility with other Qt containers.

See also
empty(), isNull(), size()

Definition at line 292 of file qanystringview.h.

References empty().

Referenced by QmlTypesClassDescription::collect(), and QQmlSA::PassManagerPrivate::registerPropertyPass().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isNull()

bool QAnyStringView::isNull ( ) const
inlineconstexprnoexcept

Returns whether this string view is null - that is, whether {data() == nullptr}.

This functions is provided for compatibility with other Qt containers.

See also
empty(), isEmpty(), size()

Definition at line 291 of file qanystringview.h.

References m_data.

◆ last()

QAnyStringView::last ( qsizetype n) const
inlineconstexpr
Since
6.5

Returns a string view that contains the last n code points of this string view.

Note
The behavior is undefined when n < 0 or n > size().
See also
first(), sliced(), chopped(), chop(), truncate(), {Sizes and Sub-Strings}

Definition at line 251 of file qanystringview.h.

References sliced().

+ Here is the call graph for this function:

◆ left()

QAnyStringView::left ( qsizetype n) const
inlineconstexpr
Since
6.5
Deprecated
Use first() instead in new code.

Returns the substring of length n starting at position 0 in this object.

The entire string view is returned if n is greater than or equal to size(), or less than zero.

See also
first(), last(), sliced(), chopped(), chop(), truncate(), {Sizes and Sub-Strings}

Definition at line 231 of file qanystringview.h.

References sliced().

+ Here is the call graph for this function:

◆ length()

QAnyStringView::length ( ) const
inlineconstexprnoexcept

Same as size().

This function is provided for compatibility with other Qt containers.

See also
size()

Definition at line 293 of file qanystringview.h.

◆ mid()

QAnyStringView::mid ( qsizetype pos,
qsizetype n = -1 ) const
inlineconstexpr
Since
6.5

Returns the substring of length n starting at position pos in this object.

Deprecated
Use sliced() instead in new code.

Returns an empty string view if n exceeds the length of the string view. If there are less than n code points available in the string view starting at pos, or if n is negative (default), the function returns all code points that are available from pos.

See also
first(), last(), sliced(), chopped(), chop(), truncate(), {Sizes and Sub-Strings}

Definition at line 224 of file qanystringview.h.

References QAnyStringView(), pos, and sliced().

+ Here is the call graph for this function:

◆ right()

QAnyStringView::right ( qsizetype n) const
inlineconstexpr
Since
6.5
Deprecated
Use last() instead in new code.

Returns the substring of length n starting at position size() - n in this object.

The entire string view is returned if n is greater than or equal to size(), or less than zero.

See also
first(), last(), sliced(), chopped(), chop(), truncate(), {Sizes and Sub-Strings}

Definition at line 238 of file qanystringview.h.

References sliced().

+ Here is the call graph for this function:

◆ size()

qsizetype QAnyStringView::size ( ) const
inlineconstexprnoexcept

Returns the size of this string view, in the encoding's code points.

See also
empty(), isEmpty(), isNull(), size_bytes(), {Sizes and Sub-Strings}

Definition at line 264 of file qanystringview.h.

Referenced by Interface::Interface().

+ Here is the caller graph for this function:

◆ size_bytes()

QAnyStringView::size_bytes ( ) const
inlineconstexprnoexcept

Returns the size of this string view, but in bytes, not code-points.

You can use this function together with data() for hashing or serialization.

This function is provided for STL compatibility.

See also
size(), data()

Definition at line 285 of file qanystringview.h.

◆ sliced() [1/2]

constexpr QAnyStringView QAnyStringView::sliced ( qsizetype pos) const
inlineconstexpr

Definition at line 245 of file qanystringview.h.

References pos.

Referenced by chopped(), first(), last(), left(), mid(), and right().

+ Here is the caller graph for this function:

◆ sliced() [2/2]

constexpr QAnyStringView QAnyStringView::sliced ( qsizetype pos,
qsizetype n ) const
inlineconstexpr

Definition at line 247 of file qanystringview.h.

References pos.

◆ toString()

QString QAnyStringView::toString ( ) const
inline

Returns a deep copy of this string view's data as a QString.

The return value will be a null QString if and only if this string view is null.

Definition at line 1218 of file qstring.h.

References QtPrivate::convertToQString().

Referenced by ForbiddenChildrenPropertyValidatorPass::addWarning(), QQmlComponentPrivate::completeLoadFromModule(), QQmlJSLogger::defaultCategories(), QQmlSA::PassManager::hasImportedModule(), QtPrivate::BindableWarnings::printUnsuitableBindableWarning(), and QQmlSA::PassManagerPrivate::registerPropertyPass().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ truncate()

QAnyStringView::truncate ( qsizetype n)
inlineconstexpr
Since
6.5

Truncates this string view to n code points.

Same as {*this = first(n)}.

Note
The behavior is undefined when n < 0 or n > size().
See also
sliced(), first(), last(), chopped(), chop(), {Sizes and Sub-Strings}

Definition at line 256 of file qanystringview.h.

◆ visit() [1/2]

template<typename Visitor >
decltype(auto) template< typename Visitor > QAnyStringView::visit ( Visitor && v) const
inlineconstexpr

Calls v with either a QUtf8StringView, QLatin1String, or QStringView, depending on the encoding of the string data this string-view references.

This is how most functions taking QAnyStringView fork off into per-encoding functions:

void processImpl(QLatin1String s) { ~~~ }
void processImpl(QUtf8StringView s) { ~~~ }
void processImpl(QStringView s) { ~~~ }
void process(QAnyStringView s)
{
s.visit([](auto s) { processImpl(s); });
}
\inmodule QtCore
\inmodule QtCore
Definition qstringview.h:78
GLdouble s
[6]
Definition qopenglext.h:235

Here, we're reusing the same name, s, for both the QAnyStringView object, as well as the lambda's parameter. This is idiomatic code and helps track the identity of the objects through visit() calls, for example in more complex situations such as

{
// assuming operator==(QAnyStringView, QAnyStringView) didn't, yet, exist:
return lhs.visit([rhs](auto lhs) {
rhs.visit([lhs](auto rhs) {
return lhs == rhs;
});
});
}
static Q_CORE_EXPORT bool equal(QAnyStringView lhs, QAnyStringView rhs) noexcept
Definition qstring.cpp:1448
constexpr decltype(auto) visit(Visitor &&v) const
Calls v with either a QUtf8StringView, QLatin1String, or QStringView, depending on the encoding of th...

visit() requires that all lambda instantiations have the same return type. If they differ, you get a compile error, even if there is a common type. To fix, you can use explicit return types on the lambda, or cast in the return statements:

// wrong:
{
return input.visit([](auto input) { // ERROR: lambdas return different types
return input.sliced(0, input.size() / 2);
});
}
// correct:
{
return input.visit([](auto input) -> QAnyStringView { // OK, explicit return type
return input.sliced(0, input.size() / 2);
});
}
// also correct:
{
return input.visit([](auto input) {
return QAnyStringView(input.sliced(0, input.size() / 2)); // OK, cast to common type
});
}
constexpr QAnyStringView() noexcept
Constructs a null string view.
constexpr QAnyStringView sliced(qsizetype pos) const
GLenum GLenum GLenum input

Referenced by back(), front(), and QAnyStringViewUtils::trimmed().

+ Here is the caller graph for this function:

◆ visit() [2/2]

template<typename Visitor >
constexpr decltype(auto) QAnyStringView::visit ( Visitor && v) const
constexpr

Definition at line 104 of file qstring.h.

Friends And Related Symbol Documentation

◆ ::tst_QAnyStringView

friend class ::tst_QAnyStringView
friend

Definition at line 338 of file qanystringview.h.

◆ comparesEqual

bool comparesEqual ( const QAnyStringView & lhs,
const QAnyStringView & rhs )
friend

Definition at line 297 of file qanystringview.h.

◆ compareThreeWay

Qt::strong_ordering compareThreeWay ( const QAnyStringView & lhs,
const QAnyStringView & rhs )
friend

Definition at line 300 of file qanystringview.h.

◆ convertToQString()

QString convertToQString ( QAnyStringView string)
related
Since
6.0

Returns a UTF-16 representation of string as a QString.

See also
QString::toLatin1(), QStringView::toLatin1(), QtPrivate::convertToUtf8(), QtPrivate::convertToLocal8Bit(), QtPrivate::convertToUcs4()

Definition at line 5620 of file qstring.cpp.

◆ operator<<

Q_CORE_EXPORT friend QDebug operator<< ( QDebug d,
QAnyStringView s )
friend

Definition at line 634 of file qanystringview.cpp.

Member Data Documentation

◆ detects_US_ASCII_at_compile_time

constexpr bool QAnyStringView::detects_US_ASCII_at_compile_time
inlinestaticconstexpr
Initial value:
=
false

Definition at line 271 of file qanystringview.h.

◆ m_data

const void* QAnyStringView::m_data

Definition at line 333 of file qanystringview.h.

Referenced by data(), and isNull().

◆ m_data_utf16

const char16_t* QAnyStringView::m_data_utf16

Definition at line 335 of file qanystringview.h.

◆ m_data_utf8

const char* QAnyStringView::m_data_utf8

Definition at line 334 of file qanystringview.h.


The documentation for this class was generated from the following files: