From 2bae847079852eb808e50a136659e98898616bef Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Sat, 23 Jul 2022 22:15:02 +0200 Subject: Fixed a few small issues with cregex.c Reverted cregex_match() to cregex_find(). Renamed cre_* flags. --- examples/regex1.c | 2 +- examples/regex2.c | 2 +- examples/regex_match.c | 6 +-- include/stc/cregex.h | 23 ++++---- src/cregex.c | 45 ++++++++-------- tests/cregex_test.c | 142 ++++++++++++++++++++++++------------------------- 6 files changed, 111 insertions(+), 109 deletions(-) diff --git a/examples/regex1.c b/examples/regex1.c index 98fc644d..7e8040ac 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(cstr_str(&input), &float_expr, NULL, 0) == 1) + if (cregex_is_match(cstr_str(&input), &float_expr, 0)) 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 23f774ba..307579d4 100644 --- a/examples/regex2.c +++ b/examples/regex2.c @@ -20,7 +20,7 @@ int main() } csview m[20]; printf("input: %s\n", inputs[i]); - if (cregex_match(inputs[i], &re, m, 0) == 1) + if (cregex_find(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 821813bb..cb0fdd11 100644 --- a/examples/regex_match.c +++ b/examples/regex_match.c @@ -15,20 +15,20 @@ int main() int res = cregex_compile(&re, "[+-]?([0-9]*\\.)?\\d+([Ee][+-]?\\d+)?", 0); printf("%d\n", res); csview m[5]; - if (cregex_match(s, &re, m, 0) == 1) { + if (cregex_find(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(s, &re, m, cre_NEXT) == 1) { + while (cregex_find(s, &re, m, cre_m_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("hello@wørld", &re, m, 0)) == 1) { + if ((res = cregex_find("hello@wørld", &re, m, 0)) == 1) { c_forrange (i, res) printf("match: [%" c_PRIsv "]\n", c_ARGsv(m[i])); } else diff --git a/include/stc/cregex.h b/include/stc/cregex.h index 1b77a980..c920ae7b 100644 --- a/include/stc/cregex.h +++ b/include/stc/cregex.h @@ -52,12 +52,12 @@ typedef enum { enum { /* compile-flags */ - cre_DOTALL = 1<<0, - cre_CASELESS = 1<<1, + cre_c_dotall = 1<<0, + cre_c_caseless = 1<<1, /* match-flags */ - cre_FULLMATCH = 1<<2, - cre_NEXT = 1<<3, - cre_STARTEND = 1<<4, + cre_m_fullmatch = 1<<2, + cre_m_next = 1<<3, + cre_m_startend = 1<<4, /* limits */ cre_MAXCLASSES = 16, cre_MAXCAPTURES = 32, @@ -65,6 +65,7 @@ enum { typedef struct { struct Reprog* prog; + int error; } cregex; typedef csview cregmatch; @@ -89,12 +90,16 @@ cregex cregex_from(const char* pattern, int cflags) { int cregex_captures(const cregex* self); /* return 1 on match, 0 on nomatch, and -1 on failure. */ -int cregex_match(const char* input, const cregex* re, - csview match[], int mflags); +int cregex_find(const char* input, const cregex* re, + csview match[], int mflags); /* match + compile RE pattern */ -int cregex_match_p(const char* input, const char* pattern, - csview match[], int cmflags); +int cregex_find_p(const char* input, const char* pattern, + csview match[], int cmflags); + +static inline +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, diff --git a/src/cregex.c b/src/cregex.c index d2baf2ef..1d9609b3 100644 --- a/src/cregex.c +++ b/src/cregex.c @@ -326,7 +326,7 @@ typedef struct Parser short subidstack[NSTACK]; /* parallel to atorstack */ short* subidp; short cursubid; /* id of current subexpression */ - int errors; + int error; Reflags flags; int dot_type; int rune_type; @@ -352,7 +352,7 @@ static int bldcclass(Parser *par); static void rcerror(Parser *par, cregex_error_t err) { - par->errors = err; + par->error = err; longjmp(par->regkaboom, 1); } @@ -608,10 +608,6 @@ nextc(Parser *par, Rune *rp) {"{Upper}", 7, UTF_up}, {"{Lu}", 4, UTF_up}, {"{Alnum}", 7, UTF_an}, {"{XDigit}", 8, UTF_xd}, - {"{Blank}", 7, ASC_bl}, - {"{Graph}", 7, ASC_gr}, - {"{Print}", 7, ASC_pr}, - {"{Punct}", 7, ASC_pu}, }; int inv = *rp == 'P'; for (unsigned i = 0; i < (sizeof cls/sizeof *cls); ++i) @@ -672,13 +668,13 @@ lex(Parser *par) case '|': return OR; case '.': return par->dot_type; case '(': - if (par->exprp[0] == '?') { + if (par->exprp[0] == '?') { /* override global flags */ for (int k = 1, enable = 1; ; ++k) switch (par->exprp[k]) { case 0 : par->exprp += k; return END; case ')': par->exprp += k + 1; goto start; case '-': enable = 0; break; - case 's': if (!par->flags.dotall) par->dot_type = ANY + enable; break; - case 'i': if (!par->flags.caseless) par->rune_type = RUNE + enable; break; + case 's': par->dot_type = ANY + enable; break; + case 'i': par->rune_type = RUNE + enable; break; default: rcerror(par, cre_unknownoperator); return 0; } } @@ -804,15 +800,15 @@ regcomp1(Reprog *progp, Parser *par, const char *s, int cflags) const int instcap = 5 + 6*strlen(s); Reprog* pp = (Reprog *)realloc(progp, sizeof(Reprog) + instcap*sizeof(Reinst)); if (pp == NULL) { - par->errors = cre_outofmemory; + par->error = cre_outofmemory; free(progp); return NULL; } - pp->flags.caseless = (cflags & cre_CASELESS) != 0; - pp->flags.dotall = (cflags & cre_DOTALL) != 0; + pp->flags.caseless = (cflags & cre_c_caseless) != 0; + pp->flags.dotall = (cflags & cre_c_dotall) != 0; par->freep = pp->firstinst; par->classp = pp->cclass; - par->errors = 0; + par->error = 0; if (setjmp(par->regkaboom)) goto out; @@ -864,7 +860,7 @@ regcomp1(Reprog *progp, Parser *par, const char *s, int cflags) dump(pp); #endif out: - if (par->errors) { + if (par->error) { free(pp); pp = NULL; } @@ -1059,7 +1055,7 @@ regexec1(const Reprog *progp, /* program to run */ /* efficiency: advance and re-evaluate */ continue; case END: /* Match! */ - match = !(mflags & cre_FULLMATCH) || + match = !(mflags & cre_m_fullmatch) || ((s == j->eol || r == 0 || r == '\n') && (tlp->se.m[0].str == bol || tlp->se.m[0].str[-1] == '\n')); tlp->se.m[0].size = s - tlp->se.m[0].str; @@ -1126,9 +1122,9 @@ regexec(const Reprog *progp, /* program to run */ j.eol = NULL; if (mp && mp[0].size) { - if (mflags & cre_STARTEND) + if (mflags & cre_m_startend) j.starts = mp[0].str, j.eol = mp[0].str + mp[0].size; - else if (mflags & cre_NEXT) + else if (mflags & cre_m_next) j.starts = mp[0].str + mp[0].size; } @@ -1200,7 +1196,8 @@ int cregex_compile(cregex *self, const char* pattern, int cflags) { Parser par; self->prog = regcomp1(self->prog, &par, pattern, cflags); - return self->prog ? cre_success : par.errors; + self->error = par.error; + return self->prog ? cre_success : par.error; } int @@ -1209,8 +1206,8 @@ cregex_captures(const cregex* self) { } int -cregex_match(const char* input, const cregex* re, - csview match[], int mflags) { +cregex_find(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; @@ -1219,12 +1216,12 @@ cregex_match(const char* input, const cregex* re, } } -int cregex_match_p(const char* input, const char* pattern, - csview match[], int cmflags) { +int cregex_find_p(const char* input, const char* pattern, + csview match[], int cmflags) { cregex re = cregex_init(); int res = cregex_compile(&re, pattern, cmflags); if (res < 0) return res; - res = cregex_match(input, &re, match, cmflags); + res = cregex_find(input, &re, match, cmflags); cregex_drop(&re); return res; } @@ -1239,7 +1236,7 @@ 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(input + from, re, match, 0) == 1) { + while (count-- && cregex_find(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); diff --git a/tests/cregex_test.c b/tests/cregex_test.c index e6166a10..c17f1dd2 100644 --- a/tests/cregex_test.c +++ b/tests/cregex_test.c @@ -22,16 +22,16 @@ START_TEST(compile_match_char) cregex re = cregex_new("äsdf"); ck_assert_int_eq(cregex_error(), cregex_OK); - cregex_match match; - ck_assert(cregex_match(&re, "äsdf", &match)); + cregex_find match; + ck_assert(cregex_find(&re, "äsdf", &match)); ck_assert_uint_eq(match.start, 0); ck_assert_uint_eq(match.end, 5); // ä is two bytes wide - ck_assert(cregex_match(&re, "zäsdf", &match)); + ck_assert(cregex_find(&re, "zäsdf", &match)); ck_assert_uint_eq(match.start, 1); ck_assert_uint_eq(match.end, 6); - ck_assert(cregex_match(&re, "äsdf", &match)); + ck_assert(cregex_find(&re, "äsdf", &match)); ck_assert_uint_eq(match.start, 0); ck_assert_uint_eq(match.end, 5); @@ -44,13 +44,13 @@ START_TEST(compile_match_anchors) cregex re[1] = {cregex_new("^äs.f$")}; ck_assert_int_eq(cregex_error(), cregex_OK); - cregex_match match; - ck_assert(cregex_match(re, "äsdf", &match)); + cregex_find match; + ck_assert(cregex_find(re, "äsdf", &match)); ck_assert_uint_eq(match.start, 0); ck_assert_uint_eq(match.end, 5); - ck_assert(cregex_match(re, "äs♥f", &match)); - ck_assert(cregex_match(re, "äsöf", &match)); + ck_assert(cregex_find(re, "äs♥f", &match)); + ck_assert(cregex_find(re, "äsöf", &match)); cregex_drop(re); } @@ -62,31 +62,31 @@ START_TEST(compile_match_quantifiers) re = cregex_new("ä+"); ck_assert_int_eq(cregex_error(), cregex_OK); - cregex_match match; - ck_assert(cregex_match(&re, "ääb", &match)); + cregex_find match; + ck_assert(cregex_find(&re, "ääb", &match)); ck_assert_uint_eq(match.start, 0); ck_assert_uint_eq(match.end, 4); - ck_assert(cregex_match(&re, "bäbb", &match)); + ck_assert(cregex_find(&re, "bäbb", &match)); ck_assert_uint_eq(match.start, 1); ck_assert_uint_eq(match.end, 3); - ck_assert(!cregex_match(&re, "bbb", &match)); + ck_assert(!cregex_find(&re, "bbb", &match)); } c_auto (cregex, re) { re = cregex_new("bä*"); ck_assert_int_eq(cregex_error(), cregex_OK); - cregex_match match; - ck_assert(cregex_match(&re, "bääb", &match)); + cregex_find match; + ck_assert(cregex_find(&re, "bääb", &match)); ck_assert_uint_eq(match.start, 0); ck_assert_uint_eq(match.end, 5); - ck_assert(cregex_match(&re, "bäbb", &match)); + ck_assert(cregex_find(&re, "bäbb", &match)); ck_assert_uint_eq(match.start, 0); ck_assert_uint_eq(match.end, 3); - ck_assert(cregex_match(&re, "bbb", &match)); + ck_assert(cregex_find(&re, "bbb", &match)); ck_assert_uint_eq(match.start, 0); ck_assert_uint_eq(match.end, 1); } @@ -109,28 +109,28 @@ START_TEST(compile_match_complex_quants) re4 = cregex_new("ä{,3}"); ck_assert_int_eq(cregex_error(), cregex_OK); - cregex_match match; - ck_assert(cregex_match(&re1, "ääb", &match)); + cregex_find match; + ck_assert(cregex_find(&re1, "ääb", &match)); ck_assert_uint_eq(match.start, 0); ck_assert_uint_eq(match.end, 4); - ck_assert(cregex_match(&re1, "äääb", &match)); - ck_assert(cregex_match(&re1, "äb", &match)); - ck_assert(!cregex_match(&re1, "b", &match)); + ck_assert(cregex_find(&re1, "äääb", &match)); + ck_assert(cregex_find(&re1, "äb", &match)); + ck_assert(!cregex_find(&re1, "b", &match)); - ck_assert(cregex_match(&re2, "ää", &match)); + ck_assert(cregex_find(&re2, "ää", &match)); ck_assert_uint_eq(match.start, 0); ck_assert_uint_eq(match.end, 2); - ck_assert(cregex_match(&re2, "bbäb", &match)); - ck_assert(!cregex_match(&re2, "bbbb", &match)); + ck_assert(cregex_find(&re2, "bbäb", &match)); + ck_assert(!cregex_find(&re2, "bbbb", &match)); - ck_assert(cregex_match(&re3, "ääääääääääb", &match)); + ck_assert(cregex_find(&re3, "ääääääääääb", &match)); ck_assert_uint_eq(match.start, 0); ck_assert_uint_eq(match.end, 20); - ck_assert(cregex_match(&re3, "b", &match)); + ck_assert(cregex_find(&re3, "b", &match)); - ck_assert(cregex_match(&re4, "bä", &match)); - ck_assert(cregex_match(&re4, "bää", &match)); - ck_assert(cregex_match(&re4, "bäää", &match)); + ck_assert(cregex_find(&re4, "bä", &match)); + ck_assert(cregex_find(&re4, "bää", &match)); + ck_assert(cregex_find(&re4, "bäää", &match)); } } END_TEST @@ -140,9 +140,9 @@ START_TEST(compile_match_escaped_chars) cregex re = cregex_new("\\n\\r\\t\\{"); ck_assert_int_eq(cregex_error(), cregex_OK); - cregex_match match; - ck_assert(cregex_match(&re, "\n\r\t{", &match)); - ck_assert(!cregex_match(&re, "\n\r\t", &match)); + cregex_find match; + ck_assert(cregex_find(&re, "\n\r\t{", &match)); + ck_assert(!cregex_find(&re, "\n\r\t", &match)); cregex_drop(&re); } @@ -159,17 +159,17 @@ START_TEST(compile_match_class_simple) re3 = cregex_new("\\D"); ck_assert_int_eq(cregex_error(), cregex_OK); - cregex_match match; - ck_assert(cregex_match(&re1, " ", &match)); - ck_assert(cregex_match(&re1, "\r", &match)); - ck_assert(cregex_match(&re1, "\n", &match)); + cregex_find match; + ck_assert(cregex_find(&re1, " ", &match)); + ck_assert(cregex_find(&re1, "\r", &match)); + ck_assert(cregex_find(&re1, "\n", &match)); - ck_assert(cregex_match(&re2, "a", &match)); - ck_assert(cregex_match(&re2, "0", &match)); - ck_assert(cregex_match(&re2, "_", &match)); + ck_assert(cregex_find(&re2, "a", &match)); + ck_assert(cregex_find(&re2, "0", &match)); + ck_assert(cregex_find(&re2, "_", &match)); - ck_assert(cregex_match(&re3, "k", &match)); - ck_assert(!cregex_match(&re3, "0", &match)); + ck_assert(cregex_find(&re3, "k", &match)); + ck_assert(!cregex_find(&re3, "0", &match)); } } END_TEST @@ -181,15 +181,15 @@ START_TEST(compile_match_or) re = cregex_new("as|df"); ck_assert_int_eq(cregex_error(), cregex_OK); - cregex_match match; - ck_assert(cregex_match(&re, "as", &match)); - ck_assert(cregex_match(&re, "df", &match)); + cregex_find match; + ck_assert(cregex_find(&re, "as", &match)); + ck_assert(cregex_find(&re, "df", &match)); re2 = cregex_new("(as|df)"); ck_assert_int_eq(cregex_error(), cregex_OK); - ck_assert(cregex_match(&re2, "as", &match)); - ck_assert(cregex_match(&re2, "df", &match)); + ck_assert(cregex_find(&re2, "as", &match)); + ck_assert(cregex_find(&re2, "df", &match)); } } END_TEST @@ -199,11 +199,11 @@ START_TEST(compile_match_class_complex_0) cregex re = cregex_new("[asdf]"); ck_assert_int_eq(cregex_error(), cregex_OK); - cregex_match match; - ck_assert(cregex_match(&re, "a", &match)); - ck_assert(cregex_match(&re, "s", &match)); - ck_assert(cregex_match(&re, "d", &match)); - ck_assert(cregex_match(&re, "f", &match)); + cregex_find match; + ck_assert(cregex_find(&re, "a", &match)); + ck_assert(cregex_find(&re, "s", &match)); + ck_assert(cregex_find(&re, "d", &match)); + ck_assert(cregex_find(&re, "f", &match)); cregex_drop(&re); } @@ -214,12 +214,12 @@ START_TEST(compile_match_class_complex_1) cregex re = cregex_new("[a-zä0-9öA-Z]"); ck_assert_int_eq(cregex_error(), cregex_OK); - cregex_match match; - ck_assert(cregex_match(&re, "a", &match)); - ck_assert(cregex_match(&re, "5", &match)); - ck_assert(cregex_match(&re, "A", &match)); - ck_assert(cregex_match(&re, "ä", &match)); - ck_assert(cregex_match(&re, "ö", &match)); + cregex_find match; + ck_assert(cregex_find(&re, "a", &match)); + ck_assert(cregex_find(&re, "5", &match)); + ck_assert(cregex_find(&re, "A", &match)); + ck_assert(cregex_find(&re, "ä", &match)); + ck_assert(cregex_find(&re, "ö", &match)); cregex_drop(&re); } @@ -230,10 +230,10 @@ START_TEST(compile_match_cap) cregex re = cregex_new("(abc)d"); ck_assert_int_eq(cregex_error(), cregex_OK); - cregex_match match; - ck_assert(cregex_match(&re, "abcd", &match)); - ck_assert(cregex_match(&re, "llljabcdkk", &match)); - ck_assert(!cregex_match(&re, "abc", &match)); + cregex_find match; + ck_assert(cregex_find(&re, "abcd", &match)); + ck_assert(cregex_find(&re, "llljabcdkk", &match)); + ck_assert(!cregex_find(&re, "abc", &match)); cregex_drop(&re); } @@ -263,16 +263,16 @@ START_TEST(search_all) c_auto (cregex, re) { re = cregex_new("ab"); - cregex_match m = {0}; + cregex_find m = {0}; bool res; - res = cregex_match_next(&re, "ab,ab,ab", &m); + res = cregex_find_next(&re, "ab,ab,ab", &m); ck_assert(res && m.start == 0); - res = cregex_match_next(&re, "ab,ab,ab", &m); + res = cregex_find_next(&re, "ab,ab,ab", &m); ck_assert(res && m.start == 3); - res = cregex_match_next(&re, "ab,ab,ab", &m); + res = cregex_find_next(&re, "ab,ab,ab", &m); ck_assert(res && m.start == 6); - res = cregex_match_next(&re, "ab,ab,ab", &m); + res = cregex_find_next(&re, "ab,ab,ab", &m); ck_assert(!res); } } @@ -293,10 +293,10 @@ START_TEST(captures_cap) re = cregex_new("(ab)((cd)+)"); ck_assert_uint_eq(cregex_capture_size(re), 3); - cregex_match match; - ck_assert(cregex_match(&re, "xxabcdcde", &match)); + cregex_find match; + ck_assert(cregex_find(&re, "xxabcdcde", &match)); - cregex_match cap0, cap1, cap2; + cregex_find cap0, cap1, cap2; cregex_capture(&re, 0, &cap0); cregex_capture(&re, 1, &cap1); cregex_capture(&re, 2, &cap2); @@ -308,8 +308,8 @@ START_TEST(captures_cap) ck_assert_uint_eq(cap2.start, 4); ck_assert_uint_eq(cap2.end, 8); - ck_assert(!cregex_matches(&re, "abcdcde")); - ck_assert(cregex_matches(&re, "abcdcdcd")); + ck_assert(!cregex_findes(&re, "abcdcde")); + ck_assert(cregex_findes(&re, "abcdcdcd")); } } END_TEST -- cgit v1.2.3