Skip to content

Commit

Permalink
escape the null character in csv, tsv, sh formats (close #200)
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed Nov 14, 2022
1 parent 5253817 commit 6631dd3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
12 changes: 6 additions & 6 deletions cli/test.yaml
Expand Up @@ -5848,9 +5848,9 @@
args:
- '@csv'
input: |
[1, "foo", null, "foo,\n\"bar\"\tbaz"]
[1, "foo", null, "foo,\n\"bar\"\tbaz\u0000\\0"]
expected: |
"1,\"foo\",,\"foo,\n\"\"bar\"\"\tbaz\""
"1,\"foo\",,\"foo,\n\"\"bar\"\"\tbaz\\0\\0\""
- name: format strings @csv with string interpolation
args:
Expand Down Expand Up @@ -5879,9 +5879,9 @@
args:
- '@tsv'
input: |
[1, "foo", null, "foo,\n\"bar\"\tba\\z"]
[1, "foo", null, "foo,\n\"bar\"\tbaz\u0000\\0"]
expected: |
"1\tfoo\t\tfoo,\\n\"bar\"\\tba\\\\z"
"1\tfoo\t\tfoo,\\n\"bar\"\\tbaz\\0\\\\0"
- name: format strings @tsv with string interpolation
args:
Expand Down Expand Up @@ -5912,11 +5912,11 @@
input: |
null
true
[1, "f'o'o", null, false]
[1, "f'o'o\u0000\\0", null, false]
expected: |
"null"
"true"
"1 'f'\\''o'\\''o' null false"
"1 'f'\\''o'\\''o\\0\\0' null false"
- name: format strings @sh error
args:
Expand Down
15 changes: 13 additions & 2 deletions func.go
Expand Up @@ -808,9 +808,14 @@ func funcToURI(v interface{}) interface{} {
}
}

var csvEscaper = strings.NewReplacer(
`"`, `""`,
"\x00", `\0`,
)

func funcToCSV(v interface{}) interface{} {
return formatJoin("csv", v, ",", func(s string) string {
return `"` + strings.ReplaceAll(s, `"`, `""`) + `"`
return `"` + csvEscaper.Replace(s) + `"`
})
}

Expand All @@ -819,18 +824,24 @@ var tsvEscaper = strings.NewReplacer(
"\r", `\r`,
"\n", `\n`,
"\\", `\\`,
"\x00", `\0`,
)

func funcToTSV(v interface{}) interface{} {
return formatJoin("tsv", v, "\t", tsvEscaper.Replace)
}

var shEscaper = strings.NewReplacer(
"'", `'\''`,
"\x00", `\0`,
)

func funcToSh(v interface{}) interface{} {
if _, ok := v.([]interface{}); !ok {
v = []interface{}{v}
}
return formatJoin("sh", v, " ", func(s string) string {
return "'" + strings.ReplaceAll(s, "'", `'\''`) + "'"
return "'" + shEscaper.Replace(s) + "'"
})
}

Expand Down

0 comments on commit 6631dd3

Please sign in to comment.