diff options
| author | Tyge Lovset <[email protected]> | 2022-07-22 00:34:19 +0200 |
|---|---|---|
| committer | Tyge Lovset <[email protected]> | 2022-07-22 00:34:19 +0200 |
| commit | 698e70e7d3e7dcdf6b6e7b03689dabc184d34bf6 (patch) | |
| tree | 21e7619432ded4b4db2b0dc06cfc88c565a038e7 | |
| parent | f1bc406edb6faef3420de7f77a6f1246065861d9 (diff) | |
| download | STC-modified-698e70e7d3e7dcdf6b6e7b03689dabc184d34bf6.tar.gz STC-modified-698e70e7d3e7dcdf6b6e7b03689dabc184d34bf6.zip | |
Changed / improved cregex API again (as promised). Possible final API.
| -rw-r--r-- | examples/regex1.c | 2 | ||||
| -rw-r--r-- | examples/regex2.c | 2 | ||||
| -rw-r--r-- | examples/regex_match.c | 8 | ||||
| -rw-r--r-- | examples/regex_replace.c | 10 | ||||
| -rw-r--r-- | include/stc/cregex.h | 38 | ||||
| -rw-r--r-- | src/cregex.c | 39 |
6 files changed, 54 insertions, 45 deletions
diff --git a/examples/regex1.c b/examples/regex1.c index d5c14509..98fc644d 100644 --- a/examples/regex1.c +++ b/examples/regex1.c @@ -22,7 +22,7 @@ int main(int argc, char* argv[]) if (cstr_equals(input, "q")) break; - if (cregex_match_re(cstr_str(&input), &float_expr, 0, NULL, 0) > 0) + if (cregex_match(cstr_str(&input), &float_expr, NULL, 0) == 1) printf("Input is a float\n"); else printf("Invalid input : Not a float\n"); diff --git a/examples/regex2.c b/examples/regex2.c index 0d10205a..7c17e663 100644 --- a/examples/regex2.c +++ b/examples/regex2.c @@ -20,7 +20,7 @@ int main() } cregmatch m[20]; printf("input: %s\n", inputs[i]); - if (cregex_match_re(inputs[i], &re, 20, m, 0) > 0) + if (cregex_match(inputs[i], &re, m, 0) == 1) { c_forrange (j, cregex_captures(&re)) { diff --git a/examples/regex_match.c b/examples/regex_match.c index 5f1075ff..646f2318 100644 --- a/examples/regex_match.c +++ b/examples/regex_match.c @@ -13,21 +13,21 @@ int main() { int res = cregex_compile(&re, "[+-]?([0-9]*\\.)?\\d+([Ee][+-]?\\d+)?", 0); printf("%d\n", res); - csview m[10]; - if (cregex_match_re(s, &re, 10, m, 0) > 0) { + csview m[5]; + if (cregex_match(s, &re, m, 0) == 1) { printf("Found digits at position %" PRIuMAX "-%" PRIuMAX "\n", m[0].str - s, m[0].str - s + m[0].size); } else { printf("Could not find any digits\n"); } - while (cregex_match_re(s, &re, 10, m, cre_NEXT) > 0) { + while (cregex_match(s, &re, m, cre_NEXT) == 1) { printf("%" c_PRIsv " ; ", c_ARGsv(m[0])); } puts(""); res = cregex_compile(&re, "(.+)\\b(.+)", 0); printf("groups: %d\n", res); - if ((res = cregex_match_re("hello@wørld", &re, 10, m, 0)) > 0) { + if ((res = cregex_match("hello@wørld", &re, m, 0)) == 1) { c_forrange (i, res) printf("match: [%" c_PRIsv "]\n", c_ARGsv(m[i])); } else diff --git a/examples/regex_replace.c b/examples/regex_replace.c index ff4b4a6a..d57dede8 100644 --- a/examples/regex_replace.c +++ b/examples/regex_replace.c @@ -21,23 +21,23 @@ int main() { printf("input: %s\n", input); /* European date format */ - cstr_take(&str, cregex_replace(input, pattern, "\\3.\\2.\\1")); + cstr_take(&str, cregex_replace_pat(input, pattern, "\\3.\\2.\\1")); printf("euros: %s\n", cstr_str(&str)); /* US date format, and subtract 20 years: */ - cstr_take(&str, cregex_replace_ex(input, pattern, "\\1/\\3/\\2", sub_20y, 0, 0)); + cstr_take(&str, cregex_replace_patx(input, pattern, "\\1/\\3/\\2", sub_20y, 0, 0)); printf("us-20: %s\n", cstr_str(&str)); /* replace with a fixed string: */ - cstr_take(&str, cregex_replace(input, pattern, "YYYY-MM-DD")); + cstr_take(&str, cregex_replace_pat(input, pattern, "YYYY-MM-DD")); printf("fixed: %s\n", cstr_str(&str)); /* Wrap first date inside []: */ - cstr_take(&str, cregex_replace_ex(input, pattern, "[\\0]", NULL, 0, 1)); + cstr_take(&str, cregex_replace_patx(input, pattern, "[\\0]", NULL, 1, 0)); printf("brack: %s\n", cstr_str(&str)); /* Wrap all words in {} */ - cstr_take(&str, cregex_replace("[52] apples and [31] mangoes", "[a-z]+", "{\\0}")); + cstr_take(&str, cregex_replace_pat("[52] apples and [31] mangoes", "[a-z]+", "{\\0}")); printf("curly: %s\n", cstr_str(&str)); } } diff --git a/include/stc/cregex.h b/include/stc/cregex.h index 4e82c60a..e806dc06 100644 --- a/include/stc/cregex.h +++ b/include/stc/cregex.h @@ -69,35 +69,43 @@ typedef struct { typedef csview cregmatch; -static inline cregex cregex_init(void) { - cregex rx = {NULL}; return rx; +static inline +cregex cregex_init(void) { + cregex rx = {0}; + return rx; } /* return number of capture groups on success, or (negative) error code on failure. */ int cregex_compile(cregex *self, const char* pattern, int cflags); +static inline +cregex cregex_from(const char* pattern, int cflags, int* res) { + cregex rx = {0}; + int ret = cregex_compile(&rx, pattern, cflags); + if (res) *res = ret; + return rx; +} + /* number of capture groups in a regex pattern */ int cregex_captures(const cregex* self); /* return 1 on match, 0 on nomatch, and -1 on failure. */ -int cregex_match_re(const char* input, const cregex* re, - unsigned nmatch, csview match[], int mflags); +int cregex_match(const char* input, const cregex* re, + csview match[], int mflags); -int cregex_match_ex(const char* input, const char* pattern, int cflags, - unsigned nmatch, csview match[], int mflags); -static inline -int cregex_match(const char* input, const char* pattern, unsigned nmatch, csview match[]) - { return cregex_match_ex(input, pattern, 0, nmatch, match, 0); } +int cregex_match_pat(const char* input, const char* pattern, + csview match[], int cmflags); /* replace regular expression */ -cstr cregex_replace_re(const char* input, const cregex* re, const char* replace, - cstr (*mfun)(int i, csview match), int cflags, unsigned count); +cstr cregex_replace(const char* input, const cregex* re, const char* replace, + cstr (*mfun)(int i, csview match), unsigned count); -cstr cregex_replace_ex(const char* input, const char* pattern, const char* replace, - cstr (*mfun)(int i, csview match), int cflags, unsigned count); +cstr cregex_replace_patx(const char* input, const char* pattern, + const char* replace, cstr (*mfun)(int i, csview match), + unsigned count, int cflags); static inline -cstr cregex_replace(const char* input, const char* pattern, const char* replace) - { return cregex_replace_ex(input, pattern, replace, NULL, 0, 0); } +cstr cregex_replace_pat(const char* input, const char* pattern, const char* replace) + { return cregex_replace_patx(input, pattern, replace, NULL, 0, 0); } /* destroy regex */ void cregex_drop(cregex* self); diff --git a/src/cregex.c b/src/cregex.c index 7ae2b9ce..54b13431 100644 --- a/src/cregex.c +++ b/src/cregex.c @@ -1125,11 +1125,11 @@ regexec(const Reprog *progp, /* program to run */ j.starts = bol; j.eol = NULL; - if (ms && mp->size) { + if (mp && mp[0].size) { if (mflags & cre_STARTEND) - j.starts = mp->str, j.eol = mp->str + mp->size; + j.starts = mp[0].str, j.eol = mp[0].str + mp[0].size; else if (mflags & cre_NEXT) - j.starts = mp->str + mp->size; + j.starts = mp[0].str + mp[0].size; } j.starttype = 0; @@ -1156,7 +1156,7 @@ regexec(const Reprog *progp, /* program to run */ } static void -cregex_build_subst(const char* replace, unsigned nmatch, const csview match[], +build_subst_string(const char* replace, unsigned nmatch, const csview match[], cstr (*mfun)(int i, csview match), cstr* subst) { cstr_clear(subst); unsigned len = 0, cap = cstr_capacity(*subst); @@ -1202,7 +1202,7 @@ int cregex_compile(cregex *self, const char* pattern, int cflags) { Parser par; self->prog = regcomp1(self->prog, &par, pattern, cflags); - return self->prog ? 1 + self->prog->nsubids : par.errors; + return self->prog ? cre_success : par.errors; } int @@ -1211,9 +1211,9 @@ cregex_captures(const cregex* self) { } int -cregex_match_re(const char* input, const cregex* re, - unsigned nmatch, csview match[], int mflags) { - int res = regexec(re->prog, input, nmatch, match, mflags); +cregex_match(const char* input, const cregex* re, + csview match[], int mflags) { + int res = regexec(re->prog, input, cregex_captures(re), match, mflags); switch (res) { case 1: return cre_success; case 0: return cre_nomatch; @@ -1221,19 +1221,19 @@ cregex_match_re(const char* input, const cregex* re, } } -int cregex_match_ex(const char* input, const char* pattern, int cflags, - unsigned nmatch, csview match[], int mflags) { +int cregex_match_pat(const char* input, const char* pattern, + csview match[], int cmflags) { cregex re = cregex_init(); - int res = cregex_compile(&re, pattern, cflags); + int res = cregex_compile(&re, pattern, cmflags); if (res < 0) return res; - res = cregex_match_re(input, &re, nmatch, match, mflags); + res = cregex_match(input, &re, match, cmflags); cregex_drop(&re); return res; } cstr -cregex_replace_re(const char* input, const cregex* re, const char* replace, - cstr (*mfun)(int i, csview match), int cflags, unsigned count) { +cregex_replace(const char* input, const cregex* re, const char* replace, + cstr (*mfun)(int i, csview match), unsigned count) { cstr out = cstr_null; cstr subst = cstr_null; size_t from = 0; @@ -1241,8 +1241,8 @@ cregex_replace_re(const char* input, const cregex* re, const char* replace, unsigned nmatch = cregex_captures(re); if (!count) count = ~0; - while (count-- && cregex_match_re(input + from, re, nmatch, match, 0) > 0) { - cregex_build_subst(replace, nmatch, match, mfun, &subst); + while (count-- && cregex_match(input + from, re, match, 0) == 1) { + build_subst_string(replace, nmatch, match, mfun, &subst); const size_t pos = match[0].str - input; cstr_append_n(&out, input + from, pos - from); cstr_append_s(&out, subst); @@ -1254,13 +1254,14 @@ cregex_replace_re(const char* input, const cregex* re, const char* replace, } cstr -cregex_replace_ex(const char* input, const char* pattern, const char* replace, - cstr (*mfun)(int i, csview match), int cflags, unsigned count) { +cregex_replace_patx(const char* input, const char* pattern, + const char* replace, cstr (*mfun)(int i, csview match), + unsigned count, int cflags) { cregex re = cregex_init(); int res = cregex_compile(&re, pattern, cflags); if (res < 0) return cstr_new("[[error: invalid regex pattern]]"); - cstr out = cregex_replace_re(input, &re, replace, mfun, cflags, count); + cstr out = cregex_replace(input, &re, replace, mfun, count); cregex_drop(&re); return out; } |
