diff options
| author | tylo <[email protected]> | 2020-03-09 16:13:23 +0100 |
|---|---|---|
| committer | tylo <[email protected]> | 2020-03-09 16:13:23 +0100 |
| commit | 7ef38a5f3dbf7fbda4137ca3f341e9b962362fab (patch) | |
| tree | 90749ddd7d243fe2f2a29d25be2e6ec0a016b012 /cstring.h | |
| parent | 9a0186979a3ad014352b5b129687df98f060133e (diff) | |
| download | STC-modified-7ef38a5f3dbf7fbda4137ca3f341e9b962362fab.tar.gz STC-modified-7ef38a5f3dbf7fbda4137ca3f341e9b962362fab.zip | |
Added a few features.
Diffstat (limited to 'cstring.h')
| -rw-r--r-- | cstring.h | 15 |
1 files changed, 4 insertions, 11 deletions
@@ -43,17 +43,8 @@ static size_t _cstring_null_rep[] = {0, 0, 0}; #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 cstring_makeTemp(str) _cstring_makeTemp(str, (size_t *) alloca(2 * sizeof(size_t) + strlen(str) + 1))
-static inline CString _cstring_makeTemp(const char* str, size_t *rep) {
- CString cs = {(char *) (rep + 2)};
- strcpy(cs.str, str);
- rep[0] = strlen(str);
- rep[1] = 0; // no cap -> no destroy. OK
- return cs;
-}
-
static inline void cstring_reserve(CString* self, size_t cap) {
size_t len = cstring_size(*self), oldcap = cstring_capacity(*self);
if (cap > oldcap) {
@@ -147,9 +138,10 @@ static inline void _cstring_internalMove(CString* self, size_t pos1, size_t pos2 }
static inline void cstring_insertN(CString* self, size_t pos, const char* str, size_t n) {
- char* xstr = (char *) memcpy(alloca(n), str, n);
+ char* xstr = (char *) memcpy(n > _cdef_max_alloca ? malloc(n) : alloca(n), str, n);
_cstring_internalMove(self, pos, pos + n);
memcpy(&self->str[pos], xstr, n);
+ if (n > _cdef_max_alloca) free(xstr);
}
static inline void cstring_insert(CString* self, size_t pos, const char* str) {
@@ -169,9 +161,10 @@ static inline size_t cstring_findN(CString cs, size_t pos, const char* needle, s 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(alloca(n2), s2, n2);
+ char* xs2 = (char *) memcpy(n2 > _cdef_max_alloca ? malloc(n2) : alloca(n2), s2, n2);
_cstring_internalMove(self, pos2 + n1, pos2 + n2);
memcpy(&self->str[pos2], xs2, n2);
+ if (n2 > _cdef_max_alloca) free(xs2);
return pos2;
}
|
