bug-grep
[Top][All Lists]
Advanced

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

[bug-grep] search.c clean-up


From: Jim Meyering
Subject: [bug-grep] search.c clean-up
Date: Mon, 22 Nov 2004 22:51:41 +0100

Here's a little change I wrote months ago.
The only problem is that it relies on mempcpy which is not
always available, so this will have to wait until gnulib
support is brought up to date.

The tests pass with this change.

2004-11-22  Jim Meyering  <address@hidden>

        * src/search.c (WRAP_PATTERN): New macro to factor out duplication...
        (Gcompile): ...here,
        (Ecompile): ...and here.

Index: src/search.c
===================================================================
RCS file: /cvsroot/grep/grep/src/search.c,v
retrieving revision 1.29
diff -u -p -r1.29 search.c
--- src/search.c        22 Nov 2004 13:40:56 -0000      1.29
+++ src/search.c        22 Nov 2004 21:52:24 -0000
@@ -1,5 +1,5 @@
 /* search.c - searching subroutines using dfa, kwset and regex for grep.
-   Copyright 1992, 1998, 2000 Free Software Foundation, Inc.
+   Copyright 1992, 1998, 2000, 2004 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -232,20 +232,25 @@ Gcompile (char const *pattern, size_t si
         In the whole-line case, we use the pattern:
         ^\(userpattern\)$.  */
 
-      static char const line_beg[] = "^\\(";
-      static char const line_end[] = "\\)$";
-      static char const word_beg[] = "\\(^\\|[^[:alnum:]_]\\)\\(";
-      static char const word_end[] = "\\)\\([^[:alnum:]_]\\|$\\)";
-      char *n = xmalloc (sizeof word_beg - 1 + size + sizeof word_end);
-      size_t i;
-      strcpy (n, match_lines ? line_beg : word_beg);
-      i = strlen (n);
-      memcpy (n + i, pattern, size);
-      i += size;
-      strcpy (n + i, match_lines ? line_end : word_end);
-      i += strlen (n + i);
-      pattern = n;
-      size = i;
+#define WRAP_PATTERN(Pattern, Pat_size, Prefix, Suffix)                        
        \
+  do                                                                           
\
+    {                                                                          
\
+      size_t new_size = strlen (Prefix) + (Pat_size) + strlen (Suffix) + 1;    
\
+      char *new_pat = xmalloc (new_size);                                      
\
+      mempcpy (mempcpy (mempcpy (new_pat, Prefix, strlen (Prefix)),            
\
+                       Pattern, Pat_size),                                     
\
+              Suffix, strlen (Suffix));                                        
\
+      size = new_size - 1;                                                     
\
+      Pattern = new_pat;                                                       
\
+    }                                                                          
\
+  while (0)
+
+      if (match_lines)
+       WRAP_PATTERN (pattern, size, "^\\(", "\\)$");
+      else
+       WRAP_PATTERN (pattern, size,
+                     "\\(^\\|[^[:alnum:]_]\\)\\(",
+                     "\\)\\([^[:alnum:]_]\\|$\\)");
     }
 
   dfacomp (pattern, size, &dfa, 1);
@@ -315,20 +320,12 @@ Ecompile (char const *pattern, size_t si
         In the whole-line case, we use the pattern:
         ^(userpattern)$.  */
 
-      static char const line_beg[] = "^(";
-      static char const line_end[] = ")$";
-      static char const word_beg[] = "(^|[^[:alnum:]_])(";
-      static char const word_end[] = ")([^[:alnum:]_]|$)";
-      char *n = xmalloc (sizeof word_beg - 1 + size + sizeof word_end);
-      size_t i;
-      strcpy (n, match_lines ? line_beg : word_beg);
-      i = strlen(n);
-      memcpy (n + i, pattern, size);
-      i += size;
-      strcpy (n + i, match_lines ? line_end : word_end);
-      i += strlen (n + i);
-      pattern = n;
-      size = i;
+      if (match_lines)
+       WRAP_PATTERN (pattern, size, "^(", ")$");
+      else
+       WRAP_PATTERN (pattern, size,
+                     "(^|[^[:alnum:]_])(",
+                     ")([^[:alnum:]_]|$)");
     }
 
   dfacomp (pattern, size, &dfa, 1);




reply via email to

[Prev in Thread] Current Thread [Next in Thread]