From 8a9693b19436f54ac62c68585284869f9c2aa932 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Tue, 4 Aug 2020 11:58:27 +0200 Subject: Revert c_nullptr to NULL in lib code. Minor cleanup. --- stc/clist.h | 30 +++++++++++++++--------------- stc/cmap.h | 12 ++++++------ stc/copt.h | 2 +- stc/cstr.h | 30 ++++++++++++++++-------------- stc/cvec.h | 8 ++++---- 5 files changed, 42 insertions(+), 40 deletions(-) diff --git a/stc/clist.h b/stc/clist.h index 8f81637c..4e834d16 100644 --- a/stc/clist.h +++ b/stc/clist.h @@ -81,10 +81,10 @@ clist_##tag##_node_t *item, **_last; \ } clist_##tag##_iter_t -#define clist_init {c_nullptr} +#define clist_init {NULL} #define clist_front(list) (list).last->next->value #define clist_back(list) (list).last->value -#define clist_empty(list) ((list).last == c_nullptr) +#define clist_empty(list) ((list).last == NULL) #define declare_clist_6(tag, Value, valueDestroy, RawValue, valueCompareRaw, valueGetRaw) \ @@ -129,12 +129,12 @@ \ STC_INLINE clist_##tag##_iter_t \ clist_##tag##_begin(clist_##tag* self) { \ - clist_##tag##_node_t *head = self->last ? self->last->next : c_nullptr; \ + clist_##tag##_node_t *head = self->last ? self->last->next : NULL; \ clist_##tag##_iter_t it = {head, &self->last}; return it; \ } \ STC_INLINE clist_##tag##_iter_t \ clist_##tag##_next(clist_##tag##_iter_t it) { \ - it.item = it.item == *it._last ? c_nullptr : it.item->next; return it; \ + it.item = it.item == *it._last ? NULL : it.item->next; return it; \ } \ STC_INLINE clist_##tag##_iter_t \ clist_##tag##_last(clist_##tag* self) { \ @@ -206,14 +206,14 @@ return prev; \ prev = i; \ } \ - prev.item = c_nullptr; \ + prev.item = NULL; \ return prev; \ } \ \ STC_API Value* \ clist_##tag##_find(clist_##tag* self, RawValue val) { \ clist_##tag##_iter_t it = clist_##tag##_find_before(self, val); \ - return it.item ? &it.item->next->value : c_nullptr; \ + return it.item ? &it.item->next->value : NULL; \ } \ \ STC_API clist_##tag##_iter_t \ @@ -246,7 +246,7 @@ #define _clist_erase_after(self, tag, node, valueDestroy) \ clist_##tag##_node_t* del = node->next, *next = del->next; \ node->next = next; \ - if (del == next) self->last = c_nullptr; \ + if (del == next) self->last = NULL; \ else if (self->last == del) self->last = node; \ valueDestroy(&del->value); \ free(del) @@ -264,7 +264,7 @@ _clist_splice(clist_void* self, clist_void_iter_t pos, clist_void* other, bool b other->last->next = next; if (bottom && pos.item == self->last) self->last = other->last; } - other->last = c_nullptr; + other->last = NULL; } /* Singly linked list Mergesort implementation by Simon Tatham. O(n*log n). @@ -274,12 +274,12 @@ STC_API clist_void_node_t * _clist_mergesort(clist_void_node_t *list, int (*cmp)(const void*, const void*)) { clist_void_node_t *p, *q, *e, *tail, *oldhead; int insize = 1, nmerges, psize, qsize, i; - if (!list) return c_nullptr; + if (!list) return NULL; while (1) { p = list; oldhead = list; - list = tail = c_nullptr; + list = tail = NULL; nmerges = 0; while (p) { @@ -288,7 +288,7 @@ _clist_mergesort(clist_void_node_t *list, int (*cmp)(const void*, const void*)) psize = 0; for (i = 0; i < insize; ++i) { ++psize; - q = (q->next == oldhead ? c_nullptr : q->next); + q = (q->next == oldhead ? NULL : q->next); if (!q) break; } qsize = insize; @@ -296,16 +296,16 @@ _clist_mergesort(clist_void_node_t *list, int (*cmp)(const void*, const void*)) while (psize > 0 || (qsize > 0 && q)) { if (psize == 0) { e = q; q = q->next; --qsize; - if (q == oldhead) q = c_nullptr; + if (q == oldhead) q = NULL; } else if (qsize == 0 || !q) { e = p; p = p->next; --psize; - if (p == oldhead) p = c_nullptr; + if (p == oldhead) p = NULL; } else if (cmp(p, q) <= 0) { e = p; p = p->next; --psize; - if (p == oldhead) p = c_nullptr; + if (p == oldhead) p = NULL; } else { e = q; q = q->next; --qsize; - if (q == oldhead) q = c_nullptr; + if (q == oldhead) q = NULL; } if (tail) tail->next = e; diff --git a/stc/cmap.h b/stc/cmap.h index 164e4144..e0a938ff 100644 --- a/stc/cmap.h +++ b/stc/cmap.h @@ -38,7 +38,7 @@ int main(void) { cmap_mx_put(&m, 5, 'a'); cmap_mx_put(&m, 8, 'b'); cmap_mx_put(&m, 12, 'c'); - cmap_mx_entry_t *e = cmap_mx_find(&m, 10); // = c_nullptr + cmap_mx_entry_t *e = cmap_mx_find(&m, 10); // = NULL char val = cmap_mx_find(&m, 5)->value; cmap_mx_put(&m, 5, 'd'); // update cmap_mx_erase(&m, 8); @@ -53,7 +53,7 @@ int main(void) { #include #include "cdefs.h" -#define cmap_init {c_nullptr, c_nullptr, 0, 0, 0.85f, 0.15f} +#define cmap_init {NULL, NULL, 0, 0, 0.85f, 0.15f} #define cmap_size(m) ((size_t) (m).size) #define cset_init cmap_init #define cset_size(s) cmap_size(s) @@ -257,10 +257,10 @@ ctype##_##tag##_bucket(const ctype##_##tag* self, const ctype##_##tag##_rawkey_t \ STC_API ctype##_##tag##_entry_t* \ ctype##_##tag##_find(const ctype##_##tag* self, ctype##_##tag##_rawkey_t rawKey) { \ - if (self->size == 0) return c_nullptr; \ + if (self->size == 0) return NULL; \ uint32_t hx; \ size_t idx = ctype##_##tag##_bucket(self, &rawKey, &hx); \ - return self->_hashx[idx] ? &self->table[idx] : c_nullptr; \ + return self->_hashx[idx] ? &self->table[idx] : NULL; \ } \ \ STC_INLINE void ctype##_##tag##_reserve_expand(ctype##_##tag* self) { \ @@ -368,13 +368,13 @@ ctype##_##tag##_begin(ctype##_##tag* map) { \ uint8_t* hx = map->_hashx; \ ctype##_##tag##_entry_t* e = map->table, *end = e + map->bucket_count; \ while (e != end && !*hx) ++e, ++hx; \ - ctype##_##tag##_iter_t it = {e == end ? c_nullptr : e, end, hx}; return it; \ + ctype##_##tag##_iter_t it = {e == end ? NULL : e, end, hx}; return it; \ } \ \ STC_API ctype##_##tag##_iter_t \ ctype##_##tag##_next(ctype##_##tag##_iter_t it) { \ do { ++it.item, ++it._hx; } while (it.item != it._end && !*it._hx); \ - if (it.item == it._end) it.item = c_nullptr; \ + if (it.item == it._end) it.item = NULL; \ return it; \ } diff --git a/stc/copt.h b/stc/copt.h index 26a7e94d..49c895e0 100644 --- a/stc/copt.h +++ b/stc/copt.h @@ -83,7 +83,7 @@ typedef struct { int val; } copt_long_t; -static const copt_t copt_init = {1, 0, c_nullptr, c_nullptr, -1, 1, 0, 0, {'-', '?', '\0'}}; +static const copt_t copt_init = {1, 0, NULL, NULL, -1, 1, 0, 0, {'-', '?', '\0'}}; static void _copt_permute(char *argv[], int j, int n) { /* move argv[j] over n elements to the left */ int k; diff --git a/stc/cstr.h b/stc/cstr.h index f9e2f884..ca7cf6a0 100644 --- a/stc/cstr.h +++ b/stc/cstr.h @@ -35,9 +35,6 @@ typedef struct cstr_t { char* str; } cstr_t; -#define _cstr_rep(self) (((size_t *) (self)->str) - 2) -#define _cstr_size(s) ((size_t *) (s).str)[-2] -#define _cstr_mem(cap) (sizeof(size_t) * (3 + (cap)/sizeof(size_t))) static size_t _cstr_nullrep[] = {0, 0, 0}; static cstr_t cstr_init = {(char* ) &_cstr_nullrep[2]}; @@ -66,6 +63,11 @@ cstr_erase(cstr_t* self, size_t pos, size_t n); STC_API char* cstr_strnstr(cstr_t s, size_t pos, const char* needle, size_t n); +#define _cstr_rep(self) (((size_t *) (self)->str) - 2) +#define _cstr_size(s) ((size_t *) (s).str)[-2] +#define _cstr_mem(cap) ((3 + (cap) / sizeof(size_t)) * sizeof(size_t)) +#define _cstr_cap(cap) ((1 + (cap) / sizeof(size_t)) * sizeof(size_t) - 1) + STC_INLINE void cstr_destroy(cstr_t* self) { if (cstr_capacity(*self)) @@ -207,10 +209,10 @@ STC_API void cstr_reserve(cstr_t* self, size_t cap) { size_t len = cstr_size(*self), oldcap = cstr_capacity(*self); if (cap > oldcap) { - size_t* rep = (size_t *) realloc(oldcap ? _cstr_rep(self) : c_nullptr, _cstr_mem(cap)); + size_t* rep = (size_t *) realloc(oldcap ? _cstr_rep(self) : NULL, _cstr_mem(cap)); self->str = (char *) (rep + 2); self->str[rep[0] = len] = '\0'; - rep[1] = cap; + rep[1] = _cstr_cap(cap); } } @@ -219,16 +221,16 @@ cstr_resize(cstr_t* self, size_t len, char fill) { size_t n = cstr_size(*self); cstr_reserve(self, len); if (len > n) memset(self->str + n, fill, len - n); - self->str[_cstr_size(*self) = len] = '\0'; + if (len | n) self->str[_cstr_size(*self) = len] = '\0'; } STC_API cstr_t cstr_make_n(const char* str, size_t len) { if (len == 0) return cstr_init; size_t *rep = (size_t *) malloc(_cstr_mem(len)); - cstr_t s = {(char *) (rep + 2)}; - memcpy(s.str, str, len); - s.str[rep[0] = rep[1] = len] = '\0'; + cstr_t s = {(char *) memcpy(rep + 2, str, len)}; + s.str[rep[0] = len] = '\0'; + rep[1] = _cstr_cap(len); return s; } @@ -237,7 +239,7 @@ cstr_from(const char* fmt, ...) { cstr_t tmp = cstr_init; va_list args; va_start(args, fmt); - int len = vsnprintf(c_nullptr, (size_t)0, fmt, args); + int len = vsnprintf(NULL, (size_t)0, fmt, args); if (len > 0) { tmp = cstr_with_capacity(len); vsprintf(tmp.str, fmt, args); @@ -262,7 +264,7 @@ cstr_append_n(cstr_t* self, const char* str, size_t len) { if (len) { size_t oldlen = cstr_size(*self), newlen = oldlen + len; if (newlen > cstr_capacity(*self)) - cstr_reserve(self, newlen * 5 / 3); + cstr_reserve(self, 5 + newlen * 3 / 2); memmove(&self->str[oldlen], str, len); self->str[_cstr_size(*self) = newlen] = '\0'; } @@ -274,7 +276,7 @@ STC_INLINE void _cstr_internal_move(cstr_t* self, size_t pos1, size_t pos2) { return; size_t len = cstr_size(*self), newlen = len + pos2 - pos1; if (newlen > cstr_capacity(*self)) - cstr_reserve(self, newlen * 5 / 3); + cstr_reserve(self, 5 + newlen * 3 / 2); memmove(&self->str[pos2], &self->str[pos1], len - pos1); self->str[_cstr_size(*self) = newlen] = '\0'; } @@ -312,7 +314,7 @@ cstr_strnstr(cstr_t s, size_t pos, const char* needle, size_t n) { char *x = s.str + pos, /* haystack */ *z = s.str + cstr_size(s) - n + 1; if (x >= z) - return c_nullptr; + return NULL; ptrdiff_t sum = 0; const char *y = x, *p = needle, *q = needle + n; while (p != q) @@ -322,7 +324,7 @@ cstr_strnstr(cstr_t s, size_t pos, const char* needle, size_t n) { return x; sum += *y++ - *x++; } - return c_nullptr; + return NULL; } #endif diff --git a/stc/cvec.h b/stc/cvec.h index 42d430a7..4be04eca 100644 --- a/stc/cvec.h +++ b/stc/cvec.h @@ -27,7 +27,7 @@ #include #include "cdefs.h" -#define cvec_init {c_nullptr} +#define cvec_init {NULL} #define cvec_size(cv) _cvec_safe_size((cv).data) #define cvec_capacity(cv) _cvec_safe_capacity((cv).data) #define cvec_empty(cv) (_cvec_safe_size((cv).data) == 0) @@ -113,12 +113,12 @@ typedef struct { \ STC_INLINE cvec_##tag##_iter_t \ cvec_##tag##_begin(cvec_##tag* vec) { \ const size_t n = cvec_size(*vec); \ - cvec_##tag##_iter_t it = {n ? vec->data : c_nullptr, vec->data + n}; \ + cvec_##tag##_iter_t it = {n ? vec->data : NULL, vec->data + n}; \ return it; \ } \ STC_INLINE cvec_##tag##_iter_t \ cvec_##tag##_next(cvec_##tag##_iter_t it) { \ - if (++it.item == it.end) it.item = c_nullptr; \ + if (++it.item == it.end) it.item = NULL; \ return it; \ } \ \ @@ -230,7 +230,7 @@ cvec_##tag##_sort(cvec_##tag* self) { \ #define _cvec_capacity(cv) ((size_t *)(cv).data)[-1] STC_INLINE size_t* _cvec_alloced(void* data) { - return data ? ((size_t *) data) - 2 : c_nullptr; + return data ? ((size_t *) data) - 2 : NULL; } STC_INLINE size_t _cvec_safe_size(const void* data) { return data ? ((const size_t *) data)[-2] : 0; -- cgit v1.2.3