Skip to content

Commit 7493c7f

Browse files
committed
Charconv is used to serialize doubles
1 parent 41e455b commit 7493c7f

15 files changed

+53
-2358
lines changed

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

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
#define BOOST_JSON_DETAIL_IMPL_FORMAT_IPP
1313

1414
#include <boost/charconv/to_chars.hpp>
15-
#include <boost/json/detail/ryu/detail/common.hpp>
15+
#include <cmath>
1616
#include <cstring>
17+
#include <limits>
1718

1819
namespace boost {
1920
namespace json {
@@ -115,35 +116,59 @@ unsigned
115116
format_double(
116117
char* dest, double d, bool allow_infinity_and_nan) noexcept
117118
{
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) ))
131124
{
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+
}
136130
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() );
140170
}
141171

142-
auto const result = charconv::to_chars(
143-
dest,
144-
dest + detail::max_number_chars,
145-
d,
146-
charconv::chars_format::scientific);
147172
return result.ptr - dest;
148173
}
149174

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

Lines changed: 0 additions & 144 deletions
This file was deleted.

0 commit comments

Comments
 (0)