mirror of
https://github.com/jbeder/yaml-cpp.git
synced 2025-09-08 20:31:17 +00:00
node/convert: support conversion for std::string_view (#1148)
This commit is contained in:
@@ -18,6 +18,10 @@
|
|||||||
#include <valarray>
|
#include <valarray>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#if __cplusplus >= 201703L
|
||||||
|
#include <string_view>
|
||||||
|
#endif
|
||||||
|
|
||||||
#include "yaml-cpp/binary.h"
|
#include "yaml-cpp/binary.h"
|
||||||
#include "yaml-cpp/node/impl.h"
|
#include "yaml-cpp/node/impl.h"
|
||||||
#include "yaml-cpp/node/iterator.h"
|
#include "yaml-cpp/node/iterator.h"
|
||||||
@@ -89,6 +93,20 @@ struct convert<char[N]> {
|
|||||||
static Node encode(const char* rhs) { return Node(rhs); }
|
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 <>
|
template <>
|
||||||
struct convert<_Null> {
|
struct convert<_Null> {
|
||||||
static Node encode(const _Null& /* rhs */) { return Node(); }
|
static Node encode(const _Null& /* rhs */) { return Node(); }
|
||||||
|
@@ -356,6 +356,15 @@ TEST(NodeTest, ConstInteratorOnSequence) {
|
|||||||
EXPECT_EQ(3, count);
|
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) {
|
TEST(NodeTest, SimpleSubkeys) {
|
||||||
Node node;
|
Node node;
|
||||||
node["device"]["udid"] = "12345";
|
node["device"]["udid"] = "12345";
|
||||||
|
Reference in New Issue
Block a user