Mercurial > prosody-hg
comparison util/stanza.lua @ 12406:a3ddf3f42212
util.stanza: Use table.move in clone
Code reduction, potentially a performance gain.
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Fri, 18 Mar 2022 16:39:48 +0100 |
| parents | 1a4c61253932 |
| children | b6b01724e04f |
comparison
equal
deleted
inserted
replaced
| 12405:308ed64dc69b | 12406:a3ddf3f42212 |
|---|---|
| 19 local ipairs = ipairs; | 19 local ipairs = ipairs; |
| 20 local type = type; | 20 local type = type; |
| 21 local s_gsub = string.gsub; | 21 local s_gsub = string.gsub; |
| 22 local s_sub = string.sub; | 22 local s_sub = string.sub; |
| 23 local s_find = string.find; | 23 local s_find = string.find; |
| 24 local t_move = table.move or require "util.table".move; | |
| 24 | 25 |
| 25 local valid_utf8 = require "util.encodings".utf8.valid; | 26 local valid_utf8 = require "util.encodings".utf8.valid; |
| 26 | 27 |
| 27 local do_pretty_printing, termcolours = pcall(require, "util.termcolours"); | 28 local do_pretty_printing, termcolours = pcall(require, "util.termcolours"); |
| 28 | 29 |
| 281 if old_namespaces then | 282 if old_namespaces then |
| 282 namespaces = {}; | 283 namespaces = {}; |
| 283 for k,v in pairs(old_namespaces) do namespaces[k] = v; end | 284 for k,v in pairs(old_namespaces) do namespaces[k] = v; end |
| 284 end | 285 end |
| 285 local new = { name = stanza.name, attr = attr, namespaces = namespaces, tags = tags }; | 286 local new = { name = stanza.name, attr = attr, namespaces = namespaces, tags = tags }; |
| 287 setmetatable(new, stanza_mt); | |
| 286 if not only_top then | 288 if not only_top then |
| 287 for i=1,#stanza do | 289 t_move(stanza, 1, #stanza, 1, new); |
| 288 local child = stanza[i]; | 290 t_move(stanza.tags, 1, #stanza.tags, 1, tags); |
| 289 if child.name then | 291 new:maptags(_clone); |
| 290 child = _clone(child); | 292 end |
| 291 t_insert(tags, child); | 293 return new; |
| 292 end | |
| 293 t_insert(new, child); | |
| 294 end | |
| 295 end | |
| 296 return setmetatable(new, stanza_mt); | |
| 297 end | 294 end |
| 298 | 295 |
| 299 local function clone(stanza, only_top) | 296 local function clone(stanza, only_top) |
| 300 if not is_stanza(stanza) then | 297 if not is_stanza(stanza) then |
| 301 error("bad argument to clone: expected stanza, got "..type(stanza)); | 298 error("bad argument to clone: expected stanza, got "..type(stanza)); |
