Fix the returned value of format_to_n with user-defined types having operator<<.

This commit is contained in:
Marek Kurdej 2018-07-09 15:49:44 +02:00 committed by Victor Zverovich
parent 9c32e73abf
commit 2a4cd6d05e
2 changed files with 23 additions and 2 deletions

View File

@ -124,8 +124,7 @@ struct formatter<T, Char,
basic_memory_buffer<Char> buffer; basic_memory_buffer<Char> buffer;
internal::format_value(buffer, value); internal::format_value(buffer, value);
basic_string_view<Char> str(buffer.data(), buffer.size()); basic_string_view<Char> str(buffer.data(), buffer.size());
formatter<basic_string_view<Char>, Char>::format(str, ctx); return formatter<basic_string_view<Char>, Char>::format(str, ctx);
return ctx.out();
} }
}; };

View File

@ -169,3 +169,25 @@ TEST(OStreamTest, ConstexprString) {
EXPECT_EQ("42", format(fmt("{}"), std::string("42"))); EXPECT_EQ("42", format(fmt("{}"), std::string("42")));
} }
#endif #endif
namespace fmt_test {
struct ABC {};
template <typename Output> Output &operator<<(Output &out, ABC) {
out << "ABC";
return out;
}
} // namespace fmt_test
TEST(FormatTest, FormatToN) {
char buffer[4];
buffer[3] = 'x';
auto result = fmt::format_to_n(buffer, 3, "{}", fmt_test::ABC());
EXPECT_EQ(3u, result.size);
EXPECT_EQ(buffer + 3, result.out);
EXPECT_EQ("ABCx", fmt::string_view(buffer, 4));
result = fmt::format_to_n(buffer, 3, "x{}y", fmt_test::ABC());
EXPECT_EQ(5u, result.size);
EXPECT_EQ(buffer + 3, result.out);
EXPECT_EQ("xABx", fmt::string_view(buffer, 4));
}