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>
This commit is contained in:
parent
8f6a3e5d71
commit
10d0167fef
@ -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)
|
static void print_value(struct refinfo *ref, int atom, int quote_style)
|
||||||
{
|
{
|
||||||
struct atom_value *v;
|
struct atom_value *v;
|
||||||
|
struct strbuf sb = STRBUF_INIT;
|
||||||
get_value(ref, atom, &v);
|
get_value(ref, atom, &v);
|
||||||
switch (quote_style) {
|
switch (quote_style) {
|
||||||
case QUOTE_NONE:
|
case QUOTE_NONE:
|
||||||
fputs(v->s, stdout);
|
fputs(v->s, stdout);
|
||||||
break;
|
break;
|
||||||
case QUOTE_SHELL:
|
case QUOTE_SHELL:
|
||||||
sq_quote_print(stdout, v->s);
|
sq_quote_buf(&sb, v->s);
|
||||||
break;
|
break;
|
||||||
case QUOTE_PERL:
|
case QUOTE_PERL:
|
||||||
perl_quote_print(stdout, v->s);
|
perl_quote_buf(&sb, v->s);
|
||||||
break;
|
break;
|
||||||
case QUOTE_PYTHON:
|
case QUOTE_PYTHON:
|
||||||
python_quote_print(stdout, v->s);
|
python_quote_buf(&sb, v->s);
|
||||||
break;
|
break;
|
||||||
case QUOTE_TCL:
|
case QUOTE_TCL:
|
||||||
tcl_quote_print(stdout, v->s);
|
tcl_quote_buf(&sb, v->s);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
if (quote_style != QUOTE_NONE) {
|
||||||
|
fputs(sb.buf, stdout);
|
||||||
|
strbuf_release(&sb);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int hex1(char ch)
|
static int hex1(char ch)
|
||||||
|
44
quote.c
44
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 */
|
/* 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 sq = '\'';
|
||||||
const char bq = '\\';
|
const char bq = '\\';
|
||||||
char c;
|
char c;
|
||||||
|
|
||||||
fputc(sq, stream);
|
strbuf_addch(sb, sq);
|
||||||
while ((c = *src++)) {
|
while ((c = *src++)) {
|
||||||
if (c == sq || c == bq)
|
if (c == sq || c == bq)
|
||||||
fputc(bq, stream);
|
strbuf_addch(sb, bq);
|
||||||
fputc(c, stream);
|
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 sq = '\'';
|
||||||
const char bq = '\\';
|
const char bq = '\\';
|
||||||
const char nl = '\n';
|
const char nl = '\n';
|
||||||
char c;
|
char c;
|
||||||
|
|
||||||
fputc(sq, stream);
|
strbuf_addch(sb, sq);
|
||||||
while ((c = *src++)) {
|
while ((c = *src++)) {
|
||||||
if (c == nl) {
|
if (c == nl) {
|
||||||
fputc(bq, stream);
|
strbuf_addch(sb, bq);
|
||||||
fputc('n', stream);
|
strbuf_addch(sb, 'n');
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (c == sq || c == bq)
|
if (c == sq || c == bq)
|
||||||
fputc(bq, stream);
|
strbuf_addch(sb, bq);
|
||||||
fputc(c, stream);
|
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;
|
char c;
|
||||||
|
|
||||||
fputc('"', stream);
|
strbuf_addch(sb, '"');
|
||||||
while ((c = *src++)) {
|
while ((c = *src++)) {
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case '[': case ']':
|
case '[': case ']':
|
||||||
case '{': case '}':
|
case '{': case '}':
|
||||||
case '$': case '\\': case '"':
|
case '$': case '\\': case '"':
|
||||||
fputc('\\', stream);
|
strbuf_addch(sb, '\\');
|
||||||
default:
|
default:
|
||||||
fputc(c, stream);
|
strbuf_addch(sb, c);
|
||||||
break;
|
break;
|
||||||
case '\f':
|
case '\f':
|
||||||
fputs("\\f", stream);
|
strbuf_addstr(sb, "\\f");
|
||||||
break;
|
break;
|
||||||
case '\r':
|
case '\r':
|
||||||
fputs("\\r", stream);
|
strbuf_addstr(sb, "\\r");
|
||||||
break;
|
break;
|
||||||
case '\n':
|
case '\n':
|
||||||
fputs("\\n", stream);
|
strbuf_addstr(sb, "\\n");
|
||||||
break;
|
break;
|
||||||
case '\t':
|
case '\t':
|
||||||
fputs("\\t", stream);
|
strbuf_addstr(sb, "\\t");
|
||||||
break;
|
break;
|
||||||
case '\v':
|
case '\v':
|
||||||
fputs("\\v", stream);
|
strbuf_addstr(sb, "\\v");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fputc('"', stream);
|
strbuf_addch(sb, '"');
|
||||||
}
|
}
|
||||||
|
6
quote.h
6
quote.h
@ -68,8 +68,8 @@ extern char *quote_path_relative(const char *in, const char *prefix,
|
|||||||
struct strbuf *out);
|
struct strbuf *out);
|
||||||
|
|
||||||
/* quoting as a string literal for other languages */
|
/* quoting as a string literal for other languages */
|
||||||
extern void perl_quote_print(FILE *stream, const char *src);
|
extern void perl_quote_buf(struct strbuf *sb, const char *src);
|
||||||
extern void python_quote_print(FILE *stream, const char *src);
|
extern void python_quote_buf(struct strbuf *sb, const char *src);
|
||||||
extern void tcl_quote_print(FILE *stream, const char *src);
|
extern void tcl_quote_buf(struct strbuf *sb, const char *src);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user