diff --git a/include/boost/charconv/to_chars.hpp b/include/boost/charconv/to_chars.hpp index e8ad328..957dea7 100644 --- a/include/boost/charconv/to_chars.hpp +++ b/include/boost/charconv/to_chars.hpp @@ -12,8 +12,9 @@ #include #include #include -#include +#include #include +#include #include #include #include @@ -455,7 +456,7 @@ BOOST_CHARCONV_CONSTEXPR to_chars_result to_chars128(char* first, char* last, In // --------------------------------------------------------------------------------------------------------------------- // Floating Point Detail // --------------------------------------------------------------------------------------------------------------------- - +/* template extern char* to_chars(typename FloatTraits::carrier_uint significand, int exponent, char* buffer) noexcept; @@ -463,7 +464,7 @@ extern char* to_chars(typename FloatTraits::carrier_uint significand, int expone template char* to_chars_n_impl(float_bits br, char* buffer) noexcept { - const auto exponent_bits = br.extract_exponent_bits(); + const static auto exponent_bits = br.extract_exponent_bits(); const auto s = br.remove_exponent_bits(exponent_bits); if (br.is_finite(exponent_bits)) @@ -537,7 +538,7 @@ void print_1_digit(std::uint32_t n, char* buffer) noexcept; void print_2_digits(std::uint32_t n, char* buffer) noexcept; void print_9_digits(std::uint32_t s32, int& exponent, char* buffer) noexcept; - +*/ } // Namespace detail // integer overloads @@ -599,9 +600,10 @@ BOOST_CHARCONV_CONSTEXPR to_chars_result to_chars(char* first, char* last, boost #endif // floating point overloads -BOOST_CHARCONV_DECL to_chars_result to_chars( char* first, char* last, float value ) noexcept; -BOOST_CHARCONV_DECL to_chars_result to_chars( char* first, char* last, double value ) noexcept; -BOOST_CHARCONV_DECL to_chars_result to_chars( char* first, char* last, long double value ) noexcept; +BOOST_CHARCONV_DECL to_chars_result to_chars(char* first, char* last, float value ) noexcept; +BOOST_CHARCONV_DECL to_chars_result to_chars(char* first, char* last, double value, + chars_format fmt = chars_format::general, int precision = -1 ) noexcept; +BOOST_CHARCONV_DECL to_chars_result to_chars(char* first, char* last, long double value ) noexcept; } // namespace charconv } // namespace boost diff --git a/src/to_chars.cpp b/src/to_chars.cpp index 9e1889a..de7dec4 100644 --- a/src/to_chars.cpp +++ b/src/to_chars.cpp @@ -286,10 +286,26 @@ boost::charconv::to_chars_result boost::charconv::to_chars( char* first, char* l return { first + std::strlen(first), 0 }; } -boost::charconv::to_chars_result boost::charconv::to_chars( char* first, char* last, double value ) noexcept +boost::charconv::to_chars_result boost::charconv::to_chars(char* first, char* last, double value, boost::charconv::chars_format fmt, int precision) noexcept { - std::snprintf( first, last - first, "%.*g", std::numeric_limits::max_digits10, value ); - return { first + std::strlen(first), 0 }; + (void)fmt; + + if (precision == -1) + { + precision = std::numeric_limits::max_digits10; + } + if (precision > static_cast(last - first)) + { + return { first, EOVERFLOW }; + } + auto* ptr = jkj::floff::floff(value, precision, first); + while (*ptr == '0') + { + --ptr; + } + return { ptr, 0 }; + //std::snprintf( first, last - first, "%.*g", std::numeric_limits::max_digits10, value ); + //return { first + std::strlen(first), 0 }; } boost::charconv::to_chars_result boost::charconv::to_chars( char* first, char* last, long double value ) noexcept