diff options
| author | Tyge Løvset <[email protected]> | 2022-07-31 09:15:36 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2022-07-31 10:32:12 +0200 |
| commit | 8a5478f7c14d4c7476d36b137fd7d2c8d95f2c13 (patch) | |
| tree | e54045852e198dfbecbd82a55a854698658c4a6b /examples/splitstr.c | |
| parent | 90b4371d8a36ab609c2200289634c171a19f7fff (diff) | |
| download | STC-modified-8a5478f7c14d4c7476d36b137fd7d2c8d95f2c13.tar.gz STC-modified-8a5478f7c14d4c7476d36b137fd7d2c8d95f2c13.zip | |
csview: csview_token(sv, sep, &start): now start > sv.size on last token. Removed usage of c_ARGsv() macro in examples => printf formatting ".*s". csview fully inlined.
Diffstat (limited to 'examples/splitstr.c')
| -rw-r--r-- | examples/splitstr.c | 32 |
1 files changed, 17 insertions, 15 deletions
diff --git a/examples/splitstr.c b/examples/splitstr.c index 942d4eec..68c36291 100644 --- a/examples/splitstr.c +++ b/examples/splitstr.c @@ -1,27 +1,29 @@ -#include <stc/cstr.h> +#include <stdio.h> #include <stc/csview.h> -#define i_val_str -#include <stc/cvec.h> -void print_split(csview str, csview sep) +void print_split(csview input, csview sep) { size_t pos = 0; - while (pos != str.size) { - csview tok = csview_token(str, sep, &pos); + while (pos <= input.size) { + csview tok = csview_token(input, sep, &pos); // print non-null-terminated csview - printf("[%" c_PRIsv "]\n", c_ARGsv(tok)); + printf("[%.*s]\n", c_ARGsv(tok)); } } -cvec_str string_split(csview str, csview sep) +#include <stc/cstr.h> +#define i_val_str +#include <stc/cstack.h> + +cstack_str string_split(csview input, csview sep) { - cvec_str vec = cvec_str_init(); + cstack_str out = cstack_str_init(); size_t pos = 0; - while (pos != str.size) { - csview tok = csview_token(str, sep, &pos); - cvec_str_push_back(&vec, cstr_from_sv(tok)); + while (pos <= input.size) { + csview tok = csview_token(input, sep, &pos); + cstack_str_push(&out, cstr_from_sv(tok)); } - return vec; + return out; } int main() @@ -31,7 +33,7 @@ int main() print_split(c_sv("This has no matching separator"), c_sv("xx")); puts(""); - c_autovar (cvec_str v = string_split(c_sv("Split,this,,string,now,"), c_sv(",")), cvec_str_drop(&v)) - c_foreach (i, cvec_str, v) + c_autovar (cstack_str s = string_split(c_sv("Split,this,,string,now,"), c_sv(",")), cstack_str_drop(&s)) + c_foreach (i, cstack_str, s) printf("[%s]\n", cstr_str(i.ref)); } |
