Add @tsv; fix #645

This commit is contained in:
Nicolas Williams committed 2014-12-24 11:21:18 -06:00
1 parent fab2048648
commit 0053aa868c
3 files changed
+32 -15

No files matched your search

+17 -6
View File
@@ -334,7 +334,7 @@ static jv escape_string(jv input, const char* escapings) {
const char* cstart; const char* cstart;
int c = 0; int c = 0;
while ((i = jvp_utf8_next((cstart = i), end, &c))) { while ((i = jvp_utf8_next((cstart = i), end, &c))) {
assert(c != -1); assert(c > 0);
if (c < 128 && lookup[c]) { if (c < 128 && lookup[c]) {
ret = jv_string_append_str(ret, lookup[c]); ret = jv_string_append_str(ret, lookup[c]);
} else { } else {
@@ -358,13 +358,24 @@ static jv f_format(jq_state *jq, jv input, jv fmt) {
} else if (!strcmp(fmt_s, "text")) { } else if (!strcmp(fmt_s, "text")) {
jv_free(fmt); jv_free(fmt);
return f_tostring(jq, input); return f_tostring(jq, input);
} else if (!strcmp(fmt_s, "csv")) { } else if (!strcmp(fmt_s, "csv") || !strcmp(fmt_s, "tsv")) {
const char *quotes, *sep, *escapings;
if (!strcmp(fmt_s, "csv")) {
quotes = "\"";
sep = ",";
escapings = "\"\"\"\0";
} else {
assert(!strcmp(fmt_s, "tsv"));
quotes = "";
sep = "\t";
escapings = "\t\\t\0";
}
jv_free(fmt); jv_free(fmt);
if (jv_get_kind(input) != JV_KIND_ARRAY) if (jv_get_kind(input) != JV_KIND_ARRAY)
return type_error(input, "cannot be csv-formatted, only array"); return type_error(input, "cannot be csv-formatted, only array");
jv line = jv_string(""); jv line = jv_string("");
jv_array_foreach(input, i, x) { jv_array_foreach(input, i, x) {
if (i) line = jv_string_append_str(line, ","); if (i) line = jv_string_append_str(line, sep);
switch (jv_get_kind(x)) { switch (jv_get_kind(x)) {
case JV_KIND_NULL: case JV_KIND_NULL:
/* null rendered as empty string */ /* null rendered as empty string */
@@ -383,9 +394,9 @@ static jv f_format(jq_state *jq, jv input, jv fmt) {
} }
break; break;
case JV_KIND_STRING: { case JV_KIND_STRING: {
line = jv_string_append_str(line, "\""); line = jv_string_append_str(line, quotes);
line = jv_string_concat(line, escape_string(x, "\"\"\"\0")); line = jv_string_concat(line, escape_string(x, escapings));
line = jv_string_append_str(line, "\""); line = jv_string_append_str(line, quotes);
break; break;
} }
default: default:
+5
View File
@@ -1454,6 +1454,11 @@ sections:
with double quotes for strings, and quotes escaped by with double quotes for strings, and quotes escaped by
repetition. repetition.
* `@tsv`:
The input must be an array, and it is rendered as TSV
with tabs escaped as `\t`.
* `@sh`: * `@sh`:
The input is escaped suitable for use in a command-line The input is escaped suitable for use in a command-line
+10 -9
View File
@@ -61,15 +61,16 @@ null
null null
"interpolation" "interpolation"
@text,@json,([1,.] | @csv),@html,@uri,@sh,@base64 @text,@json,([1,.] | (@csv, @tsv)),@html,@uri,@sh,@base64
"<>&'\"" "<>&'\"\t"
"<>&'\"" "<>&'\"\t"
"\"<>&'\\\"\"" "\"<>&'\\\"\\t\""
"1,\"<>&'\"\"\"" "1,\"<>&'\"\"\t\""
"&lt;&gt;&amp;&apos;&quot;" "1\t<>&'\"\\t"
"%3C%3E%26'%22" "&lt;&gt;&amp;&apos;&quot;\t"
"'<>&'\\''\"'" "%3C%3E%26'%22%09"
"PD4mJyI=" "'<>&'\\''\"\t'"
"PD4mJyIJ"
# regression test for #436 # regression test for #436
@base64 @base64