diff options
author | Waqas Hussain <waqas20@gmail.com> | 2009-06-25 17:22:53 +0500 |
---|---|---|
committer | Waqas Hussain <waqas20@gmail.com> | 2009-06-25 17:22:53 +0500 |
commit | 1ce4a34f68ef4ce967533a35299e205f2321f12b (patch) | |
tree | 0cefaba782788c79551ed00c015029c9e36603f3 | |
parent | 3a8c1a3f6c3258a13f4791cd3d57145225ebb64f (diff) | |
download | prosody-1ce4a34f68ef4ce967533a35299e205f2321f12b.tar.gz prosody-1ce4a34f68ef4ce967533a35299e205f2321f12b.zip |
util.stanza: Rewrote stanza_mt.__tostring. 20-30% faster stanza serialization. - #optimization
-rw-r--r-- | util/stanza.lua | 50 |
1 files changed, 18 insertions, 32 deletions
diff --git a/util/stanza.lua b/util/stanza.lua index 4f387561..aff0bfc7 100644 --- a/util/stanza.lua +++ b/util/stanza.lua @@ -23,6 +23,7 @@ local next = next; 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 @@ function stanza_mt:childtags() 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 |