Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/grep Make the matchall case a full short cut.



details:   https://anonhg.NetBSD.org/src/rev/50dac757e68b
branches:  trunk
changeset: 779149:50dac757e68b
user:      joerg <joerg%NetBSD.org@localhost>
date:      Sun May 06 21:56:08 2012 +0000

description:
Make the matchall case a full short cut.

diffstat:

 usr.bin/grep/util.c |  136 +++++++++++++++++++++++++++------------------------
 1 files changed, 72 insertions(+), 64 deletions(-)

diffs (167 lines):

diff -r dd3dcdec6fb6 -r 50dac757e68b usr.bin/grep/util.c
--- a/usr.bin/grep/util.c       Sun May 06 19:46:18 2012 +0000
+++ b/usr.bin/grep/util.c       Sun May 06 21:56:08 2012 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: util.c,v 1.13 2011/04/18 23:22:42 joerg Exp $  */
+/*     $NetBSD: util.c,v 1.14 2012/05/06 21:56:08 joerg Exp $  */
 /*     $FreeBSD: head/usr.bin/grep/util.c 211496 2010-08-19 09:28:59Z des $    */
 /*     $OpenBSD: util.c,v 1.39 2010/07/02 22:18:03 tedu Exp $  */
 
@@ -34,7 +34,7 @@
 #endif
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: util.c,v 1.13 2011/04/18 23:22:42 joerg Exp $");
+__RCSID("$NetBSD: util.c,v 1.14 2012/05/06 21:56:08 joerg Exp $");
 
 #include <sys/stat.h>
 #include <sys/types.h>
@@ -295,78 +295,86 @@
        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;
+       if (matchall) {
+               /* Short cut the case of (not) matching wild card pattern */
+               if (vflag)
+                       return (0);
+               if ((binbehave == BINFILE_BIN && nottext) || cflag || qflag ||
+                   lflag || Lflag)
+                       return (1);
+               printline(l, ':', matches, m);
+               return (1);
+       }
 
-                       /* Loop to compare with all the patterns */
-                       for (i = 0; i < patterns; i++) {
+       /* 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++) {
 /*
  * XXX: grep_search() is a workaround for speed up and should be
  * removed in the future.  See fastgrep.c.
  */
-                               if (fg_pattern[i].pattern) {
-                                       r = grep_search(&fg_pattern[i],
-                                           (unsigned char *)l->dat,
-                                           l->len, &pmatch);
-                                       r = (r == 0) ? 0 : REG_NOMATCH;
-                                       st = pmatch.rm_eo;
-                               } else {
-                                       r = regexec(&r_pattern[i], l->dat, 1,
-                                           &pmatch, eflags);
-                                       r = (r == 0) ? 0 : REG_NOMATCH;
-                                       st = 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 && fg_pattern[i].word &&
-                                   pmatch.rm_so != 0) {
-                                       wint_t wbegin, wend;
+                       if (fg_pattern[i].pattern) {
+                               r = grep_search(&fg_pattern[i],
+                                   (unsigned char *)l->dat,
+                                   l->len, &pmatch);
+                               r = (r == 0) ? 0 : REG_NOMATCH;
+                               st = pmatch.rm_eo;
+                       } else {
+                               r = regexec(&r_pattern[i], l->dat, 1,
+                                   &pmatch, eflags);
+                               r = (r == 0) ? 0 : REG_NOMATCH;
+                               st = 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 && fg_pattern[i].word &&
+                           pmatch.rm_so != 0) {
+                               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;
-                               }
+                               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 */



Home | Main Index | Thread Index | Old Index