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

[PATCH v12 03/13] ref-filter: introduce the ref_formatting_state stack machinery

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

Introduce a stack machinery for ref_formatting_state, this allows us to push and pop states. Whenever we pop a state the strbuf from that state is appended into the next state on the stack, this will allow us to support nesting of modifier atoms.

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 | 42 +++++++++++++++++++++++++++++++++---------
 1 file changed, 33 insertions(+), 9 deletions(-)
diff --git a/ref-filter.c b/ref-filter.c
index dd62640..74532d3 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -56,6 +56,7 @@ static struct {
 };
 
 struct ref_formatting_state {
+	struct ref_formatting_state *prev;
 	struct strbuf output;
 	int quote_style;
 };
@@ -134,6 +135,27 @@ int parse_ref_filter_atom(const char *atom, const char *ep)
 	return at;
 }
 
+static void push_new_state(struct ref_formatting_state **stack)
+{
+	struct ref_formatting_state *s = xcalloc(1, sizeof(struct ref_formatting_state));
+
+	strbuf_init(&s->output, 0);
+	s->prev = *stack;
+	*stack = s;
+}
+
+static void pop_state(struct ref_formatting_state **stack)
+{
+	struct ref_formatting_state *current = *stack;
+	struct ref_formatting_state *prev = current->prev;
+
+	if (prev)
+		strbuf_addbuf(&prev->output, &current->output);
+	strbuf_release(&current->output);
+	free(current);
+	*stack = prev;
+}
+
 /*
  * In a format string, find the next occurrence of %(atom).
  */
@@ -1262,23 +1284,24 @@ static void append_literal(const char *cp, const char *ep, struct ref_formatting
 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;
+	struct strbuf *final_buf;
+	struct ref_formatting_state *state = NULL;
 
-	strbuf_init(&state.output, 0);
-	state.quote_style = quote_style;
+	push_new_state(&state);
+	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)
-			append_literal(cp, sp, &state);
+			append_literal(cp, sp, state);
 		get_ref_atom_value(info, parse_ref_filter_atom(sp + 2, ep), &atomv);
-		append_atom(atomv, &state);
+		append_atom(atomv, state);
 	}
 	if (*cp) {
 		sp = cp + strlen(cp);
-		append_literal(cp, sp, &state);
+		append_literal(cp, sp, state);
 	}
 	if (need_color_reset_at_eol) {
 		struct atom_value resetv;
@@ -1287,11 +1310,12 @@ 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;
-		append_atom(&resetv, &state);
+		append_atom(&resetv, state);
 	}
-	fwrite(state.output.buf, 1, state.output.len, stdout);
+	final_buf = &state->output;
+	fwrite(final_buf->buf, 1, final_buf->len, stdout);
+	pop_state(&state);
 	putchar('\n');
-	strbuf_release(&state.output);
 }
 
 /*  If no sorting option is given, use refname to sort as default */
-- 
2.5.0
Previous: Karthik NayakNext: Matthieu Moy
Message 6 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.