diff options
| author | Tyge Løvset <[email protected]> | 2022-08-01 16:43:58 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2022-08-01 16:43:58 +0200 |
| commit | 3038d255c230be1b0605f199a00417658a21a016 (patch) | |
| tree | 164d7423ada8c9589df4c2c98ea46e533374381f | |
| parent | 8884747775e922e20b0646eeb29ce8a2b4a1c7cc (diff) | |
| download | STC-modified-3038d255c230be1b0605f199a00417658a21a016.tar.gz STC-modified-3038d255c230be1b0605f199a00417658a21a016.zip | |
start dev on v4, mainly improving API
| -rw-r--r-- | examples/regex2.c | 13 | ||||
| -rw-r--r-- | examples/regex_replace.c | 12 | ||||
| -rw-r--r-- | include/stc/cregex.h | 21 | ||||
| -rw-r--r-- | src/cregex.c | 10 |
4 files changed, 28 insertions, 28 deletions
diff --git a/examples/regex2.c b/examples/regex2.c index cc9464c3..384b4a36 100644 --- a/examples/regex2.c +++ b/examples/regex2.c @@ -4,7 +4,7 @@ int main() { - const char* inputs[] = {"date: 2024-02-29 leapyear day", "https://en.cppreference.com/w/cpp/regex/regex_search", "!123abcabc!"}; + const char* inputs[] = {"date: 2024-02-29 leapyear day, christmas eve is on 2022-12-24.", "https://en.cppreference.com/w/cpp/regex/regex_search", "!123abcabc!"}; const char* patterns[] = {"(\\d\\d\\d\\d)[-_](1[0-2]|0[1-9])[-_](3[01]|[12][0-9]|0[1-9])", "(https?://|ftp://|www\\.)([0-9A-Za-z@:%_+~#=-]+\\.)+([a-z][a-z][a-z]?)(/[/0-9A-Za-z\\.@:%_+~#=\\?&-]*)?", "!((abc|123)+)!", @@ -18,14 +18,11 @@ int main() printf("error in regex pattern: %d\n", res); continue; } - csview m[20]; printf("input: %s\n", inputs[i]); - if (cregex_find(inputs[i], &re, m, 0) == 1) - { - c_forrange (j, cregex_captures(&re)) - { - printf(" submatch %" PRIuMAX ": %.*s\n", j, c_ARGsv(m[j])); - } + + c_foreach_match (m, re, inputs[i]) { + c_forrange (int, j, cregex_captures(&re)) + printf(" submatch %d: %.*s\n", j, c_ARGsv(m[j])); puts(""); } } diff --git a/examples/regex_replace.c b/examples/regex_replace.c index 35b3c696..ceecf4dd 100644 --- a/examples/regex_replace.c +++ b/examples/regex_replace.c @@ -22,15 +22,15 @@ int main() 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")); + cstr_take(&str, cregex_replace_p(input, pattern, "YYYY-MM-DD", 0)); printf("fixed: %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)); + cstr_take(&str, cregex_replace_pe(input, pattern, "$1/$3/$2", 0, 0, add_10_years)); printf("us+10: %s\n", cstr_str(&str)); /* Wrap first date inside []: */ - cstr_take(&str, cregex_replace_pe(input, pattern, "[$0]", NULL, 1, 0)); + cstr_take(&str, cregex_replace(input, pattern, "[$0]", 1)); printf("brack: %s\n", cstr_str(&str)); /* Shows how to compile RE separately */ @@ -38,16 +38,16 @@ int main() if (cregex_captures(&re) == 0) continue; /* European date format. */ - cstr_take(&str, cregex_replace(input, &re, "$3.$2.$1")); + cstr_take(&str, cregex_replace(input, &re, "$3.$2.$1", 0)); printf("euros: %s\n", cstr_str(&str)); /* Strip out everything but the matches */ - cstr_take(&str, cregex_replace_re(input, &re, "$3.$2.$1;", NULL, 0, cre_r_strip)); + cstr_take(&str, cregex_replace_ex(input, &re, "$3.$2.$1;", 0, cre_r_strip, NULL)); printf("strip: %s\n", cstr_str(&str)); } /* Wrap all words in ${} */ - cstr_take(&str, cregex_replace_p("[52] apples and [31] mangoes", "[a-z]+", "$${$0}")); + cstr_take(&str, cregex_replace_p("[52] apples and [31] mangoes", "[a-z]+", "$${$0}", 0)); printf("curly: %s\n", cstr_str(&str)); } } diff --git a/include/stc/cregex.h b/include/stc/cregex.h index 8f6464d4..3747eaf7 100644 --- a/include/stc/cregex.h +++ b/include/stc/cregex.h @@ -70,7 +70,10 @@ typedef struct { int error; } cregex; -typedef csview cregmatch; +#define c_foreach_match(m, re, input) \ + for (csview m[cre_MAXCAPTURES] = {{0}}; cregex_find(input, &(re), m, cre_m_next) == cre_success;) + +//typedef csview cregmatch; static inline cregex cregex_init(void) { @@ -109,18 +112,18 @@ bool cregex_is_match(const char* input, const cregex* re, int mflags) { return cregex_find(input, re, NULL, mflags) == 1; } /* replace regular expression */ -cstr cregex_replace_re(const char* input, const cregex* re, const char* replace, - bool (*mfun)(int i, csview match, cstr* mstr), unsigned count, int rflags); +cstr cregex_replace_ex(const char* input, const cregex* re, const char* replace, unsigned count, + int rflags, bool (*mfun)(int i, csview match, cstr* mstr)); static inline -cstr cregex_replace(const char* input, const cregex* re, const char* replace) - { return cregex_replace_re(input, re, replace, NULL, 0, 0); } +cstr cregex_replace(const char* input, const cregex* re, const char* replace, unsigned count) + { return cregex_replace_ex(input, re, replace, count, 0, NULL); } /* replace + compile RE pattern, and extra arguments */ -cstr cregex_replace_pe(const char* input, const char* pattern, const char* replace, - bool (*mfun)(int i, csview match, cstr* mstr), unsigned count, int crflags); +cstr cregex_replace_pe(const char* input, const char* pattern, const char* replace, unsigned count, + int crflags, bool (*mfun)(int i, csview match, cstr* mstr)); static inline -cstr cregex_replace_p(const char* input, const char* pattern, const char* replace) - { return cregex_replace_pe(input, pattern, replace, NULL, 0, 0); } +cstr cregex_replace_p(const char* input, const char* pattern, const char* replace, unsigned count) + { return cregex_replace_pe(input, pattern, replace, count, 0, NULL); } /* destroy regex */ void cregex_drop(cregex* self); diff --git a/src/cregex.c b/src/cregex.c index 612a6965..d1f9c133 100644 --- a/src/cregex.c +++ b/src/cregex.c @@ -1229,8 +1229,8 @@ int cregex_find_p(const char* input, const char* pattern, } cstr -cregex_replace_re(const char* input, const cregex* re, const char* replace, - bool (*mfun)(int i, csview match, cstr* mstr), unsigned count, int rflags) { +cregex_replace_ex(const char* input, const cregex* re, const char* replace, unsigned count, + int rflags, bool (*mfun)(int i, csview match, cstr* mstr)) { cstr out = cstr_null; cstr subst = cstr_null; size_t from = 0; @@ -1252,13 +1252,13 @@ cregex_replace_re(const char* input, const cregex* re, const char* replace, } cstr -cregex_replace_pe(const char* input, const char* pattern, const char* replace, - bool (*mfun)(int i, csview match, cstr* mstr), unsigned count, int crflags) { +cregex_replace_pe(const char* input, const char* pattern, const char* replace, unsigned count, + int crflags, bool (*mfun)(int i, csview match, cstr* mstr)) { cregex re = cregex_init(); int res = cregex_compile(&re, pattern, crflags); if (res != cre_success) return cstr_new("[[error: invalid regex pattern]]"); - cstr out = cregex_replace_re(input, &re, replace, mfun, count, crflags); + cstr out = cregex_replace_ex(input, &re, replace, count, crflags, mfun); cregex_drop(&re); return out; } |
