]> mj.ucw.cz Git - libucw.git/commitdiff
rx_compile() can now compile with IGNORING CASE enabled too
authorRobert Spalek <robert@ucw.cz>
Fri, 30 Mar 2001 13:07:05 +0000 (13:07 +0000)
committerRobert Spalek <robert@ucw.cz>
Fri, 30 Mar 2001 13:07:05 +0000 (13:07 +0000)
regex-test added

lib/Makefile
lib/lib.h
lib/regex-test.c [new file with mode: 0644]
lib/regex.c

index d80125761160faea6374347be2d56161439d59d8..c5430ac082662c2fed1fc89bf5fe159687b5250a 100644 (file)
@@ -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
index dcf97932d3d1bebd9d3bce1e43be872fbee74098..22169423260926312e91fe588ac85b6e0bc162e0 100644 (file)
--- 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 (file)
index 0000000..abe684a
--- /dev/null
@@ -0,0 +1,27 @@
+/*
+ *     Sherlock Library -- Regular Expressions Test
+ *
+ *     (c) 2001 Robert Spalek <robert@ucw.cz>
+ */
+
+#include "lib/lib.h"
+
+#include <stdio.h>
+
+#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;
+}
index 5fea0976d83babbe178743efe45ee07b7c750e52..4cc75d6c3db6490e6ea642424d3aeff1c7bcb140 100644 (file)
@@ -2,16 +2,20 @@
  *     Sherlock Library -- Regular Expressions
  *
  *     (c) 1997 Martin Mares <mj@ucw.cz>
+ *     (c) 2001 Robert Spalek <robert@ucw.cz>
  */
 
 #include "lib/lib.h"
+#include "lib/chartype.h"
 
+#include <ctype.h>
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 #include <regex.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,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;