Skip to content

Commit 97872e3

Browse files
Use std::string_view for key type in json::Object
1 parent 48e8382 commit 97872e3

File tree

4 files changed

+11
-11
lines changed

4 files changed

+11
-11
lines changed

include/nodejs/json_v8_renderer.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ struct V8Renderer
3030
{
3131
Napi::Value child;
3232
std::visit(V8Renderer(env, child), keyValue.second);
33-
obj.Set(keyValue.first, child);
33+
obj.Set(keyValue.first.data(), child);
3434
}
3535
out = obj;
3636
}

include/util/json_container.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ using Value = std::variant<String, Number, Object, Array, True, False, Null>;
104104
*/
105105
struct Object
106106
{
107-
std::unordered_map<std::string, Value> values;
107+
std::unordered_map<std::string_view, Value> values;
108108
};
109109

110110
/**

include/util/json_deep_compare.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ struct Comparator
4444

4545
bool operator()(const Object &lhs, const Object &rhs) const
4646
{
47-
std::set<std::string> lhs_keys;
47+
std::set<std::string_view> lhs_keys;
4848
for (const auto &key_value : lhs.values)
4949
{
5050
lhs_keys.insert(key_value.first);
5151
}
5252

53-
std::set<std::string> rhs_keys;
53+
std::set<std::string_view> rhs_keys;
5454
for (const auto &key_value : rhs.values)
5555
{
5656
rhs_keys.insert(key_value.first);
@@ -60,7 +60,7 @@ struct Comparator
6060
{
6161
if (rhs_keys.find(key) == rhs_keys.end())
6262
{
63-
reason = rhs_path + " doesn't have key \"" + key + "\"";
63+
reason = rhs_path + " doesn't have key \"" + std::string(key) + "\"";
6464
return false;
6565
}
6666
}
@@ -69,7 +69,7 @@ struct Comparator
6969
{
7070
if (lhs_keys.find(key) == lhs_keys.end())
7171
{
72-
reason = lhs_path + " doesn't have key \"" + key + "\"";
72+
reason = lhs_path + " doesn't have key \"" + std::string(key) + "\"";
7373
return false;
7474
}
7575
}
@@ -82,7 +82,7 @@ struct Comparator
8282
const auto &rhs_child = rhs.values.find(key)->second;
8383
const auto &lhs_child = lhs.values.find(key)->second;
8484
auto is_same =
85-
std::visit(Comparator(reason, lhs_path + "." + key, rhs_path + "." + key),
85+
std::visit(Comparator(reason, lhs_path + "." + std::string(key), rhs_path + "." + std::string(key)),
8686
lhs_child,
8787
rhs_child);
8888
if (!is_same)

include/util/json_renderer.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ template <typename Out> struct Renderer
9797
void operator()(const Null &) { write<>("null"); }
9898

9999
private:
100-
void write(const std::string &str);
100+
void write(std::string_view str);
101101
void write(const char *str, size_t size);
102102
void write(char ch);
103103

@@ -110,7 +110,7 @@ template <typename Out> struct Renderer
110110
Out &out;
111111
};
112112

113-
template <> void Renderer<std::vector<char>>::write(const std::string &str)
113+
template <> void Renderer<std::vector<char>>::write(std::string_view str)
114114
{
115115
out.insert(out.end(), str.begin(), str.end());
116116
}
@@ -122,7 +122,7 @@ template <> void Renderer<std::vector<char>>::write(const char *str, size_t size
122122

123123
template <> void Renderer<std::vector<char>>::write(char ch) { out.push_back(ch); }
124124

125-
template <> void Renderer<std::ostream>::write(const std::string &str) { out << str; }
125+
template <> void Renderer<std::ostream>::write(std::string_view str) { out << str; }
126126

127127
template <> void Renderer<std::ostream>::write(const char *str, size_t size)
128128
{
@@ -131,7 +131,7 @@ template <> void Renderer<std::ostream>::write(const char *str, size_t size)
131131

132132
template <> void Renderer<std::ostream>::write(char ch) { out << ch; }
133133

134-
template <> void Renderer<std::string>::write(const std::string &str) { out += str; }
134+
template <> void Renderer<std::string>::write(std::string_view str) { out += str; }
135135

136136
template <> void Renderer<std::string>::write(const char *str, size_t size)
137137
{

0 commit comments

Comments
 (0)