URL encoding: Difference between revisions

2,073 bytes added ,  14 days ago
(→‎{{header|AutoHotkey}}: updated code to remove the need to use SetFormat)
 
(8 intermediate revisions by 5 users not shown)
Line 546:
}</syntaxhighlight>
<pre>http%3A%2F%2Ffoo%20bar%2F</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
 
function EncodeURL(URL: string): string;
var I: integer;
begin
Result:='';
for I:=1 to Length(URL) do
if URL[I] in ['0'..'9', 'A'..'Z', 'a'..'z'] then Result:=Result+URL[I]
else Result:=Result+'%'+IntToHex(byte(URL[I]),2);
end;
 
procedure EncodeAndShowURL(Memo: TMemo; URL: string);
var ES: string;
begin
Memo.Lines.Add('Unencoded URL: '+URL);
ES:=EncodeURL(URL);
Memo.Lines.Add('Encoded URL: '+ES);
Memo.Lines.Add('');
end;
 
procedure ShowEncodedURLs(Memo: TMemo);
begin
EncodeAndShowURL(Memo,'http://foo bar/');
EncodeAndShowURL(Memo,'https://rosettacode.org/wiki/URL_encoding');
EncodeAndShowURL(Memo,'https://en.wikipedia.org/wiki/Pikes_Peak_granite');
end;
 
 
 
</syntaxhighlight>
{{out}}
<pre>
Unencoded URL: http://foo bar/
Encoded URL: http%3A%2F%2Ffoo%20bar%2F
 
Unencoded URL: https://rosettacode.org/wiki/URL_encoding
Encoded URL: https%3A%2F%2Frosettacode%2Eorg%2Fwiki%2FURL%5Fencoding
 
Unencoded URL: https://en.wikipedia.org/wiki/Pikes_Peak_granite
Encoded URL: https%3A%2F%2Fen%2Ewikipedia%2Eorg%2Fwiki%2FPikes%5FPeak%5Fgranite
 
Elapsed Time: 11.734 ms.
</pre>
 
 
=={{header|Elixir}}==
Line 782 ⟶ 832:
 
=={{header|Java}}==
Java includes the ''URLEncoder'' and ''URLDecoder'' classes for this specific task.
 
<syntaxhighlight lang="java">
The built-in URLEncoder in Java converts the space " " into a plus-sign "+" instead of "%20":
<syntaxhighlight lang="java">import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
 
</syntaxhighlight>
public class Main
<syntaxhighlight lang="java">
{
URLEncoder.encode("http://foo bar/", StandardCharsets.UTF_8)
public static void main(String[] args) throws UnsupportedEncodingException
</syntaxhighlight>
{
Alternately, you could implement this with a basic for-loop.
String normal = "http://foo bar/";
<syntaxhighlight lang="java">
String encoded = URLEncoder.encode(normal, "utf-8");
String encode(String string) {
System.out.println(encoded);
StringBuilder encoded = new StringBuilder();
for (char character : string.toCharArray()) {
switch (character) {
/* rfc3986 and html5 */
case '-', '.', '_', '~', '*' -> encoded.append(character);
case ' ' -> encoded.append('+');
default -> {
if (alphanumeric(character))
encoded.append(character);
else {
encoded.append("%");
encoded.append("%02x".formatted((int) character));
}
}
}
}
return encoded.toString();
}</syntaxhighlight>
}
 
boolean alphanumeric(char character) {
{{out}}
return (character >= 'A' && character <= 'Z')
<pre>http%3A%2F%2Ffoo+bar%2F</pre>
|| (character >= 'a' && character <= 'z')
|| (character >= '0' && character <= '9');
}
</syntaxhighlight>
<pre>
http%3a%2f%2ffoo+bar%2f
</pre>
 
=={{header|JavaScript}}==
Line 894 ⟶ 966:
 
=={{header|langur}}==
<syntaxhighlight lang="langur">val .urlEncode = ffn(.s) replace({
replace(
.s, re/[^A-Za-z0-9]/,
f(.s2) join "", map f $"%\.b:X02;"s, s2b .s2re/[^A-Za-z0-9]/,
fn(.s2) { join "", map fn .b: "%{{.b:X02}}", s2b .s2 },
)
)
}
 
writeln .urlEncode("https://some website.com/")</syntaxhighlight>
Line 1,665 ⟶ 1,739:
works like ''toPercentEncoded'' and additionally encodes a space with '+'.
Both functions work for byte sequences (characters beyond '\255\' raise the exception RANGE_ERROR).
To encode Unicode characters it is necessary to convert them to UTF-8 with ''striToUtf8'' before[https://seed7.<syntaxhighlightsourceforge.net/libraries/unicode.htm#toUtf8(in_string) lang="seed7">$toUtf8] include "seed7_05before.s7i";
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "encoding.s7i";
 
Line 1,672 ⟶ 1,747:
writeln(toPercentEncoded("http://foo bar/"));
writeln(toUrlEncoded("http://foo bar/"));
end func;</syntaxhighlight>{{out}}
{{out}}
http%3A%2F%2Ffoo%20bar%2F
http%3A%2F%2Ffoo+bar%2F
Line 1,807 ⟶ 1,883:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var urlEncode = Fn.new { |url|
885

edits