stl: add ability to set/get pugi::string_t to pugi elements

this allows using pugi with more convenience
e.g.: if (node.sname() == "foobar");
This commit is contained in:
Stanislav Ionascu 2016-02-20 11:30:46 +01:00
parent 8b60bbdce4
commit 2def7fe6e8
4 changed files with 73 additions and 0 deletions

View File

@ -335,9 +335,18 @@ namespace pugi
const char_t* name() const; const char_t* name() const;
const char_t* value() const; const char_t* value() const;
#ifndef PUGIXML_NO_STL
string_t sname() const { return name(); }
string_t svalue() const { return value(); }
#endif
// Get attribute value, or the default value if attribute is empty // Get attribute value, or the default value if attribute is empty
const char_t* as_string(const char_t* def = PUGIXML_TEXT("")) const; const char_t* as_string(const char_t* def = PUGIXML_TEXT("")) const;
#ifndef PUGIXML_NO_STL
string_t as_sstring(const string_t &def = string_t()) const { return as_string(def.c_str()); }
#endif
// Get attribute value as a number, or the default value if conversion did not succeed or attribute is empty // Get attribute value as a number, or the default value if conversion did not succeed or attribute is empty
int as_int(int def = 0) const; int as_int(int def = 0) const;
unsigned int as_uint(unsigned int def = 0) const; unsigned int as_uint(unsigned int def = 0) const;
@ -370,6 +379,10 @@ namespace pugi
bool set_value(unsigned long long rhs); bool set_value(unsigned long long rhs);
#endif #endif
#ifndef PUGIXML_NO_STL
bool set_value(const string_t &rhs) { return set_value(rhs.c_str()); }
#endif
// Set attribute value (equivalent to set_value without error checking) // Set attribute value (equivalent to set_value without error checking)
xml_attribute& operator=(const char_t* rhs); xml_attribute& operator=(const char_t* rhs);
xml_attribute& operator=(int rhs); xml_attribute& operator=(int rhs);
@ -385,6 +398,10 @@ namespace pugi
xml_attribute& operator=(unsigned long long rhs); xml_attribute& operator=(unsigned long long rhs);
#endif #endif
#ifndef PUGIXML_NO_STL
xml_attribute& operator=(const string_t &rhs) { return (*this = rhs.c_str()); }
#endif
// Get next/previous attribute in the attribute list of the parent node // Get next/previous attribute in the attribute list of the parent node
xml_attribute next_attribute() const; xml_attribute next_attribute() const;
xml_attribute previous_attribute() const; xml_attribute previous_attribute() const;
@ -448,6 +465,11 @@ namespace pugi
// Note: For <node>text</node> node.value() does not return "text"! Use child_value() or text() methods to access text inside nodes. // Note: For <node>text</node> node.value() does not return "text"! Use child_value() or text() methods to access text inside nodes.
const char_t* value() const; const char_t* value() const;
#ifndef PUGIXML_NO_STL
const string_t sname() const { return name(); }
const string_t svalue() const { return value(); }
#endif
// Get attribute list // Get attribute list
xml_attribute first_attribute() const; xml_attribute first_attribute() const;
xml_attribute last_attribute() const; xml_attribute last_attribute() const;
@ -701,6 +723,10 @@ namespace pugi
unsigned long long as_ullong(unsigned long long def = 0) const; unsigned long long as_ullong(unsigned long long def = 0) const;
#endif #endif
#ifndef PUGIXML_NO_STL
string_t as_sstring(const string_t &def = string_t()) const { return as_string(def.c_str()); }
#endif
// Get text as bool (returns true if first character is in '1tTyY' set), or the default value if object is empty // Get text as bool (returns true if first character is in '1tTyY' set), or the default value if object is empty
bool as_bool(bool def = false) const; bool as_bool(bool def = false) const;
@ -736,6 +762,10 @@ namespace pugi
xml_text& operator=(unsigned long long rhs); xml_text& operator=(unsigned long long rhs);
#endif #endif
#ifndef PUGIXML_NO_STL
xml_text& operator=(const string_t &rhs) { return (*this = rhs.c_str()); }
#endif
// Get the data node (node_pcdata or node_cdata) for this object // Get the data node (node_pcdata or node_cdata) for this object
xml_node data() const; xml_node data() const;
}; };
@ -1069,6 +1099,10 @@ namespace pugi
// Get variable name // Get variable name
const char_t* name() const; const char_t* name() const;
#ifndef PUGIXML_NO_STL
string_t sname() const { return name(); }
#endif
// Get variable type // Get variable type
xpath_value_type type() const; xpath_value_type type() const;
@ -1078,6 +1112,10 @@ namespace pugi
const char_t* get_string() const; const char_t* get_string() const;
const xpath_node_set& get_node_set() const; const xpath_node_set& get_node_set() const;
#ifndef PUGIXML_NO_STL
string_t get_sstring() const { return get_string(); }
#endif
// Set variable value; no type conversion is performed, false is returned on type mismatch error // Set variable value; no type conversion is performed, false is returned on type mismatch error
bool set(bool value); bool set(bool value);
bool set(double value); bool set(double value);

View File

@ -52,6 +52,13 @@ bool test_string_equal(const pugi::char_t* lhs, const pugi::char_t* rhs)
#endif #endif
} }
#ifndef PUGIXML_NO_STL
bool test_string_equal(const pugi::string_t& lhs, const pugi::string_t& rhs)
{
return (lhs == rhs);
}
#endif
bool test_node(const pugi::xml_node& node, const pugi::char_t* contents, const pugi::char_t* indent, unsigned int flags) bool test_node(const pugi::xml_node& node, const pugi::char_t* contents, const pugi::char_t* indent, unsigned int flags)
{ {
xml_writer_string writer; xml_writer_string writer;

View File

@ -32,6 +32,10 @@ struct test_runner
bool test_string_equal(const pugi::char_t* lhs, const pugi::char_t* rhs); bool test_string_equal(const pugi::char_t* lhs, const pugi::char_t* rhs);
#ifndef PUGIXML_NO_STL
bool test_string_equal(const pugi::string_t& lhs, const pugi::string_t& rhs);
#endif
template <typename Node> inline bool test_node_name_value(const Node& node, const pugi::char_t* name, const pugi::char_t* value) template <typename Node> inline bool test_node_name_value(const Node& node, const pugi::char_t* name, const pugi::char_t* value)
{ {
return test_string_equal(node.name(), name) && test_string_equal(node.value(), value); return test_string_equal(node.name(), name) && test_string_equal(node.value(), value);

View File

@ -59,6 +59,26 @@ TEST_XML_FLAGS(dom_text_as_string, "<node><a>foo</a><b><node/><![CDATA[bar]]></b
CHECK_STRING(xml_node().text().as_string(), STR("")); CHECK_STRING(xml_node().text().as_string(), STR(""));
} }
#ifndef PUGIXML_NO_STL
TEST_XML_FLAGS(dom_text_as_sstring, "<node><a>foo</a><b><node/><![CDATA[bar]]></b><c><?pi value?></c><d/></node>", parse_default | parse_pi)
{
xml_node node = doc.child(STR("node"));
CHECK_STRING(node.child(STR("a")).text().as_sstring(), string_t(STR("foo")));
CHECK_STRING(node.child(STR("a")).first_child().text().as_sstring(), string_t(STR("foo")));
CHECK_STRING(node.child(STR("b")).text().as_sstring(), string_t(STR("bar")));
CHECK_STRING(node.child(STR("b")).last_child().text().as_sstring(), string_t(STR("bar")));
CHECK_STRING(node.child(STR("c")).text().as_sstring(), string_t(STR("")));
CHECK_STRING(node.child(STR("c")).first_child().text().as_sstring(), string_t(STR("")));
CHECK_STRING(node.child(STR("d")).text().as_sstring(), string_t(STR("")));
CHECK_STRING(xml_node().text().as_sstring(), string_t(STR("")));
}
#endif
TEST_XML(dom_text_as_int, "<node><text1>1</text1><text2>-1</text2><text3>-2147483648</text3><text4>2147483647</text4><text5>0</text5></node>") TEST_XML(dom_text_as_int, "<node><text1>1</text1><text2>-1</text2><text3>-2147483648</text3><text4>2147483647</text4><text5>0</text5></node>")
{ {
xml_node node = doc.child(STR("node")); xml_node node = doc.child(STR("node"));
@ -442,4 +462,8 @@ TEST(dom_text_defaults)
CHECK(text.as_llong(42) == 42); CHECK(text.as_llong(42) == 42);
CHECK(text.as_ullong(42) == 42); CHECK(text.as_ullong(42) == 42);
#endif #endif
#ifndef PUGIXML_NO_STL
CHECK(text.as_sstring() == string_t());
#endif
} }