ref-filter: introduce handler function for each atom

Introduce a handler function for each atom, which is called when the atom is processed in show_ref_array_item(). In this context make append_atom() as the default handler function and extract quote_formatting() out of append_atom(). Bump this to the top. Mentored-by: Christian Couder <christian.couder@gmail.com> Mentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr> Signed-off-by: Karthik Nayak <karthik.188@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Karthik Nayak committed Sep 10, 2015 at 21:18 UTC 63d89fbce11c358ab73bdbb0d36919a454b2f5a6
1 file changed +30 -24
ref-filter.c
+30 -24
@@ -69,6 +69,7 @@ struct ref_formatting_state {
69
70 struct atom_value {
71 const char *s;
72 + void (*handler)(struct atom_value *atomv, struct ref_formatting_state *state);
73 unsigned long ul; /* used for sorting when not FIELD_STR */
74 };
75
@@ -141,6 +142,32 @@ int parse_ref_filter_atom(const char *atom, const char *ep)
142 return at;
143 }
144
145 +static void quote_formatting(struct strbuf *s, const char *str, int quote_style)
146 +{
147 + switch (quote_style) {
148 + case QUOTE_NONE:
149 + strbuf_addstr(s, str);
150 + break;
151 + case QUOTE_SHELL:
152 + sq_quote_buf(s, str);
153 + break;
154 + case QUOTE_PERL:
155 + perl_quote_buf(s, str);
156 + break;
157 + case QUOTE_PYTHON:
158 + python_quote_buf(s, str);
159 + break;
160 + case QUOTE_TCL:
161 + tcl_quote_buf(s, str);
162 + break;
163 + }
164 +}
165 +
166 +static void append_atom(struct atom_value *v, struct ref_formatting_state *state)
167 +{
168 + quote_formatting(&state->stack->output, v->s, state->quote_style);
169 +}
170 +
171 static void push_stack_element(struct ref_formatting_stack **stack)
172 {
173 struct ref_formatting_stack *s = xcalloc(1, sizeof(struct ref_formatting_stack));
@@ -662,6 +689,8 @@ static void populate_value(struct ref_array_item *ref)
689 const char *formatp;
690 struct branch *branch = NULL;
691
692 + v->handler = append_atom;
693 +
694 if (*name == '*') {
695 deref = 1;
696 name++;
@@ -1228,29 +1257,6 @@ void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array)
1257 qsort(array->items, array->nr, sizeof(struct ref_array_item *), compare_refs);
1258 }
1259
1231 -static void append_atom(struct atom_value *v, struct ref_formatting_state *state)
1232 -{
1233 - struct strbuf *s = &state->stack->output;
1234 -
1235 - switch (state->quote_style) {
1236 - case QUOTE_NONE:
1237 - strbuf_addstr(s, v->s);
1238 - break;
1239 - case QUOTE_SHELL:
1240 - sq_quote_buf(s, v->s);
1241 - break;
1242 - case QUOTE_PERL:
1243 - perl_quote_buf(s, v->s);
1244 - break;
1245 - case QUOTE_PYTHON:
1246 - python_quote_buf(s, v->s);
1247 - break;
1248 - case QUOTE_TCL:
1249 - tcl_quote_buf(s, v->s);
1250 - break;
1251 - }
1252 -}
1253 -
1260 static int hex1(char ch)
1261 {
1262 if ('0' <= ch && ch <= '9')
@@ -1307,7 +1313,7 @@ void show_ref_array_item(struct ref_array_item *info, const char *format, int qu
1313 if (cp < sp)
1314 append_literal(cp, sp, &state);
1315 get_ref_atom_value(info, parse_ref_filter_atom(sp + 2, ep), &atomv);
1310 - append_atom(atomv, &state);
1316 + atomv->handler(atomv, &state);
1317 }
1318 if (*cp) {
1319 sp = cp + strlen(cp);