From 2db7cfac187b010a7406ccf2cc85c1763340840c Mon Sep 17 00:00:00 2001 From: Gerben Aaltink Date: Sat, 28 Sep 2024 05:47:57 +0200 Subject: [PATCH] Extracted bmatch.h --- bmatch.h | 39 +++++++++++++++++++++++++++++++++++++++ rmatch.c | 38 -------------------------------------- rmatch.h | 5 ++++- 3 files changed, 43 insertions(+), 39 deletions(-) create mode 100644 bmatch.h diff --git a/bmatch.h b/bmatch.h new file mode 100644 index 0000000..b1472c2 --- /dev/null +++ b/bmatch.h @@ -0,0 +1,39 @@ +#ifndef BMATCH_H +#define BMATCH_H +#include +#include +int bmatchhere(char *regexp, char *text); +int bmatchstar(int c, char *regexp, char *text); +int bmatch(char *regexp, char *text) +{ + if (regexp[0] == '^') + return bmatchhere(regexp + 1, text); + do + { + if (bmatchhere(regexp, text)) + return 1; + } while (*text++ != '\0'); + return 0; +} +int bmatchhere(char *regexp, char *text) +{ + if (regexp[0] == '\0') + return 1; + if (regexp[1] == '*') + return bmatchstar(regexp[0], regexp + 2, text); + if (regexp[0] == '$' && regexp[1] == '\0') + return *text == '\0'; + if (*text != '\0' && (regexp[0] == '.' || regexp[0] == *text)) + return bmatchhere(regexp + 1, text + 1); + return 0; +} +int bmatchstar(int c, char *regexp, char *text) +{ + do + { + if (bmatchhere(regexp, text)) + return 1; + } while (*text != '\0' && (*text++ == c || c == '.')); + return 0; +} +#endif \ No newline at end of file diff --git a/rmatch.c b/rmatch.c index d5245bf..229f156 100644 --- a/rmatch.c +++ b/rmatch.c @@ -1,42 +1,4 @@ -#include #include "rmatch.h" -#include - -int matchhere(char *regexp, char *text); -int matchstar(int c, char *regexp, char *text); -int match(char *regexp, char *text) -{ - if (regexp[0] == '^') - return matchhere(regexp + 1, text); - do - { - if (matchhere(regexp, text)) - return 1; - } while (*text++ != '\0'); - return 0; -} -int matchhere(char *regexp, char *text) -{ - if (regexp[0] == '\0') - return 1; - if (regexp[1] == '*') - return matchstar(regexp[0], regexp + 2, text); - if (regexp[0] == '$' && regexp[1] == '\0') - return *text == '\0'; - if (*text != '\0' && (regexp[0] == '.' || regexp[0] == *text)) - return matchhere(regexp + 1, text + 1); - return 0; -} -int matchstar(int c, char *regexp, char *text) -{ - do - { - if (matchhere(regexp, text)) - return 1; - } while (*text != '\0' && (*text++ == c || c == '.')); - return 0; -} - int main() { diff --git a/rmatch.h b/rmatch.h index 889eba1..7136966 100644 --- a/rmatch.h +++ b/rmatch.h @@ -1,3 +1,5 @@ +#ifndef RMATCH_H +#define RMATCH_H #include #include #include @@ -204,4 +206,5 @@ void rmatch_tests() rmatch_test("T.*e q.*k b.*n f+.*x j.*s o.*r t.*e l[oa][az].*y d[ao]g.", text_fox4, (char *[]){"The quick brown fooox jumps over the lazy dog.", NULL}); rmatch_test("T.*e q.*k b.*n fo+x j.*s o.*r t.*e l[oa][az].*y d[ao]g.", text_fox4, (char *[]){"The quick brown fooox jumps over the lazy dog.", NULL}); rmatch_test("T.*e q.*k b.*n f+x j.*s o.*r t.*e l[oa][az].*y d[ao]g.", text_fox4, (char *[]){NULL}); -} \ No newline at end of file +} +#endif \ No newline at end of file