From 14e4b338ce4f914297c30fb4c4a077df57e1fae6 Mon Sep 17 00:00:00 2001 From: Nathaniel Landau Date: Fri, 10 Jul 2015 21:39:07 -0400 Subject: [PATCH] functions for encoding/decoding URLS --- lib/sharedFunctions.sh | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/sharedFunctions.sh b/lib/sharedFunctions.sh index d421611..265cbbf 100755 --- a/lib/sharedFunctions.sh +++ b/lib/sharedFunctions.sh @@ -46,7 +46,9 @@ function readFile() { # Escape a string # ------------------------------------------------------ -escape() { echo $@ | sed 's/\//\\\//g'; } +# usage: var=$(escape "String") +# ------------------------------------------------------ +escape() { echo $@ | sed 's/[]\.|$(){}?+*^]/\\&/g'; } # needSudo # ------------------------------------------------------ @@ -510,4 +512,25 @@ progressBar() { fi tput cnorm +} + +# URL encoding/decoding from: https://gist.github.com/cdown/1163649 +urlencode() { + # urlencode + + local length="${#1}" + for (( i = 0; i < length; i++ )); do + local c="${1:i:1}" + case $c in + [a-zA-Z0-9.~_-]) printf "$c" ;; + *) printf '%%%02X' "'$c" + esac + done +} + +urldecode() { + # urldecode + + local url_encoded="${1//+/ }" + printf '%b' "${url_encoded//%/\x}" } \ No newline at end of file