From 7ffb5a4bd4b70fe0806674c4883aaca5a12e5d6b Mon Sep 17 00:00:00 2001 From: Robert Spalek Date: Fri, 30 Mar 2001 13:07:05 +0000 Subject: [PATCH] rx_compile() can now compile with IGNORING CASE enabled too regex-test added --- lib/Makefile | 1 + lib/lib.h | 2 +- lib/regex-test.c | 27 +++++++++++++++++++++++++++ lib/regex.c | 16 +++++++++++++++- 4 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 lib/regex-test.c diff --git a/lib/Makefile b/lib/Makefile index d8012576..c5430ac0 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -17,3 +17,4 @@ obj/lib/buckettool: obj/lib/buckettool.o obj/lib/libsh.a obj/lib/conf-test: obj/lib/conf-test.o obj/lib/libsh.a obj/lib/sort-test: obj/lib/sort-test.o obj/lib/libsh.a obj/lib/lfs-test: obj/lib/lfs-test.o obj/lib/libsh.a +obj/lib/regex-test: obj/lib/regex-test.o obj/lib/libsh.a diff --git a/lib/lib.h b/lib/lib.h index dcf97932..22169423 100644 --- a/lib/lib.h +++ b/lib/lib.h @@ -157,7 +157,7 @@ uns get_timer(void); typedef struct regex regex; -regex *rx_compile(byte *r); +regex *rx_compile(byte *r, int icase); void rx_free(regex *r); int rx_match(regex *r, byte *s); int rx_subst(regex *r, byte *by, byte *src, byte *dest, uns destlen); diff --git a/lib/regex-test.c b/lib/regex-test.c new file mode 100644 index 00000000..abe684ad --- /dev/null +++ b/lib/regex-test.c @@ -0,0 +1,27 @@ +/* + * Sherlock Library -- Regular Expressions Test + * + * (c) 2001 Robert Spalek + */ + +#include "lib/lib.h" + +#include + +#define TEST(txt, should) printf(#txt ": %d (should %d)\n", rx_match(r, #txt), should) +int +main(void) +{ + regex *r; + r = rx_compile("a.*b.*c", 0); + TEST(abc, 1); + TEST(ajkhkbbbbbc, 1); + TEST(Aabc, 0); + rx_free(r); + r = rx_compile("a.*b.*c", 1); + TEST(aBc, 1); + TEST(ajkhkbBBBBC, 1); + TEST(Aabc, 1); + rx_free(r); + return 0; +} diff --git a/lib/regex.c b/lib/regex.c index 5fea0976..4cc75d6c 100644 --- a/lib/regex.c +++ b/lib/regex.c @@ -2,16 +2,20 @@ * Sherlock Library -- Regular Expressions * * (c) 1997 Martin Mares + * (c) 2001 Robert Spalek */ #include "lib/lib.h" +#include "lib/chartype.h" +#include #include #include #include #include #define INITIAL_MEM 1024 /* Initial space allocated for each pattern */ +#define CHAR_SET_SIZE 256 /* How many characters in the character set. */ struct regex { struct re_pattern_buffer buf; @@ -20,13 +24,23 @@ struct regex { }; regex * -rx_compile(byte *p) +rx_compile(byte *p, int icase) { regex *r = xmalloc_zero(sizeof(regex)); const char *msg; r->buf.buffer = xmalloc(INITIAL_MEM); r->buf.allocated = INITIAL_MEM; + if (icase) + { + unsigned i; + r->buf.translate = xmalloc (CHAR_SET_SIZE); + /* Map uppercase characters to corresponding lowercase ones. */ + for (i = 0; i < CHAR_SET_SIZE; i++) + r->buf.translate[i] = Cupper(i) ? tolower (i) : i; + } + else + r->buf.translate = NULL; msg = re_compile_pattern(p, strlen(p), &r->buf); if (!msg) return r; -- 2.39.2