X-Git-Url: http://git.plutz.net/?p=cgilite;a=blobdiff_plain;f=storage.sh;h=7f70e6480d6c89c8a32d3a9ee3fb68a5bfcc35b6;hp=fd7582057e112f2ec582e6ede8d7dd1995ce2a66;hb=f0383eeb634ce1ae71bfb409a9bc25d84b6d79e9;hpb=d214859aadb31a565718da69754ad2573fa3f917 diff --git a/storage.sh b/storage.sh index fd75820..7f70e64 100755 --- a/storage.sh +++ b/storage.sh @@ -1,6 +1,6 @@ #!/bin/sh -# Copyright 2018 Paul Hänsch +# Copyright 2018, 2019 Paul Hänsch # # This is a file format helper, part of CGIlite. # @@ -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 "$*"; } \ - | printf %s "$*" |sed -r ':X; $!{N;bX;}'"$STRING" + | 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 -r "$UNSTRING" + | 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" +} +