diff --git a/src/pugixml.cpp b/src/pugixml.cpp index cec242b..cb8bfa3 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -3132,7 +3132,7 @@ PUGI__NS_BEGIN PUGI__SCANFOR(s[0] == ']' && s[1] == ']' && PUGI__ENDSWITH(s[2], '>')); PUGI__CHECK_ERROR(status_bad_cdata, s); - cursor->value_len = s - cursor->value; + cursor->value_len = static_cast(s - cursor->value); *s++ = '\0'; // Zero-terminate this segment. } } @@ -5230,6 +5230,11 @@ namespace pugi return (_attr && _attr->value) ? _attr->value + 0 : def; } + PUGI__FN string_view_t xml_attribute::as_string_sv(string_view_t def) const + { + return (_attr && _attr->value) ? string_view_t(_attr->value + 0, _attr->value_len) : def; + } + PUGI__FN int xml_attribute::as_int(int def) const { return (_attr && _attr->value) ? impl::get_value_int(_attr->value) : def; @@ -5277,11 +5282,21 @@ namespace pugi return (_attr && _attr->name) ? _attr->name + 0 : PUGIXML_TEXT(""); } + PUGI__FN string_view_t xml_attribute::name_sv() const + { + return (_attr && _attr->name) ? string_view_t(_attr->name + 0, _attr->name_len) : string_view_t(PUGIXML_TEXT(""), 0); + } + PUGI__FN const char_t* xml_attribute::value() const { return (_attr && _attr->value) ? _attr->value + 0 : PUGIXML_TEXT(""); } + PUGI__FN string_view_t xml_attribute::value_sv() const + { + return (_attr && _attr->value) ? string_view_t(_attr->value + 0, _attr->value_len) : string_view_t(PUGIXML_TEXT(""), 0); + } + PUGI__FN size_t xml_attribute::hash_value() const { return static_cast(reinterpret_cast(_attr) / sizeof(xml_attribute_struct)); @@ -5556,6 +5571,11 @@ namespace pugi return (_root && _root->name) ? _root->name + 0 : PUGIXML_TEXT(""); } + PUGI__FN string_view_t xml_node::name_sv() const + { + return (_root && _root->name) ? string_view_t(_root->name + 0, _root->name_len) : string_view_t(PUGIXML_TEXT(""), 0); + } + PUGI__FN xml_node_type xml_node::type() const { return _root ? PUGI__NODETYPE(_root) : node_null; @@ -5566,6 +5586,11 @@ namespace pugi return (_root && _root->value) ? _root->value + 0 : PUGIXML_TEXT(""); } + PUGI__FN string_view_t xml_node::value_sv() const + { + return (_root && _root->value) ? string_view_t(_root->value + 0, _root->value_len) : string_view_t(PUGIXML_TEXT(""), 0); + } + PUGI__FN xml_node xml_node::child(const char_t* name_) const { if (!_root) return xml_node(); @@ -5683,11 +5708,31 @@ namespace pugi return PUGIXML_TEXT(""); } + PUGI__FN string_view_t xml_node::child_value_sv() const + { + if (!_root) return string_view_t(PUGIXML_TEXT(""), 0); + + // element nodes can have value if parse_embed_pcdata was used + if (PUGI__NODETYPE(_root) == node_element && _root->value) + return string_view_t(_root->value, _root->value_len); + + for (xml_node_struct* i = _root->first_child; i; i = i->next_sibling) + if (impl::is_text_node(i) && i->value) + return string_view_t(i->value, i->value_len); + + return string_view_t(PUGIXML_TEXT(""), 0); + } + PUGI__FN const char_t* xml_node::child_value(const char_t* name_) const { return child(name_).child_value(); } + PUGI__FN string_view_t xml_node::child_value_sv(const char_t* name_) const + { + return child(name_).child_value_sv(); + } + PUGI__FN xml_attribute xml_node::first_attribute() const { return _root ? xml_attribute(_root->first_attribute) : xml_attribute(); @@ -6513,6 +6558,13 @@ namespace pugi return (d && d->value) ? d->value + 0 : PUGIXML_TEXT(""); } + PUGI__FN string_view_t xml_text::get_sv() const + { + xml_node_struct* d = _data(); + + return (d && d->value) ? string_view_t(d->value + 0, d->value_len) : string_view_t(PUGIXML_TEXT(""), 0); + } + PUGI__FN const char_t* xml_text::as_string(const char_t* def) const { xml_node_struct* d = _data(); @@ -6520,6 +6572,13 @@ namespace pugi return (d && d->value) ? d->value + 0 : def; } + PUGI__FN string_view_t xml_text::as_string_sv(string_view_t def) const + { + xml_node_struct* d = _data(); + + return (d && d->value) ? string_view_t(d->value + 0, d->value_len) : def; + } + PUGI__FN int xml_text::as_int(int def) const { xml_node_struct* d = _data(); diff --git a/src/pugixml.hpp b/src/pugixml.hpp index 53edae8..a43c0a7 100644 --- a/src/pugixml.hpp +++ b/src/pugixml.hpp @@ -520,10 +520,14 @@ namespace pugi // Get attribute name/value, or "" if attribute is empty const char_t* name() const; + string_view_t name_sv() const; + const char_t* value() const; + string_view_t value_sv() const; // Get attribute value, or the default value if attribute is empty const char_t* as_string(const char_t* def = PUGIXML_TEXT("")) const; + string_view_t as_string_sv(string_view_t def = string_view_t(PUGIXML_TEXT(""), 0)) const; // 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; @@ -632,10 +636,12 @@ namespace pugi // Get node name, or "" if node is empty or it has no name const char_t* name() const; + string_view_t name_sv() const; // Get node value, or "" if node is empty or it has no value // Note: For text node.value() does not return "text"! Use child_value() or text() methods to access text inside nodes. const char_t* value() const; + string_view_t value_sv() const; // Get attribute list xml_attribute first_attribute() const; @@ -669,9 +675,11 @@ namespace pugi // Get child value of current node; that is, value of the first child node of type PCDATA/CDATA const char_t* child_value() const; + string_view_t child_value_sv() const; // Get child value of child with specified name. Equivalent to child(name).child_value(). const char_t* child_value(const char_t* name) const; + string_view_t child_value_sv(const char_t* name) const; // 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(string_view_t rhs, bool shallow_copy = false); @@ -881,9 +889,11 @@ namespace pugi // Get text, or "" if object is empty const char_t* get() const; + string_view_t get_sv() const; // Get text, or the default value if object is empty const char_t* as_string(const char_t* def = PUGIXML_TEXT("")) const; + string_view_t as_string_sv(string_view_t def = string_view_t(PUGIXML_TEXT(""), 0)) const; // Get text as a number, or the default value if conversion did not succeed or object is empty int as_int(int def = 0) const;