ref-filter: introduce parsing functions for each valid atom

Parsing atoms is done in populate_value(), this is repetitive and hence expensive. Introduce a parsing function which would let us parse atoms beforehand and store the required details into the 'used_atom' structure for further usage. Helped-by: Eric Sunshine <sunshine@sunshineco.com> Helped-by: Ramsay Jones <ramsay@ramsayjones.plus.com> Helped-by: Andreas Schwab <schwab@linux-m68k.org> Signed-off-by: Karthik Nayak <Karthik.188@gmail.com> Reviewed-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Karthik Nayak committed Feb 17, 2016 at 23:36 UTC 4de707ea4f61f9df7d2a76303df00caa5ec0899d
1 file changed +10 -4
ref-filter.c
+10 -4
@@ -36,6 +36,7 @@ static int need_color_reset_at_eol;
36 static struct {
37 const char *name;
38 cmp_type cmp_type;
39 + void (*parser)(struct used_atom *atom, const char *arg);
40 } valid_atom[] = {
41 { "refname" },
42 { "objecttype" },
@@ -114,6 +115,7 @@ struct atom_value {
115 int parse_ref_filter_atom(const char *atom, const char *ep)
116 {
117 const char *sp;
118 + const char *arg;
119 int i, at;
120
121 sp = atom;
@@ -132,16 +134,16 @@ int parse_ref_filter_atom(const char *atom, const char *ep)
134 /* Is the atom a valid one? */
135 for (i = 0; i < ARRAY_SIZE(valid_atom); i++) {
136 int len = strlen(valid_atom[i].name);
137 +
138 /*
139 * If the atom name has a colon, strip it and everything after
140 * it off - it specifies the format for this entry, and
141 * shouldn't be used for checking against the valid_atom
142 * table.
143 */
141 - const char *formatp = strchr(sp, ':');
142 - if (!formatp || ep < formatp)
143 - formatp = ep;
144 - if (len == formatp - sp && !memcmp(valid_atom[i].name, sp, len))
144 + arg = memchr(sp, ':', ep - sp);
145 + if (len == (arg ? arg : ep) - sp &&
146 + !memcmp(valid_atom[i].name, sp, len))
147 break;
148 }
149
@@ -154,6 +156,10 @@ int parse_ref_filter_atom(const char *atom, const char *ep)
156 REALLOC_ARRAY(used_atom, used_atom_cnt);
157 used_atom[at].name = xmemdupz(atom, ep - atom);
158 used_atom[at].type = valid_atom[i].cmp_type;
159 + if (arg)
160 + arg = used_atom[at].name + (arg - atom) + 1;
161 + if (valid_atom[i].parser)
162 + valid_atom[i].parser(&used_atom[at], arg);
163 if (*atom == '*')
164 need_tagged = 1;
165 if (!strcmp(used_atom[at].name, "symref"))