QStringView Class▲
-
Header: QStringView
-
Since: Qt 5.10
-
qmake: QT += core
-
Group: QStringView is part of tools, string-processing
Detailed Description▲
A QStringView references a contiguous portion of a UTF-16 string it does not own. It acts as an interface type to all kinds of UTF-16 string, without the need to construct a QString first.
The UTF-16 string may be represented as an array (or an array-compatible data-structure such as QString, std::basic_string, etc.) of QChar, ushort, char16_t (on compilers that support C++11 Unicode strings) or (on platforms, such as Windows, where it is a 16-bit type) wchar_t.
QStringView is designed as an interface type; its main use-case is as a function parameter type. When QStringViews 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 QStringView on all code paths, lest the string view ends up referencing deleted data.
When used as an interface type, QStringView allows a single function to accept a wide variety of UTF-16 string data sources. One function accepting QStringView thus replaces three function overloads (taking QString, QStringRef, and (const QChar*, int)), while at the same time enabling even more string data sources to be passed to the function, such as u"Hello World", a char16_t string literal.
QStringViews should be passed by value, not by reference-to-const:
void
myfun1(QStringView sv); // preferred
void
myfun2(const
QStringView &
amp;sv); // compiles and works, but slower
If you want to give your users maximum freedom in what strings they can pass to your function, accompany the QStringView overload with overloads for
-
QChar: this overload can delegate to the QStringView version:
Sélectionnezvoid
fun(QChar ch){
fun(QStringView(&
amp;ch,1
));}
even though, for technical reasons, QStringView cannot provide a QChar constructor by itself.
-
QString: if you store an unmodified copy of the string and thus would like to take advantage of QString's implicit sharing.
-
QLatin1String: if you can implement the function without converting the QLatin1String to UTF-16 first; users expect a function overloaded on QLatin1String to perform strictly less memory allocations than the semantically equivalent call of the QStringView version, involving construction of a QString from the QLatin1String.
QStringView can also be used as the return value of a function. If you call a function returning QStringView, take extra care to not keep the QStringView 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 QStringView into a QString.
QStringView is a Literal Type, but since it stores data as char16_t, iteration is not constexpr (casts from const char16_t* to const QChar*, which is not allowed in constexpr functions). You can use an indexed loop and/or utf16() in constexpr contexts instead.
We strongly discourage the use of QList<QStringView>, because QList is a very inefficient container for QStringViews (it would heap-allocate every element). Use QVector (or std::vector) to hold QStringViews instead.
See Also▲
See also QString, QStringRef
Member Type Documentation▲
QStringView::const_iterator▲
This typedef provides an STL-style const iterator for QStringView.
See Also▲
See also iterator, const_reverse_iterator
QStringView::const_pointer▲
Alias for value_type *. Provided for compatibility with the STL.
QStringView::const_reference▲
Alias for value_type &. Provided for compatibility with the STL.
QStringView::const_reverse_iterator▲
This typedef provides an STL-style const reverse iterator for QStringView.
See Also▲
See also reverse_iterator, const_iterator
QStringView::difference_type▲
Alias for std::ptrdiff_t. Provided for compatibility with the STL.
QStringView::iterator▲
This typedef provides an STL-style const iterator for QStringView.
QStringView does not support mutable iterators, so this is the same as const_iterator.
See Also▲
See also const_iterator, reverse_iterator
QStringView::pointer▲
Alias for value_type *. Provided for compatibility with the STL.
QStringView does not support mutable pointers, so this is the same as const_pointer.
QStringView::reference▲
Alias for value_type &. Provided for compatibility with the STL.
QStringView does not support mutable references, so this is the same as const_reference.
QStringView::reverse_iterator▲
This typedef provides an STL-style const reverse iterator for QStringView.
QStringView does not support mutable reverse iterators, so this is the same as const_reverse_iterator.
See Also▲
See also const_reverse_iterator, iterator
QStringView::size_type▲
Alias for qsizetype. Provided for compatibility with the STL.
Unlike other Qt classes, QStringView uses qsizetype as its size_type, to allow accepting data from std::basic_string without truncation. The Qt API functions, for example length(), return int, while the STL-compatible functions, for example size(), return size_type.
QStringView::storage_type▲
Alias for char16_t for non-Windows or if Q_COMPILER_UNICODE_STRINGS is defined. Otherwise, alias for wchar_t.
QStringView::value_type▲
Alias for const QChar. Provided for compatibility with the STL.
Member Function Documentation▲
bool QStringView::startsWith(QChar ch) const▲
bool QStringView::startsWith(QChar ch, Qt::CaseSensitivity cs) const
bool QStringView::startsWith(QLatin1String l1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
bool QStringView::startsWith(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
Returns true if this string-view starts with string-view str, Latin-1 string l1, or character ch, respectively; otherwise returns false.
If cs is Qt::CaseSensitive (the default), the search is case-sensitive; otherwise the search is case-insensitive.
See Also▲
See also endsWith()
bool QStringView::endsWith(QChar ch) const▲
bool QStringView::endsWith(QChar ch, Qt::CaseSensitivity cs) const
bool QStringView::endsWith(QLatin1String l1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
bool QStringView::endsWith(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
Returns true if this string-view ends with string-view str, Latin-1 string l1, or character ch, respectively; otherwise returns false.
If cs is Qt::CaseSensitive (the default), the search is case-sensitive; otherwise the search is case-insensitive.
See Also▲
See also startsWith()
QStringView::QStringView()▲
QStringView::QStringView(std::nullptr_t)▲
QStringView::QStringView(const Char *str, qsizetype len)▲
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. The compatible character types are: QChar, ushort, char16_t and (on platforms, such as Windows, where it is a 16-bit type) wchar_t.
QStringView::QStringView(const Char *first, const Char *last)▲
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. The compatible character types are: QChar, ushort, char16_t and (on platforms, such as Windows, where it is a 16-bit type) wchar_t.
QStringView::QStringView(const Char (&)[N] string = N)▲
Constructs a string view on the character string literal string. The length is set to N-1, excluding the trailing {Char(0)}. If you need the full array, use the constructor from pointer and size instead:
auto
sv =
QStringView(array, std::
size(array)); // using C++17 std::size()
string must remain valid for the lifetime of this string view object.
This constructor only participates in overload resolution if string is an actual array and Char is a compatible character type. The compatible character types are: QChar, ushort, char16_t and (on platforms, such as Windows, where it is a 16-bit type) wchar_t.
QStringView::QStringView(const Char *str)▲
Constructs a string view on str. The length is determined by scanning for the first Char(0).
str must remain valid for the lifetime of this string view object.
Passing nullptr as str is safe and results in a null string view.
This constructor only participates in overload resolution if str is not an array and if Char is a compatible character type. The compatible character types are: QChar, ushort, char16_t and (on platforms, such as Windows, where it is a 16-bit type) wchar_t.
QStringView::QStringView(const QString &str)▲
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().
QStringView::QStringView(const QStringRef &str)▲
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().
QStringView::QStringView(const StdBasicString &str)▲
Constructs a string view on str. The length is taken from str.size().
str.data() must remain valid for the lifetime of this string view object.
This constructor only participates in overload resolution if StdBasicString is an instantiation of std::basic_string with a compatible character type. The compatible character types are: QChar, ushort, char16_t and (on platforms, such as Windows, where it is a 16-bit type) wchar_t.
The string view will be empty if and only if str.empty(). It is unspecified whether this constructor can result in a null string view (str.data() would have to return nullptr for this).
See Also▲
QChar QStringView::at(qsizetype n) const▲
Returns the character at position n in this string view.
The behavior is undefined if n is negative or not less than size().
See Also▲
See also operator[](), front(), back()
QChar QStringView::back() const▲
Returns the last character in the string. Same as last().
This function is provided for STL compatibility.
Calling this function on an empty string view constitutes undefined behavior.
See Also▲
QStringView::const_iterator QStringView::begin() const▲
Returns a const STL-style iterator pointing to the first character in the string.
This function is provided for STL compatibility.
See Also▲
QStringView::const_iterator QStringView::cbegin() const▲
QStringView::const_iterator QStringView::cend() const▲
void QStringView::chop(qsizetype length)▲
Truncates this string view by length characters.
Same as *this = left(size() - length).
The behavior is undefined when length < 0 or length > size().