A lightweight wrapper around a sequence of Unicode characters.
This class wraps a sequence of Unicode characters without copying them. Many types such as std::string, const char* and std::vector<char> can implicitely be converted to this type. This allows those types to be used without copying in functions that would normally require those types to be converted.
None of the constructors make a copy of the underlying data.
Because string views only consist of a pointer and a size, instances of CYIStringView are cheap to copy and are better passed by value than by reference.
None of the functions of CYIStringView modify the underlying character data – either the string view itself is modified (e.g. the Trim functions) or a CYIString is returned (which makes a copy of the data).
The char constructors assume that the incoming data is encoded in UTF-8 already. For performance reasons, this assumption is not checked during the constructor. If invalid data (or non-UTF-8) data is provided in one of the constructors, usage of some functions may result in undefined behaviour.
Some of the functions in this class accept a parameter to enable case-insensitive searching and comparing. However, there is no Unicode Normalization performed (and thus two string views that should be equal can be reported as not equal).
String views can be used to lookup values in a set or a map that uses CYIString as key, but the string view will be implicitely converted prior to doing the comparison. To avoid this, the set or map must use a transparent comparator such as std::less<void>
. For example:
std::map<CYIString, MyObject, std::less<void>> m_values;
#include <utility/YiStringView.h>
Classes | |
class | ConstIterator |
Public Types | |
enum | CaseMode { CaseMode::CaseSensitive, CaseMode::CaseInsensitiveASCII } |
typedef std::reverse_iterator< ConstIterator > | ConstReverseIterator |
typedef ConstIterator | Iterator |
typedef std::reverse_iterator< Iterator > | ReverseIterator |
Public Member Functions | |
YI_CONSTEXPR | CYIStringView () noexcept |
YI_CONSTEXPR | CYIStringView (const CYIStringView &other) noexcept |
YI_CONSTEXPR | CYIStringView (const char *pUTF8) noexcept |
YI_CONSTEXPR | CYIStringView (const char *pUTF8, size_t sizeBytes) noexcept |
CYIStringView (const ConstIterator &begin, const ConstIterator &end) | |
CYIStringView (const std::string &other) noexcept | |
CYIStringView (const NSString *pString) | |
CYIStringView (const std::vector< char > &vector) noexcept | |
CYIStringView (const std::vector< uint8_t > &vector) noexcept | |
YI_CONSTEXPR CYIStringView & | operator= (const CYIStringView &view) noexcept=default |
YI_CONSTEXPR bool | IsEmpty () const |
YI_CONSTEXPR bool | IsNotEmpty () const |
YI_CONSTEXPR const char * | GetRawData () const noexcept |
YI_CONSTEXPR size_t | GetSizeInBytes () const noexcept |
size_t | GetLength () const |
YI_CONSTEXPR char | ByteAt (size_t byteOffset) const noexcept |
char32_t | At (size_t pos) const |
char32_t | operator[] (size_t pos) const |
bool | ContainsNonASCII () const |
bool | IsNumeric () const |
ConstIterator | begin () const |
ConstIterator | end () const |
ConstReverseIterator | rbegin () const |
ConstReverseIterator | rend () const |
bool | Contains (CYIStringView s, CaseMode caseSensitivity=CaseMode::CaseSensitive) const |
bool | StartsWith (CYIStringView s, CaseMode caseSensitivity=CaseMode::CaseSensitive) const |
bool | EndsWith (CYIStringView s, CaseMode caseSensitivity=CaseMode::CaseSensitive) const |
int32_t | Compare (CYIStringView other, CaseMode caseSensitivity=CaseMode::CaseSensitive) const noexcept |
bool | Equals (CYIStringView other, CaseMode caseSensitivity=CaseMode::CaseSensitive) const noexcept |
bool | EqualsCaseInsensitiveASCII (CYIStringView other) const noexcept |
CYISmallVector< CYIStringView, 3 > | Split (CYIStringView delimiter, bool ignoreEmptySubstrings=true, size_t maximumParts=NPos) const |
CYIStringView | SubStr (size_t pos=0, size_t len=NPos) const |
CYIStringView | SubStr (const ConstIterator &begin, const ConstIterator &end) const |
CYIStringView | Left (size_t len) const |
CYIStringView | Right (size_t len) const |
CYIString | ToUpperASCII () const |
CYIString | ToLowerASCII () const |
size_t | IndexOf (CYIStringView view, size_t pos=0) const |
size_t | LastIndexOf (CYIStringView view, size_t pos=NPos) const |
ConstIterator | Find (CYIStringView view, const ConstIterator &begin) const |
ConstReverseIterator | ReverseFind (CYIStringView view, const ConstReverseIterator &begin) const |
CYIString | Arg (CYIStringView text) const |
void | Clear () |
void | Swap (CYIStringView &view) |
void | Trim () |
void | TrimLeft (size_t len=NPos) |
void | TrimRight (size_t len=NPos) |
void | RemovePrefix (size_t len) |
bool | RemovePrefix (CYIStringView prefix, CaseMode caseSensitivity=CaseMode::CaseSensitive) |
void | RemoveSuffix (size_t len) |
bool | RemoveSuffix (CYIStringView prefix, CaseMode caseSensitivity=CaseMode::CaseSensitive) |
std::unique_ptr< char[]> | ToUTF8 (size_t *pElementsCount=nullptr) const |
std::unique_ptr< char16_t[]> | ToUTF16 (size_t *pElementsCount=nullptr) const |
std::unique_ptr< char32_t[]> | ToUTF32 (size_t *pElementsCount=nullptr) const |
std::unique_ptr< wchar_t[]> | ToWideChars (size_t *pElementsCount=nullptr) const |
CYIString | ToCYIString () const |
std::string | ToStdString () const |
std::wstring | ToStdWString () const |
NSString * | ToNSString () const |
CFStringRef | ToCFStringRef () const |
template<class T , typename S = std::istream, typename B = std::streambuf> | |
T | To (bool *pError=nullptr) const |
template<class T > | |
T | ToValue (bool *pError=nullptr) const |
float | ToFloat (bool *pError=nullptr) const |
double | ToDouble (bool *pError=nullptr) const |
bool | ToBool (bool *pError=nullptr) const |
CYIString | ToHexadecimal () const |
Static Public Attributes | |
static constexpr size_t | NPos = ~size_t(0) |
typedef std::reverse_iterator<ConstIterator> CYIStringView::ConstReverseIterator |
typedef ConstIterator CYIStringView::Iterator |
typedef std::reverse_iterator<Iterator> CYIStringView::ReverseIterator |
|
strong |
An enum used to specify a case-sensitivity handling mode.
|
noexcept |
Creates an empty string view.
|
noexcept |
Copies a string view. Both the new string view and the original string view will be backed by the same data.
|
noexcept |
Constructs a string view from a UTF-8 encoded array pUTF8, which must be null terminated; providing nullptr instead results in an empty string view.
|
noexcept |
Constructs a string view from a UTF-8 encoded array pUTF8; providing nullptr instead results in an empty string view. The sizeBytes parameter determines the number of bytes to read from the array.
CYIStringView::CYIStringView | ( | const ConstIterator & | begin, |
const ConstIterator & | end | ||
) |
Constructs a string from a pair of CYIStringView iterators.
|
noexcept |
Constructs a string view from a std::string type other. The resulting string view will refer to the std::string's underlying data.
CYIStringView::CYIStringView | ( | const NSString * | pString | ) |
Constructs a string view from an NSString Objective-C object. Passing nil to this constructor will result in an empty string being created.
|
noexcept |
Constructs a string view from a UTF-8 encoded vector of char's vector. The resulting string view will refer to the std::vector's contained data.
|
noexcept |
Constructs a string view from a UTF-8 encoded vector of uint8_t's vector. The resulting string view will refer to the std::vector's contained data.
CYIString CYIStringView::Arg | ( | CYIStringView | text | ) | const |
Returns a new CYIString with the lowest-numbered 'marker' replaced with the string view text.
Markers are expected to be in the format xx, with xx being a number between 1 and 99 inclusively. Out-of-range values will result in undefined behaviour. Markers are not required to occur in order in the string. The same marker can be used multiple times within the string, which will result in all of those markers being replaced by the same argument.
Example: CYIStringView("%2, %1! %2!").Arg("World").Arg("Hello") would return "Hello, World! Hello!"
char32_t CYIStringView::At | ( | size_t | pos | ) | const |
Returns the 32-bit Unicode value at that character position specified by pos. The position must be a valid index position in the string; otherwise, a value of 0 is returned.
ConstIterator CYIStringView::begin | ( | ) | const |
Returns the iterator to the beginning of the string view. This iterator iterates over Unicode characters rather than over bytes.
|
noexcept |
Returns the byte value at the position byteOffset. If byteOffset is larger than the size (in bytes) of the string, 0 is returned.
void CYIStringView::Clear | ( | ) |
Empties the string view and resets the length to 0. This is equivalent to assigning an empty string view to this object. The backing data is unaffected.
|
noexcept |
Compares this string view with other. Returns a negative value if this string view is less than other, zero if the two string views are equal and a positive value if this string view is greater than other.
If a case-insenstive compare is needed, pass CYIStringView::CaseMode::CaseInsensitiveASCII to the caseSensitivity parameter.
bool CYIStringView::Contains | ( | CYIStringView | s, |
CaseMode | caseSensitivity = CaseMode::CaseSensitive |
||
) | const |
Returns true if the string view contains s as a sub-string. An empty string will be contained in any other string.
If a case-insensitive compare is needed, pass CYIStringView::CaseMode::CaseInsensitiveASCII to the caseSensitivity parameter.
bool CYIStringView::ContainsNonASCII | ( | ) | const |
Returns true if the string view contains non-ASCII characters.
ConstIterator CYIStringView::end | ( | ) | const |
Returns the iterator to the end of the string view. This iterator iterates over Unicode characters rather than over bytes.
bool CYIStringView::EndsWith | ( | CYIStringView | s, |
CaseMode | caseSensitivity = CaseMode::CaseSensitive |
||
) | const |
Returns true if the string view ends with the sub-string s.
If a case-insensitive compare is needed, pass CYIStringView::CaseMode::CaseInsensitiveASCII to the caseSensitivity parameter.
|
noexcept |
Returns true if this string view is equal to other.
If a case-insenstive compare is needed, pass CYIStringView::CaseMode::CaseInsensitiveASCII to the caseSensitivity parameter.
|
noexcept |
Returns true if this string view is equal to other, ignoring case differences on ASCII characters.
ConstIterator CYIStringView::Find | ( | CYIStringView | view, |
const ConstIterator & | begin | ||
) | const |
Returns the iterator pointing to the next occurrence of view within this string view, starting at position begin. Returns End() if view is not found.
size_t CYIStringView::GetLength | ( | ) | const |
Returns the number of Unicode characters present in the string view.
|
noexcept |
Returns the raw bytes that back this string view. While internally the string view is expected to be encoded in UTF-8, for forward compatibility this should not be relied upon.
|
noexcept |
Returns the length of the string, in bytes.
size_t CYIStringView::IndexOf | ( | CYIStringView | view, |
size_t | pos = 0 |
||
) | const |
Returns the character index of the sub-string view within this string view; the function returns CYIStringView::NPos if the sub-string was not found.
If pos is provided, the search will start at the provided character index.
YI_CONSTEXPR bool CYIStringView::IsEmpty | ( | ) | const |
Returns true if the string view's length is 0, false otherwise.
YI_CONSTEXPR bool CYIStringView::IsNotEmpty | ( | ) | const |
Returns true if the string view's length is greater than 0, false otherwise.
bool CYIStringView::IsNumeric | ( | ) | const |
Returns true if all characters in this string view are ASCII digits. Returns false if this string view is empty.
size_t CYIStringView::LastIndexOf | ( | CYIStringView | view, |
size_t | pos = NPos |
||
) | const |
Returns the character index of the sub-string view within this string view, starting at the end and searching in reverse; the function returns CYIStringView::NPos if the sub-string was not found.
If pos is provided, the search will start at the provided character index instead of at the end of the string view. In that case, pos would be the position of the last character that would be considered for the beginning of the string view view.
CYIStringView CYIStringView::Left | ( | size_t | len | ) | const |
Returns a string view containg the first len characters of this string view. If len is larger than the length of this string view, the full string view is returned.
|
defaultnoexcept |
Assigns a copy of s to this string view. Both this string and view will refer to the same data.
char32_t CYIStringView::operator[] | ( | size_t | pos | ) | const |
Returns the 32-bit Unicode value at that character position specified by pos. Cannot be modified. The position must be a valid index position in the string; otherwise, a value of 0 is returned.
ConstReverseIterator CYIStringView::rbegin | ( | ) | const |
Returns the reverse iterator to the end of the string view. This iterator iterates over Unicode characters rather than over bytes.
void CYIStringView::RemovePrefix | ( | size_t | len | ) |
Removes the first len characters from this string view. If there are fewer than len characters in the string view, the string view becomes empty.
bool CYIStringView::RemovePrefix | ( | CYIStringView | prefix, |
CaseMode | caseSensitivity = CaseMode::CaseSensitive |
||
) |
Removes prefix from the beginning of this string view if this string view begins with prefix. Otherwise, this function does nothing. Returns true if prefix was found.
If a case-insensitive compare is needed, pass CaseMode::CaseInsensitiveASCII to the caseSensitivity parameter.
void CYIStringView::RemoveSuffix | ( | size_t | len | ) |
Removes the last len characters from this string view. If there are fewer than len characters in the string view, the string view becomes empty.
bool CYIStringView::RemoveSuffix | ( | CYIStringView | prefix, |
CaseMode | caseSensitivity = CaseMode::CaseSensitive |
||
) |
Removes suffix from the end of this string view if this string view ends with suffix. Otherwise, this function does nothing. Returns true if suffix was found.
If a case-insensitive compare is needed, pass CaseMode::CaseInsensitiveASCII to the caseSensitivity parameter.
ConstReverseIterator CYIStringView::rend | ( | ) | const |
Returns the reverse iterator to the beginning of the string view. This iterator iterates over Unicode characters rather than over bytes.
ConstReverseIterator CYIStringView::ReverseFind | ( | CYIStringView | view, |
const ConstReverseIterator & | begin | ||
) | const |
Returns the iterator pointing to the 'next' occurrence of view within this string view, starting at position begin. Since this function works on reverse iterators, the 'next' occurrence is really the previous occurrence within the string view. Returns REnd() if view is not found.
CYIStringView CYIStringView::Right | ( | size_t | len | ) | const |
Returns a string view containg the last len characters of this string view. If len is larger than the length of this string view, the full string view is returned.
CYISmallVector<CYIStringView, 3> CYIStringView::Split | ( | CYIStringView | delimiter, |
bool | ignoreEmptySubstrings = true , |
||
size_t | maximumParts = NPos |
||
) | const |
Splits the current string view into a vector of string views whenever the delimeter occurs in the string view.
If ignoreEmptySubstrings is true, 'empty' substrings are not included in the returned vector.
The maximumParts parameter can be used to specify the maximum number of elements to return in the vector. When more than ( maximumParts - 1 ) parts are found during splitting, the remaining parts will be left as is and appended to the vector. For example, calling CYIStringView("foo;bar;baz").Split(";", true, 2) would result in a vector containing 'foo' and 'bar;baz' being returned. Providing a value of 0 to maximumParts will result in an empty vector being returned, and providing a value of 1 will result in the original string view being returned as the sole element in the vector. When ignoreEmptySubstrings is true, 'empty' substrings do not count towards the parts count. A value of CYIStringView::NPos indicates that there is no parts limit.
bool CYIStringView::StartsWith | ( | CYIStringView | s, |
CaseMode | caseSensitivity = CaseMode::CaseSensitive |
||
) | const |
Returns true if the string view starts with the sub-string s.
If a case-insensitive compare is needed, pass CYIStringView::CaseMode::CaseInsensitiveASCII to the caseSensitivity parameter.
CYIStringView CYIStringView::SubStr | ( | size_t | pos = 0 , |
size_t | len = NPos |
||
) | const |
Returns the sub-string of the current string view from character position pos for len characters. A len value of CYIStringView::NPos indicates that the sub-string from pos to the end of the string view should be returned. Returns an empty string view in the case of invalid pos or len values.
CYIStringView CYIStringView::SubStr | ( | const ConstIterator & | begin, |
const ConstIterator & | end | ||
) | const |
Returns the sub-string of the current string view from iterator begin to iterator end.
void CYIStringView::Swap | ( | CYIStringView & | view | ) |
Swaps the contents of the string view with the contents of view. The backing data is unaffected.
T CYIStringView::To | ( | bool * | pError = nullptr | ) | const |
Converts this string view to the specified template type T. Custom types are supported so long as they have an implementation of operator>> .
If a conversion error occurs, an object created with the default constructor is returned. A pointer to a boolean, pError, can optionally be provided – if a conversion error occurs, its pointed-to boolean will be set to true. If no conversion error occurs, the pointed-to boolean will be set to false.
sstream
header must be included.Sample usage:
bool CYIStringView::ToBool | ( | bool * | pError = nullptr | ) | const |
Interprets the provided string view as a boolean. The following string values will result in a value of true being returned:
The following string view values will result in a value of false being returned:
All string view values are case-insensitive. Leading and trailing spaces are ignored.
Any other string view value will be considered a conversion error and will result in a value of false being returned. A pointer to a boolean, pError, can optionally be provided – if a conversion error occurs, its pointed-to boolean will be set to true. If no conversion error occurs, the pointed-to boolean will be set to false.
CFStringRef CYIStringView::ToCFStringRef | ( | ) | const |
Returns the CFStringRef representation of this string view. The caller is responsible for deleting this resource (using CFRelease).
CYIString CYIStringView::ToCYIString | ( | ) | const |
Returns the CYIString representation fo this string view.
double CYIStringView::ToDouble | ( | bool * | pError = nullptr | ) | const |
Converts this string view to a double-precision floating point value.
If a conversion error occurs, 0.0 is returned. A pointer to a boolean, pError, can optionally be provided – if a conversion error occurs, its pointed-to boolean will be set to true. If no conversion error occurs, the pointed-to boolean will be set to false.
float CYIStringView::ToFloat | ( | bool * | pError = nullptr | ) | const |
Converts this string view to a single-precision floating point value.
If a conversion error occurs, 0.0f is returned. A pointer to a boolean, pError, can optionally be provided – if a conversion error occurs, its pointed-to boolean will be set to true. If no conversion error occurs, the pointed-to boolean will be set to false.
CYIString CYIStringView::ToHexadecimal | ( | ) | const |
Returns the hexadecimal representation of the raw bytes that back this string view. The returned characters are always in lowercase. Calling this function on an empty string view returns an empty string.
For example, calling this function on the string view "K" would return "4b", and calling this function on the string view "You.i" would return "596f752e69".
CYIString CYIStringView::ToLowerASCII | ( | ) | const |
Returns a lowercase version of this string view. The data backing this string view is not modified.
NSString* CYIStringView::ToNSString | ( | ) | const |
Returns the NSString representation of this string view.
std::string CYIStringView::ToStdString | ( | ) | const |
Returns the STL string representation of this string view.
std::wstring CYIStringView::ToStdWString | ( | ) | const |
Returns the STL wstring representation of this string view.
CYIString CYIStringView::ToUpperASCII | ( | ) | const |
Returns an uppercase version of this string view. The data backing this string view is not modified.
std::unique_ptr<char16_t[]> CYIStringView::ToUTF16 | ( | size_t * | pElementsCount = nullptr | ) | const |
Returns a new instance of the null-terminated UTF-16 representation of this string view.
If needed, the resulting number of char16_t elements in the returned array (excluding the null terminator) can be obtained by passing in a pointer to an size_t in pElementsCount.
std::unique_ptr<char32_t[]> CYIStringView::ToUTF32 | ( | size_t * | pElementsCount = nullptr | ) | const |
Returns a new instance of the null-terminated UTF-32 representation of this string view.
If needed, the resulting number of char32_t elements in the returned array (excluding the null terminator) can be obtained by passing in a pointer to an size_t in pElementsCount.
std::unique_ptr<char[]> CYIStringView::ToUTF8 | ( | size_t * | pElementsCount = nullptr | ) | const |
Returns a new instance of the null-terminated UTF-8 representation of this string view.
If needed, the resulting number of char elements in the returned array (excluding the null terminator) can be obtained by passing in a pointer to an size_t in pElementsCount.
T CYIStringView::ToValue | ( | bool * | pError = nullptr | ) | const |
Converts this string view to the specified template type T. Custom types are supported so long as they have an implementation of operator>> .
If a conversion error occurs, an object created with the default constructor is returned. A pointer to a boolean, pError, can optionally be provided – if a conversion error occurs, its pointed-to boolean will be set to true. If no conversion error occurs, the pointed-to boolean will be set to false.
sstream
header must be included.Sample usage:
std::unique_ptr<wchar_t[]> CYIStringView::ToWideChars | ( | size_t * | pElementsCount = nullptr | ) | const |
Returns a new instance of the null-terminated wchar_t respresentation for this string view.
If needed, the resulting number of wchar_t elements in the returned array (excluding the null terminator) can be obtained by passing in a pointer to an size_t pElementsCount.
void CYIStringView::Trim | ( | ) |
Trims whitespace at the beginning and at the end of the string view.
Trims whitespace at the beginning of the string view. The amount of removed whitespace can be limited using the len parameter. By default, all whitespace characters at the beginning of the string view are removed.
Trims whitespace at the end of the string view. The amount of removed whitespace can be limited using the len parameter. By default, all whitespace characters at the end of the string view are removed.
A constant whose meaning is context-dependent but usually means 'not found' or 'no limit'.