Replace operator<< with write function

This commit is contained in:
Victor Zverovich
2017-01-22 07:40:21 -08:00
parent b77c8190ca
commit ec15ef7b7b
11 changed files with 105 additions and 89 deletions

View File

@ -193,9 +193,12 @@ void format_error_code(writer &out, int error_code,
++error_code_size; ++error_code_size;
} }
error_code_size += internal::count_digits(abs_value); error_code_size += internal::count_digits(abs_value);
if (message.size() <= internal::INLINE_BUFFER_SIZE - error_code_size) if (message.size() <= internal::INLINE_BUFFER_SIZE - error_code_size) {
out << message << SEP; out.write(message);
out << ERROR_STR << error_code; out.write(SEP);
}
out.write(ERROR_STR);
out.write(error_code);
assert(out.size() <= internal::INLINE_BUFFER_SIZE); assert(out.size() <= internal::INLINE_BUFFER_SIZE);
} }
@ -367,7 +370,9 @@ FMT_FUNC void internal::format_windows_error(
if (result != 0) { if (result != 0) {
UTF16ToUTF8 utf8_message; UTF16ToUTF8 utf8_message;
if (utf8_message.convert(system_message) == ERROR_SUCCESS) { if (utf8_message.convert(system_message) == ERROR_SUCCESS) {
out << message << ": " << utf8_message; out.write(message);
out.write(": ");
out.write(utf8_message);
return; return;
} }
break; break;
@ -391,7 +396,9 @@ FMT_FUNC void format_system_error(
char *system_message = &buffer[0]; char *system_message = &buffer[0];
int result = safe_strerror(error_code, system_message, buffer.size()); int result = safe_strerror(error_code, system_message, buffer.size());
if (result == 0) { if (result == 0) {
out << message << ": " << system_message; out.write(message);
out.write(": ");
out.write(system_message);
return; return;
} }
if (result != ERANGE) if (result != ERANGE)
@ -423,7 +430,7 @@ FMT_FUNC void report_windows_error(
FMT_FUNC void vprint(std::FILE *f, CStringRef format_str, format_args args) { FMT_FUNC void vprint(std::FILE *f, CStringRef format_str, format_args args) {
MemoryWriter w; MemoryWriter w;
w.vwrite(format_str, args); w.vformat(format_str, args);
std::fwrite(w.data(), 1, w.size(), f); std::fwrite(w.data(), 1, w.size(), f);
} }

View File

@ -1535,7 +1535,7 @@ class format_arg_store {
static const uint64_t TYPES = internal::make_type<Args..., void>(); static const uint64_t TYPES = internal::make_type<Args..., void>();
format_arg_store(const Args &... args) format_arg_store(const Args &... args)
: data_(Array{internal::make_arg<IS_PACKED, Context>(args)...}) {} : data_(Array{{internal::make_arg<IS_PACKED, Context>(args)...}}) {}
const value_type *data() const { return data_.data(); } const value_type *data() const { return data_.data(); }
}; };
@ -2043,7 +2043,7 @@ class ArgFormatterBase {
template <typename Char> template <typename Char>
inline void write(basic_writer<Char> &w, const Char *start, const Char *end) { inline void write(basic_writer<Char> &w, const Char *start, const Char *end) {
if (start != end) if (start != end)
w << BasicStringRef<Char>(start, internal::to_unsigned(end - start)); w.write(BasicStringRef<Char>(start, internal::to_unsigned(end - start)));
} }
template <typename Char, typename Context> template <typename Char, typename Context>
@ -2392,8 +2392,8 @@ class basic_writer {
return std::basic_string<Char>(&buffer_[0], buffer_.size()); return std::basic_string<Char>(&buffer_[0], buffer_.size());
} }
void vwrite(BasicCStringRef<Char> format, void vformat(BasicCStringRef<Char> format,
basic_format_args<basic_format_context<Char>> args); basic_format_args<basic_format_context<Char>> args);
/** /**
\rst \rst
Writes formatted data. Writes formatted data.
@ -2403,8 +2403,8 @@ class basic_writer {
**Example**:: **Example**::
MemoryWriter out; MemoryWriter out;
out.write("Current point:\n"); out.format("Current point:\n");
out.write("({:+f}, {:+f})", -3.14, 3.14); out.format("({:+f}, {:+f})", -3.14, 3.14);
This will write the following output to the ``out`` object: This will write the following output to the ``out`` object:
@ -2420,27 +2420,24 @@ class basic_writer {
\endrst \endrst
*/ */
template <typename... Args> template <typename... Args>
void write(BasicCStringRef<Char> format, const Args & ... args) { void format(BasicCStringRef<Char> format, const Args & ... args) {
vwrite(format, make_xformat_args<basic_format_context<Char>>(args...)); vformat(format, make_xformat_args<basic_format_context<Char>>(args...));
} }
basic_writer &operator<<(int value) { void write(int value) {
write_decimal(value); write_decimal(value);
return *this;
} }
basic_writer &operator<<(unsigned value) { void write(unsigned value) {
return *this << IntFormatSpec<unsigned>(value); *this << IntFormatSpec<unsigned>(value);
} }
basic_writer &operator<<(long value) { void write(long value) {
write_decimal(value); write_decimal(value);
return *this;
} }
basic_writer &operator<<(unsigned long value) { void write(unsigned long value) {
return *this << IntFormatSpec<unsigned long>(value); *this << IntFormatSpec<unsigned long>(value);
} }
basic_writer &operator<<(LongLong value) { void write(LongLong value) {
write_decimal(value); write_decimal(value);
return *this;
} }
/** /**
@ -2448,13 +2445,12 @@ class basic_writer {
Formats *value* and writes it to the stream. Formats *value* and writes it to the stream.
\endrst \endrst
*/ */
basic_writer &operator<<(ULongLong value) { void write(ULongLong value) {
return *this << IntFormatSpec<ULongLong>(value); *this << IntFormatSpec<ULongLong>(value);
} }
basic_writer &operator<<(double value) { void write(double value) {
write_double(value, FormatSpec()); write_double(value, FormatSpec());
return *this;
} }
/** /**
@ -2463,23 +2459,19 @@ class basic_writer {
(``'g'``) and writes it to the stream. (``'g'``) and writes it to the stream.
\endrst \endrst
*/ */
basic_writer &operator<<(long double value) { void write(long double value) {
write_double(value, FormatSpec()); write_double(value, FormatSpec());
return *this;
} }
/** /**
Writes a character to the stream. Writes a character to the stream.
*/ */
basic_writer &operator<<(char value) { void write(char value) {
buffer_.push_back(value); buffer_.push_back(value);
return *this;
} }
basic_writer &operator<<( void write(typename internal::WCharHelper<wchar_t, Char>::Supported value) {
typename internal::WCharHelper<wchar_t, Char>::Supported value) {
buffer_.push_back(value); buffer_.push_back(value);
return *this;
} }
/** /**
@ -2487,17 +2479,14 @@ class basic_writer {
Writes *value* to the stream. Writes *value* to the stream.
\endrst \endrst
*/ */
basic_writer &operator<<(fmt::BasicStringRef<Char> value) { void write(fmt::BasicStringRef<Char> value) {
const Char *str = value.data(); const Char *str = value.data();
buffer_.append(str, str + value.size()); buffer_.append(str, str + value.size());
return *this;
} }
basic_writer &operator<<( void write(typename internal::WCharHelper<StringRef, Char>::Supported value) {
typename internal::WCharHelper<StringRef, Char>::Supported value) {
const char *str = value.data(); const char *str = value.data();
buffer_.append(str, str + value.size()); buffer_.append(str, str + value.size());
return *this;
} }
template <typename T, typename Spec, typename FillChar> template <typename T, typename Spec, typename FillChar>
@ -3076,7 +3065,7 @@ inline void print_colored(Color c, CStringRef format_str,
inline std::string vformat(CStringRef format_str, format_args args) { inline std::string vformat(CStringRef format_str, format_args args) {
MemoryWriter w; MemoryWriter w;
w.vwrite(format_str, args); w.vformat(format_str, args);
return w.str(); return w.str();
} }
@ -3096,7 +3085,7 @@ inline std::string format(CStringRef format_str, const Args & ... args) {
inline std::wstring vformat(WCStringRef format_str, wformat_args args) { inline std::wstring vformat(WCStringRef format_str, wformat_args args) {
WMemoryWriter w; WMemoryWriter w;
w.vwrite(format_str, args); w.vformat(format_str, args);
return w.str(); return w.str();
} }
@ -3574,7 +3563,7 @@ void do_format_arg(basic_writer<Char> &writer, basic_format_arg<Context> arg,
/** Formats arguments and writes the output to the writer. */ /** Formats arguments and writes the output to the writer. */
template <typename ArgFormatter, typename Char, typename Context> template <typename ArgFormatter, typename Char, typename Context>
void vformat(basic_writer<Char> &writer, BasicCStringRef<Char> format_str, void vwrite(basic_writer<Char> &writer, BasicCStringRef<Char> format_str,
basic_format_args<Context> args) { basic_format_args<Context> args) {
basic_format_context<Char> ctx(format_str.c_str(), args); basic_format_context<Char> ctx(format_str.c_str(), args);
const Char *&s = ctx.ptr(); const Char *&s = ctx.ptr();
@ -3599,10 +3588,10 @@ void vformat(basic_writer<Char> &writer, BasicCStringRef<Char> format_str,
} }
template <typename Char> template <typename Char>
inline void basic_writer<Char>::vwrite( inline void basic_writer<Char>::vformat(
BasicCStringRef<Char> format, BasicCStringRef<Char> format,
basic_format_args<basic_format_context<Char>> args) { basic_format_args<basic_format_context<Char>> args) {
vformat<ArgFormatter<Char>>(*this, format, args); vwrite<ArgFormatter<Char>>(*this, format, args);
} }
} // namespace fmt } // namespace fmt

View File

@ -30,7 +30,7 @@ FMT_FUNC void write(std::ostream &os, writer &w) {
FMT_FUNC void vprint(std::ostream &os, CStringRef format_str, FMT_FUNC void vprint(std::ostream &os, CStringRef format_str,
format_args args) { format_args args) {
MemoryWriter w; MemoryWriter w;
w.vwrite(format_str, args); w.vformat(format_str, args);
internal::write(os, w); internal::write(os, w);
} }
} // namespace fmt } // namespace fmt

View File

@ -505,7 +505,7 @@ template <typename Char, typename T>
void format_value(basic_writer<Char> &w, const T &value, void format_value(basic_writer<Char> &w, const T &value,
printf_context<Char>& ctx) { printf_context<Char>& ctx) {
internal::MemoryBuffer<Char, internal::INLINE_BUFFER_SIZE> buffer; internal::MemoryBuffer<Char, internal::INLINE_BUFFER_SIZE> buffer;
w << internal::format_value(buffer, value); w.write(internal::format_value(buffer, value));
} }
template <typename Char> template <typename Char>

View File

@ -111,7 +111,7 @@ typedef BasicStringWriter<wchar_t> WStringWriter;
template <typename T> template <typename T>
std::string to_string(const T &value) { std::string to_string(const T &value) {
fmt::MemoryWriter w; fmt::MemoryWriter w;
w << value; w.write(value);
return w.str(); return w.str();
} }
} }

View File

@ -48,7 +48,7 @@ class CustomPrintfArgFormatter : public PrintfArgFormatter<char> {
std::string custom_vformat(fmt::CStringRef format_str, fmt::format_args args) { std::string custom_vformat(fmt::CStringRef format_str, fmt::format_args args) {
fmt::MemoryWriter writer; fmt::MemoryWriter writer;
// Pass custom argument formatter as a template arg to vformat. // Pass custom argument formatter as a template arg to vformat.
fmt::vformat<CustomArgFormatter>(writer, format_str, args); fmt::vwrite<CustomArgFormatter>(writer, format_str, args);
return writer.str(); return writer.str();
} }

View File

@ -65,7 +65,7 @@ TEST(FormatTest, ArgConverter) {
TEST(FormatTest, FormatNegativeNaN) { TEST(FormatTest, FormatNegativeNaN) {
double nan = std::numeric_limits<double>::quiet_NaN(); double nan = std::numeric_limits<double>::quiet_NaN();
if (fmt::internal::FPUtil::isnegative(-nan)) if (fmt::internal::fputil::isnegative(-nan))
EXPECT_EQ("-nan", fmt::format("{}", -nan)); EXPECT_EQ("-nan", fmt::format("{}", -nan));
else else
fmt::print("Warning: compiler doesn't handle negative NaN correctly"); fmt::print("Warning: compiler doesn't handle negative NaN correctly");
@ -107,7 +107,7 @@ TEST(FormatTest, FormatErrorCode) {
std::string msg = "error 42", sep = ": "; std::string msg = "error 42", sep = ": ";
{ {
fmt::MemoryWriter w; fmt::MemoryWriter w;
w << "garbage"; w.write("garbage");
fmt::format_error_code(w, 42, "test"); fmt::format_error_code(w, 42, "test");
EXPECT_EQ("test: " + msg, w.str()); EXPECT_EQ("test: " + msg, w.str());
} }

View File

@ -107,8 +107,9 @@ void std_format(long double value, std::wstring &result) {
// as writing it to std::basic_ostringstream<Char>. // as writing it to std::basic_ostringstream<Char>.
template <typename Char, typename T> template <typename Char, typename T>
::testing::AssertionResult check_write(const T &value, const char *type) { ::testing::AssertionResult check_write(const T &value, const char *type) {
std::basic_string<Char> actual = fmt::BasicMemoryWriter<Char> writer;
(fmt::BasicMemoryWriter<Char>() << value).str(); writer.write(value);
std::basic_string<Char> actual = writer.str();
std::basic_string<Char> expected; std::basic_string<Char> expected;
std_format(value, expected); std_format(value, expected);
if (expected == actual) if (expected == actual)
@ -191,19 +192,19 @@ void check_move_writer(const std::string &str, MemoryWriter &w) {
TEST(WriterTest, MoveCtor) { TEST(WriterTest, MoveCtor) {
MemoryWriter w; MemoryWriter w;
w << "test"; w.write("test");
check_move_writer("test", w); check_move_writer("test", w);
// This fills the inline buffer, but doesn't cause dynamic allocation. // This fills the inline buffer, but doesn't cause dynamic allocation.
std::string s; std::string s;
for (int i = 0; i < fmt::internal::INLINE_BUFFER_SIZE; ++i) for (int i = 0; i < fmt::internal::INLINE_BUFFER_SIZE; ++i)
s += '*'; s += '*';
w.clear(); w.clear();
w << s; w.write(s);
check_move_writer(s, w); check_move_writer(s, w);
const char *inline_buffer_ptr = w.data(); const char *inline_buffer_ptr = w.data();
// Adding one more character causes the content to move from the inline to // Adding one more character causes the content to move from the inline to
// a dynamically allocated buffer. // a dynamically allocated buffer.
w << '*'; w.write('*');
MemoryWriter w2(std::move(w)); MemoryWriter w2(std::move(w));
// Move should rip the guts of the first writer. // Move should rip the guts of the first writer.
EXPECT_EQ(inline_buffer_ptr, w.data()); EXPECT_EQ(inline_buffer_ptr, w.data());
@ -220,19 +221,19 @@ void CheckMoveAssignWriter(const std::string &str, MemoryWriter &w) {
TEST(WriterTest, MoveAssignment) { TEST(WriterTest, MoveAssignment) {
MemoryWriter w; MemoryWriter w;
w << "test"; w.write("test");
CheckMoveAssignWriter("test", w); CheckMoveAssignWriter("test", w);
// This fills the inline buffer, but doesn't cause dynamic allocation. // This fills the inline buffer, but doesn't cause dynamic allocation.
std::string s; std::string s;
for (int i = 0; i < fmt::internal::INLINE_BUFFER_SIZE; ++i) for (int i = 0; i < fmt::internal::INLINE_BUFFER_SIZE; ++i)
s += '*'; s += '*';
w.clear(); w.clear();
w << s; w.write(s);
CheckMoveAssignWriter(s, w); CheckMoveAssignWriter(s, w);
const char *inline_buffer_ptr = w.data(); const char *inline_buffer_ptr = w.data();
// Adding one more character causes the content to move from the inline to // Adding one more character causes the content to move from the inline to
// a dynamically allocated buffer. // a dynamically allocated buffer.
w << '*'; w.write('*');
MemoryWriter w2; MemoryWriter w2;
w2 = std::move(w); w2 = std::move(w);
// Move should rip the guts of the first writer. // Move should rip the guts of the first writer.
@ -252,19 +253,19 @@ TEST(WriterTest, Allocator) {
std::vector<char> mem(size); std::vector<char> mem(size);
EXPECT_CALL(alloc, allocate(size)).WillOnce(testing::Return(&mem[0])); EXPECT_CALL(alloc, allocate(size)).WillOnce(testing::Return(&mem[0]));
for (int i = 0; i < fmt::internal::INLINE_BUFFER_SIZE + 1; ++i) for (int i = 0; i < fmt::internal::INLINE_BUFFER_SIZE + 1; ++i)
w << '*'; w.write('*');
EXPECT_CALL(alloc, deallocate(&mem[0], size)); EXPECT_CALL(alloc, deallocate(&mem[0], size));
} }
TEST(WriterTest, Data) { TEST(WriterTest, Data) {
MemoryWriter w; MemoryWriter w;
w << 42; w.write(42);
EXPECT_EQ("42", std::string(w.data(), w.size())); EXPECT_EQ("42", std::string(w.data(), w.size()));
} }
TEST(WriterTest, WriteWithoutArgs) { TEST(WriterTest, WriteWithoutArgs) {
MemoryWriter w; MemoryWriter w;
w.write("test"); w.format("test");
EXPECT_EQ("test", std::string(w.data(), w.size())); EXPECT_EQ("test", std::string(w.data(), w.size()));
} }
@ -317,15 +318,15 @@ TEST(WriterTest, WriteLongDouble) {
TEST(WriterTest, WriteDoubleAtBufferBoundary) { TEST(WriterTest, WriteDoubleAtBufferBoundary) {
MemoryWriter writer; MemoryWriter writer;
for (int i = 0; i < 100; ++i) for (int i = 0; i < 100; ++i)
writer << 1.23456789; writer.write(1.23456789);
} }
TEST(WriterTest, WriteDoubleWithFilledBuffer) { TEST(WriterTest, WriteDoubleWithFilledBuffer) {
MemoryWriter writer; MemoryWriter writer;
// Fill the buffer. // Fill the buffer.
for (int i = 0; i < fmt::internal::INLINE_BUFFER_SIZE; ++i) for (int i = 0; i < fmt::internal::INLINE_BUFFER_SIZE; ++i)
writer << ' '; writer.write(' ');
writer << 1.2; writer.write(1.2);
EXPECT_STREQ("1.2", writer.c_str() + fmt::internal::INLINE_BUFFER_SIZE); EXPECT_STREQ("1.2", writer.c_str() + fmt::internal::INLINE_BUFFER_SIZE);
} }
@ -399,8 +400,13 @@ TEST(WriterTest, hexu) {
} }
template <typename Char> template <typename Char>
basic_writer<Char> &operator<<(basic_writer<Char> &f, const Date &d) { basic_writer<Char> &operator<<(basic_writer<Char> &w, const Date &d) {
return f << d.year() << '-' << d.month() << '-' << d.day(); w.write(d.year());
w.write('-');
w.write(d.month());
w.write('-');
w.write(d.day());
return w;
} }
class ISO8601DateFormatter { class ISO8601DateFormatter {
@ -412,8 +418,12 @@ public:
template <typename Char> template <typename Char>
friend basic_writer<Char> &operator<<( friend basic_writer<Char> &operator<<(
basic_writer<Char> &w, const ISO8601DateFormatter &d) { basic_writer<Char> &w, const ISO8601DateFormatter &d) {
return w << pad(d.date_->year(), 4, '0') << '-' w << pad(d.date_->year(), 4, '0');
<< pad(d.date_->month(), 2, '0') << '-' << pad(d.date_->day(), 2, '0'); w.write('-');
w << pad(d.date_->month(), 2, '0');
w.write('-');
w << pad(d.date_->day(), 2, '0');
return w;
} }
}; };
@ -467,12 +477,12 @@ TEST(WriterTest, NoConflictWithIOManip) {
TEST(WriterTest, Format) { TEST(WriterTest, Format) {
MemoryWriter w; MemoryWriter w;
w.write("part{0}", 1); w.format("part{0}", 1);
EXPECT_EQ(strlen("part1"), w.size()); EXPECT_EQ(strlen("part1"), w.size());
EXPECT_STREQ("part1", w.c_str()); EXPECT_STREQ("part1", w.c_str());
EXPECT_STREQ("part1", w.data()); EXPECT_STREQ("part1", w.data());
EXPECT_EQ("part1", w.str()); EXPECT_EQ("part1", w.str());
w.write("part{0}", 2); w.format("part{0}", 2);
EXPECT_EQ(strlen("part1part2"), w.size()); EXPECT_EQ(strlen("part1part2"), w.size());
EXPECT_STREQ("part1part2", w.c_str()); EXPECT_STREQ("part1part2", w.c_str());
EXPECT_STREQ("part1part2", w.data()); EXPECT_STREQ("part1part2", w.data());
@ -495,27 +505,27 @@ TEST(ArrayWriterTest, CompileTimeSizeCtor) {
fmt::ArrayWriter w(array); fmt::ArrayWriter w(array);
EXPECT_EQ(0u, w.size()); EXPECT_EQ(0u, w.size());
EXPECT_STREQ("", w.c_str()); EXPECT_STREQ("", w.c_str());
w.write("{:10}", 1); w.format("{:10}", 1);
} }
TEST(ArrayWriterTest, Write) { TEST(ArrayWriterTest, Write) {
char array[10]; char array[10];
fmt::ArrayWriter w(array, sizeof(array)); fmt::ArrayWriter w(array, sizeof(array));
w.write("{}", 42); w.format("{}", 42);
EXPECT_EQ("42", w.str()); EXPECT_EQ("42", w.str());
} }
TEST(ArrayWriterTest, BufferOverflow) { TEST(ArrayWriterTest, BufferOverflow) {
char array[10]; char array[10];
fmt::ArrayWriter w(array, sizeof(array)); fmt::ArrayWriter w(array, sizeof(array));
w.write("{:10}", 1); w.format("{:10}", 1);
EXPECT_THROW_MSG(w.write("{}", 1), std::runtime_error, "buffer overflow"); EXPECT_THROW_MSG(w.format("{}", 1), std::runtime_error, "buffer overflow");
} }
TEST(ArrayWriterTest, WChar) { TEST(ArrayWriterTest, WChar) {
wchar_t array[10]; wchar_t array[10];
fmt::WArrayWriter w(array); fmt::WArrayWriter w(array);
w.write(L"{}", 42); w.format(L"{}", 42);
EXPECT_EQ(L"42", w.str()); EXPECT_EQ(L"42", w.str());
} }
@ -1356,7 +1366,11 @@ TEST(FormatterTest, FormatCStringRef) {
} }
void format_value(fmt::writer &w, const Date &d, fmt::format_context &) { void format_value(fmt::writer &w, const Date &d, fmt::format_context &) {
w << d.year() << '-' << d.month() << '-' << d.day(); w.write(d.year());
w.write('-');
w.write(d.month());
w.write('-');
w.write(d.day());
} }
TEST(FormatterTest, FormatCustom) { TEST(FormatterTest, FormatCustom) {
@ -1369,7 +1383,7 @@ class Answer {};
template <typename Char> template <typename Char>
void format_value(basic_writer<Char> &w, Answer, fmt::format_context &) { void format_value(basic_writer<Char> &w, Answer, fmt::format_context &) {
w << "42"; w.write("42");
} }
TEST(FormatterTest, CustomFormat) { TEST(FormatterTest, CustomFormat) {
@ -1400,14 +1414,16 @@ TEST(FormatterTest, FormatExamples) {
EXPECT_EQ("42", format(std::string("{}"), 42)); EXPECT_EQ("42", format(std::string("{}"), 42));
MemoryWriter out; MemoryWriter out;
out << "The answer is " << 42 << "\n"; out.write("The answer is ");
out.write("({:+f}, {:+f})", -3.14, 3.14); out.write(42);
out.write("\n");
out.format("({:+f}, {:+f})", -3.14, 3.14);
EXPECT_EQ("The answer is 42\n(-3.140000, +3.140000)", out.str()); EXPECT_EQ("The answer is 42\n(-3.140000, +3.140000)", out.str());
{ {
MemoryWriter writer; MemoryWriter writer;
for (int i = 0; i < 10; i++) for (int i = 0; i < 10; i++)
writer.write("{}", i); writer.format("{}", i);
std::string s = writer.str(); // s == 0123456789 std::string s = writer.str(); // s == 0123456789
EXPECT_EQ("0123456789", s); EXPECT_EQ("0123456789", s);
} }
@ -1552,8 +1568,8 @@ TEST(StrTest, Convert) {
std::string vformat_message(int id, const char *format, fmt::format_args args) { std::string vformat_message(int id, const char *format, fmt::format_args args) {
MemoryWriter w; MemoryWriter w;
w.write("[{}] ", id); w.format("[{}] ", id);
w.vwrite(format, args); w.vformat(format, args);
return w.str(); return w.str();
} }
@ -1642,7 +1658,7 @@ class MockArgFormatter : public fmt::internal::ArgFormatterBase<char> {
void custom_vformat(fmt::CStringRef format_str, fmt::format_args args) { void custom_vformat(fmt::CStringRef format_str, fmt::format_args args) {
fmt::MemoryWriter writer; fmt::MemoryWriter writer;
fmt::vformat<MockArgFormatter>(writer, format_str, args); fmt::vwrite<MockArgFormatter>(writer, format_str, args);
} }
template <typename... Args> template <typename... Args>

View File

@ -123,7 +123,7 @@ TEST(OStreamTest, Print) {
TEST(OStreamTest, WriteToOStream) { TEST(OStreamTest, WriteToOStream) {
std::ostringstream os; std::ostringstream os;
fmt::MemoryWriter w; fmt::MemoryWriter w;
w << "foo"; w.write("foo");
fmt::internal::write(os, w); fmt::internal::write(os, w);
EXPECT_EQ("foo", os.str()); EXPECT_EQ("foo", os.str());
} }

View File

@ -60,7 +60,9 @@ TEST(StringBufferTest, MoveTo) {
TEST(StringWriterTest, MoveTo) { TEST(StringWriterTest, MoveTo) {
fmt::StringWriter out; fmt::StringWriter out;
out << "The answer is " << 42 << "\n"; out.write("The answer is ");
out.write(42);
out.write("\n");
std::string s; std::string s;
out.move_to(s); out.move_to(s);
EXPECT_EQ("The answer is 42\n", s); EXPECT_EQ("The answer is 42\n", s);
@ -69,7 +71,9 @@ TEST(StringWriterTest, MoveTo) {
TEST(StringWriterTest, WString) { TEST(StringWriterTest, WString) {
fmt::WStringWriter out; fmt::WStringWriter out;
out << "The answer is " << 42 << "\n"; out.write("The answer is ");
out.write(42);
out.write("\n");
std::wstring s; std::wstring s;
out.move_to(s); out.move_to(s);
EXPECT_EQ(L"The answer is 42\n", s); EXPECT_EQ(L"The answer is 42\n", s);

View File

@ -70,7 +70,7 @@ struct Test {};
template <typename Char> template <typename Char>
void format_value(fmt::basic_writer<Char> &w, Test, void format_value(fmt::basic_writer<Char> &w, Test,
fmt::basic_format_context<Char> &) { fmt::basic_format_context<Char> &) {
w << "test"; w.write("test");
} }
template <typename Context, typename T> template <typename Context, typename T>
@ -726,7 +726,7 @@ TEST(UtilTest, SystemError) {
TEST(UtilTest, ReportSystemError) { TEST(UtilTest, ReportSystemError) {
fmt::MemoryWriter out; fmt::MemoryWriter out;
fmt::format_system_error(out, EDOM, "test error"); fmt::format_system_error(out, EDOM, "test error");
out << '\n'; out.write('\n');
EXPECT_WRITE(stderr, fmt::report_system_error(EDOM, "test error"), out.str()); EXPECT_WRITE(stderr, fmt::report_system_error(EDOM, "test error"), out.str());
} }