diff options
author | Karthik Nayak <karthik.188@gmail.com> | 2015-09-10 21:18:20 +0530 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2015-09-17 10:02:48 -0700 |
commit | 63d89fbce11c358ab73bdbb0d36919a454b2f5a6 (patch) | |
tree | d1d122731252847b42cfee0d60a4bd01ff07e0f0 /ref-filter.c | |
parent | utf8: add function to align a string into given strbuf (diff) | |
download | tgif-63d89fbce11c358ab73bdbb0d36919a454b2f5a6.tar.xz |
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>
Diffstat (limited to 'ref-filter.c')
-rw-r--r-- | ref-filter.c | 54 |
1 files 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); |