merge ws/perf-core

This commit is contained in:
4DBug committed 2026-11-01 20:56:00 -06:00
commit de755ca036
37 files changed
+5317 -2562

No files matched your search

+28 -7
View File
@@ -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
+71 -28
View File
@@ -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
+68 -4
View File
@@ -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 {}
+63 -16
View File
@@ -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 = { ["&"] = "&amp;", ["<"] = "&lt;", [">"] = "&gt;", ["\r"] = "&#
local ATTR_ESC = { ["&"] = "&amp;", ["<"] = "&lt;", [">"] = "&gt;", ['"'] = "&quot;", ["\t"] = "&#9;", ["\n"] = "&#10;", ["\r"] = "&#13;" }
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