From d702a68df2c402359c4f03f4e8bda4cfd5fc9be7 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sun, 14 Jun 2020 07:16:50 -0700 Subject: [PATCH] Fix formatting of bool with FMT_COMPILE and add more tests --- include/fmt/format.h | 3 ++- test/compile-test.cc | 9 ++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index 8368b5d2..84c56ddd 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -3380,7 +3380,8 @@ inline std::string to_string(const T& value) { } template ::value)> inline std::string to_string(T value) { - char buffer[detail::digits10() + 2]; + // Buffer should be large enough to store the number or "false" (for bool). + char buffer[(std::max)(detail::digits10() + 2, 5)]; char* begin = buffer; char* end = detail::write(begin, value); return std::string(begin, end); diff --git a/test/compile-test.cc b/test/compile-test.cc index d0f7b765..e17e07db 100644 --- a/test/compile-test.cc +++ b/test/compile-test.cc @@ -144,8 +144,15 @@ TEST(CompileTest, EmptyFormatString) { } #ifdef __cpp_if_constexpr -TEST(CompileTest, Basic) { +TEST(CompileTest, FormatDefault) { EXPECT_EQ("42", fmt::format(FMT_COMPILE("{}"), 42)); + EXPECT_EQ("42", fmt::format(FMT_COMPILE("{}"), 42u)); + EXPECT_EQ("42", fmt::format(FMT_COMPILE("{}"), 42ll)); + EXPECT_EQ("42", fmt::format(FMT_COMPILE("{}"), 42ull)); + EXPECT_EQ("true", fmt::format(FMT_COMPILE("{}"), true)); + EXPECT_EQ("x", fmt::format(FMT_COMPILE("{}"), 'x')); + EXPECT_EQ("4.2", fmt::format(FMT_COMPILE("{}"), 4.2)); EXPECT_EQ("foo", fmt::format(FMT_COMPILE("{}"), "foo")); + EXPECT_EQ("foo", fmt::format(FMT_COMPILE("{}"), std::string("foo"))); } #endif