Loading...
Searching...
No Matches
by::cpIter Class Reference

Codepoint-based string iterator. More...

#include <cpIter.hpp>

Public Member Functions

 cpIter (const nchar *begin, const nchar *from)
 Constructs iterator from pointer range.
 
 cpIter (const std::string &from, nbool isReversed=false)
 Constructs iterator from string reference.
 
 cpIter (std::string &&from, nbool isReversed=false)=delete
 
me operator+ (ncnt step)
 
meoperator++ ()
 
me operator++ (int)
 
meoperator+= (ncnt step)
 
std::string operator* () const
 
 operator nbool () const
 
nbool operator== (const me &rhs) const
 
nbool isEnd () const
 
void rel ()
 
ncnt next (ncnt step)
 Advances iterator in its default direction.
 
ncnt stepForward (ncnt step)
 Unconditionally advances iterator forward.
 
ncnt stepBackward (ncnt step)
 Unconditionally advances iterator backward.
 
std::string get () const
 
ncnt remainLen () const
 

Detailed Description

Codepoint-based string iterator.

Handles codepoint-based iteration for strings. Primarily used in nStr for traversing multibyte strings like UTF-8 Unicode.

Like typical iterators, it supports prefix/postfix increment operators, dereference operator, and bool conversion operator. When creating a cpIter, you can specify the string to traverse along with the default iteration direction.

Remarks
Direction and initial position The direction specified during cpIter creation defines the iterator's default direction and initial position. If you pass reverse = true as argument, cpIter starts from the end of the string in reverse direction. However, if you explicitly call stepBackward() or stepForward(), it will move in that direction regardless of the default direction.
Past-the-end semantics Like STL, begin points to the first element, but end points to the position after the last element (past-the-end). Therefore, when traversing in reverse, you must call next() first.

Usage

Basic usage example:

std::string src = "abcd🏁efg"; // In UTF-8, 🏁 is represented by 3 characters
cpIter e4(src, true); // Define default iteration as reverse.
// At this point, it points to past-the-end position
while(*e4 != "") // Currently at end position, so "" is returned
++e4; // Same as calling e4.next(). Moves in default reverse direction
// But this code never actually executes
ASSERT_EQ(*e4, "");
std::string expects[] = {"g", "f", "e", "🏁", "d", "c", "b", "a"};
for(int n = 0; n < 8; n++) {
// Explicitly specify direction to traverse iterator.
// Unconditionally moves backward regardless of e4's direction
e4.stepBackward(1);
// For reverse traversal, you must step first
}
Codepoint-based string iterator.
Definition cpIter.hpp:51
Rich logging support with polymorphic type conversion.
Definition richLog.hpp:34

Constructor & Destructor Documentation

◆ cpIter() [1/2]

by::cpIter::cpIter ( const nchar * begin,
const nchar * from )

Constructs iterator from pointer range.

Note
Direction automatically determined: reverse if begin > from, forward otherwise

◆ cpIter() [2/2]

by::cpIter::cpIter ( const std::string & from,
nbool isReversed = false )
explicit

Constructs iterator from string reference.

Parameters
fromString must outlive this iterator
isReversedtrue for reverse iteration (starts at end), false for forward

Member Function Documentation

◆ next()

ncnt by::cpIter::next ( ncnt step)

Advances iterator in its default direction.

Returns
Actual codepoints moved (may be less if end reached)
Note
Moves forward for forward iterators, backward for reverse iterators

◆ stepBackward()

ncnt by::cpIter::stepBackward ( ncnt step)

Unconditionally advances iterator backward.

Returns
Actual codepoints moved (may be less if beginning reached)
Note
Direction is absolute, unaffected by iterator's reverse flag

◆ stepForward()

ncnt by::cpIter::stepForward ( ncnt step)

Unconditionally advances iterator forward.

Returns
Actual codepoints moved (may be less if end reached)
Note
Direction is absolute, unaffected by iterator's reverse flag

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