2009-06-25
util.stanza: Rewrote stanza_mt.__tostring. 20-30% faster stanza serialization. - #optimization
| mwild1@896 | 1 | -- Prosody IM v0.4 |
| mwild1@760 | 2 | -- Copyright (C) 2008-2009 Matthew Wild |
| mwild1@760 | 3 | -- Copyright (C) 2008-2009 Waqas Hussain |
| mwild1@519 | 4 | -- |
| mwild1@758 | 5 | -- This project is MIT/X11 licensed. Please see the |
| mwild1@758 | 6 | -- COPYING file in the source package for more information. |
| mwild1@519 | 7 | -- |
| mwild1@519 | 8 | |
| mwild1@519 | 9 | |
| matthew@1 | 10 | local t_insert = table.insert; |
| waqas20@626 | 11 | local t_concat = table.concat; |
| matthew@1 | 12 | local t_remove = table.remove; |
| mwild1@613 | 13 | local t_concat = table.concat; |
| bt@23 | 14 | local s_format = string.format; |
| waqas20@829 | 15 | local s_match = string.match; |
| matthew@1 | 16 | local tostring = tostring; |
| matthew@1 | 17 | local setmetatable = setmetatable; |
| waqas20@829 | 18 | local getmetatable = getmetatable; |
| matthew@1 | 19 | local pairs = pairs; |
| matthew@1 | 20 | local ipairs = ipairs; |
| matthew@1 | 21 | local type = type; |
| mwild1@145 | 22 | local next = next; |
| waqas20@626 | 23 | local print = print; |
| mwild1@90 | 24 | local unpack = unpack; |
| matthew@4 | 25 | local s_gsub = string.gsub; |
| waqas20@1416 | 26 | local s_char = string.char; |
| mwild1@613 | 27 | local os = os; |
| mwild1@145 | 28 | |
| mwild1@262 | 29 | local do_pretty_printing = not os.getenv("WINDIR"); |
| mwild1@262 | 30 | local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring; |
| mwild1@262 | 31 | |
| mwild1@145 | 32 | local log = require "util.logger".init("stanza"); |
| mwild1@145 | 33 | |
| matthew@0 | 34 | module "stanza" |
| matthew@0 | 35 | |
| matthew@0 | 36 | stanza_mt = {}; |
| matthew@0 | 37 | stanza_mt.__index = stanza_mt; |
| matthew@0 | 38 | |
| matthew@0 | 39 | function stanza(name, attr) |
| matthew@1 | 40 | local stanza = { name = name, attr = attr or {}, tags = {}, last_add = {}}; |
| matthew@0 | 41 | return setmetatable(stanza, stanza_mt); |
| matthew@0 | 42 | end |
| matthew@0 | 43 | |
| matthew@0 | 44 | function stanza_mt:query(xmlns) |
| matthew@0 | 45 | return self:tag("query", { xmlns = xmlns }); |
| matthew@0 | 46 | end |
| mwild1@373 | 47 | |
| mwild1@373 | 48 | function stanza_mt:body(text, attr) |
| mwild1@373 | 49 | return self:tag("body", attr):text(text); |
| mwild1@373 | 50 | end |
| mwild1@373 | 51 | |
| matthew@0 | 52 | function stanza_mt:tag(name, attrs) |
| matthew@0 | 53 | local s = stanza(name, attrs); |
| mwild1@180 | 54 | (self.last_add[#self.last_add] or self):add_direct_child(s); |
| matthew@0 | 55 | t_insert(self.last_add, s); |
| matthew@0 | 56 | return self; |
| matthew@0 | 57 | end |
| matthew@0 | 58 | |
| matthew@0 | 59 | function stanza_mt:text(text) |
| mwild1@180 | 60 | (self.last_add[#self.last_add] or self):add_direct_child(text); |
| matthew@0 | 61 | return self; |
| matthew@0 | 62 | end |
| matthew@0 | 63 | |
| matthew@0 | 64 | function stanza_mt:up() |
| matthew@0 | 65 | t_remove(self.last_add); |
| matthew@0 | 66 | return self; |
| matthew@0 | 67 | end |
| matthew@0 | 68 | |
| mwild1@964 | 69 | function stanza_mt:reset() |
| mwild1@964 | 70 | local last_add = self.last_add; |
| mwild1@964 | 71 | for i = 1,#last_add do |
| mwild1@964 | 72 | last_add[i] = nil; |
| mwild1@964 | 73 | end |
| mwild1@964 | 74 | return self; |
| mwild1@964 | 75 | end |
| mwild1@964 | 76 | |
| mwild1@180 | 77 | function stanza_mt:add_direct_child(child) |
| matthew@1 | 78 | if type(child) == "table" then |
| matthew@1 | 79 | t_insert(self.tags, child); |
| matthew@1 | 80 | end |
| matthew@0 | 81 | t_insert(self, child); |
| matthew@0 | 82 | end |
| matthew@0 | 83 | |
| mwild1@180 | 84 | function stanza_mt:add_child(child) |
| mwild1@180 | 85 | (self.last_add[#self.last_add] or self):add_direct_child(child); |
| mwild1@180 | 86 | return self; |
| mwild1@180 | 87 | end |
| mwild1@180 | 88 | |
| matthew@0 | 89 | function stanza_mt:child_with_name(name) |
| mwild1@689 | 90 | for _, child in ipairs(self.tags) do |
| matthew@0 | 91 | if child.name == name then return child; end |
| matthew@0 | 92 | end |
| matthew@0 | 93 | end |
| matthew@0 | 94 | |
| mwild1@689 | 95 | function stanza_mt:child_with_ns(ns) |
| mwild1@689 | 96 | for _, child in ipairs(self.tags) do |
| mwild1@689 | 97 | if child.attr.xmlns == ns then return child; end |
| mwild1@689 | 98 | end |
| mwild1@689 | 99 | end |
| mwild1@689 | 100 | |
| matthew@1 | 101 | function stanza_mt:children() |
| matthew@1 | 102 | local i = 0; |
| matthew@1 | 103 | return function (a) |
| matthew@1 | 104 | i = i + 1 |
| matthew@1 | 105 | local v = a[i] |
| matthew@1 | 106 | if v then return v; end |
| matthew@1 | 107 | end, self, i; |
| matthew@1 | 108 | |
| matthew@1 | 109 | end |
| matthew@2 | 110 | function stanza_mt:childtags() |
| matthew@2 | 111 | local i = 0; |
| matthew@2 | 112 | return function (a) |
| matthew@2 | 113 | i = i + 1 |
| matthew@2 | 114 | local v = self.tags[i] |
| matthew@2 | 115 | if v then return v; end |
| matthew@2 | 116 | end, self.tags[1], i; |
| matthew@2 | 117 | |
| matthew@2 | 118 | end |
| matthew@1 | 119 | |
| waqas20@1416 | 120 | local xml_escape = (function() |
| waqas20@1416 | 121 | local escape_table = { ["'"] = "'", ["\""] = """, ["<"] = "<", [">"] = ">", ["&"] = "&" }; |
| waqas20@1416 | 122 | return function(str) return (s_gsub(str, "['&<>\"]", escape_table)); end |
| waqas20@1416 | 123 | end)(); |
| waqas20@1416 | 124 | local function _dostring(t, buf, self, xml_escape) |
| mwild1@776 | 125 | local nsid, ns, attrk = 0; |
| waqas20@1416 | 126 | t_insert(buf, "<"..t.name); |
| waqas20@1416 | 127 | for k, v in pairs(t.attr) do |
| mwild1@776 | 128 | ns, attrk = s_match(k, "^([^|]+)|(.+)$"); |
| mwild1@776 | 129 | if ns then |
| waqas20@1416 | 130 | nsid = nsid + 1; |
| waqas20@1416 | 131 | t_insert(buf, " xmlns:ns"..nsid.."='"..xml_escape(ns).."' ".."ns"..nsid..":"..attrk.."='"..xml_escape(v).."'"); |
| mwild1@776 | 132 | else |
| waqas20@1416 | 133 | t_insert(buf, " "..k.."='"..xml_escape(v).."'"); |
| mwild1@776 | 134 | end |
| waqas20@1416 | 135 | end |
| waqas20@626 | 136 | t_insert(buf, ">"); |
| waqas20@1416 | 137 | for n=1,#t do |
| waqas20@1416 | 138 | local child = t[n]; |
| waqas20@1416 | 139 | if child.name then |
| mwild1@613 | 140 | self(child, buf, self, xml_escape); |
| matthew@4 | 141 | else |
| waqas20@1416 | 142 | t_insert(buf, xml_escape(child)); |
| matthew@4 | 143 | end |
| matthew@0 | 144 | end |
| waqas20@1416 | 145 | t_insert(buf, "</"..t.name..">"); |
| waqas20@626 | 146 | end |
| waqas20@626 | 147 | function stanza_mt.__tostring(t) |
| waqas20@626 | 148 | local buf = {}; |
| waqas20@1416 | 149 | _dostring(t, buf, _dostring, xml_escape); |
| waqas20@626 | 150 | return t_concat(buf); |
| matthew@0 | 151 | end |
| matthew@0 | 152 | |
| mwild1@242 | 153 | function stanza_mt.top_tag(t) |
| mwild1@242 | 154 | local attr_string = ""; |
| mwild1@242 | 155 | if t.attr then |
| mwild1@338 | 156 | for k, v in pairs(t.attr) do if type(k) == "string" then attr_string = attr_string .. s_format(" %s='%s'", k, xml_escape(tostring(v))); end end |
| mwild1@242 | 157 | end |
| mwild1@242 | 158 | return s_format("<%s%s>", t.name, attr_string); |
| mwild1@242 | 159 | end |
| mwild1@242 | 160 | |
| matthew@0 | 161 | function stanza_mt.__add(s1, s2) |
| mwild1@180 | 162 | return s1:add_direct_child(s2); |
| matthew@0 | 163 | end |
| matthew@0 | 164 | |
| matthew@0 | 165 | |
| matthew@0 | 166 | do |
| matthew@0 | 167 | local id = 0; |
| matthew@0 | 168 | function new_id() |
| matthew@0 | 169 | id = id + 1; |
| matthew@0 | 170 | return "lx"..id; |
| matthew@0 | 171 | end |
| matthew@0 | 172 | end |
| matthew@0 | 173 | |
| mwild1@90 | 174 | function preserialize(stanza) |
| mwild1@90 | 175 | local s = { name = stanza.name, attr = stanza.attr }; |
| mwild1@90 | 176 | for _, child in ipairs(stanza) do |
| mwild1@90 | 177 | if type(child) == "table" then |
| mwild1@90 | 178 | t_insert(s, preserialize(child)); |
| mwild1@90 | 179 | else |
| mwild1@90 | 180 | t_insert(s, child); |
| mwild1@90 | 181 | end |
| mwild1@90 | 182 | end |
| mwild1@90 | 183 | return s; |
| mwild1@90 | 184 | end |
| mwild1@90 | 185 | |
| mwild1@90 | 186 | function deserialize(stanza) |
| mwild1@90 | 187 | -- Set metatable |
| waqas20@91 | 188 | if stanza then |
| waqas20@1415 | 189 | local attr = stanza.attr; |
| waqas20@1415 | 190 | for i=1,#attr do attr[i] = nil; end |
| waqas20@91 | 191 | setmetatable(stanza, stanza_mt); |
| mwild1@90 | 192 | for _, child in ipairs(stanza) do |
| mwild1@90 | 193 | if type(child) == "table" then |
| waqas20@91 | 194 | deserialize(child); |
| mwild1@90 | 195 | end |
| mwild1@90 | 196 | end |
| waqas20@91 | 197 | if not stanza.tags then |
| waqas20@91 | 198 | -- Rebuild tags |
| waqas20@91 | 199 | local tags = {}; |
| waqas20@91 | 200 | for _, child in ipairs(stanza) do |
| waqas20@91 | 201 | if type(child) == "table" then |
| waqas20@91 | 202 | t_insert(tags, child); |
| waqas20@91 | 203 | end |
| waqas20@91 | 204 | end |
| waqas20@91 | 205 | stanza.tags = tags; |
| mwild1@680 | 206 | if not stanza.last_add then |
| mwild1@680 | 207 | stanza.last_add = {}; |
| mwild1@680 | 208 | end |
| waqas20@91 | 209 | end |
| mwild1@90 | 210 | end |
| mwild1@90 | 211 | |
| mwild1@90 | 212 | return stanza; |
| mwild1@90 | 213 | end |
| mwild1@90 | 214 | |
| waqas20@829 | 215 | function clone(stanza) |
| waqas20@829 | 216 | local lookup_table = {}; |
| waqas20@829 | 217 | local function _copy(object) |
| waqas20@829 | 218 | if type(object) ~= "table" then |
| waqas20@829 | 219 | return object; |
| waqas20@829 | 220 | elseif lookup_table[object] then |
| waqas20@829 | 221 | return lookup_table[object]; |
| waqas20@829 | 222 | end |
| waqas20@829 | 223 | local new_table = {}; |
| waqas20@829 | 224 | lookup_table[object] = new_table; |
| waqas20@829 | 225 | for index, value in pairs(object) do |
| waqas20@829 | 226 | new_table[_copy(index)] = _copy(value); |
| waqas20@829 | 227 | end |
| waqas20@829 | 228 | return setmetatable(new_table, getmetatable(object)); |
| waqas20@829 | 229 | end |
| waqas20@829 | 230 | return _copy(stanza) |
| waqas20@829 | 231 | end |
| waqas20@829 | 232 | |
| matthew@0 | 233 | function message(attr, body) |
| matthew@0 | 234 | if not body then |
| matthew@0 | 235 | return stanza("message", attr); |
| matthew@0 | 236 | else |
| matthew@0 | 237 | return stanza("message", attr):tag("body"):text(body); |
| matthew@0 | 238 | end |
| matthew@0 | 239 | end |
| matthew@0 | 240 | function iq(attr) |
| matthew@0 | 241 | if attr and not attr.id then attr.id = new_id(); end |
| matthew@0 | 242 | return stanza("iq", attr or { id = new_id() }); |
| matthew@0 | 243 | end |
| matthew@0 | 244 | |
| matthew@0 | 245 | function reply(orig) |
| mwild1@209 | 246 | return stanza(orig.name, orig.attr and { to = orig.attr.from, from = orig.attr.to, id = orig.attr.id, type = ((orig.name == "iq" and "result") or orig.attr.type) }); |
| matthew@0 | 247 | end |
| matthew@0 | 248 | |
| waqas20@1151 | 249 | function error_reply(orig, type, condition, message) |
| waqas20@70 | 250 | local t = reply(orig); |
| waqas20@60 | 251 | t.attr.type = "error"; |
| waqas20@60 | 252 | t:tag("error", {type = type}) |
| waqas20@60 | 253 | :tag(condition, {xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas"}):up(); |
| waqas20@60 | 254 | if (message) then t:tag("text"):text(message):up(); end |
| waqas20@60 | 255 | return t; -- stanza ready for adding app-specific errors |
| waqas20@60 | 256 | end |
| waqas20@60 | 257 | |
| matthew@0 | 258 | function presence(attr) |
| matthew@0 | 259 | return stanza("presence", attr); |
| matthew@0 | 260 | end |
| matthew@0 | 261 | |
| mwild1@262 | 262 | if do_pretty_printing then |
| mwild1@262 | 263 | local style_attrk = getstyle("yellow"); |
| mwild1@262 | 264 | local style_attrv = getstyle("red"); |
| mwild1@262 | 265 | local style_tagname = getstyle("red"); |
| mwild1@262 | 266 | local style_punc = getstyle("magenta"); |
| mwild1@262 | 267 | |
| mwild1@262 | 268 | local attr_format = " "..getstring(style_attrk, "%s")..getstring(style_punc, "=")..getstring(style_attrv, "'%s'"); |
| mwild1@262 | 269 | local top_tag_format = getstring(style_punc, "<")..getstring(style_tagname, "%s").."%s"..getstring(style_punc, ">"); |
| mwild1@262 | 270 | --local tag_format = getstring(style_punc, "<")..getstring(style_tagname, "%s").."%s"..getstring(style_punc, ">").."%s"..getstring(style_punc, "</")..getstring(style_tagname, "%s")..getstring(style_punc, ">"); |
| mwild1@262 | 271 | local tag_format = top_tag_format.."%s"..getstring(style_punc, "</")..getstring(style_tagname, "%s")..getstring(style_punc, ">"); |
| mwild1@262 | 272 | function stanza_mt.pretty_print(t) |
| mwild1@262 | 273 | local children_text = ""; |
| mwild1@262 | 274 | for n, child in ipairs(t) do |
| mwild1@262 | 275 | if type(child) == "string" then |
| mwild1@262 | 276 | children_text = children_text .. xml_escape(child); |
| mwild1@262 | 277 | else |
| mwild1@262 | 278 | children_text = children_text .. child:pretty_print(); |
| mwild1@262 | 279 | end |
| mwild1@262 | 280 | end |
| mwild1@262 | 281 | |
| mwild1@262 | 282 | local attr_string = ""; |
| mwild1@262 | 283 | if t.attr then |
| mwild1@262 | 284 | for k, v in pairs(t.attr) do if type(k) == "string" then attr_string = attr_string .. s_format(attr_format, k, tostring(v)); end end |
| mwild1@262 | 285 | end |
| mwild1@262 | 286 | return s_format(tag_format, t.name, attr_string, children_text, t.name); |
| mwild1@262 | 287 | end |
| mwild1@262 | 288 | |
| mwild1@262 | 289 | function stanza_mt.pretty_top_tag(t) |
| mwild1@262 | 290 | local attr_string = ""; |
| mwild1@262 | 291 | if t.attr then |
| mwild1@262 | 292 | for k, v in pairs(t.attr) do if type(k) == "string" then attr_string = attr_string .. s_format(attr_format, k, tostring(v)); end end |
| mwild1@262 | 293 | end |
| mwild1@262 | 294 | return s_format(top_tag_format, t.name, attr_string); |
| mwild1@262 | 295 | end |
| mwild1@262 | 296 | else |
| mwild1@262 | 297 | -- Sorry, fresh out of colours for you guys ;) |
| mwild1@262 | 298 | stanza_mt.pretty_print = stanza_mt.__tostring; |
| mwild1@262 | 299 | stanza_mt.pretty_top_tag = stanza_mt.top_tag; |
| mwild1@262 | 300 | end |
| mwild1@262 | 301 | |
| mwild1@90 | 302 | return _M; |