Software / code / prosody
Comparison
util/stanza.lua @ 613:6c09127b50fb
New, faster, stanza serialization
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Wed, 10 Dec 2008 15:32:13 +0000 |
| parent | 519:cccd610a0ef9 |
| child | 615:4ae3e81513f3 |
comparison
equal
deleted
inserted
replaced
| 612:0d44fc0a78f8 | 613:6c09127b50fb |
|---|---|
| 18 -- | 18 -- |
| 19 | 19 |
| 20 | 20 |
| 21 local t_insert = table.insert; | 21 local t_insert = table.insert; |
| 22 local t_remove = table.remove; | 22 local t_remove = table.remove; |
| 23 local t_concat = table.concat; | |
| 23 local s_format = string.format; | 24 local s_format = string.format; |
| 24 local tostring = tostring; | 25 local tostring = tostring; |
| 25 local setmetatable = setmetatable; | 26 local setmetatable = setmetatable; |
| 26 local pairs = pairs; | 27 local pairs = pairs; |
| 27 local ipairs = ipairs; | 28 local ipairs = ipairs; |
| 28 local type = type; | 29 local type = type; |
| 29 local next = next; | 30 local next = next; |
| 30 local print = print; | 31 local print = print; |
| 31 local unpack = unpack; | 32 local unpack = unpack; |
| 32 local s_gsub = string.gsub; | 33 local s_gsub = string.gsub; |
| 33 local os = os; | 34 local os = os; |
| 34 | 35 |
| 35 local do_pretty_printing = not os.getenv("WINDIR"); | 36 local do_pretty_printing = not os.getenv("WINDIR"); |
| 36 local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring; | 37 local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring; |
| 37 | 38 |
| 38 local log = require "util.logger".init("stanza"); | 39 local log = require "util.logger".init("stanza"); |
| 114 function xml_escape(s) return s_gsub(s, "['&<>\"]", xml_entities); end | 115 function xml_escape(s) return s_gsub(s, "['&<>\"]", xml_entities); end |
| 115 end | 116 end |
| 116 | 117 |
| 117 local xml_escape = xml_escape; | 118 local xml_escape = xml_escape; |
| 118 | 119 |
| 120 local function dostring(t, buf, self, xml_escape) | |
| 121 t_insert(buf, "<"); | |
| 122 t_insert(buf, t.name); | |
| 123 for k, v in pairs(t.attr) do if type(k) == "string" then | |
| 124 t_insert(buf, " "); | |
| 125 t_insert(buf, k); | |
| 126 t_insert(buf, "='"); | |
| 127 t_insert(buf, (xml_escape(tostring(v)))); | |
| 128 t_insert(buf, "'"); | |
| 129 end end | |
| 130 t_insert(buf, ">"); | |
| 131 for n, child in ipairs(t) do | |
| 132 if child.name then | |
| 133 self(child, buf, self, xml_escape); | |
| 134 else | |
| 135 t_insert(buf, (xml_escape(child))); | |
| 136 end | |
| 137 end | |
| 138 t_insert(buf, "</"); | |
| 139 t_insert(buf, t.name); | |
| 140 t_insert(buf, ">"); | |
| 141 end | |
| 142 | |
| 119 function stanza_mt.__tostring(t) | 143 function stanza_mt.__tostring(t) |
| 120 local children_text = ""; | 144 local buf = {}; |
| 121 for n, child in ipairs(t) do | 145 dostring(t, buf, dostring, xml_escape); |
| 122 if type(child) == "string" then | 146 return t_concat(buf); |
| 123 children_text = children_text .. xml_escape(child); | 147 end |
| 124 else | 148 |
| 125 children_text = children_text .. tostring(child); | |
| 126 end | |
| 127 end | |
| 128 | |
| 129 local attr_string = ""; | |
| 130 if t.attr then | |
| 131 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 | |
| 132 end | |
| 133 return s_format("<%s%s>%s</%s>", t.name, attr_string, children_text, t.name); | |
| 134 end | |
| 135 | 149 |
| 136 function stanza_mt.top_tag(t) | 150 function stanza_mt.top_tag(t) |
| 137 local attr_string = ""; | 151 local attr_string = ""; |
| 138 if t.attr then | 152 if t.attr then |
| 139 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 | 153 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 |