URL decoding: Difference between revisions
m
syntax highlighting fixup automation
SqrtNegInf (talk | contribs) m (→{{header|Raku}}: use 'xdigit', better documented) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 13:
=={{header|11l}}==
<
V r = ‘’
V i = 0
Line 30:
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’))</
{{out}}
Line 40:
=={{header|ABAP}}==
<
DATA: lv_encoded_url TYPE string VALUE 'http%3A%2F%2Ffoo%20bar%2F',
Line 51:
UNESCAPED = lv_decoded_url.
WRITE: 'Encoded URL: ', lv_encoded_url, /, 'Decoded URL: ', lv_decoded_url.</
=={{header|Action!}}==
<
s(0)==+1
s(s(0))=c
Line 118:
Test("http%3A%2F%2Ffoo%20bar%2F")
Test("http%3A%2F%2Ffoo+bar%2F*_-.html")
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/URL_decoding.png Screenshot from Atari 8-bit computer]
Line 131:
=={{header|Ada}}==
{{libheader|AWS}}
<
with Ada.Text_IO; use Ada.Text_IO;
procedure Decode is
Line 138:
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 173:
return Buffer (1 .. Filled);
end Decode;
end URL;</
<
Ada.Text_IO;
Line 190:
end loop;
end if;
end Test_URL_Decode;</
=={{header|ALGOL 68}}==
Line 197:
<br>
For the second task example, this outputs the encoded UTF-8 characters, what you see depends on what you look at it with...
<
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:
# 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 240:
=={{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 247:
=={{header|AppleScript}}==
{{libheader|AppleScript Toolbox}}
<
=={{header|Arturo}}==
<
print decode.url "google.com/search?q=%60Abdu%27l-Bah%C3%A1"</
{{out}}
Line 260:
=={{header|AutoHotkey}}==
<
SetFormat, Integer, hex
Loop Parse, encURL
Line 273:
else out .= A_LoopField
MsgBox % out ; http://foo bar/
</syntaxhighlight>
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax:
awk '
Line 299:
return num + (length(s) ? 16*hex2dec(s) : 0)
} '
</syntaxhighlight>
{{out}}
<pre>
Line 308:
OR:
<
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}}
Line 321:
=={{header|BaCon}}==
<
LOCAL result$
Line 334:
PRINT Url_Decode$("http%3A%2F%2Ffoo%20bar%2F")
PRINT Url_Decode$("google.com/search?q=%60Abdu%27l-Bah%C3%A1")</
{{out}}
<pre>
Line 343:
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<
END
Line 364:
IF C% >= 97 IF C% <= 122 MID$(A$,A%,1) = CHR$(C%-32)
NEXT
= A$</
{{out}}
<pre>
Line 371:
=={{header|Bracmat}}==
<
= decoded hexcode notencoded
. :?decoded
Line 381:
)
& out$(decode$http%3A%2F%2Ffoo%20bar%2F)
);</
{{out}}
<pre>http://foo bar/</pre>
=={{header|C}}==
<
#include <string.h>
Line 425:
return 0;
}</
=={{header|C sharp}}==
<
namespace URLEncode
Line 445:
}
}
}</
{{out}}
Line 455:
{{libheader|Poco}}
{{works with|g++}}
<
#include "Poco/URI.h"
#include <iostream>
Line 465:
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 476:
=={{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 512:
finally (return (apply #'concatenate 'string chunks))))
(url-decode "http%3A%2F%2Ffoo%20bar%2F")</
{{out}}
<pre>"http://foo bar/"</pre>
=={{header|Crystal}}==
<
puts URI.decode "http%3A%2F%2Ffoo%20bar%2F"
puts URI.decode "google.com/search?q=%60Abdu%27l-Bah%C3%A1"</
{{out}}
<pre>http://foo bar/
Line 526:
=={{header|D}}==
<
void main() {
writeln(decodeComponent("http%3A%2F%2Ffoo%20bar%2F"));
}</
<pre>http://foo bar/</pre>
=={{header|Delphi}}==
<
{$APPTYPE CONSOLE}
Line 542:
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 563:
=={{header|F_Sharp|F#}}==
{{trans|C#}}
<
let decode uri = Uri.UnescapeDataString(uri)
Line 570:
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 587:
=={{header|Free Pascal}}==
<
var
ch: Char;
Line 612:
pos := pos +1;
end;
end;</
Line 618:
{{trans|Liberty BASIC}}
{{trans|Pascal}}
<
Const alphanum = "0123456789abcdefghijklmnopqrstuvwxyz"
Line 658:
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.
<
=={{header|Go}}==
<
import (
Line 685:
fmt.Println(u)
}
}</
{{out}}
<pre>
Line 693:
=={{header|Groovy}}==
<
=={{header|Haskell}}==
<
urlDecode :: String -> Maybe String
Line 710:
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 727:
-- 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 753:
else move(1)
return c
end</
{{libheader|Icon Programming Library}}
Line 767:
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 779:
Example use:
<
google.com/search?q=`Abdu'l-Bahá</
=={{header|Java}}==
<
import java.net.URLDecoder;
Line 795:
System.out.println(normal);
}
}</
{{out}}
Line 801:
=={{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 832:
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 848:
println(enc, " => ", dcd)
</syntaxhighlight>
{{out}}
Line 856:
=={{header|Kotlin}}==
<
import java.net.URLDecoder
Line 863:
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}}
Line 872:
=={{header|Ksh}}==
<syntaxhighlight lang="ksh">
url_decode()
{
Line 881:
url_decode "http%3A%2F%2Ffoo%20bar%2F"
url_decode "google.com/search?q=%60Abdu%27l-Bah%C3%A1"
</syntaxhighlight>
{{out}}
Line 891:
=={{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 905:
</syntaxhighlight>
=={{header|Lasso}}==
<
-> http://foo bar/
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
dim lookUp$( 256)
Line 938:
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 966:
ba.position = 1
return ba.readRawString(ba.length)
end</
<
put urldecode("google.com/search?q=%60Abdu%27l-Bah%C3%A1")</
{{Out}}
<pre>
Line 976:
=={{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 991:
-- will print "http://foo bar/"
print(decodeString("http%3A%2F%2Ffoo%20bar%2F"))</
=={{header|M2000 Interpreter}}==
Line 1,001:
b$=chr$(a$) revert bytes to words adding zeroes after each character
<syntaxhighlight lang="m2000 interpreter">
Module CheckIt {
Function decodeUrl$(a$) {
Line 1,027:
}
CheckIt
</syntaxhighlight>
=={{header|Maple}}==
<
{{Out}}
Line 1,036:
=={{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:
<
{{out}}
<pre>http://foo bar/</pre>
Line 1,061:
=={{header|MATLAB}} / {{header|Octave}}==
<
u = '';
k = 1;
Line 1,073:
end
end
end</
Usage:
<pre>octave:3> urldecoding('http%3A%2F%2Ffoo%20bar%2F')
Line 1,079:
=={{header|NetRexx}}==
<
options replace format comments java crossref savelog symbols nobinary
Line 1,122:
return decoded
</syntaxhighlight>
{{out}}
Line 1,137:
=={{header|NewLISP}}==
<
;; (source http://www.newlisp.org/index.cgi?page=Code_Snippets)
(define (url-decode url (opt nil))
Line 1,143:
(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}}
Line 1,155:
=={{header|Oberon-2}}==
{{works with|oo2c}}
<
MODULE URLDecoding;
IMPORT
Line 1,164:
Out.String(URI.Unescape("google.com/search?q=%60Abdu%27l-Bah%C3%A1"));Out.Ln;
END URLDecoding.
</syntaxhighlight>
{{out}}
<pre>
Line 1,172:
=={{header|Objeck}}==
<
class UrlDecode {
function : Main(args : String[]) ~ Nil {
Line 1,178:
}
}
</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 1,193:
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/"</
=={{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.
<
X = 0
url. = ''
Line 1,241:
End e_
Return decoded</
{{out}}
Line 1,257:
=={{header|Perl}}==
<
my $s = shift;
$s =~ tr/\+/ /;
Line 1,265:
print urldecode('http%3A%2F%2Ffoo+bar%2F')."\n";
</syntaxhighlight>
<
use strict ;
use URI::Escape ;
Line 1,273:
my $encoded = "http%3A%2F%2Ffoo%20bar%2F" ;
my $unencoded = uri_unescape( $encoded ) ;
print "The unencoded string is $unencoded !\n" ;</
=={{header|Phix}}==
<!--<
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\decode_url.exw
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>
<!--</
{{Out}}
<pre>
Line 1,321:
=={{header|PHP}}==
<
$encoded = "http%3A%2F%2Ffoo%20bar%2F";
$unencoded = rawurldecode($encoded);
echo "The unencoded string is $unencoded !\n";
?></
=={{header|PicoLisp}}==
Line 1,332:
=={{header|Pike}}==
<syntaxhighlight lang="pike">
void main()
{
Line 1,346:
}
}
</syntaxhighlight>
{{Out}}
<pre>
Line 1,354:
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
[System.Web.HttpUtility]::UrlDecode("http%3A%2F%2Ffoo%20bar%2F")
</syntaxhighlight>
{{Out}}
<pre>
Line 1,363:
=={{header|PureBasic}}==
<
Debug URL$ ; http://foo bar/</
=={{header|Python}}==
<syntaxhighlight lang="python">
#Python 2.X
import urllib
Line 1,375:
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 >;
Line 1,397:
/ [ '%' ( <xdigit> ** 2 ) ]+ / ,
{ Blob.new((:16(~$_) for $0)).decode }
) for @urls;</
{{out}}
<pre>http://foo bar/
Line 1,404:
=={{header|Red}}==
<
== "http://foo bar/"
>> dehex "google.com/search?q=%60Abdu%27l-Bah%C3%A1"
== "google.com/search?q=`Abdu'l-Bahá"</
=={{header|Retro}}==
This is provided by the '''casket''' library (used for web app development).
<
{{
Line 1,427:
}}
"http%3A%2F%2Ffoo%20bar%2F" decode buffer puts</
=={{header|REXX}}==
Line 1,433:
{{Trans|ooRexx}}
Tested with the ooRexx and Regina interpreters.
<
Do
Line 1,481:
End
Exit
</syntaxhighlight>
{{out}}
Line 1,497:
===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,525:
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,551:
else URL= URL'%'code
end /*until*/
return URL</
{{out|output|text= is identical to the 1<sup>st</sup> REXX version.}}
Line 1,557:
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,604:
println!("{}", decode(INPUT1));
println!("{}", decode(INPUT2));
}</
{{out}}
<pre>
Line 1,612:
=={{header|Scala}}==
{{libheader|Scala}}<
import scala.compat.Platform.currentTime
Line 1,627:
println(s"Successfully completed without errors. [total ${currentTime - executionStart} ms]")
}</
=={{header|Seed7}}==
Line 1,638:
Both functions return byte sequences.
To decode Unicode characters it is necessary to convert them from UTF-8 with ''utf8ToStri'' afterwards.
<
include "encoding.s7i";
Line 1,645:
writeln(fromPercentEncoded("http%3A%2F%2Ffoo%20bar%2F"));
writeln(fromUrlEncoded("http%3A%2F%2Ffoo+bar%2F"));
end func;</
{{out}}
Line 1,655:
=={{header|Sidef}}==
{{trans|Perl}}
<
str.gsub!('+', ' ');
str.gsub!(/\%([A-Fa-f0-9]{2})/, {|a| 'C'.pack(a.hex)});
Line 1,661:
}
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,679:
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,695:
PRINT "encoded: ", url_encoded
PRINT "decoded: ", url_decoded
</syntaxhighlight>
{{out}}
<pre>
Line 1,707:
{{works with|ksh}}
<
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,784:
fi
}
</syntaxhighlight>
=={{header|VBScript}}==
<
Dim regEx
Set regEx = New RegExp
Line 1,831:
url = "http%3A%2F%2Ffoo%20bar%C3%A8%2F"
WScript.Echo "Encoded URL: " & url & vbCrLf &_
"Decoded URL: " & UrlDecode(url)</
{{out}}
Line 1,838:
=={{header|Vlang}}==
<
fn main() {
for escaped in [
Line 1,847:
println(u)
}
}</
{{out}}
<pre>
Line 1,856:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<
var urlDecode = Fn.new { |enc|
Line 1,880:
"google.com/search?q=\%60Abdu\%27l-Bah\%C3\%A1"
]
for (enc in encs)System.print(urlDecode.call(enc))</
{{out}}
Line 1,889:
=={{header|XPL0}}==
<
string 0; \use zero-terminated strings
Line 1,911:
];
Text(0, Decode("http%3A%2F%2Ffoo%20bar%2f"))</
{{out}}
Line 1,920:
=={{header|Yabasic}}==
{{trans|Phix}}
<
local res$, ch$
Line 1,938:
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>
|