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

28 lines
713 B
Text
Raw Permalink Normal View History

2026-02-01 16:33:20 -08:00
-- demo\rosetta\encode_url.exw
with javascript_semantics
function nib(integer b)
return b+iff(b<=9?'0':'A'-10)
end function
2023-07-01 11:58:00 -04:00
2026-02-01 16:33:20 -08:00
function encode_url(string s, string exclusions="", bool spaceplus=false)
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
2023-07-01 11:58:00 -04:00
2026-02-01 16:33:20 -08:00
printf(1,"%s\n",{encode_url("http://foo bar/")})