|
12 | 12 | #define BOOST_JSON_DETAIL_IMPL_FORMAT_IPP
|
13 | 13 |
|
14 | 14 | #include <boost/charconv/to_chars.hpp>
|
15 |
| -#include <boost/json/detail/ryu/detail/common.hpp> |
| 15 | +#include <cmath> |
16 | 16 | #include <cstring>
|
| 17 | +#include <limits> |
17 | 18 |
|
18 | 19 | namespace boost {
|
19 | 20 | namespace json {
|
@@ -115,35 +116,59 @@ unsigned
|
115 | 116 | format_double(
|
116 | 117 | char* dest, double d, bool allow_infinity_and_nan) noexcept
|
117 | 118 | {
|
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) )) |
| 119 | + charconv::to_chars_result result; |
| 120 | + |
| 121 | + using Limits = std::numeric_limits<double>; |
| 122 | + |
| 123 | + if(BOOST_JSON_UNLIKELY( std::isnan(d) )) |
131 | 124 | {
|
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); |
| 125 | + if( allow_infinity_and_nan ) |
| 126 | + { |
| 127 | + std::memcpy(dest, "NaN", 3); |
| 128 | + result.ptr = dest + 3; |
| 129 | + } |
136 | 130 | else
|
137 |
| - return ryud::copy_special_str_conforming( |
138 |
| - dest, ieeeSign, ieeeExponent != 0, ieeeMantissa != 0); |
139 |
| - |
| 131 | + { |
| 132 | + std::memcpy(dest, "null", 4); |
| 133 | + result.ptr = dest + 4; |
| 134 | + } |
| 135 | + } |
| 136 | + else if(BOOST_JSON_UNLIKELY( d == Limits::infinity() )) |
| 137 | + { |
| 138 | + if( allow_infinity_and_nan ) |
| 139 | + { |
| 140 | + std::memcpy(dest, "Infinity", 8); |
| 141 | + result.ptr = dest + 8; |
| 142 | + } |
| 143 | + else |
| 144 | + { |
| 145 | + std::memcpy(dest, "1e99999", 7); |
| 146 | + result.ptr = dest + 7; |
| 147 | + } |
| 148 | + } |
| 149 | + else if(BOOST_JSON_UNLIKELY( d == -Limits::infinity() )) |
| 150 | + { |
| 151 | + if( allow_infinity_and_nan ) |
| 152 | + { |
| 153 | + std::memcpy(dest, "-Infinity", 9); |
| 154 | + result.ptr = dest + 9; |
| 155 | + } |
| 156 | + else |
| 157 | + { |
| 158 | + std::memcpy(dest, "-1e99999", 8); |
| 159 | + result.ptr = dest + 8; |
| 160 | + } |
| 161 | + } |
| 162 | + else |
| 163 | + { |
| 164 | + result = charconv::to_chars( |
| 165 | + dest, |
| 166 | + dest + detail::max_number_chars, |
| 167 | + d, |
| 168 | + charconv::chars_format::scientific); |
| 169 | + BOOST_ASSERT( result.ec == std::errc() ); |
140 | 170 | }
|
141 | 171 |
|
142 |
| - auto const result = charconv::to_chars( |
143 |
| - dest, |
144 |
| - dest + detail::max_number_chars, |
145 |
| - d, |
146 |
| - charconv::chars_format::scientific); |
147 | 172 | return result.ptr - dest;
|
148 | 173 | }
|
149 | 174 |
|
|
0 commit comments