From edf98792a51f7a0e7ca52290875fa9efe2c4892e Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Tue, 25 Oct 2016 05:55:40 -0700 Subject: [PATCH] Pass writer to format_value --- fmt/format.h | 24 +++++++++++++++++------- fmt/ostream.h | 2 +- fmt/printf.h | 5 +++-- fmt/time.h | 5 +++-- test/format-test.cc | 6 ++++-- test/util-test.cc | 13 ++++++++----- 6 files changed, 36 insertions(+), 19 deletions(-) diff --git a/fmt/format.h b/fmt/format.h index 40c7c17b..56347bad 100644 --- a/fmt/format.h +++ b/fmt/format.h @@ -993,7 +993,7 @@ struct Value { }; typedef void (*FormatFunc)( - void *formatter, const void *arg, void *format_str_ptr); + void *writer, void *formatter, const void *arg, void *format_str_ptr); struct CustomValue { const void *value; @@ -1160,7 +1160,7 @@ inline fmt::StringRef thousands_sep(...) { return ""; } #endif template -void format_value(Formatter &, const Char *, const T &) { +void format_value(BasicWriter &, Formatter &, const Char *, const T &) { FMT_STATIC_ASSERT(False::value, "Cannot format argument. To enable the use of ostream " "operator<< include fmt/ostream.h. Otherwise provide " @@ -1271,8 +1271,10 @@ class MakeValue : public Arg { // Formats an argument of a custom type, such as a user-defined class. template static void format_custom_arg( - void *formatter, const void *arg, void *format_str_ptr) { - format_value(*static_cast(formatter), + void *writer, void *formatter, const void *arg, void *format_str_ptr) { + typedef BasicWriter Writer; + format_value(*static_cast(writer), + *static_cast(formatter), *static_cast(format_str_ptr), *static_cast(arg)); } @@ -2178,7 +2180,7 @@ class BasicArgFormatter : public internal::ArgFormatterBase { /** Formats an argument of a custom (user-defined) type. */ void visit_custom(internal::Arg::CustomValue c) { - c.format(&formatter_, c.value, &format_); + c.format(&formatter_.writer(), &formatter_, c.value, &format_); } }; @@ -2240,6 +2242,14 @@ class basic_formatter : const Char *format(const Char *&format_str, const internal::Arg &arg); }; +template +void vformat(BasicWriter &writer, + BasicCStringRef format_str, + basic_format_args> args) { + basic_formatter formatter(args, writer); + formatter.format(format_str); +} + /** An error returned by an operating system or a language runtime, for example a file opening error. @@ -2470,7 +2480,7 @@ class BasicWriter { void vwrite(BasicCStringRef format, basic_format_args> args) { - basic_formatter(args, *this).format(format); + vformat(*this, format, args); } /** @@ -3459,7 +3469,7 @@ const Char *basic_formatter::format( FormatSpec spec; if (*s == ':') { if (arg.type == Arg::CUSTOM) { - arg.custom.format(this, arg.custom.value, &s); + arg.custom.format(&writer(), this, arg.custom.value, &s); return s; } ++s; diff --git a/fmt/ostream.h b/fmt/ostream.h index ac089749..5fee51a9 100644 --- a/fmt/ostream.h +++ b/fmt/ostream.h @@ -83,7 +83,7 @@ BasicStringRef format_value( // Formats a value. template -void format_value(basic_formatter &f, +void format_value(BasicWriter &w, basic_formatter &f, const Char *&format_str, const T &value) { internal::MemoryBuffer buffer; auto str = internal::format_value(buffer, value); diff --git a/fmt/printf.h b/fmt/printf.h index d75eef1f..1635396e 100644 --- a/fmt/printf.h +++ b/fmt/printf.h @@ -266,7 +266,7 @@ class BasicPrintfArgFormatter : public internal::ArgFormatterBase { this->writer()); const Char format_str[] = {'}', 0}; const Char *format = format_str; - c.format(&formatter, c.value, &format); + c.format(&formatter.writer(), &formatter, c.value, &format); } }; @@ -497,7 +497,8 @@ void PrintfFormatter::format(BasicCStringRef format_str) { // Formats a value. template -void format_value(PrintfFormatter &f, const Char *&, const T &value) { +void format_value(BasicWriter &w, PrintfFormatter &f, + const Char *&, const T &value) { internal::MemoryBuffer buffer; f.writer() << internal::format_value(buffer, value); } diff --git a/fmt/time.h b/fmt/time.h index ed2335c0..bb12a66f 100644 --- a/fmt/time.h +++ b/fmt/time.h @@ -14,8 +14,9 @@ #include namespace fmt { + template -void format_value(basic_formatter &f, +void format_value(Writer &w, basic_formatter &f, const char *&format_str, const std::tm &tm) { if (*format_str == ':') ++format_str; @@ -27,7 +28,7 @@ void format_value(basic_formatter &f, internal::MemoryBuffer format; format.append(format_str, end + 1); format[format.size() - 1] = '\0'; - Buffer &buffer = f.writer().buffer(); + Buffer &buffer = w.buffer(); std::size_t start = buffer.size(); for (;;) { std::size_t size = buffer.capacity() - start; diff --git a/test/format-test.cc b/test/format-test.cc index 2c7fc29f..998767bf 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1355,7 +1355,8 @@ TEST(FormatterTest, FormatCStringRef) { EXPECT_EQ("test", format("{0}", CStringRef("test"))); } -void format_value(fmt::basic_formatter &f, const char *, const Date &d) { +void format_value(fmt::Writer &w, fmt::basic_formatter &f, + const char *, const Date &d) { f.writer() << d.year() << '-' << d.month() << '-' << d.day(); } @@ -1368,7 +1369,8 @@ TEST(FormatterTest, FormatCustom) { class Answer {}; template -void format_value(fmt::basic_formatter &f, const Char *, Answer) { +void format_value(BasicWriter &w, fmt::basic_formatter &f, + const Char *, Answer) { f.writer() << "42"; } diff --git a/test/util-test.cc b/test/util-test.cc index 502f463e..d27d2a3f 100644 --- a/test/util-test.cc +++ b/test/util-test.cc @@ -64,8 +64,9 @@ namespace { struct Test {}; template -void format_value(fmt::basic_formatter &f, const Char *, Test) { - f.writer() << "test"; +void format_value(fmt::BasicWriter &w, fmt::basic_formatter &f, + const Char *, Test) { + w << "test"; } template @@ -568,7 +569,7 @@ TEST(ArgTest, MakeArg) { fmt::MemoryWriter w; fmt::basic_formatter formatter(fmt::format_args(), w); const char *s = "}"; - arg.custom.format(&formatter, &t, &s); + arg.custom.format(&formatter.writer(), &formatter, &t, &s); EXPECT_EQ("test", w.str()); } @@ -581,7 +582,8 @@ struct CustomFormatter { typedef char char_type; }; -void format_value(CustomFormatter &, const char *&s, const Test &) { +void format_value(fmt::Writer &, CustomFormatter &, const char *&s, + const Test &) { s = "custom_format"; } @@ -590,7 +592,8 @@ TEST(UtilTest, MakeValueWithCustomFormatter) { Arg arg = fmt::internal::MakeValue(t); CustomFormatter formatter; const char *s = ""; - arg.custom.format(&formatter, &t, &s); + fmt::MemoryWriter w; + arg.custom.format(&w, &formatter, &t, &s); EXPECT_STREQ("custom_format", s); }