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 | |
| 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')
| -rw-r--r-- | examples/regex2.c | 4 | ||||
| -rw-r--r-- | examples/regex_match.c | 2 | ||||
| -rw-r--r-- | examples/splitstr.c | 44 | ||||
| -rw-r--r-- | examples/utf8replace_c.c | 3 |
4 files changed, 18 insertions, 35 deletions
diff --git a/examples/regex2.c b/examples/regex2.c index 30602444..f0869d24 100644 --- a/examples/regex2.c +++ b/examples/regex2.c @@ -14,7 +14,7 @@ int main() {"!((abc|123)+)!", "!123abcabc!"} }; - c_auto (cregex, re) + c_with (cregex re = cregex_init(), cregex_drop(&re)) c_forrange (i, c_arraylen(s)) { int res = cregex_compile(&re, s[i].pattern, 0); @@ -26,7 +26,7 @@ int main() c_foreach_match (j, &re, s[i].input) { c_forrange (k, cregex_captures(&re)) - printf(" submatch %d: %.*s\n", (int)k, c_ARGsv(j.ref[k])); + printf(" submatch %d: %.*s\n", (int)k, c_ARGsv(j.match[k])); puts(""); } } diff --git a/examples/regex_match.c b/examples/regex_match.c index 1e87affb..1e463104 100644 --- a/examples/regex_match.c +++ b/examples/regex_match.c @@ -22,7 +22,7 @@ int main() // extract and convert all numbers in str to floats c_foreach_match (i, &re, str) - cstack_float_push(&vec, atof(i.ref->str)); + cstack_float_push(&vec, atof(i.match[0].str)); c_foreach (i, cstack_float, vec) printf(" %g\n", *i.ref); 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" diff --git a/examples/utf8replace_c.c b/examples/utf8replace_c.c index e7659cfd..b43aab6f 100644 --- a/examples/utf8replace_c.c +++ b/examples/utf8replace_c.c @@ -8,8 +8,7 @@ int main() { printf("%s\n", cstr_str(&hello)); /* replace second smiley at utf8 codepoint pos 7 */ - cstr_u8_replace(&hello, cstr_find(&hello, "😀rld"), 1, c_sv("🐨")); - + cstr_u8_replace(&hello, cstr_u8_to_pos(&hello, 7), 1, c_sv("🐨")); printf("%s\n", cstr_str(&hello)); cstr_replace(&hello, "🐨", "ø", 1); |
