Merge pull request 'BSDとWindowsでの修正' (#1) from bsd into master

Reviewed-on: https://gitler.moe/suwako/LibJson/pulls/1

テクニカル諏訪子 <suwako@anon.gitler.moe>、2026-06-30 03:19
コミット 6491d54760e54bef6ef33954d688e8746af89c58 ・親 02c53c7 ・親 32ee505 ・ファイルを見る

3個のファイル、+24 -15

@@ -69,6 +69,12 @@
6969 public:
7070 Object() = default;
7171
72+ Object(const Object &) = delete;
73+ Object &operator=(const Object &) = delete;
74+
75+ Object(Object &&) noexcept = default;
76+ Object &operator=(Object &&) noexcept = default;
77+
7278 Value &operator[](std::string_view key);
7379 const Value &operator[](std::string_view key) const;
7480
@@ -86,7 +92,7 @@
8692 auto end() const { return m_Data.end(); }
8793
8894 private:
89- using Pair = std::pair<string, Value>;
95+ using Pair = std::pair<string, std::unique_ptr<Value>>;
9096 vector<Pair> m_Data;
9197 std::unordered_map<string, size_t, StringHash, std::equal_to<>> m_Index;
9298 }; // struct Object
@@ -138,8 +144,8 @@
138144 Value(u32 n) : m_Value(static_cast<f64>(n)) {}
139145 Value(string s) : m_Value(std::move(s)) {}
140146 Value(cstr s) : m_Value(string(s)) {}
141- Value(Array arr) : m_Value(std::move(arr)) {}
142- Value(Object obj) : m_Value(std::move(obj)) {}
147+ Value(Array &&arr) : m_Value(std::move(arr)) {}
148+ Value(Object &&obj) : m_Value(std::move(obj)) {}
143149
144150 public:
145151 bool is_null() const { return std::holds_alternative<std::monostate>(m_Value); }
変更 src/json.cc
@@ -207,11 +207,12 @@
207207 std::optional<f64> Parser::parse_number() {
208208 cstr start = m_Text.data() + m_Pos;
209209 f64 num = 0.0;
210- auto [ptr, ec] = std::from_chars(start, m_Text.data() + m_Text.size(), num);
210+ char *end = nullptr;
211+ num = std::strtod(start, &end);
211212
212- if (ec != std::errc() || ptr == start) return std::nullopt;
213+ if (end == start) return std::nullopt;
213214
214- m_Pos += (ptr - start);
215+ m_Pos += (end - start);
215216 return num;
216217 }
217218
@@ -297,7 +298,7 @@
297298
298299 out += serialize_string(key);
299300 out += opts.pretty ? ": " : ":";
300- out += val.serialize_impl(opts, depth + 1);
301+ out += val->serialize_impl(opts, depth + 1);
301302
302303 if (++i < obj.size())
303304 out += ",";
@@ -343,23 +344,25 @@
343344 }
344345
345346 void Object::insert(string key, Value value) {
347+ auto ptr = std::make_unique<Value>(std::move(value));
348+
346349 auto it = m_Index.find(key);
347350 if (it != m_Index.end()) {
348- m_Data[it->second].second = std::move(value);
351+ m_Data[it->second].second = std::move(ptr);
349352 } else {
350353 m_Index[key] = m_Data.size();
351- m_Data.emplace_back(std::move(key), std::move(value));
354+ m_Data.emplace_back(std::move(key), std::move(ptr));
352355 }
353356 }
354357
355358 Value *Object::get(std::string_view key) {
356359 auto it = m_Index.find(key);
357- return it != m_Index.end() ? &m_Data[it->second].second : nullptr;
360+ return it != m_Index.end() ? m_Data[it->second].second.get() : nullptr;
358361 }
359362
360363 const Value *Object::get(std::string_view key) const {
361364 auto it = m_Index.find(key);
362- return it != m_Index.end() ? &m_Data[it->second].second : nullptr;
365+ return it != m_Index.end() ? m_Data[it->second].second.get() : nullptr;
363366 }
364367
365368 bool Object::contains(std::string_view key) const {
変更 tests/main.cc
@@ -169,10 +169,10 @@
169169 assert(arr[0].as_object().get("componentType")->as_number() == 5123);
170170 assert(arr[0].as_object().get("count")->as_number() == 3);
171171 assert(arr[0].as_object().get("type")->as_string() == "SCALAR");
172- auto max0 = arr[0].as_object().get("max")->as_array();
172+ const auto &max0 = arr[0].as_object().get("max")->as_array();
173173 assert(max0.size() == 1);
174174 assert(max0[0].get_number() == 2);
175- auto min0 = arr[0].as_object().get("min")->as_array();
175+ const auto &min0 = arr[0].as_object().get("min")->as_array();
176176 assert(min0.size() == 1);
177177 assert(min0[0].get_number() == 0);
178178
@@ -182,12 +182,12 @@
182182 assert(arr[1].as_object().get("componentType")->as_number() == 5126);
183183 assert(arr[1].as_object().get("count")->as_number() == 3);
184184 assert(arr[1].as_object().get("type")->as_string() == "VEC3");
185- auto max1 = arr[1].as_object().get("max")->as_array();
185+ const auto &max1 = arr[1].as_object().get("max")->as_array();
186186 assert(max1.size() == 3);
187187 assert(max1[0].get_number() == 1.0);
188188 assert(max1[1].get_number() == 1.0);
189189 assert(max1[2].get_number() == 0.0);
190- auto min1 = arr[1].as_object().get("min")->as_array();
190+ const auto &min1 = arr[1].as_object().get("min")->as_array();
191191 assert(min1.size() == 3);
192192 assert(min1[0].get_number() == 0.0);
193193 assert(min1[1].get_number() == 0.0);