Skip to content

Commit a197a7b

Browse files
committed
Charconv is used to serialize doubles
1 parent 8da0706 commit a197a7b

File tree

16 files changed

+61
-2346
lines changed

16 files changed

+61
-2346
lines changed

include/boost/json/detail/format.hpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,13 @@
1010
#ifndef BOOST_JSON_DETAIL_FORMAT_HPP
1111
#define BOOST_JSON_DETAIL_FORMAT_HPP
1212

13+
#include <boost/charconv/limits.hpp>
14+
1315
namespace boost {
1416
namespace json {
1517
namespace detail {
1618

17-
int constexpr max_number_chars =
18-
1 + // '-'
19-
19 + // unsigned 64-bit mantissa
20-
1 + // 'e'
21-
1 + // '-'
22-
5; // unsigned 16-bit exponent
19+
std::size_t constexpr max_number_chars = charconv::limits<double>::max_chars;
2320

2421
BOOST_JSON_DECL
2522
unsigned
@@ -33,7 +30,7 @@ format_int64(
3330
char* dest, int64_t i) noexcept;
3431

3532
BOOST_JSON_DECL
36-
unsigned
33+
std::size_t
3734
format_double(
3835
char* dest, double d, bool allow_infinity_and_nan = false) noexcept;
3936

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

Lines changed: 52 additions & 4 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 <cmath>
1516
#include <cstring>
1617

1718
namespace boost {
@@ -110,12 +111,59 @@ format_int64(
110111
return 1 + format_uint64(dest, ui);
111112
}
112113

113-
unsigned
114+
std::size_t
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+
unsigned variant = allow_infinity_and_nan ? 8 : 0;
119+
variant |= std::isnan(d) ? 1 : 0;
120+
if( std::isinf(d) )
121+
{
122+
variant |= 2;
123+
variant |= std::signbit(d) ? 4 : 0;
124+
}
125+
126+
std::size_t size;
127+
switch( variant )
128+
{
129+
default:
130+
{
131+
charconv::to_chars_result result = charconv::to_chars(
132+
dest,
133+
dest + detail::max_number_chars,
134+
d,
135+
charconv::chars_format::scientific);
136+
BOOST_ASSERT( result.ec == std::errc() );
137+
size = result.ptr - dest;
138+
break;
139+
}
140+
case (8 + 1):
141+
std::memcpy(dest, "NaN", 3);
142+
size = 3;
143+
break;
144+
case (0 + 1):
145+
std::memcpy(dest, "null", 4);
146+
size = 4;
147+
break;
148+
case (8 + 0 + 2):
149+
std::memcpy(dest, "Infinity", 8);
150+
size = 8;
151+
break;
152+
case (8 + 4 + 2):
153+
std::memcpy(dest, "-Infinity", 9);
154+
size = 9;
155+
break;
156+
case (0 + 0 + 2):
157+
std::memcpy(dest, "1e99999", 7);
158+
size = 7;
159+
break;
160+
case (0 + 4 + 2):
161+
std::memcpy(dest, "-1e99999", 8);
162+
size = 8;
163+
break;
164+
}
165+
166+
return size;
119167
}
120168

121169
} // detail

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

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

0 commit comments

Comments
 (0)