merge ws/perf-core
This commit is contained in:
commit
de755ca036
37 files changed
+5317
-2562
No files matched your search
@@ -202,6 +202,13 @@ function Decode.decompress(src: buffer, off: number, len: number, maxBytes: numb
|
||||
local bb, bc = 0, 0
|
||||
|
||||
local function fill()
|
||||
if bc <= 24 and ip + 4 <= lim then
|
||||
bb = bor(bb, lshift(readu32(src, ip), bc))
|
||||
local nb = rshift(32 - bc, 3)
|
||||
ip += nb
|
||||
bc += nb * 8
|
||||
return
|
||||
end
|
||||
while bc <= 24 do
|
||||
local byte = 0
|
||||
if ip < lim then
|
||||
|
||||
@@ -11,9 +11,6 @@ local bcreate, bcopy, bfill, blen = buffer.create, buffer.copy, buffer.fill, buf
|
||||
|
||||
local Decode = {}
|
||||
|
||||
-- CRC-32/BZIP2 table (polynomial 0x04C11DB7, not reflected: core/Crc32)
|
||||
local CT = Crc32.MSB_TABLE
|
||||
Decode.crcTable = CT
|
||||
|
||||
local MASK = table.create(33, 0)
|
||||
for i = 0, 32 do
|
||||
@@ -413,8 +410,12 @@ function Decode.decode(b: buffer, o: number, e: number, maxBytes: number): (buff
|
||||
return nil, "block larger than its declared size", "corrupt"
|
||||
end
|
||||
local v = MTF[idx + 1]
|
||||
for k = idx, 1, -1 do
|
||||
MTF[k + 1] = MTF[k]
|
||||
if idx > 8 then
|
||||
table.move(MTF, 1, idx, 2)
|
||||
else
|
||||
for k = idx, 1, -1 do
|
||||
MTF[k + 1] = MTF[k]
|
||||
end
|
||||
end
|
||||
MTF[1] = v
|
||||
local ch = SEQ[v + 1]
|
||||
@@ -447,7 +448,7 @@ function Decode.decode(b: buffer, o: number, e: number, maxBytes: number): (buff
|
||||
out = grow(out, op, op + nblock + 4096)
|
||||
end
|
||||
local cap = blen(out)
|
||||
local crc = 0xFFFFFFFF
|
||||
local blockStart = op
|
||||
local tPos = rshift(readu32(T, origPtr * 4), 8)
|
||||
local last, run = -1, 0
|
||||
local i = 0
|
||||
@@ -468,9 +469,6 @@ function Decode.decode(b: buffer, o: number, e: number, maxBytes: number): (buff
|
||||
end
|
||||
bfill(out, op, last, ch)
|
||||
op += ch
|
||||
for _ = 1, ch do
|
||||
crc = bxor(lshift(crc, 8), CT[bxor(rshift(crc, 24), last) + 1])
|
||||
end
|
||||
end
|
||||
run = 0
|
||||
last = -1
|
||||
@@ -490,13 +488,12 @@ function Decode.decode(b: buffer, o: number, e: number, maxBytes: number): (buff
|
||||
end
|
||||
writeu8(out, op, ch)
|
||||
op += 1
|
||||
crc = bxor(lshift(crc, 8), CT[bxor(rshift(crc, 24), ch) + 1])
|
||||
end
|
||||
end
|
||||
if op > maxBytes then
|
||||
return nil, "output larger than maxBytes", "limit"
|
||||
end
|
||||
crc = bxor(crc, 0xFFFFFFFF)
|
||||
local crc = bxor(Crc32.msb(out, blockStart, op - blockStart, 0xFFFFFFFF), 0xFFFFFFFF)
|
||||
if crc ~= storedCrc then
|
||||
return nil, string.format("block CRC mismatch (%08x, expected %08x)", crc, storedCrc), "checksum"
|
||||
end
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
-- Written from the format description (no third-party code); randomised blocks (bzip2 0.9.0 and older) are unsupported.
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Bytes = require("@rosetta/core/Bytes")
|
||||
local Crc32 = require("@rosetta/core/Crc32")
|
||||
local Decode = require("@rosetta/codecs/compress/Bzip2/Decode")
|
||||
|
||||
local Bzip2 = {}
|
||||
@@ -33,14 +34,9 @@ function Bzip2.decompress(buf: buffer | string, offset: number?, length: number?
|
||||
end
|
||||
|
||||
function Bzip2.crc(buf: buffer, offset: number?, length: number?): number
|
||||
local CT = Decode.crcTable
|
||||
local o = offset or 0
|
||||
local e = o + (length or (buffer.len(buf) - o))
|
||||
local c = 0xFFFFFFFF
|
||||
for i = o, e - 1 do
|
||||
c = bit32.bxor(bit32.lshift(c, 8), CT[bit32.bxor(bit32.rshift(c, 24), buffer.readu8(buf, i)) + 1])
|
||||
end
|
||||
return bit32.bxor(c, 0xFFFFFFFF)
|
||||
local n = length or (buffer.len(buf) - o)
|
||||
return bit32.bxor(Crc32.msb(buf, o, n, 0xFFFFFFFF), 0xFFFFFFFF)
|
||||
end
|
||||
|
||||
return Bzip2
|
||||
@@ -5,7 +5,7 @@
|
||||
-- block the cheapest of stored / fixed / dynamic Huffman, as zlib's levels.
|
||||
local Huffman = require("@rosetta/codecs/entropy/Huffman")
|
||||
|
||||
local readu8, writeu8, writeu16 = buffer.readu8, buffer.writeu8, buffer.writeu16
|
||||
local readu8, readu32, writeu8, writeu16 = buffer.readu8, buffer.readu32, buffer.writeu8, buffer.writeu16
|
||||
local bcreate, bcopy, blen = buffer.create, buffer.copy, buffer.len
|
||||
local floor = math.floor
|
||||
|
||||
@@ -136,27 +136,72 @@ local function storedBlock(o: Out, src: buffer, s: number, e: number, final: boo
|
||||
end
|
||||
|
||||
local function emitSymbols(o: Out, L: { number }, D: { number }, nsym: number, lcode: { number }, llen: { number }, dcodes: { number }, dlen: { number })
|
||||
ensure(o, nsym * 6 + 8) -- at most 15 + 5 + 15 + 13 bits a symbol
|
||||
local buf, op, acc, cnt = o.buf, o.op, o.acc, o.cnt
|
||||
-- acc holds cnt < 16 pending bits; each field is at most 15 bits, so acc stays below 2^31
|
||||
for i = 1, nsym do
|
||||
local l = L[i]
|
||||
if l < 256 then
|
||||
put(o, lcode[l + 1], llen[l + 1])
|
||||
acc += lcode[l + 1] * POW[cnt + 1]
|
||||
cnt += llen[l + 1]
|
||||
if cnt >= 16 then
|
||||
writeu16(buf, op, acc % 65536)
|
||||
acc //= 65536
|
||||
op += 2
|
||||
cnt -= 16
|
||||
end
|
||||
else
|
||||
local len = l - 256
|
||||
local c = LCODE[len]
|
||||
put(o, lcode[c + 1], llen[c + 1])
|
||||
acc += lcode[c + 1] * POW[cnt + 1]
|
||||
cnt += llen[c + 1]
|
||||
if cnt >= 16 then
|
||||
writeu16(buf, op, acc % 65536)
|
||||
acc //= 65536
|
||||
op += 2
|
||||
cnt -= 16
|
||||
end
|
||||
local xb = LEXTRA[len]
|
||||
if xb > 0 then
|
||||
put(o, len - LBASEOF[len], xb)
|
||||
acc += (len - LBASEOF[len]) * POW[cnt + 1]
|
||||
cnt += xb
|
||||
if cnt >= 16 then
|
||||
writeu16(buf, op, acc % 65536)
|
||||
acc //= 65536
|
||||
op += 2
|
||||
cnt -= 16
|
||||
end
|
||||
end
|
||||
local d = D[i]
|
||||
local dc = dcode(d)
|
||||
put(o, dcodes[dc + 1], dlen[dc + 1])
|
||||
local dc = if d <= 256 then DSMALL[d] else DLARGE[(d - 1) // 128 + 1]
|
||||
acc += dcodes[dc + 1] * POW[cnt + 1]
|
||||
cnt += dlen[dc + 1]
|
||||
if cnt >= 16 then
|
||||
writeu16(buf, op, acc % 65536)
|
||||
acc //= 65536
|
||||
op += 2
|
||||
cnt -= 16
|
||||
end
|
||||
xb = DEXT[dc + 1]
|
||||
if xb > 0 then
|
||||
put(o, d - DBASE[dc + 1], xb)
|
||||
acc += (d - DBASE[dc + 1]) * POW[cnt + 1]
|
||||
cnt += xb
|
||||
if cnt >= 16 then
|
||||
writeu16(buf, op, acc % 65536)
|
||||
acc //= 65536
|
||||
op += 2
|
||||
cnt -= 16
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
while cnt >= 8 do
|
||||
writeu8(buf, op, acc % 256)
|
||||
acc //= 256
|
||||
op += 1
|
||||
cnt -= 8
|
||||
end
|
||||
o.op, o.acc, o.cnt = op, acc, cnt
|
||||
put(o, lcode[257], llen[257]) -- end of block
|
||||
end
|
||||
|
||||
@@ -319,11 +364,6 @@ function Compress.raw(src: buffer, off: number, len: number, level: number): buf
|
||||
local function hash(p: number): number
|
||||
return ((readu8(src, p) * 251 + readu8(src, p + 1)) * 251 + readu8(src, p + 2)) % HSIZE
|
||||
end
|
||||
local function insert(p: number)
|
||||
local h = hash(p) + 1
|
||||
prev[p % WSIZE + 1] = head[h]
|
||||
head[h] = p
|
||||
end
|
||||
local function find(p: number, steps: number): (number, number)
|
||||
local maxLen = math.min(MAXMATCH, e - p)
|
||||
if maxLen < 3 then
|
||||
@@ -332,9 +372,20 @@ function Compress.raw(src: buffer, off: number, len: number, level: number): buf
|
||||
local best, bestDist = 2, 0
|
||||
local c = head[hash(p) + 1]
|
||||
local limit = p - WSIZE
|
||||
while c >= 0 and c > limit and steps > 0 do
|
||||
if readu8(src, c + best) == readu8(src, p + best) and readu8(src, c) == readu8(src, p) then
|
||||
local p0, pb = readu8(src, p), readu8(src, p + 2)
|
||||
local max4 = maxLen - 4
|
||||
if limit < -1 then
|
||||
limit = -1
|
||||
end
|
||||
for _ = 1, steps do
|
||||
if c <= limit then
|
||||
break
|
||||
end
|
||||
if readu8(src, c + best) == pb and readu8(src, c) == p0 then
|
||||
local k = 1
|
||||
while k <= max4 and readu32(src, c + k) == readu32(src, p + k) do
|
||||
k += 4
|
||||
end
|
||||
while k < maxLen and readu8(src, c + k) == readu8(src, p + k) do
|
||||
k += 1
|
||||
end
|
||||
@@ -343,6 +394,7 @@ function Compress.raw(src: buffer, off: number, len: number, level: number): buf
|
||||
if k >= nice or k >= maxLen then
|
||||
break
|
||||
end
|
||||
pb = readu8(src, p + k)
|
||||
end
|
||||
end
|
||||
local nc = prev[c % WSIZE + 1]
|
||||
@@ -350,7 +402,6 @@ function Compress.raw(src: buffer, off: number, len: number, level: number): buf
|
||||
break
|
||||
end
|
||||
c = nc
|
||||
steps -= 1
|
||||
end
|
||||
if best < 3 then
|
||||
return 0, 0
|
||||
@@ -369,7 +420,9 @@ function Compress.raw(src: buffer, off: number, len: number, level: number): buf
|
||||
local mlen, mdist = 0, 0
|
||||
if p <= stop3 then
|
||||
mlen, mdist = find(p, maxChain)
|
||||
insert(p)
|
||||
local h = ((readu8(src, p) * 251 + readu8(src, p + 1)) * 251 + readu8(src, p + 2)) % HSIZE + 1
|
||||
prev[p % WSIZE + 1] = head[h]
|
||||
head[h] = p
|
||||
if mlen >= 3 and mlen < lazyMax and p + 1 <= stop3 then
|
||||
local l2, d2 = find(p + 1, if mlen >= good then maxChain // 4 else maxChain)
|
||||
if l2 > mlen then
|
||||
@@ -377,7 +430,9 @@ function Compress.raw(src: buffer, off: number, len: number, level: number): buf
|
||||
L[nsym] = readu8(src, p)
|
||||
D[nsym] = 0
|
||||
p += 1
|
||||
insert(p)
|
||||
h = ((readu8(src, p) * 251 + readu8(src, p + 1)) * 251 + readu8(src, p + 2)) % HSIZE + 1
|
||||
prev[p % WSIZE + 1] = head[h]
|
||||
head[h] = p
|
||||
mlen, mdist = l2, d2
|
||||
end
|
||||
end
|
||||
@@ -388,9 +443,16 @@ function Compress.raw(src: buffer, off: number, len: number, level: number): buf
|
||||
D[nsym] = mdist
|
||||
local q = p + 1
|
||||
local qe = math.min(p + mlen, stop3 + 1)
|
||||
while q < qe do
|
||||
insert(q)
|
||||
q += 1
|
||||
if q < qe then
|
||||
local b1, b2 = readu8(src, q), readu8(src, q + 1)
|
||||
while q < qe do
|
||||
local b3 = readu8(src, q + 2)
|
||||
local h = ((b1 * 251 + b2) * 251 + b3) % HSIZE + 1
|
||||
prev[q % WSIZE + 1] = head[h]
|
||||
head[h] = q
|
||||
b1, b2 = b2, b3
|
||||
q += 1
|
||||
end
|
||||
end
|
||||
p += mlen
|
||||
else
|
||||
|
||||
@@ -23,6 +23,19 @@ local DEXT = { 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9,
|
||||
-- RFC 1951 3.2.7: the order the code-length code lengths are sent in
|
||||
local CLORDER = { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 }
|
||||
|
||||
-- table symbols: literals 0..255, end of block 256, invalid 257..511 (codes 286 / 287), lengths 512 + base * 8 + extra
|
||||
-- bits; distances base * 16 + extra bits
|
||||
local LMAP, DMAP = table.create(288, 300), table.create(30, 0)
|
||||
for i = 0, 256 do
|
||||
LMAP[i + 1] = i
|
||||
end
|
||||
for i = 1, 29 do
|
||||
LMAP[257 + i] = 512 + LBASE[i] * 8 + LEXT[i]
|
||||
end
|
||||
for i = 1, 30 do
|
||||
DMAP[i] = DBASE[i] * 16 + DEXT[i]
|
||||
end
|
||||
|
||||
local LITCAP = Huffman.capacity(288, LITBITS, 15)
|
||||
local DISTCAP = Huffman.capacity(32, DISTBITS, 15)
|
||||
local CLCAP = Huffman.capacity(19, CLBITS, 7)
|
||||
@@ -35,12 +48,15 @@ do
|
||||
bfill(l, 144, 9, 112)
|
||||
bfill(l, 256, 7, 24)
|
||||
bfill(l, 280, 8, 8)
|
||||
FLITS = Huffman.buildLsb(l, 0, 288, FLIT, LITBITS)
|
||||
FLITS = Huffman.buildLsbMap(l, 0, 288, FLIT, LITBITS, LMAP)
|
||||
local d = bcreate(30)
|
||||
bfill(d, 0, 5, 30)
|
||||
FDISTS = Huffman.buildLsb(d, 0, 30, FDIST, DISTBITS)
|
||||
FDISTS = Huffman.buildLsbMap(d, 0, 30, FDIST, DISTBITS, DMAP)
|
||||
end
|
||||
|
||||
-- scratch tables for dynamic blocks, shared by every call (raw never yields)
|
||||
local SLIT, SDIST, SCL, SLENS = bcreate(LITCAP * 4), bcreate(DISTCAP * 4), bcreate(CLCAP * 4), bcreate(320)
|
||||
|
||||
local Inflate = {}
|
||||
|
||||
local function grow(out: buffer, op: number, need: number): (buffer, number)
|
||||
@@ -62,9 +78,9 @@ function Inflate.raw(src: buffer, start: number, endpos: number, cap: number, ex
|
||||
end
|
||||
local pos = start
|
||||
local bitbuf, bitcnt, over = 0, 0, 0
|
||||
local lt, dt = bcreate(LITCAP * 4), bcreate(DISTCAP * 4) :: buffer
|
||||
local clt = bcreate(CLCAP * 4)
|
||||
local lens = bcreate(320)
|
||||
local lt, dt = SLIT, SDIST
|
||||
local clt = SCL
|
||||
local lens = SLENS
|
||||
local final = 0
|
||||
repeat
|
||||
-- block header (3 bits); also up to 14 bits of a dynamic header
|
||||
@@ -133,9 +149,7 @@ function Inflate.raw(src: buffer, start: number, endpos: number, cap: number, ex
|
||||
lt, dt = FLIT, FDIST
|
||||
lsub, dsub = FLITS, FDISTS
|
||||
elseif btype == 2 then
|
||||
if lt == FLIT then
|
||||
lt, dt = bcreate(LITCAP * 4), bcreate(DISTCAP * 4) :: buffer
|
||||
end
|
||||
lt, dt = SLIT, SDIST
|
||||
local hlit = band(bitbuf, 31) + 257
|
||||
local hdist = band(rshift(bitbuf, 5), 31) + 1
|
||||
local hclen = band(rshift(bitbuf, 10), 15) + 4
|
||||
@@ -240,8 +254,8 @@ function Inflate.raw(src: buffer, start: number, endpos: number, cap: number, ex
|
||||
if readu8(lens, 256) == 0 then
|
||||
return nil, "missing end-of-block code"
|
||||
end
|
||||
lsub = Huffman.buildLsb(lens, 0, hlit, lt, LITBITS)
|
||||
dsub = Huffman.buildLsb(lens, hlit, hdist, dt, DISTBITS)
|
||||
lsub = Huffman.buildLsbMap(lens, 0, hlit, lt, LITBITS, LMAP)
|
||||
dsub = Huffman.buildLsbMap(lens, hlit, hdist, dt, DISTBITS, DMAP)
|
||||
if lsub < 0 or dsub < 0 then
|
||||
return nil, "invalid literal/length or distance code set"
|
||||
end
|
||||
@@ -299,13 +313,12 @@ function Inflate.raw(src: buffer, start: number, endpos: number, cap: number, ex
|
||||
end
|
||||
writeu8(out, op, sym)
|
||||
op += 1
|
||||
elseif sym == 256 then
|
||||
break
|
||||
else
|
||||
sym -= 256
|
||||
if sym > 29 then
|
||||
return nil, "invalid literal/length code"
|
||||
elseif sym < 512 then
|
||||
if sym == 256 then
|
||||
break
|
||||
end
|
||||
return nil, "invalid literal/length code"
|
||||
else
|
||||
if bitcnt < 20 then
|
||||
if pos + 4 <= endpos then
|
||||
bitbuf = bor(bitbuf, lshift(readu32(src, pos), bitcnt))
|
||||
@@ -327,8 +340,8 @@ function Inflate.raw(src: buffer, start: number, endpos: number, cap: number, ex
|
||||
end
|
||||
end
|
||||
end
|
||||
local len = LBASE[sym]
|
||||
local xb = LEXT[sym]
|
||||
local len = rshift(sym, 3) - 64
|
||||
local xb = band(sym, 7)
|
||||
if xb > 0 then
|
||||
len += band(bitbuf, lshift(1, xb) - 1)
|
||||
bitbuf = rshift(bitbuf, xb)
|
||||
@@ -348,12 +361,9 @@ function Inflate.raw(src: buffer, start: number, endpos: number, cap: number, ex
|
||||
end
|
||||
bitbuf = rshift(bitbuf, l)
|
||||
bitcnt -= l
|
||||
local dsym = rshift(e, 5) + 1
|
||||
if dsym > 30 then
|
||||
return nil, "invalid distance code"
|
||||
end
|
||||
local dist = DBASE[dsym]
|
||||
xb = DEXT[dsym]
|
||||
local dsym = rshift(e, 5)
|
||||
local dist = rshift(dsym, 4)
|
||||
xb = band(dsym, 15)
|
||||
if xb > 0 then
|
||||
if bitcnt < 13 then
|
||||
if pos + 4 <= endpos then
|
||||
@@ -394,8 +404,11 @@ function Inflate.raw(src: buffer, start: number, endpos: number, cap: number, ex
|
||||
end
|
||||
local s = op - dist
|
||||
if dist >= len then
|
||||
if len <= 16 and op + 16 <= cap then
|
||||
-- straight-line 16-byte copy for simple LuaJIT traces; it reads only bytes before op or already copied ones
|
||||
if len <= 8 and op + 8 <= cap then
|
||||
-- straight-line 8- / 16-byte copies for simple LuaJIT traces; they read only bytes before op or already copied ones
|
||||
writeu32(out, op, readu32(out, s))
|
||||
writeu32(out, op + 4, readu32(out, s + 4))
|
||||
elseif len <= 16 and op + 16 <= cap then
|
||||
writeu32(out, op, readu32(out, s))
|
||||
writeu32(out, op + 4, readu32(out, s + 4))
|
||||
writeu32(out, op + 8, readu32(out, s + 8))
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
-- codecs/compress/Lzma/Crc64: CRC-64/XZ (ECMA-182 polynomial, reflected: 0xC96C5795D7870F42), xz check type 4.
|
||||
-- Written from the xz file format specification; the 64-bit value is kept as two u32 halves (hi, lo).
|
||||
local band, bxor, rshift, lshift, bor = bit32.band, bit32.bxor, bit32.rshift, bit32.lshift, bit32.bor
|
||||
local readu8 = buffer.readu8
|
||||
local readu8, readu32 = buffer.readu8, buffer.readu32
|
||||
|
||||
local THI, TLO = table.create(256, 0), table.create(256, 0)
|
||||
for n = 0, 255 do
|
||||
@@ -21,14 +21,43 @@ for n = 0, 255 do
|
||||
THI[n + 1], TLO[n + 1] = hi, lo
|
||||
end
|
||||
|
||||
-- slice-by-4: H[k] / L[k][n] = the register after byte n and k zero bytes
|
||||
local H1, H2, H3 = table.create(256, 0), table.create(256, 0), table.create(256, 0)
|
||||
local L1, L2, L3 = table.create(256, 0), table.create(256, 0), table.create(256, 0)
|
||||
for n = 0, 255 do
|
||||
local hi, lo = THI[n + 1], TLO[n + 1]
|
||||
for k = 1, 3 do
|
||||
local idx = band(lo, 255) + 1
|
||||
lo = bxor(TLO[idx], bor(rshift(lo, 8), lshift(band(hi, 255), 24)))
|
||||
hi = bxor(THI[idx], rshift(hi, 8))
|
||||
if k == 1 then
|
||||
H1[n + 1], L1[n + 1] = hi, lo
|
||||
elseif k == 2 then
|
||||
H2[n + 1], L2[n + 1] = hi, lo
|
||||
else
|
||||
H3[n + 1], L3[n + 1] = hi, lo
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local Crc64 = {}
|
||||
|
||||
function Crc64.of(b: buffer, o: number, n: number): (number, number)
|
||||
local hi, lo = 0xFFFFFFFF, 0xFFFFFFFF
|
||||
for i = o, o + n - 1 do
|
||||
local i, e = o, o + n
|
||||
local e4 = e - 4
|
||||
while i <= e4 do
|
||||
local w = bxor(lo, readu32(b, i))
|
||||
local a, c, d, f = band(w, 255) + 1, band(rshift(w, 8), 255) + 1, band(rshift(w, 16), 255) + 1, rshift(w, 24) + 1
|
||||
lo = bxor(L3[a], L2[c], L1[d], TLO[f], hi)
|
||||
hi = bxor(H3[a], H2[c], H1[d], THI[f])
|
||||
i += 4
|
||||
end
|
||||
while i < e do
|
||||
local idx = band(bxor(lo, readu8(b, i)), 255) + 1
|
||||
lo = bxor(TLO[idx], bor(rshift(lo, 8), lshift(band(hi, 255), 24)))
|
||||
hi = bxor(THI[idx], rshift(hi, 8))
|
||||
i += 1
|
||||
end
|
||||
return bxor(hi, 0xFFFFFFFF), bxor(lo, 0xFFFFFFFF)
|
||||
end
|
||||
|
||||
@@ -659,14 +659,24 @@ local function block(b: buffer, p: number, e: number, st: any): (string?, string
|
||||
local ml = ML_BASE[mlCode]
|
||||
local nb = ML_BITS[mlCode]
|
||||
if nb > 0 then
|
||||
ml += peek(b, s, off, nb)
|
||||
off -= nb
|
||||
local lo = off - nb
|
||||
if lo >= 0 and s + rshift(lo, 3) <= fastEnd then
|
||||
ml += band(rshift(readu32(b, s + rshift(lo, 3)), band(lo, 7)), lshift(1, nb) - 1)
|
||||
else
|
||||
ml += peek(b, s, off, nb)
|
||||
end
|
||||
off = lo
|
||||
end
|
||||
local ll = LL_BASE[llCode]
|
||||
nb = LL_BITS[llCode]
|
||||
if nb > 0 then
|
||||
ll += peek(b, s, off, nb)
|
||||
off -= nb
|
||||
local lo = off - nb
|
||||
if lo >= 0 and s + rshift(lo, 3) <= fastEnd then
|
||||
ll += band(rshift(readu32(b, s + rshift(lo, 3)), band(lo, 7)), lshift(1, nb) - 1)
|
||||
else
|
||||
ll += peek(b, s, off, nb)
|
||||
end
|
||||
off = lo
|
||||
end
|
||||
-- repeat offsets
|
||||
local offset
|
||||
@@ -699,15 +709,31 @@ local function block(b: buffer, p: number, e: number, st: any): (string?, string
|
||||
end
|
||||
-- state updates (not after the last sequence): LL, ML, OF
|
||||
if i < nbSeq then
|
||||
local lo
|
||||
nb = band(rshift(llE, 8), 255)
|
||||
llS = rshift(llE, 16) + peek(b, s, off, nb)
|
||||
off -= nb
|
||||
lo = off - nb
|
||||
if lo >= 0 and s + rshift(lo, 3) <= fastEnd then
|
||||
llS = rshift(llE, 16) + band(rshift(readu32(b, s + rshift(lo, 3)), band(lo, 7)), lshift(1, nb) - 1)
|
||||
else
|
||||
llS = rshift(llE, 16) + peek(b, s, off, nb)
|
||||
end
|
||||
off = lo
|
||||
nb = band(rshift(mlE, 8), 255)
|
||||
mlS = rshift(mlE, 16) + peek(b, s, off, nb)
|
||||
off -= nb
|
||||
lo = off - nb
|
||||
if lo >= 0 and s + rshift(lo, 3) <= fastEnd then
|
||||
mlS = rshift(mlE, 16) + band(rshift(readu32(b, s + rshift(lo, 3)), band(lo, 7)), lshift(1, nb) - 1)
|
||||
else
|
||||
mlS = rshift(mlE, 16) + peek(b, s, off, nb)
|
||||
end
|
||||
off = lo
|
||||
nb = band(rshift(ofE, 8), 255)
|
||||
ofS = rshift(ofE, 16) + peek(b, s, off, nb)
|
||||
off -= nb
|
||||
lo = off - nb
|
||||
if lo >= 0 and s + rshift(lo, 3) <= fastEnd then
|
||||
ofS = rshift(ofE, 16) + band(rshift(readu32(b, s + rshift(lo, 3)), band(lo, 7)), lshift(1, nb) - 1)
|
||||
else
|
||||
ofS = rshift(ofE, 16) + peek(b, s, off, nb)
|
||||
end
|
||||
off = lo
|
||||
end
|
||||
-- execute
|
||||
if lp + ll > litEnd then
|
||||
|
||||
@@ -38,7 +38,7 @@ end
|
||||
local count = table.create(MAXLEN + 1, 0)
|
||||
local nextc = table.create(MAXLEN + 1, 0)
|
||||
|
||||
local function build(lens: buffer, off: number, n: number, tab: buffer, P: number, msb: boolean): number
|
||||
local function build(lens: buffer, off: number, n: number, tab: buffer, P: number, msb: boolean, map: { number }?): number
|
||||
for i = 1, MAXLEN + 1 do
|
||||
count[i] = 0
|
||||
end
|
||||
@@ -76,7 +76,7 @@ local function build(lens: buffer, off: number, n: number, tab: buffer, P: numbe
|
||||
if l > 0 then
|
||||
local c = nextc[l]
|
||||
nextc[l] = c + 1
|
||||
local entry = s * 32 + l
|
||||
local entry = (if map then map[s + 1] else s) * 32 + l
|
||||
if l <= P then
|
||||
if msb then
|
||||
local first = lshift(c, P - l)
|
||||
@@ -136,6 +136,11 @@ function Huffman.buildLsb(lens: buffer, off: number, n: number, tab: buffer, P:
|
||||
return build(lens, off, n, tab, P, false)
|
||||
end
|
||||
|
||||
-- map[s + 1] replaces symbol s in the entries (values < 2^27)
|
||||
function Huffman.buildLsbMap(lens: buffer, off: number, n: number, tab: buffer, P: number, map: { number }): number
|
||||
return build(lens, off, n, tab, P, false, map)
|
||||
end
|
||||
|
||||
function Huffman.buildMsb(lens: buffer, off: number, n: number, tab: buffer, P: number): number
|
||||
return build(lens, off, n, tab, P, true)
|
||||
end
|
||||
|
||||
@@ -29,6 +29,8 @@ local ESC: { [number]: string } = {
|
||||
[116] = "\t",
|
||||
}
|
||||
|
||||
local WS: { [number]: boolean } = { [32] = true, [9] = true, [10] = true, [13] = true }
|
||||
|
||||
local utf8char = Text.utf8char
|
||||
Decode.utf8char = utf8char
|
||||
|
||||
@@ -164,7 +166,9 @@ function Decode.value(s: string, i: number, opts: Options?): (any, any, number?)
|
||||
local value: any
|
||||
|
||||
while true do
|
||||
i = match(s, "^[ \t\n\r]*()", i) :: any
|
||||
if WS[byte(s, i) or 0] then
|
||||
i = match(s, "^[ \t\n\r]*()", i) :: any
|
||||
end
|
||||
local c = byte(s, i)
|
||||
local haveValue = true
|
||||
if c == 123 then -- {
|
||||
@@ -172,7 +176,10 @@ function Decode.value(s: string, i: number, opts: Options?): (any, any, number?)
|
||||
if depth > maxDepth then
|
||||
return nil, "nested deeper than " .. maxDepth, i - 1
|
||||
end
|
||||
i = match(s, "^[ \t\n\r]*()", i + 1) :: any
|
||||
i = i + 1
|
||||
if WS[byte(s, i) or 0] then
|
||||
i = match(s, "^[ \t\n\r]*()", i) :: any
|
||||
end
|
||||
local t = {}
|
||||
c = byte(s, i)
|
||||
if c == 125 then
|
||||
@@ -184,7 +191,10 @@ function Decode.value(s: string, i: number, opts: Options?): (any, any, number?)
|
||||
if k == nil then
|
||||
return nil, ni, at
|
||||
end
|
||||
i = match(s, "^[ \t\n\r]*()", ni) :: any
|
||||
i = ni
|
||||
if WS[byte(s, i) or 0] then
|
||||
i = match(s, "^[ \t\n\r]*()", i) :: any
|
||||
end
|
||||
if byte(s, i) ~= 58 then
|
||||
return nil, if i > n then "truncated object" else "expected ':'", i - 1
|
||||
end
|
||||
@@ -200,7 +210,10 @@ function Decode.value(s: string, i: number, opts: Options?): (any, any, number?)
|
||||
if depth > maxDepth then
|
||||
return nil, "nested deeper than " .. maxDepth, i - 1
|
||||
end
|
||||
i = match(s, "^[ \t\n\r]*()", i + 1) :: any
|
||||
i = i + 1
|
||||
if WS[byte(s, i) or 0] then
|
||||
i = match(s, "^[ \t\n\r]*()", i) :: any
|
||||
end
|
||||
if byte(s, i) == 93 then
|
||||
i += 1
|
||||
depth -= 1
|
||||
@@ -256,10 +269,15 @@ function Decode.value(s: string, i: number, opts: Options?): (any, any, number?)
|
||||
t[cnt] = value
|
||||
stackK[depth] = cnt
|
||||
end
|
||||
i = match(s, "^[ \t\n\r]*()", i) :: any
|
||||
if WS[byte(s, i) or 0] then
|
||||
i = match(s, "^[ \t\n\r]*()", i) :: any
|
||||
end
|
||||
c = byte(s, i)
|
||||
if c == 44 then -- ,
|
||||
i = match(s, "^[ \t\n\r]*()", i + 1) :: any
|
||||
i = i + 1
|
||||
if WS[byte(s, i) or 0] then
|
||||
i = match(s, "^[ \t\n\r]*()", i) :: any
|
||||
end
|
||||
if ord then
|
||||
if byte(s, i) ~= 34 then
|
||||
return nil, if i > n then "truncated object" else "expected a key", i - 1
|
||||
@@ -268,7 +286,10 @@ function Decode.value(s: string, i: number, opts: Options?): (any, any, number?)
|
||||
if k == nil then
|
||||
return nil, ni, at
|
||||
end
|
||||
i = match(s, "^[ \t\n\r]*()", ni) :: any
|
||||
i = ni
|
||||
if WS[byte(s, i) or 0] then
|
||||
i = match(s, "^[ \t\n\r]*()", i) :: any
|
||||
end
|
||||
if byte(s, i) ~= 58 then
|
||||
return nil, if i > n then "truncated object" else "expected ':'", i - 1
|
||||
end
|
||||
|
||||
@@ -140,18 +140,23 @@ type State = {
|
||||
maxDepth: number,
|
||||
null: any,
|
||||
err: string?,
|
||||
nls: { string }, -- "\n" .. indent * (i - 1)
|
||||
}
|
||||
|
||||
local function put(st: State, s: string)
|
||||
local n = st.n + 1
|
||||
st.out[n] = s
|
||||
st.n = n
|
||||
local function newline(st: State, depth: number): string
|
||||
local r = "\n" .. string.rep(st.indent :: string, depth)
|
||||
st.nls[depth + 1] = r
|
||||
return r
|
||||
end
|
||||
|
||||
local function value(st: State, v: any, depth: number): boolean
|
||||
local tv = type(v)
|
||||
local out = st.out
|
||||
local n: number
|
||||
if tv == "string" then
|
||||
put(st, quote(v, st.ascii, st.slash))
|
||||
n = st.n + 1
|
||||
out[n] = quote(v, st.ascii, st.slash)
|
||||
st.n = n
|
||||
elseif tv == "number" then
|
||||
local s = number(v)
|
||||
if not s then
|
||||
@@ -161,14 +166,22 @@ local function value(st: State, v: any, depth: number): boolean
|
||||
end
|
||||
s = "null"
|
||||
end
|
||||
put(st, s)
|
||||
n = st.n + 1
|
||||
out[n] = s
|
||||
st.n = n
|
||||
elseif tv == "boolean" then
|
||||
put(st, if v then "true" else "false")
|
||||
n = st.n + 1
|
||||
out[n] = if v then "true" else "false"
|
||||
st.n = n
|
||||
elseif v == nil or v == st.null or v == Types.null then
|
||||
put(st, "null")
|
||||
n = st.n + 1
|
||||
out[n] = "null"
|
||||
st.n = n
|
||||
elseif tv == "table" then
|
||||
if Types.Date.is(v) then
|
||||
put(st, '"' .. tostring(v) .. '"')
|
||||
n = st.n + 1
|
||||
out[n] = '"' .. tostring(v) .. '"'
|
||||
st.n = n
|
||||
return true
|
||||
end
|
||||
if depth >= st.maxDepth then
|
||||
@@ -178,31 +191,44 @@ local function value(st: State, v: any, depth: number): boolean
|
||||
local ind = st.indent
|
||||
local nl, nl2 = "", ""
|
||||
if ind then
|
||||
nl = "\n" .. string.rep(ind, depth + 1)
|
||||
nl2 = "\n" .. string.rep(ind, depth)
|
||||
local nls = st.nls
|
||||
nl = nls[depth + 2] or newline(st, depth + 1)
|
||||
nl2 = nls[depth + 1] or newline(st, depth)
|
||||
end
|
||||
if kind(v, st.emptyObject) == "array" then
|
||||
local n = #v
|
||||
if n == 0 then
|
||||
put(st, "[]")
|
||||
local len = #v
|
||||
if len == 0 then
|
||||
n = st.n + 1
|
||||
out[n] = "[]"
|
||||
st.n = n
|
||||
return true
|
||||
end
|
||||
put(st, "[")
|
||||
for i = 1, n do
|
||||
n = st.n + 1
|
||||
out[n] = "["
|
||||
st.n = n
|
||||
for i = 1, len do
|
||||
if i > 1 then
|
||||
put(st, ",")
|
||||
n = st.n + 1
|
||||
out[n] = ","
|
||||
st.n = n
|
||||
end
|
||||
if ind then
|
||||
put(st, nl)
|
||||
n = st.n + 1
|
||||
out[n] = nl
|
||||
st.n = n
|
||||
end
|
||||
if not value(st, v[i], depth + 1) then
|
||||
return false
|
||||
end
|
||||
end
|
||||
if ind then
|
||||
put(st, nl2)
|
||||
n = st.n + 1
|
||||
out[n] = nl2
|
||||
st.n = n
|
||||
end
|
||||
put(st, "]")
|
||||
n = st.n + 1
|
||||
out[n] = "]"
|
||||
st.n = n
|
||||
else
|
||||
local keys: { any }
|
||||
if Types.isObject(v) then
|
||||
@@ -222,17 +248,25 @@ local function value(st: State, v: any, depth: number): boolean
|
||||
end
|
||||
end
|
||||
if #keys == 0 then
|
||||
put(st, "{}")
|
||||
n = st.n + 1
|
||||
out[n] = "{}"
|
||||
st.n = n
|
||||
return true
|
||||
end
|
||||
put(st, "{")
|
||||
n = st.n + 1
|
||||
out[n] = "{"
|
||||
st.n = n
|
||||
local sep = if ind then ": " else ":"
|
||||
for i, k in keys do
|
||||
if i > 1 then
|
||||
put(st, ",")
|
||||
n = st.n + 1
|
||||
out[n] = ","
|
||||
st.n = n
|
||||
end
|
||||
if ind then
|
||||
put(st, nl)
|
||||
n = st.n + 1
|
||||
out[n] = nl
|
||||
st.n = n
|
||||
end
|
||||
local ks: string
|
||||
if type(k) == "string" then
|
||||
@@ -243,16 +277,24 @@ local function value(st: State, v: any, depth: number): boolean
|
||||
st.err = "object key of type " .. type(k)
|
||||
return false
|
||||
end
|
||||
put(st, quote(ks, st.ascii, st.slash))
|
||||
put(st, sep)
|
||||
n = st.n + 1
|
||||
out[n] = quote(ks, st.ascii, st.slash)
|
||||
st.n = n
|
||||
n = st.n + 1
|
||||
out[n] = sep
|
||||
st.n = n
|
||||
if not value(st, v[k], depth + 1) then
|
||||
return false
|
||||
end
|
||||
end
|
||||
if ind then
|
||||
put(st, nl2)
|
||||
n = st.n + 1
|
||||
out[n] = nl2
|
||||
st.n = n
|
||||
end
|
||||
put(st, "}")
|
||||
n = st.n + 1
|
||||
out[n] = "}"
|
||||
st.n = n
|
||||
end
|
||||
elseif tv == "buffer" then
|
||||
st.err = "cannot encode a buffer (encode it as base64 or an array first)"
|
||||
@@ -284,6 +326,7 @@ local function state(opts: Options?, nullv: any?): State
|
||||
maxDepth = o.maxDepth or 1000,
|
||||
null = nullv,
|
||||
err = nil,
|
||||
nls = {},
|
||||
}
|
||||
end
|
||||
Encode.state = state
|
||||
|
||||
@@ -18,6 +18,8 @@ export type Options = {
|
||||
}
|
||||
|
||||
local NAME = "^[%a_:\128-\255][%w_:%.%-\128-\255]*"
|
||||
local TEXT = "^([^<&%]%z\1-\8\11\12\14-\31]*)()<"
|
||||
local ATTR = '^[ \t\n]+([%a_:\128-\255][%w_:%.%-\128-\255]*)[ \t\n]*=[ \t\n]*"([^"<&%c]*)"()'
|
||||
local PREDEF: { [string]: string } = { lt = "<", gt = ">", amp = "&", quot = '"', apos = "'" }
|
||||
|
||||
local utf8char = Text.utf8char
|
||||
@@ -85,7 +87,7 @@ function Parse.toUtf8(s: string): (string?, string)
|
||||
if enc then
|
||||
local e = lower(enc)
|
||||
if e == "utf-8" or e == "utf8" or e == "us-ascii" or e == "ascii" then
|
||||
if find(s, "[\128-\255]") and not utf8.len(s) then
|
||||
if not utf8.len(s) then
|
||||
return nil, "invalid UTF-8"
|
||||
end
|
||||
return s, e
|
||||
@@ -100,7 +102,7 @@ function Parse.toUtf8(s: string): (string?, string)
|
||||
end
|
||||
return nil, "unsupported encoding " .. enc
|
||||
end
|
||||
if find(s, "[\128-\255]") and not utf8.len(s) then
|
||||
if not utf8.len(s) then
|
||||
return nil, "invalid UTF-8"
|
||||
end
|
||||
return s, "utf-8"
|
||||
@@ -494,11 +496,33 @@ function Parse.events(s: string, opts: Options?): () -> (string?, any, any, any)
|
||||
return nil
|
||||
end
|
||||
local c = byte(s, i)
|
||||
if c ~= 60 and depth > 0 and not keepWs and not preserve[depth] then
|
||||
local w = match(s, "^[ \t\n]*()", i) :: number
|
||||
local cw = byte(s, w)
|
||||
if cw == 60 then
|
||||
i = w
|
||||
c = 60
|
||||
elseif cw then
|
||||
-- text with a non-blank at w and no ']', '&' or control characters before the next '<'
|
||||
local raw, e = match(s, TEXT, i)
|
||||
if raw then
|
||||
i = e
|
||||
return "text", raw
|
||||
end
|
||||
end
|
||||
end
|
||||
if c ~= 60 then -- text
|
||||
local e = find(s, "<", i, true) or (n + 1)
|
||||
local raw = sub(s, i, e - 1)
|
||||
local start = i
|
||||
i = e
|
||||
if depth > 0 and not find(raw, "[%]&%z\1-\8\11\12\14-\31]") then
|
||||
-- no ']]>', control characters or references
|
||||
if keepWs or preserve[depth] or find(raw, "[^ \t\n]") then
|
||||
return "text", raw
|
||||
end
|
||||
continue
|
||||
end
|
||||
local wsOnly = not find(raw, "[^ \t\n]")
|
||||
if depth == 0 then
|
||||
if not wsOnly then
|
||||
@@ -524,6 +548,21 @@ function Parse.events(s: string, opts: Options?): () -> (string?, any, any, any)
|
||||
end
|
||||
local c2 = byte(s, i + 1)
|
||||
if c2 == 47 then -- </
|
||||
local top = stack[depth]
|
||||
if top then
|
||||
local tl = #top
|
||||
if byte(s, i + 2 + tl) == 62 and sub(s, i + 2, i + 1 + tl) == top then
|
||||
i += 3 + tl
|
||||
stack[depth] = nil
|
||||
nsStack[depth] = nil
|
||||
preserve[depth] = nil
|
||||
depth -= 1
|
||||
if depth == 0 then
|
||||
rootDone = true
|
||||
end
|
||||
return "end", top
|
||||
end
|
||||
end
|
||||
local name = match(s, NAME, i + 2)
|
||||
if not name then
|
||||
return fail("bad end tag", i)
|
||||
@@ -617,11 +656,35 @@ function Parse.events(s: string, opts: Options?): () -> (string?, any, any, any)
|
||||
end
|
||||
local at = i
|
||||
local p = i + 1 + #name
|
||||
local attrs = Types.object()
|
||||
local attrs = {}
|
||||
local order = {}
|
||||
local na = 0
|
||||
local closed = false
|
||||
local nsMap: { [string]: string }? = nil
|
||||
while true do
|
||||
if byte(s, p) == 62 then
|
||||
p += 1
|
||||
break
|
||||
end
|
||||
-- the common attribute: name="value" with no references, controls or '<' in the value
|
||||
local an, av, ae = match(s, ATTR, p)
|
||||
if an then
|
||||
if attrs[an] ~= nil then
|
||||
return fail("duplicate attribute " .. an, match(s, "^[ \t\n]*()", p) :: number)
|
||||
end
|
||||
attrs[an] = av
|
||||
na += 1
|
||||
order[na] = an
|
||||
if nsOn then
|
||||
if an == "xmlns" or sub(an, 1, 6) == "xmlns:" then
|
||||
local m: { [string]: string } = nsMap or {}
|
||||
nsMap = m
|
||||
m[if an == "xmlns" then "" else sub(an, 7)] = av
|
||||
end
|
||||
end
|
||||
p = ae
|
||||
continue
|
||||
end
|
||||
local q = match(s, "^[ \t\n]*()", p) :: any
|
||||
local cc = byte(s, q)
|
||||
if cc == 62 then
|
||||
@@ -675,7 +738,8 @@ function Parse.events(s: string, opts: Options?): () -> (string?, any, any, any)
|
||||
return fail("duplicate attribute " .. an, q)
|
||||
end
|
||||
attrs[an] = v
|
||||
table.insert(order, an)
|
||||
na += 1
|
||||
order[na] = an
|
||||
if nsOn then
|
||||
if an == "xmlns" or sub(an, 1, 6) == "xmlns:" then
|
||||
local m: { [string]: string } = nsMap or {}
|
||||
|
||||
@@ -78,7 +78,16 @@ function Xml.decode(src: string | buffer, opts: DecodeOptions?): (Node?, Err.Err
|
||||
local cur: any = doc
|
||||
local stack = { doc }
|
||||
local depth = 1
|
||||
for ev, a, b, c in Xml.events(src, o) do
|
||||
local s, terr = text(src)
|
||||
local u, enc = nil, ""
|
||||
if s then
|
||||
u, enc = Parse.toUtf8(s)
|
||||
end
|
||||
if not u then
|
||||
return nil, terr or Err.new(if find(enc, "unsupported", 1, true) then Err.UNSUPPORTED else Err.CORRUPT, enc)
|
||||
end
|
||||
local str: string = u
|
||||
for ev, a, b, c in Parse.events(str, o) do
|
||||
if ev == "start" then
|
||||
local node: any = { name = a, attrs = b, children = {}, parent = if parents then cur else nil }
|
||||
if c then
|
||||
@@ -118,7 +127,7 @@ function Xml.decode(src: string | buffer, opts: DecodeOptions?): (Node?, Err.Err
|
||||
elseif ev == "doctype" then
|
||||
doc.doctype = { raw = a, name = b.name, publicId = b.publicId, systemId = b.systemId, entities = b.entities }
|
||||
elseif ev == "error" then
|
||||
return nil, a
|
||||
return nil, Err.new(errCode(a, b, #str), a, b)
|
||||
end
|
||||
end
|
||||
return doc, nil
|
||||
@@ -126,7 +135,7 @@ end
|
||||
|
||||
function Xml.element(name: string, attrs: { [string]: string }?, children: { any }?): Node
|
||||
local a = attrs or Types.object()
|
||||
if not Types.isObject(a) then
|
||||
if attrs and not Types.isObject(a) then
|
||||
local keys = {}
|
||||
for k in next, a do
|
||||
table.insert(keys, k)
|
||||
@@ -161,24 +170,27 @@ function Xml.setAttr(node: Node, name: string, value: string?)
|
||||
Types.set(node.attrs, name, value)
|
||||
end
|
||||
|
||||
-- name == nil: any element; a name without a prefix also matches prefixed names with that local part
|
||||
local function matches(cn: string, name: string?, plain: boolean): boolean
|
||||
return name == nil or cn == name or (plain and find(cn, ":", 1, true) ~= nil and match(cn, ":(.*)$") == name)
|
||||
end
|
||||
|
||||
function Xml.children(node: Node, name: string?): { Node }
|
||||
local out = {}
|
||||
local plain = name ~= nil and not find(name, ":", 1, true)
|
||||
for _, ch in node.children do
|
||||
if type(ch) == "table" and string.byte(ch.name) ~= 35 then
|
||||
if name == nil or ch.name == name or (not find(name, ":", 1, true) and match(ch.name, ":(.*)$") == name) then
|
||||
table.insert(out, ch)
|
||||
end
|
||||
if type(ch) == "table" and string.byte(ch.name) ~= 35 and matches(ch.name, name, plain) then
|
||||
table.insert(out, ch)
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
function Xml.child(node: Node, name: string?): Node?
|
||||
local plain = name ~= nil and not find(name, ":", 1, true)
|
||||
for _, ch in node.children do
|
||||
if type(ch) == "table" and string.byte(ch.name) ~= 35 then
|
||||
if name == nil or ch.name == name or (not find(name, ":", 1, true) and match(ch.name, ":(.*)$") == name) then
|
||||
return ch
|
||||
end
|
||||
if type(ch) == "table" and string.byte(ch.name) ~= 35 and matches(ch.name, name, plain) then
|
||||
return ch
|
||||
end
|
||||
end
|
||||
return nil
|
||||
@@ -186,10 +198,11 @@ end
|
||||
|
||||
function Xml.descendants(node: Node, name: string?): { Node }
|
||||
local out = {}
|
||||
local plain = name ~= nil and not find(name, ":", 1, true)
|
||||
local function walk(n: Node)
|
||||
for _, ch in n.children do
|
||||
if type(ch) == "table" and string.byte(ch.name) ~= 35 then
|
||||
if name == nil or ch.name == name or (not find(name :: string, ":", 1, true) and match(ch.name, ":(.*)$") == name) then
|
||||
if matches(ch.name, name, plain) then
|
||||
table.insert(out, ch)
|
||||
end
|
||||
walk(ch)
|
||||
@@ -207,6 +220,13 @@ function Xml.text(node: any): string
|
||||
if node.name == "#cdata" then
|
||||
return node.text or ""
|
||||
end
|
||||
local kids = node.children
|
||||
local nk = #kids
|
||||
if nk == 0 then
|
||||
return ""
|
||||
elseif nk == 1 and type(kids[1]) == "string" then
|
||||
return kids[1]
|
||||
end
|
||||
local parts = {}
|
||||
local function walk(n: any)
|
||||
for _, ch in n.children do
|
||||
@@ -251,14 +271,32 @@ local TEXT_ESC = { ["&"] = "&", ["<"] = "<", [">"] = ">", ["\r"] = "&#
|
||||
local ATTR_ESC = { ["&"] = "&", ["<"] = "<", [">"] = ">", ['"'] = """, ["\t"] = "	", ["\n"] = " ", ["\r"] = " " }
|
||||
|
||||
function Xml.escape(s: string): string
|
||||
if not find(s, "[&<>\r]") then
|
||||
return s
|
||||
end
|
||||
return (gsub(s, "[&<>\r]", TEXT_ESC))
|
||||
end
|
||||
function Xml.escapeAttr(s: string): string
|
||||
if not find(s, '[&<>"\t\n\r]') then
|
||||
return s
|
||||
end
|
||||
return (gsub(s, '[&<>"\t\n\r]', ATTR_ESC))
|
||||
end
|
||||
|
||||
local goodNames: { [string]: boolean } = {}
|
||||
local nGood = 0
|
||||
local function badName(name: any): boolean
|
||||
return type(name) ~= "string" or not match(name, "^[%a_:\128-\255][%w_:%.%-\128-\255]*$")
|
||||
if goodNames[name] then
|
||||
return false
|
||||
end
|
||||
if type(name) ~= "string" or not match(name, "^[%a_:\128-\255][%w_:%.%-\128-\255]*$") then
|
||||
return true
|
||||
end
|
||||
if nGood < 4096 then
|
||||
goodNames[name] = true
|
||||
nGood += 1
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
function Xml.encode(node: Node, opts: EncodeOptions?): (string?, Err.Err?)
|
||||
@@ -270,6 +308,15 @@ function Xml.encode(node: Node, opts: EncodeOptions?): (string?, Err.Err?)
|
||||
ind = o.indent
|
||||
end
|
||||
local selfClose = o.selfClose ~= false
|
||||
local indents: { string } = {}
|
||||
local function indent(depth: number): string
|
||||
local r = indents[depth + 1]
|
||||
if not r then
|
||||
r = "\n" .. string.rep(ind :: string, depth)
|
||||
indents[depth + 1] = r
|
||||
end
|
||||
return r
|
||||
end
|
||||
local out: { string } = {}
|
||||
local err: string? = nil
|
||||
local seen: { [any]: boolean } = {}
|
||||
@@ -324,7 +371,7 @@ function Xml.encode(node: Node, opts: EncodeOptions?): (string?, Err.Err?)
|
||||
end
|
||||
table.insert(out, "<" .. name)
|
||||
local attrs = n.attrs or {}
|
||||
for _, k in Types.keys(attrs) do
|
||||
for _, k in (if next(attrs) == nil then attrs else Types.keys(attrs)) do
|
||||
local v = attrs[k]
|
||||
if badName(k) then
|
||||
err = "bad attribute name " .. tostring(k)
|
||||
@@ -355,12 +402,12 @@ function Xml.encode(node: Node, opts: EncodeOptions?): (string?, Err.Err?)
|
||||
end
|
||||
for _, c in ch do
|
||||
if pretty then
|
||||
table.insert(out, "\n" .. string.rep(ind :: string, depth + 1))
|
||||
table.insert(out, indent(depth + 1))
|
||||
end
|
||||
write(c, depth + 1)
|
||||
end
|
||||
if pretty then
|
||||
table.insert(out, "\n" .. string.rep(ind :: string, depth))
|
||||
table.insert(out, indent(depth))
|
||||
end
|
||||
table.insert(out, "</" .. name .. ">")
|
||||
end
|
||||
|
||||
Reference in new issue
Block a user