all tasks

This commit is contained in:
Ingy döt Net 2013-04-11 01:07:29 -07:00
parent b83f433714
commit 68f8f3e56b
14735 changed files with 178959 additions and 0 deletions

View file

@ -0,0 +1,5 @@
This task (the reverse of [[URL encoding]]) is to provide a function or mechanism to convert an URL-encoded string into its original unencoded form.
'''Example'''
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>".

View file

@ -0,0 +1,3 @@
---
category:
- String manipulation

View file

@ -0,0 +1,18 @@
# syntax: GAWK -f URL_DECODING.AWK
BEGIN {
str = "http%3A%2F%2Ffoo%20bar%2F" # "http://foo bar/"
printf("%s\n",str)
while (match(str,/%/)) {
L = substr(str,1,RSTART-1) # chars to left of "%"
M = substr(str,RSTART+1,2) # 2 chars to right of "%"
R = substr(str,RSTART+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)
}

View 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;

View file

@ -0,0 +1,13 @@
encURL := "http%3A%2F%2Ffoo%20bar%2F"
SetFormat, Integer, hex
Loop Parse, encURL
If A_LoopField = `%
reading := 2, read := ""
else if reading
{
read .= A_LoopField, --reading
if not reading
out .= Chr("0x" . read)
}
else out .= A_LoopField
MsgBox % out ; http://foo bar/

View 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$

View 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)
);

View 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 ;
}

View 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);
}
}
}

View 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[sizeof(url)];
printf("length: %d\n", decode(url, 0));
puts(decode(url, out) < 0 ? "bad string" : out);
return 0;
}

View file

@ -0,0 +1 @@
(java.net.URLDecoder/decode "http%3A%2F%2Ffoo%20bar%2F")

View file

@ -0,0 +1 @@
console.log decodeURIComponent "http%3A%2F%2Ffoo%20bar%2F?name=Foo%20Barson"

View file

@ -0,0 +1,2 @@
> coffee foo.coffee
http://foo bar/?name=Foo Barson

View file

@ -0,0 +1,5 @@
import std.stdio, std.uri;
void main() {
writeln(decodeComponent("http%3A%2F%2Ffoo%20bar%2F"));
}

View file

@ -0,0 +1,9 @@
program URLEncoding;
{$APPTYPE CONSOLE}
uses IdURI;
begin
Writeln(TIdURI.URLDecode('http%3A%2F%2Ffoo%20bar%2F'));
end.

View file

@ -0,0 +1,16 @@
package main
import (
"fmt"
"net/url"
)
const escaped = "http%3A%2F%2Ffoo%20bar%2F"
func main() {
if u, err := url.QueryUnescape(escaped); err == nil {
fmt.Println(u)
} else {
fmt.Println(err)
}
}

View file

@ -0,0 +1 @@
assert URLDecoder.decode('http%3A%2F%2Ffoo%20bar%2F') == 'http://foo bar/'

View 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

View file

@ -0,0 +1,2 @@
require'strings convert'
urldecode=: rplc&(;"_1&a."2(,:tolower)'%',.hfd i.#a.)

View file

@ -0,0 +1,2 @@
urldecode 'http%3A%2F%2Ffoo%20bar%2F'
http://foo bar/

View file

@ -0,0 +1 @@
urldecode=: rplc&(~.,/;"_1&a."2(,:tolower)'%',.hfd i.#a.)

View file

@ -0,0 +1,12 @@
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
public class Main
{
public static void main(String[] args) throws UnsupportedEncodingException
{
String encoded = "http%3A%2F%2Ffoo%20bar%2F";
String normal = URLDecoder.decode(encoded, "utf-8");
System.out.println(normal);
}
}

View file

@ -0,0 +1 @@
decodeURIComponent("http%3A%2F%2Ffoo%20bar%2F")

View 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

View 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]

View file

@ -0,0 +1 @@
URLDecoding["http%3A%2F%2Ffoo%20bar%2F"]

View 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

View file

@ -0,0 +1,6 @@
$ ocaml
# #use "topfind";;
# #require "netstring";;
# Netencoding.Url.decode "http%3A%2F%2Ffoo%20bar%2F" ;;
- : string = "http://foo bar/"

View 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 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 Do
Nop
End
End
encoded = tail
End e_
Return decoded
End
Exit

View file

@ -0,0 +1,5 @@
class UrlDecode {
function : Main(args : String[]) ~ Nil {
Net.UrlUtility->Decode("http%3A%2F%2Ffoo%20bar%2F")->PrintLine();
}
}

View file

@ -0,0 +1,3 @@
NSString *encoded = @"http%3A%2F%2Ffoo%20bar%2F";
NSString *normal = [encoded stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%@", normal);

View file

@ -0,0 +1,5 @@
<?php
$encoded = "http%3A%2F%2Ffoo%20bar%2F";
$unencoded = rawurldecode($encoded);
echo "The unencoded string is $unencoded !\n";
?>

View file

@ -0,0 +1,5 @@
my $url = "http%3A%2F%2Ffoo%20bar%2F";
say $url.subst: :g,
/'%'(<:hexdigit>**2)/,
-> ($ord ) { chr(:16(~$ord)) }

View file

@ -0,0 +1,2 @@
$url ~~ s:g[ '%' (<:hexdigit> ** 2) ] = chr :16(~$0);
say $url;

View 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";

View 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" ;

View file

@ -0,0 +1,3 @@
URL$ = URLDecoder("http%3A%2F%2Ffoo%20bar%2F")
Debug URL$ ; http://foo bar/

View file

@ -0,0 +1,2 @@
import urllib
print urllib.unquote("http%3A%2F%2Ffoo%20bar%2F")

View 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

View file

@ -0,0 +1,32 @@
/*REXX pgm convert an 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 subroutine────────────────*/
decodeURL: procedure; parse arg encoded; decoded=''
encoded=translate(encoded,,'+') /*special case for encoded blank.*/
do while length(encoded)\==0
parse var encoded head '%' +1 code +2 tail
decoded=decoded || head
select
when length(strip(code,'T'))==2 &,
datatype(code,'X') then decoded=decoded || x2c(code)
when length(strip(code,'T'))\==0 then do
decoded=decoded'%'
tail=code || tail
end
otherwise nop
end /*select*/
encoded=tail
end /*while*/
return decoded

View file

@ -0,0 +1,21 @@
/*REXX pgm convert an 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
say url.j
say URLdecode(url.j)
end /*j*/
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────URLDECODE subroutine────────────────*/
URLdecode: procedure; parse arg yyy /*get encoded URL from arg list. */
yyy=translate(yyy,,'+') /*special case for 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

View file

@ -0,0 +1,3 @@
#lang racket
(require net/uri-codec)
(uri-decode "http%3A%2F%2Ffoo%20bar%2F")

View 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

View file

@ -0,0 +1,3 @@
require 'cgi'
puts CGI.unescape("http%3A%2F%2Ffoo%20bar%2F")
# => "http://foo bar/"

View file

@ -0,0 +1,3 @@
require 'uri'
puts URI.decode_www_form_component("http%3A%2F%2Ffoo%20bar%2F")
# => "http://foo bar/"

View file

@ -0,0 +1,4 @@
import java.net._
val encoded="http%3A%2F%2Ffoo%20bar%2F"
val decoded=URLDecoder.decode(encoded, "UTF-8")
println(decoded) // -> http://foo bar/

View 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;

View file

@ -0,0 +1,8 @@
$$ MODE TUSCRIPT
url_encoded="http%3A%2F%2Ffoo%20bar%2F"
BUILD S_TABLE hex=":%><:><2<>2<%:"
hex=STRINGS (url_encoded,hex), hex=SPLIT(hex)
hex=DECODE (hex,hex)
url_decoded=SUBSTITUTE(url_encoded,":%><2<>2<%:",0,0,hex)
PRINT "encoded: ", url_encoded
PRINT "decoded: ", url_decoded

View file

@ -0,0 +1,7 @@
proc urlDecode {str} {
set specialMap {"[" "%5B" "]" "%5D"}
set seqRE {%([0-9a-fA-F]{2})}
set replacement {[format "%c" [scan "\1" "%2x"]]}
set modStr [regsub -all $seqRE [string map $specialMap $str] $replacement]
return [encoding convertfrom utf-8 [subst -nobackslash -novariable $modStr]]
}

View file

@ -0,0 +1 @@
puts [urlDecode "http%3A%2F%2Ffoo%20bar%2F"]

View file

@ -0,0 +1,64 @@
function urldecode
{
typeset encoded=$1 decoded= rest= c= c1= c2=
typeset rest2= bug='rest2=${rest}'
if [[ -z ${BASH_VERSION:-} ]]; then
typeset -i16 hex=0; typeset -i8 oct=0
# bug /usr/bin/sh HP-UX 11.00
typeset _encoded='xyz%26xyz'
rest="${_encoded#?}"
c="${_encoded%%${rest}}"
if (( ${#c} != 1 )); then
typeset qm='????????????????????????????????????????????????????????????????????????'
typeset bug='(( ${#rest} > 0 )) && typeset -L${#rest} rest2="${qm}" || rest2=${rest}'
fi
fi
rest="${encoded#?}"
eval ${bug}
c="${encoded%%${rest2}}"
encoded="${rest}"
while [[ -n ${c} ]]; do
if [[ ${c} = '%' ]]; then
rest="${encoded#?}"
eval ${bug}
c1="${encoded%%${rest2}}"
encoded="${rest}"
rest="${encoded#?}"
eval ${bug}
c2="${encoded%%${rest2}}"
encoded="${rest}"
if [[ -z ${c1} || -z ${c2} ]]; then
c="%${c1}${c2}"
echo "WARNING: invalid % encoding: ${c}" >&2
elif [[ -n ${BASH_VERSION:-} ]]; then
c="\\x${c1}${c2}"
c=$(\echo -e "${c}")
else
hex="16#${c1}${c2}"; oct=hex
c="\\0${oct#8\#}"
c=$(print -- "${c}")
fi
elif [[ ${c} = '+' ]]; then
c=' '
fi
decoded="${decoded}${c}"
rest="${encoded#?}"
eval ${bug}
c="${encoded%%${rest2}}"
encoded="${rest}"
done
if [[ -n ${BASH_VERSION:-} ]]; then
\echo -E "${decoded}"
else
print -r -- "${decoded}"
fi
}

View file

@ -0,0 +1,23 @@
code Text=12;
string 0; \use zero-terminated strings
func Decode(S0); \Decode URL string and return its address
char S0;
char S1(80); \BEWARE: very temporary string space returned
int C, N, I, J;
[I:= 0; J:= 0;
repeat C:= S0(I); I:= I+1; \get char
if C=^% then \convert hex to char
[C:= S0(I); I:= I+1;
if C>=^a then C:= C & ~$20; \convert to uppercase
N:= C - (if C<=^9 then ^0 else ^A-10);
C:= S0(I); I:= I+1;
if C>=^a then C:= C & ~$20;
C:= N*16 + C - (if C<=^9 then ^0 else ^A-10);
];
S1(J):= C; J:= J+1; \put char in output string
until C=0;
return S1;
];
Text(0, Decode("http%3A%2F%2Ffoo%20bar%2f"))