]> git.plutz.net Git - confetti/commitdiff
quicker unescape function
authorPaul Hänsch <paul@plutz.net>
Mon, 15 Apr 2024 10:20:34 +0000 (12:20 +0200)
committerPaul Hänsch <paul@plutz.net>
Mon, 15 Apr 2024 10:20:34 +0000 (12:20 +0200)
pdiread.sh

index 08fbaec3fb373c2fbb6cdde7974caaa520b849e3..e19ceb8d5150c658381ac3d017f54f0f0caa6721 100755 (executable)
@@ -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