Differences From
Artifact [ee4d50abb4]:
1 1 -- vim: ft=terra
2 2 local m={}
3 3 local pstr = lib.mem.ptr(int8)
4 4
5 5 terra m.sanitize(txt: pstr, quo: bool)
6 + if txt.ptr == nil then return pstr.null() end
7 + if txt.ct == 0 then txt.ct = lib.str.sz(txt.ptr) end
6 8 var a: lib.str.acc a:init(txt.ct*1.3)
7 9 for i=0,txt.ct do
8 10 if txt(i) == @'<' then a:lpush('<')
9 11 elseif txt(i) == @'>' then a:lpush('>')
10 12 elseif txt(i) == @'&' then a:lpush('&')
11 13 elseif quo and txt(i) == @'"' then a:lpush('"')
12 14 else a:push(&txt(i),1) end
13 15 end
14 16 return a:finalize()
15 17 end
18 +
19 +terra m.hexdgt(i: uint8)
20 + if i >= 10 then
21 + return @'A' + (i - 10)
22 + else return 0x30 + i end
23 +end
24 +
25 +terra m.hexbyte(i: uint8): int8[2]
26 + return arrayof(int8,
27 + m.hexdgt(i / 0x10),
28 + m.hexdgt(i % 0x10))
29 +end
30 +
31 +terra m.urlenc(txt: pstr, qparam: bool)
32 + if txt.ptr == nil then return pstr.null() end
33 + if txt.ct == 0 then txt.ct = lib.str.sz(txt.ptr) end
34 + var a: lib.str.acc a:init(txt.ct*1.3)
35 + for i=0,txt.ct do
36 + if txt(i) == @' ' then a:lpush('+')
37 + elseif txt(i) == @'&' and not qparam then a:lpush('&')
38 + elseif (txt(i) < 0x2c or
39 + (txt(i) > @';' and txt(i) < @'@') or
40 + (txt(i) > @'Z' and txt(i) < @'a') or
41 + (txt(i) >= 0x7b and txt(i) <= 0x7f)) and
42 + txt(i) ~= @'_' and (qparam == true or txt(i) ~= @'=') then
43 + var str = m.hexbyte(txt(i))
44 + a:lpush('%'):push(&str[0], 2)
45 + else a:push(&txt(i),1) end
46 + end
47 + return a:finalize()
48 +end
16 49
17 50 return m