grep: factor out create_grep_pat()
authorRené Scharfe <rene.scharfe@lsrfire.ath.cx>
Sun, 20 May 2012 14:32:39 +0000 (16:32 +0200)
committerJunio C Hamano <gitster@pobox.com>
Sun, 20 May 2012 22:12:22 +0000 (15:12 -0700)
Add create_grep_pat(), a shared helper for all grep pattern allocation
and initialization needs.

Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
grep.c

diff --git a/grep.c b/grep.c
index d03d9e24c23eff2d60ae7226a412f3ccf66670fd..2b77c477643b825cb2ea46a0bfbddd2da32b8bea 100644 (file)
--- a/grep.c
+++ b/grep.c
@@ -3,15 +3,26 @@
 #include "userdiff.h"
 #include "xdiff-interface.h"
 
-void append_header_grep_pattern(struct grep_opt *opt, enum grep_header_field field, const char *pat)
+static struct grep_pat *create_grep_pat(const char *pat, size_t patlen,
+                                       const char *origin, int no,
+                                       enum grep_pat_token t,
+                                       enum grep_header_field field)
 {
        struct grep_pat *p = xcalloc(1, sizeof(*p));
        p->pattern = pat;
-       p->patternlen = strlen(pat);
-       p->origin = "header";
-       p->no = 0;
-       p->token = GREP_PATTERN_HEAD;
+       p->patternlen = patlen;
+       p->origin = origin;
+       p->no = no;
+       p->token = t;
        p->field = field;
+       return p;
+}
+
+void append_header_grep_pattern(struct grep_opt *opt,
+                               enum grep_header_field field, const char *pat)
+{
+       struct grep_pat *p = create_grep_pat(pat, strlen(pat), "header", 0,
+                                            GREP_PATTERN_HEAD, field);
        *opt->header_tail = p;
        opt->header_tail = &p->next;
        p->next = NULL;
@@ -26,12 +37,7 @@ void append_grep_pattern(struct grep_opt *opt, const char *pat,
 void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen,
                     const char *origin, int no, enum grep_pat_token t)
 {
-       struct grep_pat *p = xcalloc(1, sizeof(*p));
-       p->pattern = pat;
-       p->patternlen = patlen;
-       p->origin = origin;
-       p->no = no;
-       p->token = t;
+       struct grep_pat *p = create_grep_pat(pat, patlen, origin, no, t, 0);
        *opt->pattern_tail = p;
        opt->pattern_tail = &p->next;
        p->next = NULL;