Replace formatter with context

This commit is contained in:
Victor Zverovich
2016-11-06 18:59:17 -08:00
parent 2bba420337
commit 9998f66f8c
10 changed files with 223 additions and 242 deletions
+8 -12
View File
@@ -17,10 +17,9 @@ using fmt::BasicPrintfArgFormatter;
class CustomArgFormatter
: public fmt::BasicArgFormatter<CustomArgFormatter, char> {
public:
CustomArgFormatter(fmt::Writer &w,
fmt::basic_formatter<char, CustomArgFormatter> &f,
fmt::FormatSpec &s, const char *fmt)
: fmt::BasicArgFormatter<CustomArgFormatter, char>(w, f, s, fmt) {}
CustomArgFormatter(fmt::Writer &w, fmt::basic_format_context<char> &ctx,
fmt::FormatSpec &s)
: fmt::BasicArgFormatter<CustomArgFormatter, char>(w, ctx, s) {}
void visit_double(double value) {
if (round(value * pow(10, spec().precision())) == 0)
@@ -46,10 +45,7 @@ class CustomPrintfArgFormatter :
}
};
typedef fmt::basic_formatter<char, CustomArgFormatter> CustomFormatter;
std::string custom_vformat(fmt::CStringRef format_str,
fmt::basic_format_args<CustomFormatter> args) {
std::string custom_vformat(fmt::CStringRef format_str, fmt::format_args args) {
fmt::MemoryWriter writer;
// Pass custom argument formatter as a template arg to vformat.
fmt::vformat<CustomArgFormatter>(writer, format_str, args);
@@ -58,19 +54,19 @@ std::string custom_vformat(fmt::CStringRef format_str,
template <typename... Args>
std::string custom_format(const char *format_str, const Args & ... args) {
auto va = fmt::make_format_args<CustomFormatter>(args...);
auto va = fmt::make_format_args<fmt::format_context>(args...);
return custom_vformat(format_str, va);
}
typedef fmt::PrintfFormatter<char, CustomPrintfArgFormatter>
typedef fmt::printf_context<char, CustomPrintfArgFormatter>
CustomPrintfFormatter;
std::string custom_vsprintf(
const char* format_str,
fmt::basic_format_args<CustomPrintfFormatter> args) {
fmt::MemoryWriter writer;
CustomPrintfFormatter formatter(args);
formatter.format(writer, format_str);
CustomPrintfFormatter formatter(format_str, args);
formatter.format(writer);
return writer.str();
}
+8 -14
View File
@@ -1355,8 +1355,7 @@ TEST(FormatterTest, FormatCStringRef) {
EXPECT_EQ("test", format("{0}", CStringRef("test")));
}
void format_value(fmt::Writer &w, const Date &d, fmt::basic_formatter<char> &f,
const char *) {
void format_value(fmt::Writer &w, const Date &d, fmt::format_context &) {
w << d.year() << '-' << d.month() << '-' << d.day();
}
@@ -1369,8 +1368,7 @@ TEST(FormatterTest, FormatCustom) {
class Answer {};
template <typename Char>
void format_value(BasicWriter<Char> &w, Answer, fmt::basic_formatter<Char> &f,
const Char *) {
void format_value(BasicWriter<Char> &w, Answer, fmt::format_context &) {
w << "42";
}
@@ -1561,7 +1559,7 @@ std::string vformat_message(int id, const char *format, fmt::format_args args) {
template <typename... Args>
std::string format_message(int id, const char *format, const Args & ... args) {
auto va = fmt::make_format_args<fmt::basic_formatter<char>>(args...);
auto va = fmt::make_format_args<fmt::format_context>(args...);
return vformat_message(id, format, va);
}
@@ -1626,9 +1624,8 @@ class MockArgFormatter :
public:
typedef fmt::internal::ArgFormatterBase<MockArgFormatter, char> Base;
MockArgFormatter(fmt::Writer &w,
fmt::basic_formatter<char, MockArgFormatter> &f,
fmt::FormatSpec &s, const char *)
MockArgFormatter(fmt::Writer &w, fmt::format_context &ctx,
fmt::FormatSpec &s)
: fmt::internal::ArgFormatterBase<MockArgFormatter, char>(w, s) {
EXPECT_CALL(*this, visit_int(42));
}
@@ -1636,17 +1633,14 @@ class MockArgFormatter :
MOCK_METHOD1(visit_int, void (int value));
};
typedef fmt::basic_formatter<char, MockArgFormatter> CustomFormatter;
void custom_vformat(fmt::CStringRef format_str,
fmt::basic_format_args<CustomFormatter> args) {
void custom_vformat(fmt::CStringRef format_str, fmt::format_args args) {
fmt::MemoryWriter writer;
vformat(writer, format_str, args);
fmt::vformat<MockArgFormatter>(writer, format_str, args);
}
template <typename... Args>
void custom_format(const char *format_str, const Args & ... args) {
auto va = fmt::make_format_args<CustomFormatter>(args...);
auto va = fmt::make_format_args<fmt::format_context>(args...);
return custom_vformat(format_str, va);
}
+6 -8
View File
@@ -59,19 +59,17 @@ TEST(OStreamTest, Enum) {
}
struct TestArgFormatter : fmt::BasicArgFormatter<TestArgFormatter, char> {
TestArgFormatter(fmt::Writer &w,
fmt::basic_formatter<char, TestArgFormatter> &f,
fmt::FormatSpec &s, const char *fmt)
: fmt::BasicArgFormatter<TestArgFormatter, char>(w, f, s, fmt) {}
TestArgFormatter(fmt::Writer &w, fmt::format_context &ctx,
fmt::FormatSpec &s)
: fmt::BasicArgFormatter<TestArgFormatter, char>(w, ctx, s) {}
};
TEST(OStreamTest, CustomArg) {
fmt::MemoryWriter writer;
typedef fmt::basic_formatter<char, TestArgFormatter> Formatter;
Formatter formatter((fmt::basic_format_args<Formatter>()));
fmt::format_context ctx("}", fmt::format_args());
fmt::FormatSpec spec;
TestArgFormatter af(writer, formatter, spec, "}");
af.visit(fmt::internal::MakeArg<Formatter>(TestEnum()));
TestArgFormatter af(writer, ctx, spec);
af.visit(fmt::internal::MakeArg<fmt::format_context>(TestEnum()));
EXPECT_EQ("TestEnum", writer.str());
}
+10 -12
View File
@@ -65,13 +65,13 @@ struct Test {};
template <typename Char>
void format_value(fmt::BasicWriter<Char> &w, Test,
fmt::basic_formatter<Char> &f, const Char *) {
fmt::basic_format_context<Char> &) {
w << "test";
}
template <typename Char, typename T>
Arg make_arg(const T &value) {
typedef fmt::internal::MakeValue< fmt::basic_formatter<Char> > MakeValue;
typedef fmt::internal::MakeValue< fmt::basic_format_context<Char> > MakeValue;
Arg arg = MakeValue(value);
arg.type = fmt::internal::type<T>();
return arg;
@@ -567,9 +567,8 @@ 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()));
const char *s = "}";
arg.custom.format(&w, &formatter, &t, &s);
fmt::format_context ctx("}", fmt::format_args());
arg.custom.format(&w, &t, &ctx);
EXPECT_EQ("test", w.str());
}
@@ -580,21 +579,20 @@ TEST(UtilTest, FormatArgs) {
struct CustomFormatter {
typedef char char_type;
bool called;
};
void format_value(fmt::Writer &, const Test &, CustomFormatter &,
const char *&s) {
s = "custom_format";
void format_value(fmt::Writer &, const Test &, CustomFormatter &ctx) {
ctx.called = true;
}
TEST(UtilTest, MakeValueWithCustomFormatter) {
::Test t;
Arg arg = fmt::internal::MakeValue<CustomFormatter>(t);
CustomFormatter formatter;
const char *s = "";
CustomFormatter ctx = {false};
fmt::MemoryWriter w;
arg.custom.format(&w, &formatter, &t, &s);
EXPECT_STREQ("custom_format", s);
arg.custom.format(&w, &t, &ctx);
EXPECT_TRUE(ctx.called);
}
struct Result {