-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #220 from mayawarrier/main
Add support for parsing numbers according to JSON format
- Loading branch information
Showing
5 changed files
with
79 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
|
||
#include <cstdlib> | ||
#include <iostream> | ||
#include <vector> | ||
|
||
// test that this option is ignored | ||
#define FASTFLOAT_ALLOWS_LEADING_PLUS | ||
|
||
#include "fast_float/fast_float.h" | ||
|
||
int main() | ||
{ | ||
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, 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; | ||
} | ||
} | ||
|
||
for (std::size_t i = 0; i < reject.size(); ++i) | ||
{ | ||
const auto& f = reject[i]; | ||
double result; | ||
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; | ||
} | ||
} | ||
|
||
return EXIT_SUCCESS; | ||
} |