URL encoding: Difference between revisions
→{{header|langur}}
m (Bash: pure bash solution, no dependencies) |
Langurmonkey (talk | contribs) |
||
(18 intermediate revisions by 10 users not shown) | |||
Line 26:
;Variations:
* Lowercase escapes are legal, as in "<code><nowiki>http%3a%2f%2ffoo%20bar%2f</nowiki></code>".
* Special characters have different encodings for different standards:
** RFC 3986, ''Uniform Resource Identifier (URI): Generic Syntax'', section 2.3, says to preserve "-._~".
** HTML 5, section [http://www.whatwg.org/specs/web-apps/current-work/multipage/association-of-controls-and-forms.html#url-encoded-form-data 4.10.22.5 URL-encoded form data], says to preserve "-._*", and to encode space " " to "+".
** [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI#description encodeURI] function in Javascript will preserve "-._~" (RFC 3986) and ";,/?:@&=+$!*'()#".
;Options:
Line 242 ⟶ 244:
<pre>"http%3A%2F%2Ffoo%20bar%2F"</pre>
=={{header|Applesoft BASIC}}==
<syntaxhighlight lang="gwbasic"> 100 URL$ = "http://foo bar/"
110 GOSUB 140"URL ENCODE URL$ RETURNS R$
120 PRINT R$;
130 END
140 LET R$ = ""
150 LET L = LEN (URL$)
160 IF NOT L THEN RETURN
170 LET H$ = "0123456789ABCDEF"
180 FOR I = 1 TO L
190 LET C$ = MID$ (URL$,I,1)
200 LET C = ASC (C$)
210 IF C < ASC ("0") OR C > ASC ("Z") + 32 OR C > ASC ("9") AND C < ASC ("A") OR C > ASC ("Z") AND C < ASC ("A") + 32 THEN H = INT (C / 16):C$ = "%" + MID$ (H$,H + 1,1) + MID$ (H$,C - H * 16 + 1,1)
220 LET R$ = R$ + C$
230 NEXT I
240 RETURN</syntaxhighlight>
=={{header|Arturo}}==
Line 255 ⟶ 273:
; Modified from http://goo.gl/0a0iJq
UriEncode(Uri, Reserved:="!#$&'()*+,/:;=?@[]") {
Unreserved := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.~"
VarSetCapacity(Var, StrPut(Uri, "UTF-8"), 0)
StrPut(Uri, &Var, "UTF-8")
While (Code := NumGet(Var, A_Index - 1, "UChar")) {
If InStr(Unreserved . Reserved, Chr(Code)) {
Encoded .= Chr(Code)
}
Else {
}
}
Return Encoded
}</syntaxhighlight>
Line 304 ⟶ 320:
=={{header|Bash}}==
<syntaxhighlight lang="bash">urlencode() {
local LC_ALL=C # support unicode: loop bytes, not characters
local c i n=${#1}
for (( i=0; i<n; i++ )); do
Line 530 ⟶ 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 640 ⟶ 706:
http%3A%2F%2Ffoo+bar%2F
</pre>
=={{header|FutureBasic}}==
<pre>
In addition to the generic alphanumeric character set used in the demo code below, FB offers several special character sets for URL encoding:
fn CharacterSetURLFragmentAllowedSet
fn CharacterSetURLHostAllowedSet
fn CharacterSetURLPasswordAllowedSet
fn CharacterSetURLPathAllowedSet
fn CharacterSetURLQueryAllowedSet
fn CharacterSetURLUserAllowedSet
Users can also create custom character strings with:
fn CharacterSetWithCharactersInString( CFStringRef string ) = CFCharacterSetRef
</pre>
<syntaxhighlight lang="futurebasic">
include "NSLog.incl"
local fn PercentEncodeURLString( urlStr as CFStringRef ) as CFStringRef
CFStringRef encodedStr = fn StringByAddingPercentEncodingWithAllowedCharacters( urlStr, fn CharacterSetAlphanumericSet )
end fn = encodedStr
NSLog( @"%@", fn PercentEncodeURLString( @"http://foo bar/" ) )
NSLog( @"%@", fn PercentEncodeURLString( @"http://www.rosettacode.org/wiki/URL_encoding" ) )
HandleEvents
</syntaxhighlight>
{{out}}
<pre>
http%3A%2F%2Ffoo%20bar%2F
http%3A%2F%2Fwww%2Erosettacode%2Eorg%2Fwiki%2FURL%5Fencoding
</pre>
=={{header|Go}}==
Line 656 ⟶ 758:
http%3A%2F%2Ffoo+bar%2F
</pre>
=={{header|Groovy}}==
Line 729 ⟶ 832:
=={{header|Java}}==
Java includes the ''URLEncoder'' and ''URLDecoder'' classes for this specific task.
<syntaxhighlight lang="java">
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
</syntaxhighlight>
<syntaxhighlight lang="java">
URLEncoder.encode("http://foo bar/", StandardCharsets.UTF_8)
</syntaxhighlight>
Alternately, you could implement this with a basic for-loop.
<syntaxhighlight lang="java">
String encode(String string) {
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();
}
boolean alphanumeric(char character) {
return (character >= 'A' && character <= 'Z')
|| (character >= 'a' && character <= 'z')
|| (character >= '0' && character <= '9');
}
</syntaxhighlight>
<pre>
http%3a%2f%2ffoo+bar%2f
</pre>
=={{header|JavaScript}}==
Line 841 ⟶ 966:
=={{header|langur}}==
<syntaxhighlight lang="langur">val .urlEncode = fn(.s) {
replace(
.s, re/[^A-Za-z0-9]/,
)
}
writeln .urlEncode("https://some website.com/")</syntaxhighlight>
{{out}}
<pre>https
=={{header|Lasso}}==
Line 1,623 ⟶ 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
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "encoding.s7i";
Line 1,630 ⟶ 1,747:
writeln(toPercentEncoded("http://foo bar/"));
writeln(toUrlEncoded("http://foo bar/"));
end func;</syntaxhighlight>
{{out}}
http%3A%2F%2Ffoo%20bar%2F
http%3A%2F%2Ffoo+bar%2F
Line 1,753 ⟶ 1,871:
<pre>http%3A%2F%2Ffoo%20bar%C3%A9%2F</pre>
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">import net.urllib
fn main() {
println(urllib.query_escape("http://foo bar/"))
Line 1,765 ⟶ 1,883:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="
var urlEncode = Fn.new { |url|
|