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_buf
This commit is contained in:
Junio C Hamano 2013-09-04 12:28:12 -07:00
commit 4aa04a8f8d
4 changed files with 39 additions and 54 deletions

View 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)

View file

@ -26,8 +26,8 @@ int cmd_tar_tree(int argc, const char **argv, const char *prefix)
* $0 tree-ish basedir ==>
* git archive --format-tar --prefix=basedir tree-ish
*/
int i;
const char **nargv = xcalloc(sizeof(*nargv), argc + 3);
struct strbuf sb = STRBUF_INIT;
char *basedir_arg;
int nargc = 0;
@ -65,11 +65,10 @@ int cmd_tar_tree(int argc, const char **argv, const char *prefix)
fprintf(stderr,
"*** \"git tar-tree\" is now deprecated.\n"
"*** Running \"git archive\" instead.\n***");
for (i = 0; i < nargc; i++) {
fputc(' ', stderr);
sq_quote_print(stderr, nargv[i]);
}
fputc('\n', stderr);
sq_quote_argv(&sb, nargv, 0);
strbuf_addch(&sb, '\n');
fputs(sb.buf, stderr);
strbuf_release(&sb);
return cmd_archive(nargc, nargv, prefix);
}

61
quote.c
View file

@ -42,23 +42,6 @@ void sq_quote_buf(struct strbuf *dst, const char *src)
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)
{
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 */
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, '"');
}

View file

@ -27,8 +27,6 @@ struct strbuf;
* 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_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);
/* 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