-
Notifications
You must be signed in to change notification settings - Fork 731
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 string view support (only impl, no tests) #627
Changes from 3 commits
49383d1
ee28175
f428647
bd0b8e6
2f1be3b
3e7ecd2
71ad493
db96936
16eb5da
e9da0a6
d7c5eee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -122,6 +122,26 @@ | |
# endif | ||
#endif | ||
|
||
// Detect if C++ standard supports 'string_view' (2017 or higher) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would propose simplifying this to enabling internal PUGI_HAS_STRING_VIEW define if Separately,
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A question regarding consistency: Probably for the same reason it would be the only macro that is actually undefined in the header. (there is no There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Apologies, I've misled you about the prefix; I forgot the conventions :) Indeed, it should be prefixed with |
||
#ifndef PUGI_STRING_VIEW_SUPPORTED | ||
# if defined(__cpp_lib_string_view) || __cplusplus >= 201703L || defined(_MSVC_LANG) && _MSVC_LANG >= 201703L | ||
brandl-muc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# define PUGI_STRING_VIEW_SUPPORTED 1 | ||
# endif | ||
#endif | ||
// Enable 'string_view' support if requested and supported | ||
#ifndef PUGI_HAS_STRING_VIEW | ||
# if defined(PUGI_ENABLE_STRING_VIEW) && !defined(PUGIXML_NO_STL) | ||
# ifdef PUGI_STRING_VIEW_SUPPORTED | ||
# define PUGI_HAS_STRING_VIEW | ||
# else | ||
# warning "Support for std::string_view was requested but is not supported by your C++ standard" | ||
brandl-muc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# endif | ||
# endif | ||
#endif | ||
#ifdef PUGI_HAS_STRING_VIEW | ||
# include <string_view> | ||
#endif | ||
|
||
// Character interface macros | ||
#ifdef PUGIXML_WCHAR_MODE | ||
# define PUGIXML_TEXT(t) L ## t | ||
|
@@ -140,6 +160,11 @@ namespace pugi | |
// String type used for operations that work with STL string; depends on PUGIXML_WCHAR_MODE | ||
typedef std::basic_string<PUGIXML_CHAR> string_t; | ||
#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>; | ||
brandl-muc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#endif | ||
} | ||
|
||
// The PugiXML namespace | ||
|
@@ -423,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); | ||
|
@@ -559,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); | ||
|
@@ -790,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); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here and elsewhere
const
should not be used for arguments as they are passed by valueThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is my usual style (as much
const
as possible), although I would use east const.I don't have any problems with changing this, but out of curiosity, could you elaborate on your rationale?
The fact that they are passed by value is orthogonal to the fact that the value is not changed.
And because it is not changed it can be made
const
and IMHO consequently should be madeconst
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I personally do not subscribe to the "what could be made const should be" when that doesn't affect the interface; in this case, the caller behavior does not change based on whether the argument is const or not. I am aware of the argument that it can help prevent mistakes but I have not seen these happen much in practice so I view this as syntactic noise. You're welcome to disagree of course, but for consistency this
const
should be removed: pugixml doesn't have a single function with a const parameter that is passed by value, and the only uses of const locals are that where the value is a compile-time const (when this code was written this seemed to enhance readability, whether this is true or not is subjective).