functions for encoding/decoding URLS

This commit is contained in:
Nathaniel Landau
2015-07-10 21:39:07 -04:00
parent 7093261f4f
commit 14e4b338ce

View File

@@ -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 <string>
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 <string>
local url_encoded="${1//+/ }"
printf '%b' "${url_encoded//%/\x}"
}