4#include "../Foundation/Assert.h"
5#include "../Foundation/Span.h"
21#if SC_PLATFORM_WINDOWS
50 Assert::unreachable();
61template <
typename CharIterator>
64 static constexpr StringEncoding getEncoding() {
return CharIterator::getEncoding(); }
66 using CodeUnit = char;
77 [[nodiscard]]
constexpr bool isAtEnd()
const {
return it >= end; }
81 [[nodiscard]]
constexpr bool isAtStart()
const {
return it <= start; }
153 [[nodiscard]]
bool match(CodePoint c) {
return it < end and CharIterator::decode(it) == c; }
163 [[nodiscard]]
bool read(CodePoint& c);
221 template <
typename IteratorType>
222 [[nodiscard]]
bool endsWith(IteratorType other)
const;
227 template <
typename IteratorType>
231 [[nodiscard]]
bool advanceOfBytes(
ssize_t bytesLength);
234 static constexpr const CodeUnit* getNextOf(
const CodeUnit* src) {
return CharIterator::getNextOf(src); }
235 static constexpr const CodeUnit* getPreviousOf(
const CodeUnit* src) {
return CharIterator::getPreviousOf(src); }
236 constexpr StringIterator(
const CodeUnit* it,
const CodeUnit* end) : it(it), start(it), end(end) {}
238 const CodeUnit* start;
245 [[nodiscard]]
constexpr bool advanceUntilMatches(CodePoint c);
248 [[nodiscard]]
bool advanceUntilMatchesNonConstexpr(CodePoint c);
249 using StringIterator::StringIterator;
255 [[nodiscard]]
static constexpr StringEncoding getEncoding() {
return StringEncoding::Ascii; }
257 [[nodiscard]]
static constexpr const char* getNextOf(
const char* src) {
return src + 1; }
258 [[nodiscard]]
static constexpr const char* getPreviousOf(
const char* src) {
return src - 1; }
259 [[nodiscard]]
static constexpr CodePoint decode(
const char* src) {
return static_cast<CodePoint
>(*src); }
266 using StringIterator::StringIterator;
272 [[nodiscard]]
static StringEncoding getEncoding() {
return StringEncoding::Utf16; }
274 [[nodiscard]]
static const char* getNextOf(
const char* bytes);
276 [[nodiscard]]
static const char* getPreviousOf(
const char* bytes);
278 [[nodiscard]]
static uint32_t decode(
const char* bytes);
288 using StringIterator::StringIterator;
291 [[nodiscard]]
static StringEncoding getEncoding() {
return StringEncoding::Utf8; }
293 [[nodiscard]]
static const char* getNextOf(
const char* src);
295 [[nodiscard]]
static const char* getPreviousOf(
const char* src);
297 [[nodiscard]]
static uint32_t decode(
const char* src);
303 bool matches[256] = {
false};
308 matches[
static_cast<int>(c)] =
true;
317template <
typename CharIterator>
322 if (CharIterator::decode(it) == c)
329template <
typename CharIterator>
332 if (it < end and CharIterator::decode(it) == c)
340template <
typename CharIterator>
345 c = CharIterator::decode(it);
352template <
typename CharIterator>
363template <
typename CharIterator>
368 it = getPreviousOf(it);
374template <
typename CharIterator>
377 while (numCodePoints > 0)
389template <
typename CharIterator>
392 return it < end ? CharIterator::decode(getNextOf(it)) == c :
false;
395template <
typename CharIterator>
398 return it > start ? CharIterator::decode(getPreviousOf(it)) == c :
false;
401template <
typename CharIterator>
409template <
typename CharIterator>
412 return (it - other.it) *
static_cast<ssize_t>(
sizeof(CodeUnit));
416[[nodiscard]]
constexpr bool StringIteratorASCII::advanceUntilMatches(CodePoint c)
419 : 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
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.
@ Native
Encoding is UTF8.
@ Utf16
Encoding is UTF16-LE.
View over a contiguous sequence of items (pointer + size in elements).
Definition Span.h:32
A string iterator for ASCII strings.
Definition StringIterator.h:244
Builds a constexpr bool skip table of 256 entries used in some parsers.
Definition StringIterator.h:302
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 void setToStart()
Rewind current position to start of iterator range.
Definition StringIterator.h:70
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
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:330
constexpr bool isAtStart() const
Check if current position is at start of iterator range.
Definition StringIterator.h:81
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:364
constexpr bool advanceCodePoints(size_t numCodePoints)
Move position forward (towards end) by variable number of code points.
Definition StringIterator.h:375
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:353
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:390
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:402
constexpr bool isPrecededBy(CodePoint c)
Check if previous code point is c
Definition StringIterator.h:396
constexpr bool advanceUntilMatches(CodePoint c)
Advances position towards end until it matches CodePoint c or position == end
Definition StringIterator.h:318
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:341
bool match(CodePoint c)
Check if code unit at current position matches CodePoint c
Definition StringIterator.h:153
constexpr bool isAtEnd() const
Check if current position is at end of iterator range.
Definition StringIterator.h:77
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:108
Non-owning view over a range of characters with UTF Encoding.
Definition StringView.h:47