![]() |
Qt 6.x
The Qt SDK
|
\inmodule QtCore More...
#include <qanystringview.h>
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 void * | data () 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 |
Returns an integer that compares to zero as lhs compares to rhs. | |
static Q_CORE_EXPORT bool | equal (QAnyStringView lhs, QAnyStringView rhs) noexcept |
Friends | |
class | ::tst_QAnyStringView |
bool | operator== (QAnyStringView lhs, QAnyStringView rhs) noexcept |
bool | operator!= (QAnyStringView lhs, QAnyStringView rhs) noexcept |
bool | operator<= (QAnyStringView lhs, QAnyStringView rhs) noexcept |
bool | operator>= (QAnyStringView lhs, QAnyStringView rhs) noexcept |
bool | operator< (QAnyStringView lhs, QAnyStringView rhs) noexcept |
bool | operator> (QAnyStringView lhs, QAnyStringView rhs) noexcept |
Operators that compare lhs to rhs. | |
Q_CORE_EXPORT friend QDebug | operator<< (QDebug d, QAnyStringView s) |
Related Symbols | |
(Note that these are not member symbols.) | |
QString | convertToQString (QAnyStringView string) |
\inmodule QtCore
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
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:
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 31 of file qanystringview.h.
Alias for {std::ptrdiff_t}.
Provided for compatibility with the STL.
Definition at line 34 of file qanystringview.h.
Alias for qsizetype.
Provided for compatibility with the STL.
Definition at line 35 of file qanystringview.h.
|
inlineconstexprnoexcept |
Constructs a null string view.
Definition at line 157 of file qanystringview.h.
Referenced by fromArray(), and mid().
|
inlineconstexprnoexcept |
|
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.
Definition at line 163 of file qanystringview.h.
|
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.
Definition at line 169 of file qanystringview.h.
|
inlineconstexprnoexcept |
Definition at line 181 of file qanystringview.h.
References 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 1066 of file qstring.h.
References QString::data(), QString::isNull(), nullptr, QString::size(), and 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 1068 of file qstring.h.
References QString::data(), QString::isNull(), nullptr, QString::size(), and str.
|
inlineconstexprnoexcept |
Definition at line 91 of file qstring.h.
References QString::data(), and str.
|
inlineconstexprnoexcept |
Definition at line 191 of file qanystringview.h.
|
inlineconstexpr |
Definition at line 195 of file qanystringview.h.
|
inlineconstexpr |
Definition at line 200 of file qanystringview.h.
|
inlineconstexprnoexcept |
Definition at line 205 of file qanystringview.h.
|
inlineconstexprnoexcept |
Definition at line 207 of file qanystringview.h.
|
inlineconstexpr |
Definition at line 212 of file qanystringview.h.
|
inlineconstexprnoexcept |
Definition at line 215 of file qanystringview.h.
|
inlineconstexprnoexcept |
Definition at line 219 of file qanystringview.h.
|
constexpr |
Returns the last character in the string view.
This function is provided for STL compatibility.
Definition at line 120 of file qstring.h.
References visit().
|
inlineconstexpr |
Truncates this string view by n code points.
Same as {*this = first(size() - n)}.
Definition at line 265 of file qanystringview.h.
|
inlineconstexpr |
Returns the substring of length size() - n starting at the beginning of this object.
Same as {first(size() - n)}.
Definition at line 260 of file qanystringview.h.
References sliced().
|
staticnoexcept |
Returns an integer that compares to zero as lhs compares to rhs.
If cs is Qt::CaseSensitive (the default), the comparison is case sensitive; otherwise the comparison is case-insensitive.
Definition at line 1601 of file qstring.cpp.
References QtPrivate::compareStrings().
Referenced by QCalendarRegistryCaseInsensitiveAnyStringViewLessThan::operator()().
|
inlineconstexprnoexcept |
Returns a const pointer to the first character in the string view.
Definition at line 273 of file qanystringview.h.
References m_data.
|
inlineconstexprnoexcept |
|
staticnoexcept |
Definition at line 1450 of file qstring.cpp.
References QtPrivate::equalStrings().
|
inlineconstexpr |
Returns a string view that contains the first n code points of this string view.
Definition at line 256 of file qanystringview.h.
References sliced().
Referenced by QAnyStringViewUtils::startsWith().
|
inlinestaticconstexprnoexcept |
Definition at line 223 of file qanystringview.h.
References QAnyStringView(), and Size.
|
constexpr |
Returns the first character in the string view.
This function is provided for STL compatibility.
Definition at line 116 of file qstring.h.
References visit().
Referenced by QmlTypesClassDescription::collect().
|
inlineconstexprnoexcept |
Returns whether this string view is empty - that is, whether {size() == 0}.
This function is provided for compatibility with other Qt containers.
Definition at line 291 of file qanystringview.h.
References empty().
Referenced by QmlTypesClassDescription::collect(), QQmlSA::PassManagerPrivate::registerPropertyPass(), and QmlTypeRegistrar::write().
|
inlineconstexprnoexcept |
|
inlineconstexpr |
Returns a string view that contains the last n code points of this string view.
Definition at line 258 of file qanystringview.h.
References sliced().
Referenced by QAnyStringViewUtils::endsWith().
|
inlineconstexpr |
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.
Definition at line 238 of file qanystringview.h.
References sliced().
|
inlineconstexprnoexcept |
Same as size().
This function is provided for compatibility with other Qt containers.
Definition at line 292 of file qanystringview.h.
Referenced by QAnyStringViewUtils::endsWith(), and QAnyStringViewUtils::startsWith().
Returns the substring of length n starting at position pos in this object.
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.
Definition at line 231 of file qanystringview.h.
References QAnyStringView(), pos, and sliced().
|
inlineconstexpr |
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.
Definition at line 245 of file qanystringview.h.
References sliced().
|
inlineconstexprnoexcept |
Returns the size of this string view, in the encoding's code points.
Definition at line 271 of file qanystringview.h.
|
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.
Definition at line 284 of file qanystringview.h.
|
inlineconstexpr |
Returns a string view starting at position pos in this object, and extending to its end.
Definition at line 252 of file qanystringview.h.
References pos.
Referenced by chopped(), first(), last(), left(), mid(), and right().
Returns a string view containing n code points of this string view, starting at position pos.
Definition at line 254 of file qanystringview.h.
References pos.
|
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 1071 of file qstring.h.
References QtPrivate::convertToQString().
Referenced by ForbiddenChildrenPropertyValidatorPass::addWarning(), QmlTypesClassDescription::collect(), QQmlJSLogger::defaultCategories(), QQmlSA::GenericPass::emitWarning(), QQmlSA::GenericPass::emitWarning(), QQmlSA::PassManager::hasImportedModule(), QQmlComponent::loadFromModule(), QtPrivate::BindableWarnings::printUnsuitableBindableWarning(), QQmlSA::PassManagerPrivate::registerPropertyPass(), and QQmlSA::GenericPass::resolveType().
|
inlineconstexpr |
|
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:
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
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:
Referenced by back(), QAnyStringViewUtils::contains(), front(), and QAnyStringViewUtils::trimmed().
|
constexpr |
|
friend |
Definition at line 345 of file qanystringview.h.
|
related |
Returns a UTF-16 representation of string as a QString.
Definition at line 5459 of file qstring.cpp.
|
friend |
Definition at line 298 of file qanystringview.h.
|
friend |
Definition at line 309 of file qanystringview.h.
|
friend |
Outputs s to debug stream d.
If {d.quotedString()} is
true
, indicates which encoding the string is in. If you just want the string data, use visit() like this:
Definition at line 623 of file qanystringview.cpp.
|
friend |
Definition at line 305 of file qanystringview.h.
|
friend |
Definition at line 296 of file qanystringview.h.
|
friend |
Operators that compare lhs to rhs.
Definition at line 311 of file qanystringview.h.
|
friend |
Definition at line 307 of file qanystringview.h.
const void* QAnyStringView::m_data |
Definition at line 340 of file qanystringview.h.
const char16_t* QAnyStringView::m_data_utf16 |
Definition at line 342 of file qanystringview.h.
const char* QAnyStringView::m_data_utf8 |
Definition at line 341 of file qanystringview.h.