Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add some support for std::string_view #588

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
42 changes: 42 additions & 0 deletions src/pugixml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions src/pugixml.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
# include <iterator>
# include <iosfwd>
# include <string>
# include <string_view>
#endif

// Macro for deprecated features
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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!
Expand Down Expand Up @@ -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);
Expand Down
Loading