From 8f94758c7862a77964864c2386c2ce425d1f4aab Mon Sep 17 00:00:00 2001 From: Maya Warrier Date: Mon, 27 Mar 2023 22:50:21 -0400 Subject: [PATCH] Expose parsed string (before computation) so it can be reused --- include/fast_float/ascii_number.h | 2 ++ include/fast_float/fast_float.h | 12 +++++++++- include/fast_float/parse_number.h | 38 +++++++++++++++++++------------ 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/include/fast_float/ascii_number.h b/include/fast_float/ascii_number.h index 72b8098..9ad754e 100644 --- a/include/fast_float/ascii_number.h +++ b/include/fast_float/ascii_number.h @@ -96,6 +96,7 @@ typedef span byte_span; struct parsed_number_string { int64_t exponent{0}; uint64_t mantissa{0}; + uint64_t integer_value{-1}; const char *lastmatch{nullptr}; bool negative{false}; bool valid{false}; @@ -143,6 +144,7 @@ parsed_number_string parse_number_string(const char *p, const char *pend, parse_ const char *const end_of_integer_part = p; int64_t digit_count = int64_t(end_of_integer_part - start_digits); answer.integer = byte_span(start_digits, size_t(digit_count)); + answer.integer_value = i; int64_t exponent = 0; if ((p != pend) && (*p == decimal_point)) { ++p; diff --git a/include/fast_float/fast_float.h b/include/fast_float/fast_float.h index 65704da..d84405d 100644 --- a/include/fast_float/fast_float.h +++ b/include/fast_float/fast_float.h @@ -62,6 +62,16 @@ FASTFLOAT_CONSTEXPR20 from_chars_result from_chars_advanced(const char *first, const char *last, T &value, parse_options options) noexcept; -} // namespace fast_float +} + +#include "ascii_number.h" // parsed_number_string + +namespace fast_float { +template +FASTFLOAT_CONSTEXPR20 +from_chars_result from_chars_preparsed(parsed_number_string parsed, T& value) noexcept; +} + +// namespace fast_float #include "parse_number.h" #endif // FASTFLOAT_FAST_FLOAT_H diff --git a/include/fast_float/parse_number.h b/include/fast_float/parse_number.h index d16a25d..c880f1e 100644 --- a/include/fast_float/parse_number.h +++ b/include/fast_float/parse_number.h @@ -141,24 +141,12 @@ from_chars_result from_chars(const char *first, const char *last, template FASTFLOAT_CONSTEXPR20 -from_chars_result from_chars_advanced(const char *first, const char *last, - T &value, parse_options options) noexcept { - +from_chars_result from_chars_preparsed(parsed_number_string pns, T& value) noexcept +{ static_assert (std::is_same::value || std::is_same::value, "only float and double are supported"); - + from_chars_result answer; -#if FASTFLOAT_SKIP_WHITE_SPACE // disabled by default - while ((first != last) && fast_float::is_space(uint8_t(*first))) { - first++; - } -#endif - if (first == last) { - answer.ec = std::errc::invalid_argument; - answer.ptr = first; - return answer; - } - parsed_number_string pns = parse_number_string(first, last, options); if (!pns.valid) { return detail::parse_infnan(first, last, value); } @@ -217,6 +205,26 @@ from_chars_result from_chars_advanced(const char *first, const char *last, return answer; } +template +FASTFLOAT_CONSTEXPR20 +from_chars_result from_chars_advanced(const char *first, const char *last, + T &value, parse_options options) noexcept { + + from_chars_result answer; +#if FASTFLOAT_SKIP_WHITE_SPACE // disabled by default + while ((first != last) && fast_float::is_space(uint8_t(*first))) { + first++; + } +#endif + if (first == last) { + answer.ec = std::errc::invalid_argument; + answer.ptr = first; + return answer; + } + answer = from_chars_preparsed(parse_number_string(first, last, options), value); + return answer; +} + } // namespace fast_float #endif