Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
4
Task/URL-decoding/00-META.yaml
Normal file
4
Task/URL-decoding/00-META.yaml
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
---
|
||||
category:
|
||||
- String manipulation
|
||||
from: http://rosettacode.org/wiki/URL_decoding
|
||||
12
Task/URL-decoding/00-TASK.txt
Normal file
12
Task/URL-decoding/00-TASK.txt
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
This task (the reverse of [[URL encoding]] and distinct from [[URL parser]]) is to provide a function
|
||||
or mechanism to convert an URL-encoded string into its original unencoded form.
|
||||
|
||||
|
||||
;Test cases:
|
||||
* The encoded string "<code><nowiki>http%3A%2F%2Ffoo%20bar%2F</nowiki></code>" should be reverted to the unencoded form "<code><nowiki>http://foo bar/</nowiki></code>".
|
||||
|
||||
* 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>
|
||||
|
||||
18
Task/URL-decoding/11l/url-decoding.11l
Normal file
18
Task/URL-decoding/11l/url-decoding.11l
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
F url_decode(s)
|
||||
V r = ‘’
|
||||
V i = 0
|
||||
L i < s.len
|
||||
I s[i] == ‘%’
|
||||
[Byte] b
|
||||
L i < s.len & s[i] == ‘%’
|
||||
i++
|
||||
b.append(Int(s[i.+2], radix' 16))
|
||||
i += 2
|
||||
r ‘’= b.decode(‘utf-8’)
|
||||
E
|
||||
r ‘’= s[i]
|
||||
i++
|
||||
R r
|
||||
|
||||
print(url_decode(‘http%3A%2F%2Ffoo%20bar%2F’))
|
||||
print(url_decode(‘https://ru.wikipedia.org/wiki/%D0%A2%D1%80%D0%B0%D0%BD%D1%81%D0%BF%D0%B0%D0%B9%D0%BB%D0%B5%D1%80’))
|
||||
12
Task/URL-decoding/ABAP/url-decoding.abap
Normal file
12
Task/URL-decoding/ABAP/url-decoding.abap
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
REPORT Z_DECODE_URL.
|
||||
|
||||
DATA: lv_encoded_url TYPE string VALUE 'http%3A%2F%2Ffoo%20bar%2F',
|
||||
lv_decoded_url TYPE string.
|
||||
|
||||
CALL METHOD CL_HTTP_UTILITY=>UNESCAPE_URL
|
||||
EXPORTING
|
||||
ESCAPED = lv_encoded_url
|
||||
RECEIVING
|
||||
UNESCAPED = lv_decoded_url.
|
||||
|
||||
WRITE: 'Encoded URL: ', lv_encoded_url, /, 'Decoded URL: ', lv_decoded_url.
|
||||
36
Task/URL-decoding/ALGOL-68/url-decoding.alg
Normal file
36
Task/URL-decoding/ALGOL-68/url-decoding.alg
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
# returns c decoded as a hex digit #
|
||||
PROC hex value = ( CHAR c )INT: IF c >= "0" AND c <= "9" THEN ABS c - ABS "0"
|
||||
ELIF c >= "A" AND c <= "F" THEN 10 + ( ABS c - ABS "A" )
|
||||
ELSE 10 + ( ABS c - ABS "a" )
|
||||
FI;
|
||||
|
||||
# returns the URL encoded string decoded - minimal error handling #
|
||||
PROC url decode = ( STRING encoded )STRING:
|
||||
BEGIN
|
||||
[ LWB encoded : UPB encoded ]CHAR result;
|
||||
INT result pos := LWB encoded;
|
||||
INT pos := LWB encoded;
|
||||
INT max pos := UPB encoded;
|
||||
INT max encoded := max pos - 3;
|
||||
WHILE pos <= UPB encoded
|
||||
DO
|
||||
IF encoded[ pos ] /= "%" AND pos <= max encoded
|
||||
THEN
|
||||
# not a coded character #
|
||||
result[ result pos ] := encoded[ pos ];
|
||||
pos +:= 1
|
||||
ELSE
|
||||
# have an encoded character #
|
||||
result[ result pos ] := REPR ( ( 16 * hex value( encoded[ pos + 1 ] ) )
|
||||
+ hex value( encoded[ pos + 2 ] )
|
||||
);
|
||||
pos +:= 3
|
||||
FI;
|
||||
result pos +:= 1
|
||||
OD;
|
||||
result[ LWB result : result pos - 1 ]
|
||||
END # url decode # ;
|
||||
|
||||
# test the url decode procedure #
|
||||
print( ( url decode( "http%3A%2F%2Ffoo%20bar%2F" ), newline ) );
|
||||
print( ( url decode( "google.com/search?q=%60Abdu%27l-Bah%C3%A1" ), newline ) )
|
||||
22
Task/URL-decoding/AWK/url-decoding-1.awk
Normal file
22
Task/URL-decoding/AWK/url-decoding-1.awk
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
# syntax:
|
||||
awk '
|
||||
BEGIN {
|
||||
str = "http%3A%2F%2Ffoo%20bar%2F" # "http://foo bar/"
|
||||
printf("%s\n",str)
|
||||
len=length(str)
|
||||
for (i=1;i<=len;i++) {
|
||||
if ( substr(str,i,1) == "%") {
|
||||
L = substr(str,1,i-1) # chars to left of "%"
|
||||
M = substr(str,i+1,2) # 2 chars to right of "%"
|
||||
R = substr(str,i+3) # chars to right of "%xx"
|
||||
str = sprintf("%s%c%s",L,hex2dec(M),R)
|
||||
}
|
||||
}
|
||||
printf("%s\n",str)
|
||||
exit(0)
|
||||
}
|
||||
function hex2dec(s, num) {
|
||||
num = index("0123456789ABCDEF",toupper(substr(s,length(s)))) - 1
|
||||
sub(/.$/,"",s)
|
||||
return num + (length(s) ? 16*hex2dec(s) : 0)
|
||||
} '
|
||||
4
Task/URL-decoding/AWK/url-decoding-2.awk
Normal file
4
Task/URL-decoding/AWK/url-decoding-2.awk
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
LC_ALL=C
|
||||
echo "http%3A%2F%2Ffoo%20bar%2F" | gawk -vRS='%[[:xdigit:]]{2}' '
|
||||
RT {RT = sprintf("%c",strtonum("0x" substr(RT, 2)))}
|
||||
{gsub(/+/," ");printf "%s", $0 RT}'
|
||||
65
Task/URL-decoding/Action-/url-decoding.action
Normal file
65
Task/URL-decoding/Action-/url-decoding.action
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
PROC Append(CHAR ARRAY s CHAR c)
|
||||
s(0)==+1
|
||||
s(s(0))=c
|
||||
RETURN
|
||||
|
||||
CHAR FUNC GetCharFromHex(CHAR c1,c2)
|
||||
CHAR ARRAY hex=['0 '1 '2 '3 '4 '5 '6 '7 '8 '9 'A 'B 'C 'D 'E 'F]
|
||||
BYTE i,res
|
||||
|
||||
res=0
|
||||
FOR i=0 TO 15
|
||||
DO
|
||||
IF c1=hex(i) THEN res==+i LSH 4 FI
|
||||
IF c2=hex(i) THEN res==+i FI
|
||||
OD
|
||||
RETURN (res)
|
||||
|
||||
PROC Decode(CHAR ARRAY in,out)
|
||||
BYTE i
|
||||
CHAR c
|
||||
|
||||
out(0)=0
|
||||
i=1
|
||||
WHILE i<=in(0)
|
||||
DO
|
||||
c=in(i)
|
||||
i==+1
|
||||
IF c='+ THEN
|
||||
Append(out,' )
|
||||
ELSEIF c='% THEN
|
||||
c=GetCharFromHex(in(i),in(i+1))
|
||||
i==+2
|
||||
Append(out,c)
|
||||
ELSE
|
||||
Append(out,c)
|
||||
FI
|
||||
OD
|
||||
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)
|
||||
|
||||
Decode(in,out)
|
||||
PrintInv("decoded")
|
||||
PrintF(" %S%E%E",out)
|
||||
RETURN
|
||||
|
||||
PROC Main()
|
||||
Test("http%3A%2F%2Ffoo%20bar%2F")
|
||||
Test("http%3A%2F%2Ffoo+bar%2F*_-.html")
|
||||
RETURN
|
||||
7
Task/URL-decoding/Ada/url-decoding-1.ada
Normal file
7
Task/URL-decoding/Ada/url-decoding-1.ada
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
with AWS.URL;
|
||||
with Ada.Text_IO; use Ada.Text_IO;
|
||||
procedure Decode is
|
||||
Encoded : constant String := "http%3A%2F%2Ffoo%20bar%2F";
|
||||
begin
|
||||
Put_Line (AWS.URL.Decode (Encoded));
|
||||
end Decode;
|
||||
3
Task/URL-decoding/Ada/url-decoding-2.ada
Normal file
3
Task/URL-decoding/Ada/url-decoding-2.ada
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
package URL is
|
||||
function Decode (URL : in String) return String;
|
||||
end URL;
|
||||
28
Task/URL-decoding/Ada/url-decoding-3.ada
Normal file
28
Task/URL-decoding/Ada/url-decoding-3.ada
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
package body URL is
|
||||
function Decode (URL : in String) return String is
|
||||
Buffer : String (1 .. URL'Length);
|
||||
Filled : Natural := 0;
|
||||
Position : Positive := URL'First;
|
||||
begin
|
||||
while Position in URL'Range loop
|
||||
Filled := Filled + 1;
|
||||
|
||||
case URL (Position) is
|
||||
when '+' =>
|
||||
Buffer (Filled) := ' ';
|
||||
Position := Position + 1;
|
||||
when '%' =>
|
||||
Buffer (Filled) :=
|
||||
Character'Val
|
||||
(Natural'Value
|
||||
("16#" & URL (Position + 1 .. Position + 2) & "#"));
|
||||
Position := Position + 3;
|
||||
when others =>
|
||||
Buffer (Filled) := URL (Position);
|
||||
Position := Position + 1;
|
||||
end case;
|
||||
end loop;
|
||||
|
||||
return Buffer (1 .. Filled);
|
||||
end Decode;
|
||||
end URL;
|
||||
16
Task/URL-decoding/Ada/url-decoding-4.ada
Normal file
16
Task/URL-decoding/Ada/url-decoding-4.ada
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
with Ada.Command_Line,
|
||||
Ada.Text_IO;
|
||||
|
||||
with URL;
|
||||
|
||||
procedure Test_URL_Decode is
|
||||
use Ada.Command_Line, Ada.Text_IO;
|
||||
begin
|
||||
if Argument_Count = 0 then
|
||||
Put_Line (URL.Decode ("http%3A%2F%2Ffoo%20bar%2F"));
|
||||
else
|
||||
for I in 1 .. Argument_Count loop
|
||||
Put_Line (URL.Decode (Argument (I)));
|
||||
end loop;
|
||||
end if;
|
||||
end Test_URL_Decode;
|
||||
2
Task/URL-decoding/Apex/url-decoding.apex
Normal file
2
Task/URL-decoding/Apex/url-decoding.apex
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
EncodingUtil.urlDecode('http%3A%2F%2Ffoo%20bar%2F', 'UTF-8');
|
||||
EncodingUtil.urlDecode('google.com/search?q=%60Abdu%27l-Bah%C3%A1', 'UTF-8');
|
||||
1
Task/URL-decoding/AppleScript/url-decoding.applescript
Normal file
1
Task/URL-decoding/AppleScript/url-decoding.applescript
Normal file
|
|
@ -0,0 +1 @@
|
|||
AST URL decode "google.com/search?q=%60Abdu%27l-Bah%C3%A1"
|
||||
2
Task/URL-decoding/Arturo/url-decoding.arturo
Normal file
2
Task/URL-decoding/Arturo/url-decoding.arturo
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
print decode.url "http%3A%2F%2Ffoo%20bar%2F"
|
||||
print decode.url "google.com/search?q=%60Abdu%27l-Bah%C3%A1"
|
||||
23
Task/URL-decoding/AutoHotkey/url-decoding.ahk
Normal file
23
Task/URL-decoding/AutoHotkey/url-decoding.ahk
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
UriDecode(Uri) {
|
||||
LoopOffset := 0
|
||||
VarLength := 0
|
||||
VarSetCapacity(Var, StrPut(Uri, "UTF-8"), 0)
|
||||
Loop Parse, Uri
|
||||
{
|
||||
If (A_Index < LoopOffset) {
|
||||
Continue
|
||||
}
|
||||
If (A_LoopField = Chr(37)) {
|
||||
Number := "0x" . SubStr(Uri, A_Index + 1, 2)
|
||||
LoopOffset := A_Index + 3
|
||||
}
|
||||
Else {
|
||||
Number := Ord(A_LoopField)
|
||||
}
|
||||
NumPut(Number, Var, VarLength++, "UChar")
|
||||
}
|
||||
Return StrGet(&Var, VarLength, "UTF-8")
|
||||
}
|
||||
MsgBox % UriDecode("http%3A%2F%2Ffoo%20bar%2F")
|
||||
MsgBox % UriDecode("google.com/search?q=%60Abdu%27l-Bah%C3%A1")
|
||||
MsgBox % UriDecode("%25%32%35")
|
||||
22
Task/URL-decoding/BBC-BASIC/url-decoding.basic
Normal file
22
Task/URL-decoding/BBC-BASIC/url-decoding.basic
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
PRINT FNurldecode("http%3A%2F%2Ffoo%20bar%2F")
|
||||
END
|
||||
|
||||
DEF FNurldecode(url$)
|
||||
LOCAL i%
|
||||
REPEAT
|
||||
i% = INSTR(url$, "%", i%+1)
|
||||
IF i% THEN
|
||||
url$ = LEFT$(url$,i%-1) + \
|
||||
\ CHR$EVAL("&"+FNupper(MID$(url$,i%+1,2))) + \
|
||||
\ MID$(url$,i%+3)
|
||||
ENDIF
|
||||
UNTIL i% = 0
|
||||
= url$
|
||||
|
||||
DEF FNupper(A$)
|
||||
LOCAL A%,C%
|
||||
FOR A% = 1 TO LEN(A$)
|
||||
C% = ASCMID$(A$,A%)
|
||||
IF C% >= 97 IF C% <= 122 MID$(A$,A%,1) = CHR$(C%-32)
|
||||
NEXT
|
||||
= A$
|
||||
14
Task/URL-decoding/BaCon/url-decoding.bacon
Normal file
14
Task/URL-decoding/BaCon/url-decoding.bacon
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
FUNCTION Url_Decode$(url$)
|
||||
|
||||
LOCAL result$
|
||||
|
||||
SPLIT url$ BY "%" TO item$ SIZE total
|
||||
FOR x = 1 TO total-1
|
||||
result$ = result$ & CHR$(DEC(LEFT$(item$[x], 2))) & MID$(item$[x], 3)
|
||||
NEXT
|
||||
RETURN item$[0] & result$
|
||||
|
||||
END FUNCTION
|
||||
|
||||
PRINT Url_Decode$("http%3A%2F%2Ffoo%20bar%2F")
|
||||
PRINT Url_Decode$("google.com/search?q=%60Abdu%27l-Bah%C3%A1")
|
||||
11
Task/URL-decoding/Bracmat/url-decoding.bracmat
Normal file
11
Task/URL-decoding/Bracmat/url-decoding.bracmat
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
( ( decode
|
||||
= decoded hexcode notencoded
|
||||
. :?decoded
|
||||
& whl
|
||||
' ( @(!arg:?notencoded "%" (% %:?hexcode) ?arg)
|
||||
& !decoded !notencoded chr$(x2d$!hexcode):?decoded
|
||||
)
|
||||
& str$(!decoded !arg)
|
||||
)
|
||||
& out$(decode$http%3A%2F%2Ffoo%20bar%2F)
|
||||
);
|
||||
11
Task/URL-decoding/C++/url-decoding.cpp
Normal file
11
Task/URL-decoding/C++/url-decoding.cpp
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
#include <string>
|
||||
#include "Poco/URI.h"
|
||||
#include <iostream>
|
||||
|
||||
int main( ) {
|
||||
std::string encoded( "http%3A%2F%2Ffoo%20bar%2F" ) ;
|
||||
std::string decoded ;
|
||||
Poco::URI::decode ( encoded , decoded ) ;
|
||||
std::cout << encoded << " is decoded: " << decoded << " !" << std::endl ;
|
||||
return 0 ;
|
||||
}
|
||||
17
Task/URL-decoding/C-sharp/url-decoding.cs
Normal file
17
Task/URL-decoding/C-sharp/url-decoding.cs
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
using System;
|
||||
|
||||
namespace URLEncode
|
||||
{
|
||||
internal class Program
|
||||
{
|
||||
private static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine(Decode("http%3A%2F%2Ffoo%20bar%2F"));
|
||||
}
|
||||
|
||||
private static string Decode(string uri)
|
||||
{
|
||||
return Uri.UnescapeDataString(uri);
|
||||
}
|
||||
}
|
||||
}
|
||||
40
Task/URL-decoding/C/url-decoding.c
Normal file
40
Task/URL-decoding/C/url-decoding.c
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
inline int ishex(int x)
|
||||
{
|
||||
return (x >= '0' && x <= '9') ||
|
||||
(x >= 'a' && x <= 'f') ||
|
||||
(x >= 'A' && x <= 'F');
|
||||
}
|
||||
|
||||
int decode(const char *s, char *dec)
|
||||
{
|
||||
char *o;
|
||||
const char *end = s + strlen(s);
|
||||
int c;
|
||||
|
||||
for (o = dec; s <= end; o++) {
|
||||
c = *s++;
|
||||
if (c == '+') c = ' ';
|
||||
else if (c == '%' && ( !ishex(*s++) ||
|
||||
!ishex(*s++) ||
|
||||
!sscanf(s - 2, "%2x", &c)))
|
||||
return -1;
|
||||
|
||||
if (dec) *o = c;
|
||||
}
|
||||
|
||||
return o - dec;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
const char *url = "http%3A%2F%2ffoo+bar%2fabcd";
|
||||
char out[strlen(url) + 1];
|
||||
|
||||
printf("length: %d\n", decode(url, 0));
|
||||
puts(decode(url, out) < 0 ? "bad string" : out);
|
||||
|
||||
return 0;
|
||||
}
|
||||
1
Task/URL-decoding/Clojure/url-decoding.clj
Normal file
1
Task/URL-decoding/Clojure/url-decoding.clj
Normal file
|
|
@ -0,0 +1 @@
|
|||
(java.net.URLDecoder/decode "http%3A%2F%2Ffoo%20bar%2F")
|
||||
1
Task/URL-decoding/CoffeeScript/url-decoding-1.coffee
Normal file
1
Task/URL-decoding/CoffeeScript/url-decoding-1.coffee
Normal file
|
|
@ -0,0 +1 @@
|
|||
console.log decodeURIComponent "http%3A%2F%2Ffoo%20bar%2F?name=Foo%20Barson"
|
||||
2
Task/URL-decoding/CoffeeScript/url-decoding-2.coffee
Normal file
2
Task/URL-decoding/CoffeeScript/url-decoding-2.coffee
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
> coffee foo.coffee
|
||||
http://foo bar/?name=Foo Barson
|
||||
23
Task/URL-decoding/Common-Lisp/url-decoding.lisp
Normal file
23
Task/URL-decoding/Common-Lisp/url-decoding.lisp
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
(defun decode (string &key start)
|
||||
(assert (char= (char string start) #\%))
|
||||
(if (>= (length string) (+ start 3))
|
||||
(multiple-value-bind (code pos)
|
||||
(parse-integer string :start (1+ start) :end (+ start 3) :radix 16 :junk-allowed t)
|
||||
(if (= pos (+ start 3))
|
||||
(values (code-char code) pos)
|
||||
(values #\% (1+ start))))
|
||||
(values #\% (1+ start))))
|
||||
|
||||
(defun url-decode (url)
|
||||
(loop with start = 0
|
||||
for pos = (position #\% url :start start)
|
||||
collect (subseq url start pos) into chunks
|
||||
when pos
|
||||
collect (multiple-value-bind (decoded next) (decode url :start pos)
|
||||
(setf start next)
|
||||
(string decoded))
|
||||
into chunks
|
||||
while pos
|
||||
finally (return (apply #'concatenate 'string chunks))))
|
||||
|
||||
(url-decode "http%3A%2F%2Ffoo%20bar%2F")
|
||||
4
Task/URL-decoding/Crystal/url-decoding.crystal
Normal file
4
Task/URL-decoding/Crystal/url-decoding.crystal
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
require "uri"
|
||||
|
||||
puts URI.decode "http%3A%2F%2Ffoo%20bar%2F"
|
||||
puts URI.decode "google.com/search?q=%60Abdu%27l-Bah%C3%A1"
|
||||
5
Task/URL-decoding/D/url-decoding.d
Normal file
5
Task/URL-decoding/D/url-decoding.d
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
import std.stdio, std.uri;
|
||||
|
||||
void main() {
|
||||
writeln(decodeComponent("http%3A%2F%2Ffoo%20bar%2F"));
|
||||
}
|
||||
9
Task/URL-decoding/Delphi/url-decoding.delphi
Normal file
9
Task/URL-decoding/Delphi/url-decoding.delphi
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
program URLEncoding;
|
||||
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
uses IdURI;
|
||||
|
||||
begin
|
||||
Writeln(TIdURI.URLDecode('http%3A%2F%2Ffoo%20bar%2F'));
|
||||
end.
|
||||
2
Task/URL-decoding/Elixir/url-decoding.elixir
Normal file
2
Task/URL-decoding/Elixir/url-decoding.elixir
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
IO.inspect URI.decode("http%3A%2F%2Ffoo%20bar%2F")
|
||||
IO.inspect URI.decode("google.com/search?q=%60Abdu%27l-Bah%C3%A1")
|
||||
8
Task/URL-decoding/F-Sharp/url-decoding.fs
Normal file
8
Task/URL-decoding/F-Sharp/url-decoding.fs
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
open System
|
||||
|
||||
let decode uri = Uri.UnescapeDataString(uri)
|
||||
|
||||
[<EntryPoint>]
|
||||
let main argv =
|
||||
printfn "%s" (decode "http%3A%2F%2Ffoo%20bar%2F")
|
||||
0
|
||||
6
Task/URL-decoding/Factor/url-decoding.factor
Normal file
6
Task/URL-decoding/Factor/url-decoding.factor
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
USING: io kernel urls.encoding ;
|
||||
IN: rosetta-code.url-decoding
|
||||
|
||||
"http%3A%2F%2Ffoo%20bar%2F"
|
||||
"google.com/search?q=%60Abdu%27l-Bah%C3%A1"
|
||||
[ url-decode print ] bi@
|
||||
26
Task/URL-decoding/Free-Pascal/url-decoding.pas
Normal file
26
Task/URL-decoding/Free-Pascal/url-decoding.pas
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
function urlDecode(data: String): AnsiString;
|
||||
var
|
||||
ch: Char;
|
||||
pos, skip: Integer;
|
||||
|
||||
begin
|
||||
pos := 0;
|
||||
skip := 0;
|
||||
Result := '';
|
||||
|
||||
for ch in data do begin
|
||||
if skip = 0 then begin
|
||||
if (ch = '%') and (pos < data.length -2) then begin
|
||||
skip := 2;
|
||||
Result := Result + AnsiChar(Hex2Dec('$' + data[pos+2] + data[pos+3]));
|
||||
|
||||
end else begin
|
||||
Result := Result + ch;
|
||||
end;
|
||||
|
||||
end else begin
|
||||
skip := skip - 1;
|
||||
end;
|
||||
pos := pos +1;
|
||||
end;
|
||||
end;
|
||||
39
Task/URL-decoding/FreeBASIC/url-decoding.basic
Normal file
39
Task/URL-decoding/FreeBASIC/url-decoding.basic
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
Const alphanum = "0123456789abcdefghijklmnopqrstuvwxyz"
|
||||
|
||||
Function ToDecimal (cadena As String, base_ As Uinteger) As Uinteger
|
||||
Dim As Uinteger i, n, result = 0
|
||||
Dim As Uinteger inlength = Len(cadena)
|
||||
|
||||
For i = 1 To inlength
|
||||
n = Instr(alphanum, Mid(Lcase(cadena),i,1)) - 1
|
||||
n *= (base_^(inlength-i))
|
||||
result += n
|
||||
Next
|
||||
Return result
|
||||
End Function
|
||||
|
||||
Function url2string(cadena As String) As String
|
||||
Dim As String c, nc, res
|
||||
|
||||
For j As Integer = 1 To Len(cadena)
|
||||
c = Mid(cadena, j, 1)
|
||||
If c = "%" Then
|
||||
nc = Chr(ToDecimal((Mid(cadena, j+1, 2)), 16))
|
||||
res &= nc
|
||||
j += 2
|
||||
Else
|
||||
res &= c
|
||||
End If
|
||||
Next j
|
||||
Return res
|
||||
End Function
|
||||
|
||||
Dim As String URL = "http%3A%2F%2Ffoo%20bar%2F"
|
||||
|
||||
Print "Supplied URL '"; URL; "'"
|
||||
Print "URL decoding '"; url2string(URL); "'"
|
||||
|
||||
URL = "google.com/search?q=%60Abdu%27l-Bah%C3%A1"
|
||||
Print !"\nSupplied URL '"; URL; "'"
|
||||
Print "URL decoding '"; url2string(URL); "'"
|
||||
Sleep
|
||||
1
Task/URL-decoding/Frink/url-decoding.frink
Normal file
1
Task/URL-decoding/Frink/url-decoding.frink
Normal file
|
|
@ -0,0 +1 @@
|
|||
URLDecode["google.com/search?q=%60Abdu%27l-Bah%C3%A1","UTF8"]
|
||||
21
Task/URL-decoding/Go/url-decoding.go
Normal file
21
Task/URL-decoding/Go/url-decoding.go
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
func main() {
|
||||
for _, escaped := range []string{
|
||||
"http%3A%2F%2Ffoo%20bar%2F",
|
||||
"google.com/search?q=%60Abdu%27l-Bah%C3%A1",
|
||||
} {
|
||||
u, err := url.QueryUnescape(escaped)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
continue
|
||||
}
|
||||
fmt.Println(u)
|
||||
}
|
||||
}
|
||||
1
Task/URL-decoding/Groovy/url-decoding.groovy
Normal file
1
Task/URL-decoding/Groovy/url-decoding.groovy
Normal file
|
|
@ -0,0 +1 @@
|
|||
assert URLDecoder.decode('http%3A%2F%2Ffoo%20bar%2F') == 'http://foo bar/'
|
||||
15
Task/URL-decoding/Haskell/url-decoding-1.hs
Normal file
15
Task/URL-decoding/Haskell/url-decoding-1.hs
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
import qualified Data.Char as Char
|
||||
|
||||
urlDecode :: String -> Maybe String
|
||||
urlDecode [] = Just []
|
||||
urlDecode ('%':xs) =
|
||||
case xs of
|
||||
(a:b:xss) ->
|
||||
urlDecode xss
|
||||
>>= return . ((Char.chr . read $ "0x" ++ [a,b]) :)
|
||||
_ -> Nothing
|
||||
urlDecode ('+':xs) = urlDecode xs >>= return . (' ' :)
|
||||
urlDecode (x:xs) = urlDecode xs >>= return . (x :)
|
||||
|
||||
main :: IO ()
|
||||
main = putStrLn . maybe "Bad decode" id $ urlDecode "http%3A%2F%2Ffoo%20bar%2F"
|
||||
13
Task/URL-decoding/Haskell/url-decoding-2.hs
Normal file
13
Task/URL-decoding/Haskell/url-decoding-2.hs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
import Data.Char (chr)
|
||||
import Data.List.Split (splitOn)
|
||||
|
||||
deCode :: String -> String
|
||||
deCode url =
|
||||
let ps = splitOn "%" url
|
||||
in concat $
|
||||
head ps :
|
||||
((\(a, b) -> (chr . read) (mappend "0x" a) : b) <$> (splitAt 2 <$> tail ps))
|
||||
|
||||
-- TEST ------------------------------------------------------------------------
|
||||
main :: IO ()
|
||||
main = putStrLn $ deCode "http%3A%2F%2Ffoo%20bar%2F"
|
||||
22
Task/URL-decoding/Icon/url-decoding.icon
Normal file
22
Task/URL-decoding/Icon/url-decoding.icon
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
link hexcvt
|
||||
|
||||
procedure main()
|
||||
ue := "http%3A%2F%2Ffoo%20bar%2F"
|
||||
ud := decodeURL(ue) | stop("Improperly encoded string ",image(ue))
|
||||
write("encoded = ",image(ue))
|
||||
write("decoded = ",image(ue))
|
||||
end
|
||||
|
||||
procedure decodeURL(s) #: decode URL/URI encoded data
|
||||
static de
|
||||
initial { # build lookup table for everything
|
||||
de := table()
|
||||
every de[hexstring(ord(c := !string(&ascii)),2)] := c
|
||||
}
|
||||
|
||||
c := ""
|
||||
s ? until pos(0) do # decode every %xx or fail
|
||||
c ||:= if ="%" then \de[move(2)] | fail
|
||||
else move(1)
|
||||
return c
|
||||
end
|
||||
2
Task/URL-decoding/J/url-decoding-1.j
Normal file
2
Task/URL-decoding/J/url-decoding-1.j
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
require'strings convert'
|
||||
urldecode=: rplc&(~.,/;"_1&a."2(,:tolower)'%',.toupper hfd i.#a.)
|
||||
2
Task/URL-decoding/J/url-decoding-2.j
Normal file
2
Task/URL-decoding/J/url-decoding-2.j
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
urldecode 'http%3A%2F%2Ffoo%20bar%2F'
|
||||
http://foo bar/
|
||||
2
Task/URL-decoding/J/url-decoding-3.j
Normal file
2
Task/URL-decoding/J/url-decoding-3.j
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
urldecode 'google.com/search?q=%60Abdu%27l-Bah%C3%A1'
|
||||
google.com/search?q=`Abdu'l-Bahá
|
||||
2
Task/URL-decoding/Java/url-decoding-1.java
Normal file
2
Task/URL-decoding/Java/url-decoding-1.java
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
import java.net.URLDecoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
1
Task/URL-decoding/Java/url-decoding-2.java
Normal file
1
Task/URL-decoding/Java/url-decoding-2.java
Normal file
|
|
@ -0,0 +1 @@
|
|||
URLDecoder.decode("http%3A%2F%2Ffoo%20bar%2F", StandardCharsets.UTF_8)
|
||||
2
Task/URL-decoding/Java/url-decoding-3.java
Normal file
2
Task/URL-decoding/Java/url-decoding-3.java
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
16
Task/URL-decoding/Java/url-decoding-4.java
Normal file
16
Task/URL-decoding/Java/url-decoding-4.java
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
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();
|
||||
}
|
||||
1
Task/URL-decoding/JavaScript/url-decoding.js
Normal file
1
Task/URL-decoding/JavaScript/url-decoding.js
Normal file
|
|
@ -0,0 +1 @@
|
|||
decodeURIComponent("http%3A%2F%2Ffoo%20bar%2F")
|
||||
27
Task/URL-decoding/Jq/url-decoding-1.jq
Normal file
27
Task/URL-decoding/Jq/url-decoding-1.jq
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
# Emit . and stop as soon as "condition" is true.
|
||||
def until(condition; next):
|
||||
def u: if condition then . else (next|u) end;
|
||||
u;
|
||||
|
||||
def url_decode:
|
||||
# The helper function converts the input string written in the given
|
||||
# "base" to an integer
|
||||
def to_i(base):
|
||||
explode
|
||||
| reverse
|
||||
| map(if 65 <= . and . <= 90 then . + 32 else . end) # downcase
|
||||
| map(if . > 96 then . - 87 else . - 48 end) # "a" ~ 97 => 10 ~ 87
|
||||
| reduce .[] as $c
|
||||
# base: [power, ans]
|
||||
([1,0]; (.[0] * base) as $b | [$b, .[1] + (.[0] * $c)]) | .[1];
|
||||
|
||||
. as $in
|
||||
| length as $length
|
||||
| [0, ""] # i, answer
|
||||
| until ( .[0] >= $length;
|
||||
.[0] as $i
|
||||
| if $in[$i:$i+1] == "%"
|
||||
then [ $i + 3, .[1] + ([$in[$i+1:$i+3] | to_i(16)] | implode) ]
|
||||
else [ $i + 1, .[1] + $in[$i:$i+1] ]
|
||||
end)
|
||||
| .[1]; # answer
|
||||
1
Task/URL-decoding/Jq/url-decoding-2.jq
Normal file
1
Task/URL-decoding/Jq/url-decoding-2.jq
Normal file
|
|
@ -0,0 +1 @@
|
|||
"http%3A%2F%2Ffoo%20bar%2F" | url_decode
|
||||
1
Task/URL-decoding/Jq/url-decoding-3.jq
Normal file
1
Task/URL-decoding/Jq/url-decoding-3.jq
Normal file
|
|
@ -0,0 +1 @@
|
|||
"http://foo bar/"
|
||||
7
Task/URL-decoding/Julia/url-decoding.julia
Normal file
7
Task/URL-decoding/Julia/url-decoding.julia
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
using URIParser
|
||||
|
||||
enc = "http%3A%2F%2Ffoo%20bar%2F"
|
||||
|
||||
dcd = unescape(enc)
|
||||
|
||||
println(enc, " => ", dcd)
|
||||
8
Task/URL-decoding/Kotlin/url-decoding.kotlin
Normal file
8
Task/URL-decoding/Kotlin/url-decoding.kotlin
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
// version 1.1.2
|
||||
|
||||
import java.net.URLDecoder
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val encoded = arrayOf("http%3A%2F%2Ffoo%20bar%2F", "google.com/search?q=%60Abdu%27l-Bah%C3%A1")
|
||||
for (e in encoded) println(URLDecoder.decode(e, "UTF-8"))
|
||||
}
|
||||
8
Task/URL-decoding/Ksh/url-decoding.ksh
Normal file
8
Task/URL-decoding/Ksh/url-decoding.ksh
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
url_decode()
|
||||
{
|
||||
decode="${*//+/ }"
|
||||
eval print -r -- "\$'${decode//'%'@(??)/'\'x\1"'\$'"}'" 2>/dev/null
|
||||
}
|
||||
|
||||
url_decode "http%3A%2F%2Ffoo%20bar%2F"
|
||||
url_decode "google.com/search?q=%60Abdu%27l-Bah%C3%A1"
|
||||
10
Task/URL-decoding/Lambdatalk/url-decoding.lambdatalk
Normal file
10
Task/URL-decoding/Lambdatalk/url-decoding.lambdatalk
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
1) define a new javascript primitive:
|
||||
{script
|
||||
LAMBDATALK.DICT['decodeURIComponent'] = function() {
|
||||
return decodeURIComponent( arguments[0].trim() );
|
||||
};
|
||||
}
|
||||
|
||||
2) and use it:
|
||||
{decodeURIComponent http%3A%2F%2Ffoo%20bar%2F}
|
||||
-> http://foo bar/
|
||||
5
Task/URL-decoding/Langur/url-decoding.langur
Normal file
5
Task/URL-decoding/Langur/url-decoding.langur
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
val .helper = f(.s) b2s map f toNumber(.x, 16), rest split "%", .s
|
||||
val .decode = f(.s) replace .s, re/(%[0-9A-Fa-f]{2})+/, .helper
|
||||
|
||||
writeln .decode("http%3A%2F%2Ffoo%20bar%2F")
|
||||
writeln .decode("google.com/search?q=%60Abdu%27l-Bah%C3%A1")
|
||||
1
Task/URL-decoding/Lasso/url-decoding.lasso
Normal file
1
Task/URL-decoding/Lasso/url-decoding.lasso
Normal file
|
|
@ -0,0 +1 @@
|
|||
bytes('http%3A%2F%2Ffoo%20bar%2F') -> decodeurl
|
||||
25
Task/URL-decoding/Liberty-BASIC/url-decoding.basic
Normal file
25
Task/URL-decoding/Liberty-BASIC/url-decoding.basic
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
dim lookUp$( 256)
|
||||
|
||||
for i =0 to 256
|
||||
lookUp$( i) ="%" +dechex$( i)
|
||||
next i
|
||||
|
||||
url$ ="http%3A%2F%2Ffoo%20bar%2F"
|
||||
|
||||
print "Supplied URL '"; url$; "'"
|
||||
print "As string '"; url2string$( url$); "'"
|
||||
|
||||
end
|
||||
|
||||
function url2string$( i$)
|
||||
for j =1 to len( i$)
|
||||
c$ =mid$( i$, j, 1)
|
||||
if c$ ="%" then
|
||||
nc$ =chr$( hexdec( mid$( i$, j +1, 2)))
|
||||
url2string$ =url2string$ +nc$
|
||||
j =j +2
|
||||
else
|
||||
url2string$ =url2string$ +c$
|
||||
end if
|
||||
next j
|
||||
end function
|
||||
24
Task/URL-decoding/Lingo/url-decoding-1.lingo
Normal file
24
Task/URL-decoding/Lingo/url-decoding-1.lingo
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
----------------------------------------
|
||||
-- URL decodes a string
|
||||
-- @param {string} str
|
||||
-- @return {string}
|
||||
----------------------------------------
|
||||
on urldecode (str)
|
||||
res = ""
|
||||
ba = bytearray()
|
||||
len = str.length
|
||||
repeat with i = 1 to len
|
||||
c = str.char[i]
|
||||
if (c = "%") then
|
||||
-- fastest hex-to-dec conversion hack based on Lingo's rgb object
|
||||
ba.writeInt8(rgb(str.char[i+1..i+2]).blue)
|
||||
i = i + 2
|
||||
else if (c = "+") then
|
||||
ba.writeInt8(32)
|
||||
else
|
||||
ba.writeInt8(chartonum(c))
|
||||
end if
|
||||
end repeat
|
||||
ba.position = 1
|
||||
return ba.readRawString(ba.length)
|
||||
end
|
||||
2
Task/URL-decoding/Lingo/url-decoding-2.lingo
Normal file
2
Task/URL-decoding/Lingo/url-decoding-2.lingo
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
put urldecode("http%3A%2F%2Ffoo%20bar%2F")
|
||||
put urldecode("google.com/search?q=%60Abdu%27l-Bah%C3%A1")
|
||||
2
Task/URL-decoding/LiveCode/url-decoding-1.livecode
Normal file
2
Task/URL-decoding/LiveCode/url-decoding-1.livecode
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
put urlDecode("http%3A%2F%2Ffoo%20bar%2F") & cr & \
|
||||
urlDecode("google.com/search?q=%60Abdu%27l-Bah%C3%A1")
|
||||
2
Task/URL-decoding/LiveCode/url-decoding-2.livecode
Normal file
2
Task/URL-decoding/LiveCode/url-decoding-2.livecode
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
http://foo bar/
|
||||
google.com/search?q=`Abdu'l-Bah√°
|
||||
11
Task/URL-decoding/Lua/url-decoding.lua
Normal file
11
Task/URL-decoding/Lua/url-decoding.lua
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
function decodeChar(hex)
|
||||
return string.char(tonumber(hex,16))
|
||||
end
|
||||
|
||||
function decodeString(str)
|
||||
local output, t = string.gsub(str,"%%(%x%x)",decodeChar)
|
||||
return output
|
||||
end
|
||||
|
||||
-- will print "http://foo bar/"
|
||||
print(decodeString("http%3A%2F%2Ffoo%20bar%2F"))
|
||||
25
Task/URL-decoding/M2000-Interpreter/url-decoding.m2000
Normal file
25
Task/URL-decoding/M2000-Interpreter/url-decoding.m2000
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
Module CheckIt {
|
||||
Function decodeUrl$(a$) {
|
||||
DIM a$()
|
||||
a$()=Piece$(a$, "%")
|
||||
if len(a$())=1 then =str$(a$):exit
|
||||
k=each(a$(),2)
|
||||
\\ convert to one byte per character using str$(string)
|
||||
acc$=str$(a$(0))
|
||||
While k {
|
||||
\\ chr$() convert to UTF16LE
|
||||
\\ str$() convert to ANSI using locale (can be 1033 we can set it before as Locale 1033)
|
||||
\\ so chr$(0x93) give 0x201C
|
||||
\\ str$(chr$(0x93)) return one byte 93 in ANSI as string of one byte length
|
||||
\\ numbers are for UTF-8 so we have to preserve them
|
||||
acc$+=str$(Chr$(Eval("0x"+left$(a$(k^),2)))+Mid$(a$(k^),3))
|
||||
}
|
||||
=acc$
|
||||
}
|
||||
\\ decode from utf8
|
||||
final$=DecodeUrl$("google.com/search?q=%60Abdu%27l-Bah%C3%A1")
|
||||
Print string$(final$ as utf8dec)="google.com/search?q=`Abdu'l-Bahá"
|
||||
final$=DecodeUrl$("http%3A%2F%2Ffoo%20bar%2F")
|
||||
Print string$(final$ as utf8dec)="http://foo bar/"
|
||||
}
|
||||
CheckIt
|
||||
13
Task/URL-decoding/MATLAB/url-decoding.m
Normal file
13
Task/URL-decoding/MATLAB/url-decoding.m
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
function u = urldecoding(s)
|
||||
u = '';
|
||||
k = 1;
|
||||
while k<=length(s)
|
||||
if s(k) == '%' && k+2 <= length(s)
|
||||
u = sprintf('%s%c', u, char(hex2dec(s((k+1):(k+2)))));
|
||||
k = k + 3;
|
||||
else
|
||||
u = sprintf('%s%c', u, s(k));
|
||||
k = k + 1;
|
||||
end
|
||||
end
|
||||
end
|
||||
1
Task/URL-decoding/Maple/url-decoding.maple
Normal file
1
Task/URL-decoding/Maple/url-decoding.maple
Normal file
|
|
@ -0,0 +1 @@
|
|||
StringTools:-Decode("http%3A%2F%2Ffoo%20bar%2F", encoding=percent);
|
||||
4
Task/URL-decoding/Mathematica/url-decoding-1.math
Normal file
4
Task/URL-decoding/Mathematica/url-decoding-1.math
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
URLDecoding[url_] :=
|
||||
StringReplace[url, "%" ~~ x_ ~~ y_ :> FromDigits[x ~~ y, 16]] //.
|
||||
StringExpression[x___, Longest[n__Integer], y___] :>
|
||||
StringExpression[x, FromCharacterCode[{n}, "UTF8"], y]
|
||||
1
Task/URL-decoding/Mathematica/url-decoding-2.math
Normal file
1
Task/URL-decoding/Mathematica/url-decoding-2.math
Normal file
|
|
@ -0,0 +1 @@
|
|||
URLDecoding["http%3A%2F%2Ffoo%20bar%2F"]
|
||||
43
Task/URL-decoding/NetRexx/url-decoding.netrexx
Normal file
43
Task/URL-decoding/NetRexx/url-decoding.netrexx
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
/* NetRexx */
|
||||
options replace format comments java crossref savelog symbols nobinary
|
||||
|
||||
url = [ -
|
||||
'http%3A%2F%2Ffoo%20bar%2F', -
|
||||
'mailto%3A%22Ivan%20Aim%22%20%3Civan%2Eaim%40email%2Ecom%3E', -
|
||||
'%6D%61%69%6C%74%6F%3A%22%49%72%6D%61%20%55%73%65%72%22%20%3C%69%72%6D%61%2E%75%73%65%72%40%6D%61%69%6C%2E%63%6F%6D%3E' -
|
||||
]
|
||||
|
||||
loop u_ = 0 to url.length - 1
|
||||
say url[u_]
|
||||
say DecodeURL(url[u_])
|
||||
say
|
||||
end u_
|
||||
|
||||
return
|
||||
|
||||
method DecodeURL(arg) public static
|
||||
|
||||
Parse arg encoded
|
||||
decoded = ''
|
||||
PCT = '%'
|
||||
|
||||
loop label e_ while encoded.length() > 0
|
||||
parse encoded head (PCT) +1 code +2 tail
|
||||
decoded = decoded || head
|
||||
select
|
||||
when code.strip('T').length() = 2 & code.datatype('X') then do
|
||||
code = code.x2c()
|
||||
decoded = decoded || code
|
||||
end
|
||||
when code.strip('T').length() \= 0 then do
|
||||
decoded = decoded || PCT
|
||||
tail = code || tail
|
||||
end
|
||||
otherwise do
|
||||
nop
|
||||
end
|
||||
end
|
||||
encoded = tail
|
||||
end e_
|
||||
|
||||
return decoded
|
||||
7
Task/URL-decoding/NewLISP/url-decoding.l
Normal file
7
Task/URL-decoding/NewLISP/url-decoding.l
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
;; universal decoder, works for ASCII and UTF-8
|
||||
;; (source http://www.newlisp.org/index.cgi?page=Code_Snippets)
|
||||
(define (url-decode url (opt nil))
|
||||
(if opt (replace "+" url " "))
|
||||
(replace "%([0-9a-f][0-9a-f])" url (pack "b" (int $1 0 16)) 1))
|
||||
|
||||
(url-decode "http%3A%2F%2Ffoo%20bar%2F")
|
||||
3
Task/URL-decoding/Nim/url-decoding.nim
Normal file
3
Task/URL-decoding/Nim/url-decoding.nim
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
import cgi
|
||||
|
||||
echo decodeUrl("http%3A%2F%2Ffoo%20bar%2F")
|
||||
6
Task/URL-decoding/OCaml/url-decoding.ocaml
Normal file
6
Task/URL-decoding/OCaml/url-decoding.ocaml
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
$ ocaml
|
||||
# #use "topfind";;
|
||||
# #require "netstring";;
|
||||
|
||||
# Netencoding.Url.decode "http%3A%2F%2Ffoo%20bar%2F" ;;
|
||||
- : string = "http://foo bar/"
|
||||
8
Task/URL-decoding/Oberon-2/url-decoding.oberon
Normal file
8
Task/URL-decoding/Oberon-2/url-decoding.oberon
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
MODULE URLDecoding;
|
||||
IMPORT
|
||||
URI := URI:String,
|
||||
Out := NPCT:Console;
|
||||
BEGIN
|
||||
Out.String(URI.Unescape("http%3A%2F%2Ffoo%20bar%2F"));Out.Ln;
|
||||
Out.String(URI.Unescape("google.com/search?q=%60Abdu%27l-Bah%C3%A1"));Out.Ln;
|
||||
END URLDecoding.
|
||||
5
Task/URL-decoding/Objeck/url-decoding.objeck
Normal file
5
Task/URL-decoding/Objeck/url-decoding.objeck
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
class UrlDecode {
|
||||
function : Main(args : String[]) ~ Nil {
|
||||
Net.UrlUtility->Decode("http%3A%2F%2Ffoo%20bar%2F")->PrintLine();
|
||||
}
|
||||
}
|
||||
3
Task/URL-decoding/Objective-C/url-decoding-1.m
Normal file
3
Task/URL-decoding/Objective-C/url-decoding-1.m
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
NSString *encoded = @"http%3A%2F%2Ffoo%20bar%2F";
|
||||
NSString *normal = [encoded stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
|
||||
NSLog(@"%@", normal);
|
||||
3
Task/URL-decoding/Objective-C/url-decoding-2.m
Normal file
3
Task/URL-decoding/Objective-C/url-decoding-2.m
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
NSString *encoded = @"http%3A%2F%2Ffoo%20bar%2F";
|
||||
NSString *normal = [encoded stringByRemovingPercentEncoding];
|
||||
NSLog(@"%@", normal);
|
||||
40
Task/URL-decoding/OoRexx/url-decoding.rexx
Normal file
40
Task/URL-decoding/OoRexx/url-decoding.rexx
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
/* Rexx */
|
||||
X = 0
|
||||
url. = ''
|
||||
X = X + 1; url.0 = X; url.X = 'http%3A%2F%2Ffoo%20bar%2F'
|
||||
X = X + 1; url.0 = X; url.X = 'mailto%3A%22Ivan%20Aim%22%20%3Civan%2Eaim%40email%2Ecom%3E'
|
||||
X = X + 1; url.0 = X; url.X = '%6D%61%69%6C%74%6F%3A%22%49%72%6D%61%20%55%73%65%72%22%20%3C%69%72%6D%61%2E%75%73%65%72%40%6D%61%69%6C%2E%63%6F%6D%3E'
|
||||
|
||||
Do u_ = 1 to url.0
|
||||
Say url.u_
|
||||
Say DecodeURL(url.u_)
|
||||
Say
|
||||
End u_
|
||||
|
||||
Exit
|
||||
|
||||
DecodeURL: Procedure
|
||||
|
||||
Parse Arg encoded
|
||||
decoded = ''
|
||||
PCT = '%'
|
||||
|
||||
Do label e_ while encoded~length() > 0
|
||||
Parse Var encoded head (PCT) +1 code +2 tail
|
||||
decoded = decoded || head
|
||||
Select
|
||||
when code~strip('T')~length() = 2 & code~datatype('X') then Do
|
||||
code = code~x2c()
|
||||
decoded = decoded || code
|
||||
End
|
||||
when code~strip('T')~length() \= 0 then Do
|
||||
decoded = decoded || PCT
|
||||
tail = code || tail
|
||||
End
|
||||
otherwise
|
||||
Nop
|
||||
End
|
||||
encoded = tail
|
||||
End e_
|
||||
|
||||
Return decoded
|
||||
5
Task/URL-decoding/PHP/url-decoding.php
Normal file
5
Task/URL-decoding/PHP/url-decoding.php
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
<?php
|
||||
$encoded = "http%3A%2F%2Ffoo%20bar%2F";
|
||||
$unencoded = rawurldecode($encoded);
|
||||
echo "The unencoded string is $unencoded !\n";
|
||||
?>
|
||||
8
Task/URL-decoding/Perl/url-decoding-1.pl
Normal file
8
Task/URL-decoding/Perl/url-decoding-1.pl
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
sub urldecode {
|
||||
my $s = shift;
|
||||
$s =~ tr/\+/ /;
|
||||
$s =~ s/\%([A-Fa-f0-9]{2})/pack('C', hex($1))/eg;
|
||||
return $s;
|
||||
}
|
||||
|
||||
print urldecode('http%3A%2F%2Ffoo+bar%2F')."\n";
|
||||
7
Task/URL-decoding/Perl/url-decoding-2.pl
Normal file
7
Task/URL-decoding/Perl/url-decoding-2.pl
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
#!/usr/bin/perl -w
|
||||
use strict ;
|
||||
use URI::Escape ;
|
||||
|
||||
my $encoded = "http%3A%2F%2Ffoo%20bar%2F" ;
|
||||
my $unencoded = uri_unescape( $encoded ) ;
|
||||
print "The unencoded string is $unencoded !\n" ;
|
||||
38
Task/URL-decoding/Phix/url-decoding.phix
Normal file
38
Task/URL-decoding/Phix/url-decoding.phix
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #000080;font-style:italic;">--
|
||||
-- demo\rosetta\decode_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;">decode_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;">integer</span> <span style="color: #000000;">skip</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</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: #008080;">if</span> <span style="color: #000000;">skip</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">skip</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">else</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;">then</span>
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">scanres</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;"><=</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;">then</span>
|
||||
<span style="color: #000000;">scanres</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">scanf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"#"</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: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">],</span><span style="color: #008000;">"%x"</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">scanres</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #008000;">"decode error"</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #000000;">skip</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span>
|
||||
<span style="color: #000000;">ch</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">scanres</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">][</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #008080;">elsif</span> <span style="color: #000000;">ch</span><span style="color: #0000FF;">=</span><span style="color: #008000;">'+'</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;">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;">if</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;">decode_url</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"http%3A%2F%2Ffoo%20bar%2F"</span><span style="color: #0000FF;">)})</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;">decode_url</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"google.com/search?q=%60Abdu%27l-Bah%C3%A1"</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>
|
||||
<!--
|
||||
13
Task/URL-decoding/Pike/url-decoding.pike
Normal file
13
Task/URL-decoding/Pike/url-decoding.pike
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
void main()
|
||||
{
|
||||
array encoded_urls = ({
|
||||
"http%3A%2F%2Ffoo%20bar%2F",
|
||||
"google.com/search?q=%60Abdu%27l-Bah%C3%A1"
|
||||
});
|
||||
|
||||
|
||||
foreach(encoded_urls, string url) {
|
||||
string decoded = Protocols.HTTP.uri_decode( url );
|
||||
write( string_to_utf8(decoded) +"\n" ); // Assume sink does UTF8
|
||||
}
|
||||
}
|
||||
1
Task/URL-decoding/PowerShell/url-decoding.psh
Normal file
1
Task/URL-decoding/PowerShell/url-decoding.psh
Normal file
|
|
@ -0,0 +1 @@
|
|||
[System.Web.HttpUtility]::UrlDecode("http%3A%2F%2Ffoo%20bar%2F")
|
||||
3
Task/URL-decoding/PureBasic/url-decoding.basic
Normal file
3
Task/URL-decoding/PureBasic/url-decoding.basic
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
URL$ = URLDecoder("http%3A%2F%2Ffoo%20bar%2F")
|
||||
|
||||
Debug URL$ ; http://foo bar/
|
||||
6
Task/URL-decoding/Python/url-decoding.py
Normal file
6
Task/URL-decoding/Python/url-decoding.py
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
#Python 2.X
|
||||
import urllib
|
||||
print urllib.unquote("http%3A%2F%2Ffoo%20bar%2F")
|
||||
#Python 3.5+
|
||||
from urllib.parse import unquote
|
||||
print(unquote('http%3A%2F%2Ffoo%20bar%2F'))
|
||||
1
Task/URL-decoding/R/url-decoding.r
Normal file
1
Task/URL-decoding/R/url-decoding.r
Normal file
|
|
@ -0,0 +1 @@
|
|||
URLdecode("http%3A%2F%2Ffoo%20bar%2F")
|
||||
48
Task/URL-decoding/REXX/url-decoding-1.rexx
Normal file
48
Task/URL-decoding/REXX/url-decoding-1.rexx
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
/* Rexx */
|
||||
|
||||
Do
|
||||
X = 0
|
||||
url. = ''
|
||||
X = X + 1; url.0 = X; url.X = 'http%3A%2F%2Ffoo%20bar%2F'
|
||||
X = X + 1; url.0 = X; url.X = 'mailto%3A%22Ivan%20Aim%22%20%3Civan%2Eaim%40email%2Ecom%3E'
|
||||
X = X + 1; url.0 = X; url.X = '%6D%61%69%6C%74%6F%3A%22%49%72%6D%61%20%55%73%65%72%22%20%3C%69%72%6D%61%2E%75%73%65%72%40%6D%61%69%6C%2E%63%6F%6D%3E'
|
||||
|
||||
Do u_ = 1 to url.0
|
||||
Say url.u_
|
||||
Say DecodeURL(url.u_)
|
||||
Say
|
||||
End u_
|
||||
|
||||
Return
|
||||
End
|
||||
Exit
|
||||
|
||||
DecodeURL:
|
||||
Procedure
|
||||
Do
|
||||
Parse Arg encoded
|
||||
decoded = ''
|
||||
PCT = '%'
|
||||
|
||||
Do while length(encoded) > 0
|
||||
Parse Var encoded head (PCT) +1 code +2 tail
|
||||
decoded = decoded || head
|
||||
Select
|
||||
When length(strip(code, 'T')) = 2 & datatype(code, 'X') then Do
|
||||
code = x2c(code)
|
||||
decoded = decoded || code
|
||||
End
|
||||
When length(strip(code, 'T')) \= 0 then Do
|
||||
decoded = decoded || PCT
|
||||
tail = code || tail
|
||||
End
|
||||
Otherwise Do
|
||||
Nop
|
||||
End
|
||||
End
|
||||
encoded = tail
|
||||
End
|
||||
|
||||
Return decoded
|
||||
End
|
||||
Exit
|
||||
29
Task/URL-decoding/REXX/url-decoding-2.rexx
Normal file
29
Task/URL-decoding/REXX/url-decoding-2.rexx
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
/*REXX program converts a URL─encoded string ──► its original unencoded form. */
|
||||
url.1='http%3A%2F%2Ffoo%20bar%2F'
|
||||
url.2='mailto%3A%22Ivan%20Aim%22%20%3Civan%2Eaim%40email%2Ecom%3E'
|
||||
url.3='%6D%61%69%6C%74%6F%3A%22%49%72%6D%61%20%55%73%65%72%22%20%3C%69%72%6D%61%2E%75%73%65%72%40%6D%61%69%6C%2E%63%6F%6D%3E'
|
||||
URLs =3
|
||||
do j=1 for URLs
|
||||
say url.j
|
||||
say decodeURL(url.j)
|
||||
say
|
||||
end /*j*/
|
||||
exit
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
decodeURL: procedure; parse arg encoded; decoded= ''
|
||||
|
||||
do while encoded\==''
|
||||
parse var encoded head '%' +1 code +2 tail
|
||||
decoded= decoded || head
|
||||
L= length( strip( code, 'T') )
|
||||
select
|
||||
when L==2 & datatype(code, "X") then decoded= decoded || x2c(code)
|
||||
when L\==0 then do; decoded= decoded'%'
|
||||
tail= code || tail
|
||||
end
|
||||
otherwise nop
|
||||
end /*select*/
|
||||
encoded= tail
|
||||
end /*while*/
|
||||
|
||||
return decoded
|
||||
22
Task/URL-decoding/REXX/url-decoding-3.rexx
Normal file
22
Task/URL-decoding/REXX/url-decoding-3.rexx
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
/*REXX program converts & displays a URL─encoded string ──► its original unencoded form.*/
|
||||
url. =
|
||||
url.1='http%3A%2F%2Ffoo%20bar%2F'
|
||||
url.2='mailto%3A%22Ivan%20Aim%22%20%3Civan%2Eaim%40email%2Ecom%3E'
|
||||
url.3='%6D%61%69%6C%74%6F%3A%22%49%72%6D%61%20%55%73%65%72%22%20%3C%69%72%6D%61%2E%75%73%65%72%40%6D%61%69%6C%2E%63%6F%6D%3E'
|
||||
|
||||
do j=1 until url.j==''; say /*process each URL; display blank line.*/
|
||||
say url.j /*display the original URL. */
|
||||
say URLdecode(url.j) /* " " decoded " */
|
||||
end /*j*/
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
URLdecode: procedure; parse arg yyy /*get encoded URL from argument list. */
|
||||
yyy= translate(yyy, , '+') /*a special case for an encoded blank. */
|
||||
URL=
|
||||
do until yyy==''
|
||||
parse var yyy plain '%' +1 code +2 yyy
|
||||
URL= URL || plain
|
||||
if datatype(code, 'X') then URL= URL || x2c(code)
|
||||
else URL= URL'%'code
|
||||
end /*until*/
|
||||
return URL
|
||||
3
Task/URL-decoding/Racket/url-decoding.rkt
Normal file
3
Task/URL-decoding/Racket/url-decoding.rkt
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
#lang racket
|
||||
(require net/uri-codec)
|
||||
(uri-decode "http%3A%2F%2Ffoo%20bar%2F")
|
||||
7
Task/URL-decoding/Raku/url-decoding.raku
Normal file
7
Task/URL-decoding/Raku/url-decoding.raku
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
my @urls = < http%3A%2F%2Ffoo%20bar%2F
|
||||
google.com/search?q=%60Abdu%27l-Bah%C3%A1 >;=
|
||||
|
||||
say .subst( :g,
|
||||
/ [ '%' ( <xdigit> ** 2 ) ]+ / ,
|
||||
{ Blob.new((:16(~$_) for $0)).decode }
|
||||
) for @urls;
|
||||
4
Task/URL-decoding/Red/url-decoding.red
Normal file
4
Task/URL-decoding/Red/url-decoding.red
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
>> dehex "http%3A%2F%2Ffoo%20bar%2F"
|
||||
== "http://foo bar/"
|
||||
>> dehex "google.com/search?q=%60Abdu%27l-Bah%C3%A1"
|
||||
== "google.com/search?q=`Abdu'l-Bahá"
|
||||
16
Task/URL-decoding/Retro/url-decoding.retro
Normal file
16
Task/URL-decoding/Retro/url-decoding.retro
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
create buffer 32000 allot
|
||||
|
||||
{{
|
||||
create bit 5 allot
|
||||
: extract ( $c-$a ) drop @+ bit ! @+ bit 1+ ! bit ;
|
||||
: render ( $c-$n )
|
||||
dup '+ = [ drop 32 ] ifTrue
|
||||
dup 13 = [ drop 32 ] ifTrue
|
||||
dup 10 = [ drop 32 ] ifTrue
|
||||
dup '% = [ extract hex toNumber decimal ] ifTrue ;
|
||||
: <decode> ( $-$ ) repeat @+ 0; render ^buffer'add again ;
|
||||
---reveal---
|
||||
: decode ( $- ) buffer ^buffer'set <decode> drop ;
|
||||
}}
|
||||
|
||||
"http%3A%2F%2Ffoo%20bar%2F" decode buffer puts
|
||||
3
Task/URL-decoding/Ruby/url-decoding-1.rb
Normal file
3
Task/URL-decoding/Ruby/url-decoding-1.rb
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
require 'cgi'
|
||||
puts CGI.unescape("http%3A%2F%2Ffoo%20bar%2F")
|
||||
# => "http://foo bar/"
|
||||
3
Task/URL-decoding/Ruby/url-decoding-2.rb
Normal file
3
Task/URL-decoding/Ruby/url-decoding-2.rb
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
require 'uri'
|
||||
puts URI.decode_www_form_component("http%3A%2F%2Ffoo%20bar%2F")
|
||||
# => "http://foo bar/"
|
||||
36
Task/URL-decoding/Rust/url-decoding.rust
Normal file
36
Task/URL-decoding/Rust/url-decoding.rust
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
const INPUT1: &str = "http%3A%2F%2Ffoo%20bar%2F";
|
||||
const INPUT2: &str = "google.com/search?q=%60Abdu%27l-Bah%C3%A1";
|
||||
|
||||
fn append_frag(text: &mut String, frag: &mut String) {
|
||||
if !frag.is_empty() {
|
||||
let encoded = frag.chars()
|
||||
.collect::<Vec<char>>()
|
||||
.chunks(2)
|
||||
.map(|ch| {
|
||||
u8::from_str_radix(&ch.iter().collect::<String>(), 16).unwrap()
|
||||
}).collect::<Vec<u8>>();
|
||||
text.push_str(&std::str::from_utf8(&encoded).unwrap());
|
||||
frag.clear();
|
||||
}
|
||||
}
|
||||
|
||||
fn decode(text: &str) -> String {
|
||||
let mut output = String::new();
|
||||
let mut encoded_ch = String::new();
|
||||
let mut iter = text.chars();
|
||||
while let Some(ch) = iter.next() {
|
||||
if ch == '%' {
|
||||
encoded_ch.push_str(&format!("{}{}", iter.next().unwrap(), iter.next().unwrap()));
|
||||
} else {
|
||||
append_frag(&mut output, &mut encoded_ch);
|
||||
output.push(ch);
|
||||
}
|
||||
}
|
||||
append_frag(&mut output, &mut encoded_ch);
|
||||
output
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("{}", decode(INPUT1));
|
||||
println!("{}", decode(INPUT2));
|
||||
}
|
||||
16
Task/URL-decoding/Scala/url-decoding.scala
Normal file
16
Task/URL-decoding/Scala/url-decoding.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-decoding/Seed7/url-decoding.seed7
Normal file
8
Task/URL-decoding/Seed7/url-decoding.seed7
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
$ include "seed7_05.s7i";
|
||||
include "encoding.s7i";
|
||||
|
||||
const proc: main is func
|
||||
begin
|
||||
writeln(fromPercentEncoded("http%3A%2F%2Ffoo%20bar%2F"));
|
||||
writeln(fromUrlEncoded("http%3A%2F%2Ffoo+bar%2F"));
|
||||
end func;
|
||||
7
Task/URL-decoding/Sidef/url-decoding.sidef
Normal file
7
Task/URL-decoding/Sidef/url-decoding.sidef
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
func urldecode(str) {
|
||||
str.gsub!('+', ' ');
|
||||
str.gsub!(/\%([A-Fa-f0-9]{2})/, {|a| 'C'.pack(a.hex)});
|
||||
return str;
|
||||
}
|
||||
|
||||
say urldecode('http%3A%2F%2Ffoo+bar%2F'); # => "http://foo bar/"
|
||||
6
Task/URL-decoding/Swift/url-decoding.swift
Normal file
6
Task/URL-decoding/Swift/url-decoding.swift
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
import Foundation
|
||||
|
||||
let encoded = "http%3A%2F%2Ffoo%20bar%2F"
|
||||
if let normal = encoded.stringByReplacingPercentEscapesUsingEncoding(NSUTF8StringEncoding) {
|
||||
println(normal)
|
||||
}
|
||||
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