for-each-ref, quote: convert *_quote_print -> *_quote_buf
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>
Nguyễn Thái Ngọc Duy committed
Jul 30, 2013 at 14:01 UTC
10d0167fef35c7a4e4e3ef9dd448594b62089c5a
3 files changed
+34
-29
builtin/for-each-ref.c
+9
-4
@@ -867,24 +867,29 @@ static void sort_refs(struct ref_sort *sort, struct refinfo **refs, int num_refs
867
static void print_value(struct refinfo *ref, int atom, int quote_style)
868
{
869
struct atom_value *v;
870
+ struct strbuf sb = STRBUF_INIT;
871
get_value(ref, atom, &v);
872
switch (quote_style) {
873
case QUOTE_NONE:
874
fputs(v->s, stdout);
875
break;
876
case QUOTE_SHELL:
876
- sq_quote_print(stdout, v->s);
877
+ sq_quote_buf(&sb, v->s);
878
break;
879
case QUOTE_PERL:
879
- perl_quote_print(stdout, v->s);
880
+ perl_quote_buf(&sb, v->s);
881
break;
882
case QUOTE_PYTHON:
882
- python_quote_print(stdout, v->s);
883
+ python_quote_buf(&sb, v->s);
884
break;
885
case QUOTE_TCL:
885
- tcl_quote_print(stdout, v->s);
886
+ tcl_quote_buf(&sb, v->s);
887
break;
888
}
889
+ if (quote_style != QUOTE_NONE) {
890
+ fputs(sb.buf, stdout);
891
+ strbuf_release(&sb);
892
+ }
893
}
894
895
static int hex1(char ch)
quote.c
+22
-22
@@ -408,72 +408,72 @@ int unquote_c_style(struct strbuf *sb, const char *quoted, const char **endp)
408
409
/* quoting as a string literal for other languages */
410
411
-void perl_quote_print(FILE *stream, const char *src)
411
+void perl_quote_buf(struct strbuf *sb, const char *src)
412
{
413
const char sq = '\'';
414
const char bq = '\\';
415
char c;
416
417
- fputc(sq, stream);
417
+ strbuf_addch(sb, sq);
418
while ((c = *src++)) {
419
if (c == sq || c == bq)
420
- fputc(bq, stream);
421
- fputc(c, stream);
420
+ strbuf_addch(sb, bq);
421
+ strbuf_addch(sb, c);
422
}
423
- fputc(sq, stream);
423
+ strbuf_addch(sb, sq);
424
}
425
426
-void python_quote_print(FILE *stream, const char *src)
426
+void python_quote_buf(struct strbuf *sb, const char *src)
427
{
428
const char sq = '\'';
429
const char bq = '\\';
430
const char nl = '\n';
431
char c;
432
433
- fputc(sq, stream);
433
+ strbuf_addch(sb, sq);
434
while ((c = *src++)) {
435
if (c == nl) {
436
- fputc(bq, stream);
437
- fputc('n', stream);
436
+ strbuf_addch(sb, bq);
437
+ strbuf_addch(sb, 'n');
438
continue;
439
}
440
if (c == sq || c == bq)
441
- fputc(bq, stream);
442
- fputc(c, stream);
441
+ strbuf_addch(sb, bq);
442
+ strbuf_addch(sb, c);
443
}
444
- fputc(sq, stream);
444
+ strbuf_addch(sb, sq);
445
}
446
447
-void tcl_quote_print(FILE *stream, const char *src)
447
+void tcl_quote_buf(struct strbuf *sb, const char *src)
448
{
449
char c;
450
451
- fputc('"', stream);
451
+ strbuf_addch(sb, '"');
452
while ((c = *src++)) {
453
switch (c) {
454
case '[': case ']':
455
case '{': case '}':
456
case '$': case '\\': case '"':
457
- fputc('\\', stream);
457
+ strbuf_addch(sb, '\\');
458
default:
459
- fputc(c, stream);
459
+ strbuf_addch(sb, c);
460
break;
461
case '\f':
462
- fputs("\\f", stream);
462
+ strbuf_addstr(sb, "\\f");
463
break;
464
case '\r':
465
- fputs("\\r", stream);
465
+ strbuf_addstr(sb, "\\r");
466
break;
467
case '\n':
468
- fputs("\\n", stream);
468
+ strbuf_addstr(sb, "\\n");
469
break;
470
case '\t':
471
- fputs("\\t", stream);
471
+ strbuf_addstr(sb, "\\t");
472
break;
473
case '\v':
474
- fputs("\\v", stream);
474
+ strbuf_addstr(sb, "\\v");
475
break;
476
}
477
}
478
- fputc('"', stream);
478
+ strbuf_addch(sb, '"');
479
}
quote.h
+3
-3
@@ -68,8 +68,8 @@ extern char *quote_path_relative(const char *in, const char *prefix,
68
struct strbuf *out);
69
70
/* quoting as a string literal for other languages */
71
-extern void perl_quote_print(FILE *stream, const char *src);
72
-extern void python_quote_print(FILE *stream, const char *src);
73
-extern void tcl_quote_print(FILE *stream, const char *src);
71
+extern void perl_quote_buf(struct strbuf *sb, const char *src);
72
+extern void python_quote_buf(struct strbuf *sb, const char *src);
73
+extern void tcl_quote_buf(struct strbuf *sb, const char *src);
74
75
#endif