From f4286474831b628d927ab66e8931caa1edb7afb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matth=C3=A4us=20Brandl?= Date: Tue, 10 Sep 2024 22:48:30 +0200 Subject: [PATCH] Add string_view_t overloads for set_name(), set_value() and set() --- src/pugixml.cpp | 35 +++++++++++++++++++++++++++++++++++ src/pugixml.hpp | 16 ++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/src/pugixml.cpp b/src/pugixml.cpp index 652d1e20..8fa592ef 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -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; @@ -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; @@ -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; @@ -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(); @@ -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(); diff --git a/src/pugixml.hpp b/src/pugixml.hpp index b8d6b3c1..2eafe2b4 100644 --- a/src/pugixml.hpp +++ b/src/pugixml.hpp @@ -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; #endif } @@ -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); @@ -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); @@ -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);