summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2021-06-18 23:30:14 +0200
committerTyge Løvset <[email protected]>2021-06-18 23:30:14 +0200
commit3776efeb26088cde334eda8e8dcda66431a9a0cf (patch)
tree3cb3eaa544b05bc3cbbb006d76504970777570e9
parentde216de5b32da73ce835d2a3d03b4dd44bfeaf6d (diff)
downloadSTC-modified-3776efeb26088cde334eda8e8dcda66431a9a0cf.tar.gz
STC-modified-3776efeb26088cde334eda8e8dcda66431a9a0cf.zip
Simplified and removed unneeded stuff from csview.h
-rw-r--r--docs/cstr_api.md2
-rw-r--r--docs/csview_api.md3
-rw-r--r--examples/svmap.c38
-rw-r--r--include/stc/cstr.h40
-rw-r--r--include/stc/csview.h73
5 files changed, 32 insertions, 124 deletions
diff --git a/docs/cstr_api.md b/docs/cstr_api.md
index cdb73a69..c26bb868 100644
--- a/docs/cstr_api.md
+++ b/docs/cstr_api.md
@@ -53,7 +53,7 @@ void cstr_insert_n(cstr* self, size_t pos, const char* str, size_t n);
void cstr_replace(cstr* self, size_t pos, size_t len, const char* str);
void cstr_replace_s(cstr* self, size_t pos, size_t len, cstr s);
void cstr_replace_n(cstr* self, size_t pos, size_t len, const char* str, size_t n);
-size_t cstr_replace_all(cstr* self, const char* find, const char* replace);
+void cstr_replace_all(cstr* self, const char* find, const char* replace);
void cstr_erase(cstr* self, size_t pos);
void cstr_erase_n(cstr* self, size_t pos, size_t n);
diff --git a/docs/csview_api.md b/docs/csview_api.md
index bd4945c0..2906acdc 100644
--- a/docs/csview_api.md
+++ b/docs/csview_api.md
@@ -58,7 +58,7 @@ void csview_next(csview_iter_t* it);
#### Extended cstr methods
```c
cstr cstr_from_v(csview sv); // construct cstr from csview
-cstr cstr_from_replace_all(csview sv, csview find, csview replace);
+cstr cstr_from_replace_all_v(csview sv, csview find, csview replace);
csview cstr_sv(cstr s); // convert to csview from cstr
csview cstr_to_v(const cstr* self); // convert to csview from cstr*
@@ -69,7 +69,6 @@ cstr* cstr_assign_v(cstr* self, csview sv);
cstr* cstr_append_v(cstr* self, csview sv);
void cstr_insert_v(cstr* self, size_t pos, csview sv);
void cstr_replace_v(cstr* self, size_t pos, size_t len, csview sv);
-void cstr_replace_all_v(cstr* self, csview find, csview replace);
bool cstr_equals_v(cstr s, csview sv);
size_t cstr_find_v(cstr s, csview needle);
diff --git a/examples/svmap.c b/examples/svmap.c
deleted file mode 100644
index d0f84ce4..00000000
--- a/examples/svmap.c
+++ /dev/null
@@ -1,38 +0,0 @@
-#include <stc/csview.h>
-#include <stc/cmap.h>
-#include <stdio.h>
-
-// cmap<cstr, int> with csview as convertion key-type (raw):
-using_cmap_strvkey(si, int);
-
-int main() {
- csview fox = c_sv("The quick brown fox jumps over the lazy dog.");
- printf("\"%s\", length=%zu\n", fox.str, fox.size);
-
- c_forvar_initdel (cmap_si, frequencies)
- {
- // Non-emplace: cstr element API
- cmap_si_insert(&frequencies, cstr_lit("thousand"), 1000);
- cmap_si_insert_or_assign(&frequencies, cstr_lit("thousand"), 2000);
- cmap_si_insert(&frequencies, cstr_lit("thousand"), 3000); // ignored
-
- // Emplace: csview element API
- const char* key = "hundred";
- cmap_si_emplace(&frequencies, c_sv("hundred"), 300); // c_sv() shorthand for csview_lit()
- cmap_si_emplace(&frequencies, csview_from_n(key, 4), 400); // insert "hund"
- cmap_si_emplace_or_assign(&frequencies, csview_from(key), 500); // update
- cmap_si_emplace(&frequencies, c_sv("hundred"), 600); // ignored, already inserted
-
- // Lookup always uses "raw" type API, i.e. csview here.
- printf("at(\"hundred\"): %d\n", *cmap_si_at(&frequencies, c_sv("hundred")));
-
- c_foreach (i, cmap_si, frequencies)
- printf("%s: %d\n", i.ref->first.str, i.ref->second);
- }
- /* Output: */
- // "The quick brown fox jumps over the lazy dog.", length=44
- // at("hundred"): 500
- // hund: 400
- // hundred: 500
- // thousand: 2000
-} \ No newline at end of file
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