Software /
code /
prosody
Changeset
1416:f916f0ff90e5
util.stanza: Rewrote stanza_mt.__tostring. 20-30% faster stanza serialization. - #optimization
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Thu, 25 Jun 2009 17:22:53 +0500 |
parents | 1415:957a81b72cb2 |
children | 1417:b02b22f77326 |
files | util/stanza.lua |
diffstat | 1 files changed, 18 insertions(+), 32 deletions(-) [+] |
line wrap: on
line diff
--- a/util/stanza.lua Thu Jun 25 17:19:13 2009 +0500 +++ b/util/stanza.lua Thu Jun 25 17:22:53 2009 +0500 @@ -23,6 +23,7 @@ local print = print; local unpack = unpack; local s_gsub = string.gsub; +local s_char = string.char; local os = os; local do_pretty_printing = not os.getenv("WINDIR"); @@ -116,54 +117,39 @@ end -do - local xml_entities = { ["'"] = "'", ["\""] = """, ["<"] = "<", [">"] = ">", ["&"] = "&" }; - function xml_escape(s) return s_gsub(s, "['&<>\"]", xml_entities); end -end - -local xml_escape = xml_escape; - -local function dostring(t, buf, self, xml_escape) +local xml_escape = (function() + local escape_table = { ["'"] = "'", ["\""] = """, ["<"] = "<", [">"] = ">", ["&"] = "&" }; + return function(str) return (s_gsub(str, "['&<>\"]", escape_table)); end +end)(); +local function _dostring(t, buf, self, xml_escape) local nsid, ns, attrk = 0; - t_insert(buf, "<"); - t_insert(buf, t.name); - for k, v in pairs(t.attr) do if type(k) == "string" then - t_insert(buf, " "); + t_insert(buf, "<"..t.name); + for k, v in pairs(t.attr) do ns, attrk = s_match(k, "^([^|]+)|(.+)$"); if ns then - nsid = (nsid or -1) + 1; - t_insert(buf, "xmlns:ns"..nsid); - t_insert(buf, "='"); - t_insert(buf, (xml_escape(tostring(ns)))); - t_insert(buf, "' "); - t_insert(buf, "ns"..nsid..":"..attrk); + nsid = nsid + 1; + t_insert(buf, " xmlns:ns"..nsid.."='"..xml_escape(ns).."' ".."ns"..nsid..":"..attrk.."='"..xml_escape(v).."'"); else - t_insert(buf, k); + t_insert(buf, " "..k.."='"..xml_escape(v).."'"); end - t_insert(buf, "='"); - t_insert(buf, (xml_escape(tostring(v)))); - t_insert(buf, "'"); - end end + end t_insert(buf, ">"); - for n, child in ipairs(t) do - if child.name then + for n=1,#t do + local child = t[n]; + if child.name then self(child, buf, self, xml_escape); else - t_insert(buf, (xml_escape(child))); + t_insert(buf, xml_escape(child)); end end - t_insert(buf, "</"); - t_insert(buf, t.name); - t_insert(buf, ">"); + t_insert(buf, "</"..t.name..">"); end - function stanza_mt.__tostring(t) local buf = {}; - dostring(t, buf, dostring, xml_escape); + _dostring(t, buf, _dostring, xml_escape); return t_concat(buf); end - function stanza_mt.top_tag(t) local attr_string = ""; if t.attr then