Skip to content

Commit 4d9d950

Browse files
committed
In the fixed case make sure we arent short digits
1 parent 191dc70 commit 4d9d950

File tree

1 file changed

+12
-1
lines changed
  • include/boost/charconv/detail/dragonbox

1 file changed

+12
-1
lines changed

include/boost/charconv/detail/dragonbox/floff.hpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1333,6 +1333,7 @@ BOOST_CHARCONV_SAFEBUFFERS to_chars_result floff(const double x, int precision,
13331333

13341334
auto buffer_size = static_cast<std::size_t>(last - first);
13351335
auto buffer = first;
1336+
bool trailing_zeros_removed = false;
13361337

13371338
BOOST_CHARCONV_ASSERT(precision >= 0);
13381339
using namespace detail;
@@ -3849,6 +3850,7 @@ BOOST_CHARCONV_SAFEBUFFERS to_chars_result floff(const double x, int precision,
38493850
}
38503851

38513852
// Remove trailing zeros.
3853+
trailing_zeros_removed = true;
38523854
while (true)
38533855
{
38543856
auto prev = buffer - 1;
@@ -3903,7 +3905,16 @@ BOOST_CHARCONV_SAFEBUFFERS to_chars_result floff(const double x, int precision,
39033905
print_2_digits(static_cast<std::uint32_t>(decimal_exponent_normalized), buffer);
39043906
buffer += 2;
39053907
}
3906-
}
3908+
}
3909+
else if (!trailing_zeros_removed && buffer - (decimal_dot_pos + 1) < precision)
3910+
{
3911+
// If we have fixed precision, and we don't have enough digits after the decimal yet
3912+
// insert a sufficient amount of zeros
3913+
const auto remaining_zeros = precision - (buffer - (decimal_dot_pos + 1));
3914+
BOOST_CHARCONV_ASSERT(remaining_zeros > 0);
3915+
std::memset(buffer, '0', static_cast<std::size_t>(remaining_zeros));
3916+
buffer += remaining_zeros;
3917+
}
39073918

39083919
return {buffer, std::errc()};
39093920

0 commit comments

Comments
 (0)