Skip to content

Commit 9f6a357

Browse files
committed
Implement JSON string escape in pure-awk (several orders of magnitude faster)
I've got a very pathological branch of a certain repository where this makes a parallelized (four concurrent invocations) generation change from ~3 _minutes_ down to ~13 _seconds_.
1 parent 92ab34b commit 9f6a357

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

scripts/jq-template.awk

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,13 @@
33
# see https://github.com/docker-library/php or https://github.com/docker-library/golang for examples of usage ("apply-templates.sh")
44

55
# escape an arbitrary string for passing back to jq as program input
6-
function jq_escape(str, # parameters
7-
prog, e, out) # locals
8-
{
9-
prog = "jq --raw-input --slurp ."
10-
printf "%s", str |& prog
11-
close(prog, "to")
12-
prog |& getline out
13-
e = close(prog)
14-
if (e != 0) {
15-
exit(e)
16-
}
17-
return out
6+
function jq_escape(str) {
7+
gsub(/\\/, "\\\\", str)
8+
gsub(/\n/, "\\n", str)
9+
gsub(/\r/, "\\r", str)
10+
gsub(/\t/, "\\t", str)
11+
gsub(/"/, "\\\"", str)
12+
return "\"" str "\""
1813
}
1914

2015
# return the number of times needle appears in haystack

0 commit comments

Comments
 (0)