Skip to content

Commit d7c04b0

Browse files
committed
Charconv is used to serialize doubles
1 parent 01e6836 commit d7c04b0

File tree

16 files changed

+57
-2340
lines changed

16 files changed

+57
-2340
lines changed

include/boost/json/detail/format.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ format_int64(
3333
char* dest, int64_t i) noexcept;
3434

3535
BOOST_JSON_DECL
36-
unsigned
36+
std::size_t
3737
format_double(
3838
char* dest, double d, bool allow_infinity_and_nan = false) noexcept;
3939

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

Lines changed: 51 additions & 4 deletions
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 {
@@ -110,12 +112,57 @@ format_int64(
110112
return 1 + format_uint64(dest, ui);
111113
}
112114

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

121168
} // detail

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

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

0 commit comments

Comments
 (0)