From a8e178e11e4955306d32e4c48be348d50dbcb3df Mon Sep 17 00:00:00 2001 From: Tyge Løvset <60263450+tylo-work@users.noreply.github.com> Date: Sun, 29 Mar 2020 20:19:34 +0200 Subject: Add files via upload --- c_lib/cstring.h | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) (limited to 'c_lib/cstring.h') diff --git a/c_lib/cstring.h b/c_lib/cstring.h index 92cb1555..d0719cb6 100644 --- a/c_lib/cstring.h +++ b/c_lib/cstring.h @@ -38,12 +38,13 @@ typedef struct CString { static size_t _cstring_null_rep[] = {0, 0, 0}; #define _cstring_rep(cs) (((size_t *) (cs).str) - 2) -#define cstring_initializer {(char* ) (_cstring_null_rep + 2)} #define cstring_size(cs) ((size_t) _cstring_rep(cs)[0]) #define cstring_capacity(cs) ((size_t) _cstring_rep(cs)[1]) #define cstring_npos c_npos +static const CString cstring_init = {(char* ) &_cstring_null_rep[2]}; + static inline void cstring_reserve(CString* self, size_t cap) { size_t len = cstring_size(*self), oldcap = cstring_capacity(*self); if (cap > oldcap) { @@ -60,13 +61,8 @@ static inline void cstring_destroy(CString* self) { } } -static inline CString cstring_init(void) { - CString cs = cstring_initializer; - return cs; -} - static inline CString cstring_makeN(const char* str, size_t len) { - CString cs = cstring_initializer; + CString cs = cstring_init; if (len) { cstring_reserve(&cs, len); memcpy(cs.str, str, len); @@ -84,7 +80,7 @@ static inline CString cstring_makeCopy(CString cs) { } static inline void cstring_clear(CString* self) { - CString cs = cstring_initializer; + CString cs = cstring_init; cstring_destroy(self); *self = cs; } -- cgit v1.2.3