4#include "../Foundation/Assert.h"
5#include "../Foundation/Span.h"
6#include "../Foundation/StringSpan.h"
7#include "StringsExport.h"
23 return (encoding1 == encoding2) or (encoding2 == StringEncoding::Ascii and encoding1 == StringEncoding::Utf8) or
24 (encoding2 == StringEncoding::Utf8 and encoding1 == StringEncoding::Ascii);
35template <
typename CharIterator>
38 static constexpr StringEncoding getEncoding() {
return CharIterator::getEncoding(); }
40 using CodeUnit = char;
51 [[nodiscard]]
constexpr bool isAtEnd()
const {
return it >= end; }
55 [[nodiscard]]
constexpr bool isAtStart()
const {
return it <= start; }
71 template <
typename OtherIterator>
74 return advanceBeforeOrAfterFinding<OtherIterator, true>(other);
77 [[nodiscard]]
bool advanceAfterFinding(
StringIterator other) {
return advanceAfterFindingSameIterator(other); }
83 template <
typename OtherIterator>
86 return advanceBeforeOrAfterFinding<OtherIterator, false>(other);
89 [[nodiscard]]
bool advanceBeforeFinding(
StringIterator other) {
return advanceBeforeFindingSameIterator(other); }
139 [[nodiscard]]
bool match(CodePoint c) {
return it < end and CharIterator::decode(it, end) == c; }
149 [[nodiscard]]
bool read(CodePoint& c);
207 template <
typename IteratorType>
208 [[nodiscard]]
bool endsWith(IteratorType other)
const;
213 template <
typename IteratorType>
217 template <
typename OtherIterator,
bool after>
219 [[nodiscard]]
bool advanceAfterFindingSameIterator(
StringIterator other);
220 [[nodiscard]]
bool advanceBeforeFindingSameIterator(
StringIterator other);
221 [[nodiscard]]
bool advanceOfBytes(
ssize_t bytesLength);
224 static constexpr const CodeUnit* getNextOf(
const CodeUnit* src,
const char* end)
226 return CharIterator::getNextOf(src, end);
228 static constexpr const CodeUnit* getPreviousOf(
const CodeUnit* src,
const char* start)
230 return CharIterator::getPreviousOf(src, start);
232 constexpr StringIterator(
const CodeUnit* it,
const CodeUnit* end) : it(it), start(it), end(end) {}
234 const CodeUnit* start;
241 [[nodiscard]]
constexpr bool advanceUntilMatches(CodePoint c);
244 [[nodiscard]]
bool advanceUntilMatchesNonConstexpr(CodePoint c);
245 using StringIterator::StringIterator;
251 [[nodiscard]]
static constexpr StringEncoding getEncoding() {
return StringEncoding::Ascii; }
253 [[nodiscard]]
static constexpr const char* getNextOf(
const char* src,
const char*) {
return src + 1; }
254 [[nodiscard]]
static constexpr const char* getPreviousOf(
const char* src,
const char*) {
return src - 1; }
255 [[nodiscard]]
static constexpr CodePoint decode(
const char* src,
const char*)
257 return static_cast<CodePoint
>(*src);
265 using StringIterator::StringIterator;
271 [[nodiscard]]
static StringEncoding getEncoding() {
return StringEncoding::Utf16; }
273 [[nodiscard]]
static const char* getNextOf(
const char* bytes,
const char* end);
274 [[nodiscard]]
static const char* getPreviousOf(
const char* bytes,
const char* start);
276 [[nodiscard]]
static uint32_t decode(
const char* bytes,
const char* end);
286 using StringIterator::StringIterator;
289 [[nodiscard]]
static StringEncoding getEncoding() {
return StringEncoding::Utf8; }
291 [[nodiscard]]
static const char* getNextOf(
const char* src,
const char* end);
292 [[nodiscard]]
static const char* getPreviousOf(
const char* src,
const char* start);
294 [[nodiscard]]
static uint32_t decode(
const char* src,
const char* end);
300 bool matches[256] = {
false};
305 matches[
static_cast<int>(c)] =
true;
314template <
typename CharIterator>
319 if (CharIterator::decode(it, end) == c)
321 it = getNextOf(it, end);
326template <
typename CharIterator>
329 if (it < end and CharIterator::decode(it, end) == c)
331 it = getNextOf(it, end);
337template <
typename CharIterator>
342 c = CharIterator::decode(it, end);
343 it = getNextOf(it, end);
349template <
typename CharIterator>
354 it = getNextOf(it, end);
360template <
typename CharIterator>
365 it = getPreviousOf(it, start);
371template <
typename CharIterator>
374 while (numCodePoints > 0)
381 it = getNextOf(it, end);
386template <
typename CharIterator>
389 return it < end ? CharIterator::decode(getNextOf(it, end), end) == c :
false;
392template <
typename CharIterator>
395 return it > start ? CharIterator::decode(getPreviousOf(it, start), it) == c :
false;
398template <
typename CharIterator>
406template <
typename CharIterator>
409 return (it - other.it) *
static_cast<ssize_t>(
sizeof(CodeUnit));
413[[nodiscard]]
constexpr bool StringIteratorASCII::advanceUntilMatches(CodePoint c)
415#if defined(__clang__)
416#pragma clang diagnostic push
417#pragma clang diagnostic ignored "-Wunreachable-code"
420 : advanceUntilMatchesNonConstexpr(c);
421#if defined(__clang__)
422#pragma clang diagnostic pop
#define SC_ASSERT_RELEASE(e)
Assert expression e to be true.
Definition Assert.h:48
decltype(static_cast< char * >(nullptr) - static_cast< char * >(nullptr)) ssize_t
Platform independent signed size type.
Definition PrimitiveTypes.h:46
unsigned int uint32_t
Platform independent (4) bytes unsigned int.
Definition PrimitiveTypes.h:29
constexpr bool StringEncodingAreBinaryCompatible(StringEncoding encoding1, StringEncoding encoding2)
Checks if two encodings have the same utf unit size.
Definition StringIterator.h:21
uint32_t StringCodePoint
UTF code point (32 bit)
Definition StringIterator.h:15
View over a contiguous sequence of items (pointer + size in elements).
Definition Span.h:29
A string iterator for ASCII strings.
Definition StringIterator.h:240
Builds a constexpr bool skip table of 256 entries used in some parsers.
Definition StringIterator.h:299
A string iterator for UTF16 strings.
Definition StringIterator.h:263
A string iterator for UTF8 strings.
Definition StringIterator.h:281
A position inside a fixed range [start, end) of UTF code points.
Definition StringIterator.h:37
constexpr void setToStart()
Rewind current position to start of iterator range.
Definition StringIterator.h:44
constexpr ssize_t bytesDistanceFrom(StringIterator other) const
Get distance in bytes from current position to another StringIterator current position.
Definition StringIterator.h:407
constexpr void setToEnd()
Set current position to end of iterator range.
Definition StringIterator.h:47
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:327
bool advanceBeforeFinding(StringIterator< OtherIterator > other)
Advances position towards end until a matching range of character equal to other[it,...
Definition StringIterator.h:84
constexpr bool isAtStart() const
Check if current position is at start of iterator range.
Definition StringIterator.h:55
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:72
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:361
constexpr bool advanceCodePoints(size_t numCodePoints)
Move position forward (towards end) by variable number of code points.
Definition StringIterator.h:372
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:350
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:387
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:399
constexpr bool isPrecededBy(CodePoint c)
Check if previous code point is c
Definition StringIterator.h:393
constexpr bool advanceUntilMatches(CodePoint c)
Advances position towards end until it matches CodePoint c or position == end
Definition StringIterator.h:315
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:338
bool match(CodePoint c)
Check if code unit at current position matches CodePoint c
Definition StringIterator.h:139
constexpr bool isAtEnd() const
Check if current position is at end of iterator range.
Definition StringIterator.h:51
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:94
Non-owning view over a range of characters with UTF Encoding.
Definition StringView.h:47