README: optimize the unescape() function - json2tsv - JSON to TSV converter
(HTM) git clone git://git.codemadness.org/json2tsv
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
(DIR) commit 5c614ee9fef3c53e5c046b5fc2af19253a2899b5
(DIR) parent d0b6a90a0f4acb6b3635025c899553aad208f400
(HTM) Author: Hiltjo Posthuma <hiltjo@codemadness.org>
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(-)
---
(DIR) 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);