perf: XML encode (name cache, escape fast path, indent cache, no key list for attribute-less elements), Types.object shares one empty key list until set, rbxm column type check
This commit is contained in:
1 parent
0ec772ee30
commit
7947dd7f3b
3 files changed
+63
-19
No files matched your search
@@ -135,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)
|
||||
@@ -271,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?)
|
||||
@@ -290,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 } = {}
|
||||
@@ -344,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)
|
||||
@@ -375,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