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

[PATCH 2/9] for-each-ref: convert to use *_quote_buf instead of _quote_print

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
May 19, 2013, 10:27 UTC
Message-ID
<1368959235-27777-3-git-send-email-pclouds@gmail.com>
In-Reply-To
<1368959235-27777-1-git-send-email-pclouds@gmail.com>
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 builtin/for-each-ref.c | 14 ++++++++++----
 quote.c                | 44 ++++++++++++++++++++++----------------------
 quote.h                |  6 +++---
 3 files changed, 35 insertions(+), 29 deletions(-)
diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c
index 7f059c3..14151b4 100644
--- a/builtin/for-each-ref.c
+++ b/builtin/for-each-ref.c
@@ -867,24 +867,30 @@ static void sort_refs(struct ref_sort *sort, struct refinfo **refs, int num_refs
 static void print_value(struct refinfo *ref, int atom, int quote_style)
 {
 	struct atom_value *v;
+	struct strbuf sb_buf = STRBUF_INIT;
+	struct strbuf *sb = &sb_buf;
 	get_value(ref, atom, &v);
 	switch (quote_style) {
 	case QUOTE_NONE:
 		fputs(v->s, stdout);
 		break;
 	case QUOTE_SHELL:
-		sq_quote_print(stdout, v->s);
+		sq_quote_buf(sb, v->s);
 		break;
 	case QUOTE_PERL:
-		perl_quote_print(stdout, v->s);
+		perl_quote_buf(sb, v->s);
 		break;
 	case QUOTE_PYTHON:
-		python_quote_print(stdout, v->s);
+		python_quote_buf(sb, v->s);
 		break;
 	case QUOTE_TCL:
-		tcl_quote_print(stdout, v->s);
+		tcl_quote_buf(sb, v->s);
 		break;
 	}
+	if (quote_style != QUOTE_NONE) {
+		fputs(sb->buf, stdout);
+		strbuf_release(sb);
+	}
 }
 
 static int hex1(char ch)
diff --git a/quote.c b/quote.c
index c67f5d3..e7a240d 100644
--- a/quote.c
+++ b/quote.c
@@ -454,72 +454,72 @@ int unquote_c_style(struct strbuf *sb, const char *quoted, const char **endp)
 
 /* quoting as a string literal for other languages */
 
-void perl_quote_print(FILE *stream, const char *src)
+void perl_quote_buf(struct strbuf *sb, const char *src)
 {
 	const char sq = '\'';
 	const char bq = '\\';
 	char c;
 
-	fputc(sq, stream);
+	strbuf_addch(sb, sq);
 	while ((c = *src++)) {
 		if (c == sq || c == bq)
-			fputc(bq, stream);
-		fputc(c, stream);
+			strbuf_addch(sb, bq);
+		strbuf_addch(sb, c);
 	}
-	fputc(sq, stream);
+	strbuf_addch(sb, sq);
 }
 
-void python_quote_print(FILE *stream, const char *src)
+void python_quote_buf(struct strbuf *sb, const char *src)
 {
 	const char sq = '\'';
 	const char bq = '\\';
 	const char nl = '\n';
 	char c;
 
-	fputc(sq, stream);
+	strbuf_addch(sb, sq);
 	while ((c = *src++)) {
 		if (c == nl) {
-			fputc(bq, stream);
-			fputc('n', stream);
+			strbuf_addch(sb, bq);
+			strbuf_addch(sb, 'n');
 			continue;
 		}
 		if (c == sq || c == bq)
-			fputc(bq, stream);
-		fputc(c, stream);
+			strbuf_addch(sb, bq);
+		strbuf_addch(sb, c);
 	}
-	fputc(sq, stream);
+	strbuf_addch(sb, sq);
 }
 
-void tcl_quote_print(FILE *stream, const char *src)
+void tcl_quote_buf(struct strbuf *sb, const char *src)
 {
 	char c;
 
-	fputc('"', stream);
+	strbuf_addch(sb, '"');
 	while ((c = *src++)) {
 		switch (c) {
 		case '[': case ']':
 		case '{': case '}':
 		case '$': case '\\': case '"':
-			fputc('\\', stream);
+			strbuf_addch(sb, '\\');
 		default:
-			fputc(c, stream);
+			strbuf_addch(sb, c);
 			break;
 		case '\f':
-			fputs("\\f", stream);
+			strbuf_addstr(sb, "\\f");
 			break;
 		case '\r':
-			fputs("\\r", stream);
+			strbuf_addstr(sb, "\\r");
 			break;
 		case '\n':
-			fputs("\\n", stream);
+			strbuf_addstr(sb, "\\n");
 			break;
 		case '\t':
-			fputs("\\t", stream);
+			strbuf_addstr(sb, "\\t");
 			break;
 		case '\v':
-			fputs("\\v", stream);
+			strbuf_addstr(sb, "\\v");
 			break;
 		}
 	}
-	fputc('"', stream);
+	strbuf_addch(sb, '"');
 }
diff --git a/quote.h b/quote.h
index 133155a..ed06df5 100644
--- a/quote.h
+++ b/quote.h
@@ -69,8 +69,8 @@ extern char *quote_path_relative(const char *in, int len,
 			  struct strbuf *out, const char *prefix);
 
 /* quoting as a string literal for other languages */
-extern void perl_quote_print(FILE *stream, const char *src);
-extern void python_quote_print(FILE *stream, const char *src);
-extern void tcl_quote_print(FILE *stream, const char *src);
+extern void perl_quote_buf(struct strbuf *sb, const char *src);
+extern void python_quote_buf(struct strbuf *sb, const char *src);
+extern void tcl_quote_buf(struct strbuf *sb, const char *src);
 
 #endif
-- 
1.8.2.83.gc99314b
Previous: Felipe ContrerasNext: Felipe Contreras
Message 4 of 31 in “for-each-ref format improvements”
  1. 0/9 for-each-ref format improvementsNguyễn Thái Ngọc Duy, May 19, 2013
  2. 1/9 quote.c: make sq_quote_print a slight wrapper of sq_quote_bufNguyễn Thái Ngọc Duy, May 19, 2013
  3. Felipe ContrerasMay 19, 2013
  4. 2/9 for-each-ref: convert to use *_quote_buf instead of _quote_printNguyễn Thái Ngọc Duy, May 19, 2013
  5. Felipe ContrerasMay 19, 2013
  6. 3/9 for-each-ref: avoid printing each element directly to stdoutNguyễn Thái Ngọc Duy, May 19, 2013
  7. Ramkumar RamachandraMay 19, 2013
  8. Duy NguyenMay 19, 2013
  9. 4/9 for-each-ref: add %(current) for current branch markerNguyễn Thái Ngọc Duy, May 19, 2013
  10. Ramkumar RamachandraMay 19, 2013
  11. 5/9 for-each-ref: add %(tracking[:upstream]) for tracking infoNguyễn Thái Ngọc Duy, May 19, 2013
  12. Ramkumar RamachandraMay 19, 2013
  13. Felipe ContrerasMay 19, 2013
  14. Duy NguyenMay 19, 2013
  15. Ramkumar RamachandraMay 19, 2013
  16. Felipe ContrerasMay 19, 2013
  17. 6/9 for-each-ref: add %(color:...)Nguyễn Thái Ngọc Duy, May 19, 2013
  18. Ramkumar RamachandraMay 19, 2013
  19. 7/9 for-each-ref: prepoplulate all atoms before show_ref()Nguyễn Thái Ngọc Duy, May 19, 2013
  20. 8/9 for-each-ref: merge show_ref into show_refsNguyễn Thái Ngọc Duy, May 19, 2013
  21. 9/9 for-each-ref: support %(...:aligned) for left alignmentNguyễn Thái Ngọc Duy, May 19, 2013
  22. Ramkumar RamachandraMay 19, 2013
  23. Duy NguyenMay 19, 2013
  24. Ramkumar RamachandraMay 19, 2013
  25. Felipe ContrerasMay 19, 2013
  26. Duy NguyenMay 19, 2013
  27. Ramkumar RamachandraMay 19, 2013
  28. Duy NguyenMay 19, 2013
  29. Ramkumar RamachandraMay 19, 2013
  30. Junio C HamanoMay 20, 2013
  31. Junio C HamanoMay 21, 2013

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.