URL decoding: Difference between revisions
promoted to task |
Added PicoLisp |
||
Line 51: | Line 51: | ||
Note that a minor efficiency improvement is possible, by eliminating duplicated escape codes: |
Note that a minor efficiency improvement is possible, by eliminating duplicated escape codes: |
||
<lang j>urldecode=: rplc&(~.,/;"_1&a."2(,:tolower)'%',.hfd i.#a.)</lang> |
<lang j>urldecode=: rplc&(~.,/;"_1&a."2(,:tolower)'%',.hfd i.#a.)</lang> |
||
=={{header|PicoLisp}}== |
|||
<pre>: (ht:Pack (chop "http%3A%2F%2Ffoo%20bar%2F")) |
|||
-> "http://foo bar/"</pre> |
|||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
Revision as of 09:37, 24 June 2011
You are encouraged to solve this task according to the task description, using any language you may know.
This task (the reverse of URL encoding) is to provide a function or mechanism to convert a url-encoded string into its original unencoded form.
Example
The encoded string "http%3A%2F%2Ffoo%20bar%2F
" should be reverted to the unencoded form "http://foo bar/
".
Icon and Unicon
<lang Icon>link hexcvt
procedure main() ue := "http%3A%2F%2Ffoo%20bar%2F" ud := decodeURL(ue) | stop("Improperly encoded string ",image(ue)) write("encoded = ",image(ue)) write("decoded = ",image(ue)) end
procedure decodeURL(s) #: decode URL/URI encoded data static de initial { # build lookup table for everything
de := table() every de[hexstring(ord(c := !string(&ascii)),2)] := c }
c := "" s ? until pos(0) do # decode every %xx or fail
c ||:= if ="%" then \de[move(2)] | fail else move(1)
return c end</lang>
Output:
encoded = "http%3A%2F%2Ffoo%20bar%2F" decoded = "http://foo bar/"
J
J does not have a native urldecode (until version 7 when jhs includes a jurldecode).
Here is an implementation:
<lang j>require'strings convert' urldecode=: rplc&(;"_1&a."2(,:tolower)'%',.hfd i.#a.)</lang>
Example use:
<lang j> urldecode 'http%3A%2F%2Ffoo%20bar%2F' http://foo bar/</lang>
Note that a minor efficiency improvement is possible, by eliminating duplicated escape codes: <lang j>urldecode=: rplc&(~.,/;"_1&a."2(,:tolower)'%',.hfd i.#a.)</lang>
PicoLisp
: (ht:Pack (chop "http%3A%2F%2Ffoo%20bar%2F")) -> "http://foo bar/"
PureBasic
<lang PureBasic>URL$ = URLDecoder("http%3A%2F%2Ffoo%20bar%2F")
Debug URL$ ; http://foo bar/</lang>
Retro
This is provided by the casket library (used for web app development).
<lang Retro>create buffer 32000 allot
{{
create bit 5 allot : extract ( $c-$a ) drop @+ bit ! @+ bit 1+ ! bit ; : render ( $c-$n ) dup '+ = [ drop 32 ] ifTrue dup 13 = [ drop 32 ] ifTrue dup 10 = [ drop 32 ] ifTrue dup '% = [ extract hex toNumber decimal ] ifTrue ; : <decode> ( $-$ ) repeat @+ 0; render ^buffer'add again ;
---reveal---
: decode ( $- ) buffer ^buffer'set <decode> drop ;
}}
"http%3A%2F%2Ffoo%20bar%2F" decode buffer puts</lang>
Tcl
This code is careful to ensure that any untoward metacharacters in the input string still do not cause any problems. <lang tcl>proc urlDecode {str} {
set specialMap {"[" "%5B" "]" "%5D"} set seqRE {%([0-9a-fA-F]{2})} set replacement {[format "%c" [scan "\1" "%2x"]]} set modStr [regsub -all $seqRE [string map $specialMap $str] $replacement] return [encoding convertfrom utf-8 [subst -nobackslash -novariable $modStr]]
}</lang> Demonstrating: <lang tcl>puts [urlDecode "http%3A%2F%2Ffoo%20bar%2F"]</lang> Output:
http://foo bar/