From 9660ea1bff2221dfd422844f09b2a7d670c71df6 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sun, 10 Feb 2019 16:59:58 -0800 Subject: [PATCH] Simplify format string checks --- include/fmt/color.h | 5 ++--- include/fmt/core.h | 27 ++++++++++----------------- include/fmt/locale.h | 5 ++--- include/fmt/ostream.h | 4 ++-- include/fmt/prepare.h | 17 +++++++++-------- 5 files changed, 25 insertions(+), 33 deletions(-) diff --git a/include/fmt/color.h b/include/fmt/color.h index d4b55a05..39c52c2f 100644 --- a/include/fmt/color.h +++ b/include/fmt/color.h @@ -634,9 +634,8 @@ template inline std::basic_string format(const text_style& ts, const S& format_str, const Args&... args) { - return internal::vformat( - ts, to_string_view(format_str), - *internal::checked_args(format_str, args...)); + return internal::vformat(ts, to_string_view(format_str), + {internal::make_args_checked(format_str, args...)}); } #endif diff --git a/include/fmt/core.h b/include/fmt/core.h index a3c84b7c..dca3c2e0 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -1362,17 +1362,11 @@ FMT_ENABLE_IF_T(is_compile_string::value) check_format_string(S); template -struct checked_args - : format_arg_store::type, Args...> { - typedef typename buffer_context::type context; - - checked_args(const S& format_str, const Args&... args) - : format_arg_store(args...) { - internal::check_format_string(format_str); - } - - basic_format_args operator*() const { return *this; } -}; +inline format_arg_store::type, Args...> +make_args_checked(const S& format_str, const Args&... args) { + internal::check_format_string(format_str); + return {args...}; +} template std::basic_string vformat( @@ -1434,7 +1428,7 @@ inline FMT_ENABLE_IF_T( format_to(std::back_insert_iterator out, const S& format_str, const Args&... args) { return vformat_to(out, to_string_view(format_str), - {internal::checked_args(format_str, args...)}); + {internal::make_args_checked(format_str, args...)}); } template @@ -1457,9 +1451,8 @@ inline std::basic_string vformat( template inline std::basic_string format(const S& format_str, const Args&... args) { - return internal::vformat( - to_string_view(format_str), - {internal::checked_args(format_str, args...)}); + return internal::vformat(to_string_view(format_str), + {internal::make_args_checked(format_str, args...)}); } FMT_API void vprint(std::FILE* f, string_view format_str, format_args args); @@ -1480,7 +1473,7 @@ template inline FMT_ENABLE_IF_T(internal::is_string::value, void) print(std::FILE* f, const S& format_str, const Args&... args) { vprint(f, to_string_view(format_str), - internal::checked_args(format_str, args...)); + internal::make_args_checked(format_str, args...)); } FMT_API void vprint(string_view format_str, format_args args); @@ -1499,7 +1492,7 @@ template inline FMT_ENABLE_IF_T(internal::is_string::value, void) print(const S& format_str, const Args&... args) { vprint(to_string_view(format_str), - internal::checked_args(format_str, args...)); + internal::make_args_checked(format_str, args...)); } FMT_END_NAMESPACE diff --git a/include/fmt/locale.h b/include/fmt/locale.h index dabe6435..39c32eb0 100644 --- a/include/fmt/locale.h +++ b/include/fmt/locale.h @@ -45,9 +45,8 @@ template inline std::basic_string format(const std::locale& loc, const S& format_str, const Args&... args) { - return internal::vformat( - loc, to_string_view(format_str), - *internal::checked_args(format_str, args...)); + return internal::vformat(loc, to_string_view(format_str), + {internal::make_args_checked(format_str, args...)}); } template diff --git a/include/fmt/ostream.h b/include/fmt/ostream.h index d7375db5..798b05c0 100644 --- a/include/fmt/ostream.h +++ b/include/fmt/ostream.h @@ -138,8 +138,8 @@ template inline typename std::enable_if::value>::type print( std::basic_ostream& os, const S& format_str, const Args&... args) { - internal::checked_args ca(format_str, args...); - vprint(os, to_string_view(format_str), *ca); + vprint(os, to_string_view(format_str), + {internal::make_args_checked(format_str, args...)}); } FMT_END_NAMESPACE diff --git a/include/fmt/prepare.h b/include/fmt/prepare.h index f7c15f21..08275a54 100644 --- a/include/fmt/prepare.h +++ b/include/fmt/prepare.h @@ -187,7 +187,6 @@ class prepared_format { public: typedef FMT_CHAR(Format) char_type; typedef format_part format_part_t; - typedef internal::checked_args checked_args; prepared_format(Format f) : format_(std::move(f)), parts_provider_(to_string_view(format_)) {} @@ -218,7 +217,7 @@ class prepared_format { std::basic_string format(const Args&... args) const { basic_memory_buffer buffer; typedef back_insert_range> range; - this->vformat_to(range(buffer), *checked_args(format_, args...)); + this->vformat_to(range(buffer), make_args_checked(format_, args...)); return to_string(buffer); } @@ -229,7 +228,7 @@ class prepared_format { const Args&... args) const { internal::container_buffer buffer(internal::get_container(out)); typedef back_insert_range> range; - this->vformat_to(range(buffer), *checked_args(format_, args...)); + this->vformat_to(range(buffer), make_args_checked(format_, args...)); return out; } @@ -245,16 +244,18 @@ class prepared_format { inline typename buffer_context::type::iterator format_to( basic_memory_buffer& buf, const Args&... args) const { typedef back_insert_range> range; - return this->vformat_to(range(buf), *checked_args(format_, args...)); + return this->vformat_to(range(buf), make_args_checked(format_, args...)); } private: - template - typename Context::iterator vformat_to(Range out, - basic_format_args args) const { + typedef typename buffer_context::type context; + + template + typename context::iterator vformat_to(Range out, + basic_format_args args) const { const auto format_view = internal::to_string_view(format_); basic_parse_context parse_ctx(format_view); - Context ctx(out.begin(), args); + context ctx(out.begin(), args); const auto& parts = parts_provider_.parts(); for (auto part_it = parts.begin(); part_it != parts.end(); ++part_it) {