From 63d89fbce11c358ab73bdbb0d36919a454b2f5a6 Mon Sep 17 00:00:00 2001 From: Karthik Nayak Date: Thu, 10 Sep 2015 21:18:20 +0530 Subject: [PATCH] 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 Mentored-by: Matthieu Moy Signed-off-by: Karthik Nayak Signed-off-by: Junio C Hamano --- ref-filter.c | 54 +++++++++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/ref-filter.c b/ref-filter.c index 432cea0265..a99321633b 100644 --- a/ref-filter.c +++ b/ref-filter.c @@ -69,6 +69,7 @@ struct ref_formatting_state { struct atom_value { const char *s; + void (*handler)(struct atom_value *atomv, struct ref_formatting_state *state); unsigned long ul; /* used for sorting when not FIELD_STR */ }; @@ -141,6 +142,32 @@ int parse_ref_filter_atom(const char *atom, const char *ep) return at; } +static void quote_formatting(struct strbuf *s, const char *str, int quote_style) +{ + switch (quote_style) { + case QUOTE_NONE: + strbuf_addstr(s, str); + break; + case QUOTE_SHELL: + sq_quote_buf(s, str); + break; + case QUOTE_PERL: + perl_quote_buf(s, str); + break; + case QUOTE_PYTHON: + python_quote_buf(s, str); + break; + case QUOTE_TCL: + tcl_quote_buf(s, str); + break; + } +} + +static void append_atom(struct atom_value *v, struct ref_formatting_state *state) +{ + quote_formatting(&state->stack->output, v->s, state->quote_style); +} + static void push_stack_element(struct ref_formatting_stack **stack) { 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) const char *formatp; struct branch *branch = NULL; + v->handler = append_atom; + if (*name == '*') { deref = 1; name++; @@ -1228,29 +1257,6 @@ void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array) qsort(array->items, array->nr, sizeof(struct ref_array_item *), compare_refs); } -static void append_atom(struct atom_value *v, struct ref_formatting_state *state) -{ - struct strbuf *s = &state->stack->output; - - switch (state->quote_style) { - case QUOTE_NONE: - strbuf_addstr(s, v->s); - break; - case QUOTE_SHELL: - sq_quote_buf(s, v->s); - break; - case QUOTE_PERL: - perl_quote_buf(s, v->s); - break; - case QUOTE_PYTHON: - python_quote_buf(s, v->s); - break; - case QUOTE_TCL: - tcl_quote_buf(s, v->s); - break; - } -} - static int hex1(char ch) { if ('0' <= ch && ch <= '9') @@ -1307,7 +1313,7 @@ void show_ref_array_item(struct ref_array_item *info, const char *format, int qu if (cp < sp) append_literal(cp, sp, &state); get_ref_atom_value(info, parse_ref_filter_atom(sp + 2, ep), &atomv); - append_atom(atomv, &state); + atomv->handler(atomv, &state); } if (*cp) { sp = cp + strlen(cp);