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,31 @@
The task is to 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.
'''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>".
* Some standards give different rules: RFC 3986, ''Uniform Resource Identifier (URI): Generic Syntax'', section 2.3, says that "-._~" should not be encoded. 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 "+". The options below provide for utilization of an exception string, enabling preservation (non encoding) of particular characters to meet specific standards.
'''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.
'''See also'''
[[URL decoding]]
[[Category:String manipulation]]

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

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

View file

@ -0,0 +1,7 @@
rawURL = http://foo bar/
SetFormat, Integer, Hex
Loop Parse, rawURL
If A_LoopField is not alnum ; not a-zA-Z0-9
encURL .= "%" . SubStr(Asc(A_LoopField), 3)
else encURL .= A_LoopField
MsgBox % encURL

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

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

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

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

View 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(unsigned 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()
{
unsigned char url[] = "http://foo bar/";
char enc[sizeof(url) * 3];
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;
}

View file

@ -0,0 +1,5 @@
import std.stdio, std.uri;
void main() {
writeln(encodeComponent("http://foo bar/"));
}

View file

@ -0,0 +1,10 @@
package main
import (
"fmt"
"net/url"
)
func main() {
fmt.Println(url.QueryEscape("http://foo bar/"))
}

View 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

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

View file

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

View file

@ -0,0 +1,12 @@
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
public class Main
{
public static void main(String[] args) throws UnsupportedEncodingException
{
String normal = "http://foo bar/";
String encoded = URLEncoder.encode(normal, "utf-8");
System.out.println(encoded);
}
}

View file

@ -0,0 +1,2 @@
var normal = 'http://foo/bar/';
var encoded = encodeURIComponent(normal);

View 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

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

View file

@ -0,0 +1 @@
URLEncoding["http://foo bar/"]

View 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

View file

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

View file

@ -0,0 +1,10 @@
use FastCgi;
bundle Default {
class UrlEncode {
function : Main(args : String[]) ~ Nil {
url := "http://foo bar/";
UrlUtility->Encode(url)->PrintLine();
}
}
}

View file

@ -0,0 +1,3 @@
NSString *normal = @"http://foo bar/";
NSString *encoded = [normal stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%@", encoded);

View file

@ -0,0 +1,4 @@
<?php
$s = 'http://foo/bar/';
$s = rawurlencode($s);
?>

View file

@ -0,0 +1,3 @@
my $url = 'http://foo bar/';
say $url.subst(/<-[ A..Z a..z 0..9 ]>/, *.ord.fmt("%%%02X"), :g);

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

View file

@ -0,0 +1,4 @@
use URI::Escape;
my $s = 'http://foo/bar/';
print uri_escape($s);

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

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

View file

@ -0,0 +1 @@
URL$ = URLEncoder("http://foo bar/")

View file

@ -0,0 +1,3 @@
import urllib
s = 'http://foo/bar/'
s = urllib.quote(s)

View file

@ -0,0 +1,3 @@
Dim URL As String = "http://foo bar/"
URL = EncodeURLComponent(URL)
Print(URL)

View file

@ -0,0 +1,13 @@
Function URLEncode(URL As String, Exceptions As String = "") As String
For i As Integer = 0 To 255
If InStr(Exceptions, Chr(i)) > 0 Then Continue For i
Dim char As String = Chr(127) + Right("00" + Hex(i), 2)
URL = ReplaceAll(URL, Chr(i), char)
If i = 47 Then i = 57
If i = 64 Then i = 90
If i = 96 Then i = 122
If i = 126 Then i = 128
Next
URL = ReplaceAll(URL, Chr(127), "%")
Return URL
End Function

View 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

View file

@ -0,0 +1,31 @@
/*REXX pgm encodes an URL text, blanks──►+, preserves -._* and -._~ */
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'
URLs=4
do j=1 for URLs; say
say url.j
say URLencode(url.j)
end /*j*/
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────URENCODE subroutine─────────────────*/
URLencode: procedure; parse arg yyy; t1= '-._~' ; skip=0
t2= '-._*' ; z=''
do k=1 for length(yyy); _=substr(yyy,k,1) /*pickoff 1char*/
if skip\==0 then do /*skip t1 | t2?*/
skip=skip-1 /*skip a char. */
iterate
end
select
when datatype(_,'A') then z=z || _ /*alphanumeric?*/
when _==' ' then z=z'+' /*is a blank ? */
when substr(yyy,k,4)==t1 |, /*t1 or t2 ? */
substr(yyy,k,4)==t2 then do; skip=3 /*skip 3 chars.*/
z=z || substr(yyy,k,4)
end
otherwise z=z'%'c2x(_) /*special char.*/
end /*select*/
end /*k*/
return z

View file

@ -0,0 +1,3 @@
#lang racket
(require net/uri-codec)
(uri-encode "http://foo bar/")

View file

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

View file

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

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

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

View 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

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

View file

@ -0,0 +1 @@
puts [urlEncode "http://foo bar/"]

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

View 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/"))