URL decoding: Difference between revisions
→{{header|langur}}
Langurmonkey (talk | contribs) (added langur language example) |
Langurmonkey (talk | contribs) |
||
(14 intermediate revisions by 5 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>
Line 260 ⟶ 262:
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">
UriDecode(Uri) {
LoopOffset := 0
VarSetCapacity(Var, StrPut(Uri, "UTF-8"), 0)
{
If (A_Index < LoopOffset) {
read .= A_LoopField, --reading▼
}▼
Number := "0x" . SubStr(Uri, A_Index + 1, 2)
LoopOffset := A_Index + 3
MsgBox % out ; http://foo bar/▼
}
Else {
Number := Ord(A_LoopField)
}
NumPut(Number, Var, VarLength++, "UChar")
▲ }
Return StrGet(&Var, VarLength, "UTF-8")
}
MsgBox % UriDecode("google.com/search?q=%60Abdu%27l-Bah%C3%A1")
MsgBox % UriDecode("%25%32%35")
</syntaxhighlight>
Line 786 ⟶ 799:
=={{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 lang="java">
URLDecoder.decode("http%3A%2F%2Ffoo%20bar%2F", StandardCharsets.UTF_8)
</syntaxhighlight>
▲ {
Alternately, you could use a regular expression capture
▲ String encoded = "http%3A%2F%2Ffoo%20bar%2F";
<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>
}
</syntaxhighlight>
<pre>
google.com/search?q=`Abdu'l-Bahá
</pre>
=={{header|JavaScript}}==
Line 912 ⟶ 944:
=={{header|langur}}==
<syntaxhighlight lang="langur">val .finish =
▲<syntaxhighlight lang="langur">val .finish = f(.s) b2s map f toNumber(.x, 16), rest split "%", .s
▲val .decode = f(.s) replace .s, re/(%[0-9A-Fa-f]{2})+/, .finish
writeln .decode("http%3A%2F%2Ffoo%20bar%2F")
Line 924 ⟶ 955:
google.com/search?q=`Abdu'l-Bahá
</pre>
=={{header|Lasso}}==
Line 1,656 ⟶ 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
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "encoding.s7i";
Line 1,875 ⟶ 1,905:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="
var urlDecode = Fn.new { |enc|
|