URL decoding: Difference between revisions
Content added Content deleted
SqrtNegInf (talk | contribs) m (→{{header|Raku}}: use 'xdigit', better documented) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 13: | Line 13: | ||
=={{header|11l}}== |
=={{header|11l}}== |
||
< |
<syntaxhighlight lang="11l">F url_decode(s) |
||
V r = ‘’ |
V r = ‘’ |
||
V i = 0 |
V i = 0 |
||
Line 30: | Line 30: | ||
print(url_decode(‘http%3A%2F%2Ffoo%20bar%2F’)) |
print(url_decode(‘http%3A%2F%2Ffoo%20bar%2F’)) |
||
print(url_decode(‘https://ru.wikipedia.org/wiki/%D0%A2%D1%80%D0%B0%D0%BD%D1%81%D0%BF%D0%B0%D0%B9%D0%BB%D0%B5%D1%80’))</ |
print(url_decode(‘https://ru.wikipedia.org/wiki/%D0%A2%D1%80%D0%B0%D0%BD%D1%81%D0%BF%D0%B0%D0%B9%D0%BB%D0%B5%D1%80’))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 40: | Line 40: | ||
=={{header|ABAP}}== |
=={{header|ABAP}}== |
||
< |
<syntaxhighlight lang="abap">REPORT Z_DECODE_URL. |
||
DATA: lv_encoded_url TYPE string VALUE 'http%3A%2F%2Ffoo%20bar%2F', |
DATA: lv_encoded_url TYPE string VALUE 'http%3A%2F%2Ffoo%20bar%2F', |
||
Line 51: | Line 51: | ||
UNESCAPED = lv_decoded_url. |
UNESCAPED = lv_decoded_url. |
||
WRITE: 'Encoded URL: ', lv_encoded_url, /, 'Decoded URL: ', lv_decoded_url.</ |
WRITE: 'Encoded URL: ', lv_encoded_url, /, 'Decoded URL: ', lv_decoded_url.</syntaxhighlight> |
||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
< |
<syntaxhighlight lang="action!">PROC Append(CHAR ARRAY s CHAR c) |
||
s(0)==+1 |
s(0)==+1 |
||
s(s(0))=c |
s(s(0))=c |
||
Line 118: | Line 118: | ||
Test("http%3A%2F%2Ffoo%20bar%2F") |
Test("http%3A%2F%2Ffoo%20bar%2F") |
||
Test("http%3A%2F%2Ffoo+bar%2F*_-.html") |
Test("http%3A%2F%2Ffoo+bar%2F*_-.html") |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/URL_decoding.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/URL_decoding.png Screenshot from Atari 8-bit computer] |
||
Line 131: | Line 131: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
{{libheader|AWS}} |
{{libheader|AWS}} |
||
< |
<syntaxhighlight lang="ada">with AWS.URL; |
||
with Ada.Text_IO; use Ada.Text_IO; |
with Ada.Text_IO; use Ada.Text_IO; |
||
procedure Decode is |
procedure Decode is |
||
Line 138: | Line 138: | ||
Put_Line (AWS.URL.Decode (Encoded)); |
Put_Line (AWS.URL.Decode (Encoded)); |
||
end Decode; |
end Decode; |
||
</syntaxhighlight> |
|||
</lang> |
|||
Without external libraries: |
Without external libraries: |
||
< |
<syntaxhighlight lang="ada">package URL is |
||
function Decode (URL : in String) return String; |
function Decode (URL : in String) return String; |
||
end URL;</ |
end URL;</syntaxhighlight> |
||
< |
<syntaxhighlight lang="ada">package body URL is |
||
function Decode (URL : in String) return String is |
function Decode (URL : in String) return String is |
||
Buffer : String (1 .. URL'Length); |
Buffer : String (1 .. URL'Length); |
||
Line 173: | Line 173: | ||
return Buffer (1 .. Filled); |
return Buffer (1 .. Filled); |
||
end Decode; |
end Decode; |
||
end URL;</ |
end URL;</syntaxhighlight> |
||
< |
<syntaxhighlight lang="ada">with Ada.Command_Line, |
||
Ada.Text_IO; |
Ada.Text_IO; |
||
Line 190: | Line 190: | ||
end loop; |
end loop; |
||
end if; |
end if; |
||
end Test_URL_Decode;</ |
end Test_URL_Decode;</syntaxhighlight> |
||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
Line 197: | Line 197: | ||
<br> |
<br> |
||
For the second task example, this outputs the encoded UTF-8 characters, what you see depends on what you look at it with... |
For the second task example, this outputs the encoded UTF-8 characters, what you see depends on what you look at it with... |
||
< |
<syntaxhighlight lang="algol68"># returns c decoded as a hex digit # |
||
PROC hex value = ( CHAR c )INT: IF c >= "0" AND c <= "9" THEN ABS c - ABS "0" |
PROC hex value = ( CHAR c )INT: IF c >= "0" AND c <= "9" THEN ABS c - ABS "0" |
||
ELIF c >= "A" AND c <= "F" THEN 10 + ( ABS c - ABS "A" ) |
ELIF c >= "A" AND c <= "F" THEN 10 + ( ABS c - ABS "A" ) |
||
Line 232: | Line 232: | ||
# test the url decode procedure # |
# test the url decode procedure # |
||
print( ( url decode( "http%3A%2F%2Ffoo%20bar%2F" ), newline ) ); |
print( ( url decode( "http%3A%2F%2Ffoo%20bar%2F" ), newline ) ); |
||
print( ( url decode( "google.com/search?q=%60Abdu%27l-Bah%C3%A1" ), newline ) )</ |
print( ( url decode( "google.com/search?q=%60Abdu%27l-Bah%C3%A1" ), newline ) )</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 240: | Line 240: | ||
=={{header|Apex}}== |
=={{header|Apex}}== |
||
< |
<syntaxhighlight lang="apex">EncodingUtil.urlDecode('http%3A%2F%2Ffoo%20bar%2F', 'UTF-8'); |
||
EncodingUtil.urlDecode('google.com/search?q=%60Abdu%27l-Bah%C3%A1', 'UTF-8');</ |
EncodingUtil.urlDecode('google.com/search?q=%60Abdu%27l-Bah%C3%A1', 'UTF-8');</syntaxhighlight> |
||
<pre>http://foo bar/ |
<pre>http://foo bar/ |
||
google.com/search?q=`Abdu'l-Bahá</pre> |
google.com/search?q=`Abdu'l-Bahá</pre> |
||
Line 247: | Line 247: | ||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
{{libheader|AppleScript Toolbox}} |
{{libheader|AppleScript Toolbox}} |
||
< |
<syntaxhighlight lang="applescript">AST URL decode "google.com/search?q=%60Abdu%27l-Bah%C3%A1"</syntaxhighlight> |
||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang="rebol">print decode.url "http%3A%2F%2Ffoo%20bar%2F" |
||
print decode.url "google.com/search?q=%60Abdu%27l-Bah%C3%A1"</ |
print decode.url "google.com/search?q=%60Abdu%27l-Bah%C3%A1"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 260: | Line 260: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
< |
<syntaxhighlight lang="autohotkey">encURL := "http%3A%2F%2Ffoo%20bar%2F" |
||
SetFormat, Integer, hex |
SetFormat, Integer, hex |
||
Loop Parse, encURL |
Loop Parse, encURL |
||
Line 273: | Line 273: | ||
else out .= A_LoopField |
else out .= A_LoopField |
||
MsgBox % out ; http://foo bar/ |
MsgBox % out ; http://foo bar/ |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: |
# syntax: |
||
awk ' |
awk ' |
||
Line 299: | Line 299: | ||
return num + (length(s) ? 16*hex2dec(s) : 0) |
return num + (length(s) ? 16*hex2dec(s) : 0) |
||
} ' |
} ' |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 308: | Line 308: | ||
OR: |
OR: |
||
< |
<syntaxhighlight lang="awk"> |
||
LC_ALL=C |
LC_ALL=C |
||
echo "http%3A%2F%2Ffoo%20bar%2F" | gawk -vRS='%[[:xdigit:]]{2}' ' |
echo "http%3A%2F%2Ffoo%20bar%2F" | gawk -vRS='%[[:xdigit:]]{2}' ' |
||
RT {RT = sprintf("%c",strtonum("0x" substr(RT, 2)))} |
RT {RT = sprintf("%c",strtonum("0x" substr(RT, 2)))} |
||
{gsub(/+/," ");printf "%s", $0 RT}' |
{gsub(/+/," ");printf "%s", $0 RT}' |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 321: | Line 321: | ||
=={{header|BaCon}}== |
=={{header|BaCon}}== |
||
< |
<syntaxhighlight lang="freebasic">FUNCTION Url_Decode$(url$) |
||
LOCAL result$ |
LOCAL result$ |
||
Line 334: | Line 334: | ||
PRINT Url_Decode$("http%3A%2F%2Ffoo%20bar%2F") |
PRINT Url_Decode$("http%3A%2F%2Ffoo%20bar%2F") |
||
PRINT Url_Decode$("google.com/search?q=%60Abdu%27l-Bah%C3%A1")</ |
PRINT Url_Decode$("google.com/search?q=%60Abdu%27l-Bah%C3%A1")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 343: | Line 343: | ||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
{{works with|BBC BASIC for Windows}} |
{{works with|BBC BASIC for Windows}} |
||
< |
<syntaxhighlight lang="bbcbasic"> PRINT FNurldecode("http%3A%2F%2Ffoo%20bar%2F") |
||
END |
END |
||
Line 364: | Line 364: | ||
IF C% >= 97 IF C% <= 122 MID$(A$,A%,1) = CHR$(C%-32) |
IF C% >= 97 IF C% <= 122 MID$(A$,A%,1) = CHR$(C%-32) |
||
NEXT |
NEXT |
||
= A$</ |
= A$</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 371: | Line 371: | ||
=={{header|Bracmat}}== |
=={{header|Bracmat}}== |
||
< |
<syntaxhighlight lang="bracmat">( ( decode |
||
= decoded hexcode notencoded |
= decoded hexcode notencoded |
||
. :?decoded |
. :?decoded |
||
Line 381: | Line 381: | ||
) |
) |
||
& out$(decode$http%3A%2F%2Ffoo%20bar%2F) |
& out$(decode$http%3A%2F%2Ffoo%20bar%2F) |
||
);</ |
);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>http://foo bar/</pre> |
<pre>http://foo bar/</pre> |
||
=={{header|C}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <string.h> |
#include <string.h> |
||
Line 425: | Line 425: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C sharp}}== |
=={{header|C sharp}}== |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
namespace URLEncode |
namespace URLEncode |
||
Line 445: | Line 445: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 455: | Line 455: | ||
{{libheader|Poco}} |
{{libheader|Poco}} |
||
{{works with|g++}} |
{{works with|g++}} |
||
< |
<syntaxhighlight lang="cpp">#include <string> |
||
#include "Poco/URI.h" |
#include "Poco/URI.h" |
||
#include <iostream> |
#include <iostream> |
||
Line 465: | Line 465: | ||
std::cout << encoded << " is decoded: " << decoded << " !" << std::endl ; |
std::cout << encoded << " is decoded: " << decoded << " !" << std::endl ; |
||
return 0 ; |
return 0 ; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>http%3A%2F%2Ffoo%20bar%2F is decoded: http://foo bar/ !</pre> |
<pre>http%3A%2F%2Ffoo%20bar%2F is decoded: http://foo bar/ !</pre> |
||
Line 476: | Line 476: | ||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
< |
<syntaxhighlight lang="clojure">(java.net.URLDecoder/decode "http%3A%2F%2Ffoo%20bar%2F")</syntaxhighlight> |
||
=={{header|CoffeeScript}}== |
=={{header|CoffeeScript}}== |
||
< |
<syntaxhighlight lang="coffeescript"> |
||
console.log decodeURIComponent "http%3A%2F%2Ffoo%20bar%2F?name=Foo%20Barson" |
console.log decodeURIComponent "http%3A%2F%2Ffoo%20bar%2F?name=Foo%20Barson" |
||
</syntaxhighlight> |
|||
</lang> |
|||
<lang> |
<syntaxhighlight lang="text"> |
||
> coffee foo.coffee |
> coffee foo.coffee |
||
http://foo bar/?name=Foo Barson |
http://foo bar/?name=Foo Barson |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
< |
<syntaxhighlight lang="lisp">(defun decode (string &key start) |
||
(assert (char= (char string start) #\%)) |
(assert (char= (char string start) #\%)) |
||
(if (>= (length string) (+ start 3)) |
(if (>= (length string) (+ start 3)) |
||
Line 512: | Line 512: | ||
finally (return (apply #'concatenate 'string chunks)))) |
finally (return (apply #'concatenate 'string chunks)))) |
||
(url-decode "http%3A%2F%2Ffoo%20bar%2F")</ |
(url-decode "http%3A%2F%2Ffoo%20bar%2F")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>"http://foo bar/"</pre> |
<pre>"http://foo bar/"</pre> |
||
=={{header|Crystal}}== |
=={{header|Crystal}}== |
||
< |
<syntaxhighlight lang="crystal">require "uri" |
||
puts URI.decode "http%3A%2F%2Ffoo%20bar%2F" |
puts URI.decode "http%3A%2F%2Ffoo%20bar%2F" |
||
puts URI.decode "google.com/search?q=%60Abdu%27l-Bah%C3%A1"</ |
puts URI.decode "google.com/search?q=%60Abdu%27l-Bah%C3%A1"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>http://foo bar/ |
<pre>http://foo bar/ |
||
Line 526: | Line 526: | ||
=={{header|D}}== |
=={{header|D}}== |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.uri; |
||
void main() { |
void main() { |
||
writeln(decodeComponent("http%3A%2F%2Ffoo%20bar%2F")); |
writeln(decodeComponent("http%3A%2F%2Ffoo%20bar%2F")); |
||
}</ |
}</syntaxhighlight> |
||
<pre>http://foo bar/</pre> |
<pre>http://foo bar/</pre> |
||
=={{header|Delphi}}== |
=={{header|Delphi}}== |
||
< |
<syntaxhighlight lang="delphi">program URLEncoding; |
||
{$APPTYPE CONSOLE} |
{$APPTYPE CONSOLE} |
||
Line 542: | Line 542: | ||
begin |
begin |
||
Writeln(TIdURI.URLDecode('http%3A%2F%2Ffoo%20bar%2F')); |
Writeln(TIdURI.URLDecode('http%3A%2F%2Ffoo%20bar%2F')); |
||
end.</ |
end.</syntaxhighlight> |
||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
< |
<syntaxhighlight lang="elixir">IO.inspect URI.decode("http%3A%2F%2Ffoo%20bar%2F") |
||
IO.inspect URI.decode("google.com/search?q=%60Abdu%27l-Bah%C3%A1")</ |
IO.inspect URI.decode("google.com/search?q=%60Abdu%27l-Bah%C3%A1")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 563: | Line 563: | ||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
{{trans|C#}} |
{{trans|C#}} |
||
< |
<syntaxhighlight lang="fsharp">open System |
||
let decode uri = Uri.UnescapeDataString(uri) |
let decode uri = Uri.UnescapeDataString(uri) |
||
Line 570: | Line 570: | ||
let main argv = |
let main argv = |
||
printfn "%s" (decode "http%3A%2F%2Ffoo%20bar%2F") |
printfn "%s" (decode "http%3A%2F%2Ffoo%20bar%2F") |
||
0</ |
0</syntaxhighlight> |
||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
< |
<syntaxhighlight lang="factor">USING: io kernel urls.encoding ; |
||
IN: rosetta-code.url-decoding |
IN: rosetta-code.url-decoding |
||
"http%3A%2F%2Ffoo%20bar%2F" |
"http%3A%2F%2Ffoo%20bar%2F" |
||
"google.com/search?q=%60Abdu%27l-Bah%C3%A1" |
"google.com/search?q=%60Abdu%27l-Bah%C3%A1" |
||
[ url-decode print ] bi@</ |
[ url-decode print ] bi@</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 587: | Line 587: | ||
=={{header|Free Pascal}}== |
=={{header|Free Pascal}}== |
||
< |
<syntaxhighlight lang="pascal">function urlDecode(data: String): AnsiString; |
||
var |
var |
||
ch: Char; |
ch: Char; |
||
Line 612: | Line 612: | ||
pos := pos +1; |
pos := pos +1; |
||
end; |
end; |
||
end;</ |
end;</syntaxhighlight> |
||
Line 618: | Line 618: | ||
{{trans|Liberty BASIC}} |
{{trans|Liberty BASIC}} |
||
{{trans|Pascal}} |
{{trans|Pascal}} |
||
< |
<syntaxhighlight lang="freebasic"> |
||
Const alphanum = "0123456789abcdefghijklmnopqrstuvwxyz" |
Const alphanum = "0123456789abcdefghijklmnopqrstuvwxyz" |
||
Line 658: | Line 658: | ||
Print "URL decoding '"; url2string(URL); "'" |
Print "URL decoding '"; url2string(URL); "'" |
||
Sleep |
Sleep |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Frink}}== |
=={{header|Frink}}== |
||
While the default is to decode parameters as UTF-8 (which is the W3C recommendation,) the characters may have been encoded in another encoding scheme, and this can be handled correctly. |
While the default is to decode parameters as UTF-8 (which is the W3C recommendation,) the characters may have been encoded in another encoding scheme, and this can be handled correctly. |
||
< |
<syntaxhighlight lang="frink">URLDecode["google.com/search?q=%60Abdu%27l-Bah%C3%A1","UTF8"]</syntaxhighlight> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 685: | Line 685: | ||
fmt.Println(u) |
fmt.Println(u) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 693: | Line 693: | ||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
< |
<syntaxhighlight lang="groovy">assert URLDecoder.decode('http%3A%2F%2Ffoo%20bar%2F') == 'http://foo bar/'</syntaxhighlight> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import qualified Data.Char as Char |
||
urlDecode :: String -> Maybe String |
urlDecode :: String -> Maybe String |
||
Line 710: | Line 710: | ||
main :: IO () |
main :: IO () |
||
main = putStrLn . maybe "Bad decode" id $ urlDecode "http%3A%2F%2Ffoo%20bar%2F"</ |
main = putStrLn . maybe "Bad decode" id $ urlDecode "http%3A%2F%2Ffoo%20bar%2F"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>http://foo bar/</pre> |
<pre>http://foo bar/</pre> |
||
Another approach: |
Another approach: |
||
< |
<syntaxhighlight lang="haskell">import Data.Char (chr) |
||
import Data.List.Split (splitOn) |
import Data.List.Split (splitOn) |
||
Line 727: | Line 727: | ||
-- TEST ------------------------------------------------------------------------ |
-- TEST ------------------------------------------------------------------------ |
||
main :: IO () |
main :: IO () |
||
main = putStrLn $ deCode "http%3A%2F%2Ffoo%20bar%2F"</ |
main = putStrLn $ deCode "http%3A%2F%2Ffoo%20bar%2F"</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>http://foo bar/</pre> |
<pre>http://foo bar/</pre> |
||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
< |
<syntaxhighlight lang="icon">link hexcvt |
||
procedure main() |
procedure main() |
||
Line 753: | Line 753: | ||
else move(1) |
else move(1) |
||
return c |
return c |
||
end</ |
end</syntaxhighlight> |
||
{{libheader|Icon Programming Library}} |
{{libheader|Icon Programming Library}} |
||
Line 767: | Line 767: | ||
Here is an implementation: |
Here is an implementation: |
||
< |
<syntaxhighlight lang="j">require'strings convert' |
||
urldecode=: rplc&(~.,/;"_1&a."2(,:tolower)'%',.toupper hfd i.#a.)</ |
urldecode=: rplc&(~.,/;"_1&a."2(,:tolower)'%',.toupper hfd i.#a.)</syntaxhighlight> |
||
Example use: |
Example use: |
||
< |
<syntaxhighlight lang="j"> urldecode 'http%3A%2F%2Ffoo%20bar%2F' |
||
http://foo bar/</ |
http://foo bar/</syntaxhighlight> |
||
Note that an earlier implementation assumed the j6 implementation of <code>hfd</code> which where hexadecimal letters resulting from <code>hfd</code> were upper case. J8, in contrast, provides a lower case result from hfd. The addition of <code>toupper</code> guarantees the case insensitivity required by [http://tools.ietf.org/html/rfc3986#section-2.1 RFC 3986] regardless of which version of J you are using. As the parenthesized expression containing <code>hfd</code> is only evaluated at definition time, there's no performance penalty from the use of <code>toupper</code>. |
Note that an earlier implementation assumed the j6 implementation of <code>hfd</code> which where hexadecimal letters resulting from <code>hfd</code> were upper case. J8, in contrast, provides a lower case result from hfd. The addition of <code>toupper</code> guarantees the case insensitivity required by [http://tools.ietf.org/html/rfc3986#section-2.1 RFC 3986] regardless of which version of J you are using. As the parenthesized expression containing <code>hfd</code> is only evaluated at definition time, there's no performance penalty from the use of <code>toupper</code>. |
||
Line 779: | Line 779: | ||
Example use: |
Example use: |
||
< |
<syntaxhighlight lang="j"> urldecode 'google.com/search?q=%60Abdu%27l-Bah%C3%A1' |
||
google.com/search?q=`Abdu'l-Bahá</ |
google.com/search?q=`Abdu'l-Bahá</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
< |
<syntaxhighlight lang="java">import java.io.UnsupportedEncodingException; |
||
import java.net.URLDecoder; |
import java.net.URLDecoder; |
||
Line 795: | Line 795: | ||
System.out.println(normal); |
System.out.println(normal); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 801: | Line 801: | ||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
< |
<syntaxhighlight lang="javascript">decodeURIComponent("http%3A%2F%2Ffoo%20bar%2F")</syntaxhighlight> |
||
=={{header|jq}}== |
=={{header|jq}}== |
||
{{works with|jq|1.4}} |
{{works with|jq|1.4}} |
||
If your jq already has "until", then the definition given below may be omitted. |
If your jq already has "until", then the definition given below may be omitted. |
||
< |
<syntaxhighlight lang="jq"># Emit . and stop as soon as "condition" is true. |
||
def until(condition; next): |
def until(condition; next): |
||
def u: if condition then . else (next|u) end; |
def u: if condition then . else (next|u) end; |
||
Line 832: | Line 832: | ||
else [ $i + 1, .[1] + $in[$i:$i+1] ] |
else [ $i + 1, .[1] + $in[$i:$i+1] ] |
||
end) |
end) |
||
| .[1]; # answer</ |
| .[1]; # answer</syntaxhighlight> |
||
'''Example''': |
'''Example''': |
||
< |
<syntaxhighlight lang="jq">"http%3A%2F%2Ffoo%20bar%2F" | url_decode</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<syntaxhighlight lang="sh"> |
|||
<lang sh> |
|||
"http://foo bar/"</ |
"http://foo bar/"</syntaxhighlight> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
<syntaxhighlight lang="julia"> |
|||
<lang Julia> |
|||
using URIParser |
using URIParser |
||
Line 848: | Line 848: | ||
println(enc, " => ", dcd) |
println(enc, " => ", dcd) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 856: | Line 856: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang="scala">// version 1.1.2 |
||
import java.net.URLDecoder |
import java.net.URLDecoder |
||
Line 863: | Line 863: | ||
val encoded = arrayOf("http%3A%2F%2Ffoo%20bar%2F", "google.com/search?q=%60Abdu%27l-Bah%C3%A1") |
val encoded = arrayOf("http%3A%2F%2Ffoo%20bar%2F", "google.com/search?q=%60Abdu%27l-Bah%C3%A1") |
||
for (e in encoded) println(URLDecoder.decode(e, "UTF-8")) |
for (e in encoded) println(URLDecoder.decode(e, "UTF-8")) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 872: | Line 872: | ||
=={{header|Ksh}}== |
=={{header|Ksh}}== |
||
<syntaxhighlight lang="ksh"> |
|||
<lang Ksh> |
|||
url_decode() |
url_decode() |
||
{ |
{ |
||
Line 881: | Line 881: | ||
url_decode "http%3A%2F%2Ffoo%20bar%2F" |
url_decode "http%3A%2F%2Ffoo%20bar%2F" |
||
url_decode "google.com/search?q=%60Abdu%27l-Bah%C3%A1" |
url_decode "google.com/search?q=%60Abdu%27l-Bah%C3%A1" |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 891: | Line 891: | ||
=={{header|Lambdatalk}}== |
=={{header|Lambdatalk}}== |
||
Currently lambdatalk has no builtin primitive for decoding URLs. Let's define it using Javascript. |
Currently lambdatalk has no builtin primitive for decoding URLs. Let's define it using Javascript. |
||
< |
<syntaxhighlight lang="scheme"> |
||
1) define a new javascript primitive: |
1) define a new javascript primitive: |
||
{script |
{script |
||
Line 905: | Line 905: | ||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Lasso}}== |
=={{header|Lasso}}== |
||
< |
<syntaxhighlight lang="lasso">bytes('http%3A%2F%2Ffoo%20bar%2F') -> decodeurl</syntaxhighlight> |
||
-> http://foo bar/ |
-> http://foo bar/ |
||
=={{header|Liberty BASIC}}== |
=={{header|Liberty BASIC}}== |
||
<syntaxhighlight lang="lb"> |
|||
<lang lb> |
|||
dim lookUp$( 256) |
dim lookUp$( 256) |
||
Line 938: | Line 938: | ||
next j |
next j |
||
end function |
end function |
||
</syntaxhighlight> |
|||
</lang> |
|||
Supplied URL 'http%3A%2F%2Ffoo%20bar%2F' |
Supplied URL 'http%3A%2F%2Ffoo%20bar%2F' |
||
As string 'http://foo bar/' |
As string 'http://foo bar/' |
||
=={{header|Lingo}}== |
=={{header|Lingo}}== |
||
< |
<syntaxhighlight lang="lingo">---------------------------------------- |
||
-- URL decodes a string |
-- URL decodes a string |
||
-- @param {string} str |
-- @param {string} str |
||
Line 966: | Line 966: | ||
ba.position = 1 |
ba.position = 1 |
||
return ba.readRawString(ba.length) |
return ba.readRawString(ba.length) |
||
end</ |
end</syntaxhighlight> |
||
< |
<syntaxhighlight lang="lingo">put urldecode("http%3A%2F%2Ffoo%20bar%2F") |
||
put urldecode("google.com/search?q=%60Abdu%27l-Bah%C3%A1")</ |
put urldecode("google.com/search?q=%60Abdu%27l-Bah%C3%A1")</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 976: | Line 976: | ||
=={{header|LiveCode}}== |
=={{header|LiveCode}}== |
||
< |
<syntaxhighlight lang="livecode">put urlDecode("http%3A%2F%2Ffoo%20bar%2F") & cr & \ |
||
urlDecode("google.com/search?q=%60Abdu%27l-Bah%C3%A1")</ |
urlDecode("google.com/search?q=%60Abdu%27l-Bah%C3%A1")</syntaxhighlight>Results<syntaxhighlight lang="text">http://foo bar/ |
||
google.com/search?q=`Abdu'l-Bah√°</ |
google.com/search?q=`Abdu'l-Bah√°</syntaxhighlight> |
||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang="lua">function decodeChar(hex) |
||
return string.char(tonumber(hex,16)) |
return string.char(tonumber(hex,16)) |
||
end |
end |
||
Line 991: | Line 991: | ||
-- will print "http://foo bar/" |
-- will print "http://foo bar/" |
||
print(decodeString("http%3A%2F%2Ffoo%20bar%2F"))</ |
print(decodeString("http%3A%2F%2Ffoo%20bar%2F"))</syntaxhighlight> |
||
=={{header|M2000 Interpreter}}== |
=={{header|M2000 Interpreter}}== |
||
Line 1,001: | Line 1,001: | ||
b$=chr$(a$) revert bytes to words adding zeroes after each character |
b$=chr$(a$) revert bytes to words adding zeroes after each character |
||
<syntaxhighlight lang="m2000 interpreter"> |
|||
<lang M2000 Interpreter> |
|||
Module CheckIt { |
Module CheckIt { |
||
Function decodeUrl$(a$) { |
Function decodeUrl$(a$) { |
||
Line 1,027: | Line 1,027: | ||
} |
} |
||
CheckIt |
CheckIt |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
< |
<syntaxhighlight lang="maple">StringTools:-Decode("http%3A%2F%2Ffoo%20bar%2F", encoding=percent);</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 1,036: | Line 1,036: | ||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">URLDecoding[url_] := |
||
StringReplace[url, "%" ~~ x_ ~~ y_ :> FromDigits[x ~~ y, 16]] //. |
StringReplace[url, "%" ~~ x_ ~~ y_ :> FromDigits[x ~~ y, 16]] //. |
||
StringExpression[x___, Longest[n__Integer], y___] :> |
StringExpression[x___, Longest[n__Integer], y___] :> |
||
StringExpression[x, FromCharacterCode[{n}, "UTF8"], y]</ |
StringExpression[x, FromCharacterCode[{n}, "UTF8"], y]</syntaxhighlight> |
||
Example use: |
Example use: |
||
< |
<syntaxhighlight lang="mathematica">URLDecoding["http%3A%2F%2Ffoo%20bar%2F"]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>http://foo bar/</pre> |
<pre>http://foo bar/</pre> |
||
Line 1,061: | Line 1,061: | ||
=={{header|MATLAB}} / {{header|Octave}}== |
=={{header|MATLAB}} / {{header|Octave}}== |
||
< |
<syntaxhighlight lang="matlab">function u = urldecoding(s) |
||
u = ''; |
u = ''; |
||
k = 1; |
k = 1; |
||
Line 1,073: | Line 1,073: | ||
end |
end |
||
end |
end |
||
end</ |
end</syntaxhighlight> |
||
Usage: |
Usage: |
||
<pre>octave:3> urldecoding('http%3A%2F%2Ffoo%20bar%2F') |
<pre>octave:3> urldecoding('http%3A%2F%2Ffoo%20bar%2F') |
||
Line 1,079: | Line 1,079: | ||
=={{header|NetRexx}}== |
=={{header|NetRexx}}== |
||
< |
<syntaxhighlight lang="netrexx">/* NetRexx */ |
||
options replace format comments java crossref savelog symbols nobinary |
options replace format comments java crossref savelog symbols nobinary |
||
Line 1,122: | Line 1,122: | ||
return decoded |
return decoded |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,137: | Line 1,137: | ||
=={{header|NewLISP}}== |
=={{header|NewLISP}}== |
||
< |
<syntaxhighlight lang="newlisp">;; universal decoder, works for ASCII and UTF-8 |
||
;; (source http://www.newlisp.org/index.cgi?page=Code_Snippets) |
;; (source http://www.newlisp.org/index.cgi?page=Code_Snippets) |
||
(define (url-decode url (opt nil)) |
(define (url-decode url (opt nil)) |
||
Line 1,143: | Line 1,143: | ||
(replace "%([0-9a-f][0-9a-f])" url (pack "b" (int $1 0 16)) 1)) |
(replace "%([0-9a-f][0-9a-f])" url (pack "b" (int $1 0 16)) 1)) |
||
(url-decode "http%3A%2F%2Ffoo%20bar%2F")</ |
(url-decode "http%3A%2F%2Ffoo%20bar%2F")</syntaxhighlight> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">import cgi |
||
echo decodeUrl("http%3A%2F%2Ffoo%20bar%2F")</ |
echo decodeUrl("http%3A%2F%2Ffoo%20bar%2F")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,155: | Line 1,155: | ||
=={{header|Oberon-2}}== |
=={{header|Oberon-2}}== |
||
{{works with|oo2c}} |
{{works with|oo2c}} |
||
< |
<syntaxhighlight lang="oberon2"> |
||
MODULE URLDecoding; |
MODULE URLDecoding; |
||
IMPORT |
IMPORT |
||
Line 1,164: | Line 1,164: | ||
Out.String(URI.Unescape("google.com/search?q=%60Abdu%27l-Bah%C3%A1"));Out.Ln; |
Out.String(URI.Unescape("google.com/search?q=%60Abdu%27l-Bah%C3%A1"));Out.Ln; |
||
END URLDecoding. |
END URLDecoding. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,172: | Line 1,172: | ||
=={{header|Objeck}}== |
=={{header|Objeck}}== |
||
< |
<syntaxhighlight lang="objeck"> |
||
class UrlDecode { |
class UrlDecode { |
||
function : Main(args : String[]) ~ Nil { |
function : Main(args : String[]) ~ Nil { |
||
Line 1,178: | Line 1,178: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Objective-C}}== |
=={{header|Objective-C}}== |
||
< |
<syntaxhighlight lang="objc">NSString *encoded = @"http%3A%2F%2Ffoo%20bar%2F"; |
||
NSString *normal = [encoded stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; |
NSString *normal = [encoded stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; |
||
NSLog(@"%@", normal);</ |
NSLog(@"%@", normal);</syntaxhighlight> |
||
{{works with|Mac OS X|10.9+}}{{works with|iOS|7+}} |
{{works with|Mac OS X|10.9+}}{{works with|iOS|7+}} |
||
< |
<syntaxhighlight lang="objc">NSString *encoded = @"http%3A%2F%2Ffoo%20bar%2F"; |
||
NSString *normal = [encoded stringByRemovingPercentEncoding]; |
NSString *normal = [encoded stringByRemovingPercentEncoding]; |
||
NSLog(@"%@", normal);</ |
NSLog(@"%@", normal);</syntaxhighlight> |
||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
Line 1,193: | Line 1,193: | ||
Using the library [http://projects.camlcity.org/projects/ocamlnet.html ocamlnet] from the interactive loop: |
Using the library [http://projects.camlcity.org/projects/ocamlnet.html ocamlnet] from the interactive loop: |
||
< |
<syntaxhighlight lang="ocaml">$ ocaml |
||
# #use "topfind";; |
# #use "topfind";; |
||
# #require "netstring";; |
# #require "netstring";; |
||
# Netencoding.Url.decode "http%3A%2F%2Ffoo%20bar%2F" ;; |
# Netencoding.Url.decode "http%3A%2F%2Ffoo%20bar%2F" ;; |
||
- : string = "http://foo bar/"</ |
- : string = "http://foo bar/"</syntaxhighlight> |
||
=={{header|ooRexx}}== |
=={{header|ooRexx}}== |
||
While the implementation shown for [[#REXX|Rexx]] will also work with ooRexx, this version uses ooRexx syntax to invoke the built-in functions. |
While the implementation shown for [[#REXX|Rexx]] will also work with ooRexx, this version uses ooRexx syntax to invoke the built-in functions. |
||
< |
<syntaxhighlight lang="oorexx">/* Rexx */ |
||
X = 0 |
X = 0 |
||
url. = '' |
url. = '' |
||
Line 1,241: | Line 1,241: | ||
End e_ |
End e_ |
||
Return decoded</ |
Return decoded</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,257: | Line 1,257: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">sub urldecode { |
||
my $s = shift; |
my $s = shift; |
||
$s =~ tr/\+/ /; |
$s =~ tr/\+/ /; |
||
Line 1,265: | Line 1,265: | ||
print urldecode('http%3A%2F%2Ffoo+bar%2F')."\n"; |
print urldecode('http%3A%2F%2Ffoo+bar%2F')."\n"; |
||
</syntaxhighlight> |
|||
</lang> |
|||
< |
<syntaxhighlight lang="perl">#!/usr/bin/perl -w |
||
use strict ; |
use strict ; |
||
use URI::Escape ; |
use URI::Escape ; |
||
Line 1,273: | Line 1,273: | ||
my $encoded = "http%3A%2F%2Ffoo%20bar%2F" ; |
my $encoded = "http%3A%2F%2Ffoo%20bar%2F" ; |
||
my $unencoded = uri_unescape( $encoded ) ; |
my $unencoded = uri_unescape( $encoded ) ; |
||
print "The unencoded string is $unencoded !\n" ;</ |
print "The unencoded string is $unencoded !\n" ;</syntaxhighlight> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #000080;font-style:italic;">-- |
<span style="color: #000080;font-style:italic;">-- |
||
-- demo\rosetta\decode_url.exw |
-- demo\rosetta\decode_url.exw |
||
Line 1,313: | Line 1,313: | ||
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span> |
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 1,321: | Line 1,321: | ||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
< |
<syntaxhighlight lang="php"><?php |
||
$encoded = "http%3A%2F%2Ffoo%20bar%2F"; |
$encoded = "http%3A%2F%2Ffoo%20bar%2F"; |
||
$unencoded = rawurldecode($encoded); |
$unencoded = rawurldecode($encoded); |
||
echo "The unencoded string is $unencoded !\n"; |
echo "The unencoded string is $unencoded !\n"; |
||
?></ |
?></syntaxhighlight> |
||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
Line 1,332: | Line 1,332: | ||
=={{header|Pike}}== |
=={{header|Pike}}== |
||
<syntaxhighlight lang="pike"> |
|||
<lang Pike> |
|||
void main() |
void main() |
||
{ |
{ |
||
Line 1,346: | Line 1,346: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 1,354: | Line 1,354: | ||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
<syntaxhighlight lang="powershell"> |
|||
<lang PowerShell> |
|||
[System.Web.HttpUtility]::UrlDecode("http%3A%2F%2Ffoo%20bar%2F") |
[System.Web.HttpUtility]::UrlDecode("http%3A%2F%2Ffoo%20bar%2F") |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 1,363: | Line 1,363: | ||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
< |
<syntaxhighlight lang="purebasic">URL$ = URLDecoder("http%3A%2F%2Ffoo%20bar%2F") |
||
Debug URL$ ; http://foo bar/</ |
Debug URL$ ; http://foo bar/</syntaxhighlight> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
<syntaxhighlight lang="python"> |
|||
<lang Python> |
|||
#Python 2.X |
#Python 2.X |
||
import urllib |
import urllib |
||
Line 1,375: | Line 1,375: | ||
from urllib.parse import unquote |
from urllib.parse import unquote |
||
print(unquote('http%3A%2F%2Ffoo%20bar%2F')) |
print(unquote('http%3A%2F%2Ffoo%20bar%2F')) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|R}}== |
=={{header|R}}== |
||
< |
<syntaxhighlight lang="r">URLdecode("http%3A%2F%2Ffoo%20bar%2F")</syntaxhighlight> |
||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket"> |
||
#lang racket |
#lang racket |
||
(require net/uri-codec) |
(require net/uri-codec) |
||
(uri-decode "http%3A%2F%2Ffoo%20bar%2F") |
(uri-decode "http%3A%2F%2Ffoo%20bar%2F") |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
(formerly Perl 6) |
(formerly Perl 6) |
||
<lang |
<syntaxhighlight lang="raku" line>my @urls = < http%3A%2F%2Ffoo%20bar%2F |
||
google.com/search?q=%60Abdu%27l-Bah%C3%A1 >; |
google.com/search?q=%60Abdu%27l-Bah%C3%A1 >; |
||
Line 1,397: | Line 1,397: | ||
/ [ '%' ( <xdigit> ** 2 ) ]+ / , |
/ [ '%' ( <xdigit> ** 2 ) ]+ / , |
||
{ Blob.new((:16(~$_) for $0)).decode } |
{ Blob.new((:16(~$_) for $0)).decode } |
||
) for @urls;</ |
) for @urls;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>http://foo bar/ |
<pre>http://foo bar/ |
||
Line 1,404: | Line 1,404: | ||
=={{header|Red}}== |
=={{header|Red}}== |
||
< |
<syntaxhighlight lang="rebol">>> dehex "http%3A%2F%2Ffoo%20bar%2F" |
||
== "http://foo bar/" |
== "http://foo bar/" |
||
>> dehex "google.com/search?q=%60Abdu%27l-Bah%C3%A1" |
>> dehex "google.com/search?q=%60Abdu%27l-Bah%C3%A1" |
||
== "google.com/search?q=`Abdu'l-Bahá"</ |
== "google.com/search?q=`Abdu'l-Bahá"</syntaxhighlight> |
||
=={{header|Retro}}== |
=={{header|Retro}}== |
||
This is provided by the '''casket''' library (used for web app development). |
This is provided by the '''casket''' library (used for web app development). |
||
< |
<syntaxhighlight lang="retro">create buffer 32000 allot |
||
{{ |
{{ |
||
Line 1,427: | Line 1,427: | ||
}} |
}} |
||
"http%3A%2F%2Ffoo%20bar%2F" decode buffer puts</ |
"http%3A%2F%2Ffoo%20bar%2F" decode buffer puts</syntaxhighlight> |
||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
Line 1,433: | Line 1,433: | ||
{{Trans|ooRexx}} |
{{Trans|ooRexx}} |
||
Tested with the ooRexx and Regina interpreters. |
Tested with the ooRexx and Regina interpreters. |
||
< |
<syntaxhighlight lang="rexx">/* Rexx */ |
||
Do |
Do |
||
Line 1,481: | Line 1,481: | ||
End |
End |
||
Exit |
Exit |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,497: | Line 1,497: | ||
===version 2=== |
===version 2=== |
||
This REXX version is identical to version 1, but with superfluous and dead code removed. |
This REXX version is identical to version 1, but with superfluous and dead code removed. |
||
< |
<syntaxhighlight lang="rexx">/*REXX program converts a URL─encoded string ──► its original unencoded form. */ |
||
url.1='http%3A%2F%2Ffoo%20bar%2F' |
url.1='http%3A%2F%2Ffoo%20bar%2F' |
||
url.2='mailto%3A%22Ivan%20Aim%22%20%3Civan%2Eaim%40email%2Ecom%3E' |
url.2='mailto%3A%22Ivan%20Aim%22%20%3Civan%2Eaim%40email%2Ecom%3E' |
||
Line 1,525: | Line 1,525: | ||
end /*while*/ |
end /*while*/ |
||
return decoded</ |
return decoded</syntaxhighlight> |
||
{{out|output|text= is identical to the 1<sup>st</sup> REXX version.}} |
{{out|output|text= is identical to the 1<sup>st</sup> REXX version.}} |
||
===version 3=== |
===version 3=== |
||
This REXX version is a shorter version of version 2. |
This REXX version is a shorter version of version 2. |
||
< |
<syntaxhighlight lang="rexx">/*REXX program converts & displays a URL─encoded string ──► its original unencoded form.*/ |
||
url. = |
url. = |
||
url.1='http%3A%2F%2Ffoo%20bar%2F' |
url.1='http%3A%2F%2Ffoo%20bar%2F' |
||
Line 1,551: | Line 1,551: | ||
else URL= URL'%'code |
else URL= URL'%'code |
||
end /*until*/ |
end /*until*/ |
||
return URL</ |
return URL</syntaxhighlight> |
||
{{out|output|text= is identical to the 1<sup>st</sup> REXX version.}} |
{{out|output|text= is identical to the 1<sup>st</sup> REXX version.}} |
||
Line 1,557: | Line 1,557: | ||
Use any one of <code>CGI.unescape</code> or <code>URI.decode_www_form_component</code>. These methods also convert "+" to " ". |
Use any one of <code>CGI.unescape</code> or <code>URI.decode_www_form_component</code>. These methods also convert "+" to " ". |
||
< |
<syntaxhighlight lang="ruby">require 'cgi' |
||
puts CGI.unescape("http%3A%2F%2Ffoo%20bar%2F") |
puts CGI.unescape("http%3A%2F%2Ffoo%20bar%2F") |
||
# => "http://foo bar/"</ |
# => "http://foo bar/"</syntaxhighlight> |
||
{{works with|Ruby|1.9.2}} |
{{works with|Ruby|1.9.2}} |
||
< |
<syntaxhighlight lang="ruby">require 'uri' |
||
puts URI.decode_www_form_component("http%3A%2F%2Ffoo%20bar%2F") |
puts URI.decode_www_form_component("http%3A%2F%2Ffoo%20bar%2F") |
||
# => "http://foo bar/"</ |
# => "http://foo bar/"</syntaxhighlight> |
||
<code>URI.unescape</code> (alias <code>URI.unencode</code>) still works. <code>URI.unescape</code> is obsolete since Ruby 1.9.2 because of problems with its sibling <code>URI.escape</code>. |
<code>URI.unescape</code> (alias <code>URI.unencode</code>) still works. <code>URI.unescape</code> is obsolete since Ruby 1.9.2 because of problems with its sibling <code>URI.escape</code>. |
||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang="rust">const INPUT1: &str = "http%3A%2F%2Ffoo%20bar%2F"; |
||
const INPUT2: &str = "google.com/search?q=%60Abdu%27l-Bah%C3%A1"; |
const INPUT2: &str = "google.com/search?q=%60Abdu%27l-Bah%C3%A1"; |
||
Line 1,604: | Line 1,604: | ||
println!("{}", decode(INPUT1)); |
println!("{}", decode(INPUT1)); |
||
println!("{}", decode(INPUT2)); |
println!("{}", decode(INPUT2)); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,612: | Line 1,612: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
{{libheader|Scala}}< |
{{libheader|Scala}}<syntaxhighlight lang="scala">import java.net.{URLDecoder, URLEncoder} |
||
import scala.compat.Platform.currentTime |
import scala.compat.Platform.currentTime |
||
Line 1,627: | Line 1,627: | ||
println(s"Successfully completed without errors. [total ${currentTime - executionStart} ms]") |
println(s"Successfully completed without errors. [total ${currentTime - executionStart} ms]") |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
Line 1,638: | Line 1,638: | ||
Both functions return byte sequences. |
Both functions return byte sequences. |
||
To decode Unicode characters it is necessary to convert them from UTF-8 with ''utf8ToStri'' afterwards. |
To decode Unicode characters it is necessary to convert them from UTF-8 with ''utf8ToStri'' afterwards. |
||
< |
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
||
include "encoding.s7i"; |
include "encoding.s7i"; |
||
Line 1,645: | Line 1,645: | ||
writeln(fromPercentEncoded("http%3A%2F%2Ffoo%20bar%2F")); |
writeln(fromPercentEncoded("http%3A%2F%2Ffoo%20bar%2F")); |
||
writeln(fromUrlEncoded("http%3A%2F%2Ffoo+bar%2F")); |
writeln(fromUrlEncoded("http%3A%2F%2Ffoo+bar%2F")); |
||
end func;</ |
end func;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,655: | Line 1,655: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|Perl}} |
{{trans|Perl}} |
||
< |
<syntaxhighlight lang="ruby">func urldecode(str) { |
||
str.gsub!('+', ' '); |
str.gsub!('+', ' '); |
||
str.gsub!(/\%([A-Fa-f0-9]{2})/, {|a| 'C'.pack(a.hex)}); |
str.gsub!(/\%([A-Fa-f0-9]{2})/, {|a| 'C'.pack(a.hex)}); |
||
Line 1,661: | Line 1,661: | ||
} |
} |
||
say urldecode('http%3A%2F%2Ffoo+bar%2F'); # => "http://foo bar/"</ |
say urldecode('http%3A%2F%2Ffoo+bar%2F'); # => "http://foo bar/"</syntaxhighlight> |
||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
< |
<syntaxhighlight lang="swift">import Foundation |
||
let encoded = "http%3A%2F%2Ffoo%20bar%2F" |
let encoded = "http%3A%2F%2Ffoo%20bar%2F" |
||
if let normal = encoded.stringByReplacingPercentEscapesUsingEncoding(NSUTF8StringEncoding) { |
if let normal = encoded.stringByReplacingPercentEscapesUsingEncoding(NSUTF8StringEncoding) { |
||
println(normal) |
println(normal) |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
This code is careful to ensure that any untoward metacharacters in the input string still do not cause any problems. |
This code is careful to ensure that any untoward metacharacters in the input string still do not cause any problems. |
||
< |
<syntaxhighlight lang="tcl">proc urlDecode {str} { |
||
set specialMap {"[" "%5B" "]" "%5D"} |
set specialMap {"[" "%5B" "]" "%5D"} |
||
set seqRE {%([0-9a-fA-F]{2})} |
set seqRE {%([0-9a-fA-F]{2})} |
||
Line 1,679: | Line 1,679: | ||
set modStr [regsub -all $seqRE [string map $specialMap $str] $replacement] |
set modStr [regsub -all $seqRE [string map $specialMap $str] $replacement] |
||
return [encoding convertfrom utf-8 [subst -nobackslash -novariable $modStr]] |
return [encoding convertfrom utf-8 [subst -nobackslash -novariable $modStr]] |
||
}</ |
}</syntaxhighlight> |
||
Demonstrating: |
Demonstrating: |
||
< |
<syntaxhighlight lang="tcl">puts [urlDecode "http%3A%2F%2Ffoo%20bar%2F"]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>http://foo bar/</pre> |
<pre>http://foo bar/</pre> |
||
=={{header|TUSCRIPT}}== |
=={{header|TUSCRIPT}}== |
||
< |
<syntaxhighlight lang="tuscript"> |
||
$$ MODE TUSCRIPT |
$$ MODE TUSCRIPT |
||
url_encoded="http%3A%2F%2Ffoo%20bar%2F" |
url_encoded="http%3A%2F%2Ffoo%20bar%2F" |
||
Line 1,695: | Line 1,695: | ||
PRINT "encoded: ", url_encoded |
PRINT "encoded: ", url_encoded |
||
PRINT "decoded: ", url_decoded |
PRINT "decoded: ", url_decoded |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,707: | Line 1,707: | ||
{{works with|ksh}} |
{{works with|ksh}} |
||
< |
<syntaxhighlight lang="bash">urldecode() { local u="${1//+/ }"; printf '%b' "${u//%/\\x}"; }</syntaxhighlight> |
||
Example: |
Example: |
||
< |
<syntaxhighlight lang="bash">urldecode http%3A%2F%2Ffoo%20bar%2F |
||
http://foo bar/ |
http://foo bar/ |
||
urldecode google.com/search?q=%60Abdu%27l-Bah%C3%A1 |
urldecode google.com/search?q=%60Abdu%27l-Bah%C3%A1 |
||
google.com/search?q=`Abdu'l-Bahároot@ |
google.com/search?q=`Abdu'l-Bahároot@ |
||
</syntaxhighlight> |
|||
</lang> |
|||
< |
<syntaxhighlight lang="bash">function urldecode |
||
{ |
{ |
||
typeset encoded=$1 decoded= rest= c= c1= c2= |
typeset encoded=$1 decoded= rest= c= c1= c2= |
||
Line 1,784: | Line 1,784: | ||
fi |
fi |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|VBScript}}== |
=={{header|VBScript}}== |
||
< |
<syntaxhighlight lang="vbscript">Function RegExTest(str,patrn) |
||
Dim regEx |
Dim regEx |
||
Set regEx = New RegExp |
Set regEx = New RegExp |
||
Line 1,831: | Line 1,831: | ||
url = "http%3A%2F%2Ffoo%20bar%C3%A8%2F" |
url = "http%3A%2F%2Ffoo%20bar%C3%A8%2F" |
||
WScript.Echo "Encoded URL: " & url & vbCrLf &_ |
WScript.Echo "Encoded URL: " & url & vbCrLf &_ |
||
"Decoded URL: " & UrlDecode(url)</ |
"Decoded URL: " & UrlDecode(url)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,838: | Line 1,838: | ||
=={{header|Vlang}}== |
=={{header|Vlang}}== |
||
< |
<syntaxhighlight lang="vlang">import net.urllib |
||
fn main() { |
fn main() { |
||
for escaped in [ |
for escaped in [ |
||
Line 1,847: | Line 1,847: | ||
println(u) |
println(u) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,856: | Line 1,856: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
< |
<syntaxhighlight lang="ecmascript">import "/fmt" for Conv |
||
var urlDecode = Fn.new { |enc| |
var urlDecode = Fn.new { |enc| |
||
Line 1,880: | Line 1,880: | ||
"google.com/search?q=\%60Abdu\%27l-Bah\%C3\%A1" |
"google.com/search?q=\%60Abdu\%27l-Bah\%C3\%A1" |
||
] |
] |
||
for (enc in encs)System.print(urlDecode.call(enc))</ |
for (enc in encs)System.print(urlDecode.call(enc))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,889: | Line 1,889: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang="xpl0">code Text=12; |
||
string 0; \use zero-terminated strings |
string 0; \use zero-terminated strings |
||
Line 1,911: | Line 1,911: | ||
]; |
]; |
||
Text(0, Decode("http%3A%2F%2Ffoo%20bar%2f"))</ |
Text(0, Decode("http%3A%2F%2Ffoo%20bar%2f"))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,920: | Line 1,920: | ||
=={{header|Yabasic}}== |
=={{header|Yabasic}}== |
||
{{trans|Phix}} |
{{trans|Phix}} |
||
< |
<syntaxhighlight lang="yabasic">sub decode_url$(s$) |
||
local res$, ch$ |
local res$, ch$ |
||
Line 1,938: | Line 1,938: | ||
print decode_url$("http%3A%2F%2Ffoo%20bar%2F") |
print decode_url$("http%3A%2F%2Ffoo%20bar%2F") |
||
print decode_url$("google.com/search?q=%60Abdu%27l-Bah%C3%A1")</ |
print decode_url$("google.com/search?q=%60Abdu%27l-Bah%C3%A1")</syntaxhighlight> |
||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
< |
<syntaxhighlight lang="zkl">"http%3A%2F%2Ffoo%20bar%2F".pump(String, // push each char through these fcns: |
||
fcn(c){ if(c=="%") return(Void.Read,2); return(Void.Skip,c) },// %-->read 2 chars else pass through |
fcn(c){ if(c=="%") return(Void.Read,2); return(Void.Skip,c) },// %-->read 2 chars else pass through |
||
fcn(_,b,c){ (b+c).toInt(16).toChar() }) // "%" (ignored) "3"+"1"-->0x31-->"1"</ |
fcn(_,b,c){ (b+c).toInt(16).toChar() }) // "%" (ignored) "3"+"1"-->0x31-->"1"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>http://foo bar/</pre> |
<pre>http://foo bar/</pre> |
||
or use libCurl: |
or use libCurl: |
||
< |
<syntaxhighlight lang="zkl">var Curl=Import.lib("zklCurl"); |
||
Curl.urlDecode("http%3A%2F%2Ffoo%20bar%2F");</ |
Curl.urlDecode("http%3A%2F%2Ffoo%20bar%2F");</syntaxhighlight> |
||
{{out}}<pre>http://foo bar/</pre> |
{{out}}<pre>http://foo bar/</pre> |