From 95dedd0aed265a8db79363b46c441dad17415488 Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Sun, 9 Mar 2025 15:13:43 -0400 Subject: [PATCH 1/4] turning json option into macro parameter --- include/fast_float/ascii_number.h | 10 +++++----- include/fast_float/parse_number.h | 5 +++-- tests/json_fmt.cpp | 2 +- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/include/fast_float/ascii_number.h b/include/fast_float/ascii_number.h index 9001016..e5d0e63 100644 --- a/include/fast_float/ascii_number.h +++ b/include/fast_float/ascii_number.h @@ -279,7 +279,7 @@ report_parse_error(UC const *p, parse_error error) { // Assuming that you use no more than 19 digits, this will // parse an ASCII string. -template +template fastfloat_really_inline FASTFLOAT_CONSTEXPR20 parsed_number_string_t parse_number_string(UC const *p, UC const *pend, parse_options_t options) noexcept { @@ -294,13 +294,13 @@ parse_number_string(UC const *p, UC const *pend, // C++17 20.19.3.(7.1) explicitly forbids '+' sign here if ((*p == UC('-')) || (uint64_t(fmt & chars_format::allow_leading_plus) && - !uint64_t(fmt & detail::basic_json_fmt) && *p == UC('+'))) { + !basic_json_fmt && *p == UC('+'))) { ++p; if (p == pend) { return report_parse_error( p, parse_error::missing_integer_or_dot_after_sign); } - if (uint64_t(fmt & detail::basic_json_fmt)) { + if (basic_json_fmt) { if (!is_integer(*p)) { // a sign must be followed by an integer return report_parse_error(p, parse_error::missing_integer_after_sign); @@ -329,7 +329,7 @@ parse_number_string(UC const *p, UC const *pend, UC const *const end_of_integer_part = p; int64_t digit_count = int64_t(end_of_integer_part - start_digits); answer.integer = span(start_digits, size_t(digit_count)); - if (uint64_t(fmt & detail::basic_json_fmt)) { + if (basic_json_fmt) { // at least 1 digit in integer part, without leading zeros if (digit_count == 0) { return report_parse_error(p, parse_error::no_digits_in_integer_part); @@ -358,7 +358,7 @@ parse_number_string(UC const *p, UC const *pend, answer.fraction = span(before, size_t(p - before)); digit_count -= exponent; } - if (uint64_t(fmt & detail::basic_json_fmt)) { + if (basic_json_fmt) { // at least 1 digit in fractional part if (has_decimal_point && exponent == 0) { return report_parse_error(p, diff --git a/include/fast_float/parse_number.h b/include/fast_float/parse_number.h index 0dbb3a1..9814254 100644 --- a/include/fast_float/parse_number.h +++ b/include/fast_float/parse_number.h @@ -304,8 +304,9 @@ from_chars_float_advanced(UC const *first, UC const *last, T &value, answer.ptr = first; return answer; } - parsed_number_string_t pns = - parse_number_string(first, last, options); + parsed_number_string_t pns = uint64_t(fmt & detail::basic_json_fmt) ? + parse_number_string(first, last, options) : + parse_number_string(first, last, options); if (!pns.valid) { if (uint64_t(fmt & chars_format::no_infnan)) { answer.ec = std::errc::invalid_argument; diff --git a/tests/json_fmt.cpp b/tests/json_fmt.cpp index f04207a..1ba0d5a 100644 --- a/tests/json_fmt.cpp +++ b/tests/json_fmt.cpp @@ -131,7 +131,7 @@ int main() { for (std::size_t i = 0; i < reject.size(); ++i) { auto const &f = reject[i].input; auto const &expected_reason = reject[i].reason; - auto answer = fast_float::parse_number_string( + auto answer = fast_float::parse_number_string( f.data(), f.data() + f.size(), fast_float::parse_options( fast_float::chars_format::json | From 6f0049a2e76fb9f76895907ba20f7911928a54ec Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Sun, 9 Mar 2025 15:14:52 -0400 Subject: [PATCH 2/4] lint --- benchmarks/benchmark.cpp | 2 +- include/fast_float/ascii_number.h | 5 ++--- include/fast_float/parse_number.h | 7 ++++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/benchmarks/benchmark.cpp b/benchmarks/benchmark.cpp index 42be62b..5645da2 100644 --- a/benchmarks/benchmark.cpp +++ b/benchmarks/benchmark.cpp @@ -236,7 +236,7 @@ int main(int argc, char **argv) { << std::endl; #endif } - if(argc > 1) { + if (argc > 1) { fileload(argv[1]); return EXIT_SUCCESS; } diff --git a/include/fast_float/ascii_number.h b/include/fast_float/ascii_number.h index e5d0e63..81c986a 100644 --- a/include/fast_float/ascii_number.h +++ b/include/fast_float/ascii_number.h @@ -292,9 +292,8 @@ parse_number_string(UC const *p, UC const *pend, // assume p < pend, so dereference without checks; answer.negative = (*p == UC('-')); // C++17 20.19.3.(7.1) explicitly forbids '+' sign here - if ((*p == UC('-')) || - (uint64_t(fmt & chars_format::allow_leading_plus) && - !basic_json_fmt && *p == UC('+'))) { + if ((*p == UC('-')) || (uint64_t(fmt & chars_format::allow_leading_plus) && + !basic_json_fmt && *p == UC('+'))) { ++p; if (p == pend) { return report_parse_error( diff --git a/include/fast_float/parse_number.h b/include/fast_float/parse_number.h index 9814254..e74c478 100644 --- a/include/fast_float/parse_number.h +++ b/include/fast_float/parse_number.h @@ -304,9 +304,10 @@ from_chars_float_advanced(UC const *first, UC const *last, T &value, answer.ptr = first; return answer; } - parsed_number_string_t pns = uint64_t(fmt & detail::basic_json_fmt) ? - parse_number_string(first, last, options) : - parse_number_string(first, last, options); + parsed_number_string_t pns = + uint64_t(fmt & detail::basic_json_fmt) + ? parse_number_string(first, last, options) + : parse_number_string(first, last, options); if (!pns.valid) { if (uint64_t(fmt & chars_format::no_infnan)) { answer.ec = std::errc::invalid_argument; From b29208f93d73863358bdb1ca8d7c29ff3ce28a25 Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Sun, 9 Mar 2025 17:10:55 -0400 Subject: [PATCH 3/4] adding FASTFLOAT_IF_CONSTEXPR17 --- include/fast_float/ascii_number.h | 6 +++--- include/fast_float/constexpr_feature_detect.h | 6 ++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/include/fast_float/ascii_number.h b/include/fast_float/ascii_number.h index 81c986a..dea1efb 100644 --- a/include/fast_float/ascii_number.h +++ b/include/fast_float/ascii_number.h @@ -299,7 +299,7 @@ parse_number_string(UC const *p, UC const *pend, return report_parse_error( p, parse_error::missing_integer_or_dot_after_sign); } - if (basic_json_fmt) { + FASTFLOAT_IF_CONSTEXPR17(basic_json_fmt) { if (!is_integer(*p)) { // a sign must be followed by an integer return report_parse_error(p, parse_error::missing_integer_after_sign); @@ -328,7 +328,7 @@ parse_number_string(UC const *p, UC const *pend, UC const *const end_of_integer_part = p; int64_t digit_count = int64_t(end_of_integer_part - start_digits); answer.integer = span(start_digits, size_t(digit_count)); - if (basic_json_fmt) { + FASTFLOAT_IF_CONSTEXPR17(basic_json_fmt) { // at least 1 digit in integer part, without leading zeros if (digit_count == 0) { return report_parse_error(p, parse_error::no_digits_in_integer_part); @@ -357,7 +357,7 @@ parse_number_string(UC const *p, UC const *pend, answer.fraction = span(before, size_t(p - before)); digit_count -= exponent; } - if (basic_json_fmt) { + FASTFLOAT_IF_CONSTEXPR17(basic_json_fmt) { // at least 1 digit in fractional part if (has_decimal_point && exponent == 0) { return report_parse_error(p, diff --git a/include/fast_float/constexpr_feature_detect.h b/include/fast_float/constexpr_feature_detect.h index 648b55d..6751afe 100644 --- a/include/fast_float/constexpr_feature_detect.h +++ b/include/fast_float/constexpr_feature_detect.h @@ -27,6 +27,12 @@ #define FASTFLOAT_HAS_IS_CONSTANT_EVALUATED 0 #endif +#if defined(__cpp_if_constexpr) && __cpp_if_constexpr >= 201606L +#define FASTFLOAT_IF_CONSTEXPR17(x) if constexpr (x) +#else +#define FASTFLOAT_IF_CONSTEXPR17(x) if (x) +#endif + // Testing for relevant C++20 constexpr library features #if FASTFLOAT_HAS_IS_CONSTANT_EVALUATED && FASTFLOAT_HAS_BIT_CAST && \ defined(__cpp_lib_constexpr_algorithms) && \ From c6732cd28bb856568749d3cd73305ed32191272e Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Mon, 10 Mar 2025 09:02:38 -0400 Subject: [PATCH 4/4] lint --- include/fast_float/ascii_number.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/include/fast_float/ascii_number.h b/include/fast_float/ascii_number.h index dea1efb..97f0681 100644 --- a/include/fast_float/ascii_number.h +++ b/include/fast_float/ascii_number.h @@ -304,7 +304,8 @@ parse_number_string(UC const *p, UC const *pend, return report_parse_error(p, parse_error::missing_integer_after_sign); } - } else { + } + else { if (!is_integer(*p) && (*p != decimal_point)) { // a sign must be followed by an integer or the dot @@ -363,8 +364,8 @@ parse_number_string(UC const *p, UC const *pend, return report_parse_error(p, parse_error::no_digits_in_fractional_part); } - } else if (digit_count == - 0) { // we must have encountered at least one integer! + } + else if (digit_count == 0) { // we must have encountered at least one integer! return report_parse_error(p, parse_error::no_digits_in_mantissa); } int64_t exp_number = 0; // explicit exponential part