Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/include/cx_json_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,24 @@ namespace JSON
fmap([] (char) { return 0; }, make_char_parser('0')) | int1_parser(),
[] (char sign, int i) { return sign == '+' ? i : -i; });

constexpr auto frac_parser = make_char_parser('.') < int0_parser();

constexpr auto mantissa_parser = combine(
integral_parser, option(0, frac_parser),
[] (int i, int f) -> double {
constexpr auto frac_parser = make_char_parser('.') < combine(
many(make_char_parser('0'), 0, [](int acc, auto) -> int { return acc+1; }), // count the number of leading zeros
int1_parser(),
[](int exp, int f) -> double {
double d = 0;
while (f > 0) {
d += f % 10;
d /= 10;
f /= 10;
}
return i + d;
while (exp--) {
d /= 10;
}
return d;
});

constexpr auto mantissa_parser = combine(integral_parser, option(0.0, frac_parser), std::plus{});

constexpr auto e_parser = make_char_parser('e') | make_char_parser('E');
constexpr auto sign_parser = make_char_parser('+') | neg_parser;
constexpr auto exponent_parser =
Expand Down
5 changes: 5 additions & 0 deletions src/test/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ void number_parse_tests()
constexpr auto number_val = JSON::number_parser()("456.123e-1"sv);
static_assert(number_val && number_val->first == 456.123e-1);
}

{
constexpr auto number_val = JSON::number_parser()("0.0625"sv);
static_assert(number_val && number_val->first == 0.0625);
}
}

void numobjects_tests()
Expand Down