Cogs.Foundation
|
Provides a weakly referenced view over the contents of a string. More...
#include <StringView.h>
Public Types | |
using | iterator = const char * |
Iterator type. | |
using | const_iterator = const char * |
Const iterator type. | |
using | value_type = char |
Value type. | |
using | Vector = Cogs::Collections::SmallVector< StringView, 10 > |
Vector of strings type. | |
Public Member Functions | |
constexpr | StringView () noexcept=default |
Constructs an empty string view. | |
constexpr | StringView (const char *data) noexcept |
Construct a string view of the given null-terminated string. | |
template<size_t LENGTH> | |
constexpr | StringView (const char(&data)[LENGTH]) noexcept |
Construct a string view from the given string literal. | |
constexpr | StringView (const char *data, const size_t length) noexcept |
Construct a string view of the given string data, with the given length. | |
constexpr | StringView (const std::string_view s) noexcept |
Construct a String view of the given std::string_view object. | |
StringView (const std::string &s) noexcept | |
Construct a string view of the given string object. | |
bool | operator== (const StringView &other) const |
Operator equal. | |
constexpr iterator | begin () noexcept |
Iterator to the beginning of the string. | |
constexpr iterator | end () noexcept |
Iterator to the end of the string. | |
constexpr const_iterator | begin () const noexcept |
Const iterator to the beginning of the string. | |
constexpr const_iterator | end () const noexcept |
Const iterator to the end of the string. | |
constexpr const_iterator | cbegin () const noexcept |
Const iterator to the beginning of the string. | |
constexpr const_iterator | cend () const noexcept |
Const iterator to the end of the string. | |
constexpr bool | empty () const noexcept |
Check if the string is empty. | |
constexpr | operator bool () const noexcept |
Explicit boolean conversion operator. | |
operator std::string () const | |
Explicit std::string conversion operator. | |
constexpr | operator std::string_view () const noexcept |
Implicit std::string_view conversion operator. | |
std::string | to_string () const |
String conversion method. | |
constexpr std::string_view | to_string_view () const noexcept |
Create a standard library string_view of the same view. | |
constexpr const char * | data () const noexcept |
Get the sequence of characters referenced by the string view. | |
constexpr size_t | size () const noexcept |
Get the size of the string. | |
constexpr size_t | length () const noexcept |
Get the length of the string. | |
constexpr char | operator[] (const size_t index) const noexcept |
Get the character at position index in the string. | |
constexpr size_t | hash () const noexcept |
Get the hash code of the string. | |
constexpr size_t | hash (size_t hashValue) const noexcept |
Combine the hash code of the string with the initial hash value provided. | |
size_t | hashLowercase (size_t hashValue=Cogs::hash()) const noexcept |
Get the hash code of the string converted to lowercase. | |
size_t | find (const StringView &other, size_t offset=0) const noexcept |
Find the given string segment inside the string. | |
size_t | find_first_of (char character, size_t pos=0) const noexcept |
Find the first occurance of the given character from the specified starting position. | |
size_t | find_last_of (const StringView characters, size_t pos=NoPosition) const noexcept |
See std::basic_string::find_last_of. | |
size_t | find_last_of (const char character, size_t pos=NoPosition) const noexcept |
See std::basic_string::find_last_of. | |
int | compare (const StringView &other) const noexcept |
Lexicographically compares the content of this string view with the given other. | |
constexpr StringView | substr (size_t offset, size_t count=NoPosition) const noexcept |
Get the given sub string. | |
Vector | split (const StringView &delimiters) const |
Split this string into parts. | |
Vector | splitAll (const StringView &delimiters) const |
Split this string into parts, empty parts will be retained. | |
StringView & | trimStart () noexcept |
Remove any whitespace from the start of this StringView's contents. | |
StringView & | trimEnd () noexcept |
Remove any whitespace from the end of this StringView's contents. | |
StringView & | trim () noexcept |
Remove any whitespace from the start and end of this StringView's contents. | |
Static Public Attributes | |
static constexpr size_t | npos = static_cast<size_t>(-1) |
No position. | |
static constexpr size_t | NoPosition = npos |
No position. | |
Private Types | |
using | traits_type = std::char_traits< char > |
Private Member Functions | |
int | compareMemory (const char *a, const char *b, size_t length) const |
Compare two memory sequences. | |
Static Private Member Functions | |
static bool | isWhiteSpace (char c) |
Helper function that tests whether the given character is an ASCII whitespace character. | |
Private Attributes | |
const char * | str = nullptr |
Pointer to the string this view is over. | |
size_t | strSize = 0 |
Length of the string data pointed to. | |
Provides a weakly referenced view over the contents of a string.
E.g. same usage rules as std::string_view
No ownership is assumed over the given string, making the valid lifetime of a StringView bound to the lifetime of the string data it is constructed from. Therefore, it is usually not safe to store instances of StringView in objects, except when it can be reasonably guaranteed that the strings in question are literals or otherwise exists in the StringView instance usage lifetime.
using Cogs::StringView::const_iterator = const char * |
Const iterator type.
using Cogs::StringView::iterator = const char * |
Iterator type.
|
private |
using Cogs::StringView::value_type = char |
Value type.
Vector of strings type.
|
constexprdefaultnoexcept |
Constructs an empty string view.
|
inlineconstexprnoexcept |
Construct a string view of the given null-terminated string.
The string length is automatically calculated from the string data.
data | Pointer to string data in memory. |
|
inlineconstexprnoexcept |
Construct a string view from the given string literal.
data | Pointer to the string literal. |
|
inlineconstexprnoexcept |
Construct a string view of the given string data, with the given length.
data | Pointer to string data in memory. |
length | Length of the string data, not including null-termination, if any. |
|
inlineconstexprnoexcept |
Construct a String view of the given std::string_view object.
s | string_view to create a view of. |
|
noexcept |
Construct a string view of the given string object.
s | String to create a view of. |
|
inlineconstexprnoexcept |
Const iterator to the beginning of the string.
|
inlineconstexprnoexcept |
Iterator to the beginning of the string.
|
inlineconstexprnoexcept |
Const iterator to the beginning of the string.
|
inlineconstexprnoexcept |
Const iterator to the end of the string.
|
noexcept |
Lexicographically compares the content of this string view with the given other.
other | StringView instance to compare to. |
|
inlineprivate |
Compare two memory sequences.
|
inlineconstexprnoexcept |
Get the sequence of characters referenced by the string view.
Note: the data is not necessarily null-terminated, and may even contain null characters. To get a null-terminated string, use to_string.
Referenced by Cogs::ImmutableString::ImmutableString(), and Cogs::Memory::MemoryBuffer::writeString().
|
inlineconstexprnoexcept |
Check if the string is empty.
Referenced by Cogs::Reflection::TypeDatabase::createType(), and Cogs::ImmutableString::ImmutableString().
|
inlineconstexprnoexcept |
Const iterator to the end of the string.
|
inlineconstexprnoexcept |
Iterator to the end of the string.
|
noexcept |
Find the given string segment inside the string.
Starts the search at character offset.
|
noexcept |
Find the first occurance of the given character from the specified starting position.
Referenced by split(), and splitAll().
|
noexcept |
See std::basic_string::find_last_of.
References Cogs::N.
|
noexcept |
See std::basic_string::find_last_of.
References Cogs::N.
|
inlineconstexprnoexcept |
|
inlineconstexprnoexcept |
Combine the hash code of the string with the initial hash value provided.
Deprecated. Use Cogs::hash(string, hashValue)
References Cogs::hash().
|
noexcept |
Get the hash code of the string converted to lowercase.
Deprecated. Use Cogs::hashLowercase(string, hashValue)
Lowercase is here defined as default "C" locale, i.e., A-Z is converted to a-z.
References Cogs::hashLowercase().
|
inlinestaticprivate |
Helper function that tests whether the given character is an ASCII whitespace character.
Tests for tab (\t), line feed (
), carriage return (\r), and space ( ).
|
inlineconstexprnoexcept |
|
inlineexplicitconstexprnoexcept |
Explicit boolean conversion operator.
Provided for better check before use semantics.
|
inlineexplicit |
Explicit std::string conversion operator.
Provided to make it possible to cast a string view to a std::string.
|
inlineconstexprnoexcept |
Implicit std::string_view conversion operator.
Provided to make StringView and std::string_view buddies (transition to std::string_view).
|
inline |
Operator equal.
Compares this instance to the given StringView. To consider the string views equal, the length and content of the referenced strings must match exactly.
other | StringView instance to compare this instance to. |
|
inlineconstexprnoexcept |
Get the character at position index in the string.
|
inlineconstexprnoexcept |
Get the size of the string.
Equivalent to length().
Referenced by Cogs::ImmutableString::ImmutableString(), and Cogs::Memory::MemoryBuffer::writeString().
Cogs::StringView::Vector Cogs::StringView::split | ( | const StringView & | delimiters | ) | const |
Split this string into parts.
References find_first_of(), and Cogs::Collections::SmallVectorImplBase< T, isPod >::push_back().
Cogs::StringView::Vector Cogs::StringView::splitAll | ( | const StringView & | delimiters | ) | const |
Split this string into parts, empty parts will be retained.
References find_first_of(), and Cogs::Collections::SmallVectorImplBase< T, isPod >::push_back().
|
inlineconstexprnoexcept |
Get the given sub string.
std::string Cogs::StringView::to_string | ( | ) | const |
String conversion method.
Provided to match standard library API. Deprecated: Use std::string(StringViewObject) for portability when converting to std::string_view
|
inlineconstexprnoexcept |
Create a standard library string_view of the same view.
Deprecated: Use std::string_view(StringViewObject)
|
noexcept |
Remove any whitespace from the start and end of this StringView's contents.
References trimEnd().
|
noexcept |
Remove any whitespace from the end of this StringView's contents.
Referenced by trim().
|
noexcept |
Remove any whitespace from the start of this StringView's contents.
|
staticconstexpr |
No position.
|
staticconstexpr |
No position.
|
private |
Pointer to the string this view is over.
Referenced by operator==(), and to_string().
|
private |
Length of the string data pointed to.
Referenced by operator==(), and to_string().