|
CX Framework
Cross-platform C utility framework
|
Modules | |
| Byte-at-a-time Access | |
| UTF-8 Code Point Access | |
Data Structures | |
| struct | striter |
Typedefs | |
| typedef struct striter | striter |
Enumerations | |
| enum | STRI_SEEK_TYPE { } |
| Iterator seek type - specifies what units to seek by. More... | |
| enum | STRI_SEEK_WHENCE { } |
| Iterator seek origin - specifies where to seek from. More... | |
Functions | |
| void | striInit (striter *i, strref s) |
| void | striInitRev (striter *i, strref s) |
| bool | striNext (striter *i) |
| bool | striPrev (striter *i) |
| bool | striSeek (striter *i, int32 off, STRI_SEEK_TYPE type, STRI_SEEK_WHENCE whence) |
| void | striFinish (striter *i) |
| bool | striValid (striter *i) |
| void | striBorrow (striter *i, strref s) |
| void | striBorrowRev (striter *i, strref s) |
String iterators provide low-level access to string bytes for scanning and parsing operations. They are designed to efficiently handle both simple strings and ropes without exposing the internal representation.
An iterator represents a "run" of contiguous bytes within the string. For simple strings, the entire string is one run. For ropes (large strings built from concatenation), the string consists of multiple runs that must be traversed separately.
The iterator exposes the current run through public members:
Pattern 1 - Process runs directly (most efficient):
Pattern 2 - Byte-at-a-time access (simpler but slower):
Pattern 3 - UTF-8 code point access:
For maximum performance in controlled situations, borrowed iterators don't hold a reference to the string. They can be discarded without calling striFinish(), but the source string must remain valid for the iterator's lifetime. Use striBorrow() instead of striInit().
String iterator structure
Represents a position within a string and the current run of contiguous bytes. Users should access the public members (bytes, off, len, cursor) but not modify them directly - use the iterator functions instead.
| enum STRI_SEEK_TYPE |
| enum STRI_SEEK_WHENCE |
| void striBorrow | ( | striter * | i, |
| strref | s | ||
| ) |
Initializes a borrowed iterator at the beginning (forward)
Creates an iterator without holding a reference to the string. This is more efficient but requires the source string to remain valid for the iterator's lifetime. The iterator can be discarded without calling striFinish().
Use this for performance-critical code with well-controlled lifetimes.
| i | Iterator structure to initialize |
| s | String to iterate over (must remain valid) |
Example:
| void striBorrowRev | ( | striter * | i, |
| strref | s | ||
| ) |
Initializes a borrowed iterator at the end (reverse)
Like striBorrow(), but positioned at the end for reverse iteration with striPrev(). Does not hold a string reference and can be discarded without striFinish().
| i | Iterator structure to initialize |
| s | String to iterate over (must remain valid) |
Example:
| void striFinish | ( | striter * | i | ) |
Finishes with an iterator and releases resources
Releases the string reference held by the iterator. Must be called for every iterator created with striInit() or striInitRev() to prevent memory leaks. Not required for borrowed iterators (striBorrow).
| i | Iterator to finish |
Example:
| void striInit | ( | striter * | i, |
| strref | s | ||
| ) |
Initializes an iterator at the beginning of a string (forward iteration)
Creates an iterator positioned at the start of the string, set up to iterate forward. The iterator holds a reference to the string, so it must be cleaned up with striFinish() when done.
| i | Iterator structure to initialize |
| s | String to iterate over |
Example:
| void striInitRev | ( | striter * | i, |
| strref | s | ||
| ) |
Initializes an iterator at the end of a string (reverse iteration)
Creates an iterator positioned at the end of the string, set up to iterate backward using striPrev(). The iterator holds a reference to the string.
| i | Iterator structure to initialize |
| s | String to iterate over |
Example:
| bool striNext | ( | striter * | i | ) |
Advances to the next run of bytes
Moves the iterator forward to the next contiguous run of bytes in the string. For simple strings, this will reach the end immediately. For rope strings, this advances to the next segment.
If cursor was in the middle of the previous run, the cursor position carries over (becomes negative) into the new run.
| i | Iterator to advance |
Example:
Referenced by striAdvance(), striChar(), and striPeekChar().
| bool striPrev | ( | striter * | i | ) |
Moves to the previous run of bytes
Moves the iterator backward to the previous contiguous run of bytes. Used for reverse iteration, typically after striInitRev().
| i | Iterator to move backward |
Example:
| bool striSeek | ( | striter * | i, |
| int32 | off, | ||
| STRI_SEEK_TYPE | type, | ||
| STRI_SEEK_WHENCE | whence | ||
| ) |
Seeks to a specific position in the string
Repositions the iterator to a new location specified by offset, type, and origin. Can seek by byte offset or UTF-8 code points (slower). After seeking, the iterator is positioned at the start of a run containing the target position.
| i | Iterator to reposition |
| off | Offset to seek to |
| type | STRI_BYTE (byte offset) or STRI_U8CHAR (UTF-8 code points) |
| whence | STRI_SET (from start), STRI_CUR (from current), STRI_END (from end) |
Example:
|
inline |
Tests if an iterator has data available
Returns true if the iterator is positioned at a valid run with data, false if it has reached the end of the string or is invalid.
| i | Iterator to test |