ref-filter: introduce align_atom_parser()

Introduce align_atom_parser() which will parse an 'align' atom and store the required alignment position and width in the 'used_atom' structure for further usage in populate_value(). Since this patch removes the last usage of match_atom_name(), remove the function from ref-filter.c. Helped-by: Eric Sunshine <sunshine@sunshineco.com> Helped-by: Jeff King <peff@peff.net> Helped-by: Ramsay Jones <ramsay@ramsayjones.plus.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 5bd881d998d6475b8a866af00b1a4d4e6b04c2cc
1 file changed +40 -51
ref-filter.c
+40 -51
@@ -16,6 +16,11 @@
16
17 typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type;
18
19 +struct align {
20 + align_type position;
21 + unsigned int width;
22 +};
23 +
24 /*
25 * An atom is a valid field atom listed below, possibly prefixed with
26 * a "*" to denote deref_tag().
@@ -31,6 +36,7 @@ static struct used_atom {
36 cmp_type type;
37 union {
38 char color[COLOR_MAXLEN];
39 + struct align align;
40 } u;
41 } *used_atom;
42 static int used_atom_cnt, need_tagged, need_symref;
@@ -55,6 +61,37 @@ static align_type parse_align_position(const char *s)
61 return -1;
62 }
63
64 +static void align_atom_parser(struct used_atom *atom, const char *arg)
65 +{
66 + struct align *align = &atom->u.align;
67 + struct string_list params = STRING_LIST_INIT_DUP;
68 + int i;
69 + unsigned int width = ~0U;
70 +
71 + if (!arg)
72 + die(_("expected format: %%(align:<width>,<position>)"));
73 +
74 + align->position = ALIGN_LEFT;
75 +
76 + string_list_split(&params, arg, ',', -1);
77 + for (i = 0; i < params.nr; i++) {
78 + const char *s = params.items[i].string;
79 + int position;
80 +
81 + if (!strtoul_ui(s, 10, &width))
82 + ;
83 + else if ((position = parse_align_position(s)) >= 0)
84 + align->position = position;
85 + else
86 + die(_("unrecognized %%(align) argument: %s"), s);
87 + }
88 +
89 + if (width == ~0U)
90 + die(_("positive width expected with the %%(align) atom"));
91 + align->width = width;
92 + string_list_clear(&params, 0);
93 +}
94 +
95 static struct {
96 const char *name;
97 cmp_type cmp_type;
@@ -93,17 +130,12 @@ static struct {
130 { "flag" },
131 { "HEAD" },
132 { "color", FIELD_STR, color_atom_parser },
96 - { "align" },
133 + { "align", FIELD_STR, align_atom_parser },
134 { "end" },
135 };
136
137 #define REF_FORMATTING_STATE_INIT { 0, NULL }
138
102 -struct align {
103 - align_type position;
104 - unsigned int width;
105 -};
106 -
139 struct contents {
140 unsigned int lines;
141 struct object_id oid;
@@ -288,22 +320,6 @@ static void end_atom_handler(struct atom_value *atomv, struct ref_formatting_sta
320 pop_stack_element(&state->stack);
321 }
322
291 -static int match_atom_name(const char *name, const char *atom_name, const char **val)
292 -{
293 - const char *body;
294 -
295 - if (!skip_prefix(name, atom_name, &body))
296 - return 0; /* doesn't even begin with "atom_name" */
297 - if (!body[0]) {
298 - *val = NULL; /* %(atom_name) and no customization */
299 - return 1;
300 - }
301 - if (body[0] != ':')
302 - return 0; /* "atom_namefoo" is not "atom_name" or "atom_name:..." */
303 - *val = body + 1; /* "atom_name:val" */
304 - return 1;
305 -}
306 -
323 /*
324 * In a format string, find the next occurrence of %(atom).
325 */
@@ -845,7 +861,6 @@ static void populate_value(struct ref_array_item *ref)
861 int deref = 0;
862 const char *refname;
863 const char *formatp;
848 - const char *valp;
864 struct branch *branch = NULL;
865
866 v->handler = append_atom;
@@ -909,34 +924,8 @@ static void populate_value(struct ref_array_item *ref)
924 else
925 v->s = " ";
926 continue;
912 - } else if (match_atom_name(name, "align", &valp)) {
913 - struct align *align = &v->u.align;
914 - struct string_list params = STRING_LIST_INIT_DUP;
915 - int i;
916 - int width = -1;
917 -
918 - if (!valp)
919 - die(_("expected format: %%(align:<width>,<position>)"));
920 -
921 - align->position = ALIGN_LEFT;
922 -
923 - string_list_split(&params, valp, ',', -1);
924 - for (i = 0; i < params.nr; i++) {
925 - const char *s = params.items[i].string;
926 - int position;
927 -
928 - if (!strtoul_ui(s, 10, (unsigned int *)&width))
929 - ;
930 - else if ((position = parse_align_position(s)) >= 0)
931 - align->position = position;
932 - else
933 - die(_("improper format entered align:%s"), s);
934 - }
935 -
936 - if (width < 0)
937 - die(_("positive width expected with the %%(align) atom"));
938 - align->width = width;
939 - string_list_clear(&params, 0);
927 + } else if (starts_with(name, "align")) {
928 + v->u.align = atom->u.align;
929 v->handler = align_atom_handler;
930 continue;
931 } else if (!strcmp(name, "end")) {