diff --git a/include/fmt/core.h b/include/fmt/core.h index ced03272..8cb07584 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -2440,9 +2440,9 @@ FMT_CONSTEXPR void check_int_type_spec(char spec, ErrorHandler&& eh) { } // Checks char specs and returns true if the type spec is char (and not int). -template +template FMT_CONSTEXPR bool check_char_specs(const basic_format_specs& specs, - ErrorHandler&& eh) { + ErrorHandler&& eh = {}) { if (specs.type && specs.type != 'c') { check_int_type_spec(specs.type, eh); return false; @@ -2514,23 +2514,11 @@ FMT_CONSTEXPR float_specs parse_float_type_spec( return result; } -template -class cstring_type_checker : public ErrorHandler { - public: - constexpr explicit cstring_type_checker(ErrorHandler eh) : ErrorHandler(eh) {} - - FMT_CONSTEXPR void on_string() {} - FMT_CONSTEXPR void on_pointer() {} -}; - -template -FMT_CONSTEXPR void handle_cstring_type_spec(Char spec, Handler&& handler) { - if (spec == 0 || spec == 's') - handler.on_string(); - else if (spec == 'p') - handler.on_pointer(); - else - handler.on_error("invalid type specifier"); +template +FMT_CONSTEXPR bool check_cstring_type_spec(Char spec, ErrorHandler&& eh = {}) { + if (spec == 0 || spec == 's') return true; + if (spec != 'p') eh.on_error("invalid type specifier"); + return false; } template @@ -2543,8 +2531,8 @@ FMT_CONSTEXPR void check_pointer_type_spec(Char spec, ErrorHandler&& eh) { if (spec != 0 && spec != 'p') eh.on_error("invalid type specifier"); } -// A format specifier handler that checks if specifiers are consistent with the -// argument type. +// A parse_format_specs handler that checks if specifiers are consistent with +// the argument type. template class specs_checker : public Handler { private: detail::type arg_type_; @@ -2801,8 +2789,7 @@ struct formatter(eh)); + detail::check_cstring_type_spec(specs_.type, eh); break; case detail::type::string_type: detail::check_string_type_spec(specs_.type, eh); diff --git a/include/fmt/format.h b/include/fmt/format.h index aefad1be..7a61bd3c 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -1306,7 +1306,7 @@ template FMT_CONSTEXPR OutputIt write(OutputIt out, Char value, const basic_format_specs& specs, locale_ref loc = {}) { - return check_char_specs(specs, error_handler()) + return check_char_specs(specs) ? write_char(out, value, specs) : write(out, static_cast(value), specs, loc); } @@ -1531,20 +1531,9 @@ template FMT_CONSTEXPR OutputIt write(OutputIt out, const Char* s, const basic_format_specs& specs, locale_ref) { - struct handler { - OutputIt out; - const Char* value; - const basic_format_specs& specs; - - void on_string() { - out = write(out, basic_string_view(value), specs, {}); - } - void on_pointer() { out = write_ptr(out, to_uintptr(value), &specs); } - void on_error(const char* message) { error_handler().on_error(message); } - }; - auto h = handler{out, s, specs}; - handle_cstring_type_spec(specs.type, h); - return h.out; + return check_cstring_type_spec(specs.type) + ? write(out, basic_string_view(s), specs, {}) + : write_ptr(out, to_uintptr(s), &specs); } template