mirror of
https://github.com/fastfloat/fast_float.git
synced 2025-12-08 01:36:49 +08:00
Add an option to allow inf/nan even in json mode
- Most JSON parsers offer this option too
This commit is contained in:
parent
ce562d9c65
commit
7b1fc2f95d
@ -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
|
||||
};
|
||||
|
||||
|
||||
@ -10,16 +10,15 @@
|
||||
|
||||
int main()
|
||||
{
|
||||
const std::vector<double> expected{ -0.2, 0.02, 0.002, 1., 0. };
|
||||
const std::vector<std::string> accept{ "-0.2", "0.02", "0.002", "1e+0000", "0e-2" };
|
||||
const std::vector<std::string> 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<double> expected{ -0.2, 0.02, 0.002, 1., 0., std::numeric_limits<double>::infinity() };
|
||||
const std::vector<std::string> accept{ "-0.2", "0.02", "0.002", "1e+0000", "0e-2", "inf" };
|
||||
const std::vector<std::string> 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;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user