From 132624e04689bb663685b10625013efa063633ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tyge=20L=C3=B8vset?= Date: Wed, 23 Oct 2024 15:04:58 +0200 Subject: [PATCH] - Internal cleanups of code and some refactoring for code clarity. - Renamed coroutine *_await_and_return() => *_await_with_return(). --- include/c11/fmt.h | 2 +- include/stc/box.h | 2 +- include/stc/cbits.h | 2 +- include/stc/common.h | 2 +- include/stc/coroutine.h | 22 +++++++++++----------- include/stc/csview.h | 7 ++++--- include/stc/hmap.h | 25 +++++++++++++------------ include/stc/list.h | 18 +++++++++--------- include/stc/priv/cregex_prv.c | 6 +++--- include/stc/priv/cstr_prv.c | 8 ++++---- include/stc/priv/cstr_prv.h | 10 +++++----- include/stc/priv/queue_prv.h | 4 ++-- include/stc/smap.h | 30 +++++++++++++++--------------- include/stc/stack.h | 18 +++++++++++------- include/stc/vec.h | 17 ++++++++++------- include/stc/zsview.h | 8 ++++---- misc/examples/coroutines/coread.c | 2 +- misc/examples/hashmaps/hashmap.c | 4 ++-- 18 files changed, 98 insertions(+), 89 deletions(-) diff --git a/include/c11/fmt.h b/include/c11/fmt.h index f3426ba7..f0d07a1f 100644 --- a/include/c11/fmt.h +++ b/include/c11/fmt.h @@ -269,7 +269,7 @@ FMT_DEF int _fmt_parse(char* p, int nargs, const char *fmt, ...) { case 'g': if (empty) memcpy(p, ".8", 2), p += 2; break; case '@': ++arg; if (empty) memcpy(p, ".16", 3), p += 3; break; } - if (!strchr("csdioxXufFeEaAgGnp", fmt[-1])) + if (strchr("csdioxXufFeEaAgGnp", fmt[-1]) == NULL) while (*arg) *p++ = *arg++; if (p0 && (p[-1] == 's' || p[-1] == 'c')) /* left-align str */ memmove(p0 + 1, p0, (size_t)(p++ - p0)), *p0 = '-'; diff --git a/include/stc/box.h b/include/stc/box.h index 51567203..510e4cd5 100644 --- a/include/stc/box.h +++ b/include/stc/box.h @@ -125,7 +125,7 @@ STC_INLINE Self _c_MEMB(_from)(_m_raw raw) #if !defined i_no_clone STC_INLINE Self _c_MEMB(_clone)(Self other) { - if (!other.get) return other; + if (other.get == NULL) return other; Self out = {_i_malloc(_m_value, 1)}; *out.get = i_keyclone((*other.get)); return out; diff --git a/include/stc/cbits.h b/include/stc/cbits.h index 9426a076..3347fe01 100644 --- a/include/stc/cbits.h +++ b/include/stc/cbits.h @@ -127,7 +127,7 @@ STC_INLINE char* _cbits_to_str(const uintptr_t* set, const isize sz, for (isize i = 0; i < n; ++i) \ if ((set[i] OPR other[i]) != VAL) \ return false; \ - if (!(sz & (_cbits_WB - 1))) \ + if ((sz & (_cbits_WB - 1)) == 0) \ return true; \ const uintptr_t i = (uintptr_t)n, m = _cbits_bit(sz) - 1; \ return ((set[i] OPR other[i]) & m) == (VAL & m) diff --git a/include/stc/common.h b/include/stc/common.h index 8f3e1450..ae688bac 100644 --- a/include/stc/common.h +++ b/include/stc/common.h @@ -290,7 +290,7 @@ STC_INLINE isize c_next_pow2(isize n) { // substring in substring? STC_INLINE char* c_strnstrn(const char *str, isize slen, const char *needle, isize nlen) { - if (!nlen) return (char *)str; + if (nlen == 0) return (char *)str; if (nlen > slen) return NULL; slen -= nlen; do { diff --git a/include/stc/coroutine.h b/include/stc/coroutine.h index 55d7cd65..94aedfdc 100644 --- a/include/stc/coroutine.h +++ b/include/stc/coroutine.h @@ -65,8 +65,8 @@ enum { }; typedef enum { CCO_DONE = 0, - CCO_AWAIT = 1<<29, - CCO_YIELD = 1<<30, + CCO_YIELD = 1<<29, + CCO_AWAIT = 1<<30, } cco_result; typedef struct { @@ -97,12 +97,12 @@ typedef struct { return value; \ } while (0) -#define cco_await(promise) cco_await_and_return(promise, CCO_AWAIT) -#define cco_await_and_return(promise, ret) \ +#define cco_await(until) cco_await_with_return(until, CCO_AWAIT) +#define cco_await_with_return(until, ret) \ do { \ *_state = __LINE__; \ /* fall through */ \ - case __LINE__: if (!(promise)) {return ret; goto _resume;} \ + case __LINE__: if (!(until)) {return ret; goto _resume;} \ } while (0) /* cco_await_coroutine(): assumes coroutine returns a cco_result value (int) */ @@ -232,10 +232,10 @@ typedef struct cco_runtime { typedef struct { ptrdiff_t count; } cco_semaphore; -#define cco_await_semaphore(sem) cco_await_semaphore_and_return(sem, CCO_AWAIT) -#define cco_await_semaphore_and_return(sem, ret) \ +#define cco_await_semaphore(sem) cco_await_semaphore_with_return(sem, CCO_AWAIT) +#define cco_await_semaphore_with_return(sem, ret) \ do { \ - cco_await_and_return((sem)->count > 0, ret); \ + cco_await_with_return((sem)->count > 0, ret); \ --(sem)->count; \ } while (0) @@ -290,12 +290,12 @@ typedef struct { ptrdiff_t count; } cco_semaphore; typedef struct { double interval, start; } cco_timer; -#define cco_await_timer(tm, sec) cco_await_timer_and_return(tm, sec, CCO_AWAIT) +#define cco_await_timer(tm, sec) cco_await_timer_with_return(tm, sec, CCO_AWAIT) #define cco_await_timer_v(...) c_MACRO_OVERLOAD(cco_await_timer_v, __VA_ARGS__) -#define cco_await_timer_and_return(tm, sec, ret) \ +#define cco_await_timer_with_return(tm, sec, ret) \ do { \ cco_timer_start(tm, sec); \ - cco_await_and_return(cco_timer_expired(tm), ret); \ + cco_await_with_return(cco_timer_expired(tm), ret); \ } while (0) static inline void cco_timer_start(cco_timer* tm, double sec) { diff --git a/include/stc/csview.h b/include/stc/csview.h index e30f2a7a..6224e5e3 100644 --- a/include/stc/csview.h +++ b/include/stc/csview.h @@ -107,11 +107,12 @@ STC_INLINE const char* csview_at(csview sv, isize idx) /* utf8 iterator */ STC_INLINE csview_iter csview_begin(const csview* self) { - return c_literal(csview_iter){.u8 = {{self->buf, utf8_chr_size(self->buf)}, - self->buf + self->size}}; + csview_iter it = {.u8 = {{self->buf, utf8_chr_size(self->buf)}, + self->buf + self->size}}; + return it; } STC_INLINE csview_iter csview_end(const csview* self) { - return c_literal(csview_iter){.u8 = {{0}, self->buf + self->size}}; + (void)self; csview_iter it = {0}; return it; } STC_INLINE void csview_next(csview_iter* it) { it->ref += it->chr.size; diff --git a/include/stc/hmap.h b/include/stc/hmap.h index 8c8c7321..3c0aaa8b 100644 --- a/include/stc/hmap.h +++ b/include/stc/hmap.h @@ -238,7 +238,7 @@ STC_INLINE _m_iter _c_MEMB(_advance)(_m_iter it, size_t n) { STC_INLINE _m_iter _c_MEMB(_find)(const Self* self, _m_keyraw rkey) { _m_value* ref; - if (self->size && (ref = _c_MEMB(_bucket_lookup_)(self, &rkey).ref)) + if (self->size != 0 && (ref = _c_MEMB(_bucket_lookup_)(self, &rkey).ref) != NULL) return c_literal(_m_iter){ref, &self->table[self->bucket_count], &self->meta[ref - self->table]}; @@ -257,7 +257,7 @@ _c_MEMB(_get_mut)(Self* self, _m_keyraw rkey) STC_INLINE int _c_MEMB(_erase)(Self* self, _m_keyraw rkey) { _m_value* ref; - if (self->size && (ref = _c_MEMB(_bucket_lookup_)(self, &rkey).ref)) + if (self->size != 0 && (ref = _c_MEMB(_bucket_lookup_)(self, &rkey).ref) != NULL) { _c_MEMB(_erase_entry)(self, ref); return 1; } return 0; } @@ -284,10 +284,11 @@ _c_MEMB(_eq)(const Self* self, const Self* other) { #if defined(i_implement) || defined(i_static) STC_DEF _m_iter _c_MEMB(_begin)(const Self* self) { - _m_iter it = {self->table, self->table+self->bucket_count, self->meta}; - if (it._mref) - while (it._mref->dist == 0) - ++it.ref, ++it._mref; + _m_iter it = {self->table, self->table, self->meta}; + if (it.ref == NULL) return it; + it._end += self->bucket_count; + while (it._mref->dist == 0) + ++it.ref, ++it._mref; if (it.ref == it._end) it.ref = NULL; return it; } @@ -351,7 +352,7 @@ STC_DEF void _c_MEMB(_clear)(Self* self) { if (_res.inserted) _res.ref->first = i_keyfrom(rkey); else { - if (!_res.ref) return _res; + if (_res.ref == NULL) return _res; i_valdrop((&_res.ref->second)); } _res.ref->second = i_valfrom(rmapped); @@ -416,19 +417,19 @@ _c_MEMB(_bucket_insert_)(const Self* self, const _m_keyraw* rkeyptr) { #if !defined i_no_clone STC_DEF Self _c_MEMB(_clone)(Self map) { - if (map.bucket_count) { + if (map.bucket_count != 0) { _m_value *d = _i_malloc(_m_value, map.bucket_count); const isize _mbytes = (map.bucket_count + 1)*c_sizeof *map.meta; struct hmap_meta *m = (struct hmap_meta *)i_malloc(_mbytes); - if (d && m) { + if (d != NULL && m != NULL) { c_memcpy(m, map.meta, _mbytes); _m_value *_dst = d, *_end = map.table + map.bucket_count; for (; map.table != _end; ++map.table, ++map.meta, ++_dst) if (map.meta->dist) *_dst = _c_MEMB(_value_clone)(*map.table); } else { - if (d) i_free(d, map.bucket_count*c_sizeof *d); - if (m) i_free(m, _mbytes); + if (d != NULL) i_free(d, map.bucket_count*c_sizeof *d); + if (m != NULL) i_free(m, _mbytes); d = 0, m = 0, map.bucket_count = 0; } map.table = d, map.meta = m; @@ -457,7 +458,7 @@ _c_MEMB(_reserve)(Self* self, const isize _newcap) { const _m_value* d = self->table; const struct hmap_meta* m = self->meta; - for (isize i = 0; i < _oldbucks; ++i, ++d) if ((m++)->dist) { + for (isize i = 0; i < _oldbucks; ++i, ++d) if ((m++)->dist != 0) { _m_keyraw r = i_keytoraw(_i_keyref(d)); _m_result _res = _c_MEMB(_bucket_insert_)(&map, &r); *_res.ref = *d; // move diff --git a/include/stc/list.h b/include/stc/list.h index 6335971d..c9dcf290 100644 --- a/include/stc/list.h +++ b/include/stc/list.h @@ -160,7 +160,7 @@ STC_INLINE void _c_MEMB(_value_drop)(_m_value* pval) { i_keydrop(pval); STC_INLINE isize _c_MEMB(_count)(const Self* self) { isize n = 1; const _m_node *node = self->last; - if (!node) return 0; + if (node == NULL) return 0; while ((node = node->next) != self->last) ++n; return n; } @@ -250,7 +250,7 @@ _c_MEMB(_push_back)(Self* self, _m_value value) { STC_DEF _m_value* _c_MEMB(_push_front)(Self* self, _m_value value) { _c_list_insert_entry_after(self->last, value); - if (!self->last) + if (self->last == NULL) self->last = entry; return &entry->value; } @@ -265,7 +265,7 @@ _c_MEMB(_push_back_node)(Self* self, _m_node* node) { STC_DEF _m_value* _c_MEMB(_insert_after_node)(Self* self, _m_node* ref, _m_node* node) { _c_list_insert_after_node(ref, node); - if (!self->last) + if (self->last == NULL) self->last = node; return &node->value; } @@ -274,7 +274,7 @@ STC_DEF _m_iter _c_MEMB(_insert_at)(Self* self, _m_iter it, _m_value value) { _m_node* node = it.ref ? it.prev : self->last; _c_list_insert_entry_after(node, value); - if (!self->last || !it.ref) { + if (self->last == NULL || it.ref == NULL) { it.prev = self->last ? self->last : entry; self->last = entry; } @@ -295,7 +295,7 @@ _c_MEMB(_erase_range)(Self* self, _m_iter it1, _m_iter it2) { _m_node *end = it2.ref ? _clist_tonode(it2.ref) : self->last->next; if (it1.ref != it2.ref) do { _c_MEMB(_erase_after_node)(self, it1.prev); - if (!self->last) break; + if (self->last == NULL) break; } while (it1.prev->next != end); return it2; } @@ -330,14 +330,14 @@ _c_MEMB(_reverse)(Self* self) { STC_DEF _m_iter _c_MEMB(_splice)(Self* self, _m_iter it, Self* other) { - if (!self->last) + if (self->last == NULL) self->last = other->last; else if (other->last) { _m_node *p = it.ref ? it.prev : self->last, *next = p->next; it.prev = other->last; p->next = it.prev->next; it.prev->next = next; - if (!it.ref) self->last = it.prev; + if (it.ref == NULL) self->last = it.prev; } other->last = NULL; return it; @@ -378,7 +378,7 @@ _c_MEMB(_remove)(Self* self, _m_raw val) { _m_raw r = i_keytoraw((&node->value)); if (i_eq((&r), (&val))) { _c_MEMB(_erase_after_node)(self, prev), ++n; - if (!self->last) break; + if (self->last == NULL) break; } else prev = node; } while (node != self->last); @@ -396,7 +396,7 @@ STC_DEF bool _c_MEMB(_sort)(Self* self) { c_foreach (i, Self, *self) { if (len == cap) { isize cap_n = cap + cap/2 + 8; - if (!(p = (_m_value *)i_realloc(arr, cap*c_sizeof *p, cap_n*c_sizeof *p))) + if ((p = (_m_value *)i_realloc(arr, cap*c_sizeof *p, cap_n*c_sizeof *p)) == NULL) goto done; arr = p, cap = cap_n; } diff --git a/include/stc/priv/cregex_prv.c b/include/stc/priv/cregex_prv.c index 5b0d5311..64289265 100644 --- a/include/stc/priv/cregex_prv.c +++ b/include/stc/priv/cregex_prv.c @@ -642,7 +642,7 @@ _lexasciiclass(_Parser *par, _Rune *rp) /* assume *rp == '[' and *par->exprp == }; int inv = par->exprp[1] == '^', off = 1 + inv; for (unsigned i = 0; i < (sizeof cls/sizeof *cls); ++i) - if (!strncmp(par->exprp + off, cls[i].c, (size_t)cls[i].n)) { + if (strncmp(par->exprp + off, cls[i].c, (size_t)cls[i].n) == 0) { *rp = (_Rune)cls[i].r; par->exprp += off + cls[i].n; break; @@ -685,7 +685,7 @@ _lexutfclass(_Parser *par, _Rune *rp) }; unsigned inv = (*rp == 'P'); for (unsigned i = 0; i < (sizeof cls/sizeof *cls); ++i) { - if (!strncmp(par->exprp, cls[i].c, (size_t)cls[i].n)) { + if (strncmp(par->exprp, cls[i].c, (size_t)cls[i].n) == 0) { if (par->rune_type == TOK_IRUNE && (cls[i].r == UTF_ll || cls[i].r == UTF_lu)) *rp = (_Rune)(UTF_lc + inv); else @@ -868,7 +868,7 @@ _regcomp1(_Reprog *pp, _Parser *par, const char *s, int cflags) isize instcap = 5 + 6*c_strlen(s); isize new_allocsize = c_sizeof(_Reprog) + instcap*c_sizeof(_Reinst); pp = (_Reprog *)i_realloc(pp, pp ? pp->allocsize : 0, new_allocsize); - if (! pp) { + if (pp == NULL) { par->error = CREG_OUTOFMEMORY; return NULL; } diff --git a/include/stc/priv/cstr_prv.c b/include/stc/priv/cstr_prv.c index 7df3200c..cc19cb64 100644 --- a/include/stc/priv/cstr_prv.c +++ b/include/stc/priv/cstr_prv.c @@ -82,7 +82,7 @@ char* cstr_reserve(cstr* self, const isize cap) { char* cstr_resize(cstr* self, const isize size, const char value) { cstr_view r = cstr_getview(self); if (size > r.size) { - if (size > r.cap && !(r.data = cstr_reserve(self, size))) + if (size > r.cap && (r.data = cstr_reserve(self, size)) == NULL) return NULL; c_memset(r.data + r.size, value, size - r.size); } @@ -108,7 +108,7 @@ char* cstr_append_n(cstr* self, const char* str, const isize len) { if (r.size + len > r.cap) { const size_t off = (size_t)(str - r.data); r.data = cstr_reserve(self, r.size*3/2 + len); - if (!r.data) return NULL; + if (r.data == NULL) return NULL; if (off <= (size_t)r.size) str = r.data + off; /* handle self append */ } c_memcpy(r.data + r.size, str, len); @@ -119,7 +119,7 @@ char* cstr_append_n(cstr* self, const char* str, const isize len) { cstr cstr_from_replace(csview in, csview search, csview repl, int32_t count) { cstr out = cstr_init(); isize from = 0; char* res; - if (!count) count = INT32_MAX; + if (count == 0) count = INT32_MAX; if (search.size) while (count-- && (res = c_strnstrn(in.buf + from, in.size - from, search.buf, search.size))) { const isize pos = (res - in.buf); @@ -160,7 +160,7 @@ void cstr_shrink_to_fit(cstr* self) { char* cstr_append_uninit(cstr *self, isize len) { cstr_view r = cstr_getview(self); - if (r.size + len > r.cap && !(r.data = cstr_reserve(self, r.size*3/2 + len))) + if (r.size + len > r.cap && (r.data = cstr_reserve(self, r.size*3/2 + len)) == NULL) return NULL; _cstr_set_size(self, r.size + len); return r.data + r.size; diff --git a/include/stc/priv/cstr_prv.h b/include/stc/priv/cstr_prv.h index d9c6e409..1fe755fd 100644 --- a/include/stc/priv/cstr_prv.h +++ b/include/stc/priv/cstr_prv.h @@ -223,22 +223,22 @@ STC_INLINE csview cstr_u8_chr(const cstr* self, isize u8pos) { STC_INLINE cstr_iter cstr_begin(const cstr* self) { csview sv = cstr_sv(self); - if (!sv.size) return c_literal(cstr_iter){.ref = NULL}; - return c_literal(cstr_iter){.chr = {sv.buf, utf8_chr_size(sv.buf)}}; + cstr_iter it = {.chr = {sv.buf, utf8_chr_size(sv.buf)}}; + return it; } STC_INLINE cstr_iter cstr_end(const cstr* self) { - (void)self; return c_literal(cstr_iter){NULL}; + (void)self; cstr_iter it = {0}; return it; } STC_INLINE void cstr_next(cstr_iter* it) { it->ref += it->chr.size; it->chr.size = utf8_chr_size(it->ref); - if (!*it->ref) it->ref = NULL; + if (*it->ref == '\0') it->ref = NULL; } STC_INLINE cstr_iter cstr_advance(cstr_iter it, isize u8pos) { it.ref = c_const_cast(char *, utf8_offset(it.ref, u8pos)); it.chr.size = utf8_chr_size(it.ref); - if (!*it.ref) it.ref = NULL; + if (*it.ref == '\0') it.ref = NULL; return it; } diff --git a/include/stc/priv/queue_prv.h b/include/stc/priv/queue_prv.h index f4523f3f..2ed6b268 100644 --- a/include/stc/priv/queue_prv.h +++ b/include/stc/priv/queue_prv.h @@ -177,7 +177,7 @@ _c_MEMB(_reserve)(Self* self, const isize n) { return true; isize oldpow2 = self->capmask + 1, newpow2 = c_next_pow2(n + 1); _m_value* d = (_m_value *)i_realloc(self->cbuf, oldpow2*c_sizeof *d, newpow2*c_sizeof *d); - if (!d) + if (d == NULL) return false; isize head = oldpow2 - self->start; if (self->start <= self->end) @@ -213,7 +213,7 @@ _c_MEMB(_shrink_to_fit)(Self *self) { if (sz > self->capmask/2) return; Self out = _c_MEMB(_with_capacity)(sz); - if (!out.cbuf) + if (out.cbuf == NULL) return; c_foreach (i, Self, *self) out.cbuf[j++] = *i.ref; diff --git a/include/stc/smap.h b/include/stc/smap.h index 3ea28b2b..c53f4bd2 100644 --- a/include/stc/smap.h +++ b/include/stc/smap.h @@ -282,7 +282,7 @@ _c_MEMB(_reserve)(Self* self, const isize cap) { return false; _m_node* nodes = (_m_node*)i_realloc(self->nodes, (self->capacity + 1)*c_sizeof(_m_node), (cap + 1)*c_sizeof(_m_node)); - if (!nodes) + if (nodes == NULL) return false; nodes[0] = c_literal(_m_node){0}; self->nodes = nodes; @@ -311,7 +311,7 @@ _c_MEMB(_back)(const Self* self) { static int32_t _c_MEMB(_new_node_)(Self* self, int level) { int32_t tn; - if (self->disp) { + if (self->disp != 0) { tn = self->disp; self->disp = self->nodes[tn].link[1]; } else { @@ -345,7 +345,7 @@ _c_MEMB(_new_node_)(Self* self, int level) { if (_res.inserted) _res.ref->first = i_keyfrom(rkey); else { - if (!_res.ref) return _res; + if (_res.ref == NULL) return _res; i_valdrop((&_res.ref->second)); } _res.ref->second = i_valfrom(rmapped); @@ -375,7 +375,7 @@ STC_DEF _m_iter _c_MEMB(_lower_bound)(const Self* self, _m_keyraw rkey) { _m_iter it; _c_MEMB(_find_it)(self, rkey, &it); - if (!it.ref && it._top) { + if (it.ref == NULL && it._top != 0) { int32_t tn = it._st[--it._top]; it._tn = it._d[tn].link[1]; it.ref = &it._d[tn].value; @@ -385,7 +385,7 @@ _c_MEMB(_lower_bound)(const Self* self, _m_keyraw rkey) { STC_DEF int32_t _c_MEMB(_skew_)(_m_node *d, int32_t tn) { - if (tn && d[d[tn].link[0]].level == d[tn].level) { + if (tn != 0 && d[d[tn].link[0]].level == d[tn].level) { int32_t tmp = d[tn].link[0]; d[tn].link[0] = d[tmp].link[1]; d[tmp].link[1] = tn; @@ -414,7 +414,7 @@ _c_MEMB(_insert_entry_i_)(Self* self, int32_t tn, const _m_keyraw* rkey, _m_resu while (tx) { up[top++] = tx; const _m_keyraw _raw = i_keytoraw(_i_keyref(&d[tx].value)); - if (!(c = i_cmp((&_raw), rkey))) + if ((c = i_cmp((&_raw), rkey)) == 0) { _res->ref = &d[tx].value; return tn; } dir = (c < 0); tx = d[tx].link[dir]; @@ -428,7 +428,7 @@ _c_MEMB(_insert_entry_i_)(Self* self, int32_t tn, const _m_keyraw* rkey, _m_resu return tx; d[up[top - 1]].link[dir] = tx; while (top--) { - if (top) + if (top != 0) dir = (d[up[top - 1]].link[1] == up[top]); up[top] = _c_MEMB(_skew_)(d, up[top]); up[top] = _c_MEMB(_split_)(d, up[top]); @@ -457,8 +457,8 @@ _c_MEMB(_erase_r_)(Self *self, int32_t tn, const _m_keyraw* rkey, int *erased) { if (c != 0) d[tn].link[c < 0] = _c_MEMB(_erase_r_)(self, d[tn].link[c < 0], rkey, erased); else { - if (!(*erased)++) - _c_MEMB(_value_drop)(&d[tn].value); + if ((*erased)++ == 0) + _c_MEMB(_value_drop)(&d[tn].value); // drop first time, not second. if (d[tn].link[0] && d[tn].link[1]) { tx = d[tn].link[0]; while (d[tx].link[1]) @@ -491,7 +491,7 @@ STC_DEF int _c_MEMB(_erase)(Self* self, _m_keyraw rkey) { int erased = 0; int32_t root = _c_MEMB(_erase_r_)(self, self->root, &rkey, &erased); - if (!erased) + if (erased == 0) return 0; self->root = root; --self->size; @@ -502,7 +502,7 @@ STC_DEF _m_iter _c_MEMB(_erase_at)(Self* self, _m_iter it) { _m_keyraw raw = i_keytoraw(_i_keyref(it.ref)); _c_MEMB(_next)(&it); - if (it.ref) { + if (it.ref != NULL) { _m_keyraw nxt = i_keytoraw(_i_keyref(it.ref)); _c_MEMB(_erase)(self, raw); _c_MEMB(_find_it)(self, nxt, &it); @@ -513,8 +513,8 @@ _c_MEMB(_erase_at)(Self* self, _m_iter it) { STC_DEF _m_iter _c_MEMB(_erase_range)(Self* self, _m_iter it1, _m_iter it2) { - if (!it2.ref) { - while (it1.ref) + if (it2.ref == NULL) { + while (it1.ref != NULL) it1 = _c_MEMB(_erase_at)(self, it1); return it1; } @@ -568,7 +568,7 @@ _c_MEMB(_emplace)(Self* self, _m_keyraw rkey _i_MAP_ONLY(, _m_rmapped rmapped)) static void _c_MEMB(_drop_r_)(_m_node* d, int32_t tn) { - if (tn) { + if (tn != 0) { _c_MEMB(_drop_r_)(d, d[tn].link[0]); _c_MEMB(_drop_r_)(d, d[tn].link[1]); _c_MEMB(_value_drop)(&d[tn].value); @@ -578,7 +578,7 @@ _c_MEMB(_drop_r_)(_m_node* d, int32_t tn) { STC_DEF void _c_MEMB(_drop)(const Self* cself) { Self* self = (Self*)cself; - if (self->capacity) { + if (self->capacity != 0) { _c_MEMB(_drop_r_)(self->nodes, self->root); i_free(self->nodes, (self->capacity + 1)*c_sizeof(_m_node)); } diff --git a/include/stc/stack.h b/include/stc/stack.h index 36575b88..34859c34 100644 --- a/include/stc/stack.h +++ b/include/stc/stack.h @@ -169,7 +169,7 @@ STC_INLINE _m_value* _c_MEMB(_emplace)(Self* self, _m_raw raw) #if !defined i_no_clone STC_INLINE Self _c_MEMB(_clone)(Self s) { Self tmp = {_i_malloc(_m_value, s.size), s.size, s.size}; - if (!tmp.data) tmp.capacity = 0; + if (tmp.data == NULL) tmp.capacity = 0; else for (isize i = 0; i < s.size; ++s.data) tmp.data[i++] = i_keyclone((*s.data)); s.data = tmp.data; @@ -190,21 +190,25 @@ STC_INLINE i_keyraw _c_MEMB(_value_toraw)(const _m_value* val) { return i_keytoraw(val); } #endif // !i_no_clone +// iteration + STC_INLINE _m_iter _c_MEMB(_begin)(const Self* self) { - isize n = self->size; _m_value* d = (_m_value*)self->data; - return c_literal(_m_iter){n ? d : NULL, d + n}; + _m_iter it = {(_m_value*)self->data, (_m_value*)self->data}; + if (it.ref != NULL) it.end += self->size; + return it; } STC_INLINE _m_iter _c_MEMB(_rbegin)(const Self* self) { - isize n = self->size; _m_value* d = (_m_value*)self->data; - return c_literal(_m_iter){n ? d + n - 1 : NULL, d - 1}; + _m_iter it = {(_m_value*)self->data, (_m_value*)self->data}; + if (it.ref != NULL) { it.ref += self->size - 1; it.end -= 1; } + return it; } STC_INLINE _m_iter _c_MEMB(_end)(const Self* self) - { (void)self; return c_literal(_m_iter){0}; } + { (void)self; _m_iter it = {0}; return it; } STC_INLINE _m_iter _c_MEMB(_rend)(const Self* self) - { (void)self; return c_literal(_m_iter){0}; } + { (void)self; _m_iter it = {0}; return it; } STC_INLINE void _c_MEMB(_next)(_m_iter* it) { if (++it->ref == it->end) it->ref = NULL; } diff --git a/include/stc/vec.h b/include/stc/vec.h index ab9830c4..e70bfc20 100644 --- a/include/stc/vec.h +++ b/include/stc/vec.h @@ -193,22 +193,25 @@ STC_INLINE _m_value* _c_MEMB(_at_mut)(Self* self, const isize idx) { c_assert(idx < self->size); return self->data + idx; } +// iteration STC_INLINE _m_iter _c_MEMB(_begin)(const Self* self) { - isize n = self->size; - return c_literal(_m_iter){n ? self->data : NULL, self->data + n}; + _m_iter it = {(_m_value*)self->data, (_m_value*)self->data}; + if (it.ref != NULL) it.end += self->size; + return it; } STC_INLINE _m_iter _c_MEMB(_rbegin)(const Self* self) { - isize n = self->size; - return c_literal(_m_iter){n ? self->data + n - 1 : NULL, self->data - 1}; + _m_iter it = {(_m_value*)self->data, (_m_value*)self->data}; + if (it.ref != NULL) { it.ref += self->size - 1; it.end -= 1; } + return it; } STC_INLINE _m_iter _c_MEMB(_end)(const Self* self) - { (void)self; return c_literal(_m_iter){0}; } + { (void)self; _m_iter it = {0}; return it; } STC_INLINE _m_iter _c_MEMB(_rend)(const Self* self) - { (void)self; return c_literal(_m_iter){0}; } + { (void)self; _m_iter it = {0}; return it; } STC_INLINE void _c_MEMB(_next)(_m_iter* it) { if (++it->ref == it->end) it->ref = NULL; } @@ -277,7 +280,7 @@ _c_MEMB(_reserve)(Self* self, const isize cap) { if (cap > self->capacity || (cap && cap == self->size)) { _m_value* d = (_m_value*)i_realloc(self->data, self->capacity*c_sizeof *d, cap*c_sizeof *d); - if (!d) + if (d == NULL) return false; self->data = d; self->capacity = cap; diff --git a/include/stc/zsview.h b/include/stc/zsview.h index 0d4f3ce5..afcbcc6f 100644 --- a/include/stc/zsview.h +++ b/include/stc/zsview.h @@ -121,23 +121,23 @@ STC_INLINE bool zsview_u8_valid(zsview zs) // requires linking with utf8 symbols /* utf8 iterator */ STC_INLINE zsview_iter zsview_begin(const zsview* self) { - return c_literal(zsview_iter){.chr = {self->str, utf8_chr_size(self->str)}}; + zsview_iter it = {.chr = {self->str, utf8_chr_size(self->str)}}; return it; } STC_INLINE zsview_iter zsview_end(const zsview* self) { - (void)self; return c_literal(zsview_iter){.ref = NULL}; + (void)self; zsview_iter it = {0}; return it; } STC_INLINE void zsview_next(zsview_iter* it) { it->ref += it->chr.size; it->chr.size = utf8_chr_size(it->ref); - if (!*it->ref) it->ref = NULL; + if (*it->ref == '\0') it->ref = NULL; } STC_INLINE zsview_iter zsview_advance(zsview_iter it, isize u8pos) { it.ref = c_const_cast(char *, utf8_offset(it.ref, u8pos)); it.chr.size = utf8_chr_size(it.ref); - if (!*it.ref) it.ref = NULL; + if (*it.ref == '\0') it.ref = NULL; return it; } diff --git a/misc/examples/coroutines/coread.c b/misc/examples/coroutines/coread.c index 86b4084b..45ac4570 100644 --- a/misc/examples/coroutines/coread.c +++ b/misc/examples/coroutines/coread.c @@ -16,7 +16,7 @@ int file_read(struct file_read* g) { cco_scope(g) { g->fp = fopen(g->filename, "r"); - if (!g->fp) cco_return; + if (g->fp == NULL) cco_return; g->line = (cstr){0}; cco_await( !cstr_getline(&g->line, g->fp) ); diff --git a/misc/examples/hashmaps/hashmap.c b/misc/examples/hashmaps/hashmap.c index 8399966e..124252fb 100644 --- a/misc/examples/hashmaps/hashmap.c +++ b/misc/examples/hashmaps/hashmap.c @@ -6,10 +6,10 @@ #include "stc/hmap.h" const char* call(const char* number) { - if (!strcmp(number, "798-1364")) + if (strcmp(number, "798-1364") == 0) return "We're sorry, the call cannot be completed as dialed." " Please hang up and try again."; - else if (!strcmp(number, "645-7689")) + else if (strcmp(number, "645-7689") == 0) return "Hello, this is Mr. Awesome's Pizza. My name is Fred." " What can I get for you today?"; else