From f1cb7204d972b1b6931bc40018e5e9d3e365f54f Mon Sep 17 00:00:00 2001 From: Daniela Engert Date: Sun, 7 Oct 2018 14:38:29 +0200 Subject: [PATCH] Parameterize printf functions on the type of the format string. Signed-off-by: Daniela Engert --- include/fmt/printf.h | 136 ++++++++++++++++++------------------------- 1 file changed, 57 insertions(+), 79 deletions(-) diff --git a/include/fmt/printf.h b/include/fmt/printf.h index 27910f3bb33c2..d838252198da5 100644 --- a/include/fmt/printf.h +++ b/include/fmt/printf.h @@ -577,17 +577,12 @@ struct printf_context { std::back_insert_iterator, typename Buffer::value_type> type; }; -template -inline format_arg_store::type, Args...> - make_printf_args(const Args & ... args) { - return format_arg_store::type, Args...>( - args...); -} -typedef basic_format_args::type> printf_args; -typedef basic_format_args::type> wprintf_args; - -inline std::string vsprintf(string_view format, printf_args args) { - memory_buffer buffer; +template +inline std::basic_string +vsprintf(basic_string_view format, + basic_format_args>::type> args) { + basic_memory_buffer buffer; printf(buffer, format, args); return to_string(buffer); } @@ -601,22 +596,16 @@ inline std::string vsprintf(string_view format, printf_args args) { std::string message = fmt::sprintf("The answer is %d", 42); \endrst */ -template -inline std::string sprintf(string_view format_str, const Args & ... args) { - return vsprintf(format_str, - make_format_args::type>(args...)); -} - -inline std::wstring vsprintf(wstring_view format, wprintf_args args) { - wmemory_buffer buffer; - printf(buffer, format, args); - return to_string(buffer); -} - -template -inline std::wstring sprintf(wstring_view format_str, const Args & ... args) { - return vsprintf(format_str, - make_format_args::type>(args...)); +template +inline typename std::enable_if < + internal::is_format_string::value, std::basic_string>::type + sprintf(const S &format_str, const Args & ... args) { + internal::check_format_string(format_str); + typedef internal::basic_buffer buffer; + typedef typename printf_context::type context; + format_arg_store as{ args... }; + return vsprintf(internal::to_string_view(format_str), + basic_format_args(as)); } template @@ -639,25 +628,22 @@ inline int vfprintf(std::FILE *f, basic_string_view format, fmt::fprintf(stderr, "Don't %s!", "panic"); \endrst */ -template -inline int fprintf(std::FILE *f, string_view format_str, const Args & ... args) { - auto vargs = make_format_args< - typename printf_context::type>(args...); - return vfprintf(f, format_str, vargs); -} - -template -inline int fprintf(std::FILE *f, wstring_view format_str, - const Args & ... args) { - return vfprintf(f, format_str, - make_format_args::type>(args...)); -} - -inline int vprintf(string_view format, printf_args args) { - return vfprintf(stdout, format, args); +template +inline typename std::enable_if < + internal::is_format_string::value, int>::type + fprintf(std::FILE *f, const S &format_str, const Args & ... args) { + internal::check_format_string(format_str); + typedef internal::basic_buffer buffer; + typedef typename printf_context::type context; + format_arg_store as{ args... }; + return vfprintf(f, internal::to_string_view(format_str), + basic_format_args(as)); } -inline int vprintf(wstring_view format, wprintf_args args) { +template +inline int vprintf(basic_string_view format, + basic_format_args>::type> args) { return vfprintf(stdout, format, args); } @@ -670,29 +656,24 @@ inline int vprintf(wstring_view format, wprintf_args args) { fmt::printf("Elapsed time: %.2f seconds", 1.23); \endrst */ -template -inline int printf(string_view format_str, const Args & ... args) { - return vprintf(format_str, - make_format_args::type>(args...)); -} - -template -inline int printf(wstring_view format_str, const Args & ... args) { - return vprintf(format_str, - make_format_args::type>(args...)); -} - -inline int vfprintf(std::ostream &os, string_view format_str, - printf_args args) { - memory_buffer buffer; - printf(buffer, format_str, args); - internal::write(os, buffer); - return static_cast(buffer.size()); +template +inline typename std::enable_if < + internal::is_format_string::value, int>::type + printf(const S &format_str, const Args & ... args) { + internal::check_format_string(format_str); + typedef internal::basic_buffer buffer; + typedef typename printf_context::type context; + format_arg_store as{ args... }; + return vprintf(internal::to_string_view(format_str), + basic_format_args(as)); } -inline int vfprintf(std::wostream &os, wstring_view format_str, - wprintf_args args) { - wmemory_buffer buffer; +template +inline int vfprintf(std::basic_ostream &os, + basic_string_view format_str, + basic_format_args>::type> args) { + basic_memory_buffer buffer; printf(buffer, format_str, args); internal::write(os, buffer); return static_cast(buffer.size()); @@ -707,20 +688,17 @@ inline int vfprintf(std::wostream &os, wstring_view format_str, fmt::fprintf(cerr, "Don't %s!", "panic"); \endrst */ -template -inline int fprintf(std::ostream &os, string_view format_str, - const Args & ... args) { - auto vargs = make_format_args< - typename printf_context::type>(args...); - return vfprintf(os, format_str, vargs); -} - -template -inline int fprintf(std::wostream &os, wstring_view format_str, - const Args & ... args) { - auto vargs = make_format_args< - typename printf_context::type>(args...); - return vfprintf(os, format_str, vargs); +template +inline typename std::enable_if < + internal::is_format_string::value, int>::type + fprintf(std::basic_ostream &os, + const S &format_str, const Args & ... args) { + internal::check_format_string(format_str); + typedef internal::basic_buffer buffer; + typedef typename printf_context::type context; + format_arg_store as{ args... }; + return vfprintf(os, internal::to_string_view(format_str), + basic_format_args(as)); } FMT_END_NAMESPACE