mirror of
https://github.com/fmtlib/fmt.git
synced 2025-01-27 06:35:37 +00:00
Make truncating_iterator an output_iterator (#2158)
This commit is contained in:
parent
772aeca338
commit
e718ec3e93
@ -469,15 +469,17 @@ template <typename OutputIt> class truncating_iterator_base {
|
|||||||
protected:
|
protected:
|
||||||
OutputIt out_;
|
OutputIt out_;
|
||||||
size_t limit_;
|
size_t limit_;
|
||||||
size_t count_;
|
size_t count_ = 0;
|
||||||
|
|
||||||
|
truncating_iterator_base() : out_(), limit_(0) {}
|
||||||
|
|
||||||
truncating_iterator_base(OutputIt out, size_t limit)
|
truncating_iterator_base(OutputIt out, size_t limit)
|
||||||
: out_(out), limit_(limit), count_(0) {}
|
: out_(out), limit_(limit) {}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
using iterator_category = std::output_iterator_tag;
|
using iterator_category = std::output_iterator_tag;
|
||||||
using value_type = typename std::iterator_traits<OutputIt>::value_type;
|
using value_type = typename std::iterator_traits<OutputIt>::value_type;
|
||||||
using difference_type = void;
|
using difference_type = std::ptrdiff_t;
|
||||||
using pointer = void;
|
using pointer = void;
|
||||||
using reference = void;
|
using reference = void;
|
||||||
using _Unchecked_type =
|
using _Unchecked_type =
|
||||||
@ -502,6 +504,8 @@ class truncating_iterator<OutputIt, std::false_type>
|
|||||||
public:
|
public:
|
||||||
using value_type = typename truncating_iterator_base<OutputIt>::value_type;
|
using value_type = typename truncating_iterator_base<OutputIt>::value_type;
|
||||||
|
|
||||||
|
truncating_iterator() = default;
|
||||||
|
|
||||||
truncating_iterator(OutputIt out, size_t limit)
|
truncating_iterator(OutputIt out, size_t limit)
|
||||||
: truncating_iterator_base<OutputIt>(out, limit) {}
|
: truncating_iterator_base<OutputIt>(out, limit) {}
|
||||||
|
|
||||||
@ -525,6 +529,8 @@ template <typename OutputIt>
|
|||||||
class truncating_iterator<OutputIt, std::true_type>
|
class truncating_iterator<OutputIt, std::true_type>
|
||||||
: public truncating_iterator_base<OutputIt> {
|
: public truncating_iterator_base<OutputIt> {
|
||||||
public:
|
public:
|
||||||
|
truncating_iterator() = default;
|
||||||
|
|
||||||
truncating_iterator(OutputIt out, size_t limit)
|
truncating_iterator(OutputIt out, size_t limit)
|
||||||
: truncating_iterator_base<OutputIt>(out, limit) {}
|
: truncating_iterator_base<OutputIt>(out, limit) {}
|
||||||
|
|
||||||
|
@ -12,9 +12,11 @@
|
|||||||
#include <climits>
|
#include <climits>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
#include <iterator>
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
// Check if fmt/format.h compiles with windows.h included before it.
|
// Check if fmt/format.h compiles with windows.h included before it.
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
@ -157,6 +159,24 @@ TEST(IteratorTest, TruncatingIterator) {
|
|||||||
EXPECT_EQ(it.base(), p + 1);
|
EXPECT_EQ(it.base(), p + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
TEST(IteratorTest, TruncatingIteratorDefaultConstruct) {
|
||||||
|
static_assert(
|
||||||
|
std::is_default_constructible<fmt::detail::truncating_iterator<char*>>::value,
|
||||||
|
"");
|
||||||
|
|
||||||
|
fmt::detail::truncating_iterator<char*> it;
|
||||||
|
EXPECT_EQ(nullptr, it.base());
|
||||||
|
EXPECT_EQ(std::size_t{0}, it.count());
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef __cpp_lib_ranges
|
||||||
|
TEST(IteratorTest, TruncatingIteratorOutputIterator) {
|
||||||
|
static_assert(std::output_iterator<fmt::detail::truncating_iterator<char*>,
|
||||||
|
char>);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
TEST(IteratorTest, TruncatingBackInserter) {
|
TEST(IteratorTest, TruncatingBackInserter) {
|
||||||
std::string buffer;
|
std::string buffer;
|
||||||
auto bi = std::back_inserter(buffer);
|
auto bi = std::back_inserter(buffer);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user