svn commit: r223009 - head/usr.bin/grep

Gabor Kovesdan gabor at FreeBSD.org
Sun Jun 12 12:51:58 UTC 2011


Author: gabor
Date: Sun Jun 12 12:51:58 2011
New Revision: 223009
URL: http://svn.freebsd.org/changeset/base/223009

Log:
  - Use REG_NOSUB to bypass submatch counting when not necessary. This may
    yield in somewhat better performance in a few cases.
  
  Approved by:	delphij (mentor)

Modified:
  head/usr.bin/grep/grep.c
  head/usr.bin/grep/util.c

Modified: head/usr.bin/grep/grep.c
==============================================================================
--- head/usr.bin/grep/grep.c	Sun Jun 12 12:44:02 2011	(r223008)
+++ head/usr.bin/grep/grep.c	Sun Jun 12 12:51:58 2011	(r223009)
@@ -73,7 +73,7 @@ const char	*errstr[] = {
 };
 
 /* Flags passed to regcomp() and regexec() */
-int		 cflags = 0;
+int		 cflags = REG_NOSUB;
 int		 eflags = REG_STARTEND;
 
 /* Shortcut for matching all cases like empty regex */
@@ -519,6 +519,7 @@ main(int argc, char *argv[])
 			break;
 		case 'o':
 			oflag = true;
+			cflags &= ~REG_NOSUB;
 			break;
 		case 'p':
 			linkbehave = LINK_SKIP;
@@ -552,9 +553,11 @@ main(int argc, char *argv[])
 			break;
 		case 'w':
 			wflag = true;
+			cflags &= ~REG_NOSUB;
 			break;
 		case 'x':
 			xflag = true;
+			cflags &= ~REG_NOSUB;
 			break;
 		case 'Z':
 			filebehave = FILE_GZIP;
@@ -588,6 +591,7 @@ main(int argc, char *argv[])
 			    strcasecmp("none", optarg) != 0 &&
 			    strcasecmp("no", optarg) != 0)
 				errx(2, getstr(3), "--color");
+			cflags &= ~REG_NOSUB;
 			break;
 		case LABEL_OPT:
 			label = optarg;

Modified: head/usr.bin/grep/util.c
==============================================================================
--- head/usr.bin/grep/util.c	Sun Jun 12 12:44:02 2011	(r223008)
+++ head/usr.bin/grep/util.c	Sun Jun 12 12:51:58 2011	(r223009)
@@ -309,7 +309,9 @@ procline(struct str *l, int nottext)
 					r = regexec(&r_pattern[i], l->dat, 1,
 					    &pmatch, eflags);
 				r = (r == 0) ? 0 : REG_NOMATCH;
-				st = pmatch.rm_eo;
+				st = (cflags & REG_NOSUB)
+					? (size_t)l->len
+					: (size_t)pmatch.rm_eo;
 				if (r == REG_NOMATCH)
 					continue;
 				/* Check for full match */


More information about the svn-src-all mailing list