diff options
| author | Tyge Løvset <[email protected]> | 2020-07-16 17:17:24 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2020-07-16 17:17:24 +0200 |
| commit | 5f004f4f91b4a383005c763ea925cf655ba2d7c2 (patch) | |
| tree | 9a4d9162edc37f897f07a82ee15f9adf90a39827 /stc/cstring.h | |
| parent | 1d8b2adc084d684b90ec525e342b7a3a159a0474 (diff) | |
| download | STC-modified-5f004f4f91b4a383005c763ea925cf655ba2d7c2.tar.gz STC-modified-5f004f4f91b4a383005c763ea925cf655ba2d7c2.zip | |
CHanged API: Renamed CString to CStr and CVector to CVec. All function names are changed likewise.
Diffstat (limited to 'stc/cstring.h')
| -rw-r--r-- | stc/cstring.h | 252 |
1 files changed, 126 insertions, 126 deletions
diff --git a/stc/cstring.h b/stc/cstring.h index 5ce9b868..8380e8dd 100644 --- a/stc/cstring.h +++ b/stc/cstring.h @@ -31,28 +31,28 @@ #include "cdefs.h"
-typedef struct CString {
+typedef struct CStr {
char* str;
-} CString;
+} CStr;
-static size_t _cstring_nullrep[] = {0, 0, 0};
-#define _cstring_rep(self) (((size_t *) (self)->str) - 2)
-#define _cstring_size(s) ((size_t *) (s).str)[-2]
-#define _cstring_mem(cap) (sizeof(size_t) * (3 + (cap)/sizeof(size_t)))
+static size_t _cstr_nullrep[] = {0, 0, 0};
+#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)))
-#define cstring_size(s) ((const size_t *) (s).str)[-2]
-#define cstring_capacity(s) ((const size_t *) (s).str)[-1]
-#define cstring_npos ((size_t) (-1))
+#define cstr_size(s) ((const size_t *) (s).str)[-2]
+#define cstr_capacity(s) ((const size_t *) (s).str)[-1]
+#define cstr_npos ((size_t) (-1))
-static const CString cstring_init = {(char* ) &_cstring_nullrep[2]};
+static const CStr cstr_init = {(char* ) &_cstr_nullrep[2]};
static inline void
-cstring_reserve(CString* self, size_t cap) {
- size_t len = cstring_size(*self), oldcap = cstring_capacity(*self);
+cstr_reserve(CStr* self, size_t cap) {
+ size_t len = cstr_size(*self), oldcap = cstr_capacity(*self);
if (cap > oldcap) {
- size_t* rep = (size_t *) realloc(oldcap ? _cstring_rep(self) : NULL, _cstring_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;
@@ -60,67 +60,67 @@ cstring_reserve(CString* self, size_t cap) { }
static inline void
-cstring_resize(CString* self, size_t len, char fill) {
- size_t n = cstring_size(*self);
- cstring_reserve(self, len);
+cstr_resize(CStr* 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[_cstring_size(*self) = len] = '\0';
+ self->str[_cstr_size(*self) = len] = '\0';
}
static inline void
-cstring_destroy(CString* self) {
- if (cstring_capacity(*self)) {
- free(_cstring_rep(self));
+cstr_destroy(CStr* self) {
+ if (cstr_capacity(*self)) {
+ free(_cstr_rep(self));
}
}
-static inline CString
-cstring_makeFilled(size_t len, char fill) {
- CString s = cstring_init;
- if (len) cstring_resize(&s, len, fill);
+static inline CStr
+cstr_makeFilled(size_t len, char fill) {
+ CStr s = cstr_init;
+ if (len) cstr_resize(&s, len, fill);
return s;
}
-static inline CString
-cstring_makeReserved(size_t cap) {
- if (cap == 0) return cstring_init;
- size_t *rep = (size_t *) malloc(_cstring_mem(cap));
- CString s = {(char *) (rep + 2)};
+static inline CStr
+cstr_makeReserved(size_t cap) {
+ if (cap == 0) return cstr_init;
+ size_t *rep = (size_t *) malloc(_cstr_mem(cap));
+ CStr s = {(char *) (rep + 2)};
rep[0] = 0, rep[1] = cap, s.str[0] = '\0';
return s;
}
-static inline CString
-cstring_makeN(const char* str, size_t len) {
- if (len == 0) return cstring_init;
- size_t *rep = (size_t *) malloc(_cstring_mem(len));
- CString s = {(char *) (rep + 2)};
+static inline CStr
+cstr_makeN(const char* str, size_t len) {
+ if (len == 0) return cstr_init;
+ size_t *rep = (size_t *) malloc(_cstr_mem(len));
+ CStr s = {(char *) (rep + 2)};
memcpy(s.str, str, len);
s.str[rep[0] = rep[1] = len] = '\0';
return s;
}
-static inline CString
-cstring_make(const char* str) {
- return cstring_makeN(str, strlen(str));
+static inline CStr
+cstr_make(const char* str) {
+ return cstr_makeN(str, strlen(str));
}
-static inline CString
-cstring_makeCopy(CString s) {
- return cstring_makeN(s.str, cstring_size(s));
+static inline CStr
+cstr_makeCopy(CStr s) {
+ return cstr_makeN(s.str, cstr_size(s));
}
-static inline CString
-cstring_makeFmt(const char* fmt, ...) {
- CString tmp = cstring_init;
+static inline CStr
+cstr_makeFmt(const char* fmt, ...) {
+ CStr tmp = cstr_init;
int len;
va_list args;
va_start(args, fmt);
len = vsnprintf(NULL, (size_t)0, fmt, args);
if (len > 0) {
- tmp = cstring_makeReserved(len);
+ tmp = cstr_makeReserved(len);
vsprintf(tmp.str, fmt, args);
- _cstring_size(tmp) = len;
+ _cstr_size(tmp) = len;
}
va_end(args);
return tmp;
@@ -128,162 +128,162 @@ cstring_makeFmt(const char* fmt, ...) { static inline void
-cstring_clear(CString* self) {
- cstring_destroy(self);
- *self = cstring_init;
+cstr_clear(CStr* self) {
+ cstr_destroy(self);
+ *self = cstr_init;
}
-static inline CString*
-cstring_assignN(CString* self, const char* str, size_t len) {
- if (len || cstring_capacity(*self)) {
- cstring_reserve(self, len);
+static inline CStr*
+cstr_assignN(CStr* self, const char* str, size_t len) {
+ if (len || cstr_capacity(*self)) {
+ cstr_reserve(self, len);
memmove(self->str, str, len);
- self->str[_cstring_size(*self) = len] = '\0';
+ self->str[_cstr_size(*self) = len] = '\0';
}
return self;
}
-static inline CString*
-cstring_assign(CString* self, const char* str) {
- return cstring_assignN(self, str, strlen(str));
+static inline CStr*
+cstr_assign(CStr* self, const char* str) {
+ return cstr_assignN(self, str, strlen(str));
}
-static inline CString*
-cstring_copy(CString* self, CString s) {
- return cstring_assignN(self, s.str, cstring_size(s));
+static inline CStr*
+cstr_copy(CStr* self, CStr s) {
+ return cstr_assignN(self, s.str, cstr_size(s));
}
-static inline CString*
-cstring_take(CString* self, CString s) {
- if (self->str != s.str && cstring_capacity(*self))
- free(_cstring_rep(self));
+static inline CStr*
+cstr_take(CStr* self, CStr s) {
+ if (self->str != s.str && cstr_capacity(*self))
+ free(_cstr_rep(self));
self->str = s.str;
return self;
}
-static inline CString
-cstring_move(CString* self) {
- CString tmp = *self;
- *self = cstring_init;
+static inline CStr
+cstr_move(CStr* self) {
+ CStr tmp = *self;
+ *self = cstr_init;
return tmp;
}
-static inline CString*
-cstring_appendN(CString* self, const char* str, size_t len) {
+static inline CStr*
+cstr_appendN(CStr* 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 = cstr_size(*self), newlen = oldlen + len;
+ if (newlen > cstr_capacity(*self))
+ cstr_reserve(self, newlen * 5 / 3);
memmove(&self->str[oldlen], str, len);
- self->str[_cstring_size(*self) = newlen] = '\0';
+ self->str[_cstr_size(*self) = newlen] = '\0';
}
return self;
}
-static inline CString*
-cstring_append(CString* self, const char* str) {
- return cstring_appendN(self, str, strlen(str));
+static inline CStr*
+cstr_append(CStr* self, const char* str) {
+ return cstr_appendN(self, str, strlen(str));
}
-static inline CString*
-cstring_appendC(CString* self, char ch) {
- return cstring_appendN(self, &ch, 1);
+static inline CStr*
+cstr_appendC(CStr* self, char ch) {
+ return cstr_appendN(self, &ch, 1);
}
-static inline CString*
-cstring_appendS(CString* self, CString s) {
- return cstring_appendN(self, s.str, cstring_size(s));
+static inline CStr*
+cstr_appendS(CStr* self, CStr s) {
+ return cstr_appendN(self, s.str, cstr_size(s));
}
-static inline void _cstring_internalMove(CString* self, size_t pos1, size_t pos2) {
+static inline void _cstr_internalMove(CStr* 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 = cstr_size(*self), newlen = len + pos2 - pos1;
+ if (newlen > cstr_capacity(*self))
+ cstr_reserve(self, newlen * 5 / 3);
memmove(&self->str[pos2], &self->str[pos1], len - pos1);
- self->str[_cstring_size(*self) = newlen] = '\0';
+ self->str[_cstr_size(*self) = newlen] = '\0';
}
static inline void
-cstring_insertN(CString* self, size_t pos, const char* str, size_t n) {
+cstr_insertN(CStr* self, size_t pos, const char* str, size_t n) {
char* xstr = (char *) memcpy(n > c_max_alloca ? malloc(n) : alloca(n), str, n);
- _cstring_internalMove(self, pos, pos + n);
+ _cstr_internalMove(self, pos, pos + n);
memcpy(&self->str[pos], xstr, n);
if (n > c_max_alloca) free(xstr);
}
static inline void
-cstring_insert(CString* self, size_t pos, const char* str) {
- cstring_insertN(self, pos, str, strlen(str));
+cstr_insert(CStr* self, size_t pos, const char* str) {
+ cstr_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);
+cstr_erase(CStr* self, size_t pos, size_t n) {
+ size_t len = cstr_size(*self);
if (len) {
memmove(&self->str[pos], &self->str[pos + n], len - (pos + n));
- self->str[_cstring_size(*self) -= n] = '\0';
+ self->str[_cstr_size(*self) -= n] = '\0';
}
}
-static inline size_t cstring_findN(CString s, size_t pos, const char* needle, size_t n);
+static inline size_t cstr_findN(CStr s, size_t pos, const char* needle, size_t n);
static inline size_t
-cstring_replaceN(CString* self, size_t pos, const char* str1, size_t n1, const char* str2, size_t n2) {
- size_t pos2 = cstring_findN(*self, pos, str1, n1);
- if (pos2 == cstring_npos) return cstring_npos;
+cstr_replaceN(CStr* self, size_t pos, const char* str1, size_t n1, const char* str2, size_t n2) {
+ size_t pos2 = cstr_findN(*self, pos, str1, n1);
+ if (pos2 == cstr_npos) return cstr_npos;
char* xstr2 = (char *) memcpy(n2 > c_max_alloca ? malloc(n2) : alloca(n2), str2, n2);
- _cstring_internalMove(self, pos2 + n1, pos2 + n2);
+ _cstr_internalMove(self, pos2 + n1, pos2 + n2);
memcpy(&self->str[pos2], xstr2, n2);
if (n2 > c_max_alloca) free(xstr2);
return pos2;
}
static inline size_t
-cstring_replace(CString* self, size_t pos, const char* str1, const char* str2) {
- return cstring_replaceN(self, pos, str1, strlen(str1), str2, strlen(str2));
+cstr_replace(CStr* self, size_t pos, const char* str1, const char* str2) {
+ return cstr_replaceN(self, pos, str1, strlen(str1), str2, strlen(str2));
}
static inline char
-cstring_back(CString s) {
- return s.str[cstring_size(s) - 1];
+cstr_back(CStr s) {
+ return s.str[cstr_size(s) - 1];
}
-static inline CString*
-cstring_push(CString* self, char value) {
- return cstring_appendN(self, &value, 1);
+static inline CStr*
+cstr_push(CStr* self, char value) {
+ return cstr_appendN(self, &value, 1);
}
static inline void
-cstring_pop(CString* self) {
- --_cstring_size(*self);
+cstr_pop(CStr* self) {
+ --_cstr_size(*self);
}
/* readonly */
static inline bool
-cstring_empty(CString s) {
- return cstring_size(s) == 0;
+cstr_empty(CStr s) {
+ return cstr_size(s) == 0;
}
static inline bool
-cstring_equals(CString s1, const char* str) {
+cstr_equals(CStr s1, const char* str) {
return strcmp(s1.str, str) == 0;
}
static inline bool
-cstring_equalsS(CString s1, CString s2) {
+cstr_equalsS(CStr s1, CStr s2) {
return strcmp(s1.str, s2.str) == 0;
}
static inline int
-cstring_compare(const void* s1, const void* s2) {
- return strcmp(((const CString*)s1)->str, ((const CString*)s2)->str);
+cstr_compare(const void* s1, const void* s2) {
+ return strcmp(((const CStr*)s1)->str, ((const CStr*)s2)->str);
}
static inline char*
-cstring_strnstr(CString s, size_t pos, const char* needle, size_t n) {
+cstr_strnstr(CStr s, size_t pos, const char* needle, size_t n) {
char *x = s.str + pos, /* haystack */
- *z = s.str + cstring_size(s) - n + 1;
+ *z = s.str + cstr_size(s) - n + 1;
if (x >= z)
return NULL;
ptrdiff_t sum = 0;
@@ -299,24 +299,24 @@ cstring_strnstr(CString s, size_t pos, const char* needle, size_t n) { }
static inline size_t
-cstring_findN(CString s, size_t pos, const char* needle, size_t n) {
- char* res = cstring_strnstr(s, pos, needle, n);
- return res ? res - s.str : cstring_npos;
+cstr_findN(CStr s, size_t pos, const char* needle, size_t n) {
+ char* res = cstr_strnstr(s, pos, needle, n);
+ return res ? res - s.str : cstr_npos;
}
static inline size_t
-cstring_find(CString s, size_t pos, const char* needle) {
+cstr_find(CStr s, size_t pos, const char* needle) {
char* res = strstr(s.str + pos, needle);
- return res ? res - s.str : cstring_npos;
+ return res ? res - s.str : cstr_npos;
}
-/* CVector / CMap API functions: */
+/* CVec / CMap API functions: */
-#define cstring_getRaw(x) ((x)->str)
-#define cstring_compareRaw(x, y) strcmp(*(x), *(y))
-#define cstring_equalsRaw(x, y) (strcmp(*(x), *(y)) == 0)
-static inline uint32_t cstring_hashRaw(const char* const* sPtr, size_t ignored) {
+#define cstr_getRaw(x) ((x)->str)
+#define cstr_compareRaw(x, y) strcmp(*(x), *(y))
+#define cstr_equalsRaw(x, y) (strcmp(*(x), *(y)) == 0)
+static inline uint32_t cstr_hashRaw(const char* const* sPtr, size_t ignored) {
return c_defaultHash(*sPtr, strlen(*sPtr));
}
|
