|
11 | 11 | #ifndef BOOST_JSON_DETAIL_IMPL_FORMAT_IPP
|
12 | 12 | #define BOOST_JSON_DETAIL_IMPL_FORMAT_IPP
|
13 | 13 |
|
14 |
| -#include <boost/json/detail/ryu/ryu.hpp> |
| 14 | +#include <boost/charconv/to_chars.hpp> |
| 15 | +#include <boost/json/detail/ryu/detail/common.hpp> |
15 | 16 | #include <cstring>
|
16 | 17 |
|
17 | 18 | namespace boost {
|
@@ -114,8 +115,36 @@ unsigned
|
114 | 115 | format_double(
|
115 | 116 | char* dest, double d, bool allow_infinity_and_nan) noexcept
|
116 | 117 | {
|
117 |
| - return static_cast<int>( |
118 |
| - ryu::d2s_buffered_n(d, dest, allow_infinity_and_nan)); |
| 118 | + namespace ryud = ryu::detail; |
| 119 | + // Copyright 2018 Ulf Adams |
| 120 | + // |
| 121 | + // Decode bits into sign, mantissa, and exponent. |
| 122 | + std::uint64_t const bits = ryud::double_to_bits(d); |
| 123 | + const bool ieeeSign = ((bits >> (ryud::DOUBLE_MANTISSA_BITS + ryud::DOUBLE_EXPONENT_BITS)) & 1) != 0; |
| 124 | + const std::uint64_t ieeeMantissa = bits & ((1ull << ryud::DOUBLE_MANTISSA_BITS) - 1); |
| 125 | + const std::uint32_t ieeeExponent = (std::uint32_t)((bits >> ryud::DOUBLE_MANTISSA_BITS) & ((1u << ryud::DOUBLE_EXPONENT_BITS) - 1)); |
| 126 | + // Case distinction; exit early for the easy cases. |
| 127 | + if(BOOST_JSON_UNLIKELY( |
| 128 | + ieeeExponent == ((1u << ryud::DOUBLE_EXPONENT_BITS) - 1u) || |
| 129 | + (ieeeExponent == 0 |
| 130 | + && ieeeMantissa == 0) )) |
| 131 | + { |
| 132 | + // We changed how special numbers are output by default |
| 133 | + if (allow_infinity_and_nan) |
| 134 | + return ryud::copy_special_str( |
| 135 | + dest, ieeeSign, ieeeExponent != 0, ieeeMantissa != 0); |
| 136 | + else |
| 137 | + return ryud::copy_special_str_conforming( |
| 138 | + dest, ieeeSign, ieeeExponent != 0, ieeeMantissa != 0); |
| 139 | + |
| 140 | + } |
| 141 | + |
| 142 | + auto const result = charconv::to_chars( |
| 143 | + dest, |
| 144 | + dest + detail::max_number_chars, |
| 145 | + d, |
| 146 | + charconv::chars_format::scientific); |
| 147 | + return result.ptr - dest; |
119 | 148 | }
|
120 | 149 |
|
121 | 150 | } // detail
|
|
0 commit comments