]> git.ipfire.org Git - thirdparty/git.git/commitdiff
for-each-ref, quote: convert *_quote_print -> *_quote_buf
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>
Tue, 30 Jul 2013 08:31:25 +0000 (14:01 +0530)
committerJunio C Hamano <gitster@pobox.com>
Tue, 30 Jul 2013 15:06:27 +0000 (08:06 -0700)
The print_value() function in for-each-ref.c prints values to stdout
immediately using {sq|perl|python|tcl}_quote_print().  Change these
lower-level quote functions to instead leave their results in strbuf
so that we can later add post-processing to the results of them.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/for-each-ref.c
quote.c
quote.h

index 7f059c31dfbd11f77c891134ccc34cdc1f65bcb8..1d4083c2ddef07c6488d36720e6a0c48b04c6585 100644 (file)
@@ -867,24 +867,29 @@ 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 = STRBUF_INIT;
        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 5c8808160e700af0c055a84f8527eb3ac76962d9..9fd66c6fc534b2f3a9c64171b4da5829df5c0c30 100644 (file)
--- a/quote.c
+++ b/quote.c
@@ -408,72 +408,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 ed110a5d8d9e98a680c67379c7c39e4eda5b25f5..6996ebda5082460b14bb052ee4beffc5a2915e64 100644 (file)
--- a/quote.h
+++ b/quote.h
@@ -68,8 +68,8 @@ extern char *quote_path_relative(const char *in, const char *prefix,
                          struct strbuf *out);
 
 /* 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