URL decoding: Difference between revisions

5,075 bytes added ,  27 days ago
No edit summary
 
(20 intermediate revisions by 9 users not shown)
Line 10:
 
* &nbsp; The encoded string &nbsp; "<code><nowiki>google.com/search?q=%60Abdu%27l-Bah%C3%A1</nowiki></code>" &nbsp; should revert to the unencoded form &nbsp; "<code><nowiki>google.com/search?q=`Abdu'l-Bahá</nowiki></code>".
 
* &nbsp; The encoded string &nbsp; "<code><nowiki>%25%32%35</nowiki></code>" &nbsp; should revert to the unencoded form &nbsp; "<code><nowiki>%25</nowiki></code>" and '''not''' "<code><nowiki>%</nowiki></code>".
<br><br>
 
=={{header|11l}}==
<langsyntaxhighlight lang="11l">F url_decode(s)
V r = ‘’
V i = 0
Line 30 ⟶ 32:
 
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’))</langsyntaxhighlight>
 
{{out}}
Line 40 ⟶ 42:
=={{header|ABAP}}==
 
<langsyntaxhighlight ABAPlang="abap">REPORT Z_DECODE_URL.
 
DATA: lv_encoded_url TYPE string VALUE 'http%3A%2F%2Ffoo%20bar%2F',
Line 51 ⟶ 53:
UNESCAPED = lv_decoded_url.
 
WRITE: 'Encoded URL: ', lv_encoded_url, /, 'Decoded URL: ', lv_decoded_url.</langsyntaxhighlight>
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">PROC Append(CHAR ARRAY s CHAR c)
s(0)==+1
s(s(0))=c
Line 118 ⟶ 120:
Test("http%3A%2F%2Ffoo%20bar%2F")
Test("http%3A%2F%2Ffoo+bar%2F*_-.html")
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/URL_decoding.png Screenshot from Atari 8-bit computer]
Line 131 ⟶ 133:
=={{header|Ada}}==
{{libheader|AWS}}
<langsyntaxhighlight Adalang="ada">with AWS.URL;
with Ada.Text_IO; use Ada.Text_IO;
procedure Decode is
Line 138 ⟶ 140:
Put_Line (AWS.URL.Decode (Encoded));
end Decode;
</syntaxhighlight>
</lang>
 
Without external libraries:
 
<langsyntaxhighlight Adalang="ada">package URL is
function Decode (URL : in String) return String;
end URL;</langsyntaxhighlight>
 
<langsyntaxhighlight Adalang="ada">package body URL is
function Decode (URL : in String) return String is
Buffer : String (1 .. URL'Length);
Line 173 ⟶ 175:
return Buffer (1 .. Filled);
end Decode;
end URL;</langsyntaxhighlight>
 
<langsyntaxhighlight Adalang="ada">with Ada.Command_Line,
Ada.Text_IO;
 
Line 190 ⟶ 192:
end loop;
end if;
end Test_URL_Decode;</langsyntaxhighlight>
 
=={{header|ALGOL 68}}==
Line 197 ⟶ 199:
<br>
For the second task example, this outputs the encoded UTF-8 characters, what you see depends on what you look at it with...
<langsyntaxhighlight 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"
ELIF c >= "A" AND c <= "F" THEN 10 + ( ABS c - ABS "A" )
Line 232 ⟶ 234:
# test the url decode procedure #
print( ( url decode( "http%3A%2F%2Ffoo%20bar%2F" ), newline ) );
print( ( url decode( "google.com/search?q=%60Abdu%27l-Bah%C3%A1" ), newline ) )</langsyntaxhighlight>
{{out}}
<pre>
Line 240 ⟶ 242:
 
=={{header|Apex}}==
<langsyntaxhighlight 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');</langsyntaxhighlight>
<pre>http://foo bar/
google.com/search?q=`Abdu'l-Bahá</pre>
Line 247 ⟶ 249:
=={{header|AppleScript}}==
{{libheader|AppleScript Toolbox}}
<langsyntaxhighlight AppleScriptlang="applescript">AST URL decode "google.com/search?q=%60Abdu%27l-Bah%C3%A1"</langsyntaxhighlight>
 
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">print decode.url "http%3A%2F%2Ffoo%20bar%2F"
print decode.url "google.com/search?q=%60Abdu%27l-Bah%C3%A1"</langsyntaxhighlight>
 
{{out}}
Line 260 ⟶ 262:
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">
<lang AutoHotkey>encURL := "http%3A%2F%2Ffoo%20bar%2F"
UriDecode(Uri) {
SetFormat, Integer, hex
LoopOffset := 0
Loop Parse, encURL
If A_LoopFieldVarLength := `%0
VarSetCapacity(Var, StrPut(Uri, "UTF-8"), 0)
reading := 2, read := ""
else ifLoop readingParse, Uri
{
If (A_Index < LoopOffset) {
read .= A_LoopField, --reading
if not reading Continue
out .= Chr("0x" . read)}
If (A_LoopField = Chr(37)) {
}
Number := "0x" . SubStr(Uri, A_Index + 1, 2)
else out .= A_LoopField
LoopOffset := A_Index + 3
MsgBox % out ; http://foo bar/
}
</lang>
Else {
Number := Ord(A_LoopField)
}
NumPut(Number, Var, VarLength++, "UChar")
}
Return StrGet(&Var, VarLength, "UTF-8")
}
MsgBox % UriDecode("http%3A%2F%2Ffoo%20bar%2F")
MsgBox % UriDecode("google.com/search?q=%60Abdu%27l-Bah%C3%A1")
MsgBox % UriDecode("%25%32%35")
</syntaxhighlight>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax:
awk '
Line 299 ⟶ 312:
return num + (length(s) ? 16*hex2dec(s) : 0)
} '
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 308 ⟶ 321:
OR:
 
<langsyntaxhighlight lang="awk">
LC_ALL=C
echo "http%3A%2F%2Ffoo%20bar%2F" | gawk -vRS='%[[:xdigit:]]{2}' '
RT {RT = sprintf("%c",strtonum("0x" substr(RT, 2)))}
{gsub(/+/," ");printf "%s", $0 RT}'
</syntaxhighlight>
</lang>
 
{{out}}
Line 321 ⟶ 334:
 
=={{header|BaCon}}==
<langsyntaxhighlight lang="freebasic">FUNCTION Url_Decode$(url$)
 
LOCAL result$
Line 334 ⟶ 347:
 
PRINT Url_Decode$("http%3A%2F%2Ffoo%20bar%2F")
PRINT Url_Decode$("google.com/search?q=%60Abdu%27l-Bah%C3%A1")</langsyntaxhighlight>
{{out}}
<pre>
Line 340 ⟶ 353:
google.com/search?q=`Abdu'l-Bahá
</pre>
 
=={{header|Bash}}==
See [[#UNIX Shell]]
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> PRINT FNurldecode("http%3A%2F%2Ffoo%20bar%2F")
END
Line 364 ⟶ 380:
IF C% >= 97 IF C% <= 122 MID$(A$,A%,1) = CHR$(C%-32)
NEXT
= A$</langsyntaxhighlight>
{{out}}
<pre>
Line 371 ⟶ 387:
 
=={{header|Bracmat}}==
<langsyntaxhighlight lang="bracmat">( ( decode
= decoded hexcode notencoded
. :?decoded
Line 381 ⟶ 397:
)
& out$(decode$http%3A%2F%2Ffoo%20bar%2F)
);</langsyntaxhighlight>
{{out}}
<pre>http://foo bar/</pre>
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <string.h>
 
Line 425 ⟶ 441:
 
return 0;
}</langsyntaxhighlight>
 
=={{header|C sharp}}==
 
<langsyntaxhighlight lang="csharp">using System;
 
namespace URLEncode
Line 445 ⟶ 461:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 455 ⟶ 471:
{{libheader|Poco}}
{{works with|g++}}
<langsyntaxhighlight lang="cpp">#include <string>
#include "Poco/URI.h"
#include <iostream>
Line 465 ⟶ 481:
std::cout << encoded << " is decoded: " << decoded << " !" << std::endl ;
return 0 ;
}</langsyntaxhighlight>
{{out}}
<pre>http%3A%2F%2Ffoo%20bar%2F is decoded: http://foo bar/ !</pre>
Line 476 ⟶ 492:
=={{header|Clojure}}==
 
<langsyntaxhighlight lang="clojure">(java.net.URLDecoder/decode "http%3A%2F%2Ffoo%20bar%2F")</langsyntaxhighlight>
 
=={{header|CoffeeScript}}==
 
<langsyntaxhighlight lang="coffeescript">
console.log decodeURIComponent "http%3A%2F%2Ffoo%20bar%2F?name=Foo%20Barson"
</syntaxhighlight>
</lang>
 
<syntaxhighlight lang="text">
> coffee foo.coffee
http://foo bar/?name=Foo Barson
</syntaxhighlight>
</lang>
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(defun decode (string &key start)
(assert (char= (char string start) #\%))
(if (>= (length string) (+ start 3))
Line 512 ⟶ 528:
finally (return (apply #'concatenate 'string chunks))))
 
(url-decode "http%3A%2F%2Ffoo%20bar%2F")</langsyntaxhighlight>
{{out}}
<pre>"http://foo bar/"</pre>
 
=={{header|Crystal}}==
<langsyntaxhighlight lang="crystal">require "uri"
 
puts URI.decode "http%3A%2F%2Ffoo%20bar%2F"
puts URI.decode "google.com/search?q=%60Abdu%27l-Bah%C3%A1"</langsyntaxhighlight>
{{out}}
<pre>http://foo bar/
Line 526 ⟶ 542:
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio, std.uri;
 
void main() {
writeln(decodeComponent("http%3A%2F%2Ffoo%20bar%2F"));
}</langsyntaxhighlight>
<pre>http://foo bar/</pre>
 
=={{header|Delphi}}==
<langsyntaxhighlight Delphilang="delphi">program URLEncoding;
 
{$APPTYPE CONSOLE}
Line 542 ⟶ 558:
begin
Writeln(TIdURI.URLDecode('http%3A%2F%2Ffoo%20bar%2F'));
end.</langsyntaxhighlight>
 
=={{header|Elixir}}==
<langsyntaxhighlight 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")</langsyntaxhighlight>
 
{{out}}
Line 563 ⟶ 579:
=={{header|F_Sharp|F#}}==
{{trans|C#}}
<langsyntaxhighlight lang="fsharp">open System
 
let decode uri = Uri.UnescapeDataString(uri)
Line 570 ⟶ 586:
let main argv =
printfn "%s" (decode "http%3A%2F%2Ffoo%20bar%2F")
0</langsyntaxhighlight>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: io kernel urls.encoding ;
IN: rosetta-code.url-decoding
 
"http%3A%2F%2Ffoo%20bar%2F"
"google.com/search?q=%60Abdu%27l-Bah%C3%A1"
[ url-decode print ] bi@</langsyntaxhighlight>
{{out}}
<pre>
Line 587 ⟶ 603:
=={{header|Free Pascal}}==
 
<langsyntaxhighlight lang="pascal">function urlDecode(data: String): AnsiString;
var
ch: Char;
Line 612 ⟶ 628:
pos := pos +1;
end;
end;</langsyntaxhighlight>
 
 
Line 618 ⟶ 634:
{{trans|Liberty BASIC}}
{{trans|Pascal}}
<langsyntaxhighlight lang="freebasic">
Const alphanum = "0123456789abcdefghijklmnopqrstuvwxyz"
 
Line 658 ⟶ 674:
Print "URL decoding '"; url2string(URL); "'"
Sleep
</syntaxhighlight>
</lang>
 
=={{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.
<langsyntaxhighlight lang="frink">URLDecode["google.com/search?q=%60Abdu%27l-Bah%C3%A1","UTF8"]</langsyntaxhighlight>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 685 ⟶ 701:
fmt.Println(u)
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 693 ⟶ 709:
 
=={{header|Groovy}}==
<langsyntaxhighlight lang="groovy">assert URLDecoder.decode('http%3A%2F%2Ffoo%20bar%2F') == 'http://foo bar/'</langsyntaxhighlight>
 
=={{header|Haskell}}==
<langsyntaxhighlight Haskelllang="haskell">import qualified Data.Char as Char
 
urlDecode :: String -> Maybe String
Line 710 ⟶ 726:
 
main :: IO ()
main = putStrLn . maybe "Bad decode" id $ urlDecode "http%3A%2F%2Ffoo%20bar%2F"</langsyntaxhighlight>
{{out}}
<pre>http://foo bar/</pre>
 
Another approach:
<langsyntaxhighlight lang="haskell">import Data.Char (chr)
import Data.List.Split (splitOn)
 
Line 727 ⟶ 743:
-- TEST ------------------------------------------------------------------------
main :: IO ()
main = putStrLn $ deCode "http%3A%2F%2Ffoo%20bar%2F"</langsyntaxhighlight>
{{Out}}
<pre>http://foo bar/</pre>
 
=={{header|Icon}} and {{header|Unicon}}==
<langsyntaxhighlight Iconlang="icon">link hexcvt
 
procedure main()
Line 753 ⟶ 769:
else move(1)
return c
end</langsyntaxhighlight>
 
{{libheader|Icon Programming Library}}
Line 767 ⟶ 783:
Here is an implementation:
 
<langsyntaxhighlight lang="j">require'strings convert'
urldecode=: rplc&(~.,/;"_1&a."2(,:tolower)'%',.toupper hfd i.#a.)</langsyntaxhighlight>
 
Example use:
 
<langsyntaxhighlight lang="j"> urldecode 'http%3A%2F%2Ffoo%20bar%2F'
http://foo bar/</langsyntaxhighlight>
 
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 ⟶ 795:
Example use:
 
<langsyntaxhighlight lang="j"> urldecode 'google.com/search?q=%60Abdu%27l-Bah%C3%A1'
google.com/search?q=`Abdu'l-Bahá</langsyntaxhighlight>
 
=={{header|Java}}==
Java offers the ''URLDecoder'' and ''URLEncoder'' classes for this specific task.
 
<syntaxhighlight lang="java">
<lang java>import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
 
</syntaxhighlight>
public class Main
<syntaxhighlight lang="java">
{
URLDecoder.decode("http%3A%2F%2Ffoo%20bar%2F", StandardCharsets.UTF_8)
public static void main(String[] args) throws UnsupportedEncodingException
</syntaxhighlight>
{
Alternately, you could use a regular expression capture
String encoded = "http%3A%2F%2Ffoo%20bar%2F";
<syntaxhighlight lang="java">
String normal = URLDecoder.decode(encoded, "utf-8");
import java.util.regex.Matcher;
System.out.println(normal);
import java.util.regex.Pattern;
</syntaxhighlight>
<syntaxhighlight lang="java">
String decode(String string) {
Pattern pattern = Pattern.compile("%([A-Za-z\\d]{2})");
Matcher matcher = pattern.matcher(string);
StringBuilder decoded = new StringBuilder(string);
char character;
int start, end, offset = 0;
while (matcher.find()) {
character = (char) Integer.parseInt(matcher.group(1), 16);
/* offset the matched index since were adjusting the string */
start = matcher.start() - offset;
end = matcher.end() - offset;
decoded.replace(start, end, String.valueOf(character));
offset += 2;
}
return decoded.toString();
}</lang>
}
 
</syntaxhighlight>
{{out}}
<pre>
<pre>http://foo bar/</pre>
http://foo bar/
google.com/search?q=`Abdu'l-Bahá
</pre>
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="javascript">decodeURIComponent("http%3A%2F%2Ffoo%20bar%2F")</langsyntaxhighlight>
 
=={{header|jq}}==
{{works with|jq|1.4}}
If your jq already has "until", then the definition given below may be omitted.
<langsyntaxhighlight lang="jq"># Emit . and stop as soon as "condition" is true.
def until(condition; next):
def u: if condition then . else (next|u) end;
Line 832 ⟶ 867:
else [ $i + 1, .[1] + $in[$i:$i+1] ]
end)
| .[1]; # answer</langsyntaxhighlight>
'''Example''':
<langsyntaxhighlight lang="jq">"http%3A%2F%2Ffoo%20bar%2F" | url_decode</langsyntaxhighlight>
{{out}}
<syntaxhighlight lang="sh">
<lang sh>
"http://foo bar/"</langsyntaxhighlight>
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">
<lang Julia>
using URIParser
 
Line 848 ⟶ 883:
 
println(enc, " => ", dcd)
</syntaxhighlight>
</lang>
 
{{out}}
Line 856 ⟶ 891:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.2
 
import java.net.URLDecoder
Line 863 ⟶ 898:
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"))
}</langsyntaxhighlight>
 
{{out}}
Line 872 ⟶ 907:
 
=={{header|Ksh}}==
<syntaxhighlight lang="ksh">
<lang Ksh>
url_decode()
{
Line 881 ⟶ 916:
url_decode "http%3A%2F%2Ffoo%20bar%2F"
url_decode "google.com/search?q=%60Abdu%27l-Bah%C3%A1"
</syntaxhighlight>
</lang>
 
{{out}}
Line 891 ⟶ 926:
=={{header|Lambdatalk}}==
Currently lambdatalk has no builtin primitive for decoding URLs. Let's define it using Javascript.
<langsyntaxhighlight lang="scheme">
1) define a new javascript primitive:
{script
Line 905 ⟶ 940:
 
 
</syntaxhighlight>
</lang>
 
 
=={{header|langur}}==
<syntaxhighlight lang="langur">val .finish = fn(.s) { b2s map fn(.x) { number(.x, 16) }, rest split "%", .s }
val .decode = fn(.s) { replace .s, re/(%[0-9A-Fa-f]{2})+/, .finish }
 
writeln .decode("http%3A%2F%2Ffoo%20bar%2F")
writeln .decode("google.com/search?q=%60Abdu%27l-Bah%C3%A1")
</syntaxhighlight>
 
{{out}}
<pre>http://foo bar/
google.com/search?q=`Abdu'l-Bahá
</pre>
 
=={{header|Lasso}}==
<langsyntaxhighlight Lassolang="lasso">bytes('http%3A%2F%2Ffoo%20bar%2F') -> decodeurl</langsyntaxhighlight>
-> http://foo bar/
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
<lang lb>
dim lookUp$( 256)
 
Line 938 ⟶ 987:
next j
end function
</syntaxhighlight>
</lang>
Supplied URL 'http%3A%2F%2Ffoo%20bar%2F'
As string 'http://foo bar/'
 
=={{header|Lingo}}==
<langsyntaxhighlight Lingolang="lingo">----------------------------------------
-- URL decodes a string
-- @param {string} str
Line 966 ⟶ 1,015:
ba.position = 1
return ba.readRawString(ba.length)
end</langsyntaxhighlight>
<langsyntaxhighlight Lingolang="lingo">put urldecode("http%3A%2F%2Ffoo%20bar%2F")
put urldecode("google.com/search?q=%60Abdu%27l-Bah%C3%A1")</langsyntaxhighlight>
{{Out}}
<pre>
Line 976 ⟶ 1,025:
 
=={{header|LiveCode}}==
<langsyntaxhighlight LiveCodelang="livecode">put urlDecode("http%3A%2F%2Ffoo%20bar%2F") & cr & \
urlDecode("google.com/search?q=%60Abdu%27l-Bah%C3%A1")</langsyntaxhighlight>Results<syntaxhighlight lang="text">http://foo bar/
google.com/search?q=`Abdu'l-Bah√°</langsyntaxhighlight>
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">function decodeChar(hex)
return string.char(tonumber(hex,16))
end
Line 991 ⟶ 1,040:
 
-- will print "http://foo bar/"
print(decodeString("http%3A%2F%2Ffoo%20bar%2F"))</langsyntaxhighlight>
 
=={{header|M2000 Interpreter}}==
Line 1,001 ⟶ 1,050:
 
b$=chr$(a$) revert bytes to words adding zeroes after each character
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module CheckIt {
Function decodeUrl$(a$) {
Line 1,027 ⟶ 1,076:
}
CheckIt
</syntaxhighlight>
</lang>
 
=={{header|Maple}}==
<langsyntaxhighlight Maplelang="maple">StringTools:-Decode("http%3A%2F%2Ffoo%20bar%2F", encoding=percent);</langsyntaxhighlight>
 
{{Out}}
Line 1,036 ⟶ 1,085:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight lang="mathematica">URLDecoding[url_] :=
StringReplace[url, "%" ~~ x_ ~~ y_ :> FromDigits[x ~~ y, 16]] //.
StringExpression[x___, Longest[n__Integer], y___] :>
StringExpression[x, FromCharacterCode[{n}, "UTF8"], y]</langsyntaxhighlight>
Example use:
<langsyntaxhighlight lang="mathematica">URLDecoding["http%3A%2F%2Ffoo%20bar%2F"]</langsyntaxhighlight>
{{out}}
<pre>http://foo bar/</pre>
Line 1,061 ⟶ 1,110:
 
=={{header|MATLAB}} / {{header|Octave}}==
<langsyntaxhighlight MATLABlang="matlab">function u = urldecoding(s)
u = '';
k = 1;
Line 1,073 ⟶ 1,122:
end
end
end</langsyntaxhighlight>
Usage:
<pre>octave:3> urldecoding('http%3A%2F%2Ffoo%20bar%2F')
Line 1,079 ⟶ 1,128:
 
=={{header|NetRexx}}==
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref savelog symbols nobinary
 
Line 1,122 ⟶ 1,171:
 
return decoded
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,137 ⟶ 1,186:
 
=={{header|NewLISP}}==
<langsyntaxhighlight NewLISPlang="newlisp">;; universal decoder, works for ASCII and UTF-8
;; (source http://www.newlisp.org/index.cgi?page=Code_Snippets)
(define (url-decode url (opt nil))
Line 1,143 ⟶ 1,192:
(replace "%([0-9a-f][0-9a-f])" url (pack "b" (int $1 0 16)) 1))
 
(url-decode "http%3A%2F%2Ffoo%20bar%2F")</langsyntaxhighlight>
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">import cgi
 
echo decodeUrl("http%3A%2F%2Ffoo%20bar%2F")</langsyntaxhighlight>
 
{{out}}
Line 1,155 ⟶ 1,204:
=={{header|Oberon-2}}==
{{works with|oo2c}}
<langsyntaxhighlight lang="oberon2">
MODULE URLDecoding;
IMPORT
Line 1,164 ⟶ 1,213:
Out.String(URI.Unescape("google.com/search?q=%60Abdu%27l-Bah%C3%A1"));Out.Ln;
END URLDecoding.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,172 ⟶ 1,221:
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">
class UrlDecode {
function : Main(args : String[]) ~ Nil {
Line 1,178 ⟶ 1,227:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Objective-C}}==
<langsyntaxhighlight lang="objc">NSString *encoded = @"http%3A%2F%2Ffoo%20bar%2F";
NSString *normal = [encoded stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%@", normal);</langsyntaxhighlight>
{{works with|Mac OS X|10.9+}}{{works with|iOS|7+}}
<langsyntaxhighlight lang="objc">NSString *encoded = @"http%3A%2F%2Ffoo%20bar%2F";
NSString *normal = [encoded stringByRemovingPercentEncoding];
NSLog(@"%@", normal);</langsyntaxhighlight>
 
=={{header|OCaml}}==
Line 1,193 ⟶ 1,242:
Using the library [http://projects.camlcity.org/projects/ocamlnet.html ocamlnet] from the interactive loop:
 
<langsyntaxhighlight lang="ocaml">$ ocaml
# #use "topfind";;
# #require "netstring";;
 
# Netencoding.Url.decode "http%3A%2F%2Ffoo%20bar%2F" ;;
- : string = "http://foo bar/"</langsyntaxhighlight>
 
=={{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.
<langsyntaxhighlight ooRexxlang="oorexx">/* Rexx */
X = 0
url. = ''
Line 1,241 ⟶ 1,290:
End e_
 
Return decoded</langsyntaxhighlight>
 
{{out}}
Line 1,257 ⟶ 1,306:
=={{header|Perl}}==
 
<langsyntaxhighlight Perllang="perl">sub urldecode {
my $s = shift;
$s =~ tr/\+/ /;
Line 1,265 ⟶ 1,314:
 
print urldecode('http%3A%2F%2Ffoo+bar%2F')."\n";
</syntaxhighlight>
</lang>
 
<langsyntaxhighlight Perllang="perl">#!/usr/bin/perl -w
use strict ;
use URI::Escape ;
Line 1,273 ⟶ 1,322:
my $encoded = "http%3A%2F%2Ffoo%20bar%2F" ;
my $unencoded = uri_unescape( $encoded ) ;
print "The unencoded string is $unencoded !\n" ;</langsyntaxhighlight>
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\decode_url.exw
Line 1,313 ⟶ 1,362:
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
<!--</langsyntaxhighlight>-->
{{Out}}
<pre>
Line 1,321 ⟶ 1,370:
 
=={{header|PHP}}==
<langsyntaxhighlight lang="php"><?php
$encoded = "http%3A%2F%2Ffoo%20bar%2F";
$unencoded = rawurldecode($encoded);
echo "The unencoded string is $unencoded !\n";
?></langsyntaxhighlight>
 
=={{header|PicoLisp}}==
Line 1,332 ⟶ 1,381:
 
=={{header|Pike}}==
<syntaxhighlight lang="pike">
<lang Pike>
void main()
{
Line 1,346 ⟶ 1,395:
}
}
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 1,354 ⟶ 1,403:
 
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
<lang PowerShell>
[System.Web.HttpUtility]::UrlDecode("http%3A%2F%2Ffoo%20bar%2F")
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 1,363 ⟶ 1,412:
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">URL$ = URLDecoder("http%3A%2F%2Ffoo%20bar%2F")
 
Debug URL$ ; http://foo bar/</langsyntaxhighlight>
 
=={{header|Python}}==
<syntaxhighlight lang="python">
<lang Python>
#Python 2.X
import urllib
Line 1,375 ⟶ 1,424:
from urllib.parse import unquote
print(unquote('http%3A%2F%2Ffoo%20bar%2F'))
</syntaxhighlight>
</lang>
 
=={{header|R}}==
 
<langsyntaxhighlight Rlang="r">URLdecode("http%3A%2F%2Ffoo%20bar%2F")</langsyntaxhighlight>
 
=={{header|Racket}}==
 
<langsyntaxhighlight lang="racket">
#lang racket
(require net/uri-codec)
(uri-decode "http%3A%2F%2Ffoo%20bar%2F")
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line>my @urls = < http%3A%2F%2Ffoo%20bar%2F
google.com/search?q=%60Abdu%27l-Bah%C3%A1 >;
 
say .subst( :g,
/ [ '%' ( <:hexdigitxdigit> ** 2 ) ]+ / ,
{ Blob.new((:16(~$_) for $0)).decode }
) for @urls;</langsyntaxhighlight>
{{out}}
<pre>http://foo bar/
Line 1,404 ⟶ 1,453:
=={{header|Red}}==
 
<langsyntaxhighlight lang="rebol">>> dehex "http%3A%2F%2Ffoo%20bar%2F"
== "http://foo bar/"
>> dehex "google.com/search?q=%60Abdu%27l-Bah%C3%A1"
== "google.com/search?q=`Abdu'l-Bahá"</langsyntaxhighlight>
 
=={{header|Retro}}==
This is provided by the '''casket''' library (used for web app development).
 
<langsyntaxhighlight Retrolang="retro">create buffer 32000 allot
 
{{
Line 1,427 ⟶ 1,476:
}}
 
"http%3A%2F%2Ffoo%20bar%2F" decode buffer puts</langsyntaxhighlight>
 
=={{header|REXX}}==
Line 1,433 ⟶ 1,482:
{{Trans|ooRexx}}
Tested with the ooRexx and Regina interpreters.
<langsyntaxhighlight REXXlang="rexx">/* Rexx */
 
Do
Line 1,481 ⟶ 1,530:
End
Exit
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,497 ⟶ 1,546:
===version 2===
This REXX version is identical to version 1, but with superfluous and dead code removed.
<langsyntaxhighlight REXXlang="rexx">/*REXX program converts a URL─encoded string ──► its original unencoded form. */
url.1='http%3A%2F%2Ffoo%20bar%2F'
url.2='mailto%3A%22Ivan%20Aim%22%20%3Civan%2Eaim%40email%2Ecom%3E'
Line 1,525 ⟶ 1,574:
end /*while*/
 
return decoded</langsyntaxhighlight>
{{out|output|text=&nbsp; is identical to the 1<sup>st</sup> REXX version.}}
 
===version 3===
This REXX version is a shorter version of version 2.
<langsyntaxhighlight REXXlang="rexx">/*REXX program converts & displays a URL─encoded string ──► its original unencoded form.*/
url. =
url.1='http%3A%2F%2Ffoo%20bar%2F'
Line 1,551 ⟶ 1,600:
else URL= URL'%'code
end /*until*/
return URL</langsyntaxhighlight>
{{out|output|text=&nbsp; is identical to the 1<sup>st</sup> REXX version.}}
 
Line 1,557 ⟶ 1,606:
Use any one of <code>CGI.unescape</code> or <code>URI.decode_www_form_component</code>. These methods also convert "+" to " ".
 
<langsyntaxhighlight lang="ruby">require 'cgi'
puts CGI.unescape("http%3A%2F%2Ffoo%20bar%2F")
# => "http://foo bar/"</langsyntaxhighlight>
 
{{works with|Ruby|1.9.2}}
<langsyntaxhighlight lang="ruby">require 'uri'
puts URI.decode_www_form_component("http%3A%2F%2Ffoo%20bar%2F")
# => "http://foo bar/"</langsyntaxhighlight>
 
<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}}==
<langsyntaxhighlight lang="rust">const INPUT1: &str = "http%3A%2F%2Ffoo%20bar%2F";
const INPUT2: &str = "google.com/search?q=%60Abdu%27l-Bah%C3%A1";
 
Line 1,604 ⟶ 1,653:
println!("{}", decode(INPUT1));
println!("{}", decode(INPUT2));
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,612 ⟶ 1,661:
 
=={{header|Scala}}==
{{libheader|Scala}}<langsyntaxhighlight lang="scala">import java.net.{URLDecoder, URLEncoder}
import scala.compat.Platform.currentTime
 
Line 1,627 ⟶ 1,676:
 
println(s"Successfully completed without errors. [total ${currentTime - executionStart} ms]")
}</langsyntaxhighlight>
 
=={{header|Seed7}}==
Line 1,637 ⟶ 1,686:
works like ''fromPercentEncoded'' and additionally decodes '+' with a space.
Both functions return byte sequences.
To decode Unicode characters it is necessary to convert them from UTF-8 with ''utf8ToStri''[https://seed7.sourceforge.net/libraries/unicode.htm#fromUtf8(in_string) fromUtf8] afterwards.
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "encoding.s7i";
 
Line 1,645 ⟶ 1,694:
writeln(fromPercentEncoded("http%3A%2F%2Ffoo%20bar%2F"));
writeln(fromUrlEncoded("http%3A%2F%2Ffoo+bar%2F"));
end func;</langsyntaxhighlight>
 
{{out}}
Line 1,655 ⟶ 1,704:
=={{header|Sidef}}==
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">func urldecode(str) {
str.gsub!('+', ' ');
str.gsub!(/\%([A-Fa-f0-9]{2})/, {|a| 'C'.pack(a.hex)});
Line 1,661 ⟶ 1,710:
}
 
say urldecode('http%3A%2F%2Ffoo+bar%2F'); # => "http://foo bar/"</langsyntaxhighlight>
 
=={{header|Swift}}==
<langsyntaxhighlight lang="swift">import Foundation
 
let encoded = "http%3A%2F%2Ffoo%20bar%2F"
if let normal = encoded.stringByReplacingPercentEscapesUsingEncoding(NSUTF8StringEncoding) {
println(normal)
}</langsyntaxhighlight>
 
=={{header|Tcl}}==
This code is careful to ensure that any untoward metacharacters in the input string still do not cause any problems.
<langsyntaxhighlight lang="tcl">proc urlDecode {str} {
set specialMap {"[" "%5B" "]" "%5D"}
set seqRE {%([0-9a-fA-F]{2})}
Line 1,679 ⟶ 1,728:
set modStr [regsub -all $seqRE [string map $specialMap $str] $replacement]
return [encoding convertfrom utf-8 [subst -nobackslash -novariable $modStr]]
}</langsyntaxhighlight>
Demonstrating:
<langsyntaxhighlight lang="tcl">puts [urlDecode "http%3A%2F%2Ffoo%20bar%2F"]</langsyntaxhighlight>
{{out}}
<pre>http://foo bar/</pre>
 
=={{header|TUSCRIPT}}==
<langsyntaxhighlight lang="tuscript">
$$ MODE TUSCRIPT
url_encoded="http%3A%2F%2Ffoo%20bar%2F"
Line 1,695 ⟶ 1,744:
PRINT "encoded: ", url_encoded
PRINT "decoded: ", url_decoded
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,707 ⟶ 1,756:
{{works with|ksh}}
 
<langsyntaxhighlight lang="bash">urldecode() { local u="${1//+/ }"; printf '%b' "${u//%/\\x}"; }</langsyntaxhighlight>
 
Alternative: Replace <code>printf '%b' "${u//%/\\x}"</code> with <code>echo -e "${u//%/\\x}"</code>
 
Example:
 
<langsyntaxhighlight lang="bash">urldecode http%3A%2F%2Ffoo%20bar%2F
http://foo bar/
 
urldecode google.com/search?q=%60Abdu%27l-Bah%C3%A1
google.com/search?q=`Abdu'l-Bahároot@
</syntaxhighlight>
</lang>
 
 
 
<langsyntaxhighlight lang="bash">function urldecode
{
typeset encoded=$1 decoded= rest= c= c1= c2=
Line 1,784 ⟶ 1,833:
fi
}
</syntaxhighlight>
</lang>
 
=={{header|VBScript}}==
<langsyntaxhighlight VBScriptlang="vbscript">Function RegExTest(str,patrn)
Dim regEx
Set regEx = New RegExp
Line 1,831 ⟶ 1,880:
url = "http%3A%2F%2Ffoo%20bar%C3%A8%2F"
WScript.Echo "Encoded URL: " & url & vbCrLf &_
"Decoded URL: " & UrlDecode(url)</langsyntaxhighlight>
{{out}}
Line 1,837 ⟶ 1,886:
Decoded URL: http://foo barè/</pre>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">import net.urllib
fn main() {
for escaped in [
Line 1,847 ⟶ 1,896:
println(u)
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,856 ⟶ 1,905:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Conv
 
var urlDecode = Fn.new { |enc|
Line 1,880 ⟶ 1,929:
"google.com/search?q=\%60Abdu\%27l-Bah\%C3\%A1"
]
for (enc in encs)System.print(urlDecode.call(enc))</langsyntaxhighlight>
 
{{out}}
Line 1,889 ⟶ 1,938:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">code Text=12;
string 0; \use zero-terminated strings
 
Line 1,911 ⟶ 1,960:
];
 
Text(0, Decode("http%3A%2F%2Ffoo%20bar%2f"))</langsyntaxhighlight>
 
{{out}}
Line 1,920 ⟶ 1,969:
=={{header|Yabasic}}==
{{trans|Phix}}
<langsyntaxhighlight Yabasiclang="yabasic">sub decode_url$(s$)
local res$, ch$
Line 1,938 ⟶ 1,987:
 
print decode_url$("http%3A%2F%2Ffoo%20bar%2F")
print decode_url$("google.com/search?q=%60Abdu%27l-Bah%C3%A1")</langsyntaxhighlight>
 
=={{header|zkl}}==
<langsyntaxhighlight 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(_,b,c){ (b+c).toInt(16).toChar() }) // "%" (ignored) "3"+"1"-->0x31-->"1"</langsyntaxhighlight>
{{out}}
<pre>http://foo bar/</pre>
or use libCurl:
<langsyntaxhighlight lang="zkl">var Curl=Import.lib("zklCurl");
Curl.urlDecode("http%3A%2F%2Ffoo%20bar%2F");</langsyntaxhighlight>
{{out}}<pre>http://foo bar/</pre>
889

edits