mirror of
https://github.com/fish-shell/fish-shell
synced 2024-12-26 12:53:13 +00:00
Rewrite __fish_urlencode to not encode valid characters
Much better to only encode the characters that are not URL-safe. This also doesn't involve any forking, and it even handles newlines and NULs in the input.
This commit is contained in:
parent
211829c586
commit
d8a497434f
1 changed files with 16 additions and 5 deletions
|
@ -1,11 +1,22 @@
|
||||||
function __fish_urlencode --description "URL-encode stdin"
|
function __fish_urlencode --description "URL-encode stdin"
|
||||||
set -l output
|
set -l join ''
|
||||||
set -l chars
|
set -l chars
|
||||||
# Set locale to C and IFS to "" in order to split a line into bytes.
|
# Set locale to C and IFS to "" in order to split a line into bytes.
|
||||||
while begin; set -lx LC_ALL C; set -lx IFS ''; read --array chars; end
|
while begin; set -lx LC_ALL C; set -lx IFS ''; read -az chars; end
|
||||||
if count $chars > /dev/null
|
printf '%s' $join
|
||||||
set output $output (printf '%%%02x' "'"$chars)
|
# 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
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
echo -s $output | sed -e 's/%2[fF]/\//g'
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue