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

Fixing buffer_appender's ++ slicing. #1822

Merged
merged 5 commits into from
Aug 18, 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
18 changes: 15 additions & 3 deletions include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -845,11 +845,23 @@ template <typename T = char> class counting_buffer : public buffer<T> {
// It is used to reduce symbol sizes for the common case.
template <typename T>
class buffer_appender : public std::back_insert_iterator<buffer<T>> {
using base = std::back_insert_iterator<buffer<T>>;
public:
explicit buffer_appender(buffer<T>& buf)
: std::back_insert_iterator<buffer<T>>(buf) {}
buffer_appender(std::back_insert_iterator<buffer<T>> it)
: std::back_insert_iterator<buffer<T>>(it) {}
: base(buf) {}
buffer_appender(base it)
: base(it) {}

buffer_appender& operator++() {
base::operator++();
return *this;
}

buffer_appender operator++(int) {
buffer_appender tmp = *this;
++*this;
return tmp;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Given that back_insert_iterator only has like... 4 member functions, and I've already implemented 2 of them (and over-implemented them too, these could both just be return *this;), maybe we should just implement the whole thing in here without std?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's fine as is.

};

// Maps an output iterator into a buffer.
Expand Down
34 changes: 34 additions & 0 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2473,3 +2473,37 @@ TEST(FormatTest, FormatUTF8Precision) {
EXPECT_EQ(result.size(), 5);
EXPECT_EQ(from_u8str(result), from_u8str(str.substr(0, 5)));
}

#ifdef __cpp_decltype_auto
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we make the test work without this, e.g. checking if the types are the same manually?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Just have a static_assert.

struct lazy_optional {
bool engaged;
std::string value;
};
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd minimize this even further into an empty struct and make the name reflect what we are trying to test here.


FMT_BEGIN_NAMESPACE
template <> struct formatter<lazy_optional> {
template <typename ParseContext>
auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
return ctx.begin();
}

template <typename Context>
auto format(const lazy_optional& opt, Context& ctx) {
if (opt.engaged) {
auto out = format_to(ctx.out(), "Some(");
out = format_to(out, "{}", opt.value);
*out = ')';
return ++out;
} else {
return format_to(ctx.out(), "None()");
}
}
};
FMT_END_NAMESPACE

TEST(FormatTest, BackInsertSlicing) {
EXPECT_EQ(fmt::format("{}", lazy_optional{false, ""}), "None()");
EXPECT_EQ(fmt::format("{}", lazy_optional{true, "X"}), "Some(X)");
}
#endif