From ddd7caf38eeb281641f0881634c576b50c637c38 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sat, 13 Oct 2018 06:52:33 -0700 Subject: [PATCH] Fix locale-dependent formatting (#905) --- include/fmt/format.h | 14 ++++++++++---- test/format-test.cc | 2 ++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index f232be54..96774875 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -988,6 +988,8 @@ struct no_thousands_sep { template void operator()(Char *) {} + + enum { size = 0 }; }; // A functor that adds a thousands separator. @@ -1012,6 +1014,8 @@ class add_thousands_sep { std::uninitialized_copy(sep_.data(), sep_.data() + sep_.size(), internal::make_checked(buffer, sep_.size())); } + + enum { size = 1 }; }; template @@ -1052,10 +1056,12 @@ template ::digits10 + 2]; - format_decimal(buffer, value, num_digits, sep); - return internal::copy_str(buffer, buffer + num_digits, out); + // Buffer should be large enough to hold all digits (<= digits10 + 1). + enum { max_size = std::numeric_limits::digits10 + 1 }; + FMT_ASSERT(ThousandsSep::size <= 1, "invalid separator"); + char_type buffer[max_size + max_size / 3]; + auto end = format_decimal(buffer, value, num_digits, sep); + return internal::copy_str(buffer, end, out); } template diff --git a/test/format-test.cc b/test/format-test.cc index dd3ed739..484fa21d 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1378,6 +1378,8 @@ TEST(FormatterTest, FormatIntLocale) { EXPECT_EQ("123", format("{:n}", 123)); EXPECT_EQ("1,234", format("{:n}", 1234)); EXPECT_EQ("1,234,567", format("{:n}", 1234567)); + EXPECT_EQ("4,294,967,295", + format("{:n}", std::numeric_limits::max())); } struct ConvertibleToLongLong {