URL decoding: Difference between revisions
J |
m Don't need these extra headings in the TOC |
||
Line 5: | Line 5: | ||
This task is the reverse of [[URL decoding]]. |
This task is the reverse of [[URL decoding]]. |
||
'''Example''' |
|||
The encoded string "<code><nowiki>http%3A%2F%2Ffoo%20bar%2F</nowiki></code>" should be reverted to the unencoded form "<code><nowiki>http://foo bar/</nowiki></code>" |
The encoded string "<code><nowiki>http%3A%2F%2Ffoo%20bar%2F</nowiki></code>" should be reverted to the unencoded form "<code><nowiki>http://foo bar/</nowiki></code>" |
||
'''See also:''' |
|||
[[URL encoding]] |
[[URL encoding]] |
Revision as of 15:27, 17 June 2011
The task is to provide a function or mechanism to convert a url encoded string into its original unencoded form.
This task is the reverse of URL decoding.
Example
The encoded string "http%3A%2F%2Ffoo%20bar%2F
" should be reverted to the unencoded form "http://foo bar/
"
See also:
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>
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>