]> git.plutz.net Git - clickslide/commitdiff
added invalidate function
authorpaul <paul@plutz.net>
Mon, 5 Oct 2015 23:40:50 +0000 (23:40 +0000)
committerpaul <paul@plutz.net>
Mon, 5 Oct 2015 23:40:50 +0000 (23:40 +0000)
svn path=/trunk/; revision=17

misc.sh

diff --git a/misc.sh b/misc.sh
index 05a3af52385b85547a8952dd0131778e2ce73268..9d92805e4c9451c9c7fe8094a8fc2f60e1e0071a 100755 (executable)
--- a/misc.sh
+++ b/misc.sh
@@ -38,3 +38,15 @@ validate(){
   printf %s\\n "${value}" \
   | sed -rn "2q; /^(${regex})\$/{p;q}; a${default}"
 }
+
+invalidate(){
+  # print default if value matches regex; otherwise print value
+  value="$1"
+  regex="$(printf %s\\n "$2" \
+           | sed -r 's;(^|[^\\]+)((\\\\)+)/;\1\2\\/;g; s;(^|[^\\])/;\1\\/;g; s;(^|[^\\]+)((\\\\)+)/;\1\2\\/;g; s;(^|[^\\])/;\1\\/;g;'
+          )" # ^^ escape only unescaped slash characters for later insertion
+  default="$3"
+
+  printf %s\\n "${value}" \
+  | sed -rn "2q; /^(${regex})\$/{bd}; p;q; :d;a${default}"
+}