Skip to content

Commit

Permalink
Number parsing based on yyjson. Fix roundtripping of uint64_t and add…
Browse files Browse the repository at this point in the history
…ed test case.
  • Loading branch information
mwalcott3 committed Dec 14, 2022
1 parent e804eec commit 6e7cab3
Show file tree
Hide file tree
Showing 4 changed files with 775 additions and 23 deletions.
44 changes: 21 additions & 23 deletions include/glaze/json/read.hpp
Expand Up @@ -14,6 +14,7 @@
#include "glaze/util/parse.hpp"
#include "glaze/util/for_each.hpp"
#include "glaze/file/file_ops.hpp"
#include "glaze/util/strod.hpp"

namespace glz
{
Expand Down Expand Up @@ -108,26 +109,22 @@ namespace glz

if constexpr (std::contiguous_iterator<std::decay_t<It>>)
{
if constexpr (std::is_floating_point_v<T>)
{
const auto size = std::distance(it, end);
const auto start = &*it;
auto [p, ec] = fast_float::from_chars(start, start + size, value);
if (ec != std::errc{}) [[unlikely]]
throw std::runtime_error("Failed to parse number");
it += (p - &*it);
}
else
{
double temp;
const auto size = std::distance(it, end);
const auto start = &*it;
auto [p, ec] = fast_float::from_chars(start, start + size, temp);
if (ec != std::errc{}) [[unlikely]]
throw std::runtime_error("Failed to parse number");
it += (p - &*it);
value = static_cast<T>(temp);
}
auto start = reinterpret_cast<const uint8_t *>(&*it);
auto s = parse_number(value, start);
if (!s) [[unlikely]]
throw std::runtime_error("Failed to parse number");
//if constexpr (std::floating_point<std::decay_t<T>>) {
// std::string ts = "2.2861746047729334,";
// double val2{};
// auto start2 = reinterpret_cast<const uint8_t*>(ts.data());
// auto s = parse_number(val2, start2);

// std::cout << "\nnum:" << value << ", " << val2 <<
// ", '"
// << std::string_view{&*it, std::size_t(start - reinterpret_cast<const uint8_t*>(&*it))}
// << "'\n";
//}
it += (start - reinterpret_cast<const uint8_t *>(&*it));
}
else {
double num;
Expand All @@ -139,10 +136,11 @@ namespace glz
buffer[i] = *it++;
++i;
}
auto [p, ec] = fast_float::from_chars(buffer, buffer + i, num);
if (ec != std::errc{}) [[unlikely]]
buffer[i] = '\0';
auto start = reinterpret_cast<const uint8_t*>(buffer);
auto s = parse_number(value, start);
if (!s) [[unlikely]]
throw std::runtime_error("Failed to parse number");
value = static_cast<T>(num);
}
}
};
Expand Down

0 comments on commit 6e7cab3

Please sign in to comment.