Skip to content

Commit

Permalink
fix typo which caused the loss of the counting information when using…
Browse files Browse the repository at this point in the history
… a printf context with a truncating_iterator
  • Loading branch information
rimathia authored and vitaut committed Jun 5, 2020
1 parent 21409cf commit 95c6ac0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
5 changes: 2 additions & 3 deletions include/fmt/printf.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,7 @@ template <typename OutputIt, typename Char> class basic_printf_context;
\endrst
*/
template <typename OutputIt, typename Char>
class printf_arg_formatter
: public detail::arg_formatter_base<OutputIt, Char> {
class printf_arg_formatter : public detail::arg_formatter_base<OutputIt, Char> {
public:
using iterator = OutputIt;

Expand Down Expand Up @@ -592,7 +591,7 @@ OutputIt basic_printf_context<OutputIt, Char>::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);
}
Expand Down
20 changes: 20 additions & 0 deletions test/printf-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -606,3 +606,23 @@ TEST(PrintfTest, VSPrintfMakeWArgsExample) {
{fmt::make_wprintf_args(42, L"something")}));
#endif
}

TEST(PrintfTest, PrintfDetermineOutputSize) {
using backit = std::back_insert_iterator<std::vector<char>>;
using truncated_printf_context =
fmt::basic_printf_context<fmt::detail::truncating_iterator<backit>, char>;

auto v = std::vector<char>{};
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<backit>(it, 0), format_string,
fmt::make_format_args<truncated_printf_context>(format_arg))
.format()
.count()),
expected_size);
}

0 comments on commit 95c6ac0

Please sign in to comment.