Use std::string_view for key type in json::Object

This commit is contained in:
Siarhei Fedartsou 2024-11-02 11:23:06 +01:00
parent 48e8382785
commit 97872e34d8
4 changed files with 11 additions and 11 deletions

View File

@ -30,7 +30,7 @@ struct V8Renderer
{ {
Napi::Value child; Napi::Value child;
std::visit(V8Renderer(env, child), keyValue.second); std::visit(V8Renderer(env, child), keyValue.second);
obj.Set(keyValue.first, child); obj.Set(keyValue.first.data(), child);
} }
out = obj; out = obj;
} }

View File

@ -104,7 +104,7 @@ using Value = std::variant<String, Number, Object, Array, True, False, Null>;
*/ */
struct Object struct Object
{ {
std::unordered_map<std::string, Value> values; std::unordered_map<std::string_view, Value> values;
}; };
/** /**

View File

@ -44,13 +44,13 @@ struct Comparator
bool operator()(const Object &lhs, const Object &rhs) const bool operator()(const Object &lhs, const Object &rhs) const
{ {
std::set<std::string> lhs_keys; std::set<std::string_view> lhs_keys;
for (const auto &key_value : lhs.values) for (const auto &key_value : lhs.values)
{ {
lhs_keys.insert(key_value.first); lhs_keys.insert(key_value.first);
} }
std::set<std::string> rhs_keys; std::set<std::string_view> rhs_keys;
for (const auto &key_value : rhs.values) for (const auto &key_value : rhs.values)
{ {
rhs_keys.insert(key_value.first); rhs_keys.insert(key_value.first);
@ -60,7 +60,7 @@ struct Comparator
{ {
if (rhs_keys.find(key) == rhs_keys.end()) if (rhs_keys.find(key) == rhs_keys.end())
{ {
reason = rhs_path + " doesn't have key \"" + key + "\""; reason = rhs_path + " doesn't have key \"" + std::string(key) + "\"";
return false; return false;
} }
} }
@ -69,7 +69,7 @@ struct Comparator
{ {
if (lhs_keys.find(key) == lhs_keys.end()) if (lhs_keys.find(key) == lhs_keys.end())
{ {
reason = lhs_path + " doesn't have key \"" + key + "\""; reason = lhs_path + " doesn't have key \"" + std::string(key) + "\"";
return false; return false;
} }
} }
@ -82,7 +82,7 @@ struct Comparator
const auto &rhs_child = rhs.values.find(key)->second; const auto &rhs_child = rhs.values.find(key)->second;
const auto &lhs_child = lhs.values.find(key)->second; const auto &lhs_child = lhs.values.find(key)->second;
auto is_same = auto is_same =
std::visit(Comparator(reason, lhs_path + "." + key, rhs_path + "." + key), std::visit(Comparator(reason, lhs_path + "." + std::string(key), rhs_path + "." + std::string(key)),
lhs_child, lhs_child,
rhs_child); rhs_child);
if (!is_same) if (!is_same)

View File

@ -97,7 +97,7 @@ template <typename Out> struct Renderer
void operator()(const Null &) { write<>("null"); } void operator()(const Null &) { write<>("null"); }
private: private:
void write(const std::string &str); void write(std::string_view str);
void write(const char *str, size_t size); void write(const char *str, size_t size);
void write(char ch); void write(char ch);
@ -110,7 +110,7 @@ template <typename Out> struct Renderer
Out &out; Out &out;
}; };
template <> void Renderer<std::vector<char>>::write(const std::string &str) template <> void Renderer<std::vector<char>>::write(std::string_view str)
{ {
out.insert(out.end(), str.begin(), str.end()); out.insert(out.end(), str.begin(), str.end());
} }
@ -122,7 +122,7 @@ template <> void Renderer<std::vector<char>>::write(const char *str, size_t size
template <> void Renderer<std::vector<char>>::write(char ch) { out.push_back(ch); } template <> void Renderer<std::vector<char>>::write(char ch) { out.push_back(ch); }
template <> void Renderer<std::ostream>::write(const std::string &str) { out << str; } template <> void Renderer<std::ostream>::write(std::string_view str) { out << str; }
template <> void Renderer<std::ostream>::write(const char *str, size_t size) template <> void Renderer<std::ostream>::write(const char *str, size_t size)
{ {
@ -131,7 +131,7 @@ template <> void Renderer<std::ostream>::write(const char *str, size_t size)
template <> void Renderer<std::ostream>::write(char ch) { out << ch; } template <> void Renderer<std::ostream>::write(char ch) { out << ch; }
template <> void Renderer<std::string>::write(const std::string &str) { out += str; } template <> void Renderer<std::string>::write(std::string_view str) { out += str; }
template <> void Renderer<std::string>::write(const char *str, size_t size) template <> void Renderer<std::string>::write(const char *str, size_t size)
{ {