From 0a96c032b9ef24eef9335ab0cf30852cfdb98d0b Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Thu, 25 Oct 2018 07:20:02 -0700 Subject: [PATCH] Parameterize v*printf on string type (#920) --- include/fmt/core.h | 2 +- include/fmt/format-inl.h | 6 +++--- include/fmt/format.h | 8 ++++---- include/fmt/ostream.h | 2 +- include/fmt/printf.h | 36 ++++++++++++++++++------------------ test/printf-test.cc | 10 ++++++++++ 6 files changed, 37 insertions(+), 27 deletions(-) diff --git a/include/fmt/core.h b/include/fmt/core.h index ecd145ee..02c89c29 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -1398,7 +1398,7 @@ typename std::enable_if< const S &format_str, basic_format_args::type> args) { internal::container_buffer buf(internal::get_container(out)); - vformat_to(buf, to_string_view(format_str), args); + internal::vformat_to(buf, to_string_view(format_str), args); return out; } diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h index 731ad1ad..bb8858b4 100644 --- a/include/fmt/format-inl.h +++ b/include/fmt/format-inl.h @@ -943,14 +943,14 @@ FMT_FUNC void report_windows_error( FMT_FUNC void vprint(std::FILE *f, string_view format_str, format_args args) { memory_buffer buffer; - vformat_to(buffer, format_str, - basic_format_args::type>(args)); + internal::vformat_to(buffer, format_str, + basic_format_args::type>(args)); std::fwrite(buffer.data(), 1, buffer.size(), f); } FMT_FUNC void vprint(std::FILE *f, wstring_view format_str, wformat_args args) { wmemory_buffer buffer; - vformat_to(buffer, format_str, args); + internal::vformat_to(buffer, format_str, args); std::fwrite(buffer.data(), sizeof(wchar_t), buffer.size(), f); } diff --git a/include/fmt/format.h b/include/fmt/format.h index f3302597..76ceee4b 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -3378,7 +3378,7 @@ template inline typename buffer_context::type::iterator vformat_to( internal::basic_buffer &buf, const S &format_str, basic_format_args::type> args) { - return vformat_to(buf, to_string_view(format_str), args); + return internal::vformat_to(buf, to_string_view(format_str), args); } template < @@ -3391,8 +3391,8 @@ inline typename buffer_context::type::iterator format_to( internal::check_format_string(format_str); typedef typename buffer_context::type context; format_arg_store as{args...}; - return vformat_to(buf, to_string_view(format_str), - basic_format_args(as)); + return internal::vformat_to(buf, to_string_view(format_str), + basic_format_args(as)); } template @@ -3495,7 +3495,7 @@ inline std::basic_string internal::vformat( basic_string_view format_str, basic_format_args::type> args) { basic_memory_buffer buffer; - vformat_to(buffer, format_str, args); + internal::vformat_to(buffer, format_str, args); return fmt::to_string(buffer); } diff --git a/include/fmt/ostream.h b/include/fmt/ostream.h index 85430779..fa26f6d2 100644 --- a/include/fmt/ostream.h +++ b/include/fmt/ostream.h @@ -129,7 +129,7 @@ inline void vprint(std::basic_ostream &os, basic_string_view format_str, basic_format_args::type> args) { basic_memory_buffer buffer; - vformat_to(buffer, format_str, args); + internal::vformat_to(buffer, format_str, args); internal::write(os, buffer); } /** diff --git a/include/fmt/printf.h b/include/fmt/printf.h index e12a4f6b..b50c8faf 100644 --- a/include/fmt/printf.h +++ b/include/fmt/printf.h @@ -580,13 +580,13 @@ struct printf_context { typedef basic_format_args::type> printf_args; typedef basic_format_args::type> wprintf_args; -template +template inline std::basic_string -vsprintf(basic_string_view format, +vsprintf(const S &format, basic_format_args>::type> args) { basic_memory_buffer buffer; - printf(buffer, format, args); + printf(buffer, to_string_view(format), args); return to_string(buffer); } @@ -601,21 +601,21 @@ vsprintf(basic_string_view format, */ template inline FMT_ENABLE_IF_STRING(S, std::basic_string) - sprintf(const S &format_str, const Args & ... args) { - internal::check_format_string(format_str); + sprintf(const S &format, const Args & ... args) { + internal::check_format_string(format); typedef internal::basic_buffer buffer; typedef typename printf_context::type context; format_arg_store as{ args... }; - return vsprintf(to_string_view(format_str), + return vsprintf(to_string_view(format), basic_format_args(as)); } -template -inline int vfprintf(std::FILE *f, basic_string_view format, +template +inline int vfprintf(std::FILE *f, const S &format, basic_format_args>::type> args) { basic_memory_buffer buffer; - printf(buffer, format, args); + printf(buffer, to_string_view(format), args); std::size_t size = buffer.size(); return std::fwrite( buffer.data(), sizeof(Char), size, f) < size ? -1 : static_cast(size); @@ -632,20 +632,20 @@ inline int vfprintf(std::FILE *f, basic_string_view format, */ template inline FMT_ENABLE_IF_STRING(S, int) - fprintf(std::FILE *f, const S &format_str, const Args & ... args) { - internal::check_format_string(format_str); + fprintf(std::FILE *f, const S &format, const Args & ... args) { + internal::check_format_string(format); typedef internal::basic_buffer buffer; typedef typename printf_context::type context; format_arg_store as{ args... }; - return vfprintf(f, to_string_view(format_str), + return vfprintf(f, to_string_view(format), basic_format_args(as)); } -template -inline int vprintf(basic_string_view format, +template +inline int vprintf(const S &format, basic_format_args>::type> args) { - return vfprintf(stdout, format, args); + return vfprintf(stdout, to_string_view(format), args); } /** @@ -668,13 +668,13 @@ inline FMT_ENABLE_IF_STRING(S, int) basic_format_args(as)); } -template +template inline int vfprintf(std::basic_ostream &os, - basic_string_view format_str, + const S &format, basic_format_args>::type> args) { basic_memory_buffer buffer; - printf(buffer, format_str, args); + printf(buffer, to_string_view(format), args); internal::write(os, buffer); return static_cast(buffer.size()); } diff --git a/test/printf-test.cc b/test/printf-test.cc index 22b28aca..81d949dd 100644 --- a/test/printf-test.cc +++ b/test/printf-test.cc @@ -499,3 +499,13 @@ TEST(PrintfTest, OStream) { EXPECT_EQ("Don't panic!", os.str()); EXPECT_EQ(12, ret); } + +TEST(PrintfTest, VPrintf) { + typedef fmt::printf_context::type context; + fmt::format_arg_store as{42}; + fmt::basic_format_args args(as); + EXPECT_EQ(fmt::vsprintf("%d", args), "42"); + EXPECT_WRITE(stdout, fmt::vprintf("%d", args), "42"); + EXPECT_WRITE(stdout, fmt::vfprintf(stdout, "%d", args), "42"); + EXPECT_WRITE(stdout, fmt::vfprintf(std::cout, "%d", args), "42"); +}