Pass writer directly to format_value (#400)

This commit is contained in:
Victor Zverovich
2016-10-26 17:54:11 -07:00
parent b656a1c133
commit 2bba420337
8 changed files with 76 additions and 89 deletions

View File

@@ -17,9 +17,10 @@ using fmt::BasicPrintfArgFormatter;
class CustomArgFormatter
: public fmt::BasicArgFormatter<CustomArgFormatter, char> {
public:
CustomArgFormatter(fmt::basic_formatter<char, CustomArgFormatter> &f,
CustomArgFormatter(fmt::Writer &w,
fmt::basic_formatter<char, CustomArgFormatter> &f,
fmt::FormatSpec &s, const char *fmt)
: fmt::BasicArgFormatter<CustomArgFormatter, char>(f, s, fmt) {}
: fmt::BasicArgFormatter<CustomArgFormatter, char>(w, f, s, fmt) {}
void visit_double(double value) {
if (round(value * pow(10, spec().precision())) == 0)
@@ -47,12 +48,11 @@ class CustomPrintfArgFormatter :
typedef fmt::basic_formatter<char, CustomArgFormatter> CustomFormatter;
std::string custom_vformat(const char *format_str,
std::string custom_vformat(fmt::CStringRef format_str,
fmt::basic_format_args<CustomFormatter> args) {
fmt::MemoryWriter writer;
// Pass custom argument formatter as a template arg to basic_formatter.
CustomFormatter formatter(args, writer);
formatter.format(format_str);
// Pass custom argument formatter as a template arg to vformat.
fmt::vformat<CustomArgFormatter>(writer, format_str, args);
return writer.str();
}
@@ -69,8 +69,8 @@ std::string custom_vsprintf(
const char* format_str,
fmt::basic_format_args<CustomPrintfFormatter> args) {
fmt::MemoryWriter writer;
CustomPrintfFormatter formatter(args, writer);
formatter.format(format_str);
CustomPrintfFormatter formatter(args);
formatter.format(writer, format_str);
return writer.str();
}

View File

@@ -1357,7 +1357,7 @@ TEST(FormatterTest, FormatCStringRef) {
void format_value(fmt::Writer &w, const Date &d, fmt::basic_formatter<char> &f,
const char *) {
f.writer() << d.year() << '-' << d.month() << '-' << d.day();
w << d.year() << '-' << d.month() << '-' << d.day();
}
TEST(FormatterTest, FormatCustom) {
@@ -1371,7 +1371,7 @@ class Answer {};
template <typename Char>
void format_value(BasicWriter<Char> &w, Answer, fmt::basic_formatter<Char> &f,
const Char *) {
f.writer() << "42";
w << "42";
}
TEST(FormatterTest, CustomFormat) {
@@ -1626,9 +1626,10 @@ class MockArgFormatter :
public:
typedef fmt::internal::ArgFormatterBase<MockArgFormatter, char> Base;
MockArgFormatter(fmt::basic_formatter<char, MockArgFormatter> &f,
MockArgFormatter(fmt::Writer &w,
fmt::basic_formatter<char, MockArgFormatter> &f,
fmt::FormatSpec &s, const char *)
: fmt::internal::ArgFormatterBase<MockArgFormatter, char>(f.writer(), s) {
: fmt::internal::ArgFormatterBase<MockArgFormatter, char>(w, s) {
EXPECT_CALL(*this, visit_int(42));
}
@@ -1637,11 +1638,10 @@ class MockArgFormatter :
typedef fmt::basic_formatter<char, MockArgFormatter> CustomFormatter;
void custom_vformat(const char *format_str,
void custom_vformat(fmt::CStringRef format_str,
fmt::basic_format_args<CustomFormatter> args) {
fmt::MemoryWriter writer;
CustomFormatter formatter(args, writer);
formatter.format(format_str);
vformat(writer, format_str, args);
}
template <typename... Args>

View File

@@ -59,17 +59,18 @@ TEST(OStreamTest, Enum) {
}
struct TestArgFormatter : fmt::BasicArgFormatter<TestArgFormatter, char> {
TestArgFormatter(fmt::basic_formatter<char, TestArgFormatter> &f,
TestArgFormatter(fmt::Writer &w,
fmt::basic_formatter<char, TestArgFormatter> &f,
fmt::FormatSpec &s, const char *fmt)
: fmt::BasicArgFormatter<TestArgFormatter, char>(f, s, fmt) {}
: fmt::BasicArgFormatter<TestArgFormatter, char>(w, f, s, fmt) {}
};
TEST(OStreamTest, CustomArg) {
fmt::MemoryWriter writer;
typedef fmt::basic_formatter<char, TestArgFormatter> Formatter;
Formatter formatter(fmt::basic_format_args<Formatter>(), writer);
Formatter formatter((fmt::basic_format_args<Formatter>()));
fmt::FormatSpec spec;
TestArgFormatter af(formatter, spec, "}");
TestArgFormatter af(writer, formatter, spec, "}");
af.visit(fmt::internal::MakeArg<Formatter>(TestEnum()));
EXPECT_EQ("TestEnum", writer.str());
}

View File

@@ -567,9 +567,9 @@ TEST(ArgTest, MakeArg) {
EXPECT_EQ(fmt::internal::Arg::CUSTOM, arg.type);
EXPECT_EQ(&t, arg.custom.value);
fmt::MemoryWriter w;
fmt::basic_formatter<char> formatter(fmt::format_args(), w);
fmt::basic_formatter<char> formatter((fmt::format_args()));
const char *s = "}";
arg.custom.format(&formatter.writer(), &formatter, &t, &s);
arg.custom.format(&w, &formatter, &t, &s);
EXPECT_EQ("test", w.str());
}