From: paul Date: Sat, 14 Jan 2017 07:05:32 +0000 (+0000) Subject: quicker (and simpler) validation functions X-Git-Url: https://git.plutz.net/?a=commitdiff_plain;h=c6eac77df584035b4dd5f849a766c9359d5f3479;p=clickslide quicker (and simpler) validation functions svn path=/trunk/; revision=50 --- diff --git a/misc.sh b/misc.sh index 43edfcf..9639309 100755 --- a/misc.sh +++ b/misc.sh @@ -1,6 +1,6 @@ #!/bin/zsh -# Copyright 2014, 2015 Paul Hänsch +# Copyright 2014 - 2017 Paul Hänsch # # This file is part of shcgi. # @@ -33,25 +33,19 @@ data_dirs(){ validate(){ # print value if value matches regex; otherwise print default value="$1" - regex="$(printf %s\\n "$2" | sed -r ':X;s;(^|[^\\])((\\\\)*)/;\1\2\\/;g;tX')" - # ^^ escape only unescaped slash characters for later insertion + regex="$2" default="$3" - printf %s\\n "${value}" \ - | sed -rn "2q; /^(${regex})\$/{p;q}; a${default} - " + printf %s "${value%%${BR}*}" |grep -xE "$regex" || printf %s "$default" } invalidate(){ # print default if value matches regex; otherwise print value value="$1" - regex="$(printf %s\\n "$2" | sed -r ':X;s;(^|[^\\])((\\\\)*)/;\1\2\\/;g;tX')" - # ^^ escape only unescaped slash characters for later insertion + regex="$2" default="$3" - - printf %s\\n "${value}" \ - | sed -rn "2q; /^(${regex})\$/{bX}; p;q; :X;a${default} - " + + printf %s "${value%%${BR}*}" |grep -qxE "$regex" && printf %s "$default" || printf %s "${value%%${BR}*}" } declare -A item_name