From: Paul Hänsch Date: Wed, 29 Jan 2020 17:22:44 +0000 (+0100) Subject: faster STRING and UNSTRING functions X-Git-Url: https://git.plutz.net/?a=commitdiff_plain;h=0d6196d34eed576738a7ca4f4f40f240503b1d96;p=cgilite faster STRING and UNSTRING functions --- 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" +} +