git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v12 02/13] ref-filter: introduce ref_formatting_state

From
Karthik Nayak <karthik.188@gmail.com>
Date
Aug 18, 2015, 18:37 UTC
Message-ID
<1439923052-7373-3-git-send-email-Karthik.188@gmail.com>
In-Reply-To
<1439923052-7373-1-git-send-email-Karthik.188@gmail.com>

Introduce ref_formatting_state which will hold the formatted output strbuf instead of directly printing to stdout. This will help us in creating modifier atoms which modify the format specified before printing to stdout.

Rename some functions to reflect the changes made: print_value() -> append_atom() emit() -> append_literal()

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>
---
 ref-filter.c | 44 +++++++++++++++++++++++++-------------------
 1 file changed, 25 insertions(+), 19 deletions(-)
diff --git a/ref-filter.c b/ref-filter.c
index e53c77e..dd62640 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -55,6 +55,11 @@ static struct {
 	{ "color" },
 };
 
+struct ref_formatting_state {
+	struct strbuf output;
+	int quote_style;
+};
+
 struct atom_value {
 	const char *s;
 	unsigned long ul; /* used for sorting when not FIELD_STR */
@@ -1195,30 +1200,25 @@ 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 print_value(struct atom_value *v, int quote_style)
+static void append_atom(struct atom_value *v, struct ref_formatting_state *state)
 {
-	struct strbuf sb = STRBUF_INIT;
-	switch (quote_style) {
+	switch (state->quote_style) {
 	case QUOTE_NONE:
-		fputs(v->s, stdout);
+		strbuf_addstr(&state->output, v->s);
 		break;
 	case QUOTE_SHELL:
-		sq_quote_buf(&sb, v->s);
+		sq_quote_buf(&state->output, v->s);
 		break;
 	case QUOTE_PERL:
-		perl_quote_buf(&sb, v->s);
+		perl_quote_buf(&state->output, v->s);
 		break;
 	case QUOTE_PYTHON:
-		python_quote_buf(&sb, v->s);
+		python_quote_buf(&state->output, v->s);
 		break;
 	case QUOTE_TCL:
-		tcl_quote_buf(&sb, v->s);
+		tcl_quote_buf(&state->output, v->s);
 		break;
 	}
-	if (quote_style != QUOTE_NONE) {
-		fputs(sb.buf, stdout);
-		strbuf_release(&sb);
-	}
 }
 
 static int hex1(char ch)
@@ -1239,7 +1239,7 @@ static int hex2(const char *cp)
 		return -1;
 }
 
-static void emit(const char *cp, const char *ep)
+static void append_literal(const char *cp, const char *ep, struct ref_formatting_state *state)
 {
 	while (*cp && (!ep || cp < ep)) {
 		if (*cp == '%') {
@@ -1248,13 +1248,13 @@ static void emit(const char *cp, const char *ep)
 			else {
 				int ch = hex2(cp + 1);
 				if (0 <= ch) {
-					putchar(ch);
+					strbuf_addch(&state->output, ch);
 					cp += 3;
 					continue;
 				}
 			}
 		}
-		putchar(*cp);
+		strbuf_addch(&state->output, *cp);
 		cp++;
 	}
 }
@@ -1262,19 +1262,23 @@ static void emit(const char *cp, const char *ep)
 void show_ref_array_item(struct ref_array_item *info, const char *format, int quote_style)
 {
 	const char *cp, *sp, *ep;
+	struct ref_formatting_state state;
+
+	strbuf_init(&state.output, 0);
+	state.quote_style = quote_style;
 
 	for (cp = format; *cp && (sp = find_next(cp)); cp = ep + 1) {
 		struct atom_value *atomv;
 
 		ep = strchr(sp, ')');
 		if (cp < sp)
-			emit(cp, sp);
+			append_literal(cp, sp, &state);
 		get_ref_atom_value(info, parse_ref_filter_atom(sp + 2, ep), &atomv);
-		print_value(atomv, quote_style);
+		append_atom(atomv, &state);
 	}
 	if (*cp) {
 		sp = cp + strlen(cp);
-		emit(cp, sp);
+		append_literal(cp, sp, &state);
 	}
 	if (need_color_reset_at_eol) {
 		struct atom_value resetv;
@@ -1283,9 +1287,11 @@ 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;
-		print_value(&resetv, quote_style);
+		append_atom(&resetv, &state);
 	}
+	fwrite(state.output.buf, 1, state.output.len, stdout);
 	putchar('\n');
+	strbuf_release(&state.output);
 }
 
 /*  If no sorting option is given, use refname to sort as default */
-- 
2.5.0
Previous: Karthik NayakNext: Karthik Nayak
Message 5 of 38 in “port tag.c to use ref-filter.c APIs”
  1. 00/13 port tag.c to use ref-filter.c APIsKarthik Nayak, Aug 18, 2015
  2. 01/13 ref-filter: move `struct atom_value` to ref-filter.cKarthik Nayak, Aug 18, 2015
  3. Matthieu MoyAug 19, 2015
  4. Karthik NayakAug 19, 2015
  5. 02/13 ref-filter: introduce ref_formatting_stateKarthik Nayak, Aug 18, 2015
  6. 03/13 ref-filter: introduce the ref_formatting_state stack machineryKarthik Nayak, Aug 18, 2015
  7. Matthieu MoyAug 19, 2015
  8. Karthik NayakAug 19, 2015
  9. Matthieu MoyAug 19, 2015
  10. Karthik NayakAug 19, 2015
  11. Karthik NayakAug 19, 2015
  12. Matthieu MoyAug 20, 2015
  13. Karthik NayakAug 20, 2015
  14. Junio C HamanoAug 20, 2015
  15. Matthieu MoyAug 20, 2015
  16. Junio C HamanoAug 20, 2015
  17. Junio C HamanoAug 19, 2015
  18. Karthik NayakAug 20, 2015
  19. Junio C HamanoAug 20, 2015
  20. 04/13 utf8: add function to align a string into given strbufKarthik Nayak, Aug 18, 2015
  21. 05/13 ref-filter: implement an `align` atomKarthik Nayak, Aug 18, 2015
  22. Karthik NayakAug 18, 2015
  23. Eric SunshineAug 20, 2015
  24. Karthik NayakAug 21, 2015
  25. 06/13 ref-filter: add option to filter out tags, branches and remotesKarthik Nayak, Aug 18, 2015
  26. 07/13 ref-filter: support printing N lines from tag annotationKarthik Nayak, Aug 18, 2015
  27. 08/13 ref-filter: add support to sort by versionKarthik Nayak, Aug 18, 2015
  28. 09/13 ref-filter: add option to match literal patternKarthik Nayak, Aug 18, 2015
  29. 10/13 tag.c: use 'ref-filter' data structuresKarthik Nayak, Aug 18, 2015
  30. Matthieu MoyAug 19, 2015
  31. Karthik NayakAug 19, 2015
  32. 11/13 tag.c: use 'ref-filter' APIsKarthik Nayak, Aug 18, 2015
  33. 12/13 tag.c: implement '--format' optionKarthik Nayak, Aug 18, 2015
  34. Matthieu MoyAug 19, 2015
  35. Karthik NayakAug 20, 2015
  36. 13/13 tag.c: implement '--merged' and '--no-merged' optionsKarthik Nayak, Aug 18, 2015
  37. Eric SunshineAug 18, 2015
  38. Karthik NayakAug 18, 2015

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.