Thread (20 messages) 20 messages, 3 authors, 2016-06-15

[PATCH v10 03/13] ref-filter: introduce ref_formatting_state

flat view
STALE3768d

From: Karthik Nayak <hidden>
Date: 2016-06-15 23:06:07
Subsystem: the rest · Maintainer: Linus Torvalds

Revision v10 of 6 in this series.

Revisions (6)
  1. v3 [diff vs current]
  2. v4 [diff vs current]
  3. v10 current
  4. v13 [diff vs current]
  5. v16 [diff vs current]
  6. v17 [diff vs current]
Introduce a ref_formatting_state which will eventually hold the values
of modifier atoms. Implement this within ref-filter.

Mentored-by: Christian Couder [off-list ref]
Mentored-by: Matthieu Moy [off-list ref]
Signed-off-by: Karthik Nayak <redacted>
---
 ref-filter.c | 65 +++++++++++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 47 insertions(+), 18 deletions(-)
diff --git a/ref-filter.c b/ref-filter.c
index 95f027b..e2df209 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -1195,23 +1195,29 @@ 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 format_quote_value(struct atom_value *v, int quote_style, struct strbuf *output)
+
+struct ref_formatting_state {
+	struct strbuf output;
+	int quote_style;
+};
+
+static void format_quote_value(struct atom_value *v, struct ref_formatting_state *state)
 {
-	switch (quote_style) {
+	switch (state->quote_style) {
 	case QUOTE_NONE:
-		strbuf_addstr(output, v->s);
+		strbuf_addstr(&state->output, v->s);
 		break;
 	case QUOTE_SHELL:
-		sq_quote_buf(output, v->s);
+		sq_quote_buf(&state->output, v->s);
 		break;
 	case QUOTE_PERL:
-		perl_quote_buf(output, v->s);
+		perl_quote_buf(&state->output, v->s);
 		break;
 	case QUOTE_PYTHON:
-		python_quote_buf(output, v->s);
+		python_quote_buf(&state->output, v->s);
 		break;
 	case QUOTE_TCL:
-		tcl_quote_buf(output, v->s);
+		tcl_quote_buf(&state->output, v->s);
 		break;
 	}
 }
@@ -1234,7 +1240,7 @@ static int hex2(const char *cp)
 		return -1;
 }
 
-static void append_non_atom(const char *cp, const char *ep, struct strbuf *output)
+static void append_non_atom(const char *cp, const char *ep, struct ref_formatting_state *state)
 {
 	while (*cp && (!ep || cp < ep)) {
 		if (*cp == '%') {
@@ -1243,34 +1249,55 @@ static void append_non_atom(const char *cp, const char *ep, struct strbuf *outpu
 			else {
 				int ch = hex2(cp + 1);
 				if (0 <= ch) {
-					strbuf_addch(output, ch);
+					strbuf_addch(&state->output, ch);
 					cp += 3;
 					continue;
 				}
 			}
 		}
-		strbuf_addch(output, *cp);
+		strbuf_addch(&state->output, *cp);
 		cp++;
 	}
 }
 
+static void set_formatting_state(struct atom_value *atomv, struct ref_formatting_state *state)
+{
+	/* Based on the atomv values, the formatting state is set */
+}
+
+static void perform_state_formatting(struct ref_formatting_state *state, struct strbuf *final)
+{
+	/* More formatting options to be eventually added */
+	strbuf_addbuf(final, &state->output);
+	strbuf_reset(&state->output);
+}
+
 void show_ref_array_item(struct ref_array_item *info, const char *format, int quote_style)
 {
 	const char *cp, *sp, *ep;
-	struct strbuf output = STRBUF_INIT;
+	struct strbuf final_buf = STRBUF_INIT;
+	struct ref_formatting_state state;
+
+	state.quote_style = quote_style;
+	strbuf_init(&state.output, 0);
 
 	for (cp = format; *cp && (sp = find_next(cp)); cp = ep + 1) {
 		struct atom_value *atomv;
 
 		ep = strchr(sp, ')');
-		if (cp < sp)
-			append_non_atom(cp, sp, &output);
+		if (cp < sp) {
+			append_non_atom(cp, sp, &state);
+			perform_state_formatting(&state, &final_buf);
+		}
 		get_ref_atom_value(info, parse_ref_filter_atom(sp + 2, ep), &atomv);
-		format_quote_value(atomv, quote_style, &output);
+		set_formatting_state(atomv, &state);
+		format_quote_value(atomv, &state);
+		perform_state_formatting(&state, &final_buf);
 	}
 	if (*cp) {
 		sp = cp + strlen(cp);
-		append_non_atom(cp, sp, &output);
+		append_non_atom(cp, sp, &state);
+		perform_state_formatting(&state, &final_buf);
 	}
 	if (need_color_reset_at_eol) {
 		struct atom_value resetv;
@@ -1279,11 +1306,13 @@ void show_ref_array_item(struct ref_array_item *info, const char *format, int qu
 		if (color_parse("reset", color) < 0)
 			die("BUG: couldn't parse 'reset' as a color");
 		resetv.s = color;
-		format_quote_value(&resetv, quote_style, &output);
+		format_quote_value(&resetv, &state);
+		perform_state_formatting(&state, &final_buf);
 	}
-	fwrite(output.buf, 1, output.len, stdout);
+	fwrite(final_buf.buf, 1, final_buf.len, stdout);
 	putchar('\n');
-	strbuf_release(&output);
+	strbuf_release(&final_buf);
+	strbuf_release(&state.output);
 }
 
 /*  If no sorting option is given, use refname to sort as default */
-- 
2.5.0
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help