From b8f24b90481d83d06ae89e2594c249c9741dae21 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Fri, 11 Jun 2021 16:38:06 +0200 Subject: Improve naming of c_sv() and c_lit() in csview.h: Rename c_sv(cstr) => cstr_sv(cstr), and c_lit(string literal) => c_sv(string literal). Hopefully not too much used yet, as they are fairly new. --- docs/csview_api.md | 40 +++++++++++++++++++++------------------- examples/splitstr.c | 6 +++--- examples/svmap.c | 8 ++++---- include/stc/csview.h | 5 +++-- 4 files changed, 31 insertions(+), 28 deletions(-) diff --git a/docs/csview_api.md b/docs/csview_api.md index 2a4260f7..ba2697bd 100644 --- a/docs/csview_api.md +++ b/docs/csview_api.md @@ -25,12 +25,12 @@ All csview definitions and prototypes are available by including a single header ## Methods ```c -csview c_lit(const char literal_only[]); // csview from literal, no strlen() -csview c_sv(cstr s); // construct csview from cstr -csview csview_from(const char* str); // construct from (const char*) +csview csview_from(const char* str); // make csview from const char* csview csview_from_n(const char* str, size_t n); // construct -csview csview_from_s(cstr s); // same as c_sv() -csview csview_lit(const char literal_only[]); // same as c_lit() +csview csview_from_s(cstr s); // same as cstr_sv() + +csview csview_lit(const char literal_only[]); // make csview from literal, no strlen() +csview c_sv(const char literal_only[]); // same as csview_lit() size_t csview_size(csview sv); size_t csview_length(csview sv); @@ -57,10 +57,13 @@ void csview_next(csview_iter_t* it); ``` #### Extended cstr methods ```c -cstr cstr_from_v(csview sv); -csview cstr_to_v(const cstr* self); -csview cstr_substr(cstr s, intptr_t pos, size_t n); // negative pos count from end -csview cstr_slice(cstr s, intptr_t p1, intptr_t p2); // negative p1, p2 count from end +cstr cstr_from_v(csview sv); // construct cstr from csview + +csview cstr_sv(cstr s); // convert to csview from cstr +csview cstr_to_v(const cstr* self); // convert to csview from cstr* +csview cstr_substr(cstr s, intptr_t pos, size_t n); // negative pos counts from end +csview cstr_slice(cstr s, intptr_t p1, intptr_t p2); // negative p1, p2 counts from end + 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); @@ -90,9 +93,8 @@ uint64_t csview_hash_ref(const csview* x, size_t ignored); | Name | Value | Usage | |:-----------------|:--------------------|:----------------------------------| -| `csview_null` | same as `c_lit("")` | `sview = csview_null;` | +| `csview_null` | same as `c_sv("")` | `sview = csview_null;` | | `csview_npos` | same as `cstr_npos` | | -| `c_lit(literal)` | csview constructor | `sview = c_lit("hello, world");` | | `csview_ARG(sv)` | printf argument | `printf("%.*s", csview_ARG(sv));` | ## Associative cstr-containers with csview emplace/lookup API @@ -175,12 +177,12 @@ cvec_str string_split(csview str, csview sep) int main() { - print_split(c_lit("//This is a//double-slash//separated//string"), c_lit("//")); + print_split(c_sv("//This is a//double-slash//separated//string"), c_sv("//")); puts(""); - print_split(c_lit("This has no matching separator"), c_lit("xx")); + print_split(c_sv("This has no matching separator"), c_sv("xx")); puts(""); - c_forvar (cvec_str v = string_split(c_lit("Split,this,,string,now,"), c_lit(",")), cvec_str_del(&v)) + c_forvar (cvec_str v = string_split(c_sv("Split,this,,string,now,"), c_sv(",")), cvec_str_del(&v)) c_foreach (i, cvec_str, v) printf("\"%s\"\n", i.ref->str); } @@ -213,18 +215,18 @@ using_cmap_strvkey(si, int); int main() { - csview text = c_lit("The length of this literal is evaluated at compile time and stored in csview text."); + csview text = c_sv("The length of this literal is evaluated at compile time and stored in csview text."); csview suffix = csview_substr(text, -12, cstr_npos); // from pos -12 to end printf("%.*s\n", csview_ARG(suffix)); c_forvar_initdel (cmap_si, map) { - cmap_si_emplace(&map, c_lit("hello"), 100); - cmap_si_emplace(&map, c_lit("world"), 200); - cmap_si_emplace(&map, c_lit("hello"), 300); // already in map, ignored + cmap_si_emplace(&map, c_sv("hello"), 100); + cmap_si_emplace(&map, c_sv("world"), 200); + cmap_si_emplace(&map, c_sv("hello"), 300); // already in map, ignored // Efficient lookup: no string allocation or strlen() takes place: - cmap_si_value_t* val = cmap_si_get(&map, c_lit("hello")); + cmap_si_value_t* val = cmap_si_get(&map, c_sv("hello")); printf("%s: %d\n", val->first.str, val->second); } } diff --git a/examples/splitstr.c b/examples/splitstr.c index b6c703b4..54bbe92d 100644 --- a/examples/splitstr.c +++ b/examples/splitstr.c @@ -29,12 +29,12 @@ cvec_str string_split(csview str, csview sep) int main() { puts("Output from print_split():"); - print_split(c_lit("//This is a//double-slash//separated//string"), c_lit("//")); puts(""); - print_split(c_lit("This has no matching separator"), c_lit("xx")); puts(""); + print_split(c_sv("//This is a//double-slash//separated//string"), c_sv("//")); puts(""); + print_split(c_sv("This has no matching separator"), c_sv("xx")); puts(""); puts("Output from string_split():"); cstr string = cstr_lit("Split,this,,string,now,"); - cvec_str vec = string_split(c_sv(string), c_lit(",")); + cvec_str vec = string_split(cstr_sv(string), c_sv(",")); c_fordefer (cvec_str_del(&vec), cstr_del(&string)) c_foreach (i, cvec_str, vec) diff --git a/examples/svmap.c b/examples/svmap.c index 769913df..d0f84ce4 100644 --- a/examples/svmap.c +++ b/examples/svmap.c @@ -6,7 +6,7 @@ using_cmap_strvkey(si, int); int main() { - csview fox = c_lit("The quick brown fox jumps over the lazy dog."); + 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) @@ -18,13 +18,13 @@ int main() { // Emplace: csview element API const char* key = "hundred"; - cmap_si_emplace(&frequencies, c_lit("hundred"), 300); // c_lit() shorthand for csview_lit() + 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_lit("hundred"), 600); // ignored, already inserted + 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_lit("hundred"))); + 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); diff --git a/include/stc/csview.h b/include/stc/csview.h index eb787a72..04133437 100644 --- a/include/stc/csview.h +++ b/include/stc/csview.h @@ -28,12 +28,13 @@ typedef struct { const char* str; size_t size; } csview; typedef struct { const char *ref; } csview_iter_t; typedef char csview_value_t; + #define csview_null c_make(csview){"", 0} #define csview_npos cstr_npos #define csview_ARG(sv) (int)(sv).size, (sv).str -#define c_lit(literal) csview_lit(literal) -#define c_sv(s) csview_from_s(s) +#define c_sv(literal) csview_lit(literal) +#define cstr_sv(s) csview_from_s(s) #define csview_lit(literal) \ c_make(csview){literal, sizeof c_make(strlit_t){literal} - 1} -- cgit v1.2.3