Merge pull request #48 from infosiftr/pure-awk-json-string-escaping
Implement JSON string escape in pure-awk (several orders of magnitude faster)
This commit is contained in:
commit
3a1bc49181
|
|
@ -3,18 +3,13 @@
|
||||||
# see https://github.com/docker-library/php or https://github.com/docker-library/golang for examples of usage ("apply-templates.sh")
|
# see https://github.com/docker-library/php or https://github.com/docker-library/golang for examples of usage ("apply-templates.sh")
|
||||||
|
|
||||||
# escape an arbitrary string for passing back to jq as program input
|
# escape an arbitrary string for passing back to jq as program input
|
||||||
function jq_escape(str, # parameters
|
function jq_escape(str) {
|
||||||
prog, e, out) # locals
|
gsub(/\\/, "\\\\", str)
|
||||||
{
|
gsub(/\n/, "\\n", str)
|
||||||
prog = "jq --raw-input --slurp ."
|
gsub(/\r/, "\\r", str)
|
||||||
printf "%s", str |& prog
|
gsub(/\t/, "\\t", str)
|
||||||
close(prog, "to")
|
gsub(/"/, "\\\"", str)
|
||||||
prog |& getline out
|
return "\"" str "\""
|
||||||
e = close(prog)
|
|
||||||
if (e != 0) {
|
|
||||||
exit(e)
|
|
||||||
}
|
|
||||||
return out
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# return the number of times needle appears in haystack
|
# return the number of times needle appears in haystack
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue