From 7c96e3a8be4b5222e83be46061f56990d9004700 Mon Sep 17 00:00:00 2001 From: IRainman Date: Wed, 9 Apr 2025 15:22:10 +0300 Subject: [PATCH] reduce size of from_chars_result_t to 4 bytes. Cleanup for usage FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN. --- include/fast_float/ascii_number.h | 6 +++--- include/fast_float/float_common.h | 26 +++++++++++++++----------- include/fast_float/parse_number.h | 14 +++++--------- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/include/fast_float/ascii_number.h b/include/fast_float/ascii_number.h index fcbbfe9..94d47f0 100644 --- a/include/fast_float/ascii_number.h +++ b/include/fast_float/ascii_number.h @@ -582,7 +582,9 @@ parse_int_string(UC const *p, UC const *pend, T &value, } } -#ifndef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN +#ifdef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN + value = T(i); +#else if (negative) { #ifdef FASTFLOAT_VISUAL_STUDIO #pragma warning(push) @@ -600,9 +602,7 @@ parse_int_string(UC const *p, UC const *pend, T &value, #pragma warning(pop) #endif } else { -#endif value = T(i); -#ifndef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN } #endif diff --git a/include/fast_float/float_common.h b/include/fast_float/float_common.h index 1b4362d..667f819 100644 --- a/include/fast_float/float_common.h +++ b/include/fast_float/float_common.h @@ -33,29 +33,29 @@ namespace fast_float { -enum class chars_format : uint64_t; +enum class chars_format : uint8_t; #ifndef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN namespace detail { -constexpr chars_format basic_json_fmt = chars_format(1 << 5); -constexpr chars_format basic_fortran_fmt = chars_format(1 << 6); +constexpr chars_format basic_json_fmt = chars_format(1 << 4); +constexpr chars_format basic_fortran_fmt = chars_format(1 << 5); } // namespace detail #endif -enum class chars_format : uint64_t { +enum class chars_format : uint8_t { scientific = 1 << 0, - fixed = 1 << 2, + fixed = 1 << 1, general = fixed | scientific, - hex = 1 << 3, + hex = 1 << 2, #ifndef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN - no_infnan = 1 << 4, + no_infnan = 1 << 3, // RFC 8259: https://datatracker.ietf.org/doc/html/rfc8259#section-6 json = uint64_t(detail::basic_json_fmt) | general | no_infnan, // Extension of RFC 8259 where, e.g., "inf" and "nan" are allowed. json_or_infnan = uint64_t(detail::basic_json_fmt) | general, fortran = uint64_t(detail::basic_fortran_fmt) | general, - allow_leading_plus = 1 << 7, - skip_white_space = 1 << 8, + allow_leading_plus = 1 << 6, + skip_white_space = 1 << 7, #endif }; @@ -70,14 +70,18 @@ template struct parse_options_t { FASTFLOAT_CONSTEXPR20 explicit parse_options_t( chars_format fmt = chars_format::general, UC dot = UC('.'), int const b = 10) noexcept - : format(fmt), decimal_point(dot), base(b) {} + : format(fmt), decimal_point(dot), base(uint8_t(b)) { +#ifdef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN + assert(b >= 2 && b <= 36); +#endif + } /** Which number formats are accepted */ chars_format format; /** The character used as decimal point */ UC decimal_point; /** The base used for integers */ - uint32_t base; /* only allowed from 2 to 36 */ + uint8_t base; /* only allowed from 2 to 36 */ FASTFLOAT_ASSUME(base >= 2 && base <= 36); }; diff --git a/include/fast_float/parse_number.h b/include/fast_float/parse_number.h index 677a146..46b71ec 100644 --- a/include/fast_float/parse_number.h +++ b/include/fast_float/parse_number.h @@ -378,20 +378,16 @@ from_chars_int_advanced(UC const *first, UC const *last, T &value, first++; } } -#else - // We are in parser code with external loop that checks bounds. - FASTFLOAT_ASSUME(first < last); -#endif - if ( -#ifndef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN - first == last || -#endif - options.base < 2 || options.base > 36) { + if (first == last || options.base < 2 || options.base > 36) { from_chars_result_t answer; answer.ec = std::errc::invalid_argument; answer.ptr = first; return answer; } +#else + // We are in parser code with external loop that checks bounds. + FASTFLOAT_ASSUME(first < last); +#endif return parse_int_string(first, last, value, options); }