Fixing buffer_appender's ++ slicing.

This commit is contained in:
Barry Revzin 2020-08-14 15:44:11 -05:00
parent 76e97dc4df
commit 5a480de205
2 changed files with 41 additions and 3 deletions

View File

@ -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;
}
};
// Maps an output iterator into a buffer.

View File

@ -2473,3 +2473,29 @@ TEST(FormatTest, FormatUTF8Precision) {
EXPECT_EQ(result.size(), 5);
EXPECT_EQ(from_u8str(result), from_u8str(str.substr(0, 5)));
}
struct lazy_optional {
bool engaged;
std::string value;
};
FMT_BEGIN_NAMESPACE
template <> struct formatter<lazy_optional> : formatter<std::string_view> {
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)");
}