Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
gaoqiong
yaml-cpp
Commits
35b44980
Unverified
Commit
35b44980
authored
Jul 25, 2023
by
Kefu Chai
Committed by
GitHub
Jul 24, 2023
Browse files
node/convert: support conversion for std::string_view (#1148)
parent
b8882652
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
27 additions
and
0 deletions
+27
-0
include/yaml-cpp/node/convert.h
include/yaml-cpp/node/convert.h
+18
-0
test/node/node_test.cpp
test/node/node_test.cpp
+9
-0
No files found.
include/yaml-cpp/node/convert.h
View file @
35b44980
...
...
@@ -18,6 +18,10 @@
#include <valarray>
#include <vector>
#if __cplusplus >= 201703L
#include <string_view>
#endif
#include "yaml-cpp/binary.h"
#include "yaml-cpp/node/impl.h"
#include "yaml-cpp/node/iterator.h"
...
...
@@ -89,6 +93,20 @@ struct convert<char[N]> {
static
Node
encode
(
const
char
*
rhs
)
{
return
Node
(
rhs
);
}
};
#if __cplusplus >= 201703L
template
<
>
struct
convert
<
std
::
string_view
>
{
static
Node
encode
(
std
::
string_view
rhs
)
{
return
Node
(
std
::
string
(
rhs
));
}
static
bool
decode
(
const
Node
&
node
,
std
::
string_view
&
rhs
)
{
if
(
!
node
.
IsScalar
())
return
false
;
rhs
=
node
.
Scalar
();
return
true
;
}
};
#endif
template
<
>
struct
convert
<
_Null
>
{
static
Node
encode
(
const
_Null
&
/* rhs */
)
{
return
Node
();
}
...
...
test/node/node_test.cpp
View file @
35b44980
...
...
@@ -356,6 +356,15 @@ TEST(NodeTest, ConstInteratorOnSequence) {
EXPECT_EQ
(
3
,
count
);
}
#if __cplusplus >= 201703L
TEST
(
NodeTest
,
StdStringViewAsKey
)
{
Node
node
;
std
::
string_view
key
=
"username"
;
node
[
key
]
=
"monkey"
;
EXPECT_EQ
(
"monkey"
,
node
[
key
].
as
<
std
::
string
>
());
}
#endif
TEST
(
NodeTest
,
SimpleSubkeys
)
{
Node
node
;
node
[
"device"
][
"udid"
]
=
"12345"
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment