Skip to content

Commit d06376b

Browse files
committed
Charconv is used to serialize doubles
1 parent 65936e7 commit d06376b

15 files changed

+59
-2335
lines changed

include/boost/json/detail/impl/format.ipp

+57-3
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
#ifndef BOOST_JSON_DETAIL_IMPL_FORMAT_IPP
1212
#define BOOST_JSON_DETAIL_IMPL_FORMAT_IPP
1313

14-
#include <boost/json/detail/ryu/ryu.hpp>
14+
#include <boost/charconv/to_chars.hpp>
15+
#include <cmath>
1516
#include <cstring>
17+
#include <limits>
1618

1719
namespace boost {
1820
namespace json {
@@ -114,8 +116,60 @@ unsigned
114116
format_double(
115117
char* dest, double d, bool allow_infinity_and_nan) noexcept
116118
{
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;
119173
}
120174

121175
} // detail

include/boost/json/detail/ryu/detail/common.hpp

-144
This file was deleted.

0 commit comments

Comments
 (0)