@tsv: escape \r, \n, \\

When using '@tsv' output format with '-r' option,
escape \r, \n and \\ in addition to \t.

Example:
    $ printf '{"id":"hello\\ttab\\nworld","x":43 }' | jq .
    {
      "id": "hello\ttab\nworld",
      "x": 43
    }

Before: newlines are not escaped, generating invalid TSV output:

    $ printf '{"id":"hello\\ttab\\nworld","x":43 }' \
        | ./jq-old -r '[.id,.x] | @tsv'
    hello\ttab
    world	43

After: newlines are properly escaped:

    $ printf '{"id":"hello\\ttab\\nworld","x":43 }' \
        | ./jq-new -r '[.id, .x] | @tsv'
    hello\ttab\nworld	43

Before: backslashes themselves are not escaped, so there's no way to
distinguish between escaped characters and 'real' backslashes
(in the example below, there is should not be newline, despite the
output containing "\n".

    $ printf '{"x":"hello\\ttab\\\\new world"}' \
        | ./jq-old -r '[.x]|@tsv'
    hello\ttab\new world

After: backslashes are escaped:

    $ printf '{"x":"hello\\ttab\\\\new world"}' \
        | ./jq-new -r '[.x]|@tsv'
    hello\ttab\\new world
This commit is contained in:
Assaf Gordon committed 2015-04-15 16:11:58 -04:00
1 parent ddad9618dc
commit 3210b29bba
1 file changed
+1 -1
+1 -1
View File
@@ -383,7 +383,7 @@ static jv f_format(jq_state *jq, jv input, jv fmt) {
assert(!strcmp(fmt_s, "tsv"));
quotes = "";
sep = "\t";
escapings = "\t\\t\0";
escapings = "\t\\t\0\r\\r\0\n\\n\0\\\\\\\0";
}
jv_free(fmt);
if (jv_get_kind(input) != JV_KIND_ARRAY)