Skip to content

Commit 1f2a720

Browse files
committed
Charconv is used to serialize doubles
1 parent 2df2366 commit 1f2a720

File tree

20 files changed

+67
-2345
lines changed

20 files changed

+67
-2345
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ function(boost_json_setup_properties target)
8888
Boost::align
8989
Boost::assert
9090
Boost::config
91+
Boost::charconv
9192
Boost::container
9293
Boost::container_hash
9394
Boost::core

build.jam

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ using boost-pretty-printers ;
1313
constant boost_dependencies :
1414
/boost/align//boost_align
1515
/boost/assert//boost_assert
16+
/boost/charconv//boost_charconv
1617
/boost/config//boost_config
1718
/boost/container//boost_container
1819
/boost/container_hash//boost_container_hash

build/Jamfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ project
5050
<toolset>msvc:<define>_SCL_SECURE_NO_WARNINGS
5151
: usage-requirements
5252
<library>/boost/container//boost_container/<warnings-as-errors>off
53+
<library>/boost/charconv//boost_charconv/<warnings-as-errors>off
5354
<define>BOOST_JSON_NO_LIB=1
5455
: source-location ../src
5556
;

include/boost/json/detail/format.hpp

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

13+
#include <boost/charconv/limits.hpp>
14+
#include <boost/json/detail/config.hpp>
15+
1316
namespace boost {
1417
namespace json {
1518
namespace detail {
1619

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
20+
constexpr std::size_t max_number_chars =
21+
boost::charconv::limits<double>::max_chars;
2322

2423
BOOST_JSON_DECL
2524
unsigned
@@ -33,7 +32,7 @@ format_int64(
3332
char* dest, int64_t i) noexcept;
3433

3534
BOOST_JSON_DECL
36-
unsigned
35+
std::size_t
3736
format_double(
3837
char* dest, double d, bool allow_infinity_and_nan = false) noexcept;
3938

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+
boost::charconv::to_chars_result result = boost::charconv::to_chars(
132+
dest,
133+
dest + detail::max_number_chars,
134+
d,
135+
boost::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)