Skip to content

Commit 41e455b

Browse files
committed
Charconv is used for serialization
1 parent 7d4bbaa commit 41e455b

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

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

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
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 <boost/json/detail/ryu/detail/common.hpp>
1516
#include <cstring>
1617

1718
namespace boost {
@@ -114,8 +115,36 @@ unsigned
114115
format_double(
115116
char* dest, double d, bool allow_infinity_and_nan) noexcept
116117
{
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;
119148
}
120149

121150
} // detail

0 commit comments

Comments
 (0)