#include <Containers/StringView.h>
template<class T>
BasicStringView class
Base for string views.
A lighter alternative to C++17 std::
Usage
The class is meant to be used through either the StringView or MutableStringView typedefs. It's implicitly convertible from C string literals, but the recommended way is using the Literals::
While both expressions are mostly equivalent, the literal is constexpr
so you can use it in a compile-time context (and on the other hand, the implicit conversion uses std::
C string literals are implicitly immutable, in order to create a mutable one you need to assign the literal to a char[]
(instead of const char*
) and then create a MutableStringView in a second step.
This class is implicitly convertible from and to ArrayView, however note that the conversion will not preserve the global / null-terminated annotations.
String view slicing
The string view class inherits the slicing APIs of ArrayView — slice(), sliceSize(), prefix(), suffix(), exceptPrefix() and exceptSuffix() — and in addition it provides string-specific utilities. These are are all derived from the slicing APIs, which means they also return sub-views of the original string:
- split() and splitWithoutEmptyParts() split the view on given set of delimiter characters
- join() and joinWithoutEmptyParts() is an inverse of the above
- partition() is similar to split(), but always returning three elements with a clearly defined behavior, which can make certain code more robust while reducing the amount of possible error states
- trimmed() (and its variants trimmedPrefix() / trimmedSuffix()), commonly used to remove leading and trailing whitespace
- exceptPrefix(StringView) const / exceptSuffix(StringView) const checks that a view starts (or ends) with given string and then removes it.
Converting StringView instances to null-terminated C strings
If possible when interacting with 3rd party APIs, passing a string together with the size information is always preferable to passing just a plain const char*
. Apart from saving an unnecessary std::
Unlike a String, string views can point to any slice of a larger string and thus can't guarantee null termination. Because of this and because even a view with StringViewFlags::'\0'
anywhere in the middle, there's no implicit conversion to const char*
provided, and the pointer returned by data() should only be used together with size().
The quickest safe way to get a null-terminated string out of a StringView is to convert the view to a String and then use String::
Similarly as described in Converting String instances to null-terminated C strings, pointers to data in SSO instances will get invalidated when the instance is moved. With String::
Conversion to array views
String views are implicitly convertible to ArrayView as described in the following table. This also extends to other container types constructibe from ArrayView.
String view type | ↭ | Array view type |
---|---|---|
StringView | → | ArrayView<const char> |
MutableStringView | → | ArrayView<const char> |
MutableStringView | → | ArrayView<char> |
STL compatibility
Instances of StringView and BasicStringView are implicitly convertible from and to std::#include <string>
, which significantly affects compile times.
Creating a std::
On compilers that support C++17 and std::#include <string_view>
, but this one is even significantly heavier than the <string> include on certain implementations, so it's separate from a std::
The std::
Constructors, destructors, conversion operators
-
BasicStringView(std::
nullptr_t = nullptr) constexpr noexcept - Default constructor.
-
BasicStringView(T* data,
std::
size_t size, StringViewFlags flags = {}) constexpr noexcept - Construct from a C string of known size.
- BasicStringView(String& data) noexcept
- Construct from a String.
-
template<class U = T, class = typename std::BasicStringView(const String& data) noexcept
enable_if<std:: is_const<U>::value>::type> - Construct from a const String.
- BasicStringView(ArrayView<T> data, StringViewFlags flags = {}) noexcept
- Construct from an ArrayView.
-
template<class U, class = typename std::BasicStringView(BasicStringView<U> mutable_) constexpr noexcept
enable_if<std:: is_same<const U, T>::value>::type> - Construct a StringView from a MutableStringView.
- BasicStringView(T* data, StringViewFlags extraFlags = {}) noexcept
- Construct from a null-terminated C string.
-
template<class U, class = decltype(Implementation::StringViewConverter<T, typename std::BasicStringView(U&& other) constexpr noexcept
decay<U && >::type>::from(std:: declval<U && >()))> - Construct a view on an external type / from an external representation.
-
template<class U, class = decltype(Implementation::StringViewConverter<T, U>::to(std::operator U() const constexpr
declval<BasicStringView<T>>()))> - Convert the view to external representation.
- operator bool() const explicit constexpr
- Whether the string is non-empty and non-null.
Public functions
- auto flags() const -> StringViewFlags constexpr
- Flags.
- auto data() const -> T* constexpr
- String data.
-
auto size() const -> std::
size_t constexpr - String size.
- auto empty() const -> bool constexpr
- Whether the string is empty.
- auto begin() const -> T* constexpr
- Pointer to the first byte.
- auto cbegin() const -> T* constexpr
- auto end() const -> T* constexpr
- Pointer to (one item after) the last byte.
- auto cend() const -> T* constexpr
- auto front() const -> T& constexpr
- First byte.
- auto back() const -> T& constexpr
- Last byte.
-
auto operator[](std::
size_t i) const -> T& constexpr - Element access.
- auto slice(T* begin, T* end) const -> BasicStringView<T> constexpr
- View slice.
-
auto slice(std::
size_t begin, std:: size_t end) const -> BasicStringView<T> constexpr -
auto sliceSize(T* begin,
std::
size_t size) const -> BasicStringView<T> constexpr - View slice of given size.
-
auto sliceSize(std::
size_t begin, std:: size_t size) const -> BasicStringView<T> constexpr - auto prefix(T* end) const -> BasicStringView<T> constexpr
- View prefix until a pointer.
- auto suffix(T* begin) const -> BasicStringView<T> constexpr
- View suffix after a pointer.
-
auto prefix(std::
size_t size) const -> BasicStringView<T> constexpr - View on the first
size
bytes. -
auto exceptPrefix(std::
size_t size) const -> BasicStringView<T> constexpr - View except the first
size
bytes. -
auto exceptSuffix(std::
size_t size) const -> BasicStringView<T> constexpr - View except the last
size
bytes. - auto split(char delimiter) const -> Array<BasicStringView<T>>
- Split on given character.
- auto split(StringView delimiter) const -> Array<BasicStringView<T>>
- Split on given substring.
- auto splitWithoutEmptyParts(char delimiter) const -> Array<BasicStringView<T>>
- Split on given character, removing empty parts.
- auto splitOnAnyWithoutEmptyParts(StringView delimiters) const -> Array<BasicStringView<T>>
- Split on any character from given set, removing empty parts.
- auto splitOnWhitespaceWithoutEmptyParts() const -> Array<BasicStringView<T>>
- Split on whitespace, removing empty parts.
- auto partition(char separator) const -> StaticArray<3, BasicStringView<T>>
- Partition on a character.
- auto partition(StringView separator) const -> StaticArray<3, BasicStringView<T>>
- Partition on a substring.
- auto join(ArrayView<const StringView> strings) const -> String
- Join strings with this view as the delimiter.
-
auto join(std::
initializer_list<StringView> strings) const -> String - auto joinWithoutEmptyParts(ArrayView<const StringView> strings) const -> String
- Join strings with this view as the delimiter, skipping empty parts.
-
auto joinWithoutEmptyParts(std::
initializer_list<StringView> strings) const -> String - auto hasPrefix(StringView prefix) const -> bool
- Whether the string begins with given prefix.
- auto hasPrefix(char prefix) const -> bool
- auto hasSuffix(StringView suffix) const -> bool
- Whether the string ends with given suffix.
- auto hasSuffix(char suffix) const -> bool
- auto exceptPrefix(StringView prefix) const -> BasicStringView<T>
- View with given prefix stripped.
- auto exceptPrefix(char prefix) const -> BasicStringView<T> deleted
- Using char literals for prefix stripping is not allowed.
- auto exceptSuffix(StringView suffix) const -> BasicStringView<T>
- View with given suffix stripped.
-
template<class = typename std::auto exceptSuffix(T&& suffix) const -> BasicStringView<T> deleted
enable_if<std:: is_same<typename std:: decay<T>::type, char>::value>::type> - Using char literals for suffix stripping is not allowed.
- auto trimmed(StringView characters) const -> BasicStringView<T>
- View with given characters trimmed from prefix and suffix.
- auto trimmed() const -> BasicStringView<T>
- View with whitespace trimmed from prefix and suffix.
- auto trimmedPrefix(StringView characters) const -> BasicStringView<T>
- View with given characters trimmed from prefix.
- auto trimmedPrefix() const -> BasicStringView<T>
- View with whitespace trimmed from prefix.
- auto trimmedSuffix(StringView characters) const -> BasicStringView<T>
- View with given characters trimmed from suffix.
- auto trimmedSuffix() const -> BasicStringView<T>
- View with whitespace trimmed from suffix.
- auto find(StringView substring) const -> BasicStringView<T>
- Find a substring.
- auto find(char character) const -> BasicStringView<T>
- Find a character.
- auto findOr(StringView substring, T* fail) const -> BasicStringView<T>
- Find a substring with a custom failure pointer.
- auto findOr(char character, T* fail) const -> BasicStringView<T>
- Find a character with a custom failure pointer.
- auto findLast(StringView substring) const -> BasicStringView<T>
- Find the last occurence of a substring.
- auto findLast(char character) const -> BasicStringView<T>
- Find the last occurence of a character.
- auto findLastOr(StringView substring, T* fail) const -> BasicStringView<T>
- Find the last occurence a substring with a custom failure pointer.
- auto findLastOr(char character, T* fail) const -> BasicStringView<T>
- Find the last occurence of a character with a custom failure pointer.
- auto contains(StringView substring) const -> bool
- Whether the view contains a substring.
- auto contains(char character) const -> bool
- Whether the view contains a character.
- auto findAny(StringView characters) const -> BasicStringView<T>
- Find any character from given set.
- auto findAnyOr(StringView characters, T* fail) const -> BasicStringView<T>
- Find any character from given set with a custom failure pointer.
- auto findLastAny(StringView characters) const -> BasicStringView<T>
- Find the last occurence of any character from given set.
- auto findLastAnyOr(StringView characters, T* fail) const -> BasicStringView<T>
- Find the last occurence of any character from given set with a custom failure pointer.
- auto containsAny(StringView substring) const -> bool
- Whether the view contains any character from given set.
-
auto count(char character) const -> std::
size_t - Count of occurences of given character.
Friends
- auto operator==(StringView a, StringView b) -> bool
- auto operator!=(StringView a, StringView b) -> bool
- auto operator<(StringView a, StringView b) -> bool
- auto operator<=(StringView a, StringView b) -> bool
- auto operator>=(StringView a, StringView b) -> bool
- auto operator>(StringView a, StringView b) -> bool
-
auto operator*(StringView string,
std::
size_t count) -> String - String multiplication.
Function documentation
template<class T>
Death:: Containers:: BasicStringView<T>:: BasicStringView(std:: nullptr_t = nullptr) constexpr noexcept
Default constructor.
A default-constructed instance has StringViewFlags::
template<class T>
Death:: Containers:: BasicStringView<T>:: BasicStringView(T* data,
std:: size_t size,
StringViewFlags flags = {}) constexpr noexcept
Construct from a C string of known size.
Parameters | |
---|---|
data | C string |
size | Size of the C string, excluding the null terminator |
flags | Flags describing additional string properties |
If StringViewFlags::data
is not nullptr
and data[size] == '\0'
. That can avoid copies and allocations in code that passes such string to APIs that expect null-terminated strings (such as std::
If you're unsure about data origin, the safe bet is to keep flags at their default. On the other hand, C string literals are always global and null-terminated — for those, the recommended way is to use the operator""_
template<class T>
Death:: Containers:: BasicStringView<T>:: BasicStringView(String& data) noexcept
Construct from a String.
The resulting view has StringViewFlags::
template<class T>
template<class U = T, class = typename std:: enable_if<std:: is_const<U>::value>::type>
Death:: Containers:: BasicStringView<T>:: BasicStringView(const String& data) noexcept
Construct from a const String.
Enabled only if the view is not mutable. The resulting view has StringViewFlags::
template<class T>
Death:: Containers:: BasicStringView<T>:: BasicStringView(ArrayView<T> data,
StringViewFlags flags = {}) noexcept
Construct from an ArrayView.
The resulting view has the same size as data
, by default no null-termination is assumed.
template<class T>
Death:: Containers:: BasicStringView<T>:: BasicStringView(T* data,
StringViewFlags extraFlags = {}) noexcept
Construct from a null-terminated C string.
Contrary to the behavior of std::data
is allowed to be nullptr
— in that case an empty view is constructed.
Calls BasicStringView(T*, std::size
set to std::data
if data
is not nullptr
. If data
is nullptr
, size
is set to 0
. In addition to extraFlags
, if data
is not nullptr
, StringViewFlags::
The BasicStringView(std::constexpr
.
template<class T>
Death:: Containers:: BasicStringView<T>:: operator bool() const explicit constexpr
Whether the string is non-empty and non-null.
Returns true
if the string is non-empty and the pointer is not nullptr
, false
otherwise. If you rely on just one of these conditions, use empty() and data() instead.
template<class T>
T* Death:: Containers:: BasicStringView<T>:: data() const constexpr
String data.
The pointer is not guaranteed to be null-terminated, use flags() and StringViewFlags::
template<class T>
std:: size_t Death:: Containers:: BasicStringView<T>:: size() const constexpr
String size.
Excludes the null terminator.
template<class T>
T& Death:: Containers:: BasicStringView<T>:: front() const constexpr
First byte.
Expects there is at least one byte.
template<class T>
T& Death:: Containers:: BasicStringView<T>:: back() const constexpr
Last byte.
Expects there is at least one byte.
template<class T>
BasicStringView<T> Death:: Containers:: BasicStringView<T>:: slice(T* begin,
T* end) const constexpr
View slice.
Both arguments are expected to be in range. Propagates the StringViewFlags::end
points to (one item after) the end of the original null-terminated string, the result has StringViewFlags::
template<class T>
BasicStringView<T> Death:: Containers:: BasicStringView<T>:: slice(std:: size_t begin,
std:: size_t end) const constexpr
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
template<class T>
BasicStringView<T> Death:: Containers:: BasicStringView<T>:: sliceSize(T* begin,
std:: size_t size) const constexpr
View slice of given size.
Equivalent to data.slice(begin, begin + size)
.
template<class T>
BasicStringView<T> Death:: Containers:: BasicStringView<T>:: sliceSize(std:: size_t begin,
std:: size_t size) const constexpr
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
template<class T>
BasicStringView<T> Death:: Containers:: BasicStringView<T>:: prefix(T* end) const constexpr
View prefix until a pointer.
Equivalent to string.slice(string.begin(), end)
. If end
is nullptr
, returns zero-sized nullptr
view.
template<class T>
BasicStringView<T> Death:: Containers:: BasicStringView<T>:: suffix(T* begin) const constexpr
View suffix after a pointer.
Equivalent to string.slice(begin, string.end())
. If begin
is nullptr
and the original view isn't, returns a zero-sized nullptr
view.
template<class T>
BasicStringView<T> Death:: Containers:: BasicStringView<T>:: prefix(std:: size_t size) const constexpr
View on the first size
bytes.
Equivalent to string.slice(0, size)
.
template<class T>
BasicStringView<T> Death:: Containers:: BasicStringView<T>:: exceptPrefix(std:: size_t size) const constexpr
View except the first size
bytes.
Equivalent to string.slice(size, string.size())
.
template<class T>
BasicStringView<T> Death:: Containers:: BasicStringView<T>:: exceptSuffix(std:: size_t size) const constexpr
View except the last size
bytes.
Equivalent to string.slice(0, string.size() - size)
.
template<class T>
Array<BasicStringView<T>> Death:: Containers:: BasicStringView<T>:: split(char delimiter) const
Split on given character.
If delimiter
is not found, returns a single-item array containing the full input string. If the string is empty, returns an empty array. The function uses slice() internally, meaning it propagates the flags() as appropriate.
template<class T>
Array<BasicStringView<T>> Death:: Containers:: BasicStringView<T>:: split(StringView delimiter) const
Split on given substring.
If delimiter
is not found, returns a single-item array containing the full input string. If the string is empty, returns an empty array. The function uses slice() internally, meaning it propagates the flags() as appropriate.
Note that this function looks for the whole delimiter. If you want to split on any character from a set, use splitOnAnyWithoutEmptyParts() instead.
template<class T>
Array<BasicStringView<T>> Death:: Containers:: BasicStringView<T>:: splitWithoutEmptyParts(char delimiter) const
Split on given character, removing empty parts.
If delimiter
is not found, returns a single-item array containing the full input string. If the string is empty or consists just of delimiter
characters, returns an empty array. The function uses slice() internally, meaning it propagates the flags() as appropriate.
If you have just a single delimiter character, split(char) const is more efficient. If you need to split on a multi-character delimiter, use split(StringView) const instead.
template<class T>
Array<BasicStringView<T>> Death:: Containers:: BasicStringView<T>:: splitOnAnyWithoutEmptyParts(StringView delimiters) const
Split on any character from given set, removing empty parts.
If no characters from delimiters
are found, returns a single-item array containing the full input string. If the string is empty or consists just of characters from delimiters
, returns an empty array. The function uses slice() internally, meaning it propagates the flags() as appropriate.
template<class T>
Array<BasicStringView<T>> Death:: Containers:: BasicStringView<T>:: splitOnWhitespaceWithoutEmptyParts() const
Split on whitespace, removing empty parts.
Equivalent to calling splitOnAnyWithoutEmptyParts(StringView) const with " \t\f\v\r\n"
passed to delimiters
.
template<class T>
StaticArray<3, BasicStringView<T>> Death:: Containers:: BasicStringView<T>:: partition(char separator) const
Partition on a character.
Equivalent to Python's str.partition(). Splits string
at the first occurrence of separator
. First returned value is the part before the separator, second the separator, third a part after the separator. If the separator is not found, returns the input string followed by two empty strings.
The function uses slice() internally, meaning it propagates the flags() as appropriate. Additionally, the resulting views are nullptr
only if the input is nullptr
, otherwise the view always points to existing memory.
template<class T>
StaticArray<3, BasicStringView<T>> Death:: Containers:: BasicStringView<T>:: partition(StringView separator) const
Partition on a substring.
Like partition(char) const, but looks for a whole substring instead of a single character.
template<class T>
String Death:: Containers:: BasicStringView<T>:: join(ArrayView<const StringView> strings) const
Join strings with this view as the delimiter.
Similar in usage to Python's str.join()
template<class T>
String Death:: Containers:: BasicStringView<T>:: join(std:: initializer_list<StringView> strings) const
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
template<class T>
String Death:: Containers:: BasicStringView<T>:: joinWithoutEmptyParts(ArrayView<const StringView> strings) const
Join strings with this view as the delimiter, skipping empty parts.
Like join(), but empty views in strings
are skipped instead of causing multiple repeated delimiters in the output.
template<class T>
String Death:: Containers:: BasicStringView<T>:: joinWithoutEmptyParts(std:: initializer_list<StringView> strings) const
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
template<class T>
bool Death:: Containers:: BasicStringView<T>:: hasPrefix(StringView prefix) const
Whether the string begins with given prefix.
For an empty string returns true
only if prefix
is empty as well.
template<class T>
bool Death:: Containers:: BasicStringView<T>:: hasSuffix(StringView suffix) const
Whether the string ends with given suffix.
For an empty string returns true
only if suffix
is empty as well.
template<class T>
BasicStringView<T> Death:: Containers:: BasicStringView<T>:: exceptPrefix(StringView prefix) const
View with given prefix stripped.
Expects that the string actually begins with given prefix. The function uses slice() internally, meaning it propagates the flags() as appropriate. Additionally, the resulting view is nullptr
only if the input is nullptr
, otherwise the view always points to existing memory.
template<class T>
BasicStringView<T> Death:: Containers:: BasicStringView<T>:: exceptPrefix(char prefix) const deleted
Using char literals for prefix stripping is not allowed.
To avoid accidentally interpreting a char
literal as a size and calling exceptPrefix(std::
template<class T>
BasicStringView<T> Death:: Containers:: BasicStringView<T>:: exceptSuffix(StringView suffix) const
View with given suffix stripped.
Expects that the string actually ends with given suffix. The function uses slice() internally, meaning it propagates the flags() as appropriate. Additionally, the resulting view is nullptr
only if the input is nullptr
, otherwise the view always points to existing memory.
template<class T>
template<class = typename std:: enable_if<std:: is_same<typename std:: decay<T>::type, char>::value>::type>
BasicStringView<T> Death:: Containers:: BasicStringView<T>:: exceptSuffix(T&& suffix) const deleted
Using char literals for suffix stripping is not allowed.
To avoid accidentally interpreting a char
literal as a size and calling exceptSuffix(std::
template<class T>
BasicStringView<T> Death:: Containers:: BasicStringView<T>:: trimmed(StringView characters) const
View with given characters trimmed from prefix and suffix.
The function uses slice() internally, meaning it propagates the flags() as appropriate. Additionally, the resulting view is nullptr
only if the input is nullptr
, otherwise the view always points to existing memory.
template<class T>
BasicStringView<T> Death:: Containers:: BasicStringView<T>:: trimmed() const
View with whitespace trimmed from prefix and suffix.
Equivalent to calling trimmed(StringView) const with " \t\f\v\r\n"
passed to characters
.
template<class T>
BasicStringView<T> Death:: Containers:: BasicStringView<T>:: trimmedPrefix(StringView characters) const
View with given characters trimmed from prefix.
The function uses slice() internally, meaning it propagates the flags() as appropriate. Additionally, the resulting view is nullptr
only if the input is nullptr
, otherwise the view always points to existing memory.
template<class T>
BasicStringView<T> Death:: Containers:: BasicStringView<T>:: trimmedPrefix() const
View with whitespace trimmed from prefix.
Equivalent to calling trimmedPrefix(StringView) const with " \t\f\v\r\n"
passed to characters
.
template<class T>
BasicStringView<T> Death:: Containers:: BasicStringView<T>:: trimmedSuffix(StringView characters) const
View with given characters trimmed from suffix.
The function uses slice() internally, meaning it propagates the flags() as appropriate. Additionally, the resulting view is nullptr
only if the input is nullptr
, otherwise the view always points to existing memory.
template<class T>
BasicStringView<T> Death:: Containers:: BasicStringView<T>:: trimmedSuffix() const
View with whitespace trimmed from suffix.
Equivalent to calling trimmedSuffix(StringView) const with " \t\f\v\r\n"
passed to characters
.
template<class T>
BasicStringView<T> Death:: Containers:: BasicStringView<T>:: find(StringView substring) const
Find a substring.
Returns a view pointing to the first found substring. If not found, an empty nullptr
view is returned. The function uses slice() internally, meaning it propagates the flags() as appropriate, except in case of a failure, where it always returns no StringViewFlags.
Note that the function operates with a complexity and as such is meant mainly for one-time searches in non-performance-critical code. For repeated searches or searches of large substrings it's recommended to use the std::
This function is equivalent to calling find() on a std::
template<class T>
BasicStringView<T> Death:: Containers:: BasicStringView<T>:: find(char character) const
Find a character.
Faster than find(StringView) const if the string has just one byte.
template<class T>
BasicStringView<T> Death:: Containers:: BasicStringView<T>:: findOr(StringView substring,
T* fail) const
Find a substring with a custom failure pointer.
Like find(StringView) const, but returns an empty view pointing to the fail
value instead of nullptr
, which is useful to avoid explicit handling of cases where the substring wasn't found.
The fail
value can be nullptr
or any other pointer, but commonly it's set to either begin() or end().
Consider using findOr(char, T*) const for single-byte substrings.
template<class T>
BasicStringView<T> Death:: Containers:: BasicStringView<T>:: findOr(char character,
T* fail) const
Find a character with a custom failure pointer.
Faster than findOr(StringView, T*) const if the string has just one byte.
template<class T>
BasicStringView<T> Death:: Containers:: BasicStringView<T>:: findLast(StringView substring) const
Find the last occurence of a substring.
Returns a view pointing to the last found substring. If not found, an empty nullptr
view is returned. The function uses slice() internally, meaning it propagates the flags() as appropriate, except in case of a failure, where it always returns no StringViewFlags.
Similarly as with find(), note that the function operates with a complexity and as such is meant mainly for one-time searches in non-performance-critical code. See the documentation of find() for further information and suggested alternatives. Consider using findLast(char) const instead for single-byte substrings.
This function is equivalent to calling rfind() on a std::
template<class T>
BasicStringView<T> Death:: Containers:: BasicStringView<T>:: findLast(char character) const
Find the last occurence of a character.
Faster than findLast(StringView) const if the string has just one byte.
template<class T>
BasicStringView<T> Death:: Containers:: BasicStringView<T>:: findLastOr(StringView substring,
T* fail) const
Find the last occurence a substring with a custom failure pointer.
Like findLast(StringView) const, but returns an empty view pointing to the fail
value instead of nullptr
, which is useful to avoid explicit handling of cases where the substring wasn't found. See findOr() for an example use case.
template<class T>
BasicStringView<T> Death:: Containers:: BasicStringView<T>:: findLastOr(char character,
T* fail) const
Find the last occurence of a character with a custom failure pointer.
Faster than findLastOr(StringView, T*) const if the string has just one byte.
template<class T>
bool Death:: Containers:: BasicStringView<T>:: contains(StringView substring) const
Whether the view contains a substring.
A slightly lighter variant of find() useful when you only want to know if a substring was found or not. Consider using contains(char) const for single-byte substrings, see also count(char) const for counting the number of occurences.
template<class T>
bool Death:: Containers:: BasicStringView<T>:: contains(char character) const
Whether the view contains a character.
Faster than contains(StringView) const if the string has just one byte.
template<class T>
BasicStringView<T> Death:: Containers:: BasicStringView<T>:: findAny(StringView characters) const
Find any character from given set.
Returns a view pointing to the first found character from the set. If no characters from characters
are found, an empty nullptr
view is returned. The function uses slice() internally, meaning it propagates the flags() as appropriate, except in case of a failure, where it always returns no StringViewFlags.
This function is equivalent to calling find_
template<class T>
BasicStringView<T> Death:: Containers:: BasicStringView<T>:: findAnyOr(StringView characters,
T* fail) const
Find any character from given set with a custom failure pointer.
Like findAny(StringView) const, but returns an empty view pointing to the fail
value instead of nullptr
, which is useful to avoid explicit handling of cases where no character was found.
The fail
value can be nullptr
or any other pointer, but commonly it's set to either begin() or end().
template<class T>
BasicStringView<T> Death:: Containers:: BasicStringView<T>:: findLastAny(StringView characters) const
Find the last occurence of any character from given set.
Returns a view pointing to the last found character from the set. If no characters from characters
are found, an empty nullptr
view is returned. The function uses slice() internally, meaning it propagates the flags() as appropriate, except in case of a failure, where it always returns no StringViewFlags.
This function is equivalent to calling find_
template<class T>
BasicStringView<T> Death:: Containers:: BasicStringView<T>:: findLastAnyOr(StringView characters,
T* fail) const
Find the last occurence of any character from given set with a custom failure pointer.
Like findLastAny(StringView) const, but returns an empty view pointing to the fail
value instead of nullptr
, which is useful to avoid explicit handling of cases where the substring wasn't found.
template<class T>
bool Death:: Containers:: BasicStringView<T>:: containsAny(StringView substring) const
Whether the view contains any character from given set.
A slightly lighter variant of findAny() useful when you only want to know if a character was found or not.
template<class T>
std:: size_t Death:: Containers:: BasicStringView<T>:: count(char character) const
Count of occurences of given character.
If it's only needed to know whether a character is contained in a string at all, consider using contains(char) const instead.
template<class T>
String operator*(StringView string,
std:: size_t count)
String multiplication.
Equivalent to string multiplication in Python, returns string
repeated count
times.
template<class T>
StringView operator""_s(const char* data,
std:: size_t size) constexpr
String view literal.
The returned instance has both StringViewFlags::