Optimize copy_str for counting_iterator

This commit is contained in:
Victor Zverovich 2020-09-22 07:38:19 -07:00
parent 2591ab91c3
commit 16410056bf
2 changed files with 12 additions and 1 deletions

View File

@ -443,13 +443,17 @@ class counting_iterator {
++count_;
return *this;
}
counting_iterator operator++(int) {
auto it = *this;
++*this;
return it;
}
friend counting_iterator operator+(counting_iterator it, difference_type n) {
it.count_ += static_cast<size_t>(n);
return it;
}
value_type operator*() const { return {}; }
};
@ -583,6 +587,12 @@ OutputIt copy_str(InputIt begin, InputIt end, OutputIt it) {
[](char c) { return static_cast<char8_type>(c); });
}
template <typename Char, typename InputIt>
inline counting_iterator copy_str(InputIt begin, InputIt end,
counting_iterator it) {
return it + (end - begin);
}
#ifndef FMT_USE_GRISU
# define FMT_USE_GRISU 1
#endif

View File

@ -147,6 +147,7 @@ TEST(IteratorTest, CountingIterator) {
auto prev = it++;
EXPECT_EQ(prev.count(), 0);
EXPECT_EQ(it.count(), 1);
EXPECT_EQ((it + 41).count(), 42);
}
TEST(IteratorTest, TruncatingIterator) {