From eda5a6418d0dda97261e340998f8f7e23a8e57b2 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Fri, 13 May 2022 13:42:20 +0200 Subject: Added cstr_assign_s(), and changed return type to char* for cstr_assign*() and cstr_append*(), i.e. the pointer to the (possible new) string buffer. --- docs/cstr_api.md | 13 +++++++------ docs/csview_api.md | 4 ++-- include/stc/cstr.h | 31 +++++++++++++++++-------------- include/stc/csview.h | 8 ++++---- 4 files changed, 30 insertions(+), 26 deletions(-) diff --git a/docs/cstr_api.md b/docs/cstr_api.md index 54178d29..e5a3ce37 100644 --- a/docs/cstr_api.md +++ b/docs/cstr_api.md @@ -45,14 +45,15 @@ void cstr_shrink_to_fit(cstr* self); char* cstr_expand_uninit(cstr* self, size_t n); // return ptr to uninit data void cstr_clear(cstr* self); -cstr* cstr_assign(cstr* self, const char* str); -cstr* cstr_assign_n(cstr* self, const char* str, size_t n); // assign n first chars of str -cstr* cstr_copy(cstr* self, cstr s); // cstr_take(self, cstr_clone(s)) +char* cstr_assign(cstr* self, const char* str); +char* cstr_assign_s(cstr* self, cstr s); +char* cstr_assign_n(cstr* self, const char* str, size_t n); // assign n first chars of str +void cstr_copy(cstr* self, cstr s); // like cstr_assign_s() int cstr_printf(cstr* self, const char* fmt, ...); // printf() formatting -cstr* cstr_append(cstr* self, const char* str); -cstr* cstr_append_s(cstr* self, cstr s); -cstr* cstr_append_n(cstr* self, const char* str, size_t n); +char* cstr_append(cstr* self, const char* str); +char* cstr_append_s(cstr* self, cstr s); +char* cstr_append_n(cstr* self, const char* str, size_t n); void cstr_insert(cstr* self, size_t pos, const char* str); void cstr_insert_s(cstr* self, size_t pos, cstr s); diff --git a/docs/csview_api.md b/docs/csview_api.md index b4319c85..0a071278 100644 --- a/docs/csview_api.md +++ b/docs/csview_api.md @@ -82,8 +82,8 @@ cstr cstr_from_replace_all_sv(csview sv, csview find, csview replace) csview cstr_substr(const cstr* s, intptr_t pos, size_t n); // negative pos count from end csview cstr_slice(const cstr* s, intptr_t p, intptr_t q); // negative p or q count from end -cstr* cstr_assign_sv(cstr* self, csview sv); -cstr* cstr_append_sv(cstr* self, csview sv); +csview cstr_assign_sv(cstr* self, csview sv); // return csview of assigned cstr +void cstr_append_sv(cstr* self, csview sv); void cstr_insert_sv(cstr* self, size_t pos, csview sv); void cstr_replace_sv(cstr* self, size_t pos, size_t len, csview sv); diff --git a/include/stc/cstr.h b/include/stc/cstr.h index a6400dc5..a1347630 100644 --- a/include/stc/cstr.h +++ b/include/stc/cstr.h @@ -80,8 +80,8 @@ STC_API char* cstr_reserve(cstr* self, size_t cap); STC_API void cstr_shrink_to_fit(cstr* self); STC_API void cstr_resize(cstr* self, size_t size, char value); STC_API size_t cstr_find_n(cstr s, const char* needle, size_t pos, size_t nmax); -STC_API cstr* cstr_assign_n(cstr* self, const char* str, size_t n); -STC_API cstr* cstr_append_n(cstr* self, const char* str, size_t n); +STC_API char* cstr_assign_n(cstr* self, const char* str, size_t n); +STC_API char* cstr_append_n(cstr* self, const char* str, size_t n); STC_API bool cstr_getdelim(cstr *self, int delim, FILE *fp); STC_API void cstr_erase_n(cstr* self, size_t pos, size_t n); STC_API cstr cstr_from_fmt(const char* fmt, ...); @@ -226,20 +226,23 @@ STC_INLINE bool cstr_ends_with(cstr s, const char* sub) { STC_INLINE bool cstr_ends_with_s(cstr s, cstr sub) { return cstr_ends_with(s, cstr_str(&sub)); } -STC_INLINE void cstr_assign(cstr* self, const char* str) - { cstr_assign_n(self, str, strlen(str)); } +STC_INLINE char* cstr_assign(cstr* self, const char* str) + { return cstr_assign_n(self, str, strlen(str)); } -STC_INLINE void cstr_copy(cstr* self, cstr s) { +STC_INLINE char* cstr_assign_s(cstr* self, cstr s) { csview sv = cstr_sv(&s); - cstr_assign_n(self, sv.str, sv.size); + return cstr_assign_n(self, sv.str, sv.size); } -STC_INLINE void cstr_append(cstr* self, const char* str) - { cstr_append_n(self, str, strlen(str)); } +STC_INLINE void cstr_copy(cstr* self, cstr s) + { cstr_assign_s(self, s); } -STC_INLINE void cstr_append_s(cstr* self, cstr s) { +STC_INLINE char* cstr_append(cstr* self, const char* str) + { return cstr_append_n(self, str, strlen(str)); } + +STC_INLINE char* cstr_append_s(cstr* self, cstr s) { csview sv = cstr_sv(&s); - cstr_append_n(self, sv.str, sv.size); + return cstr_append_n(self, sv.str, sv.size); } STC_INLINE void cstr_replace_n(cstr* self, size_t pos, size_t len, const char* str, size_t n) { @@ -352,14 +355,14 @@ STC_DEF size_t cstr_find_n(cstr s, const char* needle, const size_t pos, const s return res ? res - sv.str : cstr_npos; } -STC_DEF cstr* cstr_assign_n(cstr* self, const char* str, const size_t n) { +STC_DEF char* cstr_assign_n(cstr* self, const char* str, const size_t n) { char* d = cstr_reserve(self, n); memmove(d, str, n); _cstr_set_size(self, n); - return self; + return d; } -STC_DEF cstr* cstr_append_n(cstr* self, const char* str, const size_t n) { +STC_DEF char* cstr_append_n(cstr* self, const char* str, const size_t n) { cstr_buf r = cstr_buffer(self); if (r.size + n > r.cap) { const size_t off = (size_t)(str - r.data); @@ -368,7 +371,7 @@ STC_DEF cstr* cstr_append_n(cstr* self, const char* str, const size_t n) { } memcpy(r.data + r.size, str, n); _cstr_set_size(self, r.size + n); - return self; + return r.data; } STC_DEF bool cstr_getdelim(cstr *self, const int delim, FILE *fp) { diff --git a/include/stc/csview.h b/include/stc/csview.h index 53f08dbb..4cbe1ee2 100644 --- a/include/stc/csview.h +++ b/include/stc/csview.h @@ -96,10 +96,10 @@ STC_INLINE csview cstr_substr(const cstr* self, intptr_t pos, size_t n) { return csview_substr(csview_from_s(self), pos, n); } STC_INLINE csview cstr_slice(const cstr* self, intptr_t p1, intptr_t p2) { return csview_slice(csview_from_s(self), p1, p2); } -STC_INLINE cstr* cstr_assign_sv(cstr* self, csview sv) - { return cstr_assign_n(self, sv.str, sv.size); } -STC_INLINE cstr* cstr_append_sv(cstr* self, csview sv) - { return cstr_append_n(self, sv.str, sv.size); } +STC_INLINE csview cstr_assign_sv(cstr* self, csview sv) + { return c_make(csview){cstr_assign_n(self, sv.str, sv.size), sv.size}; } +STC_INLINE void cstr_append_sv(cstr* self, csview sv) + { cstr_append_n(self, sv.str, sv.size); } STC_INLINE void cstr_insert_sv(cstr* self, size_t pos, csview sv) { cstr_replace_n(self, pos, 0, sv.str, sv.size); } STC_INLINE void cstr_replace_sv(cstr* self, csview sub, csview with) -- cgit v1.2.3