From fcdbc1ac6b7e6f65d4dde37ed7552707863fcf80 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Sun, 6 Feb 2022 16:15:41 +0100 Subject: Misc improvements. --- include/stc/cregex.h | 17 ++++++----------- include/stc/cstr.h | 18 +++++++++--------- include/stc/csview.h | 4 ++-- 3 files changed, 17 insertions(+), 22 deletions(-) (limited to 'include') diff --git a/include/stc/cregex.h b/include/stc/cregex.h index a6079c38..38d7042c 100644 --- a/include/stc/cregex.h +++ b/include/stc/cregex.h @@ -72,28 +72,23 @@ typedef struct { size_t len; } cregmatch; -/* return number of capture groups on success, or (negative) error code on failure. */ -int cregex_compile(cregex *rx, const char* pattern, int cflags); - static inline cregex cregex_init(void) { cregex rx = {NULL}; return rx; } -static inline cregex cregex_new(const char* pattern, int cflags) { - cregex rx; - cregex_compile(&rx, pattern, cflags); - return rx; -} -/* number of capture groups in the regex pattern */ +/* return number of capture groups on success, or (negative) error code on failure. */ +int cregex_compile(cregex *self, const char* pattern, int cflags); + +/* number of capture groups in a regex pattern */ int cregex_captures(cregex rx); /* return number of capture groups on success, or (negative) error code on failure. */ -int cregex_find(const cregex *rx, const char* string, +int cregex_find(const cregex *self, const char* string, size_t nmatch, cregmatch match[], int mflags); void cregex_replace(const char* src, char* dst, int dsize, int nmatch, const cregmatch match[]); -void cregex_drop(cregex* preg); +void cregex_drop(cregex* self); #endif diff --git a/include/stc/cstr.h b/include/stc/cstr.h index 78fd4cce..937e5151 100644 --- a/include/stc/cstr.h +++ b/include/stc/cstr.h @@ -32,18 +32,18 @@ #include #define cstr_npos (SIZE_MAX >> 1) -typedef struct { size_t size, cap; char str[sizeof(size_t)]; } _cstr_rep_t; -#define _cstr_rep(self) c_container_of((self)->str, _cstr_rep_t, str) +typedef struct { size_t size, cap; char chr; } _cstr_rep_t; +#define _cstr_rep(self) c_container_of((self)->str, _cstr_rep_t, chr) #ifdef _i_static - static _cstr_rep_t _cstr_nullrep = {0, 0, {0}}; - static const cstr cstr_null = {_cstr_nullrep.str}; + static _cstr_rep_t _cstr_nullrep = {0, 0, 0}; + static const cstr cstr_null = {&_cstr_nullrep.chr}; #else extern const cstr cstr_null; #endif /* optimal memory: based on malloc_usable_size() sequence: 24, 40, 56, ... */ -#define _cstr_opt_mem(cap) ((((offsetof(_cstr_rep_t, str) + (cap) + 8)>>4)<<4) + 8) +#define _cstr_opt_mem(cap) ((((offsetof(_cstr_rep_t, chr) + (cap) + 8)>>4)<<4) + 8) /* optimal string capacity: 7, 23, 39, ... */ -#define _cstr_opt_cap(cap) (_cstr_opt_mem(cap) - offsetof(_cstr_rep_t, str) - 1) +#define _cstr_opt_cap(cap) (_cstr_opt_mem(cap) - offsetof(_cstr_rep_t, chr) - 1) STC_API cstr cstr_from_n(const char* str, size_t n); STC_API cstr cstr_from_fmt(const char* fmt, ...); @@ -186,7 +186,7 @@ cstr_reserve(cstr* self, const size_t cap) { const size_t oldcap = rep->cap; if (cap > oldcap) { rep = (_cstr_rep_t*) c_realloc(oldcap ? rep : NULL, _cstr_opt_mem(cap)); - self->str = rep->str; + self->str = &rep->chr; if (oldcap == 0) self->str[rep->size = 0] = '\0'; return (rep->cap = _cstr_opt_cap(cap)); } @@ -205,9 +205,9 @@ STC_DEF cstr cstr_from_n(const char* str, const size_t n) { if (n == 0) return cstr_null; _cstr_rep_t* rep = (_cstr_rep_t*) c_malloc(_cstr_opt_mem(n)); - rep->str[rep->size = n] = '\0'; + cstr s = {(char *) memcpy(&rep->chr, str, n)}; + s.str[rep->size = n] = '\0'; rep->cap = _cstr_opt_cap(n); - cstr s = {(char *) memcpy(rep->str, str, n)}; return s; } diff --git a/include/stc/csview.h b/include/stc/csview.h index 2979b2da..c2bd041d 100644 --- a/include/stc/csview.h +++ b/include/stc/csview.h @@ -144,7 +144,7 @@ csview_substr(csview sv, intptr_t pos, size_t n) { pos += sv.size; if (pos < 0) pos = 0; } - if (pos > sv.size) pos = sv.size; + if (pos > (intptr_t)sv.size) pos = sv.size; if (pos + n > sv.size) n = sv.size - pos; sv.str += pos, sv.size = n; return sv; @@ -157,7 +157,7 @@ csview_slice(csview sv, intptr_t p1, intptr_t p2) { if (p1 < 0) p1 = 0; } if (p2 < 0) p2 += sv.size; - if (p2 > sv.size) p2 = sv.size; + if (p2 > (intptr_t)sv.size) p2 = sv.size; sv.str += p1, sv.size = p2 > p1 ? p2 - p1 : 0; return sv; } -- cgit v1.2.3