From 4fe41778b4429c4974166e5ae531577dba22bed5 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Wed, 10 Aug 2022 14:55:01 +0200 Subject: Fixed iters for carr2/carr3 and cstr/csview. --- include/stc/carr2.h | 8 ++++---- include/stc/carr3.h | 8 ++++---- include/stc/cstr.h | 8 +++++--- include/stc/csview.h | 20 +++++++++++++------- 4 files changed, 26 insertions(+), 18 deletions(-) (limited to 'include') diff --git a/include/stc/carr2.h b/include/stc/carr2.h index 66a98f58..f15c6034 100644 --- a/include/stc/carr2.h +++ b/include/stc/carr2.h @@ -89,13 +89,13 @@ STC_INLINE size_t _cx_memb(_idx)(const _cx_self* self, size_t x, size_t y) { } STC_INLINE _cx_iter _cx_memb(_begin)(const _cx_self* self) - { return c_make(_cx_iter){*self->data}; } + { size_t n = self->xdim*self->ydim; return c_make(_cx_iter){n ? *self->data : NULL, *self->data + n}; } STC_INLINE _cx_iter _cx_memb(_end)(const _cx_self* self) - { return c_make(_cx_iter){*self->data + self->xdim*self->ydim}; } + { return c_make(_cx_iter){NULL, *self->data + self->xdim*self->ydim}; } + +STC_INLINE void _cx_memb(_next)(_cx_iter* it) { if (++it->ref == it->_end) it->ref = NULL; } -STC_INLINE void _cx_memb(_next)(_cx_iter* it) - { ++it->ref; } /* -------------------------- IMPLEMENTATION ------------------------- */ #if defined(i_implement) diff --git a/include/stc/carr3.h b/include/stc/carr3.h index 8ff1670e..ec67645c 100644 --- a/include/stc/carr3.h +++ b/include/stc/carr3.h @@ -92,13 +92,13 @@ STC_INLINE size_t _cx_memb(_idx)(const _cx_self* self, size_t x, size_t y, size_ } STC_INLINE _cx_iter _cx_memb(_begin)(const _cx_self* self) - { return c_make(_cx_iter){**self->data}; } + { size_t n = _cx_memb(_size)(self); return c_make(_cx_iter){n ? **self->data : NULL, **self->data + n}; } STC_INLINE _cx_iter _cx_memb(_end)(const _cx_self* self) - { return c_make(_cx_iter){**self->data + _cx_memb(_size)(self)}; } + { return c_make(_cx_iter){NULL, **self->data + _cx_memb(_size)(self)}; } + +STC_INLINE void _cx_memb(_next)(_cx_iter* it) { if (++it->ref == it->_end) it->ref = NULL; } -STC_INLINE void _cx_memb(_next)(_cx_iter* it) - { ++it->ref; } /* -------------------------- IMPLEMENTATION ------------------------- */ #if defined(i_implement) diff --git a/include/stc/cstr.h b/include/stc/cstr.h index 2dbea85a..509811eb 100644 --- a/include/stc/cstr.h +++ b/include/stc/cstr.h @@ -223,16 +223,18 @@ STC_INLINE csview cstr_u8_chr(const cstr* self, size_t u8idx) { // utf8 iterator STC_INLINE cstr_iter cstr_begin(const cstr* self) { - const char* str = cstr_str(self); - return c_make(cstr_iter){.u8 = {{str, utf8_chr_size(str)}}}; + csview sv = cstr_sv(self); + if (!sv.size) return c_make(cstr_iter){NULL}; + return c_make(cstr_iter){.u8 = {{sv.str, utf8_chr_size(sv.str)}, sv.str + sv.size}}; } STC_INLINE cstr_iter cstr_end(const cstr* self) { csview sv = cstr_sv(self); - return c_make(cstr_iter){sv.str + sv.size}; + return c_make(cstr_iter){.u8 = {{NULL}, sv.str + sv.size}}; } STC_INLINE void cstr_next(cstr_iter* it) { it->ref += it->u8.chr.size; it->u8.chr.size = utf8_chr_size(it->ref); + if (it->ref == it->u8._end) it->ref = NULL; } diff --git a/include/stc/csview.h b/include/stc/csview.h index ef545bf2..c48c6ae6 100644 --- a/include/stc/csview.h +++ b/include/stc/csview.h @@ -69,15 +69,21 @@ STC_INLINE csview csview_slice(csview sv, size_t p1, size_t p2) { return sv; } -/* iterator */ -STC_INLINE csview_iter csview_begin(const csview* self) - { return c_make(csview_iter){.u8 = {{self->str, utf8_chr_size(self->str)}}}; } +/* utf8 iterator */ +STC_INLINE csview_iter csview_begin(const csview* self) { + if (!self->size) return c_make(csview_iter){NULL}; + return c_make(csview_iter){.u8 = {{self->str, utf8_chr_size(self->str)}, self->str + self->size}}; +} + +STC_INLINE csview_iter csview_end(const csview* self) + { return c_make(csview_iter){.u8 = {{NULL}, self->str + self->size}}; } -STC_INLINE csview_iter csview_end(const csview* self) - { return c_make(csview_iter){self->str + self->size}; } +STC_INLINE void csview_next(csview_iter* it) { + it->ref += it->u8.chr.size; + it->u8.chr.size = utf8_chr_size(it->ref); + if (it->ref == it->u8._end) it->ref = NULL; +} -STC_INLINE void csview_next(csview_iter* it) - { it->ref += it->u8.chr.size; it->u8.chr.size = utf8_chr_size(it->ref); } /* utf8 */ STC_INLINE size_t csview_u8_size(csview sv) -- cgit v1.2.3