diff options
| -rw-r--r-- | docs/cregex_api.md | 14 | ||||
| -rw-r--r-- | examples/regex1.c | 2 | ||||
| -rw-r--r-- | examples/regex2.c | 2 | ||||
| -rw-r--r-- | examples/regex_match.c | 4 | ||||
| -rw-r--r-- | examples/regex_replace.c | 12 | ||||
| -rw-r--r-- | examples/splitstr.c | 2 | ||||
| -rw-r--r-- | include/stc/cregex.h | 14 | ||||
| -rw-r--r-- | src/cregex.c | 6 |
8 files changed, 29 insertions, 27 deletions
diff --git a/docs/cregex_api.md b/docs/cregex_api.md index 52014afa..feb17a9a 100644 --- a/docs/cregex_api.md +++ b/docs/cregex_api.md @@ -40,9 +40,9 @@ bool cregex_is_match(const cregex* re, const char* input); cstr cregex_replace(const cregex* re, const char* input, const char* replace, unsigned count); cstr cregex_replace_sv(const cregex* re, csview input, const char* replace, unsigned count, - int rflags, bool(*mfun)(int capgrp, csview match, cstr* mstr)); + bool(*mfun)(int capgrp, csview match, cstr* mstr), int rflags); cstr cregex_replace_pattern(const char* pattern, const char* input, const char* replace, unsigned count, - int rflags, bool(*mfun)(int capgrp, csview match, cstr* mstr)); + bool(*mfun)(int capgrp, csview match, cstr* mstr), int rflags); void cregex_drop(cregex* self); // destroy ``` @@ -72,11 +72,11 @@ void cregex_drop(cregex* self); // destroy ### Compiling a regular expression ```c cregex re1 = cregex_init(); -int result = cregex_compile(&re1, "[0-9]+", 0); +int result = cregex_compile(&re1, "[0-9]+", cre_default); if (result < 0) return result; const char* url = "(https?://|ftp://|www\\.)([0-9A-Za-z@:%_+~#=-]+\\.)+([a-z][a-z][a-z]?)(/[/0-9A-Za-z\\.@:%_+~#=\\?&-]*)?"; -cregex re2 = cregex_from(url, 0); +cregex re2 = cregex_from(url, cre_default); if (re2.error) return re2.error; ... cregex_drop(&re2); @@ -94,11 +94,11 @@ int main() { const char* input = "start date is 2023-03-01, and end date is 2025-12-31."; const char* pattern = "\\b(\\d\\d\\d\\d)-(\\d\\d)-(\\d\\d)\\b"; - cregex re = cregex_from(pattern, 0); + cregex re = cregex_from(pattern, cre_default); // Lets find the first date in the string: csview match[4]; // full-match, year, month, date. - if (cregex_find(&re, input, match, 0) == cre_success) + if (cregex_find(&re, input, match, cre_default) == cre_success) printf("Found date: %.*s\n", c_ARGsv(match[0])); else printf("Could not find any date\n"); @@ -115,7 +115,7 @@ int main() { For a single match you may use the all-in-one function: ```c -if (cregex_find_pattern(pattern, input, match, 0)) +if (cregex_find_pattern(pattern, input, match, cre_default)) printf("Found date: %.*s\n", c_ARGsv(match[0])); ``` diff --git a/examples/regex1.c b/examples/regex1.c index 56ba8f1d..d1f061fb 100644 --- a/examples/regex1.c +++ b/examples/regex1.c @@ -11,7 +11,7 @@ int main(int argc, char* argv[]) c_auto (cstr, input) c_auto (cregex, float_expr) { - int res = cregex_compile(&float_expr, "^[+-]?[0-9]+((\\.[0-9]*)?|\\.[0-9]+)$", 0); + int res = cregex_compile(&float_expr, "^[+-]?[0-9]+((\\.[0-9]*)?|\\.[0-9]+)$", cre_default); // Until "q" is given, ask for another number if (res > 0) while (true) { diff --git a/examples/regex2.c b/examples/regex2.c index 013fa557..1f656265 100644 --- a/examples/regex2.c +++ b/examples/regex2.c @@ -17,7 +17,7 @@ int main() c_with (cregex re = cregex_init(), cregex_drop(&re)) c_forrange (i, c_arraylen(s)) { - int res = cregex_compile(&re, s[i].pattern, 0); + int res = cregex_compile(&re, s[i].pattern, cre_default); if (res < 0) { printf("error in regex pattern: %d\n", res); continue; diff --git a/examples/regex_match.c b/examples/regex_match.c index 50fdba9a..da23b3a2 100644 --- a/examples/regex_match.c +++ b/examples/regex_match.c @@ -18,7 +18,7 @@ int main() c_auto (cstr, nums) { const char* pattern = "[+-]?([0-9]*\\.)?\\d+([Ee][+-]?\\d+)?"; - int res = cregex_compile(&re, pattern, 0); + int res = cregex_compile(&re, pattern, cre_default); printf("%d: %s\n", res, pattern); // extract and convert all numbers in str to floats @@ -29,7 +29,7 @@ int main() printf(" %g\n", *i.ref); // extracts the numbers only to a comma separated string. - nums = cregex_replace_sv(&re, csview_from(str), " $0,", 0, cre_r_strip, NULL); + nums = cregex_replace_sv(&re, csview_from(str), " $0,", 0, NULL, cre_r_strip); printf("\n%s\n", cstr_str(&nums)); } } diff --git a/examples/regex_replace.c b/examples/regex_replace.c index 6464198c..e6054d9f 100644 --- a/examples/regex_replace.c +++ b/examples/regex_replace.c @@ -23,19 +23,19 @@ int main() printf("INPUT: %s\n", input); /* replace with a fixed string, extended all-in-one call: */ - cstr_take(&str, cregex_replace_pattern(pattern, input, "YYYY-MM-DD", 0, 0, NULL)); + cstr_take(&str, cregex_replace_pattern(pattern, input, "YYYY-MM-DD", 0, NULL, cre_default)); printf("fixed: %s\n", cstr_str(&str)); /* US date format, and add 10 years to dates: */ - cstr_take(&str, cregex_replace_pattern(pattern, input, "$1/$3/$2", 0, 0, add_10_years)); + cstr_take(&str, cregex_replace_pattern(pattern, input, "$1/$3/$2", 0, add_10_years, cre_default)); printf("us+10: %s\n", cstr_str(&str)); /* Wrap first date inside []: */ - cstr_take(&str, cregex_replace_pattern(pattern, input, "[$0]", 1, 0, NULL)); + cstr_take(&str, cregex_replace_pattern(pattern, input, "[$0]", 1, NULL, cre_default)); printf("brack: %s\n", cstr_str(&str)); /* Shows how to compile RE separately */ - c_with (cregex re = cregex_from(pattern, 0), cregex_drop(&re)) { + c_with (cregex re = cregex_from(pattern, cre_default), cregex_drop(&re)) { if (cregex_captures(&re) == 0) continue; // break c_with /* European date format. */ @@ -43,12 +43,12 @@ int main() printf("euros: %s\n", cstr_str(&str)); /* Strip out everything but the matches */ - cstr_take(&str, cregex_replace_sv(&re, csview_from(input), "$3.$2.$1;", 0, cre_r_strip, NULL)); + cstr_take(&str, cregex_replace_sv(&re, csview_from(input), "$3.$2.$1;", 0, NULL, cre_r_strip)); printf("strip: %s\n", cstr_str(&str)); } /* Wrap all words in ${} */ - cstr_take(&str, cregex_replace_pattern("[a-z]+", "52 apples and 31 mangoes", "$${$0}", 0, 0, NULL)); + cstr_take(&str, cregex_replace_pattern("[a-z]+", "52 apples and 31 mangoes", "$${$0}", 0, NULL, cre_default)); printf("curly: %s\n", cstr_str(&str)); } } diff --git a/examples/splitstr.c b/examples/splitstr.c index 8e976892..27dd308c 100644 --- a/examples/splitstr.c +++ b/examples/splitstr.c @@ -14,7 +14,7 @@ int main() puts("\nSplit with c_formatch (regex):"); - c_with (cregex re = cregex_from("[^ ]+", 0), cregex_drop(&re)) + c_with (cregex re = cregex_from("[^ ]+", cre_default), cregex_drop(&re)) c_formatch (i, &re, " Hello World C99! ") printf("'%.*s'\n", c_ARGsv(i.match[0])); } diff --git a/include/stc/cregex.h b/include/stc/cregex.h index 9cde33e6..d88bb6a8 100644 --- a/include/stc/cregex.h +++ b/include/stc/cregex.h @@ -52,6 +52,7 @@ typedef enum { } cregex_result; enum { + cre_default = 0, /* compile-flags */ cre_c_dotall = 1<<0, /* dot matches newline too */ cre_c_caseless = 1<<1, /* ignore case */ @@ -105,8 +106,9 @@ int cregex_find(const cregex* re, const char* input, csview match[], int mflags); static inline int cregex_find_sv(const cregex* re, csview input, csview match[]) { - match[0] = input; - return cregex_find(re, input.str, match, cre_m_startend); + csview *mp = NULL; + if (match) { match[0] = input; mp = match; } + return cregex_find(re, input.str, mp, cre_m_startend); } /* match + compile RE pattern */ @@ -115,21 +117,21 @@ int cregex_find_pattern(const char* pattern, const char* input, static inline bool cregex_is_match(const cregex* re, const char* input) - { return cregex_find(re, input, NULL, 0) == cre_success; } + { return cregex_find(re, input, NULL, cre_default) == cre_success; } /* replace regular expression */ cstr cregex_replace_sv(const cregex* re, csview input, const char* replace, unsigned count, - int rflags, bool (*mfun)(int i, csview match, cstr* mstr)); + bool (*mfun)(int i, csview match, cstr* mstr), int rflags); static inline cstr cregex_replace(const cregex* re, const char* input, const char* replace, unsigned count) { csview sv = {input, strlen(input)}; - return cregex_replace_sv(re, sv, replace, count, 0, NULL); + return cregex_replace_sv(re, sv, replace, count, NULL, cre_default); } /* replace + compile RE pattern, and extra arguments */ cstr cregex_replace_pattern(const char* pattern, const char* input, const char* replace, unsigned count, - int crflags, bool (*mfun)(int i, csview match, cstr* mstr)); + bool (*mfun)(int i, csview match, cstr* mstr), int crflags); /* destroy regex */ void cregex_drop(cregex* self); diff --git a/src/cregex.c b/src/cregex.c index 88893de6..6eadd257 100644 --- a/src/cregex.c +++ b/src/cregex.c @@ -1225,7 +1225,7 @@ cregex_find_pattern(const char* pattern, const char* input, cstr cregex_replace_sv(const cregex* re, csview input, const char* replace, unsigned count, - int rflags, bool (*mfun)(int, csview, cstr*)) { + bool (*mfun)(int, csview, cstr*), int rflags) { cstr out = cstr_null; cstr subst = cstr_null; csview match[cre_MAXCAPTURES]; @@ -1248,12 +1248,12 @@ cregex_replace_sv(const cregex* re, csview input, const char* replace, unsigned cstr cregex_replace_pattern(const char* pattern, const char* input, const char* replace, unsigned count, - int crflags, bool (*mfun)(int, csview, cstr*)) { + bool (*mfun)(int, csview, cstr*), int crflags) { cregex re = cregex_init(); if (cregex_compile(&re, pattern, crflags) != cre_success) return cstr_new("[[error: invalid regex pattern]]"); csview sv = {input, strlen(input)}; - cstr out = cregex_replace_sv(&re, sv, replace, count, crflags, mfun); + cstr out = cregex_replace_sv(&re, sv, replace, count, mfun, crflags); cregex_drop(&re); return out; } |
