Sane C++ Libraries
C++ Platform Abstraction Libraries
SC::StringIterator< CharIterator > Struct Template Reference

A position inside a fixed range [start, end) of UTF code points. More...

#include <StringIterator.h>

Public Types

using CodeUnit = char
 
using CodePoint = StringCodePoint
 

Public Member Functions

constexpr void setToStart ()
 Rewind current position to start of iterator range. More...
 
constexpr void setToEnd ()
 Set current position to end of iterator range. More...
 
constexpr bool isAtEnd () const
 Check if current position is at end of iterator range. More...
 
constexpr bool isAtStart () const
 Check if current position is at start of iterator range. More...
 
constexpr bool advanceUntilMatches (CodePoint c)
 Advances position towards end until it matches CodePoint c or position == end More...
 
bool reverseAdvanceUntilMatches (CodePoint c)
 Moves position towards start until CodePoint c is found or position == end More...
 
bool advanceAfterFinding (StringIterator other)
 Advances position towards end until a matching range of character equal to other[it, end) is found. More...
 
bool advanceBeforeFinding (StringIterator other)
 Advances position towards end until a matching range of character equal to other[it, end) is found. More...
 
bool advanceByLengthOf (StringIterator other)
 Advances position by the same number of code points as other. More...
 
bool advanceUntilMatchesAny (Span< const CodePoint > items, CodePoint &matched)
 Advances position until any CodePoint in the given Span is found. More...
 
bool reverseAdvanceUntilMatchesAny (Span< const CodePoint > items, CodePoint &matched)
 Moves position towards start until any CodePoint in the given Span is found. More...
 
bool advanceUntilDifferentFrom (CodePoint c, CodePoint *optionalReadChar=nullptr)
 Advances position until a code point different from c is found or end is reached. More...
 
constexpr bool advanceIfMatches (CodePoint c)
 Advance position only if next code point matches c. More...
 
bool advanceBackwardIfMatches (CodePoint c)
 Move position by one code point towards start if previous code point matches c More...
 
bool advanceIfMatchesAny (Span< const CodePoint > items)
 Advance position only if any of the code points in given Span is matched. More...
 
bool advanceIfMatchesRange (CodePoint first, CodePoint last)
 Advance position if any code point in the range [first, last] is matched. More...
 
bool match (CodePoint c)
 Check if code unit at current position matches CodePoint c More...
 
constexpr bool advanceRead (CodePoint &c)
 Decode code unit at current position and advance. More...
 
bool read (CodePoint &c)
 Read code unit at current position. More...
 
bool advanceBackwardRead (CodePoint &c)
 Move to previous position and read code unit. More...
 
constexpr bool stepForward ()
 Move position to next code point. More...
 
constexpr bool stepBackward ()
 Move position to previous code point. More...
 
constexpr bool advanceCodePoints (size_t numCodePoints)
 Move position forward (towards end) by variable number of code points. More...
 
bool reverseAdvanceCodePoints (size_t numCodePoints)
 Move position backwards (towards start) by variable number of code pints. More...
 
constexpr bool isFollowedBy (CodePoint c)
 Check if next code point is c More...
 
constexpr bool isPrecededBy (CodePoint c)
 Check if previous code point is c More...
 
constexpr StringIterator sliceFromStartUntil (StringIterator otherPoint) const
 Returns another StringIterator range, starting from start to otherPoint position. More...
 
constexpr ssize_t bytesDistanceFrom (StringIterator other) const
 Get distance in bytes from current position to another StringIterator current position. More...
 
bool endsWithAnyOf (Span< const CodePoint > codePoints) const
 Check if this Iterator ends with any code point in the given span. More...
 
bool startsWithAnyOf (Span< const CodePoint > codePoints) const
 Check if this Iterator starts with any code point in the given span. More...
 
template<typename IteratorType >
bool endsWith (IteratorType other) const
 Check if this Iterator at its end matches entirely another Iterator's range. More...
 
template<typename IteratorType >
bool startsWith (IteratorType other) const
 Check if this Iterator at its start matches entirely another Iterator's range. More...
 

Static Public Member Functions

static constexpr StringEncoding getEncoding ()
 

Protected Member Functions

bool advanceOfBytes (ssize_t bytesLength)
 
constexpr StringIterator (const CodeUnit *it, const CodeUnit *end)
 
constexpr auto * getCurrentIt () const
 

Static Protected Member Functions

static constexpr const CodeUnit * getNextOf (const CodeUnit *src)
 
static constexpr const CodeUnit * getPreviousOf (const CodeUnit *src)
 

Protected Attributes

const CodeUnit * it
 
const CodeUnit * start
 
const CodeUnit * end
 

Friends

struct StringView
 

Detailed Description

template<typename CharIterator>
struct SC::StringIterator< CharIterator >

A position inside a fixed range [start, end) of UTF code points.

It's a range of bytes (start and end pointers) with a current pointer pointing at a specific code point of the range. There are three classes derived from it (SC::StringIteratorASCII, SC::StringIteratorUTF8 and SC::StringIteratorUTF16) and they allow doing operations along the string view in UTF code points.

Note
Code points are not the same as perceived characters (that would be grapheme clusters). Invariants: start <= end and it >= start and it <= end.
Template Parameters
CharIteratorStringIteratorASCII, StringIteratorUTF8 or StringIteratorUTF16

Member Function Documentation

◆ advanceAfterFinding()

template<typename CharIterator >
bool SC::StringIterator< CharIterator >::advanceAfterFinding ( StringIterator< CharIterator >  other)

Advances position towards end until a matching range of character equal to other[it, end) is found.

Position pointer is advanced additional after the matching range.

Parameters
otherThe range of character to be found [it, end)
Returns
true if other was found, false if end was reached

◆ advanceBackwardIfMatches()

template<typename CharIterator >
bool SC::StringIterator< CharIterator >::advanceBackwardIfMatches ( CodePoint  c)

Move position by one code point towards start if previous code point matches c

Parameters
cThe CodePoint being searched
Returns
true if next code point matches c, false otherwise or if position is already at end

◆ advanceBackwardRead()

template<typename CharIterator >
bool SC::StringIterator< CharIterator >::advanceBackwardRead ( CodePoint &  c)

Move to previous position and read code unit.

Parameters
coutput code point read
Returns
true if position is at start before decoding code unit

◆ advanceBeforeFinding()

template<typename CharIterator >
bool SC::StringIterator< CharIterator >::advanceBeforeFinding ( StringIterator< CharIterator >  other)

Advances position towards end until a matching range of character equal to other[it, end) is found.

Position pointer is stopped before the first matching character of the range.

Parameters
otherThe range of character to be found [it, end)
Returns
true if other was found, false if end was reached

◆ advanceByLengthOf()

template<typename CharIterator >
bool SC::StringIterator< CharIterator >::advanceByLengthOf ( StringIterator< CharIterator >  other)
inline

Advances position by the same number of code points as other.

Parameters
otherThe other range of character
Returns
true if advance succeeded, false if end was reached

◆ advanceCodePoints()

template<typename CharIterator >
constexpr bool SC::StringIterator< CharIterator >::advanceCodePoints ( size_t  numCodePoints)
constexpr

Move position forward (towards end) by variable number of code points.

Parameters
numCodePointsnumber of code points to move forward
Returns
true if it's possible advancing numCodePoints before reaching end

◆ advanceIfMatches()

template<typename CharIterator >
constexpr bool SC::StringIterator< CharIterator >::advanceIfMatches ( CodePoint  c)
constexpr

Advance position only if next code point matches c.

Parameters
cThe CodePoint being searched
Returns
true if next code point matches c, false otherwise or if position is already at end

◆ advanceIfMatchesAny()

template<typename CharIterator >
bool SC::StringIterator< CharIterator >::advanceIfMatchesAny ( Span< const CodePoint >  items)

Advance position only if any of the code points in given Span is matched.

Parameters
itemsSpan of points to be checked
Returns
true if next code point was successfully matched

◆ advanceIfMatchesRange()

template<typename CharIterator >
bool SC::StringIterator< CharIterator >::advanceIfMatchesRange ( CodePoint  first,
CodePoint  last 
)

Advance position if any code point in the range [first, last] is matched.

Parameters
firstThe initial CodePoint defining the range to be checked
lastThe final CodePoint defining the range to be checked
Returns
true if a code point in the given range was matched

◆ advanceRead()

template<typename CharIterator >
constexpr bool SC::StringIterator< CharIterator >::advanceRead ( CodePoint &  c)
constexpr

Decode code unit at current position and advance.

Parameters
coutput code point read
Returns
true if position is at end before decoding code unit

◆ advanceUntilDifferentFrom()

template<typename CharIterator >
bool SC::StringIterator< CharIterator >::advanceUntilDifferentFrom ( CodePoint  c,
CodePoint *  optionalReadChar = nullptr 
)

Advances position until a code point different from c is found or end is reached.

Parameters
cThe CodePoint to be compared
optionalReadCharThe CodePoint that was found, different from c
Returns
true if it finds at least one code point different from c, false if iterator ends before finding it

◆ advanceUntilMatches()

template<typename CharIterator >
constexpr bool SC::StringIterator< CharIterator >::advanceUntilMatches ( CodePoint  c)
constexpr

Advances position towards end until it matches CodePoint c or position == end

Parameters
cThe CodePoint to be searched
Returns
true if c was found, false if end was reached

◆ advanceUntilMatchesAny()

template<typename CharIterator >
bool SC::StringIterator< CharIterator >::advanceUntilMatchesAny ( Span< const CodePoint >  items,
CodePoint &  matched 
)

Advances position until any CodePoint in the given Span is found.

Parameters
itemsA contiguous span of CodePoint to match
matchedThe matched CodePoint in the Span
Returns
true if one CodePoint was matched, false if end was reached

◆ bytesDistanceFrom()

template<typename CharIterator >
constexpr ssize_t SC::StringIterator< CharIterator >::bytesDistanceFrom ( StringIterator< CharIterator >  other) const
constexpr

Get distance in bytes from current position to another StringIterator current position.

Parameters
otherThe StringIterator from which to compute distance
Returns
(signed) number of bytes between the two StringIterator

◆ endsWith()

template<typename CharIterator >
template<typename IteratorType >
bool SC::StringIterator< CharIterator >::endsWith ( IteratorType  other) const

Check if this Iterator at its end matches entirely another Iterator's range.

Parameters
otherThe other iterator to match
Returns
true if this Iterator matches entire other Iterator at its end

◆ endsWithAnyOf()

template<typename CharIterator >
bool SC::StringIterator< CharIterator >::endsWithAnyOf ( Span< const CodePoint >  codePoints) const

Check if this Iterator ends with any code point in the given span.

Parameters
codePointsA span of code points to check for
Returns
true if at least one code point of codepoints exists at the end of the range

◆ isAtEnd()

template<typename CharIterator >
constexpr bool SC::StringIterator< CharIterator >::isAtEnd ( ) const
inlineconstexpr

Check if current position is at end of iterator range.

Returns
true if position is at end of iterator range

◆ isAtStart()

template<typename CharIterator >
constexpr bool SC::StringIterator< CharIterator >::isAtStart ( ) const
inlineconstexpr

Check if current position is at start of iterator range.

Returns
true if position is at start of iterator range

◆ isFollowedBy()

template<typename CharIterator >
constexpr bool SC::StringIterator< CharIterator >::isFollowedBy ( CodePoint  c)
constexpr

Check if next code point is c

Parameters
cthe code point
Returns
true if next code point is c

◆ isPrecededBy()

template<typename CharIterator >
constexpr bool SC::StringIterator< CharIterator >::isPrecededBy ( CodePoint  c)
constexpr

Check if previous code point is c

Parameters
cthe code point
Returns
true if previous code point is c

◆ match()

template<typename CharIterator >
bool SC::StringIterator< CharIterator >::match ( CodePoint  c)
inline

Check if code unit at current position matches CodePoint c

Parameters
ccode point to match
Returns
true if code unit at current position matches c, false if there is no match or position is at end

◆ read()

template<typename CharIterator >
bool SC::StringIterator< CharIterator >::read ( CodePoint &  c)

Read code unit at current position.

Parameters
coutput code point read
Returns
true if position is at end before decoding code unit

◆ reverseAdvanceCodePoints()

template<typename CharIterator >
bool SC::StringIterator< CharIterator >::reverseAdvanceCodePoints ( size_t  numCodePoints)

Move position backwards (towards start) by variable number of code pints.

Parameters
numCodePointsnumber of code points to move backwards
Returns
true if it's possible moving numCodePoints backwards before reaching start

◆ reverseAdvanceUntilMatches()

template<typename CharIterator >
bool SC::StringIterator< CharIterator >::reverseAdvanceUntilMatches ( CodePoint  c)

Moves position towards start until CodePoint c is found or position == end

Parameters
cThe CodePoint to be searched
Returns
true if c was found, false if start was reached

◆ reverseAdvanceUntilMatchesAny()

template<typename CharIterator >
bool SC::StringIterator< CharIterator >::reverseAdvanceUntilMatchesAny ( Span< const CodePoint >  items,
CodePoint &  matched 
)

Moves position towards start until any CodePoint in the given Span is found.

Parameters
itemsA contiguous span of CodePoint to match
matchedThe matched CodePoint in the Span
Returns
true if one CodePoint was matched, false if start was reached

◆ setToEnd()

template<typename CharIterator >
constexpr void SC::StringIterator< CharIterator >::setToEnd ( )
inlineconstexpr

Set current position to end of iterator range.

◆ setToStart()

template<typename CharIterator >
constexpr void SC::StringIterator< CharIterator >::setToStart ( )
inlineconstexpr

Rewind current position to start of iterator range.

◆ sliceFromStartUntil()

template<typename CharIterator >
constexpr StringIterator< CharIterator > SC::StringIterator< CharIterator >::sliceFromStartUntil ( StringIterator< CharIterator >  otherPoint) const
constexpr

Returns another StringIterator range, starting from start to otherPoint position.

Parameters
otherPointThe StringIterator containing the ending position to slice to
Returns
The new StringIterator [start, otherPoint.position]

◆ startsWith()

template<typename CharIterator >
template<typename IteratorType >
bool SC::StringIterator< CharIterator >::startsWith ( IteratorType  other) const

Check if this Iterator at its start matches entirely another Iterator's range.

Parameters
otherThe other iterator to match
Returns
true if this Iterator matches entire other at its start

◆ startsWithAnyOf()

template<typename CharIterator >
bool SC::StringIterator< CharIterator >::startsWithAnyOf ( Span< const CodePoint >  codePoints) const

Check if this Iterator starts with any code point in the given span.

Parameters
codePointsA span of code points to check for
Returns
true if at least one code point of codepoints exists at the start of the range

◆ stepBackward()

template<typename CharIterator >
constexpr bool SC::StringIterator< CharIterator >::stepBackward
constexpr

Move position to previous code point.

Returns
true if position is not at start before trying to move backwards

◆ stepForward()

template<typename CharIterator >
constexpr bool SC::StringIterator< CharIterator >::stepForward
constexpr

Move position to next code point.

Returns
true if position is not at end before trying to move forward

The documentation for this struct was generated from the following file: