add some support for std::string_view

This commit is contained in:
Daniel Ottiger 2023-10-16 11:17:08 +02:00
parent 2e357d19a3
commit a7d7c36f2c
3 changed files with 52 additions and 0 deletions

View File

@ -49,6 +49,7 @@ option(PUGIXML_NO_XPATH "Disable XPath" OFF)
option(PUGIXML_NO_STL "Disable STL" OFF)
option(PUGIXML_NO_EXCEPTIONS "Disable Exceptions" OFF)
mark_as_advanced(PUGIXML_NO_XPATH PUGIXML_NO_STL PUGIXML_NO_EXCEPTIONS)
set(CMAKE_CXX_STANDARD 20)
set(PUGIXML_PUBLIC_DEFINITIONS
$<$<BOOL:${PUGIXML_WCHAR_MODE}>:PUGIXML_WCHAR_MODE>

View File

@ -5440,6 +5440,11 @@ namespace pugi
return impl::strcpy_insitu(_attr->value, _attr->header, impl::xml_memory_page_value_allocated_mask, rhs, size);
}
PUGI_IMPL_FN bool xml_attribute::set_value(string_view_t rhs)
{
return set_value(rhs.data(), rhs.size());
}
PUGI_IMPL_FN bool xml_attribute::set_value(int rhs)
{
if (!_attr) return false;
@ -5588,6 +5593,11 @@ namespace pugi
return xml_object_range<xml_attribute_iterator>(attributes_begin(), attributes_end());
}
PUGI_IMPL_FN xml_object_range<xml_named_node_iterator> xml_node::children(const string_t& name) const
{
return children(name.c_str());
}
PUGI_IMPL_FN bool xml_node::operator==(const xml_node& r) const
{
return (_root == r._root);
@ -5656,6 +5666,11 @@ namespace pugi
return xml_node();
}
PUGI_IMPL_FN xml_node xml_node::child(const string_t& name) const
{
return child(name.c_str());
}
PUGI_IMPL_FN xml_attribute xml_node::attribute(const char_t* name_) const
{
if (!_root) return xml_attribute();
@ -5684,6 +5699,11 @@ namespace pugi
return xml_node();
}
PUGI_IMPL_FN xml_node xml_node::next_sibling(const string_t& name) const
{
return next_sibling(name.c_str());
}
PUGI_IMPL_FN xml_node xml_node::next_sibling() const
{
return _root ? xml_node(_root->next_sibling) : xml_node();
@ -5870,6 +5890,11 @@ namespace pugi
return a;
}
PUGI_IMPL_FN xml_attribute xml_node::append_attribute(const pugi::string_t& name)
{
return append_attribute(name.c_str());
}
PUGI_IMPL_FN xml_attribute xml_node::prepend_attribute(const char_t* name_)
{
if (!impl::allow_insert_attribute(type())) return xml_attribute();
@ -6072,6 +6097,11 @@ namespace pugi
return result;
}
PUGI_IMPL_FN xml_node xml_node::append_child(const string_t& name)
{
return append_child(name.c_str());
}
PUGI_IMPL_FN xml_node xml_node::prepend_child(const char_t* name_)
{
xml_node result = prepend_child(node_element);
@ -7395,6 +7425,18 @@ namespace pugi
return load_buffer(contents, impl::strlength(contents) * sizeof(char_t), options, encoding);
}
PUGI_IMPL_FN xml_parse_result xml_document::load_string(string_view_t contents, unsigned int options)
{
// Force native encoding (skip autodetection)
#ifdef PUGIXML_WCHAR_MODE
xml_encoding encoding = encoding_wchar;
#else
xml_encoding encoding = encoding_utf8;
#endif
return load_buffer(contents.data(), contents.size() * sizeof(char_t), options, encoding);
}
PUGI_IMPL_FN xml_parse_result xml_document::load(const char_t* contents, unsigned int options)
{
return load_string(contents, options);

View File

@ -36,6 +36,7 @@
# include <iterator>
# include <iosfwd>
# include <string>
# include <string_view>
#endif
// Macro for deprecated features
@ -139,6 +140,7 @@ namespace pugi
#ifndef PUGIXML_NO_STL
// String type used for operations that work with STL string; depends on PUGIXML_WCHAR_MODE
typedef std::basic_string<PUGIXML_CHAR, std::char_traits<PUGIXML_CHAR>, std::allocator<PUGIXML_CHAR> > string_t;
typedef std::basic_string_view<PUGIXML_CHAR> string_view_t;
#endif
}
@ -425,6 +427,7 @@ namespace pugi
bool set_name(const char_t* rhs, size_t size);
bool set_value(const char_t* rhs);
bool set_value(const char_t* rhs, size_t size);
bool set_value(string_view_t rhs);
// Set attribute value with type conversion (numbers are converted to strings, boolean is converted to "true"/"false")
bool set_value(int rhs);
@ -543,8 +546,10 @@ namespace pugi
// Get child, attribute or next/previous sibling with the specified name
xml_node child(const char_t* name) const;
xml_node child(const string_t& name) const;
xml_attribute attribute(const char_t* name) const;
xml_node next_sibling(const char_t* name) const;
xml_node next_sibling(const string_t& name) const;
xml_node previous_sibling(const char_t* name) const;
// Get attribute, starting the search from a hint (and updating hint so that searching for a sequence of attributes is fast)
@ -564,6 +569,7 @@ namespace pugi
// Add attribute with specified name. Returns added attribute, or empty attribute on errors.
xml_attribute append_attribute(const char_t* name);
xml_attribute append_attribute(const string_t& name);
xml_attribute prepend_attribute(const char_t* name);
xml_attribute insert_attribute_after(const char_t* name, const xml_attribute& attr);
xml_attribute insert_attribute_before(const char_t* name, const xml_attribute& attr);
@ -582,6 +588,7 @@ namespace pugi
// Add child element with specified name. Returns added node, or empty node on errors.
xml_node append_child(const char_t* name);
xml_node append_child(const string_t& name);
xml_node prepend_child(const char_t* name);
xml_node insert_child_after(const char_t* name, const xml_node& node);
xml_node insert_child_before(const char_t* name, const xml_node& node);
@ -719,6 +726,7 @@ namespace pugi
// Range-based for support
xml_object_range<xml_node_iterator> children() const;
xml_object_range<xml_attribute_iterator> attributes() const;
xml_object_range<xml_named_node_iterator> children(const string_t& name) const;
// Range-based for support for all children with the specified name
// Note: name pointer must have a longer lifetime than the returned object; be careful with passing temporaries!
@ -1081,6 +1089,7 @@ namespace pugi
// Load document from zero-terminated string. No encoding conversions are applied.
xml_parse_result load_string(const char_t* contents, unsigned int options = parse_default);
xml_parse_result load_string(string_view_t contents, unsigned int options = parse_default);
// Load document from file
xml_parse_result load_file(const char* path, unsigned int options = parse_default, xml_encoding encoding = encoding_auto);