URL decoding: Difference between revisions

(added new test case; %25%32%35 should decode to %25 and not %)
 
(12 intermediate revisions by 6 users not shown)
Line 263:
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">
UrlDecodeUriDecode(urlUri) {
needleLoopOffset := "i)(?:%[0-9a-f]{2})+"
positionVarLength := 10
VarSetCapacity(Var, StrPut(Uri, "UTF-8"), 0)
Loop {
Loop Parse, Uri
position := RegExMatch(url, needle, code, position)
Loop {
If (position = 0) {
If (A_Index < LoopOffset) Break{
Continue
}
VarSetCapacity(binary,If StrLen(code)A_LoopField //= 3, 0Chr(37)) {
Loop, Parse, % Number := "0x" . SubStr(codeUri, 2),A_Index %+ "%"1, 2)
{ LoopOffset := A_Index + 3
{ }
NumPut("0x" . A_LoopField, binary, A_Index - 1, "UChar")
Else {
Number := Ord(A_LoopField)
}
replacementNumPut(Number, :=Var, StrGet(&binaryVarLength++, "UTF-8UChar")
url := RegExReplace(url, needle, replacement, , 1, position)
position := position + StrLen(replacement)
}
Return urlStrGet(&Var, VarLength, "UTF-8")
}
MsgBox % UrlDecodeUriDecode("http%3A%2F%2Ffoo%20bar%2F")
MsgBox % UrlDecodeUriDecode("google.com/search?q=%60Abdu%27l-Bah%C3%A1")
MsgBox % UrlDecodeUriDecode("%25%32%35")
</syntaxhighlight>
 
Line 798 ⟶ 799:
 
=={{header|Java}}==
Java offers the ''URLDecoder'' and ''URLEncoder'' classes for this specific task.
 
<syntaxhighlight 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();
}</syntaxhighlight>
}
 
</syntaxhighlight>
{{out}}
<pre>
<pre>http://foo bar/</pre>
google.com/search?q=`Abdu'l-Bahá
</pre>
 
=={{header|JavaScript}}==
Line 924 ⟶ 944:
 
=={{header|langur}}==
<syntaxhighlight lang="langur">val .helper = f(.s) b2s map f toNumber(.x, 16), rest split "%", .s
val finish = fn s:b2s(map(fn x:number(x, 16), rest(split("%", s))))
val .decode = f(.fn s) :replace .(s, re/(%[0-9A-Fa-f]{2})+/, .helperfinish)
 
writeln .decode("http%3A%2F%2Ffoo%20bar%2F")
writeln .decode("google.com/search?q=%60Abdu%27l-Bah%C3%A1")
</syntaxhighlight>
 
Line 1,283 ⟶ 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
String encoded = "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}}==
Line 1,666 ⟶ 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 ''utf8ToStri''[https://seed7.sourceforge.net/libraries/unicode.htm#fromUtf8(in_string) fromUtf8] afterwards.
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "encoding.s7i";
Line 1,885 ⟶ 1,926:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Conv
 
var urlDecode = Fn.new { |enc|
1,007

edits