4#include "../Foundation/Assert.h"
5#include "../Foundation/Span.h"
21#if SC_PLATFORM_WINDOWS
48template <
typename CharIterator>
51 static constexpr StringEncoding getEncoding() {
return CharIterator::getEncoding(); }
53 using CodeUnit = char;
64 [[nodiscard]]
constexpr bool isAtEnd()
const {
return it >= end; }
68 [[nodiscard]]
constexpr bool isAtStart()
const {
return it <= start; }
140 [[nodiscard]]
bool match(CodePoint c) {
return it < end and CharIterator::decode(it) == c; }
150 [[nodiscard]]
bool read(CodePoint& c);
208 template <
typename IteratorType>
209 [[nodiscard]]
bool endsWith(IteratorType other)
const;
214 template <
typename IteratorType>
218 [[nodiscard]]
bool advanceOfBytes(
ssize_t bytesLength);
221 static constexpr const CodeUnit* getNextOf(
const CodeUnit* src) {
return CharIterator::getNextOf(src); }
222 static constexpr const CodeUnit* getPreviousOf(
const CodeUnit* src) {
return CharIterator::getPreviousOf(src); }
223 constexpr StringIterator(
const CodeUnit* it,
const CodeUnit* end) : it(it), start(it), end(end) {}
224 constexpr auto* getCurrentIt()
const {
return it; }
226 const CodeUnit* start;
233 [[nodiscard]]
constexpr bool advanceUntilMatches(CodePoint c);
236 [[nodiscard]]
bool advanceUntilMatchesNonConstexpr(CodePoint c);
237 using StringIterator::StringIterator;
244 [[nodiscard]]
static constexpr const char* getNextOf(
const char* src) {
return src + 1; }
245 [[nodiscard]]
static constexpr const char* getPreviousOf(
const char* src) {
return src - 1; }
246 [[nodiscard]]
static constexpr CodePoint decode(
const char* src) {
return static_cast<CodePoint
>(*src); }
253 using StringIterator::StringIterator;
260 [[nodiscard]]
static const char* getNextOf(
const char* bytes);
262 [[nodiscard]]
static const char* getPreviousOf(
const char* bytes);
264 [[nodiscard]]
static uint32_t decode(
const char* bytes);
274 using StringIterator::StringIterator;
278 [[nodiscard]]
static const char* getNextOf(
const char* src);
280 [[nodiscard]]
static const char* getPreviousOf(
const char* src);
282 [[nodiscard]]
static uint32_t decode(
const char* src);
288 bool matches[256] = {
false};
293 matches[
static_cast<int>(c)] =
true;
316 Assert::unreachable();
319template <
typename CharIterator>
324 if (CharIterator::decode(it) == c)
331template <
typename CharIterator>
334 if (it < end and CharIterator::decode(it) == c)
342template <
typename CharIterator>
347 c = CharIterator::decode(it);
354template <
typename CharIterator>
365template <
typename CharIterator>
370 it = getPreviousOf(it);
376template <
typename CharIterator>
379 while (numCodePoints > 0)
391template <
typename CharIterator>
394 return it < end ? CharIterator::decode(getNextOf(it)) == c :
false;
397template <
typename CharIterator>
400 return it > start ? CharIterator::decode(getPreviousOf(it)) == c :
false;
403template <
typename CharIterator>
411template <
typename CharIterator>
414 return (it - other.it) *
static_cast<ssize_t>(
sizeof(CodeUnit));
418[[nodiscard]]
constexpr bool StringIteratorASCII::advanceUntilMatches(CodePoint c)
421 : advanceUntilMatchesNonConstexpr(c);
#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 char uint8_t
Platform independent (1) byte unsigned int.
Definition: PrimitiveTypes.h:36
unsigned int uint32_t
Platform independent (4) bytes unsigned int.
Definition: PrimitiveTypes.h:38
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:302
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:308
@ Ascii
Encoding is ASCII.
@ Native
Encoding is UTF8.
@ Utf16
Encoding is UTF16-LE.
View over a contiguous sequence of items (pointer + size in elements).
Definition: Span.h:20
A string iterator for ASCII strings.
Definition: StringIterator.h:232
A position inside a fixed range [start, end) of UTF code points.
Definition: StringIterator.h:50
constexpr void setToStart()
Rewind current position to start of iterator range.
Definition: StringIterator.h:57
constexpr ssize_t bytesDistanceFrom(StringIterator other) const
Get distance in bytes from current position to another StringIterator current position.
Definition: StringIterator.h:412
constexpr void setToEnd()
Set current position to end of iterator range.
Definition: StringIterator.h:60
bool advanceUntilMatchesAny(Span< const CodePoint > items, CodePoint &matched)
Advances position until any CodePoint in the given Span is found.
bool advanceBackwardIfMatches(CodePoint c)
Move position by one code point towards start if previous code point matches c
constexpr bool advanceIfMatches(CodePoint c)
Advance position only if next code point matches c.
Definition: StringIterator.h:332
constexpr bool isAtStart() const
Check if current position is at start of iterator range.
Definition: StringIterator.h:68
bool reverseAdvanceUntilMatches(CodePoint c)
Moves position towards start until CodePoint c is found or position == end
bool advanceBeforeFinding(StringIterator other)
Advances position towards end until a matching range of character equal to other[it,...
bool advanceUntilDifferentFrom(CodePoint c, CodePoint *optionalReadChar=nullptr)
Advances position until a code point different from c is found or end is reached.
bool advanceAfterFinding(StringIterator other)
Advances position towards end until a matching range of character equal to other[it,...
bool startsWith(IteratorType other) const
Check if this Iterator at its start matches entirely another Iterator's range.
bool reverseAdvanceCodePoints(size_t numCodePoints)
Move position backwards (towards start) by variable number of code pints.
constexpr bool stepBackward()
Move position to previous code point.
Definition: StringIterator.h:366
constexpr bool advanceCodePoints(size_t numCodePoints)
Move position forward (towards end) by variable number of code points.
Definition: StringIterator.h:377
bool reverseAdvanceUntilMatchesAny(Span< const CodePoint > items, CodePoint &matched)
Moves position towards start until any CodePoint in the given Span is found.
constexpr bool stepForward()
Move position to next code point.
Definition: StringIterator.h:355
bool endsWithAnyOf(Span< const CodePoint > codePoints) const
Check if this Iterator ends with any code point in the given span.
bool advanceIfMatchesAny(Span< const CodePoint > items)
Advance position only if any of the code points in given Span is matched.
constexpr bool isFollowedBy(CodePoint c)
Check if next code point is c
Definition: StringIterator.h:392
bool advanceBackwardRead(CodePoint &c)
Move to previous position and read code unit.
bool startsWithAnyOf(Span< const CodePoint > codePoints) const
Check if this Iterator starts with any code point in the given span.
constexpr StringIterator sliceFromStartUntil(StringIterator otherPoint) const
Returns another StringIterator range, starting from start to otherPoint position.
Definition: StringIterator.h:404
constexpr bool isPrecededBy(CodePoint c)
Check if previous code point is c
Definition: StringIterator.h:398
constexpr bool advanceUntilMatches(CodePoint c)
Advances position towards end until it matches CodePoint c or position == end
Definition: StringIterator.h:320
bool read(CodePoint &c)
Read code unit at current position.
constexpr bool advanceRead(CodePoint &c)
Decode code unit at current position and advance.
Definition: StringIterator.h:343
bool match(CodePoint c)
Check if code unit at current position matches CodePoint c
Definition: StringIterator.h:140
constexpr bool isAtEnd() const
Check if current position is at end of iterator range.
Definition: StringIterator.h:64
bool advanceIfMatchesRange(CodePoint first, CodePoint last)
Advance position if any code point in the range [first, last] is matched.
bool endsWith(IteratorType other) const
Check if this Iterator at its end matches entirely another Iterator's range.
bool advanceByLengthOf(StringIterator other)
Advances position by the same number of code points as other.
Definition: StringIterator.h:95
Builds a constexpr bool skip table of 256 entries used in some parsers.
Definition: StringIterator.h:287
A string iterator for UTF16 strings.
Definition: StringIterator.h:251
A string iterator for UTF8 strings.
Definition: StringIterator.h:269
Non-owning view over a range of characters with UTF Encoding.
Definition: StringView.h:47