Move the fraction only for a multibyte decimal point

For a one-byte decimal point, the std::copy() call had its destination at
the start of its source range, which std::copy() does not allow. The bytes
are now moved only when the decimal point is longer than the '.' it is
replaced with. Also name the right place of get_decimal_point().

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann committed 2026-11-02 09:32:50 +01:00
1 parent e96469f783
commit 98cfd55317
2 files changed
+16 -6

No files matched your search

@@ -2096,8 +2096,9 @@ class serializer
JSON_ASSERT(static_cast<std::size_t>(len) < number_buffer.size()); JSON_ASSERT(static_cast<std::size_t>(len) < number_buffer.size());
// look up the locale's decimal point now, matching what // look up the locale's decimal point now, matching what
// snprintf_float() just used (see lexer::get_decimal_point()); it is a // snprintf_float() just used (see get_decimal_point() in
// string, which can be longer than one byte (U+066B in ps_AF, #5993). // number_parse.hpp); it is a string, which can be longer than one
// byte (U+066B in ps_AF, #5993).
// A thousands separator needs no handling: %g never groups digits. // A thousands separator needs no handling: %g never groups digits.
const auto* loc = std::localeconv(); const auto* loc = std::localeconv();
JSON_ASSERT(loc != nullptr); JSON_ASSERT(loc != nullptr);
@@ -2114,13 +2115,17 @@ class serializer
if (dec_pos != last) if (dec_pos != last)
{ {
*dec_pos = '.'; *dec_pos = '.';
// move the fraction and exponent behind the '.' (a no-op for a one-byte decimal point) if (decimal_point_len > 1)
{
// move the fraction and exponent behind the '.' (std::copy
// allows the overlap, as the destination is before the source)
// NOLINTNEXTLINE(readability-qualified-auto,llvm-qualified-auto): std::copy returns an iterator, see https://github.com/nlohmann/json/issues/3081 // NOLINTNEXTLINE(readability-qualified-auto,llvm-qualified-auto): std::copy returns an iterator, see https://github.com/nlohmann/json/issues/3081
const auto end = std::copy(dec_pos + static_cast<std::ptrdiff_t>(decimal_point_len), last, dec_pos + 1); const auto end = std::copy(dec_pos + static_cast<std::ptrdiff_t>(decimal_point_len), last, dec_pos + 1);
*end = '\0'; *end = '\0';
len = end - first; len = end - first;
} }
} }
}
put_buffer(number_buffer, static_cast<std::size_t>(len)); put_buffer(number_buffer, static_cast<std::size_t>(len));
+8 -3
View File
@@ -37343,8 +37343,9 @@ class serializer
JSON_ASSERT(static_cast<std::size_t>(len) < number_buffer.size()); JSON_ASSERT(static_cast<std::size_t>(len) < number_buffer.size());
// look up the locale's decimal point now, matching what // look up the locale's decimal point now, matching what
// snprintf_float() just used (see lexer::get_decimal_point()); it is a // snprintf_float() just used (see get_decimal_point() in
// string, which can be longer than one byte (U+066B in ps_AF, #5993). // number_parse.hpp); it is a string, which can be longer than one
// byte (U+066B in ps_AF, #5993).
// A thousands separator needs no handling: %g never groups digits. // A thousands separator needs no handling: %g never groups digits.
const auto* loc = std::localeconv(); const auto* loc = std::localeconv();
JSON_ASSERT(loc != nullptr); JSON_ASSERT(loc != nullptr);
@@ -37361,13 +37362,17 @@ class serializer
if (dec_pos != last) if (dec_pos != last)
{ {
*dec_pos = '.'; *dec_pos = '.';
// move the fraction and exponent behind the '.' (a no-op for a one-byte decimal point) if (decimal_point_len > 1)
{
// move the fraction and exponent behind the '.' (std::copy
// allows the overlap, as the destination is before the source)
// NOLINTNEXTLINE(readability-qualified-auto,llvm-qualified-auto): std::copy returns an iterator, see https://github.com/nlohmann/json/issues/3081 // NOLINTNEXTLINE(readability-qualified-auto,llvm-qualified-auto): std::copy returns an iterator, see https://github.com/nlohmann/json/issues/3081
const auto end = std::copy(dec_pos + static_cast<std::ptrdiff_t>(decimal_point_len), last, dec_pos + 1); const auto end = std::copy(dec_pos + static_cast<std::ptrdiff_t>(decimal_point_len), last, dec_pos + 1);
*end = '\0'; *end = '\0';
len = end - first; len = end - first;
} }
} }
}
put_buffer(number_buffer, static_cast<std::size_t>(len)); put_buffer(number_buffer, static_cast<std::size_t>(len));