You.i Engine
CYIStringView Class Reference

Detailed Description

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).

Warning
Instances of this class do not copy the data they refer to, and thus it is important to ensure that the CYIStringView instances do not outlive the data that they are based on. For example, if a string view is created from an std::string then the string view can no longer be used once the std::string goes out of scope.
In most cases it is not safe to store instances of a string view in a member variable of a class. This may only be safely done if the lifetime of the underlying string view data can be guaranteed to be longer than that of the class instances.

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;

Warning
In most cases it is not safe to use instances of a string view as key in a set or a map. This may only be safely done if the lifetime of the underlying string view data can be guaranteed to be longer than that of the string view instances.
All case-insensitive functions in this class only ignore case on plain ASCII characters, even if the string view contains non-ASCII characters. If case-insensitivity is required on non-ASCII characters, use the functions in CYIStringICU.
Note
This class is unrelated to CYISceneView. To render text on the screen, use an instance of CYITextSceneNode or CYIScrollingTextView.
See also
CYIString

#include <utility/YiStringView.h>

Classes

class  ConstIterator
 

Public Types

enum  CaseMode {
  CaseMode::CaseSensitive,
  CaseMode::CaseInsensitiveASCII
}
 
typedef std::reverse_iterator< ConstIteratorConstReverseIterator
 
typedef ConstIterator Iterator
 
typedef std::reverse_iterator< IteratorReverseIterator
 

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 CYIStringViewoperator= (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>
To (bool *pError=nullptr) const
 
template<class 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)
 

Member Typedef Documentation

◆ ConstReverseIterator

typedef std::reverse_iterator<ConstIterator> CYIStringView::ConstReverseIterator

◆ Iterator

◆ ReverseIterator

typedef std::reverse_iterator<Iterator> CYIStringView::ReverseIterator

Member Enumeration Documentation

◆ CaseMode

An enum used to specify a case-sensitivity handling mode.

Note
For 'true' case-insensitive functions, see CYIStringICU.
Enumerator
CaseSensitive 

Searches and compares are performed in a case-sensitive way. This case mode is typically faster than other case modes.

CaseInsensitiveASCII 

Searches and compares are performed in a case-insensitive way. Only characters in the range [a-zA-Z] are compared in a case-insensitive manner.

Constructor & Destructor Documentation

◆ CYIStringView() [1/9]

YI_CONSTEXPR CYIStringView::CYIStringView ( )
noexcept

Creates an empty string view.

◆ CYIStringView() [2/9]

YI_CONSTEXPR CYIStringView::CYIStringView ( const CYIStringView other)
noexcept

Copies a string view. Both the new string view and the original string view will be backed by the same data.

◆ CYIStringView() [3/9]

YI_CONSTEXPR CYIStringView::CYIStringView ( const char *  pUTF8)
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.

Note
The data is assumed to be encoded with UTF-8. If the data is not encoded in UTF-8, the use of some of the CYIStringView functions will result in undefined behaviour.

◆ CYIStringView() [4/9]

YI_CONSTEXPR CYIStringView::CYIStringView ( const char *  pUTF8,
size_t  sizeBytes 
)
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.

Note
This constructor provides access to exactly sizeBytes bytes of the provided array, even if null elements are encountered. Consequently, the provided size should not include the terminating null character (if it exists).
The data is assumed to be encoded with UTF-8. If the data is not encoded in UTF-8, the use of some of the CYIStringView functions will result in undefined behaviour.

◆ CYIStringView() [5/9]

CYIStringView::CYIStringView ( const ConstIterator begin,
const ConstIterator end 
)

Constructs a string from a pair of CYIStringView iterators.

Warning
The two iterators must have been created from the same CYIStringView. Passing in two iterators created from different string views will result in undefined behaviour. Passing a begin iterator that is after the end iterator will also result in undefined behaviour.

◆ CYIStringView() [6/9]

CYIStringView::CYIStringView ( const std::string &  other)
noexcept

Constructs a string view from a std::string type other. The resulting string view will refer to the std::string's underlying data.

Note
The data in the std::string is assumed to be encoded with UTF-8. If the data is not encoded in UTF-8, the use of some of the CYIStringView functions will result in undefined behaviour.

◆ CYIStringView() [7/9]

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.

Note
Only available on Objective-C platforms.

◆ CYIStringView() [8/9]

CYIStringView::CYIStringView ( const std::vector< char > &  vector)
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.

Note
The data is assumed to be encoded with UTF-8. If the data is not encoded in UTF-8, the use of some of the CYIString functions will result in undefined behaviour.

◆ CYIStringView() [9/9]

CYIStringView::CYIStringView ( const std::vector< uint8_t > &  vector)
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.

Note
The data is assumed to be encoded with UTF-8. If the data is not encoded in UTF-8, the use of some of the CYIString functions will result in undefined behaviour.

Member Function Documentation

◆ Arg()

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!"

◆ At()

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.

Note
This function runs in O(n) time. When many (or all) characters need to be accessed, it is recommended to instead use iterators.

◆ begin()

ConstIterator CYIStringView::begin ( ) const

Returns the iterator to the beginning of the string view. This iterator iterates over Unicode characters rather than over bytes.

◆ ByteAt()

YI_CONSTEXPR char CYIStringView::ByteAt ( size_t  byteOffset) const
noexcept

Returns the byte value at the position byteOffset. If byteOffset is larger than the size (in bytes) of the string, 0 is returned.

Note
This function runs in O(1) time.
See also
GetSizeInBytes

◆ Clear()

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.

◆ Compare()

int32_t CYIStringView::Compare ( CYIStringView  other,
CaseMode  caseSensitivity = CaseMode::CaseSensitive 
) const
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.

Note
Comparison of non-ASCII characters is always done in a case-sensitive manner.
See also
CYIStringICU::CompareCaseInsensitive

◆ Contains()

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.

Note
Comparison of non-ASCII characters is always done in a case-sensitive manner.
See also
CYIStringICU::ContainsCaseInsensitive

◆ ContainsNonASCII()

bool CYIStringView::ContainsNonASCII ( ) const

Returns true if the string view contains non-ASCII characters.

Note
This function runs in O(n) time.

◆ end()

ConstIterator CYIStringView::end ( ) const

Returns the iterator to the end of the string view. This iterator iterates over Unicode characters rather than over bytes.

◆ EndsWith()

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.

Note
Empty strings are 'found' at the end of every string.
Comparison of non-ASCII characters is always done in a case-sensitive manner.
See also
CYIStringICU::EndsWithCaseInsensitive

◆ Equals()

bool CYIStringView::Equals ( CYIStringView  other,
CaseMode  caseSensitivity = CaseMode::CaseSensitive 
) const
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.

Note
Comparison of non-ASCII characters is always done in a case-sensitive manner.
See also
CYIStringICU::EqualsCaseInsensitive

◆ EqualsCaseInsensitiveASCII()

bool CYIStringView::EqualsCaseInsensitiveASCII ( CYIStringView  other) const
noexcept

Returns true if this string view is equal to other, ignoring case differences on ASCII characters.

Note
This is a convenience function that is equivalent to calling Equals with CYIStringView::CaseMode::CaseInsensitiveASCII.
Comparison of non-ASCII characters is always done in a case-sensitive manner.
See also
CYIStringView::Equals
CYIStringICU::EqualsCaseInsensitive

◆ Find()

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.

◆ GetLength()

size_t CYIStringView::GetLength ( ) const

Returns the number of Unicode characters present in the string view.

Note
This function runs in O(n) time.
The IsEmpty() function is preferred to check if the string view is empty, as that function always runs in O(1) time.

◆ GetRawData()

YI_CONSTEXPR const char* CYIStringView::GetRawData ( ) const
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.

Warning
Do not retain or delete this pointer.
Unlike CYIString::GetData(), the char array returned by this function is not null-terminated, and thus this function should always be used in conjunction with GetSizeInBytes().
Note
The returned pointer can only be dereferenced when GetSizeInBytes() returns a non-0 value.

◆ GetSizeInBytes()

YI_CONSTEXPR size_t CYIStringView::GetSizeInBytes ( ) const
noexcept

Returns the length of the string, in bytes.

Note
This function always runs in O(1) time.

◆ IndexOf()

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.

Note
A case-insensitive version of this function is not available because the length of the string view (and therefore the indicies) may be different after case conversion.

◆ IsEmpty()

YI_CONSTEXPR bool CYIStringView::IsEmpty ( ) const

Returns true if the string view's length is 0, false otherwise.

Note
This function runs in O(1) time.

◆ IsNotEmpty()

YI_CONSTEXPR bool CYIStringView::IsNotEmpty ( ) const

Returns true if the string view's length is greater than 0, false otherwise.

Note
This function runs in O(1) time.
See also
IsEmpty()

◆ IsNumeric()

bool CYIStringView::IsNumeric ( ) const

Returns true if all characters in this string view are ASCII digits. Returns false if this string view is empty.

Note
Negative and floating-point values are considered non-numeric and will result in false being returned.
This function runs in O(n) time.

◆ LastIndexOf()

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.

Note
A case-insensitive version of this function is not available because the length of the string view (and therefore the indicies) may be different after case conversion.

◆ Left()

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.

◆ operator=()

YI_CONSTEXPR CYIStringView& CYIStringView::operator= ( const CYIStringView view)
defaultnoexcept

Assigns a copy of s to this string view. Both this string and view will refer to the same data.

◆ operator[]()

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.

Note
This function runs in O(n) time. When many (or all) characters need to be accessed, it is recommended to instead use iterators.

◆ rbegin()

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.

◆ RemovePrefix() [1/2]

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.

◆ RemovePrefix() [2/2]

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.

Note
Comparison of non-ASCII characters is always done in a case-sensitive manner.

◆ RemoveSuffix() [1/2]

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.

◆ RemoveSuffix() [2/2]

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.

Note
Comparison of non-ASCII characters is always done in a case-sensitive manner.

◆ rend()

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.

◆ ReverseFind()

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.

◆ Right()

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.

◆ Split()

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.

◆ StartsWith()

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.

Note
Empty strings are 'found' at the beginning of every string.
Comparison of non-ASCII characters is always done in a case-sensitive manner.
See also
CYIStringICU::StartsWithCaseInsensitive

◆ SubStr() [1/2]

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.

◆ SubStr() [2/2]

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.

◆ Swap()

void CYIStringView::Swap ( CYIStringView view)

Swaps the contents of the string view with the contents of view. The backing data is unaffected.

◆ To()

template<class T , typename S = std::istream, typename B = std::streambuf>
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.

Note
When used with non-primitive types, the sstream header must be included.

Sample usage:

int32_t convertedValue = myStringView.To<int32_t>();

◆ ToBool()

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:

  • true
  • yes
  • 1
  • on
  • enabled
  • enable
  • active

The following string view values will result in a value of false being returned:

  • false
  • no
  • 0
  • off
  • disabled
  • disable
  • inactive

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.

Note
Empty string views will result in a value of false being returned, and is considered a conversion error.
Warning
The accepted values are not localized.

◆ ToCFStringRef()

CFStringRef CYIStringView::ToCFStringRef ( ) const

Returns the CFStringRef representation of this string view. The caller is responsible for deleting this resource (using CFRelease).

Note
Only available on Objective-C platforms.

◆ ToCYIString()

CYIString CYIStringView::ToCYIString ( ) const

Returns the CYIString representation fo this string view.

Note
CYIStringView's are implicitely convertible to CYIString, and so calling this function is usually unnecessary.

◆ ToDouble()

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.

◆ ToFloat()

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.

◆ ToHexadecimal()

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".

◆ ToLowerASCII()

CYIString CYIStringView::ToLowerASCII ( ) const

Returns a lowercase version of this string view. The data backing this string view is not modified.

Note
Only the ASCII characters within the string view are converted (e.g the range [A-Z]). Non-ASCII characters are left as-is.
See also
CYIStringICU::ToLower

◆ ToNSString()

NSString* CYIStringView::ToNSString ( ) const

Returns the NSString representation of this string view.

Note
Only available on Objective-C platforms.

◆ ToStdString()

std::string CYIStringView::ToStdString ( ) const

Returns the STL string representation of this string view.

Note
Unlike CYIString::ToStdString(), this function allocates a new std::string instance.

◆ ToStdWString()

std::wstring CYIStringView::ToStdWString ( ) const

Returns the STL wstring representation of this string view.

Note
The encoding of the wstring depends on the size of wchar_t: if wchar_t is 2 bytes wide, the data is encoded in UTF-16; otherwise, it is encoded in UTF-32.

◆ ToUpperASCII()

CYIString CYIStringView::ToUpperASCII ( ) const

Returns an uppercase version of this string view. The data backing this string view is not modified.

Note
Only the ASCII characters within the string view are converted (e.g the range [a-z]). Non-ASCII characters are left as-is.
See also
CYIStringICU::ToUpper

◆ ToUTF16()

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.

◆ ToUTF32()

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.

◆ ToUTF8()

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.

Note
The GetRawData() function can be used when a copy isn't required.
See also
GetRawData()

◆ ToValue()

template<class T >
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.

Note
When used with non-primitive types, the sstream header must be included.

Sample usage:

int32_t convertedValue = myStringView.To<int32_t>();
See also
To(bool *) const

◆ ToWideChars()

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.

Note
The encoding of the array depends on the size of wchar_t: if wchar_t is 2 bytes wide, the data is encoded in UTF-16; otherwise, it is encoded in UTF-32.

◆ Trim()

void CYIStringView::Trim ( )

Trims whitespace at the beginning and at the end of the string view.

◆ TrimLeft()

void CYIStringView::TrimLeft ( size_t  len = NPos)

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.

◆ TrimRight()

void CYIStringView::TrimRight ( size_t  len = NPos)

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.

Member Data Documentation

◆ NPos

constexpr size_t CYIStringView::NPos = ~size_t(0)
static

A constant whose meaning is context-dependent but usually means 'not found' or 'no limit'.


The documentation for this class was generated from the following file: