From 41ef6709c8371b6240b24b8258c12ba61c52cdcb Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Mon, 24 May 2021 19:43:49 +0200 Subject: Added splitstr.c example. Renamed macro csview_ARG() to csview_arg(): special for printing csviews with "%.*s" format. --- docs/csview_api.md | 6 +++--- examples/splitstr.c | 39 +++++++++++++++++++++++++++++++++++++++ include/stc/csview.h | 2 +- 3 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 examples/splitstr.c diff --git a/docs/csview_api.md b/docs/csview_api.md index 97ae5694..bb612d55 100644 --- a/docs/csview_api.md +++ b/docs/csview_api.md @@ -10,7 +10,7 @@ when passing it around. It is faster when using`csview` as convertion type (raw) containers with cstr keys. E.g. prefer `using_cmap_svkey()` over `using_cmap_strkey()`. Note that a **csview** may not be null-terminated, and should therefore be printed the following way: -`printf("%.*s", csview_ARG(sv))`. +`printf("%.*s", csview_arg(sv))`. See the c++ class [std::basic_string_view](https://en.cppreference.com/w/cpp/string/basic_string_view) for a functional description. @@ -91,7 +91,7 @@ uint64_t csview_hash_ref(const csview* x, size_t ignored); |:-----------------|:--------------------|:----------------------------------| | `csview_null` | same as `c_lit("")` | `sview = csview_null;` | | `c_lit(literal)` | csview constructor | `sview = c_lit("hello, world");` | -| `csview_ARG(sv)` | printf argument | `printf("%.*s", csview_ARG(sv));` | +| `csview_arg(sv)` | printf argument | `printf("%.*s", csview_arg(sv));` | ## cstr-containers with csview emplace/lookup API ``` @@ -160,7 +160,7 @@ void print_split(csview str, csview sep) csview token = csview_first_token(str, sep); for (;;) { // print non-null-terminated csview - printf("\"%.*s\"\n", csview_ARG(token)); + printf("\"%.*s\"\n", csview_arg(token)); if (csview_end(&token).ref == csview_end(&str).ref) break; token = csview_next_token(str, sep, token); } diff --git a/examples/splitstr.c b/examples/splitstr.c new file mode 100644 index 00000000..67499d0f --- /dev/null +++ b/examples/splitstr.c @@ -0,0 +1,39 @@ +#include +#include + +void print_split(csview str, csview sep) +{ + csview token = csview_first_token(str, sep); + for (;;) { + // print non-null-terminated csview + printf("\t\"%.*s\"\n", csview_arg(token)); + if (csview_end(&token).ref == csview_end(&str).ref) break; + token = csview_next_token(str, sep, token); + } +} + +using_cvec_str(); + +cvec_str string_split(csview str, csview sep) +{ + cvec_str vec = cvec_str_init(); + csview token = csview_first_token(str, sep); + for (;;) { + cvec_str_push_back(&vec, cstr_from_v(token)); + if (csview_end(&token).ref == csview_end(&str).ref) break; + token = csview_next_token(str, sep, token); + } + return vec; +} + +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(""); + + puts("Output from string_split():"); + c_forvar (cvec_str v = string_split(c_lit("Split,this,,string,now,"), c_lit(",")), cvec_str_del(&v)) + c_foreach (i, cvec_str, v) + printf("\t\"%s\"\n", i.ref->str); +} \ No newline at end of file diff --git a/include/stc/csview.h b/include/stc/csview.h index 95c6be20..bbb2752f 100644 --- a/include/stc/csview.h +++ b/include/stc/csview.h @@ -29,7 +29,7 @@ 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_ARG(sv) (int)(sv).size, (sv).str +#define csview_arg(sv) (int)(sv).size, (sv).str #define c_lit(literal) csview_lit(literal) #define c_sv(s) csview_from_s(s) -- cgit v1.2.3