September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -1,3 +1,4 @@
---
category:
- String manipulation
- Encodings

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

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

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

View file

@ -0,0 +1,29 @@
--
-- demo\rosetta\encode_url.exw
-- ===========================
--
function nib(integer b)
return b+iff(b<=9?'0':'A'-10)
end function
function encode_url(string s, string exclusions="", integer spaceplus=0)
string res = ""
for i=1 to length(s) do
integer ch = s[i]
if ch=' ' and spaceplus then
ch = '+'
elsif not find(ch,exclusions)
and (ch<'0'
or (ch>'9' and ch<'A')
or (ch>'Z' and ch<'a')
or ch>'z') then
res &= '%'
res &= nib(floor(ch/#10))
ch = nib(and_bits(ch,#0F))
end if
res &= ch
end for
return res
end function
printf(1,"%s\n",{encode_url("http://foo bar/")})

View file

@ -0,0 +1,3 @@
[uri]::EscapeDataString('http://foo bar/')
http%3A%2F%2Ffoo%20bar%2F

View file

@ -1,31 +1,28 @@
/*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.*/
/*──────────────────────────────────URLENCODE 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
/*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

View file

@ -0,0 +1,2 @@
var CURL=Import("zklCurl");
CURL.urlEncode("http://foo bar/") //--> "http%3A%2F%2Ffoo%20bar%2F"