README: optimize the unescape() function - json2tsv - JSON to TSV converter | |
git clone git://git.codemadness.org/json2tsv | |
Log | |
Files | |
Refs | |
README | |
LICENSE | |
--- | |
commit 5c614ee9fef3c53e5c046b5fc2af19253a2899b5 | |
parent d0b6a90a0f4acb6b3635025c899553aad208f400 | |
Author: Hiltjo Posthuma <[email protected]> | |
Date: Tue, 26 Oct 2021 02:17:11 +0200 | |
README: optimize the unescape() function | |
For bigger datasets use the -F and -R option though. | |
Diffstat: | |
M README | 19 ++++++------------- | |
1 file changed, 6 insertions(+), 13 deletions(-) | |
--- | |
diff --git a/README b/README | |
@@ -91,19 +91,12 @@ BEGIN { | |
FS = OFS = "\t"; | |
} | |
function unescape(s) { | |
- for (data = ""; (idx = index(s, "\\")); s = substr(s, idx + 2)) { | |
- prev = substr(s, 1, idx - 1) | |
- c = substr(s, idx + 1, 1) | |
- if (c == "t") | |
- data = data prev "\t" | |
- else if (c == "n") | |
- data = data prev "\n" | |
- else if (c == "\\") | |
- data = data prev "\\" | |
- else | |
- data = data prev # ignore other. | |
- } | |
- return data s # rest | |
+ # use the character "\x01" as a temporary replacement for "\". | |
+ gsub("\\\\\\\\", "\x01", s); | |
+ gsub("\\\\n", "\n", s); | |
+ gsub("\\\\t", "\t", s); | |
+ gsub("\x01", "\\", s); # restore "\x01" to "\". | |
+ return s; | |
} | |
$2 == "s" && index($3, "\\") { | |
$3 = unescape($3); |