forked from fmtlib/fmt
Fix handling of user-defined types in format_to (#793)
This commit is contained in:
@ -3216,9 +3216,8 @@ struct formatter<
|
|||||||
specs_.precision_, specs_.precision_ref, ctx);
|
specs_.precision_, specs_.precision_ref, ctx);
|
||||||
typedef output_range<typename FormatContext::iterator,
|
typedef output_range<typename FormatContext::iterator,
|
||||||
typename FormatContext::char_type> range_type;
|
typename FormatContext::char_type> range_type;
|
||||||
visit(arg_formatter<range_type>(ctx, specs_),
|
return visit(arg_formatter<range_type>(ctx, specs_),
|
||||||
internal::make_arg<FormatContext>(val));
|
internal::make_arg<FormatContext>(val));
|
||||||
return ctx.out();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -1122,7 +1122,8 @@ class Answer {};
|
|||||||
FMT_BEGIN_NAMESPACE
|
FMT_BEGIN_NAMESPACE
|
||||||
template <>
|
template <>
|
||||||
struct formatter<Answer> : formatter<int> {
|
struct formatter<Answer> : formatter<int> {
|
||||||
auto format(Answer, fmt::format_context &ctx) -> decltype(ctx.out()) {
|
template <typename FormatContext>
|
||||||
|
auto format(Answer, FormatContext &ctx) -> decltype(ctx.out()) {
|
||||||
return formatter<int>::format(42, ctx);
|
return formatter<int>::format(42, ctx);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -1133,6 +1134,13 @@ TEST(FormatterTest, CustomFormat) {
|
|||||||
EXPECT_EQ("0042", format("{:04}", Answer()));
|
EXPECT_EQ("0042", format("{:04}", Answer()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(FormatterTest, CustomFormatTo) {
|
||||||
|
char buf[10] = {};
|
||||||
|
auto end = fmt::format_to(buf, "{}", Answer());
|
||||||
|
EXPECT_EQ(end, buf + 2);
|
||||||
|
EXPECT_STREQ(buf, "42");
|
||||||
|
}
|
||||||
|
|
||||||
TEST(FormatterTest, WideFormatString) {
|
TEST(FormatterTest, WideFormatString) {
|
||||||
EXPECT_EQ(L"42", format(L"{}", 42));
|
EXPECT_EQ(L"42", format(L"{}", 42));
|
||||||
EXPECT_EQ(L"4.2", format(L"{}", 4.2));
|
EXPECT_EQ(L"4.2", format(L"{}", 4.2));
|
||||||
|
Reference in New Issue
Block a user