RosettaCodeData/Task/URL-encoding/Phix/url-encoding.phix

30 lines
720 B
Plaintext

--
-- demo\rosetta\encode_url.exw
-- ===========================
--
function nib(integer b)
return b+iff(b<=9?'0':'A'-10)
end function
function encode_url(string s, string exclusions="", integer spaceplus=0)
string res = ""
for i=1 to length(s) do
integer ch = s[i]
if ch=' ' and spaceplus then
ch = '+'
elsif not find(ch,exclusions)
and (ch<'0'
or (ch>'9' and ch<'A')
or (ch>'Z' and ch<'a')
or ch>'z') then
res &= '%'
res &= nib(floor(ch/#10))
ch = nib(and_bits(ch,#0F))
end if
res &= ch
end for
return res
end function
printf(1,"%s\n",{encode_url("http://foo bar/")})