Skip to content

Commit

Permalink
fix formatting with empty compiled format string (#2042)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexezeder authored and vitaut committed Dec 3, 2020
1 parent f43416e commit c609ebf
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion include/fmt/compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ FMT_INLINE std::basic_string<typename S::char_type> format(const S&,
#ifdef __cpp_if_constexpr
if constexpr (std::is_same<typename S::char_type, char>::value) {
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...));
}
#endif
Expand Down
5 changes: 4 additions & 1 deletion include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -3538,7 +3538,7 @@ struct formatter<T, Char,
struct formatter<Type, Char> : formatter<Base, Char> { \
template <typename FormatContext> \
auto format(Type const& val, FormatContext& ctx) -> decltype(ctx.out()) { \
return formatter<Base, Char>::format(val, ctx); \
return formatter<Base, Char>::format(static_cast<Base>(val), ctx); \
} \
}

Expand All @@ -3552,6 +3552,9 @@ FMT_FORMAT_AS(Char*, const Char*);
FMT_FORMAT_AS(std::basic_string<Char>, basic_string_view<Char>);
FMT_FORMAT_AS(std::nullptr_t, const void*);
FMT_FORMAT_AS(detail::std_string_view<Char>, basic_string_view<Char>);
#if __cplusplus >= 201703L
FMT_FORMAT_AS(std::byte, unsigned);
#endif

template <typename Char>
struct formatter<void*, Char> : formatter<const void*, Char> {
Expand Down
4 changes: 4 additions & 0 deletions test/compile-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ TEST(CompileTest, TextAndArg) {
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

#if __cplusplus >= 202002L
Expand Down
7 changes: 7 additions & 0 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1762,6 +1762,13 @@ TEST(FormatTest, JoinArg) {
#endif
}

#if __cplusplus >= 201703L
TEST(FormatTest, JoinBytes) {
std::vector<std::byte> v = {std::byte(1), std::byte(2), std::byte(3)};
EXPECT_EQ("1, 2, 3", fmt::format("{}", fmt::join(v, ", ")));
}
#endif

template <typename T> std::string str(const T& value) {
return fmt::format("{}", value);
}
Expand Down

0 comments on commit c609ebf

Please sign in to comment.