Merge pull request #477 from zeux/compactopt

Optimize compact mode
This commit is contained in:
Arseny Kapoulkine 2022-05-31 20:00:01 -05:00 committed by GitHub
commit 2639dfd053
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1276,12 +1276,14 @@ PUGI__NS_BEGIN
child->parent = parent;
if (node->next_sibling)
node->next_sibling->prev_sibling_c = child;
xml_node_struct* next = node->next_sibling;
if (next)
next->prev_sibling_c = child;
else
parent->first_child->prev_sibling_c = child;
child->next_sibling = node->next_sibling;
child->next_sibling = next;
child->prev_sibling_c = node;
node->next_sibling = child;
@ -1293,12 +1295,14 @@ PUGI__NS_BEGIN
child->parent = parent;
if (node->prev_sibling_c->next_sibling)
node->prev_sibling_c->next_sibling = child;
xml_node_struct* prev = node->prev_sibling_c;
if (prev->next_sibling)
prev->next_sibling = child;
else
parent->first_child = child;
child->prev_sibling_c = node->prev_sibling_c;
child->prev_sibling_c = prev;
child->next_sibling = node;
node->prev_sibling_c = child;
@ -1308,15 +1312,18 @@ PUGI__NS_BEGIN
{
xml_node_struct* parent = node->parent;
if (node->next_sibling)
node->next_sibling->prev_sibling_c = node->prev_sibling_c;
else
parent->first_child->prev_sibling_c = node->prev_sibling_c;
xml_node_struct* next = node->next_sibling;
xml_node_struct* prev = node->prev_sibling_c;
if (node->prev_sibling_c->next_sibling)
node->prev_sibling_c->next_sibling = node->next_sibling;
if (next)
next->prev_sibling_c = prev;
else
parent->first_child = node->next_sibling;
parent->first_child->prev_sibling_c = prev;
if (prev->next_sibling)
prev->next_sibling = next;
else
parent->first_child = next;
node->parent = 0;
node->prev_sibling_c = 0;
@ -1360,39 +1367,46 @@ PUGI__NS_BEGIN
inline void insert_attribute_after(xml_attribute_struct* attr, xml_attribute_struct* place, xml_node_struct* node)
{
if (place->next_attribute)
place->next_attribute->prev_attribute_c = attr;
xml_attribute_struct* next = place->next_attribute;
if (next)
next->prev_attribute_c = attr;
else
node->first_attribute->prev_attribute_c = attr;
attr->next_attribute = place->next_attribute;
attr->next_attribute = next;
attr->prev_attribute_c = place;
place->next_attribute = attr;
}
inline void insert_attribute_before(xml_attribute_struct* attr, xml_attribute_struct* place, xml_node_struct* node)
{
if (place->prev_attribute_c->next_attribute)
place->prev_attribute_c->next_attribute = attr;
xml_attribute_struct* prev = place->prev_attribute_c;
if (prev->next_attribute)
prev->next_attribute = attr;
else
node->first_attribute = attr;
attr->prev_attribute_c = place->prev_attribute_c;
attr->prev_attribute_c = prev;
attr->next_attribute = place;
place->prev_attribute_c = attr;
}
inline void remove_attribute(xml_attribute_struct* attr, xml_node_struct* node)
{
if (attr->next_attribute)
attr->next_attribute->prev_attribute_c = attr->prev_attribute_c;
else
node->first_attribute->prev_attribute_c = attr->prev_attribute_c;
xml_attribute_struct* next = attr->next_attribute;
xml_attribute_struct* prev = attr->prev_attribute_c;
if (attr->prev_attribute_c->next_attribute)
attr->prev_attribute_c->next_attribute = attr->next_attribute;
if (next)
next->prev_attribute_c = prev;
else
node->first_attribute = attr->next_attribute;
node->first_attribute->prev_attribute_c = prev;
if (prev->next_attribute)
prev->next_attribute = next;
else
node->first_attribute = next;
attr->prev_attribute_c = 0;
attr->next_attribute = 0;
@ -5191,53 +5205,72 @@ namespace pugi
PUGI__FN xml_attribute xml_attribute::next_attribute() const
{
return _attr ? xml_attribute(_attr->next_attribute) : xml_attribute();
if (!_attr) return xml_attribute();
return xml_attribute(_attr->next_attribute);
}
PUGI__FN xml_attribute xml_attribute::previous_attribute() const
{
return _attr && _attr->prev_attribute_c->next_attribute ? xml_attribute(_attr->prev_attribute_c) : xml_attribute();
if (!_attr) return xml_attribute();
xml_attribute_struct* prev = _attr->prev_attribute_c;
return prev->next_attribute ? xml_attribute(prev) : xml_attribute();
}
PUGI__FN const char_t* xml_attribute::as_string(const char_t* def) const
{
return (_attr && _attr->value) ? _attr->value + 0 : def;
if (!_attr) return def;
const char_t* value = _attr->value;
return value ? value : def;
}
PUGI__FN int xml_attribute::as_int(int def) const
{
return (_attr && _attr->value) ? impl::get_value_int(_attr->value) : def;
if (!_attr) return def;
const char_t* value = _attr->value;
return value ? impl::get_value_int(value) : def;
}
PUGI__FN unsigned int xml_attribute::as_uint(unsigned int def) const
{
return (_attr && _attr->value) ? impl::get_value_uint(_attr->value) : def;
if (!_attr) return def;
const char_t* value = _attr->value;
return value ? impl::get_value_uint(value) : def;
}
PUGI__FN double xml_attribute::as_double(double def) const
{
return (_attr && _attr->value) ? impl::get_value_double(_attr->value) : def;
if (!_attr) return def;
const char_t* value = _attr->value;
return value ? impl::get_value_double(value) : def;
}
PUGI__FN float xml_attribute::as_float(float def) const
{
return (_attr && _attr->value) ? impl::get_value_float(_attr->value) : def;
if (!_attr) return def;
const char_t* value = _attr->value;
return value ? impl::get_value_float(value) : def;
}
PUGI__FN bool xml_attribute::as_bool(bool def) const
{
return (_attr && _attr->value) ? impl::get_value_bool(_attr->value) : def;
if (!_attr) return def;
const char_t* value = _attr->value;
return value ? impl::get_value_bool(value) : def;
}
#ifdef PUGIXML_HAS_LONG_LONG
PUGI__FN long long xml_attribute::as_llong(long long def) const
{
return (_attr && _attr->value) ? impl::get_value_llong(_attr->value) : def;
if (!_attr) return def;
const char_t* value = _attr->value;
return value ? impl::get_value_llong(value) : def;
}
PUGI__FN unsigned long long xml_attribute::as_ullong(unsigned long long def) const
{
return (_attr && _attr->value) ? impl::get_value_ullong(_attr->value) : def;
if (!_attr) return def;
const char_t* value = _attr->value;
return value ? impl::get_value_ullong(value) : def;
}
#endif
@ -5248,12 +5281,16 @@ namespace pugi
PUGI__FN const char_t* xml_attribute::name() const
{
return (_attr && _attr->name) ? _attr->name + 0 : PUGIXML_TEXT("");
if (!_attr) return PUGIXML_TEXT("");
const char_t* name = _attr->name;
return name ? name : PUGIXML_TEXT("");
}
PUGI__FN const char_t* xml_attribute::value() const
{
return (_attr && _attr->value) ? _attr->value + 0 : PUGIXML_TEXT("");
if (!_attr) return PUGIXML_TEXT("");
const char_t* value = _attr->value;
return value ? value : PUGIXML_TEXT("");
}
PUGI__FN size_t xml_attribute::hash_value() const
@ -5527,7 +5564,9 @@ namespace pugi
PUGI__FN const char_t* xml_node::name() const
{
return (_root && _root->name) ? _root->name + 0 : PUGIXML_TEXT("");
if (!_root) return PUGIXML_TEXT("");
const char_t* name = _root->name;
return name ? name : PUGIXML_TEXT("");
}
PUGI__FN xml_node_type xml_node::type() const
@ -5537,7 +5576,9 @@ namespace pugi
PUGI__FN const char_t* xml_node::value() const
{
return (_root && _root->value) ? _root->value + 0 : PUGIXML_TEXT("");
if (!_root) return PUGIXML_TEXT("");
const char_t* value = _root->value;
return value ? value : PUGIXML_TEXT("");
}
PUGI__FN xml_node xml_node::child(const char_t* name_) const
@ -5545,7 +5586,11 @@ namespace pugi
if (!_root) return xml_node();
for (xml_node_struct* i = _root->first_child; i; i = i->next_sibling)
if (i->name && impl::strequal(name_, i->name)) return xml_node(i);
{
const char_t* iname = i->name;
if (iname && impl::strequal(name_, iname))
return xml_node(i);
}
return xml_node();
}
@ -5555,8 +5600,11 @@ namespace pugi
if (!_root) return xml_attribute();
for (xml_attribute_struct* i = _root->first_attribute; i; i = i->next_attribute)
if (i->name && impl::strequal(name_, i->name))
{
const char_t* iname = i->name;
if (iname && impl::strequal(name_, iname))
return xml_attribute(i);
}
return xml_attribute();
}
@ -5566,7 +5614,11 @@ namespace pugi
if (!_root) return xml_node();
for (xml_node_struct* i = _root->next_sibling; i; i = i->next_sibling)
if (i->name && impl::strequal(name_, i->name)) return xml_node(i);
{
const char_t* iname = i->name;
if (iname && impl::strequal(name_, iname))
return xml_node(i);
}
return xml_node();
}
@ -5581,7 +5633,11 @@ namespace pugi
if (!_root) return xml_node();
for (xml_node_struct* i = _root->prev_sibling_c; i->next_sibling; i = i->prev_sibling_c)
if (i->name && impl::strequal(name_, i->name)) return xml_node(i);
{
const char_t* iname = i->name;
if (iname && impl::strequal(name_, iname))
return xml_node(i);
}
return xml_node();
}
@ -5597,24 +5653,30 @@ namespace pugi
// optimistically search from hint up until the end
for (xml_attribute_struct* i = hint; i; i = i->next_attribute)
if (i->name && impl::strequal(name_, i->name))
{
const char_t* iname = i->name;
if (iname && impl::strequal(name_, iname))
{
// update hint to maximize efficiency of searching for consecutive attributes
hint_._attr = i->next_attribute;
return xml_attribute(i);
}
}
// wrap around and search from the first attribute until the hint
// 'j' null pointer check is technically redundant, but it prevents a crash in case the assertion above fails
for (xml_attribute_struct* j = _root->first_attribute; j && j != hint; j = j->next_attribute)
if (j->name && impl::strequal(name_, j->name))
{
const char_t* jname = j->name;
if (jname && impl::strequal(name_, jname))
{
// update hint to maximize efficiency of searching for consecutive attributes
hint_._attr = j->next_attribute;
return xml_attribute(j);
}
}
return xml_attribute();
}
@ -5622,9 +5684,8 @@ namespace pugi
PUGI__FN xml_node xml_node::previous_sibling() const
{
if (!_root) return xml_node();
if (_root->prev_sibling_c->next_sibling) return xml_node(_root->prev_sibling_c);
else return xml_node();
xml_node_struct* prev = _root->prev_sibling_c;
return prev->next_sibling ? xml_node(prev) : xml_node();
}
PUGI__FN xml_node xml_node::parent() const
@ -5651,8 +5712,11 @@ namespace pugi
return _root->value;
for (xml_node_struct* i = _root->first_child; i; i = i->next_sibling)
if (impl::is_text_node(i) && i->value)
return i->value;
{
const char_t* ivalue = i->value;
if (impl::is_text_node(i) && ivalue)
return ivalue;
}
return PUGIXML_TEXT("");
}
@ -5664,22 +5728,28 @@ namespace pugi
PUGI__FN xml_attribute xml_node::first_attribute() const
{
return _root ? xml_attribute(_root->first_attribute) : xml_attribute();
if (!_root) return xml_attribute();
return xml_attribute(_root->first_attribute);
}
PUGI__FN xml_attribute xml_node::last_attribute() const
{
return _root && _root->first_attribute ? xml_attribute(_root->first_attribute->prev_attribute_c) : xml_attribute();
if (!_root) return xml_attribute();
xml_attribute_struct* first = _root->first_attribute;
return first ? xml_attribute(first->prev_attribute_c) : xml_attribute();
}
PUGI__FN xml_node xml_node::first_child() const
{
return _root ? xml_node(_root->first_child) : xml_node();
if (!_root) return xml_node();
return xml_node(_root->first_child);
}
PUGI__FN xml_node xml_node::last_child() const
{
return _root && _root->first_child ? xml_node(_root->first_child->prev_sibling_c) : xml_node();
if (!_root) return xml_node();
xml_node_struct* first = _root->first_child;
return first ? xml_node(first->prev_sibling_c) : xml_node();
}
PUGI__FN bool xml_node::set_name(const char_t* rhs)
@ -6205,12 +6275,22 @@ namespace pugi
if (!_root) return xml_node();
for (xml_node_struct* i = _root->first_child; i; i = i->next_sibling)
if (i->name && impl::strequal(name_, i->name))
{
const char_t* iname = i->name;
if (iname && impl::strequal(name_, iname))
{
for (xml_attribute_struct* a = i->first_attribute; a; a = a->next_attribute)
if (a->name && impl::strequal(attr_name, a->name) && impl::strequal(attr_value, a->value ? a->value + 0 : PUGIXML_TEXT("")))
return xml_node(i);
{
const char_t* aname = a->name;
if (aname && impl::strequal(attr_name, aname))
{
const char_t* avalue = a->value;
if (impl::strequal(attr_value, avalue ? avalue : PUGIXML_TEXT("")))
return xml_node(i);
}
}
}
}
return xml_node();
}
@ -6221,8 +6301,15 @@ namespace pugi
for (xml_node_struct* i = _root->first_child; i; i = i->next_sibling)
for (xml_attribute_struct* a = i->first_attribute; a; a = a->next_attribute)
if (a->name && impl::strequal(attr_name, a->name) && impl::strequal(attr_value, a->value ? a->value + 0 : PUGIXML_TEXT("")))
return xml_node(i);
{
const char_t* aname = a->name;
if (aname && impl::strequal(attr_name, aname))
{
const char_t* avalue = a->value;
if (impl::strequal(attr_value, avalue ? avalue : PUGIXML_TEXT("")))
return xml_node(i);
}
}
return xml_node();
}
@ -6236,8 +6323,9 @@ namespace pugi
for (xml_node_struct* i = _root; i; i = i->parent)
{
const char_t* iname = i->name;
offset += (i != _root);
offset += i->name ? impl::strlength(i->name) : 0;
offset += iname ? impl::strlength(iname) : 0;
}
string_t result;
@ -6248,12 +6336,13 @@ namespace pugi
if (j != _root)
result[--offset] = delimiter;
if (j->name)
const char_t* jname = j->name;
if (jname)
{
size_t length = impl::strlength(j->name);
size_t length = impl::strlength(jname);
offset -= length;
memcpy(&result[offset], j->name, length * sizeof(char_t));
memcpy(&result[offset], jname, length * sizeof(char_t));
}
}
@ -6291,7 +6380,8 @@ namespace pugi
{
for (xml_node_struct* j = context._root->first_child; j; j = j->next_sibling)
{
if (j->name && impl::strequalrange(j->name, path_segment, static_cast<size_t>(path_segment_end - path_segment)))
const char_t* jname = j->name;
if (jname && impl::strequalrange(jname, path_segment, static_cast<size_t>(path_segment_end - path_segment)))
{
xml_node subsearch = xml_node(j).first_element_by_path(next_segment, delimiter);
@ -6483,65 +6573,74 @@ namespace pugi
PUGI__FN const char_t* xml_text::get() const
{
xml_node_struct* d = _data();
return (d && d->value) ? d->value + 0 : PUGIXML_TEXT("");
if (!d) return PUGIXML_TEXT("");
const char_t* value = d->value;
return value ? value : PUGIXML_TEXT("");
}
PUGI__FN const char_t* xml_text::as_string(const char_t* def) const
{
xml_node_struct* d = _data();
return (d && d->value) ? d->value + 0 : def;
if (!d) return def;
const char_t* value = d->value;
return value ? value : def;
}
PUGI__FN int xml_text::as_int(int def) const
{
xml_node_struct* d = _data();
return (d && d->value) ? impl::get_value_int(d->value) : def;
if (!d) return def;
const char_t* value = d->value;
return value ? impl::get_value_int(value) : def;
}
PUGI__FN unsigned int xml_text::as_uint(unsigned int def) const
{
xml_node_struct* d = _data();
return (d && d->value) ? impl::get_value_uint(d->value) : def;
if (!d) return def;
const char_t* value = d->value;
return value ? impl::get_value_uint(value) : def;
}
PUGI__FN double xml_text::as_double(double def) const
{
xml_node_struct* d = _data();
return (d && d->value) ? impl::get_value_double(d->value) : def;
if (!d) return def;
const char_t* value = d->value;
return value ? impl::get_value_double(value) : def;
}
PUGI__FN float xml_text::as_float(float def) const
{
xml_node_struct* d = _data();
return (d && d->value) ? impl::get_value_float(d->value) : def;
if (!d) return def;
const char_t* value = d->value;
return value ? impl::get_value_float(value) : def;
}
PUGI__FN bool xml_text::as_bool(bool def) const
{
xml_node_struct* d = _data();
return (d && d->value) ? impl::get_value_bool(d->value) : def;
if (!d) return def;
const char_t* value = d->value;
return value ? impl::get_value_bool(value) : def;
}
#ifdef PUGIXML_HAS_LONG_LONG
PUGI__FN long long xml_text::as_llong(long long def) const
{
xml_node_struct* d = _data();
return (d && d->value) ? impl::get_value_llong(d->value) : def;
if (!d) return def;
const char_t* value = d->value;
return value ? impl::get_value_llong(value) : def;
}
PUGI__FN unsigned long long xml_text::as_ullong(unsigned long long def) const
{
xml_node_struct* d = _data();
return (d && d->value) ? impl::get_value_ullong(d->value) : def;
if (!d) return def;
const char_t* value = d->value;
return value ? impl::get_value_ullong(value) : def;
}
#endif