From 0d6196d34eed576738a7ca4f4f40f240503b1d96 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Paul=20H=C3=A4nsch?= Date: Wed, 29 Jan 2020 18:22:44 +0100 Subject: [PATCH] faster STRING and UNSTRING functions --- storage.sh | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/storage.sh b/storage.sh index 10a8029..d84f0c2 100755 --- a/storage.sh +++ b/storage.sh @@ -20,6 +20,10 @@ [ -n "$include_storage" ] && return 0 include_storage="$0" +CR=" " +BR=' +' + LOCK(){ local lock timeout block lock="${1}.lock" @@ -72,11 +76,27 @@ STRING=' s; ;+;g; ' -STRING(){ +STRING_OLD(){ { [ $# -eq 0 ] && cat || printf %s "$*"; } \ | sed -E ':X; $!{N;bX;}'"$STRING" } +STRING(){ + local in out='' + [ $# -gt 0 ] && in="$*" || in="$(cat)" + while [ "$in" ]; do case $in in + \\*) out="${out}\\\\"; in="${in#\\}" ;; + "$BR"*) out="${out}\\n"; in="${in#${BR}}" ;; + "$CR"*) out="${out}\\r"; in="${in#${CR}}" ;; + " "*) out="${out}\\t"; in="${in# }" ;; + +*) out="${out}\\+"; in="${in#+}" ;; + " "*) out="${out}+"; in="${in# }" ;; + *) out="${out}${in%%[\\${CR}${BR} + ]*}"; in="${in#${in%%[\\${BR}${CR} + ]*}}" ;; + esac; done + printf '%s' "$out" +} + + UNSTRING=' :UNSTRING_X s;((^|[^\\])(\\\\)*)\\n;\1\n;g; @@ -87,7 +107,23 @@ UNSTRING=' s;((^|[^\\])(\\\\)*)\\\+;\1+;g; s;\\\\;\\;g; ' -UNSTRING(){ +UNSTRING_OLD(){ { [ $# -eq 0 ] && cat || printf %s "$*"; } \ | sed -E "$UNSTRING" } +UNSTRING(){ + 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}" ;; + \\r*) out="${out}${CR}"; in="${in#\\r}" ;; + \\t*) out="${out} "; in="${in#\\t}" ;; + \\+) out="${out}+"; in="${in#\\+}" ;; + +*) out="${out} "; in="${in#+}" ;; + \\*) in="${in#\\}" ;; + *) out="${out}${in%%[\\+]*}"; in="${in#${in%%[\\+]*}}" ;; + esac; done + printf '%s' "$out" +} + -- 2.39.2