From 7b1fc2f95d000317496107b736037311cb67ba4b Mon Sep 17 00:00:00 2001 From: Maya Warrier Date: Thu, 14 Sep 2023 21:07:22 -0400 Subject: [PATCH] Add an option to allow inf/nan even in json mode - Most JSON parsers offer this option too --- include/fast_float/float_common.h | 1 + tests/json_fmt.cpp | 11 +++++------ 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/include/fast_float/float_common.h b/include/fast_float/float_common.h index c101673..2ad9e8c 100644 --- a/include/fast_float/float_common.h +++ b/include/fast_float/float_common.h @@ -20,6 +20,7 @@ enum chars_format { hex = 1 << 3, no_infnan = 1 << 4, json = FASTFLOAT_JSONFMT | fixed | scientific | no_infnan, + json_or_infnan = FASTFLOAT_JSONFMT | fixed | scientific, general = fixed | scientific }; diff --git a/tests/json_fmt.cpp b/tests/json_fmt.cpp index 1fdd636..bdd32d9 100644 --- a/tests/json_fmt.cpp +++ b/tests/json_fmt.cpp @@ -10,16 +10,15 @@ int main() { - const std::vector expected{ -0.2, 0.02, 0.002, 1., 0. }; - const std::vector accept{ "-0.2", "0.02", "0.002", "1e+0000", "0e-2" }; - const std::vector reject{ "-.2", "00.02", "0.e+1", "00.e+1", ".25", "+0.25", "inf", "nan(snan)"}; - const auto fmt = fast_float::chars_format::json; + const std::vector expected{ -0.2, 0.02, 0.002, 1., 0., std::numeric_limits::infinity() }; + const std::vector accept{ "-0.2", "0.02", "0.002", "1e+0000", "0e-2", "inf" }; + const std::vector reject{ "-.2", "00.02", "0.e+1", "00.e+1", ".25", "+0.25", "inf", "nan(snan)" }; for (std::size_t i = 0; i < accept.size(); ++i) { const auto& f = accept[i]; double result; - auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result, fmt); + auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result, fast_float::chars_format::json_or_infnan); if (answer.ec != std::errc() || result != expected[i]) { std::cerr << "json fmt rejected valid json " << f << std::endl; return EXIT_FAILURE; @@ -30,7 +29,7 @@ int main() { const auto& f = reject[i]; double result; - auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result, fmt); + auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result, fast_float::chars_format::json); if (answer.ec == std::errc()) { std::cerr << "json fmt accepted invalid json " << f << std::endl; return EXIT_FAILURE;