2013-08-24 08:16:16 +00:00
|
|
|
function __fish_urlencode --description "URL-encode stdin"
|
2016-02-27 08:14:52 +00:00
|
|
|
set -l join ''
|
2015-12-11 23:35:57 +00:00
|
|
|
set -l chars
|
|
|
|
# Set locale to C and IFS to "" in order to split a line into bytes.
|
2016-02-27 08:14:52 +00:00
|
|
|
while begin; set -lx LC_ALL C; set -lx IFS ''; read -az chars; end
|
|
|
|
printf '%s' $join
|
|
|
|
# chomp off a trailing newline
|
|
|
|
if test "$chars[-1]" = \n
|
|
|
|
set -e chars[-1]
|
|
|
|
set join '%0A%00'
|
|
|
|
else
|
|
|
|
set join '%00'
|
|
|
|
end
|
|
|
|
for c in $chars
|
|
|
|
if string match -q -r '[/._~A-Za-z0-9-]' $c
|
|
|
|
printf '%s' $c
|
|
|
|
else
|
|
|
|
printf '%%%02X' "'$c"
|
|
|
|
end
|
2013-08-24 08:16:16 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|