diff options
| author | Tyge Løvset <[email protected]> | 2022-09-06 18:20:23 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2022-09-06 18:20:23 +0200 |
| commit | 90311ff7eb34e5fc4fc1c2c38b8d0433642e9659 (patch) | |
| tree | 11352fe3b7530ed1c6b49fa1914ccef7142c92b4 /examples/splitstr.c | |
| parent | 2857a84df612d2e1ae4389e3a747a39d933c0400 (diff) | |
| download | STC-modified-90311ff7eb34e5fc4fc1c2c38b8d0433642e9659.tar.gz STC-modified-90311ff7eb34e5fc4fc1c2c38b8d0433642e9659.zip | |
- Added c_foreach_token(it, ...) macro in csview.h.
- Changed c_foreach_match(it, ...): to access matches, use it.match[j]
- splitstr.c now shows usages of both the above.
Diffstat (limited to 'examples/splitstr.c')
| -rw-r--r-- | examples/splitstr.c | 44 |
1 files changed, 14 insertions, 30 deletions
diff --git a/examples/splitstr.c b/examples/splitstr.c index c483fbe0..f2a1dad1 100644 --- a/examples/splitstr.c +++ b/examples/splitstr.c @@ -1,39 +1,23 @@ #include <stdio.h> +#define i_implement // cstr functions +#include <stc/cstr.h> #include <stc/csview.h> +#include <stc/cregex.h> -void print_split(csview input, csview sep) +int main() { - size_t pos = 0; - while (pos <= input.size) { - csview tok = csview_token(input, sep, &pos); - // print non-null-terminated csview - printf("[%.*s]\n", c_ARGsv(tok)); - } -} + puts("Split with c_foreach_token (csview):"); -#include <stc/cstr.h> -#define i_val_str -#include <stc/cstack.h> + c_foreach_token (i, "Hello World C99!", " ") + printf("'%.*s'\n", c_ARGsv(i.token)); -cstack_str string_split(csview input, csview sep) -{ - cstack_str out = cstack_str_init(); - size_t pos = 0; - while (pos <= input.size) { - csview tok = csview_token(input, sep, &pos); - cstack_str_push(&out, cstr_from_sv(tok)); - } - return out; -} -int main() -{ - 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("\nSplit with c_foreach_match (regex):"); - c_with (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)); + c_with (cregex re = cregex_from("[^ ]+", 0), cregex_drop(&re)) + c_foreach_match (i, &re, " Hello World C99! ") + printf("'%.*s'\n", c_ARGsv(i.match[0])); } + +#include "../src/cregex.c" +#include "../src/utf8code.c" |
