Sane C++ Libraries
C++ Platform Abstraction Libraries
StringView.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Strings/StringIterator.h"
5
6namespace SC
7{
8struct SC_COMPILER_EXPORT StringView;
9struct SC_COMPILER_EXPORT StringViewTokenizer;
10struct SC_COMPILER_EXPORT StringAlgorithms;
11
12} // namespace SC
13
16
19
26
47{
49 constexpr StringView();
50
56 constexpr StringView(Span<const char> textSpan, bool nullTerm, StringEncoding encoding);
57
61 template <size_t N>
62 constexpr StringView(const char (&text)[N]);
63
64#if SC_PLATFORM_WINDOWS || DOXYGEN
68 template <size_t N>
69 constexpr StringView(const wchar_t (&text)[N]);
70
74 constexpr StringView(Span<const wchar_t> textSpan, bool nullTerm);
75#endif
76
81 static StringView fromNullTerminated(const char* text, StringEncoding encoding);
82
83#if SC_PLATFORM_WINDOWS
88 static StringView fromNullTerminated(const wchar_t* text, StringEncoding encoding);
89#endif
90
93 [[nodiscard]] constexpr StringEncoding getEncoding() const { return static_cast<StringEncoding>(encoding); }
94
97 [[nodiscard]] constexpr const char* bytesWithoutTerminator() const { return text; }
98
102 [[nodiscard]] constexpr const char* bytesIncludingTerminator() const;
103
111 auto getNullTerminatedNative() const;
112
115 constexpr Span<const char> toCharSpan() const SC_LANGUAGE_LIFETIME_BOUND { return {text, textSizeInBytes}; }
116
117 constexpr operator SpanStringView() const SC_LANGUAGE_LIFETIME_BOUND
118 {
120 return {text, textSizeInBytes};
121 return {};
122 }
123
126 Span<const uint8_t> toBytesSpan() const SC_LANGUAGE_LIFETIME_BOUND
127 {
128 return Span<const uint8_t>::reinterpret_bytes(text, textSizeInBytes);
129 }
130
132 enum class Comparison
133 {
134 Smaller = -1,
135 Equals = 0,
136 Bigger = 1
137 };
138
158 [[nodiscard]] Comparison compare(StringView other) const;
159
176 [[nodiscard]] bool operator<(StringView other) const { return compare(other) == Comparison::Smaller; }
177
184 template <typename Func>
185 [[nodiscard]] constexpr auto withIterator(Func&& func) const;
186
195 template <typename Func>
196 [[nodiscard]] static constexpr auto withIterators(StringView s1, StringView s2, Func&& func);
197
201 template <typename StringIterator>
202 constexpr StringIterator getIterator() const;
203
207 [[nodiscard]] constexpr bool operator!=(StringView other) const { return not operator==(other); }
208
212 [[nodiscard]] constexpr bool operator==(StringView other) const;
213
226 [[nodiscard]] constexpr bool fullyOverlaps(StringView other, size_t& commonOverlappingPoints) const;
227
230 [[nodiscard]] constexpr bool isEmpty() const { return text == nullptr or textSizeInBytes == 0; }
231
234 [[nodiscard]] constexpr bool isNullTerminated() const { return hasNullTerm; }
235
238 [[nodiscard]] constexpr size_t sizeInBytes() const { return textSizeInBytes; }
239
244 [[nodiscard]] constexpr size_t sizeInBytesIncludingTerminator() const;
245
254 [[nodiscard]] bool endsWithAnyOf(Span<const StringCodePoint> codePoints) const;
255
264 [[nodiscard]] bool startsWithAnyOf(Span<const StringCodePoint> codePoints) const;
265
274 [[nodiscard]] bool startsWith(const StringView str) const;
275
284 [[nodiscard]] bool endsWith(const StringView str) const;
285
300 [[nodiscard]] bool containsString(const StringView str) const;
301
314 [[nodiscard]] bool splitAfter(const StringView stringToMatch, StringView& remainingAfterSplit) const;
315
328 [[nodiscard]] bool splitBefore(const StringView stringToMatch, StringView& stringBeforeSplit) const;
329
333 [[nodiscard]] bool containsCodePoint(StringCodePoint c) const;
334
339 [[nodiscard]] constexpr bool hasCompatibleEncoding(StringView str) const;
340
342
350 template <typename StringIterator>
352
357 template <typename StringIterator>
359
364 template <typename StringIterator>
366
378 [[nodiscard]] StringView sliceStartEnd(size_t start, size_t end) const;
379
391 [[nodiscard]] StringView sliceStartLength(size_t start, size_t length) const;
392
402 [[nodiscard]] StringView sliceStart(size_t offset) const;
403
413 [[nodiscard]] StringView sliceEnd(size_t offset) const;
414
424 [[nodiscard]] StringView trimEndAnyOf(Span<const StringCodePoint> codePoints) const;
425
436
446 [[nodiscard]] StringView trimAnyOf(Span<const StringCodePoint> codePoints) const;
447
456 [[nodiscard]] StringView trimWhiteSpaces() const;
457
461 [[nodiscard]] constexpr StringView sliceStartBytes(size_t start) const;
462
467 [[nodiscard]] constexpr StringView sliceStartEndBytes(size_t start, size_t end) const;
468
473 [[nodiscard]] constexpr StringView sliceStartLengthBytes(size_t start, size_t length) const;
474
476
486 [[nodiscard]] bool isIntegerNumber() const;
487
500 [[nodiscard]] bool isFloatingNumber() const;
501
515 [[nodiscard]] bool parseInt32(int32_t& value) const;
516
530 [[nodiscard]] bool parseFloat(float& value) const;
531
545 [[nodiscard]] bool parseDouble(double& value) const;
546
547 private:
548 union
549 {
550 const char* text;
551 const wchar_t* textWide;
552 };
553 using SizeType = size_t;
554
555 static constexpr SizeType NumOptionBits = 3;
556 static constexpr SizeType MaxLength = (~static_cast<SizeType>(0)) >> NumOptionBits;
557
558 SizeType textSizeInBytes : sizeof(SizeType) * 8 - NumOptionBits;
559 SizeType encoding : 2;
560 SizeType hasNullTerm : 1;
561
562 template <typename T>
563 struct identity
564 {
565 };
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);
572
573 template <typename StringIterator>
574 constexpr bool equalsIterator(StringView other, size_t& points) const;
575};
576
579{
581
582 size_t numSplitsNonEmpty = 0;
583 size_t numSplitsTotal = 0;
584
588
590 {
592 SkipEmpty
593 };
594
596 StringViewTokenizer(StringView text) : remaining(text), originalText(text) {}
597
603
613 [[nodiscard]] bool tokenizeNext(Span<const StringCodePoint> separators, Options options = Options::SkipEmpty);
614
619
632 [[nodiscard]] bool tokenizeNextLine() { return tokenizeNext({'\n'}); }
633
640
648
650 [[nodiscard]] bool isFinished() const;
651
652 private:
653 StringView originalText; // Original text as passed in the constructor
654};
655
675{
676 [[nodiscard]] static bool matchWildcard(StringView s1, StringView s2);
677
678 private:
679 template <typename StringIterator1, typename StringIterator2>
680 [[nodiscard]] static bool matchWildcardIterator(StringIterator1 pattern, StringIterator2 text);
681};
682
684
685//-----------------------------------------------------------------------------------------------------------------------
686// Implementations Details
687//-----------------------------------------------------------------------------------------------------------------------
688namespace SC
689{
690constexpr SC::StringView operator""_a8(const char* txt, size_t sz)
691{
692 return SC::StringView({txt, sz}, true, SC::StringEncoding::Ascii);
693}
694constexpr SC::StringView operator""_u8(const char* txt, size_t sz)
695{
696 return SC::StringView({txt, sz}, true, SC::StringEncoding::Utf8);
697}
698constexpr SC::StringView operator""_u16(const char* txt, size_t sz)
699{
700 const bool isNullTerminated = sz > 0 and sz % 2 == 1 and txt[sz - 1] == 0;
701 return SC::StringView({txt, isNullTerminated ? sz - 1 : sz}, isNullTerminated, SC::StringEncoding::Utf16);
702}
703} // namespace SC
704
706 : text(nullptr), textSizeInBytes(0), encoding(static_cast<SizeType>(StringEncoding::Ascii)), hasNullTerm(false)
707{}
708
709constexpr SC::StringView::StringView(Span<const char> textSpan, bool nullTerm, StringEncoding encoding)
710 : text(textSpan.data()), textSizeInBytes(static_cast<SizeType>(textSpan.sizeInBytes())),
711 encoding(static_cast<SizeType>(encoding)), hasNullTerm(nullTerm)
712{
713 SC_ASSERT_DEBUG(textSpan.sizeInBytes() <= MaxLength);
714}
715
716template <SC::size_t N>
717constexpr SC::StringView::StringView(const char (&text)[N])
718 : text(text), textSizeInBytes(N - 1), encoding(static_cast<SizeType>(StringEncoding::Ascii)), hasNullTerm(true)
719{}
720
721#if SC_PLATFORM_WINDOWS
722template <size_t N>
723constexpr SC::StringView::StringView(const wchar_t (&text)[N])
724 : textWide(text), textSizeInBytes((N - 1) * sizeof(wchar_t)), encoding(static_cast<SizeType>(StringEncoding::Wide)),
725 hasNullTerm(true)
726{}
727
728constexpr SC::StringView::StringView(Span<const wchar_t> textSpan, bool nullTerm)
729 : textWide(textSpan.data()), textSizeInBytes(static_cast<SizeType>(textSpan.sizeInBytes())),
730 encoding(static_cast<SizeType>(StringEncoding::Wide)), hasNullTerm(nullTerm)
731{
732 SC_ASSERT_DEBUG(textSpan.sizeInBytes() <= MaxLength);
733}
734#endif
735
736[[nodiscard]] constexpr const char* SC::StringView::bytesIncludingTerminator() const
737{
738 SC_ASSERT_RELEASE(hasNullTerm);
739 return text;
740}
741
742template <typename StringIterator>
744{
745 // For GCC complaining about specialization in non-namespace scope
746 return getIterator(identity<StringIterator>());
747}
748
749template <typename Type>
750constexpr SC::StringIteratorASCII SC::StringView::getIterator(identity<Type>) const
751{
752 return StringIteratorASCII(text, text + textSizeInBytes);
753}
754constexpr SC::StringIteratorUTF8 SC::StringView::getIterator(identity<StringIteratorUTF8>) const
755{
756 return StringIteratorUTF8(text, text + textSizeInBytes);
757}
758constexpr SC::StringIteratorUTF16 SC::StringView::getIterator(identity<StringIteratorUTF16>) const
759{
760 return StringIteratorUTF16(text, text + textSizeInBytes);
761}
762
763template <typename StringIterator1, typename StringIterator2>
764constexpr bool SC::StringView::equalsIterator(StringIterator1 t1, StringIterator2 t2, size_t& points)
765{
766 StringCodePoint c1 = 0;
767 StringCodePoint c2 = 0;
768 while (t1.advanceRead(c1) and t2.advanceRead(c2))
769 {
770 if (c1 != c2)
771 {
772 return false;
773 }
774 points++;
775 }
776 return t1.isAtEnd() and t2.isAtEnd();
777}
778
779template <typename StringIterator>
780constexpr bool SC::StringView::equalsIterator(StringView other, size_t& points) const
781{
782 auto it = getIterator<StringIterator>();
783 switch (other.getEncoding())
784 {
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);
788 }
789 Assert::unreachable();
790}
791
792[[nodiscard]] inline auto SC::StringView::getNullTerminatedNative() const
793{
794#if SC_PLATFORM_WINDOWS
795 SC_ASSERT_RELEASE(hasNullTerm && (getEncoding() == StringEncoding::Utf16));
796 return reinterpret_cast<const wchar_t*>(text);
797#else
798 SC_ASSERT_RELEASE(hasNullTerm && (getEncoding() == StringEncoding::Utf8 || getEncoding() == StringEncoding::Ascii));
799 return text;
800#endif
801}
802
803[[nodiscard]] constexpr bool SC::StringView::operator==(StringView other) const
804{
805 if (hasCompatibleEncoding(other))
806 {
807 if (textSizeInBytes != other.textSizeInBytes)
808 return false;
809 if (__builtin_is_constant_evaluated())
810 {
811 auto it1 = text;
812 auto it2 = other.text;
813 auto sz = textSizeInBytes;
814 for (size_t idx = 0; idx < sz; ++idx)
815 if (it1[idx] != it2[idx])
816 return false;
817 }
818 else
819 {
820 if (text == nullptr)
821 {
822 return other.textSizeInBytes == 0;
823 }
824 if (other.text == nullptr)
825 {
826 return textSizeInBytes == 0;
827 }
828 return memcmp(text, other.text, textSizeInBytes) == 0;
829 }
830 }
831 size_t commonOverlappingPoints = 0;
832 return fullyOverlaps(other, commonOverlappingPoints);
833}
834
835constexpr bool SC::StringView::fullyOverlaps(StringView other, size_t& commonOverlappingPoints) const
836{
837 commonOverlappingPoints = 0;
838 switch (getEncoding())
839 {
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);
843 }
844 Assert::unreachable();
845}
846
848{
849 SC_ASSERT_RELEASE(hasNullTerm);
850 return textSizeInBytes > 0 ? textSizeInBytes + StringEncodingGetSize(getEncoding()) : 0;
851}
852
853template <typename Func>
854constexpr auto SC::StringView::withIterator(Func&& func) const
855{
856 switch (getEncoding())
857 {
858 case StringEncoding::Ascii: return func(getIterator<StringIteratorASCII>());
859 case StringEncoding::Utf8: return func(getIterator<StringIteratorUTF8>());
860 case StringEncoding::Utf16: return func(getIterator<StringIteratorUTF16>());
861 }
862 Assert::unreachable();
863}
864
865template <typename Func>
866constexpr auto SC::StringView::withIterators(StringView s1, StringView s2, Func&& func)
867{
868 return s1.withIterator([&s2, &func](auto it1)
869 { return s2.withIterator([&it1, &func](auto it2) { return func(it1, it2); }); });
870}
871
873{
874 return StringEncodingAreBinaryCompatible(getEncoding(), str.getEncoding());
875}
876
877template <typename StringIterator>
879{
880 const ssize_t numBytes = to.bytesDistanceFrom(from);
881 if (numBytes >= 0)
882 {
883 StringIterator fromEnd = from;
884 fromEnd.setToEnd();
885 if (fromEnd.bytesDistanceFrom(to) >= 0) // If current iterator of to is inside from range
886 return StringView({from.getCurrentIt(), static_cast<size_t>(numBytes)}, false,
887 StringIterator::getEncoding());
888 }
889 return StringView(); // TODO: Make StringView::fromIterators return bool to make it fallible
890}
891
892template <typename StringIterator>
894{
895 StringIterator endIt = it;
896 endIt.setToEnd();
897 const size_t numBytes = static_cast<size_t>(endIt.bytesDistanceFrom(it));
898 return StringView({it.getCurrentIt(), numBytes}, false, StringIterator::getEncoding());
899}
900
901template <typename StringIterator>
902constexpr SC::StringView SC::StringView::fromIteratorFromStart(StringIterator it)
903{
904 StringIterator start = it;
905 start.setToStart();
906 const size_t numBytes = static_cast<size_t>(it.bytesDistanceFrom(start));
907 return StringView({start.getCurrentIt(), numBytes}, false, StringIterator::getEncoding());
908}
909
911{
912 if (start < sizeInBytes())
913 return sliceStartLengthBytes(start, sizeInBytes() - start);
914 SC_ASSERT_RELEASE(start < sizeInBytes());
915 return StringView({text, 0}, false, getEncoding());
916}
917
918constexpr SC::StringView SC::StringView::sliceStartEndBytes(size_t start, size_t end) const
919{
920 if (end >= start)
921 return sliceStartLengthBytes(start, end - start);
922 SC_ASSERT_RELEASE(end >= start);
923 return StringView({text, 0}, false, getEncoding());
924}
925
926constexpr SC::StringView SC::StringView::sliceStartLengthBytes(size_t start, size_t length) const
927{
928 if (start + length > sizeInBytes())
929 {
930 SC_ASSERT_RELEASE(start + length > sizeInBytes());
931 return StringView({text, 0}, false, getEncoding());
932 }
933 return StringView({text + start, length}, hasNullTerm and (start + length == sizeInBytes()), getEncoding());
934}
#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
int int32_t
Platform independent (4) bytes signed int.
Definition: PrimitiveTypes.h:46
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
uint32_t StringCodePoint
UTF code point (32 bit)
Definition: StringIterator.h:13
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
@ Ascii
Encoding is ASCII.
@ Utf8
Encoding is UTF8.
@ Utf16
Encoding is UTF16-LE.
View over a contiguous sequence of items (pointer + size in elements).
Definition: Span.h:24
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:70
constexpr SizeType sizeInBytes() const
Size of Span in bytes.
Definition: Span.h:119
An read-only view over an ASCII string (to avoid including Strings library)
Definition: Span.h:249
Algorithms operating on strings (glob / wildcard).
Definition: StringView.h:675
A string iterator for ASCII strings.
Definition: StringIterator.h:245
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:408
constexpr void setToEnd()
Set current position to end of iterator range.
Definition: StringIterator.h:73
A string iterator for UTF16 strings.
Definition: StringIterator.h:264
A string iterator for UTF8 strings.
Definition: StringIterator.h:282
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:918
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:926
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:910
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.
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