Skip to content

Commit 10d0167

Browse files
pcloudsgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Ramkumar Ramachandra <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8f6a3e5 commit 10d0167

File tree

3 files changed

+34
-29
lines changed

3 files changed

+34
-29
lines changed

builtin/for-each-ref.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -867,24 +867,29 @@ static void sort_refs(struct ref_sort *sort, struct refinfo **refs, int num_refs
867867
static void print_value(struct refinfo *ref, int atom, int quote_style)
868868
{
869869
struct atom_value *v;
870+
struct strbuf sb = STRBUF_INIT;
870871
get_value(ref, atom, &v);
871872
switch (quote_style) {
872873
case QUOTE_NONE:
873874
fputs(v->s, stdout);
874875
break;
875876
case QUOTE_SHELL:
876-
sq_quote_print(stdout, v->s);
877+
sq_quote_buf(&sb, v->s);
877878
break;
878879
case QUOTE_PERL:
879-
perl_quote_print(stdout, v->s);
880+
perl_quote_buf(&sb, v->s);
880881
break;
881882
case QUOTE_PYTHON:
882-
python_quote_print(stdout, v->s);
883+
python_quote_buf(&sb, v->s);
883884
break;
884885
case QUOTE_TCL:
885-
tcl_quote_print(stdout, v->s);
886+
tcl_quote_buf(&sb, v->s);
886887
break;
887888
}
889+
if (quote_style != QUOTE_NONE) {
890+
fputs(sb.buf, stdout);
891+
strbuf_release(&sb);
892+
}
888893
}
889894

890895
static int hex1(char ch)

quote.c

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -408,72 +408,72 @@ int unquote_c_style(struct strbuf *sb, const char *quoted, const char **endp)
408408

409409
/* quoting as a string literal for other languages */
410410

411-
void perl_quote_print(FILE *stream, const char *src)
411+
void perl_quote_buf(struct strbuf *sb, const char *src)
412412
{
413413
const char sq = '\'';
414414
const char bq = '\\';
415415
char c;
416416

417-
fputc(sq, stream);
417+
strbuf_addch(sb, sq);
418418
while ((c = *src++)) {
419419
if (c == sq || c == bq)
420-
fputc(bq, stream);
421-
fputc(c, stream);
420+
strbuf_addch(sb, bq);
421+
strbuf_addch(sb, c);
422422
}
423-
fputc(sq, stream);
423+
strbuf_addch(sb, sq);
424424
}
425425

426-
void python_quote_print(FILE *stream, const char *src)
426+
void python_quote_buf(struct strbuf *sb, const char *src)
427427
{
428428
const char sq = '\'';
429429
const char bq = '\\';
430430
const char nl = '\n';
431431
char c;
432432

433-
fputc(sq, stream);
433+
strbuf_addch(sb, sq);
434434
while ((c = *src++)) {
435435
if (c == nl) {
436-
fputc(bq, stream);
437-
fputc('n', stream);
436+
strbuf_addch(sb, bq);
437+
strbuf_addch(sb, 'n');
438438
continue;
439439
}
440440
if (c == sq || c == bq)
441-
fputc(bq, stream);
442-
fputc(c, stream);
441+
strbuf_addch(sb, bq);
442+
strbuf_addch(sb, c);
443443
}
444-
fputc(sq, stream);
444+
strbuf_addch(sb, sq);
445445
}
446446

447-
void tcl_quote_print(FILE *stream, const char *src)
447+
void tcl_quote_buf(struct strbuf *sb, const char *src)
448448
{
449449
char c;
450450

451-
fputc('"', stream);
451+
strbuf_addch(sb, '"');
452452
while ((c = *src++)) {
453453
switch (c) {
454454
case '[': case ']':
455455
case '{': case '}':
456456
case '$': case '\\': case '"':
457-
fputc('\\', stream);
457+
strbuf_addch(sb, '\\');
458458
default:
459-
fputc(c, stream);
459+
strbuf_addch(sb, c);
460460
break;
461461
case '\f':
462-
fputs("\\f", stream);
462+
strbuf_addstr(sb, "\\f");
463463
break;
464464
case '\r':
465-
fputs("\\r", stream);
465+
strbuf_addstr(sb, "\\r");
466466
break;
467467
case '\n':
468-
fputs("\\n", stream);
468+
strbuf_addstr(sb, "\\n");
469469
break;
470470
case '\t':
471-
fputs("\\t", stream);
471+
strbuf_addstr(sb, "\\t");
472472
break;
473473
case '\v':
474-
fputs("\\v", stream);
474+
strbuf_addstr(sb, "\\v");
475475
break;
476476
}
477477
}
478-
fputc('"', stream);
478+
strbuf_addch(sb, '"');
479479
}

quote.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ extern char *quote_path_relative(const char *in, const char *prefix,
6868
struct strbuf *out);
6969

7070
/* 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);
7474

7575
#endif

0 commit comments

Comments
 (0)