mirror of
https://github.com/fmtlib/fmt.git
synced 2025-02-28 16:11:28 +00:00
Fixing buffer_appender's ++ slicing (#1822)
* Fixing buffer_appender's ++ slicing. * This test requires C++14. * Removing string_view dependency. * Simplifying test case. * Adding message to static_assert
This commit is contained in:
parent
951e0d2333
commit
6be6544668
@ -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.
|
// It is used to reduce symbol sizes for the common case.
|
||||||
template <typename T>
|
template <typename T>
|
||||||
class buffer_appender : public std::back_insert_iterator<buffer<T>> {
|
class buffer_appender : public std::back_insert_iterator<buffer<T>> {
|
||||||
|
using base = std::back_insert_iterator<buffer<T>>;
|
||||||
public:
|
public:
|
||||||
explicit buffer_appender(buffer<T>& buf)
|
explicit buffer_appender(buffer<T>& buf)
|
||||||
: std::back_insert_iterator<buffer<T>>(buf) {}
|
: base(buf) {}
|
||||||
buffer_appender(std::back_insert_iterator<buffer<T>> it)
|
buffer_appender(base it)
|
||||||
: std::back_insert_iterator<buffer<T>>(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.
|
// Maps an output iterator into a buffer.
|
||||||
|
@ -2477,3 +2477,27 @@ TEST(FormatTest, FormatUTF8Precision) {
|
|||||||
EXPECT_EQ(result.size(), 5);
|
EXPECT_EQ(result.size(), 5);
|
||||||
EXPECT_EQ(from_u8str(result), from_u8str(str.substr(0, 5)));
|
EXPECT_EQ(from_u8str(result), from_u8str(str.substr(0, 5)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct check_back_appender {};
|
||||||
|
|
||||||
|
FMT_BEGIN_NAMESPACE
|
||||||
|
template <> struct formatter<check_back_appender> {
|
||||||
|
template <typename ParseContext>
|
||||||
|
auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
|
||||||
|
return ctx.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Context>
|
||||||
|
auto format(check_back_appender, Context& ctx) -> decltype(ctx.out()) {
|
||||||
|
auto out = ctx.out();
|
||||||
|
static_assert(std::is_same<decltype(++out), decltype(out)&>::value,
|
||||||
|
"needs to satisfy weakly_incrementable");
|
||||||
|
*out = 'y';
|
||||||
|
return ++out;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
FMT_END_NAMESPACE
|
||||||
|
|
||||||
|
TEST(FormatTest, BackInsertSlicing) {
|
||||||
|
EXPECT_EQ(fmt::format("{}", check_back_appender{}), "y");
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user