svn commit: r224469 - user/gabor/tre-integration/contrib/tre/lib

Gabor Kovesdan gabor at FreeBSD.org
Wed Jul 27 22:54:41 UTC 2011


Author: gabor
Date: Wed Jul 27 22:54:40 2011
New Revision: 224469
URL: http://svn.freebsd.org/changeset/base/224469

Log:
  - Add support for REG_ICASE
  - Eliminate code duplication by using macro

Modified:
  user/gabor/tre-integration/contrib/tre/lib/fastmatch.c
  user/gabor/tre-integration/contrib/tre/lib/fastmatch.h
  user/gabor/tre-integration/contrib/tre/lib/tre-compile.c

Modified: user/gabor/tre-integration/contrib/tre/lib/fastmatch.c
==============================================================================
--- user/gabor/tre-integration/contrib/tre/lib/fastmatch.c	Wed Jul 27 11:21:50 2011	(r224468)
+++ user/gabor/tre-integration/contrib/tre/lib/fastmatch.c	Wed Jul 27 22:54:40 2011	(r224469)
@@ -42,7 +42,7 @@
 #include "xmalloc.h"
 
 static int	fastcmp(const void *, const void *, size_t,
-			tre_str_type_t);
+			tre_str_type_t, bool);
 static void	revstr(tre_char_t *, int);
 static void	revs(char *str, int len);
 
@@ -90,10 +90,12 @@ static void	revs(char *str, int len);
       {									\
 	case STR_BYTE:							\
 	case STR_MBS:							\
-	  mismatch = fastcmp(fg->pattern, startptr, fg->len, type);	\
+	  mismatch = fastcmp(fg->pattern, startptr, fg->len, type,	\
+			     fg->icase);				\
 	  break;							\
 	case STR_WIDE:							\
-	  mismatch = fastcmp(fg->wpattern, startptr, fg->wlen, type);	\
+	  mismatch = fastcmp(fg->wpattern, startptr, fg->wlen, type,	\
+			     fg->icase);				\
 	default:							\
 	  break;							\
       }									\
@@ -171,31 +173,76 @@ static void	revs(char *str, int len);
   {									\
     int k = fg->wlen - i;						\
     hashtable_put(fg->qsBc_table, &fg->wpattern[i], &k);		\
+    if (fg->icase)							\
+      {									\
+	wint_t wc = iswlower(fg->wpattern[i]) ?				\
+	  towupper(fg->wpattern[i]) : towlower(fg->wpattern[i]);	\
+	hashtable_put(fg->qsBc_table, &wc, &k);				\
+      }									\
   }									\
 									\
   for (unsigned int i = 0; i <= UCHAR_MAX; i++)				\
     fg->qsBc[i] = fg->len - hasDot;					\
   for (int i = hasDot + 1; i < fg->len; i++)				\
-    fg->qsBc[(unsigned)fg->pattern[i]] = fg->len - i;
+    {									\
+      fg->qsBc[(unsigned)fg->pattern[i]] = fg->len - i;			\
+      if (fg->icase)							\
+	{								\
+	  char c = islower(fg->pattern[i]) ? toupper(fg->pattern[i])	\
+	    : tolower(fg->pattern[i]);					\
+	  fg->qsBc[(unsigned)c] = fg->len - i;				\
+	}								\
+    }
 #else
 #define FILL_QSBC							\
   for (unsigned int i = 0; i <= UCHAR_MAX; i++)				\
     fg->qsBc[i] = fg->wlen - hasDot;					\
   for (int i = hasDot + 1; i < fg->wlen; i++)				\
-    fg->qsBc[(unsigned)fg->wpattern[i]] = fg->wlen - i;
+    {									\
+      fg->qsBc[(unsigned)fg->wpattern[i]] = fg->wlen - i;		\
+      if (fg->icase)							\
+	{								\
+	  char c = islower(fg->wpattern[i]) ? toupper(fg->wpattern[i])	\
+	    : tolower(fg->wpattern[i]);					\
+	  fg->qsBc[(unsigned)c] = fg->len - i;				\
+	}								\
+    }
 #endif
 
+#define REVFUNC(name, argtype)						\
+static inline void							\
+name(argtype *str, int len)						\
+{									\
+  argtype c;								\
+									\
+  for (int i = 0; i < len / 2; i++)					\
+  {									\
+    c = str[i];								\
+    str[i] = str[len - i - 1];						\
+    str[len - i - 1] = c;						\
+  }									\
+}
+
+REVFUNC(revstr, tre_char_t)
+REVFUNC(revs, char)
+
 
 /*
  * Returns: REG_OK on success, error code otherwise
  */
 int
-tre_fastcomp_literal(fastmatch_t *fg, const tre_char_t *wpat, size_t n)
+tre_fastcomp_literal(fastmatch_t *fg, const tre_char_t *wpat, size_t n,
+		     int cflags)
 {
   int hasDot = 0;
 
   /* Initialize. */
   memset(fg, 0, sizeof(*fg));
+  fg->icase = (cflags & REG_ICASE);
+  /* XXX */
+  if (fg->icase && (MB_CUR_MAX > 1))
+    return REG_BADPAT;
+
   fg->wlen = (n == 0) ? tre_strlen(wpat) : n;
   fg->wpattern = xmalloc((fg->wlen + 1) * sizeof(tre_char_t));
   if (fg->wpattern == NULL)
@@ -215,7 +262,8 @@ tre_fastcomp_literal(fastmatch_t *fg, co
  * Returns: REG_OK on success, error code otherwise
  */
 int
-tre_fastcomp(fastmatch_t *fg, const tre_char_t *wpat, size_t n)
+tre_fastcomp(fastmatch_t *fg, const tre_char_t *wpat, size_t n,
+	     int cflags)
 {
   int firstHalfDot = -1;
   int firstLastHalfDot = -1;
@@ -224,6 +272,11 @@ tre_fastcomp(fastmatch_t *fg, const tre_
 
   /* Initialize. */
   memset(fg, 0, sizeof(*fg));
+  fg->icase = (cflags & REG_ICASE);
+  /* XXX */
+  if (fg->icase && (MB_CUR_MAX > 1))
+    return REG_BADPAT;
+
   fg->wlen = (n == 0) ? tre_strlen(wpat) : n;
 
   /* Remove end-of-line character ('$'). */
@@ -427,7 +480,7 @@ tre_fastfree(fastmatch_t *fg)
  */
 static inline int
 fastcmp(const void *pat, const void *data, size_t len,
-	tre_str_type_t type)
+	tre_str_type_t type, bool icase)
 {
   const char *str_byte = data;
   const char *pat_byte = pat;
@@ -444,11 +497,13 @@ fastcmp(const void *pat, const void *dat
       {
 	case STR_BYTE:
 	case STR_MBS:
-	  if (pat_byte[i] == str_byte[i])
+	  if (icase ? (tolower(pat_byte[i]) == tolower(str_byte[i]))
+	      : (pat_byte[i] == str_byte[i]))
 	    continue;
 	  break;
 	case STR_WIDE:
-	  if (pat_wide[i] == str_wide[i])
+	  if (icase ? (towlower(pat_wide[i]) == towlower(str_wide[i]))
+	      : (pat_wide[i] == str_wide[i]))
 	    continue;
 	  break;
 	default:
@@ -461,32 +516,3 @@ fastcmp(const void *pat, const void *dat
 
   return ret;
 }
-
-static inline void
-revstr(tre_char_t *str, int len)
-{
-  tre_char_t c;
-
-  for (int i = 0; i < len / 2; i++)
-  {
-    c = str[i];
-    str[i] = str[len - i - 1];
-    str[len - i - 1] = c;
-  }
-}
-
-/*
- * XXX: eliminate code duplication
- */
-static inline void
-revs(char *str, int len)
-{
-  char c;
-
-  for (int i = 0; i < len / 2; i++)
-  {
-    c = str[i];
-    str[i] = str[len - i - 1];
-    str[len - i - 1] = c;
-  }
-}

Modified: user/gabor/tre-integration/contrib/tre/lib/fastmatch.h
==============================================================================
--- user/gabor/tre-integration/contrib/tre/lib/fastmatch.h	Wed Jul 27 11:21:50 2011	(r224468)
+++ user/gabor/tre-integration/contrib/tre/lib/fastmatch.h	Wed Jul 27 22:54:40 2011	(r224469)
@@ -50,11 +50,12 @@ typedef struct {
   bool eol;
   bool reversed;
   bool word;
+  bool icase;
 } fastmatch_t;
 
 int	tre_fastcomp_literal(fastmatch_t *preg, const tre_char_t *regex,
-	    size_t);
-int	tre_fastcomp(fastmatch_t *preg, const tre_char_t *regex, size_t);
+	    size_t, int);
+int	tre_fastcomp(fastmatch_t *preg, const tre_char_t *regex, size_t, int);
 int	tre_fastexec(const fastmatch_t *fg, const void *data, size_t len,
 	    tre_str_type_t type, int nmatch, regmatch_t pmatch[]);
 void	tre_fastfree(fastmatch_t *preg);

Modified: user/gabor/tre-integration/contrib/tre/lib/tre-compile.c
==============================================================================
--- user/gabor/tre-integration/contrib/tre/lib/tre-compile.c	Wed Jul 27 11:21:50 2011	(r224468)
+++ user/gabor/tre-integration/contrib/tre/lib/tre-compile.c	Wed Jul 27 22:54:40 2011	(r224469)
@@ -1876,8 +1876,8 @@ tre_compile(regex_t *preg, const tre_cha
   if (!shortcut)
     return REG_ESPACE;
   ret = (cflags & REG_LITERAL)
-    ? tre_fastcomp_literal(shortcut, regex, n)
-    : tre_fastcomp(shortcut, regex, n);
+    ? tre_fastcomp_literal(shortcut, regex, n, cflags)
+    : tre_fastcomp(shortcut, regex, n, cflags);
   if (ret == REG_OK)
     {
       preg->shortcut = shortcut;


More information about the svn-src-user mailing list