URL decoding: Difference between revisions
Content deleted Content added
Added Algol 68 |
Langurmonkey (talk | contribs) |
||
(49 intermediate revisions by 25 users not shown) | |||
Line 10:
* The encoded string "<code><nowiki>google.com/search?q=%60Abdu%27l-Bah%C3%A1</nowiki></code>" should revert to the unencoded form "<code><nowiki>google.com/search?q=`Abdu'l-Bahá</nowiki></code>".
* The encoded string "<code><nowiki>%25%32%35</nowiki></code>" should revert to the unencoded form "<code><nowiki>%25</nowiki></code>" and '''not''' "<code><nowiki>%</nowiki></code>".
<br><br>
=={{header|11l}}==
<syntaxhighlight lang="11l">F url_decode(s)
V r = ‘’
V i = 0
L i < s.len
I s[i] == ‘%’
[Byte] b
L i < s.len & s[i] == ‘%’
i++
b.append(Int(s[i.+2], radix' 16))
i += 2
r ‘’= b.decode(‘utf-8’)
E
r ‘’= s[i]
i++
R r
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’))</syntaxhighlight>
{{out}}
<pre>
http://foo bar/
https://ru.wikipedia.org/wiki/Транспайлер
</pre>
=={{header|ABAP}}==
<
DATA: lv_encoded_url TYPE string VALUE 'http%3A%2F%2Ffoo%20bar%2F',
Line 25 ⟶ 53:
UNESCAPED = lv_decoded_url.
WRITE: 'Encoded URL: ', lv_encoded_url, /, 'Decoded URL: ', lv_decoded_url.</
=={{header|Action!}}==
<syntaxhighlight lang="action!">PROC Append(CHAR ARRAY s CHAR c)
s(0)==+1
s(s(0))=c
RETURN
CHAR FUNC GetCharFromHex(CHAR c1,c2)
CHAR ARRAY hex=['0 '1 '2 '3 '4 '5 '6 '7 '8 '9 'A 'B 'C 'D 'E 'F]
BYTE i,res
res=0
FOR i=0 TO 15
DO
IF c1=hex(i) THEN res==+i LSH 4 FI
IF c2=hex(i) THEN res==+i FI
OD
RETURN (res)
PROC Decode(CHAR ARRAY in,out)
BYTE i
CHAR c
out(0)=0
i=1
WHILE i<=in(0)
DO
c=in(i)
i==+1
IF c='+ THEN
Append(out,' )
ELSEIF c='% THEN
c=GetCharFromHex(in(i),in(i+1))
i==+2
Append(out,c)
ELSE
Append(out,c)
FI
OD
RETURN
PROC PrintInv(CHAR ARRAY a)
BYTE i
IF a(0)>0 THEN
FOR i=1 TO a(0)
DO
Put(a(i)%$80)
OD
FI
RETURN
PROC Test(CHAR ARRAY in)
CHAR ARRAY out(256)
PrintInv("input ")
PrintF(" %S%E",in)
Decode(in,out)
PrintInv("decoded")
PrintF(" %S%E%E",out)
RETURN
PROC Main()
Test("http%3A%2F%2Ffoo%20bar%2F")
Test("http%3A%2F%2Ffoo+bar%2F*_-.html")
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/URL_decoding.png Screenshot from Atari 8-bit computer]
<pre>
input http%3A%2F%2Ffoo%20bar%2F
decoded http://foo bar/
input http%3A%2F%2Ffoo+bar%2F*_-.html
decoded http://foo bar/*_-.html
</pre>
=={{header|Ada}}==
{{libheader|AWS}}
<
with Ada.Text_IO; use Ada.Text_IO;
procedure Decode is
Line 36 ⟶ 140:
Put_Line (AWS.URL.Decode (Encoded));
end Decode;
</syntaxhighlight>
Without external libraries:
<
function Decode (URL : in String) return String;
end URL;</
<
function Decode (URL : in String) return String is
Buffer : String (1 .. URL'Length);
Line 71 ⟶ 175:
return Buffer (1 .. Filled);
end Decode;
end URL;</
<
Ada.Text_IO;
Line 88 ⟶ 192:
end loop;
end if;
end Test_URL_Decode;</
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
Note: See the discussion page re displaying the results (which is mostly focused on the AWK solution but will apply elsewhere too).
<br>
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"
ELIF c >= "A" AND c <= "F" THEN 10 + ( ABS c - ABS "A" )
Line 127 ⟶ 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 ) )</
{{out}}
<pre>
Line 135 ⟶ 242:
=={{header|Apex}}==
<
EncodingUtil.urlDecode('google.com/search?q=%60Abdu%27l-Bah%C3%A1', 'UTF-8');</
<pre>http://foo bar/
google.com/search?q=`Abdu'l-Bahá</pre>
Line 142 ⟶ 249:
=={{header|AppleScript}}==
{{libheader|AppleScript Toolbox}}
<
=={{header|Arturo}}==
<
print decode.url "google.com/search?q=%60Abdu%27l-Bah%C3%A1"</syntaxhighlight>
{{out}}
Line 158 ⟶ 262:
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">
UriDecode(Uri) {
LoopOffset := 0
VarSetCapacity(Var, StrPut(Uri, "UTF-8"), 0)
{
If (A_Index < LoopOffset) {
If (A_LoopField = Chr(37)) {
Number := "0x" . SubStr(Uri, A_Index + 1, 2)
LoopOffset := A_Index + 3
}
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">
# syntax:
awk '
Line 197 ⟶ 312:
return num + (length(s) ? 16*hex2dec(s) : 0)
} '
</syntaxhighlight>
{{out}}
<pre>
http%3A%2F%2Ffoo%20bar%2F
http://foo bar/
</pre>
OR:
<syntaxhighlight 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>
{{out}}
<pre>
http://foo bar/
</pre>
=={{header|BaCon}}==
<
LOCAL result$
Line 218 ⟶ 347:
PRINT Url_Decode$("http%3A%2F%2Ffoo%20bar%2F")
PRINT Url_Decode$("google.com/search?q=%60Abdu%27l-Bah%C3%A1")</
{{out}}
<pre>
Line 224 ⟶ 353:
google.com/search?q=`Abdu'l-Bahá
</pre>
=={{header|Bash}}==
See [[#UNIX Shell]]
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<
END
Line 248 ⟶ 380:
IF C% >= 97 IF C% <= 122 MID$(A$,A%,1) = CHR$(C%-32)
NEXT
= A$</
{{out}}
<pre>
Line 255 ⟶ 387:
=={{header|Bracmat}}==
<
= decoded hexcode notencoded
. :?decoded
Line 265 ⟶ 397:
)
& out$(decode$http%3A%2F%2Ffoo%20bar%2F)
);</
{{out}}
<pre>http://foo bar/</pre>
=={{header|C}}==
<
#include <string.h>
Line 309 ⟶ 441:
return 0;
}</
=={{header|C sharp}}==
<
namespace URLEncode
Line 329 ⟶ 461:
}
}
}</
{{out}}
Line 339 ⟶ 471:
{{libheader|Poco}}
{{works with|g++}}
<
#include "Poco/URI.h"
#include <iostream>
Line 349 ⟶ 481:
std::cout << encoded << " is decoded: " << decoded << " !" << std::endl ;
return 0 ;
}</
{{out}}
<pre>http%3A%2F%2Ffoo%20bar%2F is decoded: http://foo bar/ !</pre>
Line 360 ⟶ 492:
=={{header|Clojure}}==
<
=={{header|CoffeeScript}}==
<
console.log decodeURIComponent "http%3A%2F%2Ffoo%20bar%2F?name=Foo%20Barson"
</syntaxhighlight>
<syntaxhighlight lang="text">
> coffee foo.coffee
http://foo bar/?name=Foo Barson
</syntaxhighlight>
=={{header|Common Lisp}}==
<
(assert (char= (char string start) #\%))
(if (>= (length string) (+ start 3))
Line 396 ⟶ 528:
finally (return (apply #'concatenate 'string chunks))))
(url-decode "http%3A%2F%2Ffoo%20bar%2F")</
{{out}}
<pre>"http://foo bar/"</pre>
=={{header|Crystal}}==
<syntaxhighlight 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"</syntaxhighlight>
{{out}}
<pre>http://foo bar/
google.com/search?q=`Abdu'l-Bahá</pre>
=={{header|D}}==
<
void main() {
writeln(decodeComponent("http%3A%2F%2Ffoo%20bar%2F"));
}</
<pre>http://foo bar/</pre>
=={{header|Delphi}}==
<
{$APPTYPE CONSOLE}
Line 417 ⟶ 558:
begin
Writeln(TIdURI.URLDecode('http%3A%2F%2Ffoo%20bar%2F'));
end.</
=={{header|Elixir}}==
<
IO.inspect URI.decode("google.com/search?q=%60Abdu%27l-Bah%C3%A1")</
{{out}}
Line 438 ⟶ 579:
=={{header|F_Sharp|F#}}==
{{trans|C#}}
<
let decode uri = Uri.UnescapeDataString(uri)
Line 445 ⟶ 586:
let main argv =
printfn "%s" (decode "http%3A%2F%2Ffoo%20bar%2F")
0</
=={{header|Factor}}==
<
IN: rosetta-code.url-decoding
"http%3A%2F%2Ffoo%20bar%2F"
"google.com/search?q=%60Abdu%27l-Bah%C3%A1"
[ url-decode print ] bi@</
{{out}}
<pre>
Line 462 ⟶ 603:
=={{header|Free Pascal}}==
<
var
ch: Char;
Line 487 ⟶ 628:
pos := pos +1;
end;
end;</
=={{header|FreeBASIC}}==
{{trans|Liberty BASIC}}
{{trans|Pascal}}
<syntaxhighlight lang="freebasic">
Const alphanum = "0123456789abcdefghijklmnopqrstuvwxyz"
Function ToDecimal (cadena As String, base_ As Uinteger) As Uinteger
Dim As Uinteger i, n, result = 0
Dim As Uinteger inlength = Len(cadena)
For i = 1 To inlength
n = Instr(alphanum, Mid(Lcase(cadena),i,1)) - 1
n *= (base_^(inlength-i))
result += n
Next
Return result
End Function
Function url2string(cadena As String) As String
Dim As String c, nc, res
For j As Integer = 1 To Len(cadena)
c = Mid(cadena, j, 1)
If c = "%" Then
nc = Chr(ToDecimal((Mid(cadena, j+1, 2)), 16))
res &= nc
j += 2
Else
res &= c
End If
Next j
Return res
End Function
Dim As String URL = "http%3A%2F%2Ffoo%20bar%2F"
Print "Supplied URL '"; URL; "'"
Print "URL decoding '"; url2string(URL); "'"
URL = "google.com/search?q=%60Abdu%27l-Bah%C3%A1"
Print !"\nSupplied URL '"; URL; "'"
Print "URL decoding '"; url2string(URL); "'"
Sleep
</syntaxhighlight>
=={{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.
<syntaxhighlight lang="frink">URLDecode["google.com/search?q=%60Abdu%27l-Bah%C3%A1","UTF8"]</syntaxhighlight>
=={{header|Go}}==
<
import (
Line 510 ⟶ 701:
fmt.Println(u)
}
}</
{{out}}
<pre>
Line 518 ⟶ 709:
=={{header|Groovy}}==
<
=={{header|Haskell}}==
<
urlDecode :: String -> Maybe String
Line 535 ⟶ 726:
main :: IO ()
main = putStrLn . maybe "Bad decode" id $ urlDecode "http%3A%2F%2Ffoo%20bar%2F"</
{{out}}
<pre>http://foo bar/</pre>
Another approach:
<
import Data.List.Split (splitOn)
Line 552 ⟶ 743:
-- TEST ------------------------------------------------------------------------
main :: IO ()
main = putStrLn $ deCode "http%3A%2F%2Ffoo%20bar%2F"</
{{Out}}
<pre>http://foo bar/</pre>
=={{header|Icon}} and {{header|Unicon}}==
<
procedure main()
Line 578 ⟶ 769:
else move(1)
return c
end</
{{libheader|Icon Programming Library}}
Line 592 ⟶ 783:
Here is an implementation:
<
urldecode=: rplc&(~.,/;"_1&a."2(,:tolower)'%',.toupper hfd i.#a.)</
Example use:
<
http://foo bar/</
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 604 ⟶ 795:
Example use:
<
google.com/search?q=`Abdu'l-Bahá</
=={{header|Java}}==
Java offers the ''URLDecoder'' and ''URLEncoder'' classes for this specific task.
<syntaxhighlight lang="java">
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
</syntaxhighlight>
<syntaxhighlight lang="java">
URLDecoder.decode("http%3A%2F%2Ffoo%20bar%2F", StandardCharsets.UTF_8)
</syntaxhighlight>
Alternately, you could use a regular expression capture
<syntaxhighlight lang="java">
import java.util.regex.Matcher;
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();
}
</syntaxhighlight>
<pre>
http://foo bar/
google.com/search?q=`Abdu'l-Bahá
</pre>
=={{header|JavaScript}}==
<
=={{header|jq}}==
{{works with|jq|1.4}}
If your jq already has "until", then the definition given below may be omitted.
<
def until(condition; next):
def u: if condition then . else (next|u) end;
Line 657 ⟶ 867:
else [ $i + 1, .[1] + $in[$i:$i+1] ]
end)
| .[1]; # answer</
'''Example''':
<
{{out}}
<syntaxhighlight lang="sh">
"http://foo bar/"</
=={{header|Julia}}==
<syntaxhighlight lang="julia">
using URIParser
Line 673 ⟶ 883:
println(enc, " => ", dcd)
</syntaxhighlight>
{{out}}
Line 681 ⟶ 891:
=={{header|Kotlin}}==
<
import java.net.URLDecoder
Line 688 ⟶ 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"))
}</
{{out}}
<pre>
http://foo bar/
google.com/search?q=`Abdu'l-Bahá
</pre>
=={{header|Ksh}}==
<syntaxhighlight lang="ksh">
url_decode()
{
decode="${*//+/ }"
eval print -r -- "\$'${decode//'%'@(??)/'\'x\1"'\$'"}'" 2>/dev/null
}
url_decode "http%3A%2F%2Ffoo%20bar%2F"
url_decode "google.com/search?q=%60Abdu%27l-Bah%C3%A1"
</syntaxhighlight>
{{out}}
Line 698 ⟶ 926:
=={{header|Lambdatalk}}==
Currently lambdatalk has no builtin primitive for decoding URLs. Let's define it using Javascript.
<
1) define a new javascript primitive:
{script
Line 712 ⟶ 940:
</syntaxhighlight>
=={{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}}==
<
-> http://foo bar/
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
dim lookUp$( 256)
Line 745 ⟶ 988:
next j
end function
</syntaxhighlight>
Supplied URL 'http%3A%2F%2Ffoo%20bar%2F'
As string 'http://foo bar/'
=={{header|Lingo}}==
<
-- URL decodes a string
-- @param {string} str
Line 773 ⟶ 1,016:
ba.position = 1
return ba.readRawString(ba.length)
end</
<
put urldecode("google.com/search?q=%60Abdu%27l-Bah%C3%A1")</
{{Out}}
<pre>
Line 783 ⟶ 1,026:
=={{header|LiveCode}}==
<
urlDecode("google.com/search?q=%60Abdu%27l-Bah%C3%A1")</
google.com/search?q=`Abdu'l-Bah√°</
=={{header|Lua}}==
<
return string.char(tonumber(hex,16))
end
Line 798 ⟶ 1,041:
-- will print "http://foo bar/"
print(decodeString("http%3A%2F%2Ffoo%20bar%2F"))</
=={{header|M2000 Interpreter}}==
Line 808 ⟶ 1,051:
b$=chr$(a$) revert bytes to words adding zeroes after each character
<syntaxhighlight lang="m2000 interpreter">
Module CheckIt {
Function decodeUrl$(a$) {
Line 834 ⟶ 1,077:
}
CheckIt
</syntaxhighlight>
=={{header|Maple}}==
<
{{Out}}
<pre> "http://foo bar/"</pre>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
StringReplace[url, "%" ~~ x_ ~~ y_ :> FromDigits[x ~~ y, 16]] //.
StringExpression[x___, Longest[n__Integer], y___] :>
StringExpression[x, FromCharacterCode[{n}, "UTF8"], y]</
Example use:
<syntaxhighlight lang="mathematica">URLDecoding["http%3A%2F%2Ffoo%20bar%2F"]</syntaxhighlight>
{{out}}
<pre>http://foo bar/</pre>
----
Using the built-in URLDecode (http://reference.wolfram.com/language/ref/URLDecode.html) function:
{{out}}
<pre>
In[]:= URLDecode["http%3A%2F%2Ffoo%20bar%2F"]
Out[]= "http://foo bar/"
In[]:= URLDecode["google.com/search?q=%60Abdu%27l-Bah%C3%A1"]
Out[]= "google.com/search?q=`Abdu'l-Bahá"
In[]:= URLDecode[{"Kurt+G%C3%B6del", "Paul+Erd%C5%91s"}]
Out[]= {"Kurt Gödel", "Paul Erdős"}
</pre>
=={{header|MATLAB}} / {{header|Octave}}==
<
u = '';
k = 1;
Line 868 ⟶ 1,123:
end
end
end</
Usage:
<pre>octave:3> urldecoding('http%3A%2F%2Ffoo%20bar%2F')
Line 874 ⟶ 1,129:
=={{header|NetRexx}}==
<
options replace format comments java crossref savelog symbols nobinary
Line 917 ⟶ 1,172:
return decoded
</syntaxhighlight>
{{out}}
Line 932 ⟶ 1,187:
=={{header|NewLISP}}==
<
;; (source http://www.newlisp.org/index.cgi?page=Code_Snippets)
(define (url-decode url (opt nil))
Line 938 ⟶ 1,193:
(replace "%([0-9a-f][0-9a-f])" url (pack "b" (int $1 0 16)) 1))
(url-decode "http%3A%2F%2Ffoo%20bar%2F")</
=={{header|Nim}}==
<
echo decodeUrl("http%3A%2F%2Ffoo%20bar%2F")</
{{out}}
<pre>http://foo bar/</pre>
=={{header|Oberon-2}}==
{{works with|oo2c}}
<
MODULE URLDecoding;
IMPORT
Line 956 ⟶ 1,214:
Out.String(URI.Unescape("google.com/search?q=%60Abdu%27l-Bah%C3%A1"));Out.Ln;
END URLDecoding.
</syntaxhighlight>
{{out}}
<pre>
Line 964 ⟶ 1,222:
=={{header|Objeck}}==
<
class UrlDecode {
function : Main(args : String[]) ~ Nil {
Line 970 ⟶ 1,228:
}
}
</syntaxhighlight>
=={{header|Objective-C}}==
<
NSString *normal = [encoded stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%@", normal);</
{{works with|Mac OS X|10.9+}}{{works with|iOS|7+}}
<
NSString *normal = [encoded stringByRemovingPercentEncoding];
NSLog(@"%@", normal);</
=={{header|OCaml}}==
Line 985 ⟶ 1,243:
Using the library [http://projects.camlcity.org/projects/ocamlnet.html ocamlnet] from the interactive loop:
<
# #use "topfind";;
# #require "netstring";;
# Netencoding.Url.decode "http%3A%2F%2Ffoo%20bar%2F" ;;
- : string = "http://foo bar/"</
==
While the implementation shown for [[#REXX|Rexx]] will also work with ooRexx, this version uses ooRexx syntax to invoke the built-in functions.
<
X = 0
url. = ''
Line 1,033 ⟶ 1,291:
End e_
Return decoded</
{{out}}
Line 1,046 ⟶ 1,304:
mailto:"Irma User" <irma.user@mail.com>
</pre>
=={{header|PascalABC.NET}}==
<syntaxhighlight lang="delphi">
uses System;
function URLDecode(s: string) := Uri.UnescapeDataString(s);
begin
Println(URLDecode('http%3A%2F%2Ffoo%20bar%2F'));
Println(URLDecode('google.com/search?q=%60Abdu%27l-Bah%C3%A1'));
Println(URLDecode('%25%32%35'));
end.
</syntaxhighlight>
{{out}}
<pre>
http://foo bar/
google.com/search?q=`Abdu'l-Bahá
%25
</pre>
=={{header|Perl}}==
<
my $s = shift;
$s =~ tr/\+/ /;
Line 1,057 ⟶ 1,335:
print urldecode('http%3A%2F%2Ffoo+bar%2F')."\n";
</syntaxhighlight>
<
use strict ;
use URI::Escape ;
Line 1,065 ⟶ 1,343:
my $encoded = "http%3A%2F%2Ffoo%20bar%2F" ;
my $unencoded = uri_unescape( $encoded ) ;
print "The unencoded string is $unencoded !\n" ;</
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\decode_url.exw
-- ===========================
--</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">decode_url</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">skip</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">skip</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">skip</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">1</span>
<span
<span style="color: #004080;">integer</span> <span style="color: #000000;">ch</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">ch</span><span style="color: #0000FF;">=</span><span style="color: #008000;">'%'</span> <span style="color: #008080;">then</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">scanres</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;"><=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">scanres</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">scanf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"#"</span><span style="color: #0000FF;">&</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">],</span><span style="color: #008000;">"%x"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">scanres</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">return</span> <span style="color: #008000;">"decode error"</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">skip</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span>
<span style="color: #000000;">ch</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">scanres</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">][</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">ch</span><span style="color: #0000FF;">=</span><span style="color: #008000;">'+'</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">ch</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">' '</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">ch</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">decode_url</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"http%3A%2F%2Ffoo%20bar%2F"</span><span style="color: #0000FF;">)})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">decode_url</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"google.com/search?q=%60Abdu%27l-Bah%C3%A1"</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}}
<pre>
Line 1,108 ⟶ 1,391:
=={{header|PHP}}==
<
$encoded = "http%3A%2F%2Ffoo%20bar%2F";
$unencoded = rawurldecode($encoded);
echo "The unencoded string is $unencoded !\n";
?></
=={{header|PicoLisp}}==
Line 1,119 ⟶ 1,402:
=={{header|Pike}}==
<syntaxhighlight lang="pike">
void main()
{
Line 1,133 ⟶ 1,416:
}
}
</syntaxhighlight>
{{Out}}
<pre>
Line 1,141 ⟶ 1,424:
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
[System.Web.HttpUtility]::UrlDecode("http%3A%2F%2Ffoo%20bar%2F")
</syntaxhighlight>
{{Out}}
<pre>
Line 1,150 ⟶ 1,433:
=={{header|PureBasic}}==
<
Debug URL$ ; http://foo bar/</
=={{header|Python}}==
<syntaxhighlight lang="python">
#Python 2.X
import urllib
Line 1,162 ⟶ 1,445:
from urllib.parse import unquote
print(unquote('http%3A%2F%2Ffoo%20bar%2F'))
</syntaxhighlight>
=={{header|R}}==
<
=={{header|Racket}}==
<
#lang racket
(require net/uri-codec)
(uri-decode "http%3A%2F%2Ffoo%20bar%2F")
</syntaxhighlight>
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku"
google.com/search?q=%60Abdu%27l-Bah%C3%A1 >;
say .subst( :g,
/ [ '%' ( <xdigit> ** 2 ) ]+ / ,
{ Blob.new((:16(~$_) for $0)).decode }
) for @urls;</syntaxhighlight>
{{out}}
<pre>http://foo bar/
google.com/search?q=`Abdu'l-Bahá</pre>
=={{header|Red}}==
<syntaxhighlight 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á"</syntaxhighlight>
=={{header|Retro}}==
This is provided by the '''casket''' library (used for web app development).
<
{{
Line 1,206 ⟶ 1,497:
}}
"http%3A%2F%2Ffoo%20bar%2F" decode buffer puts</
=={{header|REXX}}==
Line 1,212 ⟶ 1,503:
{{Trans|ooRexx}}
Tested with the ooRexx and Regina interpreters.
<
Do
Line 1,260 ⟶ 1,551:
End
Exit
</syntaxhighlight>
{{out}}
Line 1,276 ⟶ 1,567:
===version 2===
This REXX version is identical to version 1, but with superfluous and dead code removed.
<
url.1='http%3A%2F%2Ffoo%20bar%2F'
url.2='mailto%3A%22Ivan%20Aim%22%20%3Civan%2Eaim%40email%2Ecom%3E'
Line 1,304 ⟶ 1,595:
end /*while*/
return decoded</
{{out|output|text= is identical to the 1<sup>st</sup> REXX version.}}
===version 3===
This REXX version is a shorter version of version 2.
<
url. =
url.1='http%3A%2F%2Ffoo%20bar%2F'
Line 1,330 ⟶ 1,621:
else URL= URL'%'code
end /*until*/
return URL</
{{out|output|text= is identical to the 1<sup>st</sup> REXX version.}}
Line 1,336 ⟶ 1,627:
Use any one of <code>CGI.unescape</code> or <code>URI.decode_www_form_component</code>. These methods also convert "+" to " ".
<
puts CGI.unescape("http%3A%2F%2Ffoo%20bar%2F")
# => "http://foo bar/"</
{{works with|Ruby|1.9.2}}
<
puts URI.decode_www_form_component("http%3A%2F%2Ffoo%20bar%2F")
# => "http://foo bar/"</
<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}}==
<
const INPUT2: &str = "google.com/search?q=%60Abdu%27l-Bah%C3%A1";
Line 1,383 ⟶ 1,674:
println!("{}", decode(INPUT1));
println!("{}", decode(INPUT2));
}</
{{out}}
<pre>
Line 1,391 ⟶ 1,682:
=={{header|Scala}}==
{{libheader|Scala}}<
import scala.compat.Platform.currentTime
Line 1,406 ⟶ 1,697:
println(s"Successfully completed without errors. [total ${currentTime - executionStart} ms]")
}</
=={{header|Seed7}}==
Line 1,416 ⟶ 1,707:
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
<
include "encoding.s7i";
Line 1,424 ⟶ 1,715:
writeln(fromPercentEncoded("http%3A%2F%2Ffoo%20bar%2F"));
writeln(fromUrlEncoded("http%3A%2F%2Ffoo+bar%2F"));
end func;</
{{out}}
Line 1,434 ⟶ 1,725:
=={{header|Sidef}}==
{{trans|Perl}}
<
str.gsub!('+', ' ');
str.gsub!(/\%([A-Fa-f0-9]{2})/, {|a| 'C'.pack(a.hex)});
Line 1,440 ⟶ 1,731:
}
say urldecode('http%3A%2F%2Ffoo+bar%2F'); # => "http://foo bar/"</
=={{header|Swift}}==
<
let encoded = "http%3A%2F%2Ffoo%20bar%2F"
if let normal = encoded.stringByReplacingPercentEscapesUsingEncoding(NSUTF8StringEncoding) {
println(normal)
}</
=={{header|Tcl}}==
This code is careful to ensure that any untoward metacharacters in the input string still do not cause any problems.
<
set specialMap {"[" "%5B" "]" "%5D"}
set seqRE {%([0-9a-fA-F]{2})}
Line 1,458 ⟶ 1,749:
set modStr [regsub -all $seqRE [string map $specialMap $str] $replacement]
return [encoding convertfrom utf-8 [subst -nobackslash -novariable $modStr]]
}</
Demonstrating:
<
{{out}}
<pre>http://foo bar/</pre>
=={{header|TUSCRIPT}}==
<
$$ MODE TUSCRIPT
url_encoded="http%3A%2F%2Ffoo%20bar%2F"
Line 1,474 ⟶ 1,765:
PRINT "encoded: ", url_encoded
PRINT "decoded: ", url_decoded
</syntaxhighlight>
{{out}}
<pre>
Line 1,486 ⟶ 1,777:
{{works with|ksh}}
<
Alternative: Replace <code>printf '%b' "${u//%/\\x}"</code> with <code>echo -e "${u//%/\\x}"</code>
Example:
<
http://foo bar/
urldecode google.com/search?q=%60Abdu%27l-Bah%C3%A1
google.com/search?q=`Abdu'l-Bahároot@
</syntaxhighlight>
<
{
typeset encoded=$1 decoded= rest= c= c1= c2=
Line 1,563 ⟶ 1,854:
fi
}
</syntaxhighlight>
=={{header|VBScript}}==
<
Dim regEx
Set regEx = New RegExp
Line 1,610 ⟶ 1,901:
url = "http%3A%2F%2Ffoo%20bar%C3%A8%2F"
WScript.Echo "Encoded URL: " & url & vbCrLf &_
"Decoded URL: " & UrlDecode(url)</
{{out}}
<pre>Encoded URL: http%3A%2F%2Ffoo%20bar%C3%A8%2F
Decoded URL: http://foo barè/</pre>
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">import net.urllib
fn main() {
for escaped in [
"http%3A%2F%2Ffoo%20bar%2F",
"google.com/search?q=%60Abdu%27l-Bah%C3%A1",
] {
u := urllib.query_unescape(escaped)?
println(u)
}
}</syntaxhighlight>
{{out}}
<pre>
http://foo bar/
google.com/search?q=`Abdu'l-Bahá
</pre>
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./fmt" for Conv
var urlDecode = Fn.new { |enc|
var res = ""
var i = 0
while (i < enc.count) {
var c = enc[i]
if (c == "\%") {
var b = Conv.atoi(enc[i+1..i+2], 16)
res = res + String.fromByte(b)
i = i + 3
} else {
res = res + c
i = i + 1
}
}
return res
}
// We need to escape % characters in Wren as % is otherwise used for string interpolation.
var encs = [
"http\%3A\%2F\%2Ffoo\%20bar\%2F",
"google.com/search?q=\%60Abdu\%27l-Bah\%C3\%A1"
]
for (enc in encs)System.print(urlDecode.call(enc))</syntaxhighlight>
{{out}}
<pre>
http://foo bar/
google.com/search?q=`Abdu'l-Bahá
</pre>
=={{header|XPL0}}==
<
string 0; \use zero-terminated strings
Line 1,639 ⟶ 1,981:
];
Text(0, Decode("http%3A%2F%2Ffoo%20bar%2f"))</
{{out}}
Line 1,648 ⟶ 1,990:
=={{header|Yabasic}}==
{{trans|Phix}}
<
local res$, ch$
Line 1,666 ⟶ 2,008:
print decode_url$("http%3A%2F%2Ffoo%20bar%2F")
print decode_url$("google.com/search?q=%60Abdu%27l-Bah%C3%A1")</
=={{header|zkl}}==
<
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"</
{{out}}
<pre>http://foo bar/</pre>
or use libCurl:
<
Curl.urlDecode("http%3A%2F%2Ffoo%20bar%2F");</
{{out}}<pre>http://foo bar/</pre>
|