From 4da2cdc45522626d005a0221a73064f0217b69fe Mon Sep 17 00:00:00 2001 From: tylo Date: Wed, 11 Mar 2020 11:50:00 +0100 Subject: New API: c_ --- cstring.h | 184 +++++++++++++++++++++++++++++++------------------------------- 1 file changed, 92 insertions(+), 92 deletions(-) (limited to 'cstring.h') diff --git a/cstring.h b/cstring.h index bfffe2e3..e0fcd7e5 100644 --- a/cstring.h +++ b/cstring.h @@ -20,187 +20,187 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -#ifndef CSTRING__H__ -#define CSTRING__H__ +#ifndef C_STRING__H__ +#define C_STRING__H__ #include #include #include #include -#include "cdefs.h" +#include "c_defs.h" -typedef struct CString { +typedef struct c_String { char* str; -} CString; +} c_String; -static size_t _cstring_null_rep[] = {0, 0, 0}; -#define _cstring_rep(cs) (((size_t *) (cs).str) - 2) +static size_t _c_string_null_rep[] = {0, 0, 0}; +#define _c_string_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 ((size_t) -1) +#define c_string_initializer {(char* ) (_c_string_null_rep + 2)} +#define c_string_size(cs) ((size_t) _c_string_rep(cs)[0]) +#define c_string_capacity(cs) ((size_t) _c_string_rep(cs)[1]) +#define c_string_npos ((size_t) -1) -static inline void cstring_reserve(CString* self, size_t cap) { - size_t len = cstring_size(*self), oldcap = cstring_capacity(*self); +static inline void c_string_reserve(c_String* self, size_t cap) { + size_t len = c_string_size(*self), oldcap = c_string_capacity(*self); if (cap > oldcap) { - size_t* rep = (size_t *) realloc(oldcap ? _cstring_rep(*self) : NULL, sizeof(size_t) * 2 + cap + 1); + size_t* rep = (size_t *) realloc(oldcap ? _c_string_rep(*self) : NULL, sizeof(size_t) * 2 + cap + 1); self->str = (char* ) (rep + 2); self->str[ rep[0] = len ] = '\0'; rep[1] = cap; } } -static inline void cstring_destroy(CString* self) { - if (cstring_capacity(*self)) { - free(_cstring_rep(*self)); +static inline void c_string_destroy(c_String* self) { + if (c_string_capacity(*self)) { + free(_c_string_rep(*self)); } } -static inline CString cstring_init(void) { - CString cs = cstring_initializer; +static inline c_String c_string_init(void) { + c_String cs = c_string_initializer; return cs; } -static inline CString cstring_makeN(const char* str, size_t len) { - CString cs = cstring_initializer; +static inline c_String c_string_makeN(const char* str, size_t len) { + c_String cs = c_string_initializer; if (len) { - cstring_reserve(&cs, len); + c_string_reserve(&cs, len); memcpy(cs.str, str, len); - cs.str[ _cstring_rep(cs)[0] = len ] = '\0'; + cs.str[ _c_string_rep(cs)[0] = len ] = '\0'; } return cs; } -static inline CString cstring_make(const char* str) { - return cstring_makeN(str, strlen(str)); +static inline c_String c_string_make(const char* str) { + return c_string_makeN(str, strlen(str)); } -static inline CString cstring_makeCopy(CString cs) { - return cstring_makeN(cs.str, cstring_size(cs)); +static inline c_String c_string_makeCopy(c_String cs) { + return c_string_makeN(cs.str, c_string_size(cs)); } -static inline void cstring_clear(CString* self) { - CString cs = cstring_initializer; - cstring_destroy(self); +static inline void c_string_clear(c_String* self) { + c_String cs = c_string_initializer; + c_string_destroy(self); *self = cs; } -static inline CString* cstring_assignN(CString* self, const char* str, size_t len) { +static inline c_String* c_string_assignN(c_String* self, const char* str, size_t len) { if (len) { - cstring_reserve(self, len); + c_string_reserve(self, len); memmove(self->str, str, len); - self->str[_cstring_rep(*self)[0] = len] = '\0'; + self->str[_c_string_rep(*self)[0] = len] = '\0'; } return self; } -static inline CString* cstring_assign(CString* self, const char* str) { - return cstring_assignN(self, str, strlen(str)); +static inline c_String* c_string_assign(c_String* self, const char* str) { + return c_string_assignN(self, str, strlen(str)); } -static inline CString* cstring_copy(CString* self, CString cs2) { - return cstring_assignN(self, cs2.str, cstring_size(cs2)); +static inline c_String* c_string_copy(c_String* self, c_String cs2) { + return c_string_assignN(self, cs2.str, c_string_size(cs2)); } -static inline CString* cstring_appendN(CString* self, const char* str, size_t len) { +static inline c_String* c_string_appendN(c_String* self, const char* str, size_t len) { if (len) { - size_t oldlen = cstring_size(*self), newlen = oldlen + len; - if (newlen > cstring_capacity(*self)) - cstring_reserve(self, newlen * 5 / 3); + size_t oldlen = c_string_size(*self), newlen = oldlen + len; + if (newlen > c_string_capacity(*self)) + c_string_reserve(self, newlen * 5 / 3); memmove(&self->str[oldlen], str, len); - self->str[_cstring_rep(*self)[0] = newlen] = '\0'; + self->str[_c_string_rep(*self)[0] = newlen] = '\0'; } return self; } -static inline CString* cstring_append(CString* self, const char* str) { - return cstring_appendN(self, str, strlen(str)); +static inline c_String* c_string_append(c_String* self, const char* str) { + return c_string_appendN(self, str, strlen(str)); } -static inline CString* cstring_appendS(CString* self, CString cs2) { - return cstring_appendN(self, cs2.str, cstring_size(cs2)); +static inline c_String* c_string_appendS(c_String* self, c_String cs2) { + return c_string_appendN(self, cs2.str, c_string_size(cs2)); } -static inline void _cstring_internalMove(CString* self, size_t pos1, size_t pos2) { +static inline void _c_string_internalMove(c_String* self, size_t pos1, size_t pos2) { if (pos1 == pos2) return; - size_t len = cstring_size(*self), newlen = len + pos2 - pos1; - if (newlen > cstring_capacity(*self)) - cstring_reserve(self, newlen * 5 / 3); + size_t len = c_string_size(*self), newlen = len + pos2 - pos1; + if (newlen > c_string_capacity(*self)) + c_string_reserve(self, newlen * 5 / 3); memmove(&self->str[pos2], &self->str[pos1], len - pos1); - self->str[_cstring_rep(*self)[0] = newlen] = '\0'; + self->str[_c_string_rep(*self)[0] = newlen] = '\0'; } -static inline void cstring_insertN(CString* self, size_t pos, const char* str, size_t n) { - char* xstr = (char *) memcpy(n > cdefs_max_alloca ? malloc(n) : alloca(n), str, n); - _cstring_internalMove(self, pos, pos + n); +static inline void c_string_insertN(c_String* self, size_t pos, const char* str, size_t n) { + char* xstr = (char *) memcpy(n > c_defs_max_alloca ? malloc(n) : alloca(n), str, n); + _c_string_internalMove(self, pos, pos + n); memcpy(&self->str[pos], xstr, n); - if (n > cdefs_max_alloca) free(xstr); + if (n > c_defs_max_alloca) free(xstr); } -static inline void cstring_insert(CString* self, size_t pos, const char* str) { - cstring_insertN(self, pos, str, strlen(str)); +static inline void c_string_insert(c_String* self, size_t pos, const char* str) { + c_string_insertN(self, pos, str, strlen(str)); } -static inline void cstring_erase(CString* self, size_t pos, size_t n) { - size_t len = cstring_size(*self); +static inline void c_string_erase(c_String* self, size_t pos, size_t n) { + size_t len = c_string_size(*self); if (len) { memmove(&self->str[pos], &self->str[pos + n], len - (pos + n)); - self->str[_cstring_rep(*self)[0] -= n] = '\0'; + self->str[_c_string_rep(*self)[0] -= n] = '\0'; } } -static inline size_t cstring_findN(CString cs, size_t pos, const char* needle, size_t n); +static inline size_t c_string_findN(c_String cs, size_t pos, const char* needle, size_t n); -static inline size_t cstring_replaceN(CString* self, size_t pos, const char* s1, size_t n1, const char* s2, size_t n2) { - size_t pos2 = cstring_findN(*self, pos, s1, n1); - if (pos2 == cstring_npos) return cstring_npos; - char* xs2 = (char *) memcpy(n2 > cdefs_max_alloca ? malloc(n2) : alloca(n2), s2, n2); - _cstring_internalMove(self, pos2 + n1, pos2 + n2); +static inline size_t c_string_replaceN(c_String* self, size_t pos, const char* s1, size_t n1, const char* s2, size_t n2) { + size_t pos2 = c_string_findN(*self, pos, s1, n1); + if (pos2 == c_string_npos) return c_string_npos; + char* xs2 = (char *) memcpy(n2 > c_defs_max_alloca ? malloc(n2) : alloca(n2), s2, n2); + _c_string_internalMove(self, pos2 + n1, pos2 + n2); memcpy(&self->str[pos2], xs2, n2); - if (n2 > cdefs_max_alloca) free(xs2); + if (n2 > c_defs_max_alloca) free(xs2); return pos2; } -static inline size_t cstring_replace(CString* self, size_t pos, const char* s1, const char* s2) { - return cstring_replaceN(self, pos, s1, strlen(s1), s2, strlen(s2)); +static inline size_t c_string_replace(c_String* self, size_t pos, const char* s1, const char* s2) { + return c_string_replaceN(self, pos, s1, strlen(s1), s2, strlen(s2)); } -static inline char cstring_back(CString cs) { - return cs.str[cstring_size(cs) - 1]; +static inline char c_string_back(c_String cs) { + return cs.str[c_string_size(cs) - 1]; } -static inline CString* cstring_push(CString* self, char value) { - return cstring_appendN(self, &value, 1); +static inline c_String* c_string_push(c_String* self, char value) { + return c_string_appendN(self, &value, 1); } -static inline void cstring_pop(CString* self) { - --_cstring_rep(*self)[0]; +static inline void c_string_pop(c_String* self) { + --_c_string_rep(*self)[0]; } /* readonly */ -static inline bool cstring_empty(CString cs) { - return cstring_size(cs) == 0; +static inline bool c_string_empty(c_String cs) { + return c_string_size(cs) == 0; } -static inline bool cstring_equals(CString cs1, const char* str) { +static inline bool c_string_equals(c_String cs1, const char* str) { return strcmp(cs1.str, str) == 0; } -static inline bool cstring_equalsS(CString cs1, CString cs2) { +static inline bool c_string_equalsS(c_String cs1, c_String cs2) { return strcmp(cs1.str, cs2.str) == 0; } -static inline char* cstring_strnstr(CString cs, size_t pos, const char* needle, size_t n) { +static inline char* c_string_strnstr(c_String cs, size_t pos, const char* needle, size_t n) { char *x = cs.str + pos, // haystack - *z = cs.str + cstring_size(cs) - n + 1; + *z = cs.str + c_string_size(cs) - n + 1; if (x >= z) return NULL; ptrdiff_t sum = 0; @@ -215,30 +215,30 @@ static inline char* cstring_strnstr(CString cs, size_t pos, const char* needle, return NULL; } -static inline size_t cstring_findN(CString cs, size_t pos, const char* needle, size_t n) { - char* res = cstring_strnstr(cs, pos, needle, n); - return res ? res - cs.str : cstring_npos; +static inline size_t c_string_findN(c_String cs, size_t pos, const char* needle, size_t n) { + char* res = c_string_strnstr(cs, pos, needle, n); + return res ? res - cs.str : c_string_npos; } -static inline size_t cstring_find(CString cs, size_t pos, const char* needle) { +static inline size_t c_string_find(c_String cs, size_t pos, const char* needle) { char* res = strstr(cs.str + pos, needle); - return res ? res - cs.str : cstring_npos; + return res ? res - cs.str : c_string_npos; } -static inline char* cstring_splitFirst(const char* delimiters, CString cs) { +static inline char* c_string_splitFirst(const char* delimiters, c_String cs) { return strtok(cs.str, delimiters); } -static inline char* cstring_splitNext(const char* delimiters) { +static inline char* c_string_splitNext(const char* delimiters) { return strtok(NULL, delimiters); } -// CVector / CMap API functions: +// c_Vector / c_Hashmap API functions: -#define cstring_getRaw(x) ((x).str) -static inline uint32_t cstring_hashRaw(const char** str, size_t sz_ignored) { return cdefs_murmurHash(*str, strlen(*str)); } -static inline int cstring_compareRaw(CString* self, const char** str, size_t sz_ignored) { return strcmp(self->str, *str); } +#define c_string_getRaw(x) ((x).str) +static inline uint32_t c_string_hashRaw(const char** str, size_t sz_ignored) { return c_defs_murmurHash(*str, strlen(*str)); } +static inline int c_string_compareRaw(c_String* self, const char** str, size_t sz_ignored) { return strcmp(self->str, *str); } #endif -- cgit v1.2.3