Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Printf get container #1982

Merged
merged 5 commits into from
Nov 12, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions include/fmt/printf.h
Original file line number Diff line number Diff line change
Expand Up @@ -480,11 +480,13 @@ OutputIt basic_printf_context<OutputIt, Char>::format() {
}
char_type c = *it++;
if (it != end && *it == c) {
out = std::copy(start, it, out);
out = detail::write(
out, basic_string_view<Char>(start, detail::to_unsigned(it - start)));
start = ++it;
continue;
}
out = std::copy(start, it - 1, out);
out = detail::write(out, basic_string_view<Char>(
start, detail::to_unsigned(it - 1 - start)));

format_specs specs;
specs.align = align::right;
Expand Down Expand Up @@ -596,7 +598,8 @@ OutputIt basic_printf_context<OutputIt, Char>::format() {
// Format argument.
out = visit_format_arg(ArgFormatter(out, specs, *this), arg);
}
return std::copy(start, it, out);
return detail::write(
out, basic_string_view<Char>(start, detail::to_unsigned(it - start)));
}

template <typename Char>
Expand Down
22 changes: 19 additions & 3 deletions test/printf-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -619,10 +619,26 @@ TEST(PrintfTest, PrintfDetermineOutputSize) {
const auto format_arg = "Hello";
const auto expected_size = fmt::sprintf(format_string, format_arg).size();

EXPECT_EQ((truncated_printf_context(
EXPECT_EQ(expected_size,
(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);
.count()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change seems unnecessary, please revert.

}

TEST(PrintfTest, PrintfAppendToBuffer) {
using backit = std::back_insert_iterator<fmt::basic_memory_buffer<char>>;
using context = fmt::basic_printf_context<backit, char>;

const auto format_string = "%s";
const char* format_arg = "Hello";
fmt::basic_memory_buffer<char> buffer;
context(std::back_inserter(buffer), format_string,
fmt::make_format_args<context>(format_arg))
.format();

std::string result(std::begin(buffer), std::end(buffer));

EXPECT_EQ(std::string("Hello"), result);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of this test? We should avoid adding more tests that rely too much on implementation details.