CX Framework
Cross-platform C utility framework
Loading...
Searching...
No Matches
striter.h
Go to the documentation of this file.
1#pragma once
2
3#include <cx/string/strbase.h>
4
7
72
73CX_C_BEGIN
74
80typedef struct striter {
81 // Public members - safe to read
82 uint8* _Nullable bytes;
83 uint32 off;
84 uint32 len;
85 uint32 cursor;
86
87 // Private members - do not access directly
88 string _str;
89 bool _borrowed;
91
93typedef enum {
94 STRI_BYTE,
97
99typedef enum {
100 STRI_SET,
102 STRI_END
104
121void striInit(_Out_ striter* _Nonnull i, _In_opt_ strref s);
122
141void striInitRev(_Out_ striter* _Nonnull i, _In_opt_ strref s);
142
164bool striNext(_Inout_ striter* _Nonnull i);
165
183bool striPrev(_Inout_ striter* _Nonnull i);
184
203bool striSeek(_Inout_ striter* _Nonnull i, int32 off, STRI_SEEK_TYPE type, STRI_SEEK_WHENCE whence);
204
220void striFinish(_Inout_ striter* _Nonnull i);
221
231_meta_inline bool striValid(_In_ striter* _Nonnull i)
232{
233 return i->len > 0;
234}
235
254void striBorrow(_Out_ striter* _Nonnull i, _In_opt_ strref s);
255
273void striBorrowRev(_Out_ striter* _Nonnull i, _In_opt_ strref s);
274
283
306_meta_inline _Success_(return) _Must_inspect_result_ bool
307striChar(_Inout_ striter* _Nonnull i, _Out_ uint8* _Nonnull out)
308{
309 while (i->cursor >= i->len) {
310 striNext(i);
311 if (i->len == 0)
312 return false;
313 }
314
315 *out = i->bytes[i->cursor++];
316 return true;
317}
318
338_meta_inline _Success_(return) _Must_inspect_result_ bool
339striPeekChar(_Inout_ striter* _Nonnull i, _Out_ uint8* _Nonnull out)
340{
341 while (i->cursor >= i->len) {
342 striNext(i);
343 if (i->len == 0)
344 return false;
345 }
346
347 *out = i->bytes[i->cursor];
348 return true;
349}
350
370_meta_inline bool striAdvance(_Inout_ striter* _Nonnull i, uint32 by)
371{
372 i->cursor += by;
373 while (i->cursor >= i->len) {
374 striNext(i);
375 if (i->len == 0)
376 return (i->cursor == 0); // return true for hitting end of string exactly
377 }
378 return true;
379}
380
382
383#define _striU8Anno _Success_(return) _Must_inspect_result_
384
391
410_striU8Anno bool striU8Char(_Inout_ striter* _Nonnull i, _Out_ int32* out);
411
412#define _striPeekU8Anno _Success_(return) _Must_inspect_result_
413
430_striPeekU8Anno bool striPeekU8Char(_Inout_ striter* _Nonnull i, _Out_ int32* out);
431
449bool striAdvanceU8(_Inout_ striter* _Nonnull i, uint32 by);
450
452
454
455CX_C_END
bool striPeekChar(striter *i, uint8 *out)
Definition striter.h:339
bool striChar(striter *i, uint8 *out)
Definition striter.h:307
bool striAdvance(striter *i, uint32 by)
Definition striter.h:370
_striPeekU8Anno bool striPeekU8Char(striter *i, int32 *out)
_striU8Anno bool striU8Char(striter *i, int32 *out)
bool striAdvanceU8(striter *i, uint32 by)
STRI_SEEK_WHENCE
Iterator seek origin - specifies where to seek from.
Definition striter.h:99
bool striValid(striter *i)
Definition striter.h:231
bool striPrev(striter *i)
bool striNext(striter *i)
bool striSeek(striter *i, int32 off, STRI_SEEK_TYPE type, STRI_SEEK_WHENCE whence)
void striFinish(striter *i)
void striInitRev(striter *i, strref s)
STRI_SEEK_TYPE
Iterator seek type - specifies what units to seek by.
Definition striter.h:93
void striBorrowRev(striter *i, strref s)
void striBorrow(striter *i, strref s)
void striInit(striter *i, strref s)
@ STRI_END
Seek from current position.
Definition striter.h:102
@ STRI_CUR
Seek from beginning of string.
Definition striter.h:101
@ STRI_U8CHAR
Seek by byte offset.
Definition striter.h:95
Core string types and fundamental operations.
uint32 cursor
Length of current run in bytes.
Definition striter.h:85
string _str
Current position within run (for striChar/striAdvance)
Definition striter.h:88
uint32 len
Byte offset of this run from string start.
Definition striter.h:84
uint32 off
Pointer to current run of bytes.
Definition striter.h:83