From d3107f85512a733d7422bb01e75bd01fc7c503c5 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sun, 7 Jun 2020 20:55:38 -0700 Subject: [PATCH] Cleanup arg_formatter_base --- include/fmt/format.h | 64 +++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 33 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index 1948b2f2..d6857413 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -1672,7 +1672,7 @@ template <> struct is_integral : std::true_type {}; template <> struct is_integral : std::true_type {}; template + typename ErrorHandler = error_handler> class arg_formatter_base { public: using iterator = OutputIt; @@ -1752,6 +1752,36 @@ class arg_formatter_base { out_ = write_ptr(out_, to_uintptr(p), specs_); } + struct char_spec_handler : ErrorHandler { + arg_formatter_base& formatter; + char_type value; + + char_spec_handler(arg_formatter_base& f, char_type val) + : formatter(f), value(val) {} + + void on_int() { + // char is only formatted as int if there are specs. + formatter.write_int(static_cast(value), *formatter.specs_); + } + void on_char() { + if (formatter.specs_) + formatter.out_ = write_char(formatter.out_, value, *formatter.specs_); + else + formatter.write(value); + } + }; + + struct cstring_spec_handler : error_handler { + arg_formatter_base& formatter; + const char_type* value; + + cstring_spec_handler(arg_formatter_base& f, const char_type* val) + : formatter(f), value(val) {} + + void on_string() { formatter.write(value); } + void on_pointer() { formatter.write_pointer(value); } + }; + protected: iterator out() { return out_; } format_specs* specs() { return specs_; } @@ -1811,38 +1841,6 @@ class arg_formatter_base { return out_; } - struct char_spec_handler : ErrorHandler { - arg_formatter_base& formatter; - char_type value; - - char_spec_handler(arg_formatter_base& f, char_type val) - : formatter(f), value(val) {} - - void on_int() { - if (formatter.specs_) - formatter.write_int(static_cast(value), *formatter.specs_); - else - formatter.write(value); - } - void on_char() { - if (formatter.specs_) - formatter.out_ = write_char(formatter.out_, value, *formatter.specs_); - else - formatter.write(value); - } - }; - - struct cstring_spec_handler : detail::error_handler { - arg_formatter_base& formatter; - const char_type* value; - - cstring_spec_handler(arg_formatter_base& f, const char_type* val) - : formatter(f), value(val) {} - - void on_string() { formatter.write(value); } - void on_pointer() { formatter.write_pointer(value); } - }; - iterator operator()(const char_type* value) { if (!specs_) return write(value), out_; handle_cstring_type_spec(specs_->type, cstring_spec_handler(*this, value));