Tue, 10 Jul 2018 11:05:13 +0100
stanzacmp: A new iteration of the stanza matching algorithm
scansion/stanzacmp.lua | file | annotate | diff | comparison | revisions |
--- a/scansion/stanzacmp.lua Tue Jul 10 11:04:33 2018 +0100 +++ b/scansion/stanzacmp.lua Tue Jul 10 11:05:13 2018 +0100 @@ -71,26 +71,32 @@ end end - local stanza2_pos = 1; + local matched_children = {}; for _, child in ipairs(stanza1) do if type(child) == "table" or child:match("%S") then local match; -- Iterate through remaining nodes in stanza2, looking for a match + local stanza2_pos = 1; while stanza2_pos <= #stanza2 do - local child2 = stanza2[stanza2_pos]; - stanza2_pos = stanza2_pos + 1; + if not matched_children[stanza2_pos] then + local child2 = stanza2[stanza2_pos]; + stanza2_pos = stanza2_pos + 1; - if type(child2) == type(child) then - if type(child) == "table" and child2.name == child.name and child2.attr.xmlns == child.attr.xmlns then - -- Strict deep match - match = stanzas_match(child, child2); - elseif type(child) == "string" then -- Text nodes, must be equal, ignoring leading/trailing whitespace - match = trim(child) == trim(child2); + if type(child2) == type(child) then + if type(child) == "table" and child2.name == child.name and child2.attr.xmlns == child.attr.xmlns then + -- Strict deep match + match = stanzas_strict_match(child, child2); + elseif type(child) == "string" then -- Text nodes, must be equal, ignoring leading/trailing whitespace + match = trim(child) == trim(child2); + end + if match then + break; + end end - break; end end if not match then + --print(_, "No match for ", child:pretty_print()) return false; end end