ref-filter: introduce struct used_atom

Introduce the 'used_atom' structure to replace the existing implementation of 'used_atom' (which is a list of atoms). This helps us parse atoms beforehand and store required details into the 'used_atom' for future usage. Helped-by: Eric Sunshine <sunshine@sunshineco.com> 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 b072add7fbcef19bd49d70fe9adad05b2374295e
1 file changed +18 -17
ref-filter.c
+18 -17
@@ -26,8 +26,10 @@ typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type;
26 * indexed with the "atom number", which is an index into this
27 * array.
28 */
29 -static const char **used_atom;
30 -static cmp_type *used_atom_type;
29 +static struct used_atom {
30 + const char *name;
31 + cmp_type type;
32 +} *used_atom;
33 static int used_atom_cnt, need_tagged, need_symref;
34 static int need_color_reset_at_eol;
35
@@ -122,8 +124,8 @@ int parse_ref_filter_atom(const char *atom, const char *ep)
124
125 /* Do we have the atom already used elsewhere? */
126 for (i = 0; i < used_atom_cnt; i++) {
125 - int len = strlen(used_atom[i]);
126 - if (len == ep - atom && !memcmp(used_atom[i], atom, len))
127 + int len = strlen(used_atom[i].name);
128 + if (len == ep - atom && !memcmp(used_atom[i].name, atom, len))
129 return i;
130 }
131
@@ -150,12 +152,11 @@ int parse_ref_filter_atom(const char *atom, const char *ep)
152 at = used_atom_cnt;
153 used_atom_cnt++;
154 REALLOC_ARRAY(used_atom, used_atom_cnt);
153 - REALLOC_ARRAY(used_atom_type, used_atom_cnt);
154 - used_atom[at] = xmemdupz(atom, ep - atom);
155 - used_atom_type[at] = valid_atom[i].cmp_type;
155 + used_atom[at].name = xmemdupz(atom, ep - atom);
156 + used_atom[at].type = valid_atom[i].cmp_type;
157 if (*atom == '*')
158 need_tagged = 1;
158 - if (!strcmp(used_atom[at], "symref"))
159 + if (!strcmp(used_atom[at].name, "symref"))
160 need_symref = 1;
161 return at;
162 }
@@ -315,7 +316,7 @@ int verify_ref_format(const char *format)
316 at = parse_ref_filter_atom(sp + 2, ep);
317 cp = ep + 1;
318
318 - if (skip_prefix(used_atom[at], "color:", &color))
319 + if (skip_prefix(used_atom[at].name, "color:", &color))
320 need_color_reset_at_eol = !!strcmp(color, "reset");
321 }
322 return 0;
@@ -359,7 +360,7 @@ static void grab_common_values(struct atom_value *val, int deref, struct object
360 int i;
361
362 for (i = 0; i < used_atom_cnt; i++) {
362 - const char *name = used_atom[i];
363 + const char *name = used_atom[i].name;
364 struct atom_value *v = &val[i];
365 if (!!deref != (*name == '*'))
366 continue;
@@ -383,7 +384,7 @@ static void grab_tag_values(struct atom_value *val, int deref, struct object *ob
384 struct tag *tag = (struct tag *) obj;
385
386 for (i = 0; i < used_atom_cnt; i++) {
386 - const char *name = used_atom[i];
387 + const char *name = used_atom[i].name;
388 struct atom_value *v = &val[i];
389 if (!!deref != (*name == '*'))
390 continue;
@@ -405,7 +406,7 @@ static void grab_commit_values(struct atom_value *val, int deref, struct object
406 struct commit *commit = (struct commit *) obj;
407
408 for (i = 0; i < used_atom_cnt; i++) {
408 - const char *name = used_atom[i];
409 + const char *name = used_atom[i].name;
410 struct atom_value *v = &val[i];
411 if (!!deref != (*name == '*'))
412 continue;
@@ -535,7 +536,7 @@ static void grab_person(const char *who, struct atom_value *val, int deref, stru
536 const char *wholine = NULL;
537
538 for (i = 0; i < used_atom_cnt; i++) {
538 - const char *name = used_atom[i];
539 + const char *name = used_atom[i].name;
540 struct atom_value *v = &val[i];
541 if (!!deref != (*name == '*'))
542 continue;
@@ -573,7 +574,7 @@ static void grab_person(const char *who, struct atom_value *val, int deref, stru
574 if (!wholine)
575 return;
576 for (i = 0; i < used_atom_cnt; i++) {
576 - const char *name = used_atom[i];
577 + const char *name = used_atom[i].name;
578 struct atom_value *v = &val[i];
579 if (!!deref != (*name == '*'))
580 continue;
@@ -663,7 +664,7 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct obj
664 unsigned long sublen = 0, bodylen = 0, nonsiglen = 0, siglen = 0;
665
666 for (i = 0; i < used_atom_cnt; i++) {
666 - const char *name = used_atom[i];
667 + const char *name = used_atom[i].name;
668 struct atom_value *v = &val[i];
669 const char *valp = NULL;
670 if (!!deref != (*name == '*'))
@@ -809,7 +810,7 @@ static void populate_value(struct ref_array_item *ref)
810
811 /* Fill in specials first */
812 for (i = 0; i < used_atom_cnt; i++) {
812 - const char *name = used_atom[i];
813 + const char *name = used_atom[i].name;
814 struct atom_value *v = &ref->value[i];
815 int deref = 0;
816 const char *refname;
@@ -1464,7 +1465,7 @@ static int cmp_ref_sorting(struct ref_sorting *s, struct ref_array_item *a, stru
1465 {
1466 struct atom_value *va, *vb;
1467 int cmp;
1467 - cmp_type cmp_type = used_atom_type[s->atom];
1468 + cmp_type cmp_type = used_atom[s->atom].type;
1469
1470 get_ref_atom_value(a, s->atom, &va);
1471 get_ref_atom_value(b, s->atom, &vb);