Sane C++ Libraries
C++ Platform Abstraction Libraries
StringIterator.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Foundation/Assert.h" //Assert::unreachable
5#include "../Foundation/Span.h"
6
7namespace SC
8{
11
14
17{
18 Ascii = 0,
19 Utf8 = 1,
20 Utf16 = 2,
21#if SC_PLATFORM_WINDOWS
22 Native = Utf16,
23 Wide = Utf16
24#else
25 Native = Utf8
26#endif
27};
28
33constexpr bool StringEncodingAreBinaryCompatible(StringEncoding encoding1, StringEncoding encoding2);
34
39
48template <typename CharIterator>
50{
51 static constexpr StringEncoding getEncoding() { return CharIterator::getEncoding(); }
52
53 using CodeUnit = char;
54 using CodePoint = StringCodePoint;
55
57 constexpr void setToStart() { it = start; }
58
60 constexpr void setToEnd() { it = end; }
61
64 [[nodiscard]] constexpr bool isAtEnd() const { return it >= end; }
65
68 [[nodiscard]] constexpr bool isAtStart() const { return it <= start; }
69
73 [[nodiscard]] constexpr bool advanceUntilMatches(CodePoint c);
74
78 [[nodiscard]] bool reverseAdvanceUntilMatches(CodePoint c);
79
84 [[nodiscard]] bool advanceAfterFinding(StringIterator other);
85
90 [[nodiscard]] bool advanceBeforeFinding(StringIterator other);
91
95 [[nodiscard]] bool advanceByLengthOf(StringIterator other) { return advanceOfBytes(other.end - other.it); }
96
101 [[nodiscard]] bool advanceUntilMatchesAny(Span<const CodePoint> items, CodePoint& matched);
102
107 [[nodiscard]] bool reverseAdvanceUntilMatchesAny(Span<const CodePoint> items, CodePoint& matched);
108
113 [[nodiscard]] bool advanceUntilDifferentFrom(CodePoint c, CodePoint* optionalReadChar = nullptr);
114
118 [[nodiscard]] constexpr bool advanceIfMatches(CodePoint c);
119
123 [[nodiscard]] bool advanceBackwardIfMatches(CodePoint c);
124
129
134 [[nodiscard]] bool advanceIfMatchesRange(CodePoint first, CodePoint last);
135
140 [[nodiscard]] bool match(CodePoint c) { return it < end and CharIterator::decode(it) == c; }
141
145 [[nodiscard]] constexpr bool advanceRead(CodePoint& c);
146
150 [[nodiscard]] bool read(CodePoint& c);
151
155 [[nodiscard]] bool advanceBackwardRead(CodePoint& c);
156
159 [[nodiscard]] constexpr bool stepForward();
160
163 [[nodiscard]] constexpr bool stepBackward();
164
168 [[nodiscard]] constexpr bool advanceCodePoints(size_t numCodePoints);
169
173 [[nodiscard]] bool reverseAdvanceCodePoints(size_t numCodePoints);
174
178 [[nodiscard]] constexpr bool isFollowedBy(CodePoint c);
179
183 [[nodiscard]] constexpr bool isPrecededBy(CodePoint c);
184
188 [[nodiscard]] constexpr StringIterator sliceFromStartUntil(StringIterator otherPoint) const;
189
193 [[nodiscard]] constexpr ssize_t bytesDistanceFrom(StringIterator other) const;
194
198 [[nodiscard]] bool endsWithAnyOf(Span<const CodePoint> codePoints) const;
199
203 [[nodiscard]] bool startsWithAnyOf(Span<const CodePoint> codePoints) const;
204
208 template <typename IteratorType>
209 [[nodiscard]] bool endsWith(IteratorType other) const;
210
214 template <typename IteratorType>
215 [[nodiscard]] bool startsWith(IteratorType other) const;
216
217 protected:
218 [[nodiscard]] bool advanceOfBytes(ssize_t bytesLength);
219
220 friend struct StringView;
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; }
225 const CodeUnit* it;
226 const CodeUnit* start;
227 const CodeUnit* end;
228};
229
231struct SC_COMPILER_EXPORT StringIteratorASCII : public StringIterator<StringIteratorASCII>
232{
233 [[nodiscard]] constexpr bool advanceUntilMatches(CodePoint c);
234
235 private:
236 [[nodiscard]] bool advanceUntilMatchesNonConstexpr(CodePoint c);
237 using StringIterator::StringIterator;
239 friend Parent;
240 friend struct StringView;
241
242 [[nodiscard]] static constexpr StringEncoding getEncoding() { return StringEncoding::Ascii; }
243
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); }
247};
248
250struct SC_COMPILER_EXPORT StringIteratorUTF16 : public StringIterator<StringIteratorUTF16>
251{
252 private:
253 using StringIterator::StringIterator;
255 friend Parent;
256 friend struct StringView;
257
258 [[nodiscard]] static StringEncoding getEncoding() { return StringEncoding::Utf16; }
259
260 [[nodiscard]] static const char* getNextOf(const char* bytes);
261
262 [[nodiscard]] static const char* getPreviousOf(const char* bytes);
263
264 [[nodiscard]] static uint32_t decode(const char* bytes);
265};
266
268struct SC_COMPILER_EXPORT StringIteratorUTF8 : public StringIterator<StringIteratorUTF8>
269{
270 private:
272 friend Parent;
273 friend struct StringView;
274 using StringIterator::StringIterator;
275
276 [[nodiscard]] static StringEncoding getEncoding() { return StringEncoding::Utf8; }
277
278 [[nodiscard]] static const char* getNextOf(const char* src);
279
280 [[nodiscard]] static const char* getPreviousOf(const char* src);
281
282 [[nodiscard]] static uint32_t decode(const char* src);
283};
284
287{
288 bool matches[256] = {false};
290 {
291 for (auto c : chars)
292 {
293 matches[static_cast<int>(c)] = true;
294 }
295 }
296};
298
299//-----------------------------------------------------------------------------------------------------------------------
300// Implementations Details
301//-----------------------------------------------------------------------------------------------------------------------
303{
304 return (encoding1 == encoding2) or (encoding2 == StringEncoding::Ascii and encoding1 == StringEncoding::Utf8) or
305 (encoding2 == StringEncoding::Utf8 and encoding1 == StringEncoding::Ascii);
306}
307
309{
310 switch (encoding)
311 {
312 case StringEncoding::Utf16: return 2;
313 case StringEncoding::Ascii: return 1;
314 case StringEncoding::Utf8: return 1;
315 }
316 Assert::unreachable();
317}
318
319template <typename CharIterator>
321{
322 while (it < end)
323 {
324 if (CharIterator::decode(it) == c)
325 return true;
326 it = getNextOf(it);
327 }
328 return false;
329}
330
331template <typename CharIterator>
333{
334 if (it < end and CharIterator::decode(it) == c)
335 {
336 it = getNextOf(it);
337 return true;
338 }
339 return false;
340}
341
342template <typename CharIterator>
344{
345 if (it < end)
346 {
347 c = CharIterator::decode(it);
348 it = getNextOf(it);
349 return true;
350 }
351 return false;
352}
353
354template <typename CharIterator>
356{
357 if (it < end)
358 {
359 it = getNextOf(it);
360 return true;
361 }
362 return false;
363}
364
365template <typename CharIterator>
367{
368 if (it > start)
369 {
370 it = getPreviousOf(it);
371 return true;
372 }
373 return false;
374}
375
376template <typename CharIterator>
377constexpr bool StringIterator<CharIterator>::advanceCodePoints(size_t numCodePoints)
378{
379 while (numCodePoints > 0)
380 {
381 numCodePoints -= 1;
382 if (it >= end)
383 {
384 return false;
385 }
386 it = getNextOf(it);
387 }
388 return true;
389}
390
391template <typename CharIterator>
393{
394 return it < end ? CharIterator::decode(getNextOf(it)) == c : false;
395}
396
397template <typename CharIterator>
399{
400 return it > start ? CharIterator::decode(getPreviousOf(it)) == c : false;
401}
402
403template <typename CharIterator>
405 StringIterator otherPoint) const
406{
407 SC_ASSERT_RELEASE(it <= otherPoint.it);
408 return StringIterator(it, otherPoint.it);
409}
410
411template <typename CharIterator>
413{
414 return (it - other.it) * static_cast<ssize_t>(sizeof(CodeUnit));
415}
416
417// StringIteratorASCII
418[[nodiscard]] constexpr bool StringIteratorASCII::advanceUntilMatches(CodePoint c)
419{
420 return __builtin_is_constant_evaluated() ? StringIterator::advanceUntilMatches(c)
421 : advanceUntilMatchesNonConstexpr(c);
422}
423
424} // namespace SC
#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.
@ Utf8
Encoding is UTF8.
@ 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