HDK
|
A utility class to do read-only operations on a subset of an existing string. More...
#include <UT_StringView.h>
Public Member Functions | |
constexpr SYS_FORCE_INLINE | UT_StringView () |
Default constructor. Constructs an empty non-string. More... | |
constexpr SYS_FORCE_INLINE | UT_StringView (const char *str) |
Construct a string view from the entirety of a null-terminated string. More... | |
constexpr SYS_FORCE_INLINE | UT_StringView (const char *str, exint len) |
Construct a string view on a string of a given length. More... | |
SYS_FORCE_INLINE | UT_StringView (const char *start, const char *end) |
SYS_FORCE_INLINE | UT_StringView (const UT_StringView &o)=default |
Copy constructor. More... | |
UT_StringView (const UT_StringLit &s) | |
UT_StringView (const UT_StringRef &s) | |
UT_StringView (const UT_StringHolder &s) | |
UT_StringView (const UT_WorkBuffer &s) | |
SYS_FORCE_INLINE UT_StringView & | operator= (const UT_StringView &o)=default |
Assignment operator. More... | |
SYS_NO_DISCARD_RESULT bool | isFloat (bool skip_spaces=false, bool loose=false, bool allow_underscore=false) const |
Determine if string can be seen as a single floating point number. More... | |
SYS_NO_DISCARD_RESULT bool | isInteger (bool skip_spaces=false) const |
Determine if string can be seen as a single integer number. More... | |
SYS_NO_DISCARD_RESULT unsigned | hash () const |
Query functions | |
SYS_NO_DISCARD_RESULT SYS_FORCE_INLINE exint | length () const |
Returns the length of the string in bytes. More... | |
SYS_NO_DISCARD_RESULT SYS_FORCE_INLINE bool | isEmpty () const |
Returns true if the string is empty. More... | |
SYS_NO_DISCARD_RESULT SYS_FORCE_INLINE bool | isstring () const |
Returns true if the view points to a non-empty string. More... | |
SYS_NO_DISCARD_RESULT SYS_FORCE_INLINE SYS_SAFE_BOOL | operator bool () const |
Test whether the string is not an empty string (or nullptr) More... | |
SYS_NO_DISCARD_RESULT SYS_FORCE_INLINE int64 | getMemoryUsage (bool inclusive) const |
Returns the memory, in bytes, used by this object. More... | |
SYS_NO_DISCARD_RESULT SYS_FORCE_INLINE char | operator[] (exint i) const |
Returns the character at index i . No bounds checking is performed. More... | |
SYS_NO_DISCARD_RESULT SYS_FORCE_INLINE const char & | at (exint pos) const |
Returns the character at index i . More... | |
SYS_NO_DISCARD_RESULT SYS_FORCE_INLINE const char * | data () const noexcept |
Returns a pointer to the first character of a view. More... | |
SYS_NO_DISCARD_RESULT SYS_FORCE_INLINE const char & | front () const |
SYS_NO_DISCARD_RESULT SYS_FORCE_INLINE const char & | back () const |
SYS_NO_DISCARD_RESULT exint | findFirstOf (UT_StringView view, exint pos=0) const noexcept |
SYS_NO_DISCARD_RESULT exint | findFirstOf (char item, exint pos=0) const noexcept |
Returns the length of the string in bytes. More... | |
SYS_NO_DISCARD_RESULT exint | findFirstOf (const char *s, exint pos, exint count) const |
Returns the length of the string in bytes. More... | |
SYS_NO_DISCARD_RESULT exint | findFirstOf (const char *s, exint pos) const |
Returns the length of the string in bytes. More... | |
SYS_NO_DISCARD_RESULT exint | findLastOf (UT_StringView view, exint pos=npos) const noexcept |
SYS_NO_DISCARD_RESULT exint | findLastOf (char item, exint pos=npos) const noexcept |
Returns the length of the string in bytes. More... | |
SYS_NO_DISCARD_RESULT exint | findLastOf (const char *s, exint pos, exint count) const |
Returns the length of the string in bytes. More... | |
SYS_NO_DISCARD_RESULT exint | findLastOf (const char *s, exint pos=npos) const |
Returns the length of the string in bytes. More... | |
SYS_NO_DISCARD_RESULT exint | findFirstNotOf (UT_StringView view, exint pos=0) const noexcept |
SYS_NO_DISCARD_RESULT exint | findFirstNotOf (char item, exint pos=0) const noexcept |
Returns the length of the string in bytes. More... | |
SYS_NO_DISCARD_RESULT exint | findFirstNotOf (const char *s, exint pos, exint count) const |
Returns the length of the string in bytes. More... | |
SYS_NO_DISCARD_RESULT exint | findFirstNotOf (const char *s, exint pos) const |
Returns the length of the string in bytes. More... | |
SYS_NO_DISCARD_RESULT exint | findLastNotOf (UT_StringView view, exint pos=npos) const noexcept |
SYS_NO_DISCARD_RESULT exint | findLastNotOf (char item, exint pos=npos) const noexcept |
Returns the length of the string in bytes. More... | |
SYS_NO_DISCARD_RESULT exint | findLastNotOf (const char *s, exint pos, exint count) const |
Returns the length of the string in bytes. More... | |
SYS_NO_DISCARD_RESULT exint | findLastNotOf (const char *s, exint pos) const |
Returns the length of the string in bytes. More... | |
Manipulators | |
void | clear () |
SYS_NO_DISCARD_RESULT SYS_FORCE_INLINE UT_StringView | trim (const char *c=" \t\n\r") const |
SYS_NO_DISCARD_RESULT SYS_FORCE_INLINE UT_StringView | trimLeft (const char *c=" \t\n\r") const |
SYS_NO_DISCARD_RESULT SYS_FORCE_INLINE UT_StringView | trimRight (const char *c=" \t\n\r") const |
SYS_NO_DISCARD_RESULT UT_StringView | firstToken (const char *sep_chars=" \t\n\r") const |
Return the first token. More... | |
SYS_NO_DISCARD_RESULT UT_StringViewArray | tokenize (const char *sep_chars=" \t\n\r", bool group_separators=true) const |
SYS_NO_DISCARD_RESULT UT_StringViewArray | split (const char *sep_str=" ", int max_split=INT_MAX) const |
SYS_NO_DISCARD_RESULT UT_StringView | substr (exint index, exint length=SYS_EXINT_MAX) const |
void | removePrefix (exint n) |
void | removeSuffix (exint n) |
Operators | |
SYS_NO_DISCARD_RESULT int | compare (const char *str, exint str_len, bool case_sensitive=true) const |
SYS_NO_DISCARD_RESULT int | compare (const UT_StringView &sv, bool case_sensitive=true) const |
SYS_NO_DISCARD_RESULT SYS_FORCE_INLINE bool | equal (const char *str, bool case_sensitive=true) const |
SYS_NO_DISCARD_RESULT SYS_FORCE_INLINE bool | equal (const UT_StringView &other, bool case_sensitive=true) const |
SYS_NO_DISCARD_RESULT SYS_FORCE_INLINE bool | operator== (const char *str) const |
Returns true if the two strings compare as being equal. More... | |
SYS_NO_DISCARD_RESULT SYS_FORCE_INLINE bool | operator== (const UT_StringView &other) const |
SYS_NO_DISCARD_RESULT SYS_FORCE_INLINE bool | operator!= (const char *str) const |
Returns true if the two strings compare as being not equal. More... | |
SYS_NO_DISCARD_RESULT SYS_FORCE_INLINE bool | operator!= (const UT_StringView &other) const |
SYS_NO_DISCARD_RESULT SYS_FORCE_INLINE bool | operator< (const char *str) const |
SYS_NO_DISCARD_RESULT SYS_FORCE_INLINE bool | operator< (const UT_StringView &other) const |
const_iterator | find (char c, const_iterator start) const |
Find the first instance of the given character in this string. More... | |
SYS_NO_DISCARD_RESULT SYS_FORCE_INLINE const_iterator | find (char c) const |
const_iterator | find (const char *str, const_iterator start) const |
Find the first instance of the given substring in this string. More... | |
SYS_NO_DISCARD_RESULT SYS_FORCE_INLINE const_iterator | find (const char *str) const |
SYS_NO_DISCARD_RESULT const_iterator | rfind (char c, const_iterator pos) const |
SYS_NO_DISCARD_RESULT SYS_FORCE_INLINE const_iterator | rfind (char c) const |
SYS_NO_DISCARD_RESULT bool | startsWith (const char *prefix, bool case_sensitive=true, exint len=-1) const |
Returns true if our string starts with the specified prefix. More... | |
SYS_NO_DISCARD_RESULT bool | endsWith (const char *prefix, bool case_sensitive=true, exint len=-1) const |
Static Public Attributes | |
static constexpr exint | npos = std::numeric_limits<exint>::max() |
Iterators | |
typedef const char * | const_iterator |
The iterator type. UT_StringView only provides read-only iterators. More... | |
SYS_NO_DISCARD_RESULT SYS_FORCE_INLINE const_iterator | begin () const |
Returns a constant iterator pointing to the beginning of the string. More... | |
SYS_NO_DISCARD_RESULT SYS_FORCE_INLINE const_iterator | end () const |
Returns a constant iterator pointing to the end of the string. More... | |
A utility class to do read-only operations on a subset of an existing string.
Definition at line 39 of file UT_StringView.h.
typedef const char* UT_StringView::const_iterator |
The iterator type. UT_StringView only provides read-only iterators.
Definition at line 252 of file UT_StringView.h.
|
inline |
Default constructor. Constructs an empty non-string.
Definition at line 44 of file UT_StringView.h.
|
inline |
Construct a string view from the entirety of a null-terminated string.
Definition at line 51 of file UT_StringView.h.
|
inlineexplicit |
Construct a string view on a string of a given length.
Definition at line 61 of file UT_StringView.h.
|
inlineexplicit |
Construct a string view on a given string range. The end
pointer should point to one past the end of the string (i.e. in the case of null terminated strings, it should point at the null character).
Definition at line 74 of file UT_StringView.h.
|
default |
Copy constructor.
UT_StringView::UT_StringView | ( | const UT_StringLit & | s | ) |
UT_StringView::UT_StringView | ( | const UT_StringRef & | s | ) |
UT_StringView::UT_StringView | ( | const UT_StringHolder & | s | ) |
UT_StringView::UT_StringView | ( | const UT_WorkBuffer & | s | ) |
|
inline |
Returns the character at index i
.
Definition at line 127 of file UT_StringView.h.
|
inline |
Returns a constant reference to the last character in the view. Undefined behaviour if the view is empty.
Definition at line 149 of file UT_StringView.h.
|
inline |
Returns a constant iterator pointing to the beginning of the string.
Definition at line 256 of file UT_StringView.h.
Clears the string. After this operation isstring will return false, and isEmpty will return true.
Definition at line 268 of file UT_StringView.h.
SYS_NO_DISCARD_RESULT int UT_StringView::compare | ( | const char * | str, |
exint | str_len, | ||
bool | case_sensitive = true |
||
) | const |
The compare function compares this string with another, and returns and integer less than, equal to, or greater than zero if this string is found to be less than, equal to, or greater than the given string, respectively. If a length is given, then the strings are compared as if they were both of that length, or smaller.
SYS_NO_DISCARD_RESULT int UT_StringView::compare | ( | const UT_StringView & | sv, |
bool | case_sensitive = true |
||
) | const |
The compare function compares this string with another, and returns and integer less than, equal to, or greater than zero if this string is found to be less than, equal to, or greater than the given string, respectively. If a length is given, then the strings are compared as if they were both of that length, or smaller.
|
inlinenoexcept |
Returns a pointer to the first character of a view.
Definition at line 136 of file UT_StringView.h.
|
inline |
Returns a constant iterator pointing to the end of the string.
Definition at line 260 of file UT_StringView.h.
SYS_NO_DISCARD_RESULT bool UT_StringView::endsWith | ( | const char * | prefix, |
bool | case_sensitive = true , |
||
exint | len = -1 |
||
) | const |
The compare function compares this string with another, and returns and integer less than, equal to, or greater than zero if this string is found to be less than, equal to, or greater than the given string, respectively. If a length is given, then the strings are compared as if they were both of that length, or smaller.
|
inline |
Returns true if the two strings compare as being equal. If case_sensitive
is set to false
then the strings are compared in a case-insensitive fashion.
Definition at line 344 of file UT_StringView.h.
|
inline |
The compare function compares this string with another, and returns and integer less than, equal to, or greater than zero if this string is found to be less than, equal to, or greater than the given string, respectively. If a length is given, then the strings are compared as if they were both of that length, or smaller.
Definition at line 348 of file UT_StringView.h.
const_iterator UT_StringView::find | ( | char | c, |
const_iterator | start | ||
) | const |
Find the first instance of the given character in this string.
|
inline |
The compare function compares this string with another, and returns and integer less than, equal to, or greater than zero if this string is found to be less than, equal to, or greater than the given string, respectively. If a length is given, then the strings are compared as if they were both of that length, or smaller.
Definition at line 380 of file UT_StringView.h.
const_iterator UT_StringView::find | ( | const char * | str, |
const_iterator | start | ||
) | const |
Find the first instance of the given substring in this string.
|
inline |
The compare function compares this string with another, and returns and integer less than, equal to, or greater than zero if this string is found to be less than, equal to, or greater than the given string, respectively. If a length is given, then the strings are compared as if they were both of that length, or smaller.
Definition at line 386 of file UT_StringView.h.
|
noexcept |
Find the first character not equal to any of the characters in the given character sequence.
|
inlinenoexcept |
Returns the length of the string in bytes.
Definition at line 206 of file UT_StringView.h.
|
inline |
Returns the length of the string in bytes.
Definition at line 219 of file UT_StringView.h.
|
inline |
Returns the length of the string in bytes.
Definition at line 223 of file UT_StringView.h.
|
noexcept |
Find first character equal to any of the characters in the given character sequence.
|
inlinenoexcept |
Returns the length of the string in bytes.
Definition at line 161 of file UT_StringView.h.
|
inline |
Returns the length of the string in bytes.
Definition at line 173 of file UT_StringView.h.
|
inline |
Returns the length of the string in bytes.
Definition at line 177 of file UT_StringView.h.
|
noexcept |
Find the last character not equal to any of the characters in the given character sequence.
|
inlinenoexcept |
Returns the length of the string in bytes.
Definition at line 232 of file UT_StringView.h.
|
inline |
Returns the length of the string in bytes.
Definition at line 238 of file UT_StringView.h.
|
inline |
Returns the length of the string in bytes.
Definition at line 242 of file UT_StringView.h.
|
noexcept |
Find the last character equal to any of the characters in the viven character sequence.
|
inlinenoexcept |
Returns the length of the string in bytes.
Definition at line 186 of file UT_StringView.h.
|
inline |
Returns the length of the string in bytes.
Definition at line 192 of file UT_StringView.h.
|
inline |
Returns the length of the string in bytes.
Definition at line 197 of file UT_StringView.h.
SYS_NO_DISCARD_RESULT UT_StringView UT_StringView::firstToken | ( | const char * | sep_chars = " \t\n\r" | ) | const |
Return the first token.
|
inline |
Returns a constant reference to the first character in the view. Undefined behaviour if view is empty.
Definition at line 140 of file UT_StringView.h.
|
inline |
Returns the memory, in bytes, used by this object.
Definition at line 119 of file UT_StringView.h.
|
inline |
Definition at line 417 of file UT_StringView.h.
|
inline |
Returns true
if the string is empty.
Definition at line 107 of file UT_StringView.h.
SYS_NO_DISCARD_RESULT bool UT_StringView::isFloat | ( | bool | skip_spaces = false , |
bool | loose = false , |
||
bool | allow_underscore = false |
||
) | const |
Determine if string can be seen as a single floating point number.
SYS_NO_DISCARD_RESULT bool UT_StringView::isInteger | ( | bool | skip_spaces = false | ) | const |
Determine if string can be seen as a single integer number.
|
inline |
Returns true
if the view points to a non-empty string.
Definition at line 111 of file UT_StringView.h.
|
inline |
Returns the length of the string in bytes.
Definition at line 103 of file UT_StringView.h.
|
inline |
Test whether the string is not an empty string (or nullptr)
Definition at line 115 of file UT_StringView.h.
|
inline |
Returns true if the two strings compare as being not equal.
Definition at line 362 of file UT_StringView.h.
|
inline |
The compare function compares this string with another, and returns and integer less than, equal to, or greater than zero if this string is found to be less than, equal to, or greater than the given string, respectively. If a length is given, then the strings are compared as if they were both of that length, or smaller.
Definition at line 365 of file UT_StringView.h.
|
inline |
Returns true if this string is lexicographically less than the given string.
Definition at line 371 of file UT_StringView.h.
|
inline |
The compare function compares this string with another, and returns and integer less than, equal to, or greater than zero if this string is found to be less than, equal to, or greater than the given string, respectively. If a length is given, then the strings are compared as if they were both of that length, or smaller.
Definition at line 374 of file UT_StringView.h.
|
default |
Assignment operator.
|
inline |
Returns true if the two strings compare as being equal.
Definition at line 354 of file UT_StringView.h.
|
inline |
The compare function compares this string with another, and returns and integer less than, equal to, or greater than zero if this string is found to be less than, equal to, or greater than the given string, respectively. If a length is given, then the strings are compared as if they were both of that length, or smaller.
Definition at line 357 of file UT_StringView.h.
|
inline |
Returns the character at index i
. No bounds checking is performed.
Definition at line 124 of file UT_StringView.h.
Move the start of the view forward by n characters. If N is grreater then length() then it moves to the end.
Move the end of the view backwards by n characters. If n is greater then the length() then it moves to the start.
SYS_NO_DISCARD_RESULT const_iterator UT_StringView::rfind | ( | char | c, |
const_iterator | pos | ||
) | const |
Find last instance of the given character in this string, searching backwards from 'pos'.
|
inline |
The compare function compares this string with another, and returns and integer less than, equal to, or greater than zero if this string is found to be less than, equal to, or greater than the given string, respectively. If a length is given, then the strings are compared as if they were both of that length, or smaller.
Definition at line 394 of file UT_StringView.h.
SYS_NO_DISCARD_RESULT UT_StringViewArray UT_StringView::split | ( | const char * | sep_str = " " , |
int | max_split = INT_MAX |
||
) | const |
Splits the string into a list of words, using sep_str as the separator string. Unlike tokenize, consecutive delimiters are not grouped together and are instead taken to delimit empty strings. If max_split
is set, the string is split into at most max_sep
pieces.
SYS_NO_DISCARD_RESULT bool UT_StringView::startsWith | ( | const char * | prefix, |
bool | case_sensitive = true , |
||
exint | len = -1 |
||
) | const |
Returns true if our string starts with the specified prefix.
SYS_NO_DISCARD_RESULT UT_StringView UT_StringView::substr | ( | exint | index, |
exint | length = SYS_EXINT_MAX |
||
) | const |
Returns a sub-string of the current string. If positive, the index
parameter is relative to the start. If negative, it's relative to the end (e.g. substr(-1,1) will return the last character of the string). The empty string will be returned for out-of-range values.
SYS_NO_DISCARD_RESULT UT_StringViewArray UT_StringView::tokenize | ( | const char * | sep_chars = " \t\n\r" , |
bool | group_separators = true |
||
) | const |
Splits the string into individual tokens, separated by one or more of the sep
characters given. If group_separators
is false, the delimiters are not grouped together, e.g., ",," => ('', '', ''), which is equivalent of Python's split().
|
inline |
Trim characters from the left- and right-hand side of the string. By default this will trim the ASCII space characters.
Definition at line 273 of file UT_StringView.h.
|
inline |
Trim characters from the left-hand side of the string. By default this will trim the ASCII space characters.
Definition at line 279 of file UT_StringView.h.
|
inline |
Trim characters from the right-hand side of the string. By default this will trim the ASCII space characters.
Definition at line 285 of file UT_StringView.h.
The exact meaning depends on context, but generally used either as end of view indicator by the functions that expect a view index or as the error indicator by the functions that return a view index.
Definition at line 96 of file UT_StringView.h.