From e4399b525cba748e3a1190639544634e42a24244 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Paul=20H=C3=A4nsch?= Date: Mon, 15 Apr 2024 12:20:34 +0200 Subject: [PATCH] quicker unescape function --- pdiread.sh | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/pdiread.sh b/pdiread.sh index 08fbaec..e19ceb8 100755 --- a/pdiread.sh +++ b/pdiread.sh @@ -1,6 +1,6 @@ #!/bin/zsh -# Copyright 2014 - 2018 Paul Hänsch +# Copyright 2014 - 2018, 2023 Paul Hänsch # # This file is part of Confetti. # @@ -25,16 +25,17 @@ include_pdi="$0" BR=' ' - -unescape() { - local unescape='s;(^(\\\\)*|[^\\](\\\\)*)\\n;\1\n;g; s;\\(.);\1;g' - if [ $# -eq 0 ]; then - sed -E "$unescape" - else - printf %s "$*" \ - | sed -E "$unescape" - fi -} +unescape(){ + local in out='' + [ $# -gt 0 ] && in="$*" || in="$(cat)" + while [ "$in" ]; do case $in in + \\\\*) out="${out}\\"; in="${in#\\\\}" ;; + \\n*) out="${out}${BR}"; in="${in#\\n}" ;; + \\*) in="${in#\\}" ;; + *) out="${out}${in%%[\\]*}"; in="${in#"${in%%[\\]*}"}" ;; + esac; done + printf '%s\n' "$out" + } pdi_load() { # normalise PDI file for processing with pdi_* functions -- 2.39.2