From bf3a35e3cd8d1df9548aa0e57b58d7a0e5dfef14 Mon Sep 17 00:00:00 2001 From: Tyge Lovset Date: Fri, 6 May 2022 05:44:41 +0200 Subject: Made cstr_buffer() func. public, and docs for cstr_sv() - convert to csview. --- docs/cstr_api.md | 13 +++++++++---- include/stc/alt/cstr.h | 4 ++-- include/stc/cstr.h | 22 +++++++++++----------- include/stc/forward.h | 4 ++-- 4 files changed, 24 insertions(+), 19 deletions(-) diff --git a/docs/cstr_api.md b/docs/cstr_api.md index 2e8cde33..66a604a0 100644 --- a/docs/cstr_api.md +++ b/docs/cstr_api.md @@ -31,6 +31,9 @@ void cstr_drop(cstr *self); // destruc const char* cstr_str(const cstr* self); // access to const char* char* cstr_data(cstr* self); // access to char* +csview cstr_sv(const cstr* self); // access to string view +cstr_buf cstr_buffer(cstr* self); // access to mutable buffer (with capacity) + size_t cstr_size(cstr s); size_t cstr_length(cstr s); size_t cstr_capacity(cstr s); @@ -94,10 +97,12 @@ int c_strncasecmp(const char* str1, const char* str2, size_t n); ## Types -| Type name | Type definition | Used to represent... | -|:----------------|:-------------------------------|:-------------------------| -| `cstr` | `struct { char *str; }` | The string type | -| `cstr_value` | `char` | The string element type | +| Type name | Type definition | Used to represent... | +|:----------------|:-------------------------------------------|:---------------------| +| `cstr` | `struct { ... }` | The string type | +| `cstr_value` | `char` | String element type | +| `csview` | `struct { const char *str; size_t size; }` | String view type | +| `cstr_buf` | `struct { char *data; size_t size, cap; }` | String buffer type | ## Constants and macros diff --git a/include/stc/alt/cstr.h b/include/stc/alt/cstr.h index ec7f3bba..db9c0505 100644 --- a/include/stc/alt/cstr.h +++ b/include/stc/alt/cstr.h @@ -120,9 +120,9 @@ STC_INLINE bool cstr_contains(cstr s, const char* needle) STC_INLINE bool cstr_getline(cstr *self, FILE *stream) { return cstr_getdelim(self, '\n', stream); } -STC_INLINE cstr_rep_t cstr_rep(cstr* s) { +STC_INLINE cstr_buf cstr_buffer(cstr* s) { cstr_priv* p = _cstr_p(s); - return c_make(cstr_rep_t){s->str, p->size, p->cap}; + return c_make(cstr_buf){s->str, p->size, p->cap}; } STC_INLINE cstr cstr_with_capacity(const size_t cap) { diff --git a/include/stc/cstr.h b/include/stc/cstr.h index 20b50ee9..85c5061f 100644 --- a/include/stc/cstr.h +++ b/include/stc/cstr.h @@ -45,7 +45,7 @@ # pragma GCC diagnostic ignored "-Wstringop-overflow=" #endif -enum { cstr_s_cap = sizeof(cstr_rep_t) - 1 }; +enum { cstr_s_cap = sizeof(cstr_buf) - 1 }; #define cstr_s_size(s) ((size_t)(cstr_s_cap - (s)->sml.last)) #define cstr_s_set_size(s, len) ((s)->sml.last = cstr_s_cap - (len), (s)->sml.data[len] = 0) #define cstr_s_data(s) (s)->sml.data @@ -89,10 +89,10 @@ STC_API int cstr_printf(cstr* self, const char* fmt, ...); STC_API void cstr_replace_all(cstr* self, const char* find, const char* repl); STC_API cstr cstr_from_replace_all_sv(csview sv, csview find, csview repl); -STC_INLINE cstr_rep_t cstr_rep(cstr* s) { +STC_INLINE cstr_buf cstr_buffer(cstr* s) { return cstr_is_long(s) - ? c_make(cstr_rep_t){s->lon.data, cstr_l_size(s), cstr_l_cap(s)} - : c_make(cstr_rep_t){s->sml.data, cstr_s_size(s), cstr_s_cap}; + ? c_make(cstr_buf){s->lon.data, cstr_l_size(s), cstr_l_cap(s)} + : c_make(cstr_buf){s->sml.data, cstr_s_size(s), cstr_s_cap}; } STC_INLINE csview cstr_sv(const cstr* s) { return cstr_is_long(s) ? c_make(csview){s->lon.data, cstr_l_size(s)} @@ -278,7 +278,7 @@ STC_INLINE uint64_t cstr_hash(const cstr *self) { #if defined(i_implement) STC_DEF char* _cstr_internal_move(cstr* self, const size_t pos1, const size_t pos2) { - cstr_rep_t r = cstr_rep(self); + cstr_buf r = cstr_buffer(self); if (pos1 != pos2) { const size_t newlen = r.size + pos2 - pos1; if (newlen > r.cap) @@ -301,7 +301,7 @@ STC_DEF char* _cstr_init(cstr* self, const size_t len, const size_t cap) { } STC_DEF void cstr_shrink_to_fit(cstr* self) { - cstr_rep_t r = cstr_rep(self); + cstr_buf r = cstr_buffer(self); if (r.size == r.cap) return; if (r.size > cstr_s_cap) { @@ -336,7 +336,7 @@ STC_DEF char* cstr_reserve(cstr* self, const size_t cap) { } STC_DEF void cstr_resize(cstr* self, const size_t size, const char value) { - cstr_rep_t r = cstr_rep(self); + cstr_buf r = cstr_buffer(self); if (size > r.size) { if (size > r.cap) r.data = cstr_reserve(self, size); memset(r.data + r.size, value, size - r.size); @@ -353,7 +353,7 @@ STC_DEF size_t cstr_find_n(cstr s, const char* needle, const size_t pos, const s } STC_DEF cstr* cstr_assign_n(cstr* self, const char* str, const size_t n) { - cstr_rep_t r = cstr_rep(self); + cstr_buf r = cstr_buffer(self); if (n > r.cap) { r.data = (char *)c_realloc(cstr_is_long(self) ? r.data : NULL, n + 1); cstr_l_set_cap(self, n); @@ -364,7 +364,7 @@ STC_DEF cstr* cstr_assign_n(cstr* self, const char* str, const size_t n) { } STC_DEF cstr* cstr_append_n(cstr* self, const char* str, const size_t n) { - cstr_rep_t r = cstr_rep(self); + cstr_buf r = cstr_buffer(self); if (r.size + n > r.cap) { const size_t off = (size_t)(str - r.data); r.data = cstr_reserve(self, (r.size*3 >> 1) + n); @@ -380,7 +380,7 @@ STC_DEF bool cstr_getdelim(cstr *self, const int delim, FILE *fp) { if (c == EOF) return false; size_t pos = 0; - cstr_rep_t r = cstr_rep(self); + cstr_buf r = cstr_buffer(self); for (;;) { if (c == delim || c == EOF) { _cstr_set_size(self, pos); @@ -426,7 +426,7 @@ cstr_from_replace_all_sv(csview sv, csview find, csview repl) { } STC_DEF void cstr_erase_n(cstr* self, const size_t pos, size_t n) { - cstr_rep_t r = cstr_rep(self); + cstr_buf r = cstr_buffer(self); if (n > r.size - pos) n = r.size - pos; memmove(&r.data[pos], &r.data[pos + n], r.size - (pos + n)); _cstr_set_size(self, r.size - n); diff --git a/include/stc/forward.h b/include/stc/forward.h index 6af17c2b..9952416a 100644 --- a/include/stc/forward.h +++ b/include/stc/forward.h @@ -42,11 +42,11 @@ #define forward_cqueue(CX, VAL) _c_cdeq_types(CX, VAL) #define forward_cvec(CX, VAL) _c_cvec_types(CX, VAL) -typedef struct { char* data; size_t size, cap; } cstr_rep_t; +typedef struct { char* data; size_t size, cap; } cstr_buf; typedef char cstr_value; #ifndef STC_OLD_CSTR typedef union { - struct { char data[sizeof(cstr_rep_t) - 1]; unsigned char last; } sml; + struct { char data[sizeof(cstr_buf) - 1]; unsigned char last; } sml; struct { char* data; size_t size, ncap; } lon; } cstr; #else -- cgit v1.2.3