X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=lib%2Fregex.c;h=6ed90243c86a727e99a4a34c91a567dccb85e0d7;hb=89d14be7e888730e57d3a26b5ddd47d0b659c3cf;hp=524591ea03a3d5b496a63d1692c597263fba925d;hpb=16e83ddfab3e8e67212241d262e15ead3abd4b36;p=libucw.git diff --git a/lib/regex.c b/lib/regex.c index 524591ea..6ed90243 100644 --- a/lib/regex.c +++ b/lib/regex.c @@ -1,17 +1,20 @@ /* * Sherlock Library -- Regular Expressions * - * (c) 1997 Martin Mares, + * (c) 1997 Martin Mares + * (c) 2001 Robert Spalek */ +#include "lib/lib.h" +#include "lib/chartype.h" + #include #include #include #include -#include "lib.h" - #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,14 +23,24 @@ struct regex { }; regex * -rx_compile(byte *p) +rx_compile(byte *p, int icase) { - regex *r = xmalloc(sizeof(regex)); + regex *r = xmalloc_zero(sizeof(regex)); const char *msg; - bzero(r, sizeof(struct regex)); 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] = Cupcase(i); + } + else + r->buf.translate = NULL; + re_set_syntax(RE_SYNTAX_POSIX_EXTENDED); msg = re_compile_pattern(p, strlen(p), &r->buf); if (!msg) return r; @@ -37,14 +50,16 @@ rx_compile(byte *p) void rx_free(regex *r) { - free(r->buf.buffer); - free(r); + xfree(r->buf.buffer); + if (r->buf.translate) + xfree(r->buf.translate); + xfree(r); } int rx_match(regex *r, byte *s) { - uns len = strlen(s); + int len = strlen(s); r->len_cache = len; if (re_match(&r->buf, s, len, 0, &r->regs) < 0) @@ -69,7 +84,7 @@ rx_subst(regex *r, byte *by, byte *src, byte *dest, uns destlen) by++; if (*by >= '0' && *by <= '9') /* \0 gets replaced by entire pattern */ { - int j = *by++ - '0'; + uns j = *by++ - '0'; if (j < r->regs.num_regs) { byte *s = src + r->regs.start[j];