This commit is contained in:
andrei-aliashkevich 2018-03-18 02:21:08 +00:00 committed by GitHub
commit fdcdaea0e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 37 deletions

View File

@ -4411,48 +4411,50 @@ PUGI__NS_BEGIN
} }
} }
PUGI__FN void node_copy_tree(xml_node_struct* dn, xml_node_struct* sn) PUGI__FN void node_copy_tree(xml_node_struct* dn, xml_node_struct* sn, bool deep = true)
{ {
xml_allocator& alloc = get_allocator(dn); xml_allocator& alloc = get_allocator(dn);
xml_allocator* shared_alloc = (&alloc == &get_allocator(sn)) ? &alloc : 0; xml_allocator* shared_alloc = (&alloc == &get_allocator(sn)) ? &alloc : 0;
node_copy_contents(dn, sn, shared_alloc); node_copy_contents(dn, sn, shared_alloc);
xml_node_struct* dit = dn; if (deep)
xml_node_struct* sit = sn->first_child;
while (sit && sit != sn)
{ {
if (sit != dn) xml_node_struct* dit = dn;
xml_node_struct* sit = sn->first_child;
while (sit && sit != sn)
{ {
xml_node_struct* copy = append_new_node(dit, alloc, PUGI__NODETYPE(sit)); if (sit != dn)
if (copy)
{ {
node_copy_contents(copy, sit, shared_alloc); xml_node_struct* copy = append_new_node(dit, alloc, PUGI__NODETYPE(sit));
if (sit->first_child) if (copy)
{ {
dit = copy; node_copy_contents(copy, sit, shared_alloc);
sit = sit->first_child;
continue; if (sit->first_child)
{
dit = copy;
sit = sit->first_child;
continue;
}
} }
} }
}
// continue to the next node // continue to the next node
do do
{
if (sit->next_sibling)
{ {
sit = sit->next_sibling; if (sit->next_sibling)
break; {
} sit = sit->next_sibling;
break;
}
sit = sit->parent; sit = sit->parent;
dit = dit->parent; dit = dit->parent;
} while (sit != sn);
} }
while (sit != sn);
} }
} }
@ -5888,7 +5890,7 @@ namespace pugi
return result; return result;
} }
PUGI__FN xml_node xml_node::append_copy(const xml_node& proto) PUGI__FN xml_node xml_node::append_copy(const xml_node& proto, bool deep)
{ {
xml_node_type type_ = proto.type(); xml_node_type type_ = proto.type();
if (!impl::allow_insert_child(type(), type_)) return xml_node(); if (!impl::allow_insert_child(type(), type_)) return xml_node();
@ -5900,12 +5902,12 @@ namespace pugi
if (!n) return xml_node(); if (!n) return xml_node();
impl::append_node(n._root, _root); impl::append_node(n._root, _root);
impl::node_copy_tree(n._root, proto._root); impl::node_copy_tree(n._root, proto._root, deep);
return n; return n;
} }
PUGI__FN xml_node xml_node::prepend_copy(const xml_node& proto) PUGI__FN xml_node xml_node::prepend_copy(const xml_node& proto, bool deep)
{ {
xml_node_type type_ = proto.type(); xml_node_type type_ = proto.type();
if (!impl::allow_insert_child(type(), type_)) return xml_node(); if (!impl::allow_insert_child(type(), type_)) return xml_node();
@ -5917,12 +5919,12 @@ namespace pugi
if (!n) return xml_node(); if (!n) return xml_node();
impl::prepend_node(n._root, _root); impl::prepend_node(n._root, _root);
impl::node_copy_tree(n._root, proto._root); impl::node_copy_tree(n._root, proto._root, deep);
return n; return n;
} }
PUGI__FN xml_node xml_node::insert_copy_after(const xml_node& proto, const xml_node& node) PUGI__FN xml_node xml_node::insert_copy_after(const xml_node& proto, const xml_node& node, bool deep)
{ {
xml_node_type type_ = proto.type(); xml_node_type type_ = proto.type();
if (!impl::allow_insert_child(type(), type_)) return xml_node(); if (!impl::allow_insert_child(type(), type_)) return xml_node();
@ -5935,12 +5937,12 @@ namespace pugi
if (!n) return xml_node(); if (!n) return xml_node();
impl::insert_node_after(n._root, node._root); impl::insert_node_after(n._root, node._root);
impl::node_copy_tree(n._root, proto._root); impl::node_copy_tree(n._root, proto._root, deep);
return n; return n;
} }
PUGI__FN xml_node xml_node::insert_copy_before(const xml_node& proto, const xml_node& node) PUGI__FN xml_node xml_node::insert_copy_before(const xml_node& proto, const xml_node& node, bool deep)
{ {
xml_node_type type_ = proto.type(); xml_node_type type_ = proto.type();
if (!impl::allow_insert_child(type(), type_)) return xml_node(); if (!impl::allow_insert_child(type(), type_)) return xml_node();
@ -5953,7 +5955,7 @@ namespace pugi
if (!n) return xml_node(); if (!n) return xml_node();
impl::insert_node_before(n._root, node._root); impl::insert_node_before(n._root, node._root);
impl::node_copy_tree(n._root, proto._root); impl::node_copy_tree(n._root, proto._root, deep);
return n; return n;
} }

View File

@ -554,10 +554,10 @@ namespace pugi
xml_node insert_child_before(const char_t* name, const xml_node& node); xml_node insert_child_before(const char_t* name, const xml_node& node);
// Add a copy of the specified node as a child. Returns added node, or empty node on errors. // Add a copy of the specified node as a child. Returns added node, or empty node on errors.
xml_node append_copy(const xml_node& proto); xml_node append_copy(const xml_node& proto, bool deep = true);
xml_node prepend_copy(const xml_node& proto); xml_node prepend_copy(const xml_node& proto, bool deep = true);
xml_node insert_copy_after(const xml_node& proto, const xml_node& node); xml_node insert_copy_after(const xml_node& proto, const xml_node& node, bool deep = true);
xml_node insert_copy_before(const xml_node& proto, const xml_node& node); xml_node insert_copy_before(const xml_node& proto, const xml_node& node, bool deep = true);
// Move the specified node to become a child of this node. Returns moved node, or empty node on errors. // Move the specified node to become a child of this node. Returns moved node, or empty node on errors.
xml_node append_move(const xml_node& moved); xml_node append_move(const xml_node& moved);