From 3776efeb26088cde334eda8e8dcda66431a9a0cf Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Fri, 18 Jun 2021 23:30:14 +0200 Subject: Simplified and removed unneeded stuff from csview.h --- include/stc/cstr.h | 40 ++++++++++++++++------------ include/stc/csview.h | 73 +++++----------------------------------------------- 2 files changed, 30 insertions(+), 83 deletions(-) (limited to 'include') diff --git a/include/stc/cstr.h b/include/stc/cstr.h index c81604c5..6b5efd82 100644 --- a/include/stc/cstr.h +++ b/include/stc/cstr.h @@ -49,13 +49,16 @@ typedef const char strlit_t[]; STC_API cstr cstr_from_n(const char* str, size_t n); 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 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 cstr* cstr_assign_fmt(cstr* self, const char* fmt, ...); STC_API cstr* cstr_append_n(cstr* self, const char* str, size_t n); STC_API void cstr_replace_n(cstr* self, size_t pos, size_t len, const char* str, size_t n); -STC_API size_t cstr_replace_all(cstr* self, const char* find, const char* replace); +STC_API void cstr_replace_all(cstr* self, const char* find, const char* replace); STC_API void cstr_erase_n(cstr* self, size_t pos, size_t n); STC_API size_t cstr_find(cstr s, const char* needle); STC_API size_t cstr_find_n(cstr s, const char* needle, size_t pos, size_t nmax); @@ -313,22 +316,27 @@ cstr_replace_n(cstr* self, size_t pos, size_t len, const char* str, size_t n) { } } -STC_DEF size_t -cstr_replace_all(cstr* self, const char* find, const char* repl) { +STC_DEF 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) { cstr out = cstr_null; - size_t len = cstr_size(*self), find_len = strlen(find), repl_len = strlen(repl); - size_t from = 0, n = 0, pos; char* res; - if (!find_len) return 0; - while ((res = c_strnstrn(self->str + from, find, len - from, find_len))) { - pos = res - self->str; - cstr_append_n(&out, self->str + from, pos - from); - cstr_append_n(&out, repl, repl_len); - from = pos + find_len; ++n; - } - if (!n) return 0; - cstr_append_n(&out, self->str + from, len - from); - cstr_del(self); *self = out; - return n; + size_t from = 0, pos; char* res; + if (find_len) + while ((res = c_strnstrn(str + from, find, str_len - from, find_len))) { + pos = res - str; + cstr_append_n(&out, str + from, pos - from); + cstr_append_n(&out, repl, repl_len); + from = pos + find_len; + } + cstr_append_n(&out, str + from, str_len - from); + return out; +} + +STC_DEF void +cstr_replace_all(cstr* self, const char* find, const char* repl) { + cstr_take(self, cstr_from_replace_all(self->str, _cstr_rep(self)->size, + find, strlen(find), repl, strlen(repl))); } STC_DEF void diff --git a/include/stc/csview.h b/include/stc/csview.h index e39535a9..c631a0b7 100644 --- a/include/stc/csview.h +++ b/include/stc/csview.h @@ -80,10 +80,11 @@ STC_INLINE void csview_next(csview_iter_t* it) { ++it->ref; } /* cstr interaction with csview: */ -STC_API cstr cstr_from_replace_all(csview sv, csview find, csview replace); - STC_INLINE cstr cstr_from_v(csview sv) { return cstr_from_n(sv.str, sv.size); } +STC_INLINE cstr cstr_from_replace_all_v(csview sv, csview find, csview repl) + { return cstr_from_replace_all(sv.str, sv.size, find.str, find.size, + repl.str, repl.size); } STC_INLINE csview cstr_to_v(const cstr* self) { return c_make(csview){self->str, _cstr_rep(self)->size}; } STC_INLINE csview cstr_substr(cstr s, intptr_t pos, size_t n) @@ -98,8 +99,6 @@ STC_INLINE void cstr_insert_v(cstr* self, size_t pos, csview sv) { cstr_replace_n(self, pos, 0, sv.str, sv.size); } STC_INLINE void cstr_replace_v(cstr* self, size_t pos, size_t len, csview sv) { cstr_replace_n(self, pos, len, sv.str, sv.size); } -STC_INLINE void cstr_replace_all_v(cstr* self, csview find, csview replace) - { cstr_take(self, cstr_from_replace_all(cstr_sv(*self), find, replace)); } STC_INLINE bool cstr_equals_v(cstr s, csview sv) { return sv.size == cstr_size(s) && !memcmp(s.str, sv.str, sv.size); } STC_INLINE size_t cstr_find_v(cstr s, csview needle) @@ -114,57 +113,12 @@ STC_INLINE bool cstr_ends_with_v(cstr s, csview sub) { if (sub.size > cstr_size(s)) return false; return !memcmp(s.str + cstr_size(s) - sub.size, sub.str, sub.size); } -/* ---- Adaptor functions ---- */ +/* ---- Container helper functions ---- */ -#define csview_compare_ref(xp, yp) strcmp((xp)->str, (yp)->str) STC_INLINE bool csview_equals_ref(const csview* a, const csview* b) { return a->size == b->size && !memcmp(a->str, b->str, a->size); } -#define csview_hash_ref(xp, none) c_default_hash((xp)->str, (xp)->size) - -/* ---- Associative cstr-containers with csview emplace/lookup API ---- */ - -#define using_csmap_strvkey(...) c_MACRO_OVERLOAD(using_csmap_strvkey, __VA_ARGS__) - -#define using_csmap_strvkey_2(X, Mapped) \ - using_csmap_strvkey_4(X, Mapped, c_default_del, c_default_fromraw) -#define using_csmap_strvkey_3(X, Mapped, mappedDel) \ - using_csmap_strvkey_4(X, Mapped, mappedDel, c_no_clone) -#define using_csmap_strvkey_4(X, Mapped, mappedDel, mappedClone) \ - using_csmap_strvkey_7(X, Mapped, mappedDel, mappedClone, c_default_toraw, Mapped, c_true) -#define using_csmap_strvkey_7(X, Mapped, mappedDel, mappedFromRaw, mappedToRaw, RawMapped, defTypes) \ - _c_using_aatree(csmap_##X, csmap_, cstr, Mapped, csview_compare_ref, \ - mappedDel, mappedFromRaw, mappedToRaw, RawMapped, defTypes, \ - cstr_del, cstr_from_v, cstr_to_v, csview) - -#define using_csmap_strv() \ - _c_using_aatree(csmap_strv, csmap_, cstr, cstr, csview_compare_ref, \ - cstr_del, cstr_from_v, cstr_to_v, csview, c_true, \ - cstr_del, cstr_from_v, cstr_to_v, csview) -#define using_csset_strv() \ - _c_using_aatree(csset_strv, csset_, cstr, cstr, csview_compare_ref, \ - @@, @@, @@, void, c_true, cstr_del, cstr_from_v, cstr_to_v, csview) - - -#define using_cmap_strvkey(...) c_MACRO_OVERLOAD(using_cmap_strvkey, __VA_ARGS__) - -#define using_cmap_strvkey_2(X, Mapped) \ - using_cmap_strvkey_4(X, Mapped, c_default_del, c_default_fromraw) -#define using_cmap_strvkey_3(X, Mapped, mappedDel) \ - using_cmap_strvkey_4(X, Mapped, mappedDel, c_no_clone) -#define using_cmap_strvkey_4(X, Mapped, mappedDel, mappedClone) \ - using_cmap_strvkey_7(X, Mapped, mappedDel, mappedClone, c_default_toraw, Mapped, c_true) -#define using_cmap_strvkey_7(X, Mapped, mappedDel, mappedFromRaw, mappedToRaw, RawMapped, defTypes) \ - _c_using_chash(cmap_##X, cmap_, cstr, Mapped, csview_equals_ref, csview_hash_ref, \ - mappedDel, mappedFromRaw, mappedToRaw, RawMapped, defTypes, \ - cstr_del, cstr_from_v, cstr_to_v, csview) - -#define using_cmap_strv() \ - _c_using_chash(cmap_strv, cmap_, cstr, cstr, csview_equals_ref, csview_hash_ref, \ - cstr_del, cstr_from_v, cstr_to_v, csview, c_true, \ - cstr_del, cstr_from_v, cstr_to_v, csview) -#define using_cset_strv() \ - _c_using_chash(cset_strv, cset_, cstr, cstr, csview_equals_ref, csview_hash_ref, \ - @@, @@, @@, void, c_true, cstr_del, cstr_from_v, cstr_to_v, csview) +#define csview_hash_ref(xp, none) c_default_hash((xp)->str, (xp)->size) +#define csview_compare_ref(xp, yp) strcmp((xp)->str, (yp)->str) /* -------------------------- IMPLEMENTATION ------------------------- */ @@ -201,20 +155,5 @@ csview_next_token(csview sv, csview sep, csview tok) { return tok; } -STC_DEF cstr -cstr_from_replace_all(csview sv, csview find, csview replace) { - cstr out = cstr_null; - size_t from = 0, pos; char* res; - if (find.size) - while ((res = c_strnstrn(sv.str + from, find.str, sv.size - from, find.size))) { - pos = res - sv.str; - cstr_append_n(&out, sv.str + from, pos - from ); - cstr_append_v(&out, replace); - from = pos + find.size; - } - cstr_append_n(&out, sv.str + from, sv.size - from); - return out; -} - #endif #endif \ No newline at end of file -- cgit v1.2.3