Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
|
\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 |
template<typename Char , if_convertible_to< QChar, Char > = true> | |
constexpr | QAnyStringView (Char ch, QCharContainer &&capacity=QCharContainer()) 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={}) noexcept |
constexpr | QAnyStringView (QStringView v) noexcept |
template<bool UseChar8T> | |
constexpr | QAnyStringView (QBasicUtf8StringView< UseChar8T > v) noexcept |
template<typename Visitor > | |
constexpr decltype(auto) | visit (Visitor &&v) const |
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 QAnyStringView & | slice (qsizetype pos) |
constexpr QAnyStringView & | slice (qsizetype pos, qsizetype n) |
constexpr void | truncate (qsizetype n) |
constexpr void | chop (qsizetype n) |
template<typename... Args> | |
QString | arg (Args &&...args) const |
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 qsizetype | max_size () const noexcept |
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) |
\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
\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:
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 30 of file qanystringview.h.
Alias for {std::ptrdiff_t}.
Provided for compatibility with the STL.
Definition at line 33 of file qanystringview.h.
Alias for qsizetype.
Provided for compatibility with the STL.
Definition at line 34 of file qanystringview.h.
|
inlineconstexprnoexcept |
Constructs a null string view.
Definition at line 166 of file qanystringview.h.
References m_data.
Referenced by QAnyStringView().
|
inlineconstexprnoexcept |
Constructs a null string view.
Definition at line 168 of file qanystringview.h.
References QAnyStringView().
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 172 of file qanystringview.h.
References m_data.
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 178 of file qanystringview.h.
|
inlineconstexprnoexcept |
Definition at line 190 of file qanystringview.h.
|
inlinenoexcept |
|
inlinenoexcept |
|
inlineconstexprnoexcept |
|
inlineconstexprnoexcept |
Definition at line 200 of file qanystringview.h.
|
inlineconstexpr |
Definition at line 204 of file qanystringview.h.
|
inlineconstexpr |
Definition at line 209 of file qanystringview.h.
|
inlineconstexprnoexcept |
Definition at line 214 of file qanystringview.h.
|
inlineconstexprnoexcept |
Definition at line 217 of file qanystringview.h.
|
inlineconstexprnoexcept |
Definition at line 221 of file qanystringview.h.
|
inlineconstexprnoexcept |
Definition at line 224 of file qanystringview.h.
|
inlineconstexprnoexcept |
Definition at line 228 of file qanystringview.h.
|
inlinenodiscard |
|
nodiscardconstexpr |
Truncates this string view by n code points.
Same as {*this = first(size() - n)}.
Definition at line 279 of file qanystringview.h.
|
inlinenodiscardconstexpr |
Returns the substring of length size() - n starting at the beginning of this object.
Same as {first(size() - n)}.
Definition at line 269 of file qanystringview.h.
|
staticnodiscardnoexcept |
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.
Definition at line 1593 of file qstring.cpp.
|
inlinenodiscardconstexprnoexcept |
Returns a const pointer to the first character in the string view.
Definition at line 289 of file qanystringview.h.
|
inlinenodiscardconstexprnoexcept |
|
staticnodiscardnoexcept |
Definition at line 1440 of file qstring.cpp.
|
inlinenodiscardconstexpr |
Returns a string view that contains the first n code points of this string view.
Definition at line 265 of file qanystringview.h.
|
inlinestaticnodiscardconstexprnoexcept |
Definition at line 232 of file qanystringview.h.
|
nodiscardconstexpr |
|
inlinenodiscardconstexprnoexcept |
|
inlinenodiscardconstexprnoexcept |
|
inlinenodiscardconstexpr |
Returns a string view that contains the last n code points of this string view.
Definition at line 267 of file qanystringview.h.
|
inlinenodiscardconstexpr |
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.
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 247 of file qanystringview.h.
Referenced by testing.tools.safetynet_image.ImageLocations::Left().
|
inlinenodiscardconstexprnoexcept |
Same as size().
This function is provided for compatibility with other Qt containers.
Definition at line 322 of file qanystringview.h.
Referenced by testing.tools.encode_pdf_filter._PngIdatPdfStream::write().
|
inlinenodiscardconstexprnoexcept |
This function is provided for STL compatibility.
It returns the maximum number of elements that the string view can theoretically represent. In practice, the number can be much smaller, limited by the amount of memory available to the system.
Definition at line 311 of file qanystringview.h.
|
inlinenodiscardconstexpr |
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.
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 240 of file qanystringview.h.
|
inlinenodiscardconstexpr |
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.
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 254 of file qanystringview.h.
Referenced by testing.tools.safetynet_image.ImageLocations::Right().
|
inlinenodiscardconstexprnoexcept |
Returns the size of this string view, in the encoding's code points.
Definition at line 287 of file qanystringview.h.
|
inlinenodiscardconstexprnoexcept |
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 308 of file qanystringview.h.
|
inlineconstexpr |
Modifies this string view to start at position pos, extending to its end.
UB-sliced-index-only
Definition at line 272 of file qanystringview.h.
|
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]
\sa first(), last(), chopped(), chop(), truncate(), slice(), {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]
\sa first(), last(), chopped(), chop(), truncate(), slice(), {Sizes and Sub-Strings}
Modifies this string view to start at position pos, extending for n code points.
UB-sliced-index-length
Definition at line 274 of file qanystringview.h.
|
inlinenodiscardconstexpr |
Definition at line 261 of file qanystringview.h.
|
inlinenodiscardconstexpr |
Definition at line 263 of file qanystringview.h.
|
inlinenodiscard |
|
inlineconstexpr |
|
constexpr |
|
friend |
Definition at line 367 of file qanystringview.h.
|
friend |
Definition at line 326 of file qanystringview.h.
|
friend |
Definition at line 329 of file qanystringview.h.
|
related |
Returns a UTF-16 representation of string as a QString.
Definition at line 5635 of file qstring.cpp.
|
friend |
Definition at line 674 of file qanystringview.cpp.
|
inlinestaticconstexpr |
Definition at line 294 of file qanystringview.h.
const void* QAnyStringView::m_data |
Definition at line 362 of file qanystringview.h.
Referenced by QAnyStringView(), and QAnyStringView().
const char16_t* QAnyStringView::m_data_utf16 |
Definition at line 364 of file qanystringview.h.
const char* QAnyStringView::m_data_utf8 |
Definition at line 363 of file qanystringview.h.