diff options
| author | Tyge Løvset <[email protected]> | 2022-03-30 10:45:35 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2022-03-30 10:45:35 +0200 |
| commit | 0b7d884ca45e7dc08cfacc12b9dab1958bf22580 (patch) | |
| tree | 984b1a9c8b5169b09709b551ef84f7f103c6a297 /include | |
| parent | 6af8fd188566e397abfc54014ea5d981049cb6d8 (diff) | |
| download | STC-modified-0b7d884ca45e7dc08cfacc12b9dab1958bf22580.tar.gz STC-modified-0b7d884ca45e7dc08cfacc12b9dab1958bf22580.zip | |
More enhancements on cstr / cstr_sso.
Diffstat (limited to 'include')
| -rw-r--r-- | include/stc/alt/cstr.h | 23 | ||||
| -rw-r--r-- | include/stc/cstr.h | 29 |
2 files changed, 28 insertions, 24 deletions
diff --git a/include/stc/alt/cstr.h b/include/stc/alt/cstr.h index fa420aba..51a4f521 100644 --- a/include/stc/alt/cstr.h +++ b/include/stc/alt/cstr.h @@ -62,7 +62,7 @@ enum { SSO_CAP = sizeof(cstr_rep_t) - 1 }; #define cstr_l_drop(s) c_free((s)->lon.data)
STC_API char* _cstr_init(cstr* self, size_t len, size_t cap);
-STC_API void _cstr_internal_move(cstr* self, size_t pos1, size_t pos2);
+STC_API char* _cstr_internal_move(cstr* self, size_t pos1, size_t pos2);
STC_INLINE void _cstr_set_size(cstr* s, size_t len) {
SSO_CALL(s, set_size(s, len));
@@ -240,8 +240,8 @@ STC_INLINE void cstr_append_s(cstr* self, cstr s) { }
STC_INLINE void cstr_replace_n(cstr* self, size_t pos, size_t len, const char* str, size_t n) {
- _cstr_internal_move(self, pos + len, pos + n);
- memcpy(&cstr_data(self)[pos], str, n);
+ char* d = _cstr_internal_move(self, pos + len, pos + n);
+ memcpy(d + pos, str, n);
}
STC_INLINE void cstr_replace(cstr* self, size_t pos, size_t len, const char* str) {
@@ -273,15 +273,16 @@ STC_INLINE bool cstr_getline(cstr *self, FILE *fp) { /* -------------------------- IMPLEMENTATION ------------------------- */
#if defined(_i_implement)
-STC_DEF void _cstr_internal_move(cstr* self, const size_t pos1, const size_t pos2) {
- if (pos1 == pos2)
- return;
+STC_DEF char* _cstr_internal_move(cstr* self, const size_t pos1, const size_t pos2) {
cstr_rep_t r = cstr_rep(self);
- const size_t newlen = r.size + pos2 - pos1;
- if (newlen > r.cap)
- r.data = cstr_reserve(self, (r.size*3 >> 1) + pos2 - pos1);
- memmove(&r.data[pos2], &r.data[pos1], r.size - pos1);
- _cstr_set_size(self, newlen);
+ if (pos1 != pos2) {
+ const size_t newlen = r.size + pos2 - pos1;
+ if (newlen > r.cap)
+ r.data = cstr_reserve(self, (r.size*3 >> 1) + pos2 - pos1);
+ memmove(&r.data[pos2], &r.data[pos1], r.size - pos1);
+ _cstr_set_size(self, newlen);
+ }
+ return r.data;
}
STC_DEF char* _cstr_init(cstr* self, const size_t len, const size_t cap) {
diff --git a/include/stc/cstr.h b/include/stc/cstr.h index 1ab38a6e..6a866825 100644 --- a/include/stc/cstr.h +++ b/include/stc/cstr.h @@ -50,7 +50,7 @@ STC_API cstr cstr_from_fmt(const char* fmt, ...); STC_API cstr cstr_from_replace_all(const char* str, size_t str_len,
const char* find, size_t find_len,
const char* repl, size_t repl_len);
-STC_API size_t cstr_reserve(cstr* self, size_t cap);
+STC_API char* cstr_reserve(cstr* self, size_t cap);
STC_API void cstr_resize(cstr* self, size_t len, char fill);
STC_API cstr* cstr_assign_n(cstr* self, const char* str, size_t n);
STC_API int cstr_printf(cstr* self, const char* fmt, ...);
@@ -176,17 +176,17 @@ static struct cstr_priv _cstr_nullrep = {0, 0, {0}}; const cstr cstr_null = {_cstr_nullrep.str};
#endif
-STC_DEF size_t
+STC_DEF char*
cstr_reserve(cstr* self, const size_t cap) {
- cstr_priv* prv = _cstr_p(self);
- const size_t oldcap = prv->cap;
+ cstr_priv* p = _cstr_p(self);
+ const size_t oldcap = p->cap;
if (cap > oldcap) {
- prv = (cstr_priv*) c_realloc(oldcap ? prv : NULL, _cstr_opt_mem(cap));
- self->str = prv->chr;
- if (oldcap == 0) self->str[prv->size = 0] = '\0';
- return (prv->cap = _cstr_opt_cap(cap));
+ p = (cstr_priv*) c_realloc(oldcap ? p : NULL, _cstr_opt_mem(cap));
+ self->str = p->chr;
+ if (oldcap == 0) self->str[p->size = 0] = '\0';
+ p->cap = _cstr_opt_cap(cap);
}
- return oldcap;
+ return self->str;
}
STC_DEF void
@@ -334,17 +334,20 @@ cstr_erase_n(cstr* self, const size_t pos, size_t n) { STC_DEF bool
cstr_getdelim(cstr *self, const int delim, FILE *fp) {
size_t pos = 0, cap = _cstr_p(self)->cap;
+ char* d = self->str;
int c = fgetc(fp);
if (c == EOF)
return false;
for (;;) {
if (c == delim || c == EOF) {
- if (cap) self->str[_cstr_p(self)->size = pos] = '\0';
+ if (cap) d[_cstr_p(self)->size = pos] = '\0';
return true;
}
- if (pos == cap)
- cap = cstr_reserve(self, (cap*3 >> 1) + 16);
- self->str[pos++] = (char) c;
+ if (pos == cap) {
+ d = cstr_reserve(self, (cap*3 >> 1) + 16);
+ cap = cstr_capacity(*self);
+ }
+ d[pos++] = (char) c;
c = fgetc(fp);
}
}
|
