Non-owning view over a range of characters with UTF Encoding. More...
#include <StringView.h>
Public Types | |
enum class | Comparison { Smaller = -1 , Equals = 0 , Bigger = 1 } |
Result of ordering comparison done by StringView::compare. More... | |
Public Member Functions | |
constexpr | StringView () |
Construct an emtpy StringView. More... | |
constexpr | StringView (Span< const char > textSpan, bool nullTerm, StringEncoding encoding) |
Construct a StringView from a Span of bytes. More... | |
template<size_t N> | |
constexpr | StringView (const char(&text)[N]) |
Constructs a StringView with a null terminated string terminal. More... | |
template<size_t N> | |
constexpr | StringView (const wchar_t(&text)[N]) |
Constructs an UTF16 StringView with a null terminated wide-string terminal. More... | |
constexpr | StringView (Span< const wchar_t > textSpan, bool nullTerm) |
Construct an UTF16 StringView from a Span of bytes. More... | |
constexpr StringEncoding | getEncoding () const |
Get encoding of this StringView. More... | |
constexpr const char * | bytesWithoutTerminator () const |
Directly access the memory of this StringView. More... | |
constexpr const char * | bytesIncludingTerminator () const |
Directly access the memory of this null terminated-StringView. More... | |
auto | getNullTerminatedNative () const |
Directly access the memory of this null terminated-StringView. More... | |
constexpr Span< const char > | toCharSpan () const SC_LANGUAGE_LIFETIME_BOUND |
Obtain a const char Span from this StringView. More... | |
Span< const uint8_t > | toBytesSpan () const SC_LANGUAGE_LIFETIME_BOUND |
Obtain a const uint8_t Span from this StringView. More... | |
Comparison | compare (StringView other) const |
Ordering comparison between non-normalized StringView (operates on code points, not on utf graphemes) More... | |
bool | operator< (StringView other) const |
Ordering operator for StringView using StringView::compare. More... | |
template<typename Func > | |
constexpr auto | withIterator (Func &&func) const |
Call given lambda with one of StringIteratorASCII, StringIteratorUTF8, StringIteratorUTF16 depending on encoding. More... | |
template<typename StringIterator > | |
constexpr StringIterator | getIterator () const |
Returns a StringIterator from current StringView. More... | |
constexpr bool | operator!= (StringView other) const |
Compare this StringView with another StringView for inequality. More... | |
constexpr bool | operator== (StringView other) const |
Compare this StringView with another StringView for equality. More... | |
constexpr bool | fullyOverlaps (StringView other, size_t &commonOverlappingPoints) const |
Check if this StringView is equal to other StringView (operates on code points, not on utf graphemes). More... | |
constexpr bool | isEmpty () const |
Check if StringView is empty. More... | |
constexpr bool | isNullTerminated () const |
Check if StringView is immediately followed by a null termination character. More... | |
constexpr size_t | sizeInBytes () const |
Get size of the StringView in bytes. More... | |
constexpr size_t | sizeInBytesIncludingTerminator () const |
Get size of the StringView in bytes, including null terminator. More... | |
bool | endsWithAnyOf (Span< const StringCodePoint > codePoints) const |
Check if StringView ends with any utf code point in the given span. More... | |
bool | startsWithAnyOf (Span< const StringCodePoint > codePoints) const |
Check if StringView starts with any utf code point in the given span. More... | |
bool | startsWith (const StringView str) const |
Check if StringView starts with another StringView. More... | |
bool | endsWith (const StringView str) const |
Check if StringView ends with another StringView. More... | |
bool | containsString (const StringView str) const |
Check if StringView contains another StringView with compatible encoding. More... | |
bool | splitAfter (const StringView stringToMatch, StringView &remainingAfterSplit) const |
Returns the remaining part of the string after matching stringToMatch. More... | |
bool | splitBefore (const StringView stringToMatch, StringView &stringBeforeSplit) const |
Returns the part of the string before matching stringToMatch. More... | |
bool | containsCodePoint (StringCodePoint c) const |
Check if StringView contains given utf code point. More... | |
constexpr bool | hasCompatibleEncoding (StringView str) const |
Check if current StringView has compatible encoding with str. More... | |
StringView | sliceStartEnd (size_t start, size_t end) const |
Get slice [start, end) starting at offset start and ending at end (measured in utf code points) More... | |
StringView | sliceStartLength (size_t start, size_t length) const |
Get slice [start, start+length] starting at offset start and of length code points. More... | |
StringView | sliceStart (size_t offset) const |
Get slice [offset, end] measured in utf code points. More... | |
StringView | sliceEnd (size_t offset) const |
Get slice [end-offset, end] measured in utf code points. More... | |
StringView | trimEndAnyOf (Span< const StringCodePoint > codePoints) const |
Returns a shortened StringView removing ending utf code points matching the codePoints span. More... | |
StringView | trimStartAnyOf (Span< const StringCodePoint > codePoints) const |
Returns a shortened StringView removing starting utf code points matching the codePoints span. More... | |
StringView | trimAnyOf (Span< const StringCodePoint > codePoints) const |
Returns a shortened StringView removing starting and ending utf code points inside the codePoints span. More... | |
StringView | trimWhiteSpaces () const |
Returns a shortened StringView without starting/ending utf code points inside {'\r', '\n', '\t', ' '}. More... | |
constexpr StringView | sliceStartBytes (size_t start) const |
Returns a shortened StringView from current cutting the first start bytes. More... | |
constexpr StringView | sliceStartEndBytes (size_t start, size_t end) const |
Returns a shortened StringView taking a slice from start to end expressed in bytes. More... | |
constexpr StringView | sliceStartLengthBytes (size_t start, size_t length) const |
Returns a shortened StringView taking a slice from start ending at start +length bytes. More... | |
bool | isIntegerNumber () const |
If the current view is an integer number, returns true. More... | |
bool | isFloatingNumber () const |
Check if StringView can be parsed as an floating point number. More... | |
bool | parseInt32 (int32_t &value) const |
Try parsing current StringView as a 32 bit integer. More... | |
bool | parseFloat (float &value) const |
Try parsing current StringView as a floating point number. More... | |
bool | parseDouble (double &value) const |
Try parsing current StringView as a double precision floating point number. More... | |
template<typename Type > | |
constexpr SC::StringIteratorASCII | getIterator (identity< Type >) const |
template<typename StringIterator > | |
SC::StringView | fromIterators (StringIterator from, StringIterator to) |
template<typename StringIterator > | |
SC::StringView | fromIteratorUntilEnd (StringIterator it) |
template<typename StringIterator > | |
constexpr SC::StringView | fromIteratorFromStart (StringIterator it) |
Static Public Member Functions | |
static StringView | fromNullTerminated (const char *text, StringEncoding encoding) |
Constructs a StringView from a null-terminated C-String. More... | |
template<typename Func > | |
static constexpr auto | withIterators (StringView s1, StringView s2, Func &&func) |
Call given lambda with one of StringIteratorASCII, StringIteratorUTF8, StringIteratorUTF16 depending on encoding. More... | |
template<typename StringIterator > | |
static StringView | fromIterators (StringIterator from, StringIterator to) |
Returns a StringView from two iterators. The from iterator will be shortened until the start of to. More... | |
template<typename StringIterator > | |
static StringView | fromIteratorUntilEnd (StringIterator it) |
Returns a section of a string, from it to end of StringView. More... | |
template<typename StringIterator > | |
static constexpr StringView | fromIteratorFromStart (StringIterator it) |
Returns a section of a string, from start of StringView to it . More... | |
Non-owning view over a range of characters with UTF Encoding.
It additional also holds the SC::StringEncoding information (ASCII
, UTF8
or UTF16
). During construction the encoding information and the null-termination state must be specified. All methods are const because it's not possible to modify a string with it.
Example (Construct)
Example (Construct from null terminated string)
|
strong |
Result of ordering comparison done by StringView::compare.
|
constexpr |
Construct an emtpy StringView.
|
constexpr |
Construct a StringView from a Span of bytes.
textSpan | The span containing the text EXCLUDING eventual null terminator |
nullTerm | true if a null terminator code point is expected to be found after Span. On ASCII and UTF8 this is 1 byte, on UTF16 it must be 2 bytes. |
encoding | The encoding of the text contained in this StringView |
|
constexpr |
Constructs a StringView with a null terminated string terminal.
N | Number of characters in text string literal |
text | The Null terminated string literal |
|
constexpr |
Constructs an UTF16 StringView with a null terminated wide-string terminal.
N |
text | The Null terminated wide-string literal |
|
constexpr |
Construct an UTF16 StringView from a Span of bytes.
textSpan | The span containing the text EXCLUDING eventual null terminator |
nullTerm | true if a null terminator code point is expected to be found after Span (two bytes on UTF16) |
|
constexpr |
Directly access the memory of this null terminated-StringView.
|
inlineconstexpr |
Directly access the memory of this StringView.
Comparison SC::StringView::compare | ( | StringView | other | ) | const |
Ordering comparison between non-normalized StringView (operates on code points, not on utf graphemes)
other | The string being compared to current one |
Example:
bool SC::StringView::containsCodePoint | ( | StringCodePoint | c | ) | const |
Check if StringView contains given utf code point.
c | The utf code point to check against |
true
if this StringView contains code point c bool SC::StringView::containsString | ( | const StringView | str | ) | const |
Check if StringView contains another StringView with compatible encoding.
str | The other StringView to check with current |
true
if this StringView contains str true
Example:
bool SC::StringView::endsWith | ( | const StringView | str | ) | const |
Check if StringView ends with another StringView.
str | The other StringView to check with current |
true
if this StringView ends with strExample:
bool SC::StringView::endsWithAnyOf | ( | Span< const StringCodePoint > | codePoints | ) | const |
Check if StringView ends with any utf code point in the given span.
codePoints | The utf code points to check against |
true
if this StringView ends with any code point inside codePointsExample:
|
staticconstexpr |
Returns a section of a string, from start of StringView to it
.
StringIterator | One among StringIteratorASCII, StringIteratorUTF8 and StringIteratorUTF16 |
it | The iterator pointing at the start of the specified portion of StringView. |
it
|
static |
Returns a StringView from two iterators. The from iterator will be shortened until the start of to.
Returns a StringView starting at from
and ending at to
.
StringIterator | One among StringIteratorASCII, StringIteratorUTF8 and StringIteratorUTF16 |
from | Indicates where the StringView will start. The from iterator will be shortened until the start of to. |
to | Indicates where the StringView will end. |
from
and ending at to
. >
to an empty StringView will be returned
|
static |
Returns a section of a string, from it
to end of StringView.
StringIterator | One among StringIteratorASCII, StringIteratorUTF8 and StringIteratorUTF16 |
it | The iterator pointing at the start of the specified portion of StringView. |
it
up to StringView end
|
static |
Constructs a StringView from a null-terminated C-String.
text | The null-terminated C-String |
encoding | The encoding of the text contained in this StringView |
|
constexpr |
Check if this StringView is equal to other StringView (operates on code points, not on utf graphemes).
Returns the number of code points that are the same in both StringView-s.
other | The StringView to be compared to |
commonOverlappingPoints | number of equal code points in both StringView |
true
if the two StringViews are equalExample:
|
inlineconstexpr |
Get encoding of this StringView.
|
constexpr |
Returns a StringIterator from current StringView.
StringIterator | can be StringIteratorASCII, StringIteratorUTF8 or StringIteratorUTF16 |
|
inline |
Directly access the memory of this null terminated-StringView.
wchar_t
. On other platforms return type will be char
.
|
constexpr |
Check if current StringView has compatible encoding with str.
This means checking if two encodings have the same utf unit size.
str | The other StringView to check with current |
true
if this StringView has compatible encoding with str
|
inlineconstexpr |
Check if StringView is empty.
true
if string is empty bool SC::StringView::isFloatingNumber | ( | ) | const |
Check if StringView can be parsed as an floating point number.
true
if StringView is a floating point number.Example:
bool SC::StringView::isIntegerNumber | ( | ) | const |
If the current view is an integer number, returns true.
Check if StringView can be parsed as an integer number.
true
if StringView is an integer number.Example:
|
inlineconstexpr |
Check if StringView is immediately followed by a null termination character.
true
if StringView is immediately followed by a null termination character
|
inlineconstexpr |
Compare this StringView with another StringView for inequality.
other | StringView to be compared with current one |
|
inline |
Ordering operator for StringView using StringView::compare.
other | The string being compared to current one |
true
if current string is Comparison::Smaller than otherExample:
|
constexpr |
Compare this StringView with another StringView for equality.
other | StringView to be compared with current one |
bool SC::StringView::parseDouble | ( | double & | value | ) | const |
Try parsing current StringView as a double precision floating point number.
value | Will receive the parsed double precision floating point number, if function returns true . |
true
if the StringView has been successfully parsed as a double precision floating point number.Example:
bool SC::StringView::parseFloat | ( | float & | value | ) | const |
Try parsing current StringView as a floating point number.
value | Will receive the parsed floating point number, if function returns true . |
true
if the StringView has been successfully parsed as a floating point number.Example:
bool SC::StringView::parseInt32 | ( | int32_t & | value | ) | const |
Try parsing current StringView as a 32 bit integer.
value | Will receive the parsed 32 bit integer, if function returns true . |
true
if the StringView has been successfully parsed as a 32 bit integer.Example:
|
inlineconstexpr |
Get size of the StringView in bytes.
|
constexpr |
Get size of the StringView in bytes, including null terminator.
false
StringView SC::StringView::sliceEnd | ( | size_t | offset | ) | const |
Get slice [end-offset, end]
measured in utf code points.
offset | The initial code point where the slice starts |
[end-offset, end]
Example:
StringView SC::StringView::sliceStart | ( | size_t | offset | ) | const |
Get slice [offset, end]
measured in utf code points.
offset | The initial code point where the slice starts |
[offset, end]
Example:
|
constexpr |
Returns a shortened StringView from current cutting the first start
bytes.
start | Offset in bytes where the slice starts. |
start
bytes offset StringView SC::StringView::sliceStartEnd | ( | size_t | start, |
size_t | end | ||
) | const |
Get slice [start, end)
starting at offset start
and ending at end
(measured in utf code points)
start | The initial code point where the slice starts |
end | One after the final code point where the slice ends |
[start, end)
StringView sliceExample:
|
constexpr |
Returns a shortened StringView taking a slice from start
to end
expressed in bytes.
start | Offset in bytes where the slice will start. |
end | Offset in bytes where the slice will end. |
start
bytes offset and ending at end
bytes offset. StringView SC::StringView::sliceStartLength | ( | size_t | start, |
size_t | length | ||
) | const |
Get slice [start, start+length]
starting at offset start
and of length
code points.
start | The initial code point where the slice starts |
length | One after the final code point where the slice ends |
[start, start+length]
StringView sliceExample:
|
constexpr |
Returns a shortened StringView taking a slice from start
ending at start
+length
bytes.
start | Offset in bytes where the slice will start. |
length | Length in bytes from start where the slice will end. |
start
bytes offset and ending at start
+length
bytes offset. bool SC::StringView::splitAfter | ( | const StringView | stringToMatch, |
StringView & | remainingAfterSplit | ||
) | const |
Returns the remaining part of the string after matching stringToMatch.
stringToMatch | String to match inside the source string |
remainingAfterSplit | Portion of this StringView AFTER first match of stringToMatch (excluding the match) |
true
if stringToMatch has been found and split has been written to remainingAfterSplitExample:
bool SC::StringView::splitBefore | ( | const StringView | stringToMatch, |
StringView & | stringBeforeSplit | ||
) | const |
Returns the part of the string before matching stringToMatch.
stringToMatch | String to match inside the source string |
stringBeforeSplit | Portion of this StringView BEFORE first match of stringToMatch (excluding the match) |
true
if stringToMatch has been found and split has been written to remainingAfterSplitExample:
bool SC::StringView::startsWith | ( | const StringView | str | ) | const |
Check if StringView starts with another StringView.
str | The other StringView to check with current |
true
if this StringView starts with strExample:
bool SC::StringView::startsWithAnyOf | ( | Span< const StringCodePoint > | codePoints | ) | const |
Check if StringView starts with any utf code point in the given span.
codePoints | The utf code points to check against |
true
if this StringView starts with any code point inside codePointsExample:
Obtain a const uint8_t
Span from this StringView.
|
inlineconstexpr |
Obtain a const char
Span from this StringView.
StringView SC::StringView::trimAnyOf | ( | Span< const StringCodePoint > | codePoints | ) | const |
Returns a shortened StringView removing starting and ending utf code points inside the codePoints
span.
codePoints | The span of utf code points to look for |
Example:
StringView SC::StringView::trimEndAnyOf | ( | Span< const StringCodePoint > | codePoints | ) | const |
Returns a shortened StringView removing ending utf code points matching the codePoints
span.
codePoints | The span of utf code points to look for |
Example:
StringView SC::StringView::trimStartAnyOf | ( | Span< const StringCodePoint > | codePoints | ) | const |
Returns a shortened StringView removing starting utf code points matching the codePoints
span.
codePoints | The span of utf code points to look for |
Example:
StringView SC::StringView::trimWhiteSpaces | ( | ) | const |
Returns a shortened StringView without starting/ending utf code points inside {'\r', '\n', '\t', ' '}.
Example:
|
constexpr |
Call given lambda with one of StringIteratorASCII, StringIteratorUTF8, StringIteratorUTF16 depending on encoding.
Func | A function/lambda with auto operator()({StringIteratorASCII | StringIteratorUTF8 | StringIteratorUTF16}) |
func | lambda / functor to be called |
|
staticconstexpr |
Call given lambda with one of StringIteratorASCII, StringIteratorUTF8, StringIteratorUTF16 depending on encoding.
Func | Func A function/lambda with auto operator()({StringIteratorASCII | StringIteratorUTF8 | StringIteratorUTF16}, {StringIteratorASCII | StringIteratorUTF8 | StringIteratorUTF16}) |
s1 | StringView that will be passed as first iterator |
s2 | StringView that will be passed as second iterator |
func | the lambda / to be invoked |