updated stuff
This commit is contained in:
parent
db8cd85bd2
commit
2efe0bd712
12 changed files with 46 additions and 236 deletions
|
@ -51,7 +51,6 @@ function omz_urlencode() {
|
|||
if [[ -z $opts[(r)-P] ]]; then spaces_as_plus=1; fi
|
||||
local str="$in_str"
|
||||
|
||||
# URLs must use UTF-8 encoding; convert str to UTF-8 if required
|
||||
local encoding=$langinfo[CODESET]
|
||||
local safe_encodings
|
||||
safe_encodings=(UTF-8 utf8 US-ASCII)
|
||||
|
@ -63,7 +62,6 @@ function omz_urlencode() {
|
|||
fi
|
||||
fi
|
||||
|
||||
# Use LC_CTYPE=C to process text byte-by-byte
|
||||
local i byte ord LC_ALL=C
|
||||
export LC_ALL
|
||||
local reserved=';/?:@&=+$,'
|
||||
|
@ -72,14 +70,12 @@ function omz_urlencode() {
|
|||
if [[ -z $opts[(r)-r] ]]; then
|
||||
dont_escape+=$reserved
|
||||
fi
|
||||
# $mark must be last because of the "-"
|
||||
|
||||
if [[ -z $opts[(r)-m] ]]; then
|
||||
dont_escape+=$mark
|
||||
fi
|
||||
dont_escape+="]"
|
||||
|
||||
# Implemented to use a single printf call and avoid subshells in the loop,
|
||||
# for performance (primarily on Windows).
|
||||
local url_str=""
|
||||
for (( i = 1; i <= ${#str}; ++i )); do
|
||||
byte="$str[i]"
|
||||
|
@ -97,40 +93,20 @@ function omz_urlencode() {
|
|||
echo -E "$url_str"
|
||||
}
|
||||
|
||||
# URL-decode a string
|
||||
#
|
||||
# Decodes a RFC 2396 URL-encoded (%-escaped) string.
|
||||
# This decodes the '+' and '%' escapes in the input string, and leaves
|
||||
# other characters unchanged. Does not enforce that the input is a
|
||||
# valid URL-encoded string. This is a convenience to allow callers to
|
||||
# pass in a full URL or similar strings and decode them for human
|
||||
# presentation.
|
||||
#
|
||||
# Outputs the encoded string on stdout.
|
||||
# Returns nonzero if encoding failed.
|
||||
#
|
||||
# Usage:
|
||||
# omz_urldecode <urlstring> - prints decoded string followed by a newline
|
||||
function omz_urldecode {
|
||||
emulate -L zsh
|
||||
local encoded_url=$1
|
||||
|
||||
# Work bytewise, since URLs escape UTF-8 octets
|
||||
local caller_encoding=$langinfo[CODESET]
|
||||
local LC_ALL=C
|
||||
export LC_ALL
|
||||
|
||||
# Change + back to ' '
|
||||
local tmp=${encoded_url:gs/+/ /}
|
||||
# Protect other escapes to pass through the printf unchanged
|
||||
tmp=${tmp:gs/\\/\\\\/}
|
||||
# Handle %-escapes by turning them into `\xXX` printf escapes
|
||||
tmp=${tmp:gs/%/\\x/}
|
||||
local decoded
|
||||
eval "decoded=\$'$tmp'"
|
||||
|
||||
# Now we have a UTF-8 encoded string in the variable. We need to re-encode
|
||||
# it if caller is in a non-UTF-8 locale.
|
||||
local safe_encodings
|
||||
safe_encodings=(UTF-8 utf8 US-ASCII)
|
||||
if [[ -z ${safe_encodings[(r)$caller_encoding]} ]]; then
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue