Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 1 Sep 2011 16:47:50 +0000 (UTC)
From:      Gabor Kovesdan <gabor@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-user@freebsd.org
Subject:   svn commit: r225315 - in user/gabor/grep/trunk: . regex
Message-ID:  <201109011647.p81GloFp059514@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: gabor
Date: Thu Sep  1 16:47:49 2011
New Revision: 225315
URL: http://svn.freebsd.org/changeset/base/225315

Log:
  - Rely on the matcher when having a match-all empty pattern

Modified:
  user/gabor/grep/trunk/grep.c
  user/gabor/grep/trunk/regex/fastmatch.c
  user/gabor/grep/trunk/regex/tre-fastmatch.c
  user/gabor/grep/trunk/util.c

Modified: user/gabor/grep/trunk/grep.c
==============================================================================
--- user/gabor/grep/trunk/grep.c	Thu Sep  1 16:43:23 2011	(r225314)
+++ user/gabor/grep/trunk/grep.c	Thu Sep  1 16:47:49 2011	(r225315)
@@ -227,9 +227,20 @@ static void
 add_pattern(char *pat, size_t len)
 {
 
+	/* Do not add further pattern is we already match everything */
+	if (matchall)
+	  return;
+
 	/* Check if we can do a shortcut */
-	if (len == 0 || matchall) {
+	if (len == 0) {
 		matchall = true;
+		for (int i = 0; i < patterns; i++) {
+			free(pattern[i].pat);
+		}
+		pattern = grep_realloc(pattern, sizeof(struct pat));
+		pattern[0].pat = NULL;
+		pattern[0].len = 0;
+		patterns = 1;
 		return;
 	}
 	/* Increase size if necessary */

Modified: user/gabor/grep/trunk/regex/fastmatch.c
==============================================================================
--- user/gabor/grep/trunk/regex/fastmatch.c	Thu Sep  1 16:43:23 2011	(r225314)
+++ user/gabor/grep/trunk/regex/fastmatch.c	Thu Sep  1 16:47:49 2011	(r225315)
@@ -38,67 +38,72 @@
 
 /* XXX: avoid duplication */
 #define CONV_PAT							\
-  int ret;								\
-  tre_char_t *wregex;							\
-  size_t wlen;								\
-									\
-  wregex = xmalloc(sizeof(tre_char_t) * (n + 1));			\
-  if (wregex == NULL)							\
-    return REG_ESPACE;							\
-									\
-  if (TRE_MB_CUR_MAX == 1)						\
-    {									\
-      unsigned int i;							\
-      const unsigned char *str = (const unsigned char *)regex;		\
-      tre_char_t *wstr = wregex;					\
-									\
-      for (i = 0; i < n; i++)						\
-        *(wstr++) = *(str++);						\
-      wlen = n;								\
-    }									\
-  else									\
-    {									\
-      int consumed;							\
-      tre_char_t *wcptr = wregex;					\
-      mbstate_t state;							\
-      memset(&state, '\0', sizeof(state));				\
-      while (n > 0)							\
-        {								\
-          consumed = tre_mbrtowc(wcptr, regex, n, &state);		\
+  {									\
+    wregex = xmalloc(sizeof(tre_char_t) * (n + 1));			\
+    if (wregex == NULL)							\
+      return REG_ESPACE;						\
 									\
-          switch (consumed)						\
-            {								\
-            case 0:							\
-              if (*regex == '\0')					\
-                consumed = 1;						\
-              else							\
-                {							\
-                  xfree(wregex);					\
-                  return REG_BADPAT;					\
-                }							\
-              break;							\
-            case -1:							\
-              DPRINT(("mbrtowc: error %d: %s.\n", errno,		\
-		strerror(errno)));					\
-              xfree(wregex);						\
-              return REG_BADPAT;					\
-            case -2:							\
-              consumed = n;						\
-              break;							\
-            }								\
-          regex += consumed;						\
-          n -= consumed;						\
-          wcptr++;							\
-        }								\
-      wlen = wcptr - wregex;						\
-    }									\
+    if (TRE_MB_CUR_MAX == 1)						\
+      {									\
+	unsigned int i;							\
+	const unsigned char *str = (const unsigned char *)regex;	\
+	tre_char_t *wstr = wregex;					\
+									\
+	for (i = 0; i < n; i++)						\
+	  *(wstr++) = *(str++);						\
+	wlen = n;							\
+      }									\
+    else								\
+      {									\
+	int consumed;							\
+	tre_char_t *wcptr = wregex;					\
+	mbstate_t state;						\
+	memset(&state, '\0', sizeof(state));				\
+	while (n > 0)							\
+	  {								\
+	    consumed = tre_mbrtowc(wcptr, regex, n, &state);		\
+									\
+	    switch (consumed)						\
+	      {								\
+		case 0:							\
+		  if (*regex == '\0')					\
+		    consumed = 1;					\
+		  else							\
+		    {							\
+		      xfree(wregex);					\
+		      return REG_BADPAT;				\
+		    }							\
+		  break;						\
+		case -1:						\
+		  DPRINT(("mbrtowc: error %d: %s.\n", errno,		\
+		  strerror(errno)));					\
+		  xfree(wregex);					\
+		  return REG_BADPAT;					\
+		case -2:						\
+		  consumed = n;						\
+		  break;						\
+	      }								\
+	    regex += consumed;						\
+	    n -= consumed;						\
+	    wcptr++;							\
+	}								\
+        wlen = wcptr - wregex;						\
+      }									\
 									\
-  wregex[wlen] = L'\0';
+    wregex[wlen] = L'\0';						\
+  }
 
 int
 tre_fixncomp(fastmatch_t *preg, const char *regex, size_t n, int cflags)
 {
-  CONV_PAT;
+  int ret;
+  tre_char_t *wregex;
+  size_t wlen;
+
+  if (n != 0)
+    CONV_PAT
+  else
+    return tre_compile_literal(preg, NULL, 0, cflags);
 
   ret = tre_compile_literal(preg, wregex, wlen, cflags);
   xfree(wregex);
@@ -109,7 +114,14 @@ tre_fixncomp(fastmatch_t *preg, const ch
 int
 tre_fastncomp(fastmatch_t *preg, const char *regex, size_t n, int cflags)
 {
-  CONV_PAT;
+  int ret;
+  tre_char_t *wregex;
+  size_t wlen;
+
+  if (n != 0)
+    CONV_PAT
+  else
+    return tre_compile_literal(preg, NULL, 0, cflags);
 
   ret = (cflags & REG_LITERAL) ?
     tre_compile_literal(preg, wregex, wlen, cflags) :

Modified: user/gabor/grep/trunk/regex/tre-fastmatch.c
==============================================================================
--- user/gabor/grep/trunk/regex/tre-fastmatch.c	Thu Sep  1 16:43:23 2011	(r225314)
+++ user/gabor/grep/trunk/regex/tre-fastmatch.c	Thu Sep  1 16:47:49 2011	(r225315)
@@ -390,6 +390,8 @@ static int	fastcmp(const void *, const v
   if (n == 0)								\
     {									\
       fg->matchall = true;						\
+      fg->pattern = "";							\
+      fg->wpattern = TRE_CHAR("");					\
       return REG_OK;							\
     }									\
 									\

Modified: user/gabor/grep/trunk/util.c
==============================================================================
--- user/gabor/grep/trunk/util.c	Thu Sep  1 16:43:23 2011	(r225314)
+++ user/gabor/grep/trunk/util.c	Thu Sep  1 16:47:49 2011	(r225315)
@@ -281,74 +281,72 @@ procline(struct str *l, int nottext)
 	unsigned int i;
 	int c = 0, m = 0, r = 0;
 
-	if (!matchall) {
-		/* Loop to process the whole line */
-		while (st <= l->len) {
-			pmatch.rm_so = st;
-			pmatch.rm_eo = l->len;
-
-			/* Loop to compare with all the patterns */
-			for (i = 0; i < patterns; i++) {
-				if (fg_pattern[i].pattern)
-					r = fastexec(&fg_pattern[i],
-					    l->dat, 1, &pmatch, eflags);
-				else
-					r = regexec(&r_pattern[i], l->dat, 1,
-					    &pmatch, eflags);
-				r = (r == 0) ? 0 : REG_NOMATCH;
-				st = (cflags & REG_NOSUB)
-					? (size_t)l->len
-					: (size_t)pmatch.rm_eo;
-				if (r == REG_NOMATCH)
-					continue;
-				/* Check for full match */
-				if (r == 0 && xflag)
-					if (pmatch.rm_so != 0 ||
-					    (size_t)pmatch.rm_eo != l->len)
-						r = REG_NOMATCH;
-				/* Check for whole word match */
-				if (r == 0 && (wflag || fg_pattern[i].word)) {
-					wint_t wbegin, wend;
-
-					wbegin = wend = L' ';
-					if (pmatch.rm_so != 0 &&
-					    sscanf(&l->dat[pmatch.rm_so - 1],
-					    "%lc", &wbegin) != 1)
-						r = REG_NOMATCH;
-					else if ((size_t)pmatch.rm_eo !=
-					    l->len &&
-					    sscanf(&l->dat[pmatch.rm_eo],
-					    "%lc", &wend) != 1)
-						r = REG_NOMATCH;
-					else if (iswword(wbegin) ||
-					    iswword(wend))
-						r = REG_NOMATCH;
-				}
-				if (r == 0) {
-					if (m == 0)
-						c++;
-					if (m < MAX_LINE_MATCHES)
-						matches[m++] = pmatch;
-					/* matches - skip further patterns */
-					if ((color == NULL && !oflag) ||
-					    qflag || lflag)
-						break;
-				}
+	/* Loop to process the whole line */
+	while (st <= l->len) {
+		pmatch.rm_so = st;
+		pmatch.rm_eo = l->len;
+
+		/* Loop to compare with all the patterns */
+		for (i = 0; i < patterns; i++) {
+			if (fg_pattern[i].pattern)
+				r = fastexec(&fg_pattern[i],
+				    l->dat, 1, &pmatch, eflags);
+			else
+				r = regexec(&r_pattern[i], l->dat, 1,
+				    &pmatch, eflags);
+			r = (r == 0) ? 0 : REG_NOMATCH;
+			st = (cflags & REG_NOSUB)
+				? (size_t)l->len
+				: (size_t)pmatch.rm_eo;
+			if (r == REG_NOMATCH)
+				continue;
+			/* Check for full match */
+			if (r == 0 && xflag)
+				if (pmatch.rm_so != 0 ||
+				    (size_t)pmatch.rm_eo != l->len)
+					r = REG_NOMATCH;
+			/* Check for whole word match */
+			if (r == 0 && (wflag || fg_pattern[i].word)) {
+				wint_t wbegin, wend;
+
+				wbegin = wend = L' ';
+				if (pmatch.rm_so != 0 &&
+				    sscanf(&l->dat[pmatch.rm_so - 1],
+				    "%lc", &wbegin) != 1)
+					r = REG_NOMATCH;
+				else if ((size_t)pmatch.rm_eo !=
+				    l->len &&
+				    sscanf(&l->dat[pmatch.rm_eo],
+				    "%lc", &wend) != 1)
+					r = REG_NOMATCH;
+				else if (iswword(wbegin) ||
+				    iswword(wend))
+					r = REG_NOMATCH;
 			}
-
-			if (vflag) {
-				c = !c;
-				break;
+			if (r == 0) {
+				if (m == 0)
+					c++;
+				if (m < MAX_LINE_MATCHES)
+					matches[m++] = pmatch;
+				/* matches - skip further patterns */
+				if ((color == NULL && !oflag) ||
+				    qflag || lflag)
+					break;
 			}
-			/* One pass if we are not recording matches */
-			if ((color == NULL && !oflag) || qflag || lflag)
-				break;
+		}
 
-			if (st == (size_t)pmatch.rm_so)
-				break; 	/* No matches */
+		if (vflag) {
+			c = !c;
+			break;
 		}
-	} else
-		c = !vflag;
+
+		/* One pass if we are not recording matches */
+		if ((color == NULL && !oflag) || qflag || lflag)
+			break;
+
+		if (st == (size_t)pmatch.rm_so)
+			break; 	/* No matches */
+	}
 
 	if (c && binbehave == BINFILE_BIN && nottext)
 		return (c); /* Binary file */



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201109011647.p81GloFp059514>