Make format_to work with basic_memory_buffer (#776)

This commit is contained in:
Victor Zverovich 2018-06-23 08:29:26 -07:00
parent 3abd036c47
commit ab2d88ca8e
2 changed files with 15 additions and 7 deletions

View File

@ -3480,9 +3480,9 @@ std::wstring to_wstring(const T &value) {
return str; return str;
} }
template <typename Char> template <typename Char, std::size_t SIZE>
std::basic_string<Char> to_string(const basic_memory_buffer<Char> &buffer) { std::basic_string<Char> to_string(const basic_memory_buffer<Char, SIZE> &buf) {
return std::basic_string<Char>(buffer.data(), buffer.size()); return std::basic_string<Char>(buf.data(), buf.size());
} }
inline format_context::iterator vformat_to( inline format_context::iterator vformat_to(
@ -3497,15 +3497,17 @@ inline wformat_context::iterator vformat_to(
return vformat_to<arg_formatter<range>>(buf, format_str, args); return vformat_to<arg_formatter<range>>(buf, format_str, args);
} }
template <typename... Args> template <typename... Args, std::size_t SIZE = inline_buffer_size>
inline format_context::iterator format_to( inline format_context::iterator format_to(
memory_buffer &buf, string_view format_str, const Args & ... args) { basic_memory_buffer<char, SIZE> &buf, string_view format_str,
const Args & ... args) {
return vformat_to(buf, format_str, make_format_args(args...)); return vformat_to(buf, format_str, make_format_args(args...));
} }
template <typename... Args> template <typename... Args, std::size_t SIZE = inline_buffer_size>
inline wformat_context::iterator format_to( inline wformat_context::iterator format_to(
wmemory_buffer &buf, wstring_view format_str, const Args & ... args) { basic_memory_buffer<wchar_t, SIZE> &buf, wstring_view format_str,
const Args & ... args) {
return vformat_to(buf, format_str, make_format_args<wformat_context>(args...)); return vformat_to(buf, format_str, make_format_args<wformat_context>(args...));
} }

View File

@ -233,6 +233,12 @@ TEST(FormatToTest, FormatToNonbackInsertIteratorWithSignAndNumericAlignment) {
EXPECT_STREQ("+42", buffer); EXPECT_STREQ("+42", buffer);
} }
TEST(FormatToTest, FormatToMemoryBuffer) {
fmt::basic_memory_buffer<char, 100> buffer;
fmt::format_to(buffer, "{}", "foo");
EXPECT_EQ("foo", to_string(buffer));
}
TEST(FormatterTest, Escape) { TEST(FormatterTest, Escape) {
EXPECT_EQ("{", format("{{")); EXPECT_EQ("{", format("{{"));
EXPECT_EQ("before {", format("before {{")); EXPECT_EQ("before {", format("before {{"));