Merge pull request #522 from Ferenc-/followup-on-pr-490

Followup on pr 490
This commit is contained in:
Arseny Kapoulkine 2022-10-07 21:42:41 -07:00 committed by GitHub
commit c342266fae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 92 additions and 3 deletions

View File

@ -5372,11 +5372,16 @@ namespace pugi
return impl::strcpy_insitu(_attr->name, _attr->header, impl::xml_memory_page_name_allocated_mask, rhs, impl::strlength(rhs)); return impl::strcpy_insitu(_attr->name, _attr->header, impl::xml_memory_page_name_allocated_mask, rhs, impl::strlength(rhs));
} }
PUGI__FN bool xml_attribute::set_value(const char_t* rhs) PUGI__FN bool xml_attribute::set_value(const char_t* rhs, size_t sz)
{ {
if (!_attr) return false; if (!_attr) return false;
return impl::strcpy_insitu(_attr->value, _attr->header, impl::xml_memory_page_value_allocated_mask, rhs, impl::strlength(rhs)); return impl::strcpy_insitu(_attr->value, _attr->header, impl::xml_memory_page_value_allocated_mask, rhs, sz);
}
PUGI__FN bool xml_attribute::set_value(const char_t* rhs)
{
return set_value(rhs, impl::strlength(rhs));
} }
PUGI__FN bool xml_attribute::set_value(int rhs) PUGI__FN bool xml_attribute::set_value(int rhs)
@ -5762,6 +5767,16 @@ namespace pugi
return impl::strcpy_insitu(_root->name, _root->header, impl::xml_memory_page_name_allocated_mask, rhs, impl::strlength(rhs)); return impl::strcpy_insitu(_root->name, _root->header, impl::xml_memory_page_name_allocated_mask, rhs, impl::strlength(rhs));
} }
PUGI__FN bool xml_node::set_value(const char_t* rhs, size_t sz)
{
xml_node_type type_ = _root ? PUGI__NODETYPE(_root) : node_null;
if (type_ != node_pcdata && type_ != node_cdata && type_ != node_comment && type_ != node_pi && type_ != node_doctype)
return false;
return impl::strcpy_insitu(_root->value, _root->header, impl::xml_memory_page_value_allocated_mask, rhs, sz);
}
PUGI__FN bool xml_node::set_value(const char_t* rhs) PUGI__FN bool xml_node::set_value(const char_t* rhs)
{ {
xml_node_type type_ = _root ? PUGI__NODETYPE(_root) : node_null; xml_node_type type_ = _root ? PUGI__NODETYPE(_root) : node_null;
@ -6644,6 +6659,13 @@ namespace pugi
} }
#endif #endif
PUGI__FN bool xml_text::set(const char_t* rhs, size_t sz)
{
xml_node_struct* dn = _data_new();
return dn ? impl::strcpy_insitu(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs, sz) : false;
}
PUGI__FN bool xml_text::set(const char_t* rhs) PUGI__FN bool xml_text::set(const char_t* rhs)
{ {
xml_node_struct* dn = _data_new(); xml_node_struct* dn = _data_new();

View File

@ -418,6 +418,7 @@ namespace pugi
// Set attribute name/value (returns false if attribute is empty or there is not enough memory) // Set attribute name/value (returns false if attribute is empty or there is not enough memory)
bool set_name(const char_t* rhs); bool set_name(const char_t* rhs);
bool set_value(const char_t* rhs, size_t sz);
bool set_value(const char_t* rhs); bool set_value(const char_t* rhs);
// Set attribute value with type conversion (numbers are converted to strings, boolean is converted to "true"/"false") // Set attribute value with type conversion (numbers are converted to strings, boolean is converted to "true"/"false")
@ -552,6 +553,7 @@ namespace pugi
// Set node name/value (returns false if node is empty, there is not enough memory, or node can not have name/value) // Set node name/value (returns false if node is empty, there is not enough memory, or node can not have name/value)
bool set_name(const char_t* rhs); bool set_name(const char_t* rhs);
bool set_value(const char_t* rhs, size_t sz);
bool set_value(const char_t* rhs); bool set_value(const char_t* rhs);
// Add attribute with specified name. Returns added attribute, or empty attribute on errors. // Add attribute with specified name. Returns added attribute, or empty attribute on errors.
@ -777,6 +779,7 @@ namespace pugi
bool as_bool(bool def = false) const; bool as_bool(bool def = false) const;
// Set text (returns false if object is empty or there is not enough memory) // Set text (returns false if object is empty or there is not enough memory)
bool set(const char_t* rhs, size_t sz);
bool set(const char_t* rhs); bool set(const char_t* rhs);
// Set text with type conversion (numbers are converted to strings, boolean is converted to "true"/"false") // Set text with type conversion (numbers are converted to strings, boolean is converted to "true"/"false")

View File

@ -70,7 +70,13 @@ TEST_XML(dom_attr_set_value, "<node/>")
CHECK(node.append_attribute(STR("attr8")).set_value(true)); CHECK(node.append_attribute(STR("attr8")).set_value(true));
CHECK(!xml_attribute().set_value(true)); CHECK(!xml_attribute().set_value(true));
CHECK_NODE(node, STR("<node attr1=\"v1\" attr2=\"-2147483647\" attr3=\"-2147483648\" attr4=\"4294967295\" attr5=\"4294967294\" attr6=\"0.5\" attr7=\"0.25\" attr8=\"true\"/>")); CHECK(node.append_attribute(STR("attr9")).set_value(STR("v2"), 2));
CHECK(!xml_attribute().set_value(STR("v2")));
CHECK(node.append_attribute(STR("attr10")).set_value(STR("v3foobar"), 2));
CHECK(!xml_attribute().set_value(STR("v3")));
CHECK_NODE(node, STR("<node attr1=\"v1\" attr2=\"-2147483647\" attr3=\"-2147483648\" attr4=\"4294967295\" attr5=\"4294967294\" attr6=\"0.5\" attr7=\"0.25\" attr8=\"true\" attr9=\"v2\" attr10=\"v3\"/>"));
} }
#if LONG_MAX > 2147483647 #if LONG_MAX > 2147483647
@ -209,6 +215,24 @@ TEST_XML(dom_node_set_value, "<node>text</node>")
CHECK_NODE(doc, STR("<node>no text</node>")); CHECK_NODE(doc, STR("<node>no text</node>"));
} }
TEST_XML(dom_node_set_value_partially_with_size, "<node>text</node>")
{
CHECK(doc.child(STR("node")).first_child().set_value(STR("no text"), 2));
CHECK(!doc.child(STR("node")).set_value(STR("no text"), 2));
CHECK(!xml_node().set_value(STR("no text"), 2));
CHECK_NODE(doc, STR("<node>no</node>"));
}
TEST_XML(dom_node_set_value_with_size, "<node>text</node>")
{
CHECK(doc.child(STR("node")).first_child().set_value(STR("no text"), 7));
CHECK(!doc.child(STR("node")).set_value(STR("no text"), 7));
CHECK(!xml_node().set_value(STR("no text"), 7));
CHECK_NODE(doc, STR("<node>no text</node>"));
}
TEST_XML(dom_node_set_value_allocated, "<node>text</node>") TEST_XML(dom_node_set_value_allocated, "<node>text</node>")
{ {
CHECK(doc.child(STR("node")).first_child().set_value(STR("no text"))); CHECK(doc.child(STR("node")).first_child().set_value(STR("no text")));

View File

@ -249,6 +249,46 @@ TEST_XML(dom_text_set, "<node/>")
CHECK_NODE(node, STR("<node>foobarfoobar</node>")); CHECK_NODE(node, STR("<node>foobarfoobar</node>"));
} }
TEST_XML(dom_text_set_with_size, "<node/>")
{
xml_node node = doc.child(STR("node"));
xml_text t = node.text();
t.set(STR(""), 0);
CHECK(node.first_child().type() == node_pcdata);
CHECK_NODE(node, STR("<node></node>"));
t.set(STR("boo"), 3);
CHECK(node.first_child().type() == node_pcdata);
CHECK(node.first_child() == node.last_child());
CHECK_NODE(node, STR("<node>boo</node>"));
t.set(STR("foobarfoobar"), 12);
CHECK(node.first_child().type() == node_pcdata);
CHECK(node.first_child() == node.last_child());
CHECK_NODE(node, STR("<node>foobarfoobar</node>"));
}
TEST_XML(dom_text_set_partially_with_size, "<node/>")
{
xml_node node = doc.child(STR("node"));
xml_text t = node.text();
t.set(STR("foo"), 0);
CHECK(node.first_child().type() == node_pcdata);
CHECK_NODE(node, STR("<node></node>"));
t.set(STR("boofoo"), 3);
CHECK(node.first_child().type() == node_pcdata);
CHECK(node.first_child() == node.last_child());
CHECK_NODE(node, STR("<node>boo</node>"));
t.set(STR("foobarfoobar"), 3);
CHECK(node.first_child().type() == node_pcdata);
CHECK(node.first_child() == node.last_child());
CHECK_NODE(node, STR("<node>foo</node>"));
}
TEST_XML(dom_text_assign, "<node/>") TEST_XML(dom_text_assign, "<node/>")
{ {
xml_node node = doc.child(STR("node")); xml_node node = doc.child(STR("node"));