diff options
| author | Tyge Løvset <[email protected]> | 2022-07-22 15:05:25 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2022-07-22 15:05:25 +0200 |
| commit | 29d9d1d96d8a37f6d7e24dc170aa08a40f0f1559 (patch) | |
| tree | b239018af9c0dfa1b07edaf835663757dd4759ed /examples/regex_replace.c | |
| parent | 28ee78e128c14fe309cb5f7cfc3f2172bf675ea7 (diff) | |
| download | STC-modified-29d9d1d96d8a37f6d7e24dc170aa08a40f0f1559.tar.gz STC-modified-29d9d1d96d8a37f6d7e24dc170aa08a40f0f1559.zip | |
FINAL cregex update for now: optimize/change callback mfun API. Also, cstr_printf() cannot take self as print argument.
Diffstat (limited to 'examples/regex_replace.c')
| -rw-r--r-- | examples/regex_replace.c | 27 |
1 files changed, 14 insertions, 13 deletions
diff --git a/examples/regex_replace.c b/examples/regex_replace.c index 0b6c9a1e..2bb9517f 100644 --- a/examples/regex_replace.c +++ b/examples/regex_replace.c @@ -2,13 +2,14 @@ #include <stc/cstr.h> #include <stc/cregex.h> -cstr sub_20y(int i, csview m) { - if (i == 1) { // year +bool add_10_years(int i, csview m, cstr* mstr) { + if (i == 1) { // group 1 matches year int year; sscanf(m.str, "%4d", &year); - return cstr_from_fmt("%04d", year - 20); + cstr_printf(mstr, "%04d", year + 10); + return true; } - return cstr_from_sv(m); + return false; } int main() @@ -18,31 +19,31 @@ int main() c_auto (cstr, str) { - printf("input: %s\n\n", input); + printf("INPUT: %s\n", input); /* replace with a fixed string, extended all-in-one call: */ cstr_take(&str, cregex_replace_p(input, pattern, "YYYY-MM-DD")); printf("fixed: %s\n", cstr_str(&str)); - /* US date format, and subtract 20 years: */ - cstr_take(&str, cregex_replace_pe(input, pattern, "\\1/\\3/\\2", sub_20y, 0, 0)); - printf("us-20: %s\n", cstr_str(&str)); + /* US date format, and add 10 years to dates: */ + cstr_take(&str, cregex_replace_pe(input, pattern, "\\1/\\3/\\2", add_10_years, 0, 0)); + printf("us+10: %s\n", cstr_str(&str)); /* Wrap first date inside []: */ cstr_take(&str, cregex_replace_pe(input, pattern, "[\\0]", NULL, 1, 0)); printf("brack: %s\n", cstr_str(&str)); - /* Wrap all words in {} */ - cstr_take(&str, cregex_replace_p("[52] apples and [31] mangoes", "[a-z]+", "{\\0}")); - printf("curly: %s\n", cstr_str(&str)); - - /* European date format, compile RE separately */ + /* European date format. Show how to compile RE separately */ cregex re = cregex_from(pattern, 0); if (cregex_captures(&re) == 0) continue; cstr_take(&str, cregex_replace(input, &re, "\\3.\\2.\\1", NULL, 0)); cregex_drop(&re); printf("euros: %s\n", cstr_str(&str)); + + /* Wrap all words in {} */ + cstr_take(&str, cregex_replace_p("[52] apples and [31] mangoes", "[a-z]+", "{\\0}")); + printf("curly: %s\n", cstr_str(&str)); } } |
