Skip to content

Commit

Permalink
Add string_view_t overloads for set_name(), set_value() and set()
Browse files Browse the repository at this point in the history
  • Loading branch information
brandl-muc committed Sep 10, 2024
1 parent ee28175 commit f428647
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/pugixml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5441,6 +5441,13 @@ namespace pugi
return impl::strcpy_insitu(_attr->name, _attr->header, impl::xml_memory_page_name_allocated_mask, rhs, size);
}

#ifdef PUGI_HAS_STRING_VIEW
PUGI_IMPL_FN bool xml_attribute::set_name(const string_view_t rhs)
{
return set_name(rhs.data(), rhs.size());
}
#endif

PUGI_IMPL_FN bool xml_attribute::set_value(const char_t* rhs)
{
if (!_attr) return false;
Expand All @@ -5455,6 +5462,13 @@ namespace pugi
return impl::strcpy_insitu(_attr->value, _attr->header, impl::xml_memory_page_value_allocated_mask, rhs, size);
}

#ifdef PUGI_HAS_STRING_VIEW
PUGI_IMPL_FN bool xml_attribute::set_value(const string_view_t rhs)
{
return set_value(rhs.data(), rhs.size());
}
#endif

PUGI_IMPL_FN bool xml_attribute::set_value(int rhs)
{
if (!_attr) return false;
Expand Down Expand Up @@ -5848,6 +5862,13 @@ namespace pugi
return impl::strcpy_insitu(_root->name, _root->header, impl::xml_memory_page_name_allocated_mask, rhs, size);
}

#ifdef PUGI_HAS_STRING_VIEW
PUGI_IMPL_FN bool xml_node::set_name(const string_view_t rhs)
{
return set_name(rhs.data(), rhd.size());
}
#endif

PUGI_IMPL_FN bool xml_node::set_value(const char_t* rhs)
{
xml_node_type type_ = _root ? PUGI_IMPL_NODETYPE(_root) : node_null;
Expand All @@ -5868,6 +5889,13 @@ namespace pugi
return impl::strcpy_insitu(_root->value, _root->header, impl::xml_memory_page_value_allocated_mask, rhs, size);
}

#ifdef PUGI_HAS_STRING_VIEW
PUGI_IMPL_FN bool xml_node::set_value(const string_view_t rhs)
{
return set_value(rhs.data(), rhs.value());
}
#endif

PUGI_IMPL_FN xml_attribute xml_node::append_attribute(const char_t* name_)
{
if (!impl::allow_insert_attribute(type())) return xml_attribute();
Expand Down Expand Up @@ -6757,6 +6785,13 @@ namespace pugi
return dn ? impl::strcpy_insitu(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs, size) : false;
}

#ifdef PUGI_HAS_STRING_VIEW
PUGI_IMPL_FN bool xml_text::set(const string_view_t rhs)
{
return set(rhs.data(), rhs.size());
}
#endif

PUGI_IMPL_FN bool xml_text::set(int rhs)
{
xml_node_struct* dn = _data_new();
Expand Down
16 changes: 16 additions & 0 deletions src/pugixml.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ namespace pugi
#endif

#ifdef PUGI_HAS_STRING_VIEW
// String view type used for overloads of functions that accept strings; depends on PUGIXML_WCHAR_MODE
using string_view_t = std::basic_string_view<char_t>;
#endif
}
Expand Down Expand Up @@ -447,8 +448,14 @@ namespace pugi
// 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, size_t size);
#ifdef PUGI_HAS_STRING_VIEW
bool set_name(string_view_t rhs);
#endif
bool set_value(const char_t* rhs);
bool set_value(const char_t* rhs, size_t size);
#ifdef PUGI_HAS_STRING_VIEW
bool set_value(string_view_t rhs);
#endif

// Set attribute value with type conversion (numbers are converted to strings, boolean is converted to "true"/"false")
bool set_value(int rhs);
Expand Down Expand Up @@ -583,8 +590,14 @@ namespace pugi
// 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, size_t size);
#ifdef PUGI_HAS_STRING_VIEW
bool set_name(string_view_t rhs);
#endif
bool set_value(const char_t* rhs);
bool set_value(const char_t* rhs, size_t size);
#ifdef PUGI_HAS_STRING_VIEW
bool set_value(string_view_t rhs);
#endif

// Add attribute with specified name. Returns added attribute, or empty attribute on errors.
xml_attribute append_attribute(const char_t* name);
Expand Down Expand Up @@ -814,6 +827,9 @@ namespace pugi
// Set text (returns false if object is empty or there is not enough memory)
bool set(const char_t* rhs);
bool set(const char_t* rhs, size_t size);
#ifdef PUGI_HAS_STRING_VIEW
bool set(string_view_t rhs);
#endif

// Set text with type conversion (numbers are converted to strings, boolean is converted to "true"/"false")
bool set(int rhs);
Expand Down

0 comments on commit f428647

Please sign in to comment.