Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
5
Task/URL-encoding/00-META.yaml
Normal file
5
Task/URL-encoding/00-META.yaml
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
category:
|
||||
- String manipulation
|
||||
- Encodings
|
||||
from: http://rosettacode.org/wiki/URL_encoding
|
||||
39
Task/URL-encoding/00-TASK.txt
Normal file
39
Task/URL-encoding/00-TASK.txt
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
;Task:
|
||||
Provide a function or mechanism to convert a provided string into URL encoding representation.
|
||||
|
||||
In URL encoding, special characters, control characters and extended characters
|
||||
are converted into a percent symbol followed by a two digit hexadecimal code,
|
||||
So a space character encodes into %20 within the string.
|
||||
|
||||
For the purposes of this task, every character except 0-9, A-Z and a-z requires conversion, so the following characters all require conversion by default:
|
||||
|
||||
* ASCII control codes (Character ranges 00-1F hex (0-31 decimal) and 7F (127 decimal).
|
||||
* ASCII symbols (Character ranges 32-47 decimal (20-2F hex))
|
||||
* ASCII symbols (Character ranges 58-64 decimal (3A-40 hex))
|
||||
* ASCII symbols (Character ranges 91-96 decimal (5B-60 hex))
|
||||
* ASCII symbols (Character ranges 123-126 decimal (7B-7E hex))
|
||||
* Extended characters with character codes of 128 decimal (80 hex) and above.
|
||||
|
||||
<br>
|
||||
;Example:
|
||||
The string "<code><nowiki>http://foo bar/</nowiki></code>" would be encoded as "<code><nowiki>http%3A%2F%2Ffoo%20bar%2F</nowiki></code>".
|
||||
|
||||
|
||||
;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:
|
||||
It is permissible to use an exception string (containing a set of symbols
|
||||
that do not need to be converted).
|
||||
However, this is an optional feature and is not a requirement of this task.
|
||||
|
||||
|
||||
;Related tasks:
|
||||
* [[URL decoding]]
|
||||
* [[URL parser]]
|
||||
<br><br>
|
||||
|
||||
23
Task/URL-encoding/11l/url-encoding.11l
Normal file
23
Task/URL-encoding/11l/url-encoding.11l
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
F url_encode(s)
|
||||
V r = ‘’
|
||||
V buf = ‘’
|
||||
|
||||
F flush_buf() // this function is needed because strings in 11l are UTF-16 encoded
|
||||
I @buf != ‘’
|
||||
V bytes = @buf.encode(‘utf-8’)
|
||||
L(b) bytes
|
||||
@r ‘’= ‘%’hex(b).zfill(2)
|
||||
@buf = ‘’
|
||||
|
||||
L(c) s
|
||||
I c C (‘0’..‘9’, ‘a’..‘z’, ‘A’..‘Z’, ‘_’, ‘.’, ‘-’, ‘~’)
|
||||
flush_buf()
|
||||
r ‘’= c
|
||||
E
|
||||
buf ‘’= c
|
||||
|
||||
flush_buf()
|
||||
R r
|
||||
|
||||
print(url_encode(‘http://foo bar/’))
|
||||
print(url_encode(‘https://ru.wikipedia.org/wiki/Транспайлер’))
|
||||
33
Task/URL-encoding/ALGOL-68/url-encoding.alg
Normal file
33
Task/URL-encoding/ALGOL-68/url-encoding.alg
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
BEGIN
|
||||
# encodes the specified url - 0-9, A-Z and a-z are unchanged, #
|
||||
# everything else is converted to %xx where xx are hex-digits #
|
||||
PROC encode url = ( STRING url )STRING:
|
||||
IF url = "" THEN "" # empty string #
|
||||
ELSE
|
||||
# non-empty string #
|
||||
# ensure result will be big enough for a string of all encodable #
|
||||
# characters #
|
||||
STRING hex digits = "0123456789ABCDEF";
|
||||
[ 1 : ( ( UPB url - LWB url ) + 1 ) * 3 ]CHAR result;
|
||||
INT r pos := 0;
|
||||
FOR u pos FROM LWB url TO UPB url DO
|
||||
CHAR c = url[ u pos ];
|
||||
IF ( c >= "0" AND c <= "9" )
|
||||
OR ( c >= "A" AND c <= "Z" )
|
||||
OR ( c >= "a" AND c <= "z" )
|
||||
THEN
|
||||
# no need to encode this character #
|
||||
result[ r pos +:= 1 ] := c
|
||||
ELSE
|
||||
# must encode #
|
||||
INT c code = ABS c;
|
||||
result[ r pos +:= 1 ] := "%";
|
||||
result[ r pos +:= 1 ] := hex digits[ ( c code OVER 16 ) + 1 ];
|
||||
result[ r pos +:= 1 ] := hex digits[ ( c code MOD 16 ) + 1 ]
|
||||
FI
|
||||
OD;
|
||||
result[ 1 : r pos ]
|
||||
FI; # encode url #
|
||||
# task test case #
|
||||
print( ( encode url( "http://foo bar/" ), newline ) )
|
||||
END
|
||||
24
Task/URL-encoding/AWK/url-encoding.awk
Normal file
24
Task/URL-encoding/AWK/url-encoding.awk
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
BEGIN {
|
||||
for (i = 0; i <= 255; i++)
|
||||
ord[sprintf("%c", i)] = i
|
||||
}
|
||||
|
||||
# Encode string with application/x-www-form-urlencoded escapes.
|
||||
function escape(str, c, len, res) {
|
||||
len = length(str)
|
||||
res = ""
|
||||
for (i = 1; i <= len; i++) {
|
||||
c = substr(str, i, 1);
|
||||
if (c ~ /[0-9A-Za-z]/)
|
||||
#if (c ~ /[-._*0-9A-Za-z]/)
|
||||
res = res c
|
||||
#else if (c == " ")
|
||||
# res = res "+"
|
||||
else
|
||||
res = res "%" sprintf("%02X", ord[c])
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
# Escape every line of input.
|
||||
{ print escape($0) }
|
||||
89
Task/URL-encoding/Action-/url-encoding.action
Normal file
89
Task/URL-encoding/Action-/url-encoding.action
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
BYTE FUNC MustEncode(CHAR c CHAR ARRAY ex)
|
||||
BYTE i
|
||||
|
||||
IF c>='a AND c<='z OR c>='A AND c<='Z OR c>='0 AND c<='9 THEN
|
||||
RETURN (0)
|
||||
FI
|
||||
IF ex(0)>0 THEN
|
||||
FOR i=1 TO ex(0)
|
||||
DO
|
||||
IF ex(i)=c THEN
|
||||
RETURN (0)
|
||||
FI
|
||||
OD
|
||||
FI
|
||||
RETURN (1)
|
||||
|
||||
PROC Append(CHAR ARRAY s CHAR c)
|
||||
s(0)==+1
|
||||
s(s(0))=c
|
||||
RETURN
|
||||
|
||||
PROC Encode(CHAR ARRAY in,out,ex BYTE spaceToPlus)
|
||||
CHAR ARRAY hex=['0 '1 '2 '3 '4 '5 '6 '7 '8 '9 'A 'B 'C 'D 'E 'F]
|
||||
BYTE i
|
||||
CHAR c
|
||||
|
||||
out(0)=0
|
||||
FOR i=1 TO in(0)
|
||||
DO
|
||||
c=in(i)
|
||||
IF spaceToPlus=1 AND c=32 THEN
|
||||
Append(out,'+)
|
||||
ELSEIF MustEncode(c,ex) THEN
|
||||
Append(out,'%)
|
||||
Append(out,hex(c RSH 4))
|
||||
Append(out,hex(c&$0F))
|
||||
ELSE
|
||||
Append(out,c)
|
||||
FI
|
||||
OD
|
||||
RETURN
|
||||
|
||||
PROC EncodeRaw(CHAR ARRAY in,out)
|
||||
Encode(in,out,"",0)
|
||||
RETURN
|
||||
|
||||
PROC EncodeRFC3986(CHAR ARRAY in,out)
|
||||
Encode(in,out,"-._~",0)
|
||||
RETURN
|
||||
|
||||
PROC EncodeHTML5(CHAR ARRAY in,out)
|
||||
Encode(in,out,"-._*",1)
|
||||
RETURN
|
||||
|
||||
PROC PrintInv(CHAR ARRAY a)
|
||||
BYTE i
|
||||
|
||||
IF a(0)>0 THEN
|
||||
FOR i=1 TO a(0)
|
||||
DO
|
||||
Put(a(i)%$80)
|
||||
OD
|
||||
FI
|
||||
RETURN
|
||||
|
||||
PROC Test(CHAR ARRAY in)
|
||||
CHAR ARRAY out(256)
|
||||
|
||||
PrintInv("input ")
|
||||
PrintF(" %S%E",in)
|
||||
|
||||
EncodeRaw(in,out)
|
||||
PrintInv("encoded ")
|
||||
PrintF(" %S%E",out)
|
||||
|
||||
EncodeRFC3986(in,out)
|
||||
PrintInv("RFC 3986")
|
||||
PrintF(" %S%E",out)
|
||||
|
||||
EncodeHTML5(in,out)
|
||||
PrintInv("HTML 5 ")
|
||||
PrintF(" %S%E%E",out)
|
||||
RETURN
|
||||
|
||||
PROC Main()
|
||||
Test("http://foo bar/")
|
||||
Test("http://www.rosettacode.org/wiki/URL_encoding")
|
||||
Test("http://foo bar/*_-.html")
|
||||
RETURN
|
||||
7
Task/URL-encoding/Ada/url-encoding.ada
Normal file
7
Task/URL-encoding/Ada/url-encoding.ada
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
with AWS.URL;
|
||||
with Ada.Text_IO; use Ada.Text_IO;
|
||||
procedure Encode is
|
||||
Normal : constant String := "http://foo bar/";
|
||||
begin
|
||||
Put_Line (AWS.URL.Encode (Normal));
|
||||
end Encode;
|
||||
1
Task/URL-encoding/Apex/url-encoding.apex
Normal file
1
Task/URL-encoding/Apex/url-encoding.apex
Normal file
|
|
@ -0,0 +1 @@
|
|||
EncodingUtil.urlEncode('http://foo bar/', 'UTF-8')
|
||||
1
Task/URL-encoding/AppleScript/url-encoding.applescript
Normal file
1
Task/URL-encoding/AppleScript/url-encoding.applescript
Normal file
|
|
@ -0,0 +1 @@
|
|||
AST URL encode "http://foo bar/"
|
||||
15
Task/URL-encoding/Applesoft-BASIC/url-encoding.basic
Normal file
15
Task/URL-encoding/Applesoft-BASIC/url-encoding.basic
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
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
|
||||
2
Task/URL-encoding/Arturo/url-encoding.arturo
Normal file
2
Task/URL-encoding/Arturo/url-encoding.arturo
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
encoded: encode.url.slashes "http://foo bar/"
|
||||
print encoded
|
||||
17
Task/URL-encoding/AutoHotkey/url-encoding.ahk
Normal file
17
Task/URL-encoding/AutoHotkey/url-encoding.ahk
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
MsgBox, % UriEncode("http://foo bar/")
|
||||
|
||||
; 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 {
|
||||
Encoded .= Format("%{:02X}", Code)
|
||||
}
|
||||
}
|
||||
Return Encoded
|
||||
}
|
||||
13
Task/URL-encoding/BBC-BASIC/url-encoding.basic
Normal file
13
Task/URL-encoding/BBC-BASIC/url-encoding.basic
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
PRINT FNurlencode("http://foo bar/")
|
||||
END
|
||||
|
||||
DEF FNurlencode(url$)
|
||||
LOCAL c%, i%
|
||||
WHILE i% < LEN(url$)
|
||||
i% += 1
|
||||
c% = ASCMID$(url$, i%)
|
||||
IF c%<&30 OR c%>&7A OR c%>&39 AND c%<&41 OR c%>&5A AND c%<&61 THEN
|
||||
url$ = LEFT$(url$,i%-1) + "%" + RIGHT$("0"+STR$~c%,2) + MID$(url$,i%+1)
|
||||
ENDIF
|
||||
ENDWHILE
|
||||
= url$
|
||||
41
Task/URL-encoding/Bracmat/url-encoding.bracmat
Normal file
41
Task/URL-encoding/Bracmat/url-encoding.bracmat
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
( ( encode
|
||||
= encoded exceptions octet string
|
||||
. !arg:(?exceptions.?string)
|
||||
& :?encoded
|
||||
& @( !string
|
||||
: ?
|
||||
( %@?octet ?
|
||||
& !encoded
|
||||
( !octet
|
||||
: ( ~<0:~>9
|
||||
| ~<A:~>Z
|
||||
| ~<a:~>z
|
||||
)
|
||||
| @(!exceptions:? !octet ?)
|
||||
& !octet
|
||||
| "%" d2x$(asc$!octet)
|
||||
)
|
||||
: ?encoded
|
||||
& ~
|
||||
)
|
||||
)
|
||||
| str$!encoded
|
||||
)
|
||||
& out$"without exceptions:
|
||||
"
|
||||
& out$(encode$(."http://foo bar/"))
|
||||
& out$(encode$(."mailto:Ivan"))
|
||||
& out$(encode$(."Aim <ivan.aim@email.com>"))
|
||||
& out$(encode$(."mailto:Irma"))
|
||||
& out$(encode$(."User <irma.user@mail.com>"))
|
||||
& out$(encode$(."http://foo.bar.com/~user-name/_subdir/*~.html"))
|
||||
& out$"
|
||||
with RFC 3986 rules:
|
||||
"
|
||||
& out$(encode$("-._~"."http://foo bar/"))
|
||||
& out$(encode$("-._~"."mailto:Ivan"))
|
||||
& out$(encode$("-._~"."Aim <ivan.aim@email.com>"))
|
||||
& out$(encode$("-._~"."mailto:Irma"))
|
||||
& out$(encode$("-._~"."User <irma.user@mail.com>"))
|
||||
& out$(encode$("-._~"."http://foo.bar.com/~user-name/_subdir/*~.html"))
|
||||
);
|
||||
9
Task/URL-encoding/C++/url-encoding.cpp
Normal file
9
Task/URL-encoding/C++/url-encoding.cpp
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
#include <QByteArray>
|
||||
#include <iostream>
|
||||
|
||||
int main( ) {
|
||||
QByteArray text ( "http://foo bar/" ) ;
|
||||
QByteArray encoded( text.toPercentEncoding( ) ) ;
|
||||
std::cout << encoded.data( ) << '\n' ;
|
||||
return 0 ;
|
||||
}
|
||||
17
Task/URL-encoding/C-sharp/url-encoding.cs
Normal file
17
Task/URL-encoding/C-sharp/url-encoding.cs
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
using System;
|
||||
|
||||
namespace URLEncode
|
||||
{
|
||||
internal class Program
|
||||
{
|
||||
private static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine(Encode("http://foo bar/"));
|
||||
}
|
||||
|
||||
private static string Encode(string uri)
|
||||
{
|
||||
return Uri.EscapeDataString(uri);
|
||||
}
|
||||
}
|
||||
}
|
||||
34
Task/URL-encoding/C/url-encoding.c
Normal file
34
Task/URL-encoding/C/url-encoding.c
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
|
||||
char rfc3986[256] = {0};
|
||||
char html5[256] = {0};
|
||||
|
||||
/* caller responsible for memory */
|
||||
void encode(const char *s, char *enc, char *tb)
|
||||
{
|
||||
for (; *s; s++) {
|
||||
if (tb[*s]) sprintf(enc, "%c", tb[*s]);
|
||||
else sprintf(enc, "%%%02X", *s);
|
||||
while (*++enc);
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
const char url[] = "http://foo bar/";
|
||||
char enc[(strlen(url) * 3) + 1];
|
||||
|
||||
int i;
|
||||
for (i = 0; i < 256; i++) {
|
||||
rfc3986[i] = isalnum(i)||i == '~'||i == '-'||i == '.'||i == '_'
|
||||
? i : 0;
|
||||
html5[i] = isalnum(i)||i == '*'||i == '-'||i == '.'||i == '_'
|
||||
? i : (i == ' ') ? '+' : 0;
|
||||
}
|
||||
|
||||
encode(url, enc, rfc3986);
|
||||
puts(enc);
|
||||
|
||||
return 0;
|
||||
}
|
||||
2
Task/URL-encoding/Clojure/url-encoding.clj
Normal file
2
Task/URL-encoding/Clojure/url-encoding.clj
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
(import 'java.net.URLEncoder)
|
||||
(URLEncoder/encode "http://foo bar/" "UTF-8")
|
||||
15
Task/URL-encoding/Common-Lisp/url-encoding.lisp
Normal file
15
Task/URL-encoding/Common-Lisp/url-encoding.lisp
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
(defun needs-encoding-p (char)
|
||||
(not (digit-char-p char 36)))
|
||||
|
||||
(defun encode-char (char)
|
||||
(format nil "%~2,'0X" (char-code char)))
|
||||
|
||||
(defun url-encode (url)
|
||||
(apply #'concatenate 'string
|
||||
(map 'list (lambda (char)
|
||||
(if (needs-encoding-p char)
|
||||
(encode-char char)
|
||||
(string char)))
|
||||
url)))
|
||||
|
||||
(url-encode "http://foo bar/")
|
||||
6
Task/URL-encoding/Crystal/url-encoding.crystal
Normal file
6
Task/URL-encoding/Crystal/url-encoding.crystal
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
require "uri"
|
||||
|
||||
puts URI.encode("http://foo bar/")
|
||||
puts URI.encode("http://foo bar/", space_to_plus: true)
|
||||
puts URI.encode_www_form("http://foo bar/")
|
||||
puts URI.encode_www_form("http://foo bar/", space_to_plus: false)
|
||||
5
Task/URL-encoding/D/url-encoding.d
Normal file
5
Task/URL-encoding/D/url-encoding.d
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
import std.stdio, std.uri;
|
||||
|
||||
void main() {
|
||||
writeln(encodeComponent("http://foo bar/"));
|
||||
}
|
||||
24
Task/URL-encoding/Delphi/url-encoding.delphi
Normal file
24
Task/URL-encoding/Delphi/url-encoding.delphi
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
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;
|
||||
2
Task/URL-encoding/Elixir/url-encoding.elixir
Normal file
2
Task/URL-encoding/Elixir/url-encoding.elixir
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
iex(1)> URI.encode("http://foo bar/", &URI.char_unreserved?/1)
|
||||
"http%3A%2F%2Ffoo%20bar%2F"
|
||||
6
Task/URL-encoding/F-Sharp/url-encoding.fs
Normal file
6
Task/URL-encoding/F-Sharp/url-encoding.fs
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
open System
|
||||
|
||||
[<EntryPoint>]
|
||||
let main args =
|
||||
printfn "%s" (Uri.EscapeDataString(args.[0]))
|
||||
0
|
||||
6
Task/URL-encoding/Factor/url-encoding.factor
Normal file
6
Task/URL-encoding/Factor/url-encoding.factor
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
USING: combinators.short-circuit unicode urls.encoding.private ;
|
||||
|
||||
: my-url-encode ( str -- encoded )
|
||||
[ { [ alpha? ] [ "-._~" member? ] } 1|| ] (url-encode) ;
|
||||
|
||||
"http://foo bar/" my-url-encode print
|
||||
12
Task/URL-encoding/Free-Pascal/url-encoding.pas
Normal file
12
Task/URL-encoding/Free-Pascal/url-encoding.pas
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
function urlEncode(data: string): AnsiString;
|
||||
var
|
||||
ch: AnsiChar;
|
||||
begin
|
||||
Result := '';
|
||||
for ch in data do begin
|
||||
if ((Ord(ch) < 65) or (Ord(ch) > 90)) and ((Ord(ch) < 97) or (Ord(ch) > 122)) then begin
|
||||
Result := Result + '%' + IntToHex(Ord(ch), 2);
|
||||
end else
|
||||
Result := Result + ch;
|
||||
end;
|
||||
end;
|
||||
23
Task/URL-encoding/FreeBASIC/url-encoding.basic
Normal file
23
Task/URL-encoding/FreeBASIC/url-encoding.basic
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
Dim Shared As String lookUp(256)
|
||||
For cadena As Integer = 0 To 256
|
||||
lookUp(cadena) = "%" + Hex(cadena)
|
||||
Next cadena
|
||||
|
||||
Function string2url(cadena As String) As String
|
||||
Dim As String cadTemp, cadDevu
|
||||
For j As Integer = 1 To Len(cadena)
|
||||
cadTemp = Mid(cadena, j, 1)
|
||||
If Instr( "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", cadTemp) Then
|
||||
cadDevu &= cadTemp
|
||||
Else
|
||||
cadDevu &= lookUp(Asc(cadTemp))
|
||||
End If
|
||||
Next j
|
||||
Return cadDevu
|
||||
End Function
|
||||
|
||||
Dim As String URL = "http://foo bar/"
|
||||
|
||||
Print "Supplied URL '"; URL; "'"
|
||||
Print "URL encoding '"; string2url(URL); "'"
|
||||
Sleep
|
||||
1
Task/URL-encoding/Frink/url-encoding.frink
Normal file
1
Task/URL-encoding/Frink/url-encoding.frink
Normal file
|
|
@ -0,0 +1 @@
|
|||
println[URLEncode["http://foo bar/"]]
|
||||
10
Task/URL-encoding/FutureBasic/url-encoding.basic
Normal file
10
Task/URL-encoding/FutureBasic/url-encoding.basic
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
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
|
||||
10
Task/URL-encoding/Go/url-encoding.go
Normal file
10
Task/URL-encoding/Go/url-encoding.go
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println(url.QueryEscape("http://foo bar/"))
|
||||
}
|
||||
3
Task/URL-encoding/Groovy/url-encoding.groovy
Normal file
3
Task/URL-encoding/Groovy/url-encoding.groovy
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
def normal = "http://foo bar/"
|
||||
def encoded = URLEncoder.encode(normal, "utf-8")
|
||||
println encoded
|
||||
14
Task/URL-encoding/Haskell/url-encoding.hs
Normal file
14
Task/URL-encoding/Haskell/url-encoding.hs
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
import qualified Data.Char as Char
|
||||
import Text.Printf
|
||||
|
||||
encode :: Char -> String
|
||||
encode c
|
||||
| c == ' ' = "+"
|
||||
| Char.isAlphaNum c || c `elem` "-._~" = [c]
|
||||
| otherwise = printf "%%%02X" c
|
||||
|
||||
urlEncode :: String -> String
|
||||
urlEncode = concatMap encode
|
||||
|
||||
main :: IO ()
|
||||
main = putStrLn $ urlEncode "http://foo bar/"
|
||||
18
Task/URL-encoding/Icon/url-encoding.icon
Normal file
18
Task/URL-encoding/Icon/url-encoding.icon
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
link hexcvt
|
||||
|
||||
procedure main()
|
||||
write("text = ",image(u := "http://foo bar/"))
|
||||
write("encoded = ",image(ue := encodeURL(u)))
|
||||
end
|
||||
|
||||
procedure encodeURL(s) #: encode data for inclusion in a URL/URI
|
||||
static en
|
||||
initial { # build lookup table for everything
|
||||
en := table()
|
||||
every en[c := !string(~(&digits++&letters))] := "%"||hexstring(ord(c),2)
|
||||
every /en[c := !string(&cset)] := c
|
||||
}
|
||||
|
||||
every (c := "") ||:= en[!s] # re-encode everything
|
||||
return c
|
||||
end
|
||||
2
Task/URL-encoding/J/url-encoding-1.j
Normal file
2
Task/URL-encoding/J/url-encoding-1.j
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
require'strings convert'
|
||||
urlencode=: rplc&((#~2|_1 47 57 64 90 96 122 I.i.@#)a.;"_1'%',.hfd i.#a.)
|
||||
2
Task/URL-encoding/J/url-encoding-2.j
Normal file
2
Task/URL-encoding/J/url-encoding-2.j
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
urlencode 'http://foo bar/'
|
||||
http%3A%2F%2Ffoo%20bar%2F
|
||||
2
Task/URL-encoding/Java/url-encoding-1.java
Normal file
2
Task/URL-encoding/Java/url-encoding-1.java
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
1
Task/URL-encoding/Java/url-encoding-2.java
Normal file
1
Task/URL-encoding/Java/url-encoding-2.java
Normal file
|
|
@ -0,0 +1 @@
|
|||
URLEncoder.encode("http://foo bar/", StandardCharsets.UTF_8)
|
||||
25
Task/URL-encoding/Java/url-encoding-3.java
Normal file
25
Task/URL-encoding/Java/url-encoding-3.java
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
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');
|
||||
}
|
||||
2
Task/URL-encoding/JavaScript/url-encoding.js
Normal file
2
Task/URL-encoding/JavaScript/url-encoding.js
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
var normal = 'http://foo/bar/';
|
||||
var encoded = encodeURIComponent(normal);
|
||||
1
Task/URL-encoding/Jq/url-encoding-1.jq
Normal file
1
Task/URL-encoding/Jq/url-encoding-1.jq
Normal file
|
|
@ -0,0 +1 @@
|
|||
"á" | @uri
|
||||
9
Task/URL-encoding/Jq/url-encoding-2.jq
Normal file
9
Task/URL-encoding/Jq/url-encoding-2.jq
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
def url_encode:
|
||||
# The helper function checks whether the input corresponds to one of the characters: !'()*
|
||||
def recode: . as $c | [33,39,40,41,42] | index($c);
|
||||
def hex: if . < 10 then 48 + . else 55 + . end;
|
||||
@uri
|
||||
| explode
|
||||
# 37 ==> "%", 50 ==> "2"
|
||||
| map( if recode then (37, 50, ((. - 32) | hex)) else . end )
|
||||
| implode;
|
||||
1
Task/URL-encoding/Jq/url-encoding-3.jq
Normal file
1
Task/URL-encoding/Jq/url-encoding-3.jq
Normal file
|
|
@ -0,0 +1 @@
|
|||
"http://foo bar/" | @uri
|
||||
1
Task/URL-encoding/Jq/url-encoding-4.jq
Normal file
1
Task/URL-encoding/Jq/url-encoding-4.jq
Normal file
|
|
@ -0,0 +1 @@
|
|||
"http://foo bar/" | @uri == url_encode
|
||||
1
Task/URL-encoding/Jq/url-encoding-5.jq
Normal file
1
Task/URL-encoding/Jq/url-encoding-5.jq
Normal file
|
|
@ -0,0 +1 @@
|
|||
[range(0;1024) | [.] | implode | if @uri == . then . else empty end] | join(null)
|
||||
1
Task/URL-encoding/Jq/url-encoding-6.jq
Normal file
1
Task/URL-encoding/Jq/url-encoding-6.jq
Normal file
|
|
@ -0,0 +1 @@
|
|||
[range(0;1024) | [.] | implode | if url_encode == . then . else empty end] | join(null)
|
||||
7
Task/URL-encoding/Julia/url-encoding.julia
Normal file
7
Task/URL-encoding/Julia/url-encoding.julia
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
//version 1.0.1
|
||||
import HTTP.URIs: escapeuri
|
||||
|
||||
dcd = "http://foo bar/"
|
||||
enc = escapeuri(dcd)
|
||||
|
||||
println(dcd, " => ", enc)
|
||||
8
Task/URL-encoding/Kotlin/url-encoding.kotlin
Normal file
8
Task/URL-encoding/Kotlin/url-encoding.kotlin
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
// version 1.1.2
|
||||
|
||||
import java.net.URLEncoder
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val url = "http://foo bar/"
|
||||
println(URLEncoder.encode(url, "utf-8")) // note: encodes space to + not %20
|
||||
}
|
||||
9
Task/URL-encoding/Ksh/url-encoding.ksh
Normal file
9
Task/URL-encoding/Ksh/url-encoding.ksh
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
url_encode()
|
||||
{
|
||||
printf "%(url)q\n" "$*"
|
||||
}
|
||||
|
||||
|
||||
url_encode "http://foo bar/"
|
||||
url_encode "https://ru.wikipedia.org/wiki/Транспайлер"
|
||||
url_encode "google.com/search?q=`Abdu'l-Bahá"
|
||||
6
Task/URL-encoding/Langur/url-encoding.langur
Normal file
6
Task/URL-encoding/Langur/url-encoding.langur
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
val .urlEncode = f(.s) replace(
|
||||
.s, re/[^A-Za-z0-9]/,
|
||||
f(.s2) join "", map f $"%\.b:X02;", s2b .s2,
|
||||
)
|
||||
|
||||
writeln .urlEncode("https://some website.com/")
|
||||
1
Task/URL-encoding/Lasso/url-encoding.lasso
Normal file
1
Task/URL-encoding/Lasso/url-encoding.lasso
Normal file
|
|
@ -0,0 +1 @@
|
|||
bytes('http://foo bar/') -> encodeurl
|
||||
23
Task/URL-encoding/Liberty-BASIC/url-encoding.basic
Normal file
23
Task/URL-encoding/Liberty-BASIC/url-encoding.basic
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
dim lookUp$( 256)
|
||||
|
||||
for i =0 to 256
|
||||
lookUp$( i) ="%" +dechex$( i)
|
||||
next i
|
||||
|
||||
string$ ="http://foo bar/"
|
||||
|
||||
print "Supplied string '"; string$; "'"
|
||||
print "As URL '"; url$( string$); "'"
|
||||
|
||||
end
|
||||
|
||||
function url$( i$)
|
||||
for j =1 to len( i$)
|
||||
c$ =mid$( i$, j, 1)
|
||||
if instr( "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", c$) then
|
||||
url$ =url$ +c$
|
||||
else
|
||||
url$ =url$ +lookUp$( asc( c$))
|
||||
end if
|
||||
next j
|
||||
end function
|
||||
2
Task/URL-encoding/Lingo/url-encoding.lingo
Normal file
2
Task/URL-encoding/Lingo/url-encoding.lingo
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
put urlencode("http://foo bar/")
|
||||
-- "http%3a%2f%2ffoo+bar%2f"
|
||||
2
Task/URL-encoding/LiveCode/url-encoding.livecode
Normal file
2
Task/URL-encoding/LiveCode/url-encoding.livecode
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
urlEncode("http://foo bar/")
|
||||
-- http%3A%2F%2Ffoo+bar%2F
|
||||
11
Task/URL-encoding/Lua/url-encoding.lua
Normal file
11
Task/URL-encoding/Lua/url-encoding.lua
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
function encodeChar(chr)
|
||||
return string.format("%%%X",string.byte(chr))
|
||||
end
|
||||
|
||||
function encodeString(str)
|
||||
local output, t = string.gsub(str,"[^%w]",encodeChar)
|
||||
return output
|
||||
end
|
||||
|
||||
-- will print "http%3A%2F%2Ffoo%20bar%2F"
|
||||
print(encodeString("http://foo bar/"))
|
||||
69
Task/URL-encoding/M2000-Interpreter/url-encoding.m2000
Normal file
69
Task/URL-encoding/M2000-Interpreter/url-encoding.m2000
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
Module Checkit {
|
||||
Function decodeUrl$(a$) {
|
||||
DIM a$()
|
||||
a$()=Piece$(a$, "%")
|
||||
if len(a$())=1 then =str$(a$):exit
|
||||
k=each(a$(),2)
|
||||
acc$=str$(a$(0))
|
||||
While k {
|
||||
acc$+=str$(Chr$(Eval("0x"+left$(a$(k^),2)))+Mid$(a$(k^),3))
|
||||
}
|
||||
=string$(acc$ as utf8dec)
|
||||
}
|
||||
Group Parse$ {
|
||||
all$, c=1
|
||||
tc$=""
|
||||
Enum UrlType {None=0, RFC3986, HTML5}
|
||||
variation
|
||||
TypeData=("","-._~","-._*")
|
||||
Function Next {
|
||||
.tc$<=mid$(.all$,.c,1)
|
||||
.c++
|
||||
=.tc$<>""
|
||||
}
|
||||
Value {
|
||||
=.tc$
|
||||
}
|
||||
Function DecodeOne$ {
|
||||
if .tc$="" then exit
|
||||
if .tc$ ~"[A-Za-z0-9]" then =.tc$ : exit
|
||||
If .tc$=" " Then =if$(.variation=.HTML5->"+","%20") :exit
|
||||
if instr(.TypeData#val$(.variation),.tc$)>0 then =.tc$ :exit
|
||||
="%"+hex$(asc(.tc$), 1)
|
||||
}
|
||||
Function Decode$ {
|
||||
acc$=""
|
||||
.c<=1
|
||||
While .Next() {
|
||||
acc$+=.DecodeOne$()
|
||||
}
|
||||
=acc$
|
||||
}
|
||||
Set () {
|
||||
\\ using optional argument
|
||||
var=.None
|
||||
Read a$, ? var
|
||||
a$=chr$(string$(a$ as utf8enc))
|
||||
.variation<=var
|
||||
.all$<=a$
|
||||
.c<=1
|
||||
}
|
||||
}
|
||||
\\ MAIN
|
||||
Parse$()="http://foo bar/"
|
||||
Print Quote$(Parse.Decode$())
|
||||
Parse.variation=Parse.HTML5
|
||||
Print Quote$(Parse.Decode$())
|
||||
Parse.variation=Parse.RFC3986
|
||||
Print Quote$(Parse.Decode$())
|
||||
Parse$(Parse.RFC3986) ={mailto:"Irma User" <irma.user@mail.com>}
|
||||
Print Quote$(Parse.Decode$())
|
||||
Parse$(Parse.RFC3986) ={http://foo.bar.com/~user-name/_subdir/*~.html}
|
||||
m=each(Parse.UrlType)
|
||||
while m {
|
||||
Parse.variation=eval(m)
|
||||
Print Quote$(Parse.Decode$())
|
||||
Print decodeUrl$(Parse.Decode$())
|
||||
}
|
||||
}
|
||||
CheckIt
|
||||
10
Task/URL-encoding/MATLAB/url-encoding.m
Normal file
10
Task/URL-encoding/MATLAB/url-encoding.m
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
function u = urlencoding(s)
|
||||
u = '';
|
||||
for k = 1:length(s),
|
||||
if isalnum(s(k))
|
||||
u(end+1) = s(k);
|
||||
else
|
||||
u=[u,'%',dec2hex(s(k)+0)];
|
||||
end;
|
||||
end
|
||||
end
|
||||
1
Task/URL-encoding/Maple/url-encoding.maple
Normal file
1
Task/URL-encoding/Maple/url-encoding.maple
Normal file
|
|
@ -0,0 +1 @@
|
|||
URL:-Escape("http://foo bar/");
|
||||
7
Task/URL-encoding/Mathematica/url-encoding-1.math
Normal file
7
Task/URL-encoding/Mathematica/url-encoding-1.math
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
URLEncoding[url_] :=
|
||||
StringReplace[url,
|
||||
x : Except[
|
||||
Join[CharacterRange["0", "9"], CharacterRange["a", "z"],
|
||||
CharacterRange["A", "Z"]]] :>
|
||||
StringJoin[("%" ~~ #) & /@
|
||||
IntegerString[ToCharacterCode[x, "UTF8"], 16]]]
|
||||
1
Task/URL-encoding/Mathematica/url-encoding-2.math
Normal file
1
Task/URL-encoding/Mathematica/url-encoding-2.math
Normal file
|
|
@ -0,0 +1 @@
|
|||
URLEncoding["http://foo bar/"]
|
||||
64
Task/URL-encoding/NetRexx/url-encoding.netrexx
Normal file
64
Task/URL-encoding/NetRexx/url-encoding.netrexx
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
/* NetRexx */
|
||||
options replace format comments java crossref symbols nobinary
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
testcase()
|
||||
say
|
||||
say 'RFC3986'
|
||||
testcase('RFC3986')
|
||||
say
|
||||
say 'HTML5'
|
||||
testcase('HTML5')
|
||||
say
|
||||
return
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
method encode(url, varn) public static
|
||||
|
||||
variation = varn.upper
|
||||
opts = ''
|
||||
opts['RFC3986'] = '-._~'
|
||||
opts['HTML5'] = '-._*'
|
||||
|
||||
rp = ''
|
||||
loop while url.length > 0
|
||||
parse url tc +1 url
|
||||
select
|
||||
when tc.datatype('A') then do
|
||||
rp = rp || tc
|
||||
end
|
||||
when tc == ' ' then do
|
||||
if variation = 'HTML5' then
|
||||
rp = rp || '+'
|
||||
else
|
||||
rp = rp || '%' || tc.c2x
|
||||
end
|
||||
otherwise do
|
||||
if opts[variation].pos(tc) > 0 then do
|
||||
rp = rp || tc
|
||||
end
|
||||
else do
|
||||
rp = rp || '%' || tc.c2x
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return rp
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
method testcase(variation = '') public static
|
||||
|
||||
url = [ -
|
||||
'http://foo bar/' -
|
||||
, 'mailto:"Ivan Aim" <ivan.aim@email.com>' -
|
||||
, 'mailto:"Irma User" <irma.user@mail.com>' -
|
||||
, 'http://foo.bar.com/~user-name/_subdir/*~.html' -
|
||||
]
|
||||
|
||||
loop i_ = 0 to url.length - 1
|
||||
say url[i_]
|
||||
say encode(url[i_], variation)
|
||||
end i_
|
||||
|
||||
return
|
||||
6
Task/URL-encoding/NewLISP/url-encoding.l
Normal file
6
Task/URL-encoding/NewLISP/url-encoding.l
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
;; simple encoder
|
||||
;; (source http://www.newlisp.org/index.cgi?page=Code_Snippets)
|
||||
(define (url-encode str)
|
||||
(replace {([^a-zA-Z0-9])} str (format "%%%2X" (char $1)) 0))
|
||||
|
||||
(url-encode "http://foo bar/")
|
||||
3
Task/URL-encoding/Nim/url-encoding.nim
Normal file
3
Task/URL-encoding/Nim/url-encoding.nim
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
import cgi
|
||||
|
||||
echo encodeUrl("http://foo/bar/")
|
||||
6
Task/URL-encoding/OCaml/url-encoding.ocaml
Normal file
6
Task/URL-encoding/OCaml/url-encoding.ocaml
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
$ ocaml
|
||||
# #use "topfind";;
|
||||
# #require "netstring";;
|
||||
|
||||
# Netencoding.Url.encode "http://foo bar/" ;;
|
||||
- : string = "http%3A%2F%2Ffoo+bar%2F"
|
||||
12
Task/URL-encoding/Oberon-2/url-encoding.oberon
Normal file
12
Task/URL-encoding/Oberon-2/url-encoding.oberon
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
MODULE URLEncoding;
|
||||
IMPORT
|
||||
Out := NPCT:Console,
|
||||
ADT:StringBuffer,
|
||||
URI := URI:String;
|
||||
VAR
|
||||
encodedUrl: StringBuffer.StringBuffer;
|
||||
BEGIN
|
||||
encodedUrl := NEW(StringBuffer.StringBuffer,512);
|
||||
URI.AppendEscaped("http://foo bar/","",encodedUrl);
|
||||
Out.String(encodedUrl.ToString());Out.Ln
|
||||
END URLEncoding.
|
||||
10
Task/URL-encoding/Objeck/url-encoding.objeck
Normal file
10
Task/URL-encoding/Objeck/url-encoding.objeck
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
use FastCgi;
|
||||
|
||||
bundle Default {
|
||||
class UrlEncode {
|
||||
function : Main(args : String[]) ~ Nil {
|
||||
url := "http://foo bar/";
|
||||
UrlUtility->Encode(url)->PrintLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Task/URL-encoding/Objective-C/url-encoding-1.m
Normal file
3
Task/URL-encoding/Objective-C/url-encoding-1.m
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
NSString *normal = @"http://foo bar/";
|
||||
NSString *encoded = [normal stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
|
||||
NSLog(@"%@", encoded);
|
||||
3
Task/URL-encoding/Objective-C/url-encoding-2.m
Normal file
3
Task/URL-encoding/Objective-C/url-encoding-2.m
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
NSString *normal = @"http://foo bar/";
|
||||
NSString *encoded = [normal stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet alphanumericCharacterSet]];
|
||||
NSLog(@"%@", encoded);
|
||||
4
Task/URL-encoding/PHP/url-encoding.php
Normal file
4
Task/URL-encoding/PHP/url-encoding.php
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
<?php
|
||||
$s = 'http://foo/bar/';
|
||||
$s = rawurlencode($s);
|
||||
?>
|
||||
8
Task/URL-encoding/Perl/url-encoding-1.pl
Normal file
8
Task/URL-encoding/Perl/url-encoding-1.pl
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
sub urlencode {
|
||||
my $s = shift;
|
||||
$s =~ s/([^-A-Za-z0-9_.!~*'() ])/sprintf("%%%02X", ord($1))/eg;
|
||||
$s =~ tr/ /+/;
|
||||
return $s;
|
||||
}
|
||||
|
||||
print urlencode('http://foo bar/')."\n";
|
||||
4
Task/URL-encoding/Perl/url-encoding-2.pl
Normal file
4
Task/URL-encoding/Perl/url-encoding-2.pl
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
use URI::Escape;
|
||||
|
||||
my $s = 'http://foo/bar/';
|
||||
print uri_escape($s);
|
||||
6
Task/URL-encoding/Perl/url-encoding-3.pl
Normal file
6
Task/URL-encoding/Perl/url-encoding-3.pl
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
use 5.10.0;
|
||||
use CGI;
|
||||
|
||||
my $s = 'http://foo/bar/';
|
||||
say $s = CGI::escape($s);
|
||||
say $s = CGI::unescape($s);
|
||||
33
Task/URL-encoding/Phix/url-encoding.phix
Normal file
33
Task/URL-encoding/Phix/url-encoding.phix
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #000080;font-style:italic;">--
|
||||
-- demo\rosetta\encode_url.exw
|
||||
-- ===========================
|
||||
--</span>
|
||||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">nib</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">+</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">9</span><span style="color: #0000FF;">?</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">:</span><span style="color: #008000;">'A'</span><span style="color: #0000FF;">-</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">encode_url</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">string</span> <span style="color: #000000;">exclusions</span><span style="color: #0000FF;">=</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">spaceplus</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">string</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">ch</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">ch</span><span style="color: #0000FF;">=</span><span style="color: #008000;">' '</span> <span style="color: #008080;">and</span> <span style="color: #000000;">spaceplus</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">ch</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">'+'</span>
|
||||
<span style="color: #008080;">elsif</span> <span style="color: #008080;">not</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ch</span><span style="color: #0000FF;">,</span><span style="color: #000000;">exclusions</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">and</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">ch</span><span style="color: #0000FF;"><</span><span style="color: #008000;">'0'</span>
|
||||
<span style="color: #008080;">or</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">ch</span><span style="color: #0000FF;">></span><span style="color: #008000;">'9'</span> <span style="color: #008080;">and</span> <span style="color: #000000;">ch</span><span style="color: #0000FF;"><</span><span style="color: #008000;">'A'</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">or</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">ch</span><span style="color: #0000FF;">></span><span style="color: #008000;">'Z'</span> <span style="color: #008080;">and</span> <span style="color: #000000;">ch</span><span style="color: #0000FF;"><</span><span style="color: #008000;">'a'</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">or</span> <span style="color: #000000;">ch</span><span style="color: #0000FF;">></span><span style="color: #008000;">'z'</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">res</span> <span style="color: #0000FF;">&=</span> <span style="color: #008000;">'%'</span>
|
||||
<span style="color: #000000;">res</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">nib</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ch</span><span style="color: #0000FF;">/</span><span style="color: #000000;">#10</span><span style="color: #0000FF;">))</span>
|
||||
<span style="color: #000000;">ch</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">nib</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ch</span><span style="color: #0000FF;">,</span><span style="color: #000000;">#0F</span><span style="color: #0000FF;">))</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #000000;">res</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">ch</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">encode_url</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"http://foo bar/"</span><span style="color: #0000FF;">)})</span>
|
||||
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
|
||||
<!--
|
||||
8
Task/URL-encoding/PicoLisp/url-encoding.l
Normal file
8
Task/URL-encoding/PicoLisp/url-encoding.l
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
(de urlEncodeTooMuch (Str)
|
||||
(pack
|
||||
(mapcar
|
||||
'((C)
|
||||
(if (or (>= "9" C "0") (>= "Z" (uppc C) "A"))
|
||||
C
|
||||
(list '% (hex (char C))) ) )
|
||||
(chop Str) ) ) )
|
||||
1
Task/URL-encoding/Pike/url-encoding.pike
Normal file
1
Task/URL-encoding/Pike/url-encoding.pike
Normal file
|
|
@ -0,0 +1 @@
|
|||
Protocols.HTTP.uri_encode( "http://foo bar/" );
|
||||
3
Task/URL-encoding/PowerShell/url-encoding.psh
Normal file
3
Task/URL-encoding/PowerShell/url-encoding.psh
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
[uri]::EscapeDataString('http://foo bar/')
|
||||
|
||||
http%3A%2F%2Ffoo%20bar%2F
|
||||
1
Task/URL-encoding/PureBasic/url-encoding.basic
Normal file
1
Task/URL-encoding/PureBasic/url-encoding.basic
Normal file
|
|
@ -0,0 +1 @@
|
|||
URL$ = URLEncoder("http://foo bar/")
|
||||
3
Task/URL-encoding/Python/url-encoding.py
Normal file
3
Task/URL-encoding/Python/url-encoding.py
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
import urllib
|
||||
s = 'http://foo/bar/'
|
||||
s = urllib.quote(s)
|
||||
1
Task/URL-encoding/R/url-encoding-1.r
Normal file
1
Task/URL-encoding/R/url-encoding-1.r
Normal file
|
|
@ -0,0 +1 @@
|
|||
URLencode("http://foo bar/")
|
||||
2
Task/URL-encoding/R/url-encoding-2.r
Normal file
2
Task/URL-encoding/R/url-encoding-2.r
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
library(RCurl)
|
||||
curlEscape("http://foo bar/")
|
||||
3
Task/URL-encoding/REALbasic/url-encoding-1.basic
Normal file
3
Task/URL-encoding/REALbasic/url-encoding-1.basic
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
Dim URL As String = "http://foo bar/"
|
||||
URL = EncodeURLComponent(URL)
|
||||
Print(URL)
|
||||
21
Task/URL-encoding/REALbasic/url-encoding-2.basic
Normal file
21
Task/URL-encoding/REALbasic/url-encoding-2.basic
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
Function URLEncode(Data As String, ParamArray Exceptions() As String) As String
|
||||
Dim buf As String
|
||||
For i As Integer = 1 To Data.Len
|
||||
Dim char As String = Data.Mid(i, 1)
|
||||
Select Case Asc(char)
|
||||
Case 48 To 57, 65 To 90, 97 To 122, 45, 46, 95
|
||||
buf = buf + char
|
||||
Else
|
||||
If Exceptions.IndexOf(char) > -1 Then
|
||||
buf = buf + char
|
||||
Else
|
||||
buf = buf + "%" + Left(Hex(Asc(char)) + "00", 2)
|
||||
End If
|
||||
End Select
|
||||
Next
|
||||
Return buf
|
||||
End Function
|
||||
|
||||
'usage
|
||||
Dim s As String = URLEncode("http://foo bar/") ' no exceptions
|
||||
Dim t As String = URLEncode("http://foo bar/", "!", "?", ",") ' with exceptions
|
||||
72
Task/URL-encoding/REXX/url-encoding-1.rexx
Normal file
72
Task/URL-encoding/REXX/url-encoding-1.rexx
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
/* Rexx */
|
||||
do
|
||||
call testcase
|
||||
say
|
||||
say RFC3986
|
||||
call testcase RFC3986
|
||||
say
|
||||
say HTML5
|
||||
call testcase HTML5
|
||||
say
|
||||
return
|
||||
end
|
||||
exit
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
encode:
|
||||
procedure
|
||||
do
|
||||
parse arg url, varn .
|
||||
parse upper var varn variation
|
||||
drop RFC3986 HTML5
|
||||
opts. = ''
|
||||
opts.RFC3986 = '-._~'
|
||||
opts.HTML5 = '-._*'
|
||||
|
||||
rp = ''
|
||||
do while length(url) > 0
|
||||
parse var url tc +1 url
|
||||
select
|
||||
when datatype(tc, 'A') then do
|
||||
rp = rp || tc
|
||||
end
|
||||
when tc == ' ' then do
|
||||
if variation = HTML5 then
|
||||
rp = rp || '+'
|
||||
else
|
||||
rp = rp || '%' || c2x(tc)
|
||||
end
|
||||
otherwise do
|
||||
if pos(tc, opts.variation) > 0 then do
|
||||
rp = rp || tc
|
||||
end
|
||||
else do
|
||||
rp = rp || '%' || c2x(tc)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return rp
|
||||
end
|
||||
exit
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
testcase:
|
||||
procedure
|
||||
do
|
||||
parse arg variation
|
||||
X = 0
|
||||
url. = ''
|
||||
X = X + 1; url.0 = X; url.X = 'http://foo bar/'
|
||||
X = X + 1; url.0 = X; url.X = 'mailto:"Ivan Aim" <ivan.aim@email.com>'
|
||||
X = X + 1; url.0 = X; url.X = 'mailto:"Irma User" <irma.user@mail.com>'
|
||||
X = X + 1; url.0 = X; url.X = 'http://foo.bar.com/~user-name/_subdir/*~.html'
|
||||
|
||||
do i_ = 1 to url.0
|
||||
say url.i_
|
||||
say encode(url.i_, variation)
|
||||
end i_
|
||||
|
||||
return
|
||||
end
|
||||
28
Task/URL-encoding/REXX/url-encoding-2.rexx
Normal file
28
Task/URL-encoding/REXX/url-encoding-2.rexx
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
/*REXX program encodes a URL text, blanks ──► +, preserves -._* and -._~ */
|
||||
url.=; url.1= 'http://foo bar/'
|
||||
url.2= 'mailto:"Ivan Aim" <ivan.aim@email.com>'
|
||||
url.3= 'mailto:"Irma User" <irma.user@mail.com>'
|
||||
url.4= 'http://foo.bar.com/~user-name/_subdir/*~.html'
|
||||
do j=1 while url.j\==''; say
|
||||
say ' original: ' url.j
|
||||
say ' encoded: ' URLencode(url.j)
|
||||
end /*j*/
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
URLencode: procedure; parse arg $,,z; t1= '-._~' /*get args, null Z.*/
|
||||
skip=0; t2= '-._*'
|
||||
do k=1 for length($); _=substr($, k, 1) /*get a character. */
|
||||
if skip\==0 then do; skip=skip-1 /*skip t1 or t2 ? */
|
||||
iterate /*skip a character.*/
|
||||
end
|
||||
select
|
||||
when datatype(_, 'A') then z=z || _ /*is alphanumeric ?*/
|
||||
when _==' ' then z=z'+' /*is it a blank ?*/
|
||||
when substr($, k, 4)==t1 |, /*is it t1 or t2 ?*/
|
||||
substr($, k, 4)==t2 then do; skip=3 /*skip 3 characters*/
|
||||
z=z || substr($, k, 4)
|
||||
end
|
||||
otherwise z=z'%'c2x(_) /*special character*/
|
||||
end /*select*/
|
||||
end /*k*/
|
||||
return z
|
||||
3
Task/URL-encoding/Racket/url-encoding.rkt
Normal file
3
Task/URL-encoding/Racket/url-encoding.rkt
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
#lang racket
|
||||
(require net/uri-codec)
|
||||
(uri-encode "http://foo bar/")
|
||||
3
Task/URL-encoding/Raku/url-encoding.raku
Normal file
3
Task/URL-encoding/Raku/url-encoding.raku
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
my $url = 'http://foo bar/';
|
||||
|
||||
say $url.subst(/<-alnum>/, *.ord.fmt("%%%02X"), :g);
|
||||
3
Task/URL-encoding/Ruby/url-encoding-1.rb
Normal file
3
Task/URL-encoding/Ruby/url-encoding-1.rb
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
require 'cgi'
|
||||
puts CGI.escape("http://foo bar/").gsub("+", "%20")
|
||||
# => "http%3A%2F%2Ffoo%20bar%2F"
|
||||
3
Task/URL-encoding/Ruby/url-encoding-2.rb
Normal file
3
Task/URL-encoding/Ruby/url-encoding-2.rb
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
require 'uri'
|
||||
puts URI.encode_www_form_component("http://foo bar/").gsub("+", "%20")
|
||||
# => "http%3A%2F%2Ffoo%20bar%2F"
|
||||
9
Task/URL-encoding/Run-BASIC/url-encoding.basic
Normal file
9
Task/URL-encoding/Run-BASIC/url-encoding.basic
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
urlIn$ = "http://foo bar/"
|
||||
|
||||
for i = 1 to len(urlIn$)
|
||||
a$ = mid$(urlIn$,i,1)
|
||||
if (a$ >= "0" and a$ <= "9") _
|
||||
or (a$ >= "A" and a$ <= "Z") _
|
||||
or (a$ >= "a" and a$ <= "z") then url$ = url$ + a$ else url$ = url$ + "%"+dechex$(asc(a$))
|
||||
next i
|
||||
print urlIn$;" -> ";url$
|
||||
18
Task/URL-encoding/Rust/url-encoding.rust
Normal file
18
Task/URL-encoding/Rust/url-encoding.rust
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
const INPUT: &str = "http://foo bar/";
|
||||
const MAX_CHAR_VAL: u32 = std::char::MAX as u32;
|
||||
|
||||
fn main() {
|
||||
let mut buff = [0; 4];
|
||||
println!("{}", INPUT.chars()
|
||||
.map(|ch| {
|
||||
match ch as u32 {
|
||||
0 ..= 47 | 58 ..= 64 | 91 ..= 96 | 123 ..= MAX_CHAR_VAL => {
|
||||
ch.encode_utf8(&mut buff);
|
||||
buff[0..ch.len_utf8()].iter().map(|&byte| format!("%{:X}", byte)).collect::<String>()
|
||||
},
|
||||
_ => ch.to_string(),
|
||||
}
|
||||
})
|
||||
.collect::<String>()
|
||||
);
|
||||
}
|
||||
16
Task/URL-encoding/Scala/url-encoding.scala
Normal file
16
Task/URL-encoding/Scala/url-encoding.scala
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
import java.net.{URLDecoder, URLEncoder}
|
||||
import scala.compat.Platform.currentTime
|
||||
|
||||
object UrlCoded extends App {
|
||||
val original = """http://foo bar/"""
|
||||
val encoded: String = URLEncoder.encode(original, "UTF-8")
|
||||
|
||||
assert(encoded == "http%3A%2F%2Ffoo+bar%2F", s"Original: $original not properly encoded: $encoded")
|
||||
|
||||
val percentEncoding = encoded.replace("+", "%20")
|
||||
assert(percentEncoding == "http%3A%2F%2Ffoo%20bar%2F", s"Original: $original not properly percent-encoded: $percentEncoding")
|
||||
|
||||
assert(URLDecoder.decode(encoded, "UTF-8") == URLDecoder.decode(percentEncoding, "UTF-8"))
|
||||
|
||||
println(s"Successfully completed without errors. [total ${currentTime - executionStart} ms]")
|
||||
}
|
||||
8
Task/URL-encoding/Seed7/url-encoding.seed7
Normal file
8
Task/URL-encoding/Seed7/url-encoding.seed7
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
$ include "seed7_05.s7i";
|
||||
include "encoding.s7i";
|
||||
|
||||
const proc: main is func
|
||||
begin
|
||||
writeln(toPercentEncoded("http://foo bar/"));
|
||||
writeln(toUrlEncoded("http://foo bar/"));
|
||||
end func;
|
||||
7
Task/URL-encoding/Sidef/url-encoding.sidef
Normal file
7
Task/URL-encoding/Sidef/url-encoding.sidef
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
func urlencode(str) {
|
||||
str.gsub!(%r"([^-A-Za-z0-9_.!~*'() ])", {|a| "%%%02X" % a.ord});
|
||||
str.gsub!(' ', '+');
|
||||
return str;
|
||||
}
|
||||
|
||||
say urlencode('http://foo bar/');
|
||||
10
Task/URL-encoding/TUSCRIPT/url-encoding.tu
Normal file
10
Task/URL-encoding/TUSCRIPT/url-encoding.tu
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
$$ MODE TUSCRIPT
|
||||
text="http://foo bar/"
|
||||
BUILD S_TABLE spez_char="::>/:</::<%:"
|
||||
spez_char=STRINGS (text,spez_char)
|
||||
LOOP/CLEAR c=spez_char
|
||||
c=ENCODE(c,hex),c=concat("%",c),spez_char=APPEND(spez_char,c)
|
||||
ENDLOOP
|
||||
url_encoded=SUBSTITUTE(text,spez_char,0,0,spez_char)
|
||||
print "text: ", text
|
||||
PRINT "encoded: ", url_encoded
|
||||
8
Task/URL-encoding/Tcl/url-encoding-1.tcl
Normal file
8
Task/URL-encoding/Tcl/url-encoding-1.tcl
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
# Encode all except "unreserved" characters; use UTF-8 for extended chars.
|
||||
# See http://tools.ietf.org/html/rfc3986 §2.4 and §2.5
|
||||
proc urlEncode {str} {
|
||||
set uStr [encoding convertto utf-8 $str]
|
||||
set chRE {[^-A-Za-z0-9._~\n]}; # Newline is special case!
|
||||
set replacement {%[format "%02X" [scan "\\\0" "%c"]]}
|
||||
return [string map {"\n" "%0A"} [subst [regsub -all $chRE $uStr $replacement]]]
|
||||
}
|
||||
1
Task/URL-encoding/Tcl/url-encoding-2.tcl
Normal file
1
Task/URL-encoding/Tcl/url-encoding-2.tcl
Normal file
|
|
@ -0,0 +1 @@
|
|||
puts [urlEncode "http://foo bar/"]
|
||||
47
Task/URL-encoding/UNIX-Shell/url-encoding.sh
Normal file
47
Task/URL-encoding/UNIX-Shell/url-encoding.sh
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
function urlencode
|
||||
{
|
||||
typeset decoded=$1 encoded= rest= c=
|
||||
typeset rest2= bug='rest2=${rest}'
|
||||
|
||||
if [[ -z ${BASH_VERSION} ]]; then
|
||||
# bug /usr/bin/sh HP-UX 11.00
|
||||
typeset _decoded='xyz%26xyz'
|
||||
rest="${_decoded#?}"
|
||||
c="${_decoded%%${rest}}"
|
||||
if (( ${#c} != 1 )); then
|
||||
typeset qm='????????????????????????????????????????????????????????????????????????'
|
||||
typeset bug='(( ${#rest} > 0 )) && typeset -L${#rest} rest2="${qm}" || rest2=${rest}'
|
||||
fi
|
||||
fi
|
||||
|
||||
rest="${decoded#?}"
|
||||
eval ${bug}
|
||||
c="${decoded%%${rest2}}"
|
||||
decoded="${rest}"
|
||||
|
||||
while [[ -n ${c} ]]; do
|
||||
case ${c} in
|
||||
[-a-zA-z0-9.])
|
||||
;;
|
||||
' ')
|
||||
c='+'
|
||||
;;
|
||||
*)
|
||||
c=$(printf "%%%02X" "'$c")
|
||||
;;
|
||||
esac
|
||||
|
||||
encoded="${encoded}${c}"
|
||||
|
||||
rest="${decoded#?}"
|
||||
eval ${bug}
|
||||
c="${decoded%%${rest2}}"
|
||||
decoded="${rest}"
|
||||
done
|
||||
|
||||
if [[ -n ${BASH_VERSION:-} ]]; then
|
||||
\echo -E "${encoded}"
|
||||
else
|
||||
print -r -- "${encoded}"
|
||||
fi
|
||||
}
|
||||
4
Task/URL-encoding/V-(Vlang)/url-encoding.v
Normal file
4
Task/URL-encoding/V-(Vlang)/url-encoding.v
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
import net.urllib
|
||||
fn main() {
|
||||
println(urllib.query_escape("http://foo bar/"))
|
||||
}
|
||||
16
Task/URL-encoding/VBScript/url-encoding.vb
Normal file
16
Task/URL-encoding/VBScript/url-encoding.vb
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
Function UrlEncode(url)
|
||||
For i = 1 To Len(url)
|
||||
n = Asc(Mid(url,i,1))
|
||||
If (n >= 48 And n <= 57) Or (n >= 65 And n <= 90) _
|
||||
Or (n >= 97 And n <= 122) Then
|
||||
UrlEncode = UrlEncode & Mid(url,i,1)
|
||||
Else
|
||||
ChrHex = Hex(Asc(Mid(url,i,1)))
|
||||
For j = 0 to (Len(ChrHex) / 2) - 1
|
||||
UrlEncode = UrlEncode & "%" & Mid(ChrHex,(2*j) + 1,2)
|
||||
Next
|
||||
End If
|
||||
Next
|
||||
End Function
|
||||
|
||||
WScript.Echo UrlEncode("http://foo baré/")
|
||||
21
Task/URL-encoding/Wren/url-encoding.wren
Normal file
21
Task/URL-encoding/Wren/url-encoding.wren
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
import "/fmt" for Fmt
|
||||
|
||||
var urlEncode = Fn.new { |url|
|
||||
var res = ""
|
||||
for (b in url.bytes) {
|
||||
if ((b >= 48 && b <= 57) || (b >= 65 && b <= 90) || (b >= 97 && b <= 122)) {
|
||||
res = res + String.fromByte(b)
|
||||
} else {
|
||||
res = res + Fmt.swrite("\%$2X", b)
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
var urls = [
|
||||
"http://foo bar/",
|
||||
"mailto:\"Ivan Aim\" <ivan.aim@email.com>",
|
||||
"mailto:\"Irma User\" <irma.user@mail.com>",
|
||||
"http://foo.bar.com/~user-name/_subdir/*~.html"
|
||||
]
|
||||
for (url in urls) System.print(urlEncode.call(url))
|
||||
21
Task/URL-encoding/XPL0/url-encoding.xpl0
Normal file
21
Task/URL-encoding/XPL0/url-encoding.xpl0
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
code Text=12;
|
||||
string 0; \use zero-terminated strings
|
||||
|
||||
func Encode(S0); \Encode URL string and return its address
|
||||
char S0;
|
||||
char HD, S1(80); \BEWARE: very temporary string space returned
|
||||
int C, I, J;
|
||||
[HD:= "0123456789ABCDEF"; \hex digits
|
||||
I:= 0; J:= 0;
|
||||
repeat C:= S0(I); I:= I+1;
|
||||
if C>=^0 & C<=^9 ! C>=^A & C<=^Z ! C>=^a & C<=^z ! C=0
|
||||
then [S1(J):= C; J:= J+1] \simply pass char to S1
|
||||
else [S1(J):= ^%; J:= J+1; \encode char into S1
|
||||
S1(J):= HD(C>>4); J:= J+1;
|
||||
S1(J):= HD(C&$0F); J:= J+1;
|
||||
];
|
||||
until C=0;
|
||||
return S1;
|
||||
];
|
||||
|
||||
Text(0, Encode("http://foo bar/"))
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue