From 95c6ac0cc8806c511d74ab8c4801188080ee7958 Mon Sep 17 00:00:00 2001 From: rimathia Date: Fri, 5 Jun 2020 15:09:14 +0200 Subject: [PATCH] fix typo which caused the loss of the counting information when using a printf context with a truncating_iterator --- include/fmt/printf.h | 5 ++--- test/printf-test.cc | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/include/fmt/printf.h b/include/fmt/printf.h index 3a16890f..1fbc9953 100644 --- a/include/fmt/printf.h +++ b/include/fmt/printf.h @@ -206,8 +206,7 @@ template class basic_printf_context; \endrst */ template -class printf_arg_formatter - : public detail::arg_formatter_base { +class printf_arg_formatter : public detail::arg_formatter_base { public: using iterator = OutputIt; @@ -592,7 +591,7 @@ OutputIt basic_printf_context::format() { start = it; // Format argument. - visit_format_arg(ArgFormatter(out, specs, *this), arg); + out = visit_format_arg(ArgFormatter(out, specs, *this), arg); } return std::copy(start, it, out); } diff --git a/test/printf-test.cc b/test/printf-test.cc index 1c1173c4..ccd72dcd 100644 --- a/test/printf-test.cc +++ b/test/printf-test.cc @@ -606,3 +606,23 @@ TEST(PrintfTest, VSPrintfMakeWArgsExample) { {fmt::make_wprintf_args(42, L"something")})); #endif } + +TEST(PrintfTest, PrintfDetermineOutputSize) { + using backit = std::back_insert_iterator>; + using truncated_printf_context = + fmt::basic_printf_context, char>; + + auto v = std::vector{}; + auto it = std::back_inserter(v); + + const auto format_string = "%s"; + const auto format_arg = "Hello"; + const auto expected_size = fmt::sprintf(format_string, format_arg).size(); + + EXPECT_EQ((truncated_printf_context( + fmt::detail::truncating_iterator(it, 0), format_string, + fmt::make_format_args(format_arg)) + .format() + .count()), + expected_size); +}