Skip to content

Commit

Permalink
Charconv is used to serialize doubles
Browse files Browse the repository at this point in the history
  • Loading branch information
grisumbras committed Mar 19, 2024
1 parent 65936e7 commit d06376b
Show file tree
Hide file tree
Showing 15 changed files with 59 additions and 2,335 deletions.
60 changes: 57 additions & 3 deletions include/boost/json/detail/impl/format.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
#ifndef BOOST_JSON_DETAIL_IMPL_FORMAT_IPP
#define BOOST_JSON_DETAIL_IMPL_FORMAT_IPP

#include <boost/json/detail/ryu/ryu.hpp>
#include <boost/charconv/to_chars.hpp>
#include <cmath>
#include <cstring>
#include <limits>

namespace boost {
namespace json {
Expand Down Expand Up @@ -114,8 +116,60 @@ unsigned
format_double(
char* dest, double d, bool allow_infinity_and_nan) noexcept
{
return static_cast<int>(
ryu::d2s_buffered_n(d, dest, allow_infinity_and_nan));
charconv::to_chars_result result;

using Limits = std::numeric_limits<double>;

if(BOOST_JSON_UNLIKELY( std::isnan(d) ))
{
if( allow_infinity_and_nan )
{
std::memcpy(dest, "NaN", 3);
result.ptr = dest + 3;
}
else
{
std::memcpy(dest, "null", 4);
result.ptr = dest + 4;
}
}
else if(BOOST_JSON_UNLIKELY( d == Limits::infinity() ))
{
if( allow_infinity_and_nan )
{
std::memcpy(dest, "Infinity", 8);
result.ptr = dest + 8;
}
else
{
std::memcpy(dest, "1e99999", 7);
result.ptr = dest + 7;
}
}
else if(BOOST_JSON_UNLIKELY( d == -Limits::infinity() ))
{
if( allow_infinity_and_nan )
{
std::memcpy(dest, "-Infinity", 9);
result.ptr = dest + 9;
}
else
{
std::memcpy(dest, "-1e99999", 8);
result.ptr = dest + 8;
}
}
else
{
result = charconv::to_chars(
dest,
dest + detail::max_number_chars,
d,
charconv::chars_format::scientific);
BOOST_ASSERT( result.ec == std::errc() );
}

return result.ptr - dest;
}

} // detail
Expand Down
144 changes: 0 additions & 144 deletions include/boost/json/detail/ryu/detail/common.hpp

This file was deleted.

Loading

0 comments on commit d06376b

Please sign in to comment.