From 3cedfc553003e1bf20596bd90fbf28639e4765c4 Mon Sep 17 00:00:00 2001 From: Ben Deane Date: Sun, 13 Aug 2023 21:15:29 -0600 Subject: [PATCH] Fix `formatted_size` with `FMT_COMPILE` and format specs Addresses issue #3586 --- include/fmt/format.h | 4 ++-- test/compile-test.cc | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index a311fd9f96496..f524758691097 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -1387,8 +1387,8 @@ FMT_CONSTEXPR auto format_uint(Char* buffer, UInt value, int num_digits, } template -inline auto format_uint(It out, UInt value, int num_digits, bool upper = false) - -> It { +FMT_CONSTEXPR inline auto format_uint(It out, UInt value, int num_digits, + bool upper = false) -> It { if (auto ptr = to_pointer(out, to_unsigned(num_digits))) { format_uint(ptr, value, num_digits, upper); return out; diff --git a/test/compile-test.cc b/test/compile-test.cc index f85d2d8d529ec..b71e1c4c6279c 100644 --- a/test/compile-test.cc +++ b/test/compile-test.cc @@ -182,6 +182,9 @@ TEST(compile_test, format_to) { end = fmt::format_to(buf, FMT_COMPILE("{:x}"), 42); *end = '\0'; EXPECT_STREQ("2a", buf); + end = fmt::format_to(buf, FMT_COMPILE("{:s}"), "abc"); + *end = '\0'; + EXPECT_STREQ("abc", buf); } TEST(compile_test, format_to_n) { @@ -193,12 +196,30 @@ TEST(compile_test, format_to_n) { res = fmt::format_to_n(buffer, buffer_size, FMT_COMPILE("{:x}"), 42); *res.out = '\0'; EXPECT_STREQ("2a", buffer); + res = fmt::format_to_n(buffer, buffer_size, FMT_COMPILE("{:s}"), "abc"); + *res.out = '\0'; + EXPECT_STREQ("abc", buffer); } # ifdef __cpp_lib_bit_cast TEST(compile_test, constexpr_formatted_size) { FMT_CONSTEXPR20 size_t size = fmt::formatted_size(FMT_COMPILE("{}"), 42); EXPECT_EQ(size, 2); + FMT_CONSTEXPR20 size_t hex_size = + fmt::formatted_size(FMT_COMPILE("{:x}"), 15); + EXPECT_EQ(hex_size, 1); + FMT_CONSTEXPR20 size_t binary_size = + fmt::formatted_size(FMT_COMPILE("{:b}"), 15); + EXPECT_EQ(binary_size, 4); + FMT_CONSTEXPR20 size_t padded_size = + fmt::formatted_size(FMT_COMPILE("{:*^6}"), 42); + EXPECT_EQ(padded_size, 6); + FMT_CONSTEXPR20 size_t float_size = + fmt::formatted_size(FMT_COMPILE("{:.3}"), 12.345); + EXPECT_EQ(float_size, 4); + FMT_CONSTEXPR20 size_t str_size = + fmt::formatted_size(FMT_COMPILE("{:s}"), "abc"); + EXPECT_EQ(str_size, 3); } # endif