4#include "../Strings/StringIterator.h"
64#if SC_PLATFORM_WINDOWS || DOXYGEN
83#if SC_PLATFORM_WINDOWS
117 constexpr operator SpanStringView() const SC_LANGUAGE_LIFETIME_BOUND
120 return {text, textSizeInBytes};
184 template <
typename Func>
185 [[nodiscard]]
constexpr auto withIterator(Func&& func)
const;
195 template <
typename Func>
201 template <
typename StringIterator>
230 [[nodiscard]]
constexpr bool isEmpty()
const {
return text ==
nullptr or textSizeInBytes == 0; }
238 [[nodiscard]]
constexpr size_t sizeInBytes()
const {
return textSizeInBytes; }
350 template <
typename StringIterator>
357 template <
typename StringIterator>
364 template <
typename StringIterator>
551 const wchar_t* textWide;
555 static constexpr SizeType NumOptionBits = 3;
556 static constexpr SizeType MaxLength = (~static_cast<SizeType>(0)) >> NumOptionBits;
558 SizeType textSizeInBytes :
sizeof(SizeType) * 8 - NumOptionBits;
559 SizeType encoding : 2;
560 SizeType hasNullTerm : 1;
562 template <
typename T>
566 template <
typename Type>
567 constexpr StringIteratorASCII
getIterator(identity<Type>)
const;
568 constexpr StringIteratorUTF8
getIterator(identity<StringIteratorUTF8>)
const;
569 constexpr StringIteratorUTF16
getIterator(identity<StringIteratorUTF16>)
const;
570 template <
typename StringIterator1,
typename StringIterator2>
571 static constexpr bool equalsIterator(StringIterator1 t1, StringIterator2 t2,
size_t& points);
573 template <
typename StringIterator>
574 constexpr bool equalsIterator(
StringView other,
size_t& points)
const;
679 template <
typename StringIterator1,
typename StringIterator2>
680 [[nodiscard]]
static bool matchWildcardIterator(StringIterator1 pattern, StringIterator2 text);
698constexpr SC::StringView operator""_u16(
const char* txt,
size_t sz)
700 const bool isNullTerminated = sz > 0 and sz % 2 == 1 and txt[sz - 1] == 0;
706 : text(nullptr), textSizeInBytes(0), encoding(static_cast<SizeType>(
StringEncoding::
Ascii)), hasNullTerm(false)
710 : text(textSpan.data()), textSizeInBytes(static_cast<SizeType>(textSpan.sizeInBytes())),
711 encoding(static_cast<SizeType>(encoding)), hasNullTerm(nullTerm)
716template <SC::
size_t N>
718 : text(text), textSizeInBytes(N - 1), encoding(static_cast<SizeType>(
StringEncoding::
Ascii)), hasNullTerm(true)
721#if SC_PLATFORM_WINDOWS
724 : textWide(text), textSizeInBytes((N - 1) * sizeof(wchar_t)), encoding(static_cast<SizeType>(
StringEncoding::Wide)),
729 : textWide(textSpan.data()), textSizeInBytes(static_cast<SizeType>(textSpan.sizeInBytes())),
730 encoding(static_cast<SizeType>(
StringEncoding::Wide)), hasNullTerm(nullTerm)
742template <
typename StringIterator>
746 return getIterator(identity<StringIterator>());
749template <
typename Type>
756 return StringIteratorUTF8(text, text + textSizeInBytes);
760 return StringIteratorUTF16(text, text + textSizeInBytes);
763template <
typename StringIterator1,
typename StringIterator2>
764constexpr bool SC::StringView::equalsIterator(StringIterator1 t1, StringIterator2 t2,
size_t& points)
768 while (t1.advanceRead(c1) and t2.advanceRead(c2))
776 return t1.isAtEnd() and t2.isAtEnd();
779template <
typename StringIterator>
780constexpr bool SC::StringView::equalsIterator(StringView other,
size_t& points)
const
782 auto it = getIterator<StringIterator>();
783 switch (other.getEncoding())
785 case StringEncoding::Ascii:
return equalsIterator(it, other.getIterator<StringIteratorASCII>(), points);
786 case StringEncoding::Utf8:
return equalsIterator(it, other.getIterator<StringIteratorUTF8>(), points);
787 case StringEncoding::Utf16:
return equalsIterator(it, other.getIterator<StringIteratorUTF16>(), points);
789 Assert::unreachable();
794#if SC_PLATFORM_WINDOWS
796 return reinterpret_cast<const wchar_t*
>(text);
805 if (hasCompatibleEncoding(other))
807 if (textSizeInBytes != other.textSizeInBytes)
809 if (__builtin_is_constant_evaluated())
812 auto it2 = other.text;
813 auto sz = textSizeInBytes;
814 for (
size_t idx = 0; idx < sz; ++idx)
815 if (it1[idx] != it2[idx])
822 return other.textSizeInBytes == 0;
824 if (other.text ==
nullptr)
826 return textSizeInBytes == 0;
828 return memcmp(text, other.text, textSizeInBytes) == 0;
831 size_t commonOverlappingPoints = 0;
832 return fullyOverlaps(other, commonOverlappingPoints);
837 commonOverlappingPoints = 0;
838 switch (getEncoding())
840 case StringEncoding::Ascii:
return equalsIterator<StringIteratorASCII>(other, commonOverlappingPoints);
841 case StringEncoding::Utf8:
return equalsIterator<StringIteratorUTF8>(other, commonOverlappingPoints);
842 case StringEncoding::Utf16:
return equalsIterator<StringIteratorUTF16>(other, commonOverlappingPoints);
844 Assert::unreachable();
853template <
typename Func>
856 switch (getEncoding())
862 Assert::unreachable();
865template <
typename Func>
869 {
return s2.
withIterator([&it1, &func](
auto it2) {
return func(it1, it2); }); });
877template <
typename StringIterator>
886 return StringView({from.it,
static_cast<size_t>(numBytes)},
false, StringIterator::getEncoding());
891template <
typename StringIterator>
894 StringIterator endIt = it;
896 const size_t numBytes =
static_cast<size_t>(endIt.bytesDistanceFrom(it));
897 return StringView({it.it, numBytes},
false, StringIterator::getEncoding());
900template <
typename StringIterator>
903 StringIterator start = it;
905 const size_t numBytes =
static_cast<size_t>(it.bytesDistanceFrom(start));
906 return StringView({start.it, numBytes},
false, StringIterator::getEncoding());
911 if (start < sizeInBytes())
912 return sliceStartLengthBytes(start, sizeInBytes() - start);
914 return StringView({text, 0},
false, getEncoding());
920 return sliceStartLengthBytes(start, end - start);
922 return StringView({text, 0},
false, getEncoding());
927 if (start + length > sizeInBytes())
930 return StringView({text, 0},
false, getEncoding());
932 return StringView({text + start, length}, hasNullTerm and (start + length == sizeInBytes()), getEncoding());
#define SC_ASSERT_DEBUG(e)
Assert expression e to be true.
Definition Assert.h:82
#define SC_COMPILER_EXPORT
Macro for symbol visibility in non-MSVC compilers.
Definition Compiler.h:78
#define SC_ASSERT_RELEASE(e)
Assert expression e to be true.
Definition Assert.h:66
unsigned long size_t
Platform independent unsigned size type.
Definition PrimitiveTypes.h:56
signed long ssize_t
Platform independent signed size type.
Definition PrimitiveTypes.h:57
int int32_t
Platform independent (4) bytes signed int.
Definition PrimitiveTypes.h:46
constexpr bool StringEncodingAreBinaryCompatible(StringEncoding encoding1, StringEncoding encoding2)
Checks if two encodings have the same utf unit size.
Definition StringIterator.h:33
StringEncoding
String Encoding (Ascii, Utf8, Utf16)
Definition StringIterator.h:17
constexpr uint32_t StringEncodingGetSize(StringEncoding encoding)
Returns the number of bytes to represent an utf unit in the given encoding.
Definition StringIterator.h:42
uint32_t StringCodePoint
UTF code point (32 bit)
Definition StringIterator.h:13
@ Ascii
Encoding is ASCII.
@ Utf16
Encoding is UTF16-LE.
An read-only view over an ASCII string (to avoid including Strings library)
Definition Span.h:299
View over a contiguous sequence of items (pointer + size in elements).
Definition Span.h:32
static Span< Type > reinterpret_bytes(VoidType *rawMemory, SizeType sizeInBytes)
Construct a span reinterpreting raw memory (void* or const void*) to Type or const Type
Definition Span.h:89
constexpr SizeType sizeInBytes() const
Size of Span in bytes.
Definition Span.h:154
Algorithms operating on strings (glob / wildcard).
Definition StringView.h:675
A string iterator for ASCII strings.
Definition StringIterator.h:244
A string iterator for UTF16 strings.
Definition StringIterator.h:264
A string iterator for UTF8 strings.
Definition StringIterator.h:283
A position inside a fixed range [start, end) of UTF code points.
Definition StringIterator.h:63
constexpr ssize_t bytesDistanceFrom(StringIterator other) const
Get distance in bytes from current position to another StringIterator current position.
Definition StringIterator.h:410
constexpr void setToEnd()
Set current position to end of iterator range.
Definition StringIterator.h:73
Splits a StringView in tokens according to separators.
Definition StringView.h:579
StringViewTokenizer(StringView text)
Build a tokenizer operating on the given text string view.
Definition StringView.h:596
StringView component
Current component that has been tokenized by tokenizeNext.
Definition StringView.h:585
bool isFinished() const
Check if the tokenizer has processed the entire the string view passed in the constructor.
bool tokenizeNextLine()
Tokenizes from current position to first newline.
Definition StringView.h:632
StringViewTokenizer & countTokens(Span< const StringCodePoint > separators)
Count the number of tokens that exist in the string view passed in constructor, when splitted along t...
size_t numSplitsNonEmpty
How many non-empty splits have occurred in current tokenization.
Definition StringView.h:582
StringView processed
Substring of original string passed in constructor processed so far.
Definition StringView.h:586
size_t numSplitsTotal
How many total splits have occurred in current tokenization.
Definition StringView.h:583
bool tokenizeNext(Span< const StringCodePoint > separators, Options options=Options::SkipEmpty)
Splits the string along a list of separators.
StringView remaining
Substring from current position until the end of original text.
Definition StringView.h:587
Options
Definition StringView.h:590
@ IncludeEmpty
If to tokenizeNext should return also empty tokens.
Definition StringView.h:591
@ SkipEmpty
If to tokenizeNext should NOT return also empty tokens.
Definition StringView.h:592
StringCodePoint splittingCharacter
The last splitting character matched in current tokenization.
Definition StringView.h:580
Non-owning view over a range of characters with UTF Encoding.
Definition StringView.h:47
constexpr StringView(Span< const wchar_t > textSpan, bool nullTerm)
Construct an UTF16 StringView from a Span of bytes.
bool startsWithAnyOf(Span< const StringCodePoint > codePoints) const
Check if StringView starts with any utf code point in the given span.
Comparison
Result of ordering comparison done by StringView::compare.
Definition StringView.h:133
constexpr StringView()
Construct an emtpy StringView.
Definition StringView.h:705
constexpr bool isNullTerminated() const
Check if StringView is immediately followed by a null termination character.
Definition StringView.h:234
Comparison compare(StringView other) const
Ordering comparison between non-normalized StringView (operates on code points, not on utf graphemes)
constexpr const char * bytesWithoutTerminator() const
Directly access the memory of this StringView.
Definition StringView.h:97
constexpr bool operator==(StringView other) const
Compare this StringView with another StringView for equality.
Definition StringView.h:803
constexpr StringView sliceStartEndBytes(size_t start, size_t end) const
Returns a shortened StringView taking a slice from start to end expressed in bytes.
Definition StringView.h:917
constexpr StringIterator getIterator() const
Returns a StringIterator from current StringView.
Definition StringView.h:743
StringView trimEndAnyOf(Span< const StringCodePoint > codePoints) const
Returns a shortened StringView removing ending utf code points matching the codePoints span.
bool startsWith(const StringView str) const
Check if StringView starts with another StringView.
static constexpr StringView fromIteratorFromStart(StringIterator it)
Returns a section of a string, from start of StringView to it.
Span< const uint8_t > toBytesSpan() const SC_LANGUAGE_LIFETIME_BOUND
Obtain a const uint8_t Span from this StringView.
Definition StringView.h:126
bool parseFloat(float &value) const
Try parsing current StringView as a floating point number.
constexpr StringView sliceStartLengthBytes(size_t start, size_t length) const
Returns a shortened StringView taking a slice from start ending at start+length bytes.
Definition StringView.h:925
constexpr bool operator!=(StringView other) const
Compare this StringView with another StringView for inequality.
Definition StringView.h:207
bool containsCodePoint(StringCodePoint c) const
Check if StringView contains given utf code point.
constexpr bool hasCompatibleEncoding(StringView str) const
Check if current StringView has compatible encoding with str.
Definition StringView.h:872
static StringView fromIterators(StringIterator from, StringIterator to)
Returns a StringView from two iterators. The from iterator will be shortened until the start of to.
static constexpr auto withIterators(StringView s1, StringView s2, Func &&func)
Call given lambda with one of StringIteratorASCII, StringIteratorUTF8, StringIteratorUTF16 depending ...
Definition StringView.h:866
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)
constexpr Span< const char > toCharSpan() const SC_LANGUAGE_LIFETIME_BOUND
Obtain a const char Span from this StringView.
Definition StringView.h:115
bool endsWith(const StringView str) const
Check if StringView ends with another StringView.
StringView sliceStartLength(size_t start, size_t length) const
Get slice [start, start+length] starting at offset start and of length code points.
static StringView fromNullTerminated(const char *text, StringEncoding encoding)
Constructs a StringView from a null-terminated C-String.
StringView sliceStart(size_t offset) const
Get slice [offset, end] measured in utf code points.
auto getNullTerminatedNative() const
Directly access the memory of this null terminated-StringView.
Definition StringView.h:792
bool endsWithAnyOf(Span< const StringCodePoint > codePoints) const
Check if StringView ends with any utf code point in the given span.
StringView trimWhiteSpaces() const
Returns a shortened StringView without starting/ending utf code points inside {'\r',...
bool parseDouble(double &value) const
Try parsing current StringView as a double precision floating point number.
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)...
Definition StringView.h:835
StringView trimStartAnyOf(Span< const StringCodePoint > codePoints) const
Returns a shortened StringView removing starting utf code points matching the codePoints span.
bool isIntegerNumber() const
If the current view is an integer number, returns true.
constexpr StringEncoding getEncoding() const
Get encoding of this StringView.
Definition StringView.h:93
bool splitBefore(const StringView stringToMatch, StringView &stringBeforeSplit) const
Returns the part of the string before matching stringToMatch.
StringView sliceEnd(size_t offset) const
Get slice [end-offset, end] measured in utf code points.
constexpr size_t sizeInBytesIncludingTerminator() const
Get size of the StringView in bytes, including null terminator.
Definition StringView.h:847
bool splitAfter(const StringView stringToMatch, StringView &remainingAfterSplit) const
Returns the remaining part of the string after matching stringToMatch.
static StringView fromIteratorUntilEnd(StringIterator it)
Returns a section of a string, from it to end of StringView.
constexpr StringView sliceStartBytes(size_t start) const
Returns a shortened StringView from current cutting the first start bytes.
Definition StringView.h:909
constexpr const char * bytesIncludingTerminator() const
Directly access the memory of this null terminated-StringView.
Definition StringView.h:736
constexpr auto withIterator(Func &&func) const
Call given lambda with one of StringIteratorASCII, StringIteratorUTF8, StringIteratorUTF16 depending ...
Definition StringView.h:854
constexpr bool isEmpty() const
Check if StringView is empty.
Definition StringView.h:230
bool containsString(const StringView str) const
Check if StringView contains another StringView with compatible encoding.
bool operator<(StringView other) const
Ordering operator for StringView using StringView::compare.
Definition StringView.h:176
constexpr size_t sizeInBytes() const
Get size of the StringView in bytes.
Definition StringView.h:238
bool parseInt32(int32_t &value) const
Try parsing current StringView as a 32 bit integer.
StringView trimAnyOf(Span< const StringCodePoint > codePoints) const
Returns a shortened StringView removing starting and ending utf code points inside the codePoints spa...
bool isFloatingNumber() const
Check if StringView can be parsed as an floating point number.
constexpr StringView(const wchar_t(&text)[N])
Constructs an UTF16 StringView with a null terminated wide-string terminal.