Sane C++ Libraries
C++ Platform Abstraction Libraries
SC::StringView Struct Reference

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

Detailed Description

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)

StringView s("asd");
SC_ASSERT_RELEASE(s.sizeInBytes() == 3);
SC_ASSERT_RELEASE(s.isNullTerminated());
#define SC_ASSERT_RELEASE(e)
Assert expression e to be true.
Definition: Assert.h:66
constexpr StringView()
Construct an emtpy StringView.
Definition: StringView.h:704

Example (Construct from null terminated string)

const char* someString = "asdf";
// construct only "asd", not null terminated (as there is 'f' after 'd')
StringView s({someString, strlen(asd) - 1}, false, StringEncoding::Ascii);
SC_ASSERT_RELEASE(s.sizeInBytes() == 3);
SC_ASSERT_RELEASE(not s.isNullTerminated());
//
// ... or
@ Ascii
Encoding is ASCII.
static StringView fromNullTerminated(const char *text, StringEncoding encoding)
Constructs a StringView from a null-terminated C-String.

Member Enumeration Documentation

◆ Comparison

enum class SC::StringView::Comparison
strong

Result of ordering comparison done by StringView::compare.

Constructor & Destructor Documentation

◆ StringView() [1/5]

constexpr SC::StringView::StringView ( )
constexpr

Construct an emtpy StringView.

◆ StringView() [2/5]

constexpr SC::StringView::StringView ( Span< const char >  textSpan,
bool  nullTerm,
StringEncoding  encoding 
)
constexpr

Construct a StringView from a Span of bytes.

Parameters
textSpanThe span containing the text EXCLUDING eventual null terminator
nullTermtrue 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.
encodingThe encoding of the text contained in this StringView

◆ StringView() [3/5]

template<SC::size_t N>
constexpr SC::StringView::StringView ( const char(&)  text[N])
constexpr

Constructs a StringView with a null terminated string terminal.

Template Parameters
NNumber of characters in text string literal
Parameters
textThe Null terminated string literal

◆ StringView() [4/5]

template<size_t N>
constexpr SC::StringView::StringView ( const wchar_t(&)  text[N])
constexpr

Constructs an UTF16 StringView with a null terminated wide-string terminal.

Template Parameters
N
Parameters
textThe Null terminated wide-string literal

◆ StringView() [5/5]

constexpr SC::StringView::StringView ( Span< const wchar_t >  textSpan,
bool  nullTerm 
)
constexpr

Construct an UTF16 StringView from a Span of bytes.

Parameters
textSpanThe span containing the text EXCLUDING eventual null terminator
nullTermtrue if a null terminator code point is expected to be found after Span (two bytes on UTF16)

Member Function Documentation

◆ bytesIncludingTerminator()

constexpr const char * SC::StringView::bytesIncludingTerminator ( ) const
constexpr

Directly access the memory of this null terminated-StringView.

Returns
Pointer to start of StringView memory
Warning
This method will assert if string is not null terminated.

◆ bytesWithoutTerminator()

constexpr const char * SC::StringView::bytesWithoutTerminator ( ) const
inlineconstexpr

Directly access the memory of this StringView.

Returns
Pointer to start of StringView memory

◆ compare()

Comparison SC::StringView::compare ( StringView  other) const

Ordering comparison between non-normalized StringView (operates on code points, not on utf graphemes)

Parameters
otherThe string being compared to current one
Returns
Result of the comparison (smaller, equals or bigger)

Example:

// àèìòù (1 UTF16-LE sequence, 2 UTF8 sequence)
SC_ASSERT_RELEASE("\xc3\xa0\xc3\xa8\xc3\xac\xc3\xb2\xc3\xb9"_u8.compare(
"\xe0\x0\xe8\x0\xec\x0\xf2\x0\xf9\x0"_u16) == StringView::Comparison::Equals);
// 日本語語語 (1 UTF16-LE sequence, 3 UTF8 sequence)
StringView stringUtf8 = StringView("\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e\xe8\xaa\x9e\xe8\xaa\x9e"_u8);
StringView stringUtf16 = StringView("\xE5\x65\x2C\x67\x9E\x8a\x9E\x8a\x9E\x8a\x00"_u16); // LE
// Comparisons are on code points NOT grapheme clusters!!
SC_ASSERT_RELEASE(stringUtf8.compare(stringUtf16) == StringView::Comparison::Equals);
SC_ASSERT_RELEASE(stringUtf16.compare(stringUtf8) == StringView::Comparison::Equals);
SC_ASSERT_RELEASE(stringUtf8 == stringUtf16);
SC_ASSERT_RELEASE(stringUtf16 == stringUtf8);

◆ containsCodePoint()

bool SC::StringView::containsCodePoint ( StringCodePoint  c) const

Check if StringView contains given utf code point.

Parameters
cThe utf code point to check against
Returns
Returns true if this StringView contains code point c

◆ containsString()

bool SC::StringView::containsString ( const StringView  str) const

Check if StringView contains another StringView with compatible encoding.

Parameters
strThe other StringView to check with current
Returns
Returns true if this StringView contains str
Warning
This method will assert if strings have non compatible encoding. It can be checked with StringView::hasCompatibleEncoding (str) == true

Example:

StringView asd = "123 456";
SC_TRY(asd.containsString("123"));
SC_TRY(asd.containsString("456"));
SC_TRY(not asd.containsString("124"));
SC_TRY(not asd.containsString("4567"));
#define SC_TRY(expression)
Checks the value of the given expression and if failed, returns this value to caller.
Definition: Result.h:47

◆ endsWith()

bool SC::StringView::endsWith ( const StringView  str) const

Check if StringView ends with another StringView.

Parameters
strThe other StringView to check with current
Returns
Returns true if this StringView ends with str

Example:

SC_TEST_EXPECT("123 456".endsWith("456"));
#define SC_TEST_EXPECT(e)
Records a test expectation (eventually aborting or breaking o n failed test)
Definition: Testing.h:113
bool endsWith(const StringView str) const
Check if StringView ends with another StringView.

◆ endsWithAnyOf()

bool SC::StringView::endsWithAnyOf ( Span< const StringCodePoint codePoints) const

Check if StringView ends with any utf code point in the given span.

Parameters
codePointsThe utf code points to check against
Returns
Returns true if this StringView ends with any code point inside codePoints

Example:

SC_TEST_EXPECT("123 456".endsWithAnyOf({'a', '6'})); // '6' will match
bool endsWithAnyOf(Span< const StringCodePoint > codePoints) const
Check if StringView ends with any utf code point in the given span.

◆ fromIteratorFromStart()

template<typename StringIterator >
static constexpr StringView SC::StringView::fromIteratorFromStart ( StringIterator  it)
staticconstexpr

Returns a section of a string, from start of StringView to it.

Template Parameters
StringIteratorOne among StringIteratorASCII, StringIteratorUTF8 and StringIteratorUTF16
Parameters
itThe iterator pointing at the start of the specified portion of StringView.
Returns
Another StringView pointing at characters from start of StringView until it

◆ fromIterators()

template<typename StringIterator >
static StringView SC::StringView::fromIterators ( StringIterator  from,
StringIterator  to 
)
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.

Template Parameters
StringIteratorOne among StringIteratorASCII, StringIteratorUTF8 and StringIteratorUTF16
Parameters
fromIndicates where the StringView will start. The from iterator will be shortened until the start of to.
toIndicates where the StringView will end.
Returns
A StringView starting at from and ending at to.
Note
If from is > to an empty StringView will be returned

◆ fromIteratorUntilEnd()

template<typename StringIterator >
static StringView SC::StringView::fromIteratorUntilEnd ( StringIterator  it)
static

Returns a section of a string, from it to end of StringView.

Template Parameters
StringIteratorOne among StringIteratorASCII, StringIteratorUTF8 and StringIteratorUTF16
Parameters
itThe iterator pointing at the start of the specified portion of StringView.
Returns
Another StringView pointing at characters from it up to StringView end

◆ fromNullTerminated()

static StringView SC::StringView::fromNullTerminated ( const char *  text,
StringEncoding  encoding 
)
static

Constructs a StringView from a null-terminated C-String.

Parameters
textThe null-terminated C-String
encodingThe encoding of the text contained in this StringView
Returns
The StringView containing text with given encoding

◆ fullyOverlaps()

constexpr bool SC::StringView::fullyOverlaps ( StringView  other,
size_t commonOverlappingPoints 
) const
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.

Parameters
otherThe StringView to be compared to
commonOverlappingPointsnumber of equal code points in both StringView
Returns
true if the two StringViews are equal

Example:

StringView asd = "123 456"_a8;
size_t overlapPoints = 0;
SC_TEST_EXPECT(not asd.fullyOverlaps("123___", overlapPoints) and overlapPoints == 3);

◆ getEncoding()

constexpr StringEncoding SC::StringView::getEncoding ( ) const
inlineconstexpr

Get encoding of this StringView.

Returns
This StringView encoding

◆ getIterator()

template<typename StringIterator >
constexpr StringIterator SC::StringView::getIterator
constexpr

Returns a StringIterator from current StringView.

Template Parameters
StringIteratorcan be StringIteratorASCII, StringIteratorUTF8 or StringIteratorUTF16
Returns
StringIterator representing the StringView

◆ getNullTerminatedNative()

auto SC::StringView::getNullTerminatedNative ( ) const
inline

Directly access the memory of this null terminated-StringView.

Returns
Pointer to start of StringView memory. On Windows return type will be wchar_t. On other platforms return type will be char.
Warning
This method will assert that the string is null terminated. On Windows this will assert that encoding is UTF16. On other platforms this will assert that encoding is UTF8 or ASCII.

◆ hasCompatibleEncoding()

constexpr bool SC::StringView::hasCompatibleEncoding ( StringView  str) const
constexpr

Check if current StringView has compatible encoding with str.

This means checking if two encodings have the same utf unit size.

Parameters
strThe other StringView to check with current
Returns
true if this StringView has compatible encoding with str

◆ isEmpty()

constexpr bool SC::StringView::isEmpty ( ) const
inlineconstexpr

Check if StringView is empty.

Returns
true if string is empty

◆ isFloatingNumber()

bool SC::StringView::isFloatingNumber ( ) const

Check if StringView can be parsed as an floating point number.

Returns
true if StringView is a floating point number.

Example:

SC_TEST_EXPECT("-34."_a8.isFloatingNumber());
SC_TEST_EXPECT("-34.0"_a8.isFloatingNumber());
SC_TEST_EXPECT("0.34"_a8.isFloatingNumber());
SC_TEST_EXPECT(not "+12$"_a8.isFloatingNumber());
SC_TEST_EXPECT(not "$+12"_a8.isFloatingNumber());
SC_TEST_EXPECT(not "+$12"_a8.isFloatingNumber());

◆ isIntegerNumber()

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.

Returns
true if StringView is an integer number.

Example:

SC_TEST_EXPECT("-34"_a8.isIntegerNumber());
SC_TEST_EXPECT("+12"_a8.isIntegerNumber());
SC_TEST_EXPECT(not "+12$"_a8.isIntegerNumber());

◆ isNullTerminated()

constexpr bool SC::StringView::isNullTerminated ( ) const
inlineconstexpr

Check if StringView is immediately followed by a null termination character.

Returns
true if StringView is immediately followed by a null termination character

◆ operator!=()

constexpr bool SC::StringView::operator!= ( StringView  other) const
inlineconstexpr

Compare this StringView with another StringView for inequality.

Parameters
otherStringView to be compared with current one
Returns
'true' if the two StringView are different

◆ operator<()

bool SC::StringView::operator< ( StringView  other) const
inline

Ordering operator for StringView using StringView::compare.

Parameters
otherThe string being compared to current one
Returns
true if current string is Comparison::Smaller than other

Example:

StringView sv[3] = {
StringView("3"),
StringView("1"),
StringView("2"),
};
Algorithms::bubbleSort(sv, sv + 3, [](StringView a, StringView b) { return a < b; });
SC_TEST_EXPECT(sv[0] == "1");
SC_TEST_EXPECT(sv[1] == "2");
SC_TEST_EXPECT(sv[2] == "3");
constexpr void bubbleSort(Iterator first, Iterator last, BinaryPredicate predicate=BinaryPredicate())
Sorts iterator range according to BinaryPredicate (bubble sort).
Definition: AlgorithmBubbleSort.h:34

◆ operator==()

constexpr bool SC::StringView::operator== ( StringView  other) const
constexpr

Compare this StringView with another StringView for equality.

Parameters
otherStringView to be compared with current one
Returns
'true' if the two StringView are the same

◆ parseDouble()

bool SC::StringView::parseDouble ( double &  value) const

Try parsing current StringView as a double precision floating point number.

Parameters
valueWill receive the parsed double precision floating point number, if function returns true.
Returns
true if the StringView has been successfully parsed as a double precision floating point number.

Example:

StringView other("12.342321");
double value;
if(other.parseDouble(value))
{
// ... do something with value
}

◆ parseFloat()

bool SC::StringView::parseFloat ( float &  value) const

Try parsing current StringView as a floating point number.

Parameters
valueWill receive the parsed floating point number, if function returns true.
Returns
true if the StringView has been successfully parsed as a floating point number.

Example:

StringView other("12.34");
float value;
if(other.parseFloat(value))
{
// ... do something with value
}

◆ parseInt32()

bool SC::StringView::parseInt32 ( int32_t value) const

Try parsing current StringView as a 32 bit integer.

Parameters
valueWill receive the parsed 32 bit integer, if function returns true.
Returns
true if the StringView has been successfully parsed as a 32 bit integer.

Example:

StringView other("123");
int32_t value;
if(other.parseInt32(value))
{
// ... do something with value
}
int int32_t
Platform independent (4) bytes signed int.
Definition: PrimitiveTypes.h:46

◆ sizeInBytes()

constexpr size_t SC::StringView::sizeInBytes ( ) const
inlineconstexpr

Get size of the StringView in bytes.

Returns
Size in bytes of StringView

◆ sizeInBytesIncludingTerminator()

constexpr SC::size_t SC::StringView::sizeInBytesIncludingTerminator ( ) const
constexpr

Get size of the StringView in bytes, including null terminator.

Returns
Size in bytes of StringView including null terminator (2 bytes on UTF16)
Warning
This method can be called only on StringView that are null terminated. This means that it will Assert if this StringView::isNullTerminated returns false

◆ sliceEnd()

StringView SC::StringView::sliceEnd ( size_t  offset) const

Get slice [end-offset, end] measured in utf code points.

Parameters
offsetThe initial code point where the slice starts
Returns
The sliced StringView [end-offset, end]

Example:

StringView str = "123_567";
SC_TEST_EXPECT(str.sliceEnd(4) == "123");

◆ sliceStart()

StringView SC::StringView::sliceStart ( size_t  offset) const

Get slice [offset, end] measured in utf code points.

Parameters
offsetThe initial code point where the slice starts
Returns
The sliced StringView [offset, end]

Example:

StringView str = "123_567";
SC_TEST_EXPECT(str.sliceStart(4) == "567");

◆ sliceStartBytes()

constexpr SC::StringView SC::StringView::sliceStartBytes ( size_t  start) const
constexpr

Returns a shortened StringView from current cutting the first start bytes.

Parameters
startOffset in bytes where the slice starts.
Returns
A sliced StringView starting at start bytes offset

◆ sliceStartEnd()

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)

Parameters
startThe initial code point where the slice starts
endOne after the final code point where the slice ends
Returns
The [start, end) StringView slice

Example:

StringView str = "123_567";
SC_TEST_EXPECT(str.sliceStartEnd(0, 3) == "123");
SC_TEST_EXPECT(str.sliceStartEnd(4, 7) == "567");

◆ sliceStartEndBytes()

constexpr SC::StringView SC::StringView::sliceStartEndBytes ( size_t  start,
size_t  end 
) const
constexpr

Returns a shortened StringView taking a slice from start to end expressed in bytes.

Parameters
startOffset in bytes where the slice will start.
endOffset in bytes where the slice will end.
Returns
A sliced StringView starting at start bytes offset and ending at end bytes offset.

◆ sliceStartLength()

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.

Parameters
startThe initial code point where the slice starts
lengthOne after the final code point where the slice ends
Returns
The [start, start+length] StringView slice

Example:

StringView str = "123_567";
SC_TEST_EXPECT(str.sliceStartLength(7, 0) == "");
SC_TEST_EXPECT(str.sliceStartLength(0, 3) == "123");

◆ sliceStartLengthBytes()

constexpr SC::StringView SC::StringView::sliceStartLengthBytes ( size_t  start,
size_t  length 
) const
constexpr

Returns a shortened StringView taking a slice from start ending at start+length bytes.

Parameters
startOffset in bytes where the slice will start.
lengthLength in bytes from start where the slice will end.
Returns
A sliced StringView starting at start bytes offset and ending at start+length bytes offset.

◆ splitAfter()

bool SC::StringView::splitAfter ( const StringView  stringToMatch,
StringView remainingAfterSplit 
) const

Returns the remaining part of the string after matching stringToMatch.

Parameters
stringToMatchString to match inside the source string
remainingAfterSplitPortion of this StringView AFTER first match of stringToMatch (excluding the match)
Returns
Returns true if stringToMatch has been found and split has been written to remainingAfterSplit

Example:

StringView str("KEY = VALUE");
StringView split;
SC_TEST_EXPECT(str.splitAfter(" = ", split));
SC_TEST_EXPECT(split == "VALUE");

◆ splitBefore()

bool SC::StringView::splitBefore ( const StringView  stringToMatch,
StringView stringBeforeSplit 
) const

Returns the part of the string before matching stringToMatch.

Parameters
stringToMatchString to match inside the source string
stringBeforeSplitPortion of this StringView BEFORE first match of stringToMatch (excluding the match)
Returns
Returns true if stringToMatch has been found and split has been written to remainingAfterSplit

Example:

StringView str("KEY = VALUE");
StringView split;
SC_TEST_EXPECT(str.splitBefore(" = ", split));
SC_TEST_EXPECT(split == "KEY");

◆ startsWith()

bool SC::StringView::startsWith ( const StringView  str) const

Check if StringView starts with another StringView.

Parameters
strThe other StringView to check with current
Returns
Returns true if this StringView starts with str

Example:

SC_TEST_EXPECT("123 456".startsWith("123"));
bool startsWith(const StringView str) const
Check if StringView starts with another StringView.

◆ startsWithAnyOf()

bool SC::StringView::startsWithAnyOf ( Span< const StringCodePoint codePoints) const

Check if StringView starts with any utf code point in the given span.

Parameters
codePointsThe utf code points to check against
Returns
Returns true if this StringView starts with any code point inside codePoints

Example:

SC_TEST_EXPECT("123 456".startsWithAnyOf({'1', '8'})); // '1' will match
bool startsWithAnyOf(Span< const StringCodePoint > codePoints) const
Check if StringView starts with any utf code point in the given span.

◆ toBytesSpan()

Span< const uint8_t > SC::StringView::toBytesSpan ( ) const
inline

Obtain a const uint8_t Span from this StringView.

Returns
Span representing this StringView

◆ toCharSpan()

constexpr Span< const char > SC::StringView::toCharSpan ( ) const
inlineconstexpr

Obtain a const char Span from this StringView.

Returns
Span representing this StringView

◆ trimAnyOf()

StringView SC::StringView::trimAnyOf ( Span< const StringCodePoint codePoints) const

Returns a shortened StringView removing starting and ending utf code points inside the codePoints span.

Parameters
codePointsThe span of utf code points to look for
Returns
The trimmed StringView

Example:

SC_TEST_EXPECT("__\n_myTest__\n"_a8.trimAnyOf({'_', '\n'}) == "myTest");
SC_TEST_EXPECT("_myTest"_a8.trimAnyOf({'_'}) == "myTest");

◆ trimEndAnyOf()

StringView SC::StringView::trimEndAnyOf ( Span< const StringCodePoint codePoints) const

Returns a shortened StringView removing ending utf code points matching the codePoints span.

Parameters
codePointsThe span of utf code points to look for
Returns
The trimmed StringView

Example:

SC_TEST_EXPECT("myTest_\n__"_a8.trimEndAnyOf({'_', '\n'}) == "myTest");
SC_TEST_EXPECT("myTest"_a8.trimEndAnyOf({'_'}) == "myTest");

◆ trimStartAnyOf()

StringView SC::StringView::trimStartAnyOf ( Span< const StringCodePoint codePoints) const

Returns a shortened StringView removing starting utf code points matching the codePoints span.

Parameters
codePointsThe span of utf code points to look for
Returns
The trimmed StringView

Example:

SC_TEST_EXPECT("__\n_myTest"_a8.trimStartAnyOf({'_', '\n'}) == "myTest");
SC_TEST_EXPECT("_myTest"_a8.trimStartAnyOf({'_'}) == "myTest");

◆ trimWhiteSpaces()

StringView SC::StringView::trimWhiteSpaces ( ) const

Returns a shortened StringView without starting/ending utf code points inside {'\r', '\n', '\t', ' '}.

Returns
The trimmed StringView

Example:

SC_TEST_EXPECT(" \n_myTest__\n\t"_a8.trimWhiteSpaces() == "_myTest__");
SC_TEST_EXPECT("\nmyTest \r"_a8.trimWhiteSpaces() == "myTest");

◆ withIterator()

template<typename Func >
constexpr auto SC::StringView::withIterator ( Func &&  func) const
constexpr

Call given lambda with one of StringIteratorASCII, StringIteratorUTF8, StringIteratorUTF16 depending on encoding.

Template Parameters
FuncA function/lambda with auto operator()({StringIteratorASCII | StringIteratorUTF8 | StringIteratorUTF16})
Parameters
funclambda / functor to be called
Returns
whatever value is returned by invoking func

◆ withIterators()

template<typename Func >
constexpr auto SC::StringView::withIterators ( StringView  s1,
StringView  s2,
Func &&  func 
)
staticconstexpr

Call given lambda with one of StringIteratorASCII, StringIteratorUTF8, StringIteratorUTF16 depending on encoding.

Template Parameters
FuncFunc A function/lambda with auto operator()({StringIteratorASCII | StringIteratorUTF8 | StringIteratorUTF16}, {StringIteratorASCII | StringIteratorUTF8 | StringIteratorUTF16})
Parameters
s1StringView that will be passed as first iterator
s2StringView that will be passed as second iterator
functhe lambda / to be invoked
Returns
whatever value is returned by invoking func

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