|
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 <cmath> |
15 | 16 | #include <cstring>
|
| 17 | +#include <limits> |
16 | 18 |
|
17 | 19 | namespace boost {
|
18 | 20 | namespace json {
|
@@ -114,8 +116,60 @@ unsigned
|
114 | 116 | format_double(
|
115 | 117 | char* dest, double d, bool allow_infinity_and_nan) noexcept
|
116 | 118 | {
|
117 |
| - return static_cast<int>( |
118 |
| - ryu::d2s_buffered_n(d, dest, allow_infinity_and_nan)); |
| 119 | + charconv::to_chars_result result; |
| 120 | + |
| 121 | + using Limits = std::numeric_limits<double>; |
| 122 | + |
| 123 | + if(BOOST_JSON_UNLIKELY( std::isnan(d) )) |
| 124 | + { |
| 125 | + if( allow_infinity_and_nan ) |
| 126 | + { |
| 127 | + std::memcpy(dest, "NaN", 3); |
| 128 | + result.ptr = dest + 3; |
| 129 | + } |
| 130 | + else |
| 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() ); |
| 170 | + } |
| 171 | + |
| 172 | + return result.ptr - dest; |
119 | 173 | }
|
120 | 174 |
|
121 | 175 | } // detail
|
|
0 commit comments