4#include "../Foundation/Assert.h"
5#include "../Foundation/Span.h"
6#include "../Foundation/StringSpan.h"
22 return (encoding1 == encoding2) or (encoding2 == StringEncoding::Ascii and encoding1 == StringEncoding::Utf8) or
23 (encoding2 == StringEncoding::Utf8 and encoding1 == StringEncoding::Ascii);
34template <
typename CharIterator>
37 static constexpr StringEncoding getEncoding() {
return CharIterator::getEncoding(); }
39 using CodeUnit = char;
50 [[nodiscard]]
constexpr bool isAtEnd()
const {
return it >= end; }
54 [[nodiscard]]
constexpr bool isAtStart()
const {
return it <= start; }
70 template <
typename OtherIterator>
73 return advanceBeforeOrAfterFinding<OtherIterator, true>(other);
76 [[nodiscard]]
bool advanceAfterFinding(
StringIterator other) {
return advanceAfterFindingSameIterator(other); }
82 template <
typename OtherIterator>
85 return advanceBeforeOrAfterFinding<OtherIterator, false>(other);
88 [[nodiscard]]
bool advanceBeforeFinding(
StringIterator other) {
return advanceBeforeFindingSameIterator(other); }
138 [[nodiscard]]
bool match(CodePoint c) {
return it < end and CharIterator::decode(it) == c; }
148 [[nodiscard]]
bool read(CodePoint& c);
206 template <
typename IteratorType>
207 [[nodiscard]]
bool endsWith(IteratorType other)
const;
212 template <
typename IteratorType>
216 template <
typename OtherIterator,
bool after>
218 [[nodiscard]]
bool advanceAfterFindingSameIterator(
StringIterator other);
219 [[nodiscard]]
bool advanceBeforeFindingSameIterator(
StringIterator other);
220 [[nodiscard]]
bool advanceOfBytes(
ssize_t bytesLength);
223 static constexpr const CodeUnit* getNextOf(
const CodeUnit* src) {
return CharIterator::getNextOf(src); }
224 static constexpr const CodeUnit* getPreviousOf(
const CodeUnit* src) {
return CharIterator::getPreviousOf(src); }
225 constexpr StringIterator(
const CodeUnit* it,
const CodeUnit* end) : it(it), start(it), end(end) {}
227 const CodeUnit* start;
234 [[nodiscard]]
constexpr bool advanceUntilMatches(CodePoint c);
237 [[nodiscard]]
bool advanceUntilMatchesNonConstexpr(CodePoint c);
238 using StringIterator::StringIterator;
244 [[nodiscard]]
static constexpr StringEncoding getEncoding() {
return StringEncoding::Ascii; }
246 [[nodiscard]]
static constexpr const char* getNextOf(
const char* src) {
return src + 1; }
247 [[nodiscard]]
static constexpr const char* getPreviousOf(
const char* src) {
return src - 1; }
248 [[nodiscard]]
static constexpr CodePoint decode(
const char* src) {
return static_cast<CodePoint
>(*src); }
255 using StringIterator::StringIterator;
261 [[nodiscard]]
static StringEncoding getEncoding() {
return StringEncoding::Utf16; }
263 [[nodiscard]]
static const char* getNextOf(
const char* bytes);
265 [[nodiscard]]
static const char* getPreviousOf(
const char* bytes);
267 [[nodiscard]]
static uint32_t decode(
const char* bytes);
277 using StringIterator::StringIterator;
280 [[nodiscard]]
static StringEncoding getEncoding() {
return StringEncoding::Utf8; }
282 [[nodiscard]]
static const char* getNextOf(
const char* src);
284 [[nodiscard]]
static const char* getPreviousOf(
const char* src);
286 [[nodiscard]]
static uint32_t decode(
const char* src);
292 bool matches[256] = {
false};
297 matches[
static_cast<int>(c)] =
true;
306template <
typename CharIterator>
311 if (CharIterator::decode(it) == c)
318template <
typename CharIterator>
321 if (it < end and CharIterator::decode(it) == c)
329template <
typename CharIterator>
334 c = CharIterator::decode(it);
341template <
typename CharIterator>
352template <
typename CharIterator>
357 it = getPreviousOf(it);
363template <
typename CharIterator>
366 while (numCodePoints > 0)
378template <
typename CharIterator>
381 return it < end ? CharIterator::decode(getNextOf(it)) == c :
false;
384template <
typename CharIterator>
387 return it > start ? CharIterator::decode(getPreviousOf(it)) == c :
false;
390template <
typename CharIterator>
398template <
typename CharIterator>
401 return (it - other.it) *
static_cast<ssize_t>(
sizeof(CodeUnit));
405[[nodiscard]]
constexpr bool StringIteratorASCII::advanceUntilMatches(CodePoint c)
407#if defined(__clang__)
408#pragma clang diagnostic push
409#pragma clang diagnostic ignored "-Wunreachable-code"
412 : advanceUntilMatchesNonConstexpr(c);
413#if defined(__clang__)
414#pragma clang diagnostic pop
#define SC_ASSERT_RELEASE(e)
Assert expression e to be true.
Definition Assert.h:42
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:20
uint32_t StringCodePoint
UTF code point (32 bit)
Definition StringIterator.h:14
View over a contiguous sequence of items (pointer + size in elements).
Definition Span.h:29
A string iterator for ASCII strings.
Definition StringIterator.h:233
Builds a constexpr bool skip table of 256 entries used in some parsers.
Definition StringIterator.h:291
A string iterator for UTF16 strings.
Definition StringIterator.h:253
A string iterator for UTF8 strings.
Definition StringIterator.h:272
A position inside a fixed range [start, end) of UTF code points.
Definition StringIterator.h:36
constexpr void setToStart()
Rewind current position to start of iterator range.
Definition StringIterator.h:43
constexpr ssize_t bytesDistanceFrom(StringIterator other) const
Get distance in bytes from current position to another StringIterator current position.
Definition StringIterator.h:399
constexpr void setToEnd()
Set current position to end of iterator range.
Definition StringIterator.h:46
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:319
bool advanceBeforeFinding(StringIterator< OtherIterator > other)
Advances position towards end until a matching range of character equal to other[it,...
Definition StringIterator.h:83
constexpr bool isAtStart() const
Check if current position is at start of iterator range.
Definition StringIterator.h:54
bool reverseAdvanceUntilMatches(CodePoint c)
Moves position towards start until CodePoint c is found or position == end
bool advanceAfterFinding(StringIterator< OtherIterator > other)
Advances position towards end until a matching range of character equal to other[it,...
Definition StringIterator.h:71
bool advanceUntilDifferentFrom(CodePoint c, CodePoint *optionalReadChar=nullptr)
Advances position until a code point different from c is found or end is reached.
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:353
constexpr bool advanceCodePoints(size_t numCodePoints)
Move position forward (towards end) by variable number of code points.
Definition StringIterator.h:364
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:342
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:379
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:391
constexpr bool isPrecededBy(CodePoint c)
Check if previous code point is c
Definition StringIterator.h:385
constexpr bool advanceUntilMatches(CodePoint c)
Advances position towards end until it matches CodePoint c or position == end
Definition StringIterator.h:307
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:330
bool match(CodePoint c)
Check if code unit at current position matches CodePoint c
Definition StringIterator.h:138
constexpr bool isAtEnd() const
Check if current position is at end of iterator range.
Definition StringIterator.h:50
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:93
Non-owning view over a range of characters with UTF Encoding.
Definition StringView.h:46