Merge branch 'nd/sq-quote-buf'
Code simplification as a preparatory step to something larger. * nd/sq-quote-buf: quote: remove sq_quote_print() tar-tree: remove dependency on sq_quote_print() for-each-ref, quote: convert *_quote_print -> *_quote_bufmaint
commit
4aa04a8f8d
|
@ -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)
|
||||||
|
|
|
@ -26,8 +26,8 @@ int cmd_tar_tree(int argc, const char **argv, const char *prefix)
|
||||||
* $0 tree-ish basedir ==>
|
* $0 tree-ish basedir ==>
|
||||||
* git archive --format-tar --prefix=basedir tree-ish
|
* git archive --format-tar --prefix=basedir tree-ish
|
||||||
*/
|
*/
|
||||||
int i;
|
|
||||||
const char **nargv = xcalloc(sizeof(*nargv), argc + 3);
|
const char **nargv = xcalloc(sizeof(*nargv), argc + 3);
|
||||||
|
struct strbuf sb = STRBUF_INIT;
|
||||||
char *basedir_arg;
|
char *basedir_arg;
|
||||||
int nargc = 0;
|
int nargc = 0;
|
||||||
|
|
||||||
|
@ -65,11 +65,10 @@ int cmd_tar_tree(int argc, const char **argv, const char *prefix)
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"*** \"git tar-tree\" is now deprecated.\n"
|
"*** \"git tar-tree\" is now deprecated.\n"
|
||||||
"*** Running \"git archive\" instead.\n***");
|
"*** Running \"git archive\" instead.\n***");
|
||||||
for (i = 0; i < nargc; i++) {
|
sq_quote_argv(&sb, nargv, 0);
|
||||||
fputc(' ', stderr);
|
strbuf_addch(&sb, '\n');
|
||||||
sq_quote_print(stderr, nargv[i]);
|
fputs(sb.buf, stderr);
|
||||||
}
|
strbuf_release(&sb);
|
||||||
fputc('\n', stderr);
|
|
||||||
return cmd_archive(nargc, nargv, prefix);
|
return cmd_archive(nargc, nargv, prefix);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
61
quote.c
61
quote.c
|
@ -42,23 +42,6 @@ void sq_quote_buf(struct strbuf *dst, const char *src)
|
||||||
free(to_free);
|
free(to_free);
|
||||||
}
|
}
|
||||||
|
|
||||||
void sq_quote_print(FILE *stream, const char *src)
|
|
||||||
{
|
|
||||||
char c;
|
|
||||||
|
|
||||||
fputc('\'', stream);
|
|
||||||
while ((c = *src++)) {
|
|
||||||
if (need_bs_quote(c)) {
|
|
||||||
fputs("'\\", stream);
|
|
||||||
fputc(c, stream);
|
|
||||||
fputc('\'', stream);
|
|
||||||
} else {
|
|
||||||
fputc(c, stream);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fputc('\'', stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
void sq_quote_argv(struct strbuf *dst, const char** argv, size_t maxlen)
|
void sq_quote_argv(struct strbuf *dst, const char** argv, size_t maxlen)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
@ -408,72 +391,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, '"');
|
||||||
}
|
}
|
||||||
|
|
8
quote.h
8
quote.h
|
@ -27,8 +27,6 @@ struct strbuf;
|
||||||
* excluding the final null regardless of the buffer size.
|
* excluding the final null regardless of the buffer size.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
extern void sq_quote_print(FILE *stream, const char *src);
|
|
||||||
|
|
||||||
extern void sq_quote_buf(struct strbuf *, const char *src);
|
extern void sq_quote_buf(struct strbuf *, const char *src);
|
||||||
extern void sq_quote_argv(struct strbuf *, const char **argv, size_t maxlen);
|
extern void sq_quote_argv(struct strbuf *, const char **argv, size_t maxlen);
|
||||||
|
|
||||||
|
@ -68,8 +66,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