diff --git a/include/fmt/format.h b/include/fmt/format.h index c14d3f18..1fc57de1 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -1686,9 +1686,9 @@ template struct int_writer { // Index of a decimal digit with the least significant digit having index 0. int digit_index = 0; group = groups.cbegin(); - auto p = buffer.data() + size; - for (int i = num_digits - 1; i >= 0; --i) { - *--p = static_cast(digits[i]); + auto p = buffer.data() + size - 1; + for (int i = num_digits - 1; i > 0; --i) { + *p-- = static_cast(digits[i]); if (*group <= 0 || ++digit_index % *group != 0 || *group == max_value()) continue; @@ -1696,11 +1696,12 @@ template struct int_writer { digit_index = 0; ++group; } - p -= s.size(); std::uninitialized_copy(s.data(), s.data() + s.size(), make_checked(p, s.size())); + p -= s.size(); } - if (prefix_size != 0) p[-1] = static_cast('-'); + *p-- = static_cast(*digits); + if (prefix_size != 0) *p = static_cast('-'); auto data = buffer.data(); out = write_padded( out, specs, usize, usize, diff --git a/test/locale-test.cc b/test/locale-test.cc index a9ddb361..c7c30f2e 100644 --- a/test/locale-test.cc +++ b/test/locale-test.cc @@ -52,6 +52,7 @@ TEST(LocaleTest, Format) { EXPECT_EQ("1234567", fmt::format(std::locale(), "{:L}", 1234567)); EXPECT_EQ("1~234~567", fmt::format(loc, "{:L}", 1234567)); EXPECT_EQ("-1~234~567", fmt::format(loc, "{:L}", -1234567)); + EXPECT_EQ("-256", fmt::format(loc, "{:L}", -256)); fmt::format_arg_store as{1234567}; EXPECT_EQ("1~234~567", fmt::vformat(loc, "{:L}", fmt::format_args(as))); std::string s;