fix formatting with empty compiled format string (#2042)

This commit is contained in:
Alexey Ochapov 2020-12-02 18:14:57 +03:00 committed by GitHub
parent f43416e1d7
commit 683a74501f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 1 deletions

View File

@ -643,7 +643,7 @@ FMT_INLINE std::basic_string<typename S::char_type> format(const S&,
#ifdef __cpp_if_constexpr #ifdef __cpp_if_constexpr
if constexpr (std::is_same<typename S::char_type, char>::value) { if constexpr (std::is_same<typename S::char_type, char>::value) {
constexpr basic_string_view<typename S::char_type> str = S(); constexpr basic_string_view<typename S::char_type> str = S();
if (str.size() == 2 && str[0] == '{' && str[1] == '}') if constexpr (str.size() == 2 && str[0] == '{' && str[1] == '}')
return fmt::to_string(detail::first(args...)); return fmt::to_string(detail::first(args...));
} }
#endif #endif

View File

@ -173,6 +173,10 @@ TEST(CompileTest, TextAndArg) {
EXPECT_EQ(">>>42<<<", fmt::format(FMT_COMPILE(">>>{}<<<"), 42)); EXPECT_EQ(">>>42<<<", fmt::format(FMT_COMPILE(">>>{}<<<"), 42));
EXPECT_EQ("42!", fmt::format(FMT_COMPILE("{}!"), 42)); EXPECT_EQ("42!", fmt::format(FMT_COMPILE("{}!"), 42));
} }
TEST(CompileTest, Empty) {
EXPECT_EQ("", fmt::format(FMT_COMPILE("")));
}
#endif #endif
#if __cplusplus >= 202002L #if __cplusplus >= 202002L