CopperSpice API  1.7.2
QStringView< S > Class Template Reference

Provides a string view class. More...

Inherits CsBasicStringView< S >.

Public Typedefs

using const_iterator = typename S::const_iterator
 
using const_reverse_iterator = typename S::const_reverse_iterator
 
using difference_type = typename S::difference_type
 
using iterator = typename S::const_iterator
 
using reverse_iterator = typename S::const_reverse_iterator
 
using size_type = typename S::difference_type
 
using value_type = typename S::value_type
 

Public Methods

 QStringView () = default
 
 QStringView (const QStringView &other) = default
 
 QStringView (const S &str)
 
 QStringView (const_iterator begin, const_iterator end)
 
 QStringView (QStringView &&other) = default
 
 QStringView (S &&str) = delete
 
value_type at (size_type n) const
 
value_type back () const
 
iterator begin ()
 
const_iterator begin () const
 
const_iterator cbegin () const
 
const_iterator cend () const
 
const S::storage_type * charData () const
 
void chop (size_type numOfChars)
 
const_iterator constBegin () const
 
const_iterator constEnd () const
 
bool contains (const QRegularExpression< S > &regExp) const
 
bool contains (const S &str, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
 
bool contains (QStringView str, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
 
bool contains (value_type c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
 
size_type count () const
 
size_type count (const QRegularExpression< S > &regExp) const
 
size_type count (const S &str, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
 
size_type count (QStringView str, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
 
size_type count (value_type c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
 
const_reverse_iterator crbegin () const
 
const_reverse_iterator crend () const
 
bool empty () const
 
iterator end ()
 
const_iterator end () const
 
bool endsWith (QStringView str, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
 
bool endsWith (S str, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
 
template<typename C = value_type>
bool endsWith (value_type c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
 
value_type first () const
 
value_type front () const
 
const_iterator indexOfFast (const S &str) const
 
const_iterator indexOfFast (const S &str, const_iterator from, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
 
const_iterator indexOfFast (QStringView< S > str, const_iterator from, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
 
const_iterator indexOfFast (value_type c) const
 
const_iterator indexOfFast (value_type c, const_iterator from, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
 
bool isEmpty () const
 
value_type last () const
 
const_iterator lastIndexOfFast (const S &str) const
 
const_iterator lastIndexOfFast (const S &str, const_iterator from, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
 
const_iterator lastIndexOfFast (QStringView< S > str, const_iterator from, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
 
const_iterator lastIndexOfFast (value_type c) const
 
const_iterator lastIndexOfFast (value_type c, const_iterator from, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
 
QStringView< S > left (size_type numOfChars) const
 
size_type length () const
 
QStringView< S > mid (size_type indexStart, size_type numOfChars=-1) const
 
QStringView & operator= (const QStringView &other) = default
 
QStringView & operator= (QStringView &&other) = default
 
value_type operator[] (size_type n) const
 
reverse_iterator rbegin ()
 
const_reverse_iterator rbegin () const
 
reverse_iterator rend ()
 
const_reverse_iterator rend () const
 
QStringView< S > right (size_type numOfChars) const
 
size_type size () const
 
size_type size_storage () const
 
bool startsWith (QStringView< S > str, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
 
bool startsWith (S str, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
 
template<typename C = value_type>
bool startsWith (value_type c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
 
toCaseFolded () const
 
QByteArray toLatin1 () const
 
toLower () const
 
toString () const
 
toUpper () const
 
QByteArray toUtf8 () const
 
QStringView< S > trimmed () const
 

Related Functions

These are not member functions

QStringView16 make_view (QString16 &&str)
 
QStringView8 make_view (QString8 &&str)
 

Detailed Description

template<typename S>
class QStringView< S >

The QStringView class provides a view into a QString8 or a QString16 with a read only subset of the QString API.

QStringView is implemented as a pair of iterators which define the start and end of a contiguous range in an existing string. Since QStringView does not own the data it refers to, it is only valid while the original string is valid. If the original string is destroyed or modified the view is invalid. A QStringView should be passed by value, not by reference.

QStringView can also be used as the return value of a method or function. If you need access to the return value and the original string might go out of scope or change, it may be necessary to copy the data into a new QString8. A QStringView can should be treated with the same care that iterators to the original string would be.

Since this is a templated class any string data type can be passed as the template argument S. The template argument must be a string class which provides const iterators.

Member Typedef Documentation

template<typename S >
QStringView< S >::const_iterator

Provides an STL style const iterator for QStringView.

template<typename S >
QStringView< S >::const_reverse_iterator

Provides an STL style const reverse iterator for QStringView.

See also
QString8::iterator
template<typename S >
QStringView< S >::difference_type

Typedef for an integer type representing the distance between two elements in a QStringView.

template<typename S >
QStringView< S >::iterator

Provides an STL style const iterator for QStringView.

template<typename S >
QStringView< S >::reverse_iterator

Provides an STL style non-const reverse iterator for QStringView.

See also
QString8::const_iterator
template<typename S >
QStringView< S >::size_type

Typedef for an integer type representing sizes in a QStringView.

template<typename S >
QStringView< S >::value_type

Typedef for the elements stored in a QStringView.

Constructor & Destructor Documentation

template<typename S >
QStringView< S >::QStringView ( )
default

Constructs an empty QStringView.

template<typename S >
QStringView< S >::QStringView ( const_iterator  begin,
const_iterator  end 
)
inline

Constructs a QStringView using the specified iterator range.

template<typename S >
QStringView< S >::QStringView ( const QStringView< S > &  other)
default

Constructs a QStringView by making a copy of other. The string other must remain valid for the lifetime of this string view.

template<typename S >
QStringView< S >::QStringView ( QStringView< S > &&  other)
default

Move constructs a QStringView from other.

template<typename S >
QStringView< S >::QStringView ( const S &  str)
inline

Constructs a QStringView using the specified string str. The string must remain valid for the lifetime of this string view.

template<typename S >
QStringView< S >::QStringView ( S &&  str)
delete

This constructor has been marked deleted to ensure a string view is not created from a temporary string unintentionally. In order to pass a temporary string to a function which expects a QStringView, use the function make_view().

Method Documentation

template<typename S >
value_type QStringView< S >::at ( size_type  n) const
inline

Returns the code point at position n in this string view. The behavior of this method is undefined if n is negative or if n is equal to or greater than the size() of the string view.

See also
operator[](), front(), back()
template<typename S >
value_type QStringView< S >::back ( ) const
inline

Returns the last code point in the string. This method returns the same result as as last(). The behavior of this method is undefined if the string view is empty.

See also
front(), first(), last()
template<typename S >
iterator QStringView< S >::begin ( )
inline

Returns an STL style iterator pointing to the first code point in the string view.

See also
constBegin(), end()
template<typename S >
const_iterator QStringView< S >::begin ( ) const
inline

Returns a const STL style iterator pointing to the first code point in the string view.

See also
begin(), end()
template<typename S >
const_iterator QStringView< S >::cbegin ( ) const
inline

Returns a const STL style iterator pointing to the first code point in the string view.

See also
begin(), cend()
template<typename S >
const_iterator QStringView< S >::cend ( ) const
inline

Returns a const STL style iterator pointing to the imaginary code point after the last code point in the string view.

See also
cbegin(), end()
template<typename S >
const S::storage_type * QStringView< S >::charData ( ) const
inline

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

template<typename S >
void QStringView< S >::chop ( size_type  numOfChars)

Removes numOfChar code points counting from the end of the string. If numOfChar is greater than size() the result is an empty string.

QString str("LOGOUT");
str.chop(3); // str == "LOG"

To remove characters from the beginning of the string, use remove().

See also
truncate(), resize(), remove()
template<typename S >
const_iterator QStringView< S >::constBegin ( ) const
inline

Returns a const STL style iterator pointing to the first code point in the string view.

See also
begin(), constEnd()
template<typename S >
const_iterator QStringView< S >::constEnd ( ) const
inline

Returns a const STL style iterator pointing to the imaginary code point after the last code point in the string view.

See also
constBegin(), end()
template<typename S >
bool QStringView< S >::contains ( const QRegularExpression< S > &  regExp) const
inline

Returns true if this string contains an occurrence of the regular expression regExp, otherwise it returns false.

QRegularExpression regExp("[0-9]$");
// obsolete syntax, indexIn method removed
bool found_A = regExp.indexIn(child->objectName()) != -1;
// use contains()
bool found_B = child->objectName().contains(regExp);
template<typename S >
bool QStringView< S >::contains ( const S &  str,
Qt::CaseSensitivity  cs = Qt::CaseSensitive 
) const

Returns true if this string contains an occurrence of the string str, otherwise it returns false. If cs is Qt::CaseSensitive the search is case sensitive, otherwise the search is case insensitive.

QStringView str = "Peter Pan";
str.contains("peter", Qt::CaseInsensitive); // returns true
template<typename S >
bool QStringView< S >::contains ( QStringView< S >  str,
Qt::CaseSensitivity  cs = Qt::CaseSensitive 
) const

Returns true if this string contains an occurrence of the string view str, otherwise it returns false. If cs is Qt::CaseSensitive the search is case sensitive, otherwise the search is case insensitive.

template<typename S >
bool QStringView< S >::contains ( value_type  c,
Qt::CaseSensitivity  cs = Qt::CaseSensitive 
) const

Returns true if this string contains an occurrence of the character c, otherwise returns false. If cs is Qt::CaseSensitive the search is case sensitive, otherwise the search is case insensitive.

template<typename S >
size_type QStringView< S >::count ( ) const
inline

Returns the same as size().

template<typename S >
QStringView< S >::size_type QStringView< S >::count ( const QRegularExpression< S > &  regExp) const

Returns the number of occurrences of the regular expression regExp in the string.

template<typename S >
QStringView< S >::size_type QStringView< S >::count ( const S &  str,
Qt::CaseSensitivity  cs = Qt::CaseSensitive 
) const

Returns the number of (potentially overlapping) occurrences of the string str in this string. If cs is Qt::CaseSensitive the search is case sensitive, otherwise the search is case insensitive.

See also
contains(), indexOf()
template<typename S >
QStringView< S >::size_type QStringView< S >::count ( QStringView< S >  str,
Qt::CaseSensitivity  cs = Qt::CaseSensitive 
) const

Returns the number of occurrences of the string view str in this string. If cs is Qt::CaseSensitive the search is case sensitive, otherwise the search is case insensitive.

template<typename S >
QStringView< S >::size_type QStringView< S >::count ( value_type  c,
Qt::CaseSensitivity  cs = Qt::CaseSensitive 
) const

Returns the number of occurrences of character c in the string. If cs is Qt::CaseSensitive the search is case sensitive, otherwise the search is case insensitive.

template<typename S >
const_reverse_iterator QStringView< S >::crbegin ( ) const
inline

Returns an STL style iterator pointing to the last code point in the string view, in reverse order.

See also
begin(), rbegin(), rend()
template<typename S >
const_reverse_iterator QStringView< S >::crend ( ) const
inline

Returns an STL style iterator pointing to one past the first code point in the string view, in reverse order.

See also
end(), rbegin(), rend()
template<typename S >
bool QStringView< S >::empty ( ) const
inline

Returns true if this string view is empty.

See also
isEmpty(), size(), length()
template<typename S >
iterator QStringView< S >::end ( )
inline

Returns an STL style iterator pointing to the imaginary code point after the last code point in the string view.

See also
begin(), constEnd()
template<typename S >
const_iterator QStringView< S >::end ( ) const
inline

Returns a const STL style iterator pointing to the imaginary code point after the last code point in the string view.

template<typename S >
bool QStringView< S >::endsWith ( QStringView< S >  str,
Qt::CaseSensitivity  cs = Qt::CaseSensitive 
) const

Returns true if this string view ends with the string view str, otherwise returns false. If cs is Qt::CaseSensitive the search is case sensitive, otherwise the search is case insensitive.

See also
startsWith()
template<typename S >
bool QStringView< S >::endsWith ( str,
Qt::CaseSensitivity  cs = Qt::CaseSensitive 
) const
inline

Returns true if this string view ends with the string str, otherwise returns false. If cs is Qt::CaseSensitive the search is case sensitive, otherwise the search is case insensitive.

See also
startsWith()
template<typename S >
template<typename C >
bool QStringView< S >::endsWith ( value_type  c,
Qt::CaseSensitivity  cs = Qt::CaseSensitive 
) const

Returns true if this string view ends with the code point ch, otherwise returns false. If cs is Qt::CaseSensitive the search is case sensitive, otherwise the search is case insensitive.

See also
startsWith()
template<typename S >
value_type QStringView< S >::first ( ) const
inline

Returns the first code point in this string view. Returns the same result as front(). The behavior of this method is undefined if the string view is empty.

See also
back(), front(), last()
template<typename S >
value_type QStringView< S >::front ( ) const
inline

Returns the first code point in this string view. Returns the same result as first(). The behavior of this method is undefined if the string view is empty.

See also
back(), first(), last()
template<typename S >
const_iterator QStringView< S >::indexOfFast ( const S &  str) const
inline

Returns an iterator to the first occurrence of str in this string. Returns the end iterator if str is not found.

template<typename S >
const_iterator QStringView< S >::indexOfFast ( const S &  str,
const_iterator  from = cbegin(),
Qt::CaseSensitivity  cs = Qt::CaseSensitive 
) const
inline

Returns an iterator to the first occurrence of str in this string, searching forward from iterator from. Returns the end iterator if str is not found.

If cs is Qt::CaseSensitive the search is case sensitive, otherwise the search is case insensitive.

template<typename S >
const_iterator QStringView< S >::indexOfFast ( QStringView< S >  str,
const_iterator  from,
Qt::CaseSensitivity  cs = Qt::CaseSensitive 
) const
inline

Returns the iterator position of the first occurrence of the string view str in the string, searching forward from iterator position from. Returns the end iterator if str is not found.

If cs is Qt::CaseSensitive the search is case sensitive, otherwise the search is case insensitive.

template<typename S >
const_iterator QStringView< S >::indexOfFast ( value_type  c) const
inline

Returns an iterator to the first occurrence of c in this string. Returns the end iterator if c is not found.

template<typename S >
const_iterator QStringView< S >::indexOfFast ( value_type  c,
const_iterator  from = cbegin(),
Qt::CaseSensitivity  cs = Qt::CaseSensitive 
) const
inline

Returns an iterator to the first occurrence of c in this string, searching forward from iterator from. Returns the end iterator if c is not found.

If cs is Qt::CaseSensitive the search is case sensitive, otherwise the search is case insensitive.

template<typename S >
bool QStringView< S >::isEmpty ( ) const
inline

Returns true if this string view is empty.

See also
empty(), size(), length()
template<typename S >
value_type QStringView< S >::last ( ) const
inline

Returns the last code point in this string view. Returns the same result as back(). The behavior of this method is undefined if the string view is empty.

See also
back(), front(), first()
template<typename S >
const_iterator QStringView< S >::lastIndexOfFast ( const S &  str) const
inline

Returns an iterator to the last occurrence of str in this string. Returns the end iterator if str is not found.

template<typename S >
const_iterator QStringView< S >::lastIndexOfFast ( const S &  str,
const_iterator  from = cend(),
Qt::CaseSensitivity  cs = Qt::CaseSensitive 
) const
inline

Returns an iterator to the first occurrence of str in this string, searching backward from iterator from. Returns the end iterator if str is not found.

If cs is Qt::CaseSensitive the search is case sensitive, otherwise the search is case insensitive.

template<typename S >
const_iterator QStringView< S >::lastIndexOfFast ( QStringView< S >  str,
const_iterator  from,
Qt::CaseSensitivity  cs = Qt::CaseSensitive 
) const
inline

Returns the iterator position of the last occurrence of the string view str in this string, searching backward from iterator position from. Returns the end iterator if str is not found.

If cs is Qt::CaseSensitive the search is case sensitive, otherwise the search is case insensitive.

template<typename S >
const_iterator QStringView< S >::lastIndexOfFast ( value_type  c) const
inline

Returns an iterator to the last occurrence of c in this string. Returns the end iterator if c is not found.

template<typename S >
const_iterator QStringView< S >::lastIndexOfFast ( value_type  c,
const_iterator  from = cend(),
Qt::CaseSensitivity  cs = Qt::CaseSensitive 
) const
inline

Returns an iterator to the last occurrence of c in this string, searching backward from iterator from. Returns the end iterator if c is not found.

If cs is Qt::CaseSensitive the search is case sensitive, otherwise the search is case insensitive.

template<typename S >
QStringView< S > QStringView< S >::left ( size_type  numOfChars) const

Returns a string view containing the first numOfChars code points in this string view. If numOfChars is negative the original string view is returned. If numOfChars is equal to or greater than the size() of the string view an empty string view is returned.

See also
mid(), right()
template<typename S >
size_type QStringView< S >::length ( ) const
inline

Returns the number of code points in this string view. Returns the same result as size().

See also
empty(), isEmpty(), size()
template<typename S >
QStringView< S > QStringView< S >::mid ( size_type  indexStart,
size_type  numOfChars = -1 
) const

Returns a string view containing numOfChars code points of this string view, starting at the specified indexStart position. The behavior of this method is undefined if numOfChars is negative or not less than size().

See also
left(), right()
template<typename S >
QStringView & QStringView< S >::operator= ( const QStringView< S > &  other)
default

Copy assigns from other and returns a reference to this object. The string view other must remain valid for the lifetime of this string view object.

template<typename S >
QStringView & QStringView< S >::operator= ( QStringView< S > &&  other)
default

Move assigns from other and returns a reference to this object.

template<typename S >
QStringView< S >::value_type QStringView< S >::operator[] ( size_type  n) const

Returns the code point at position n in this string view. The behavior of this method is undefined if n is negative or if n is equal to or greater than the size() of the string view.

See also
at(), front(), back()
template<typename S >
reverse_iterator QStringView< S >::rbegin ( )
inline

Returns an STL style iterator pointing to the first last code point in the string view, in reverse order.

See also
begin(), crbegin(), rend()
template<typename S >
const_reverse_iterator QStringView< S >::rbegin ( ) const
inline

Returns an STL style iterator pointing to the first last code point in the string view, in reverse order.

See also
begin(), crbegin(), rend()
template<typename S >
reverse_iterator QStringView< S >::rend ( )
inline

Returns an STL style iterator pointing to one past the last code point in the string view, in reverse order.

See also
begin(), crbegin(), rend()
template<typename S >
const_reverse_iterator QStringView< S >::rend ( ) const
inline

Returns an STL style iterator pointing to one past the last code point in the string view, in reverse order.

See also
begin(), crbegin(), rend()
template<typename S >
QStringView< S > QStringView< S >::right ( size_type  numOfChars) const

Returns a string view containing the last numOfChars code points in this string view. If numOfChars is negative the original string view is returned. If numOfChars is equal to or greater than the size() of the string view an empty string view is returned.

See also
left(), mid()
template<typename S >
size_type QStringView< S >::size ( ) const
inline

Returns the number of code points in this string view. Returns the same result as length().

See also
empty(), isEmpty(), length()
template<typename S >
size_type QStringView< S >::size_storage ( ) const
inline

Returns the number of storage units in this string view. The storage size is the actual bytes required in the underlying container for a string. This value will usually be larger than size().

It is normally only used when passing a string view to a C API which expects a pointer and a length.

template<typename S >
bool QStringView< S >::startsWith ( QStringView< S >  str,
Qt::CaseSensitivity  cs = Qt::CaseSensitive 
) const

Returns true if this string view starts with the string view str, otherwise returns false. If cs is Qt::CaseSensitive the search is case sensitive, otherwise the search is case insensitive.

See also
endsWith()
template<typename S >
bool QStringView< S >::startsWith ( str,
Qt::CaseSensitivity  cs = Qt::CaseSensitive 
) const
inline

Returns true if this string view starts with the string str, otherwise returns false. If cs is Qt::CaseSensitive the search is case sensitive, otherwise the search is case insensitive.

See also
endsWith()
template<typename S >
template<typename C >
bool QStringView< S >::startsWith ( value_type  c,
Qt::CaseSensitivity  cs = Qt::CaseSensitive 
) const

Returns true if this string view starts with the code point ch, otherwise returns false. If cs is Qt::CaseSensitive the search is case sensitive, otherwise the search is case insensitive.

See also
endsWith()
template<typename S >
S QStringView< S >::toCaseFolded ( ) const
nodiscard

Returns the case folded equivalent of the string. For most Unicode characters this is the same as toLower().

template<typename S >
QByteArray QStringView< S >::toLatin1 ( ) const

Returns a Latin-1 representation of the string view as a QByteArray. Any code points which can not be represented in Latin-1 will be replaced by a question mark.

See also
toUtf8(), QTextCodec
template<typename S >
S QStringView< S >::toLower ( ) const
nodiscard

Returns a lowercase copy of the string. The case conversion will always happen in the 'C' locale. For locale dependent lower casing use QLocale::toLower().

See also
toUpper(), QLocale::toLower()
template<typename S >
S QStringView< S >::toString ( ) const
inline

Return this string view as a string of type S.

template<typename S >
S QStringView< S >::toUpper ( ) const
nodiscard

Returns an uppercase copy of the string. The case conversion will always happen in the 'C' locale. For locale dependent upper casing use QLocale::toUpper().

See also
toLower(), QLocale::toUpper()
template<typename S >
QByteArray QStringView< S >::toUtf8 ( ) const

Returns a UTF-8 string as a QByteArray.

See also
toLatin1(), QTextCodec
template<typename S >
QStringView< S > QStringView< S >::trimmed ( ) const

Strips leading and trailing whitespace from this string view and returns the result.

Whitespace means any code point for which QChar32::isSpace() returns true. This includes the ASCII characters '\t', '\n', '\v', '\f', '\r', and ' '.

Friends And Related Function Documentation

QStringView16 make_view ( QString16 &&  str)
related

Constructs a string view from the temporary string str. This function returns a QStringView16 which is only valid for the lifetime of str.

If the string view is passed to another function, verify that the called function does not attempt to save a copy of the view. As per the C++ standard, accessing a view after the lifetime of the temporary string has expired, is undefined behavior.

QStringView8 make_view ( QString8 &&  str)
related

Constructs a string view from the temporary string str. This function returns a QStringView8 which is only valid for the lifetime of str.

If the string view is passed to another function, verify that the called function does not attempt to save a copy of the view. As per the C++ standard, accessing a view after the lifetime of the temporary string has expired, is undefined behavior.