Remove counting_iterator

This commit is contained in:
Victor Zverovich
2024-07-22 16:01:18 -07:00
parent f6b4a23b83
commit ba36a04811
4 changed files with 8 additions and 57 deletions

View File

@ -1103,7 +1103,7 @@ template <typename T = char> class counting_buffer : public buffer<T> {
} }
public: public:
counting_buffer() : buffer<T>(grow, data_, 0, buffer_size) {} FMT_CONSTEXPR counting_buffer() : buffer<T>(grow, data_, 0, buffer_size) {}
auto count() -> size_t { return count_ + this->size(); } auto count() -> size_t { return count_ + this->size(); }
}; };

View File

@ -21,12 +21,6 @@ FMT_EXPORT class compiled_string {};
namespace detail { namespace detail {
template <typename T, typename InputIt>
FMT_CONSTEXPR inline auto copy(InputIt begin, InputIt end, counting_iterator it)
-> counting_iterator {
return it + (end - begin);
}
template <typename S> template <typename S>
struct is_compiled_string : std::is_base_of<compiled_string, S> {}; struct is_compiled_string : std::is_base_of<compiled_string, S> {};
@ -496,7 +490,8 @@ template <typename S, typename... Args,
FMT_ENABLE_IF(detail::is_compiled_string<S>::value)> FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
auto formatted_size(const S& fmt, const Args&... args) -> size_t { auto formatted_size(const S& fmt, const Args&... args) -> size_t {
auto buf = detail::counting_buffer<>(); auto buf = detail::counting_buffer<>();
return fmt::format_to(fmt::appender(buf), fmt, args...).count(); fmt::format_to(appender(buf), fmt, args...);
return buf.count();
} }
template <typename S, typename... Args, template <typename S, typename... Args,

View File

@ -2235,46 +2235,6 @@ FMT_CONSTEXPR FMT_INLINE auto write(OutputIt out, T value,
loc); loc);
} }
// An output iterator that counts the number of objects written to it and
// discards them.
class counting_iterator {
private:
size_t count_;
public:
using iterator_category = std::output_iterator_tag;
using difference_type = std::ptrdiff_t;
using pointer = void;
using reference = void;
FMT_UNCHECKED_ITERATOR(counting_iterator);
struct value_type {
template <typename T> FMT_CONSTEXPR void operator=(const T&) {}
};
FMT_CONSTEXPR counting_iterator() : count_(0) {}
FMT_CONSTEXPR auto count() const -> size_t { return count_; }
FMT_CONSTEXPR auto operator++() -> counting_iterator& {
++count_;
return *this;
}
FMT_CONSTEXPR auto operator++(int) -> counting_iterator {
auto it = *this;
++*this;
return it;
}
FMT_CONSTEXPR friend auto operator+(counting_iterator it, difference_type n)
-> counting_iterator {
it.count_ += static_cast<size_t>(n);
return it;
}
FMT_CONSTEXPR auto operator*() const -> value_type { return {}; }
};
template <typename Char, typename OutputIt> template <typename Char, typename OutputIt>
FMT_CONSTEXPR auto write(OutputIt out, basic_string_view<Char> s, FMT_CONSTEXPR auto write(OutputIt out, basic_string_view<Char> s,
const format_specs& specs) -> OutputIt { const format_specs& specs) -> OutputIt {
@ -2285,7 +2245,11 @@ FMT_CONSTEXPR auto write(OutputIt out, basic_string_view<Char> s,
bool is_debug = specs.type == presentation_type::debug; bool is_debug = specs.type == presentation_type::debug;
size_t width = 0; size_t width = 0;
if (is_debug) size = write_escaped_string(counting_iterator{}, s).count(); if (is_debug) {
auto buf = counting_buffer<Char>();
write_escaped_string(basic_appender<Char>(buf), s);
size = buf.count();
}
if (specs.width != 0) { if (specs.width != 0) {
if (is_debug) if (is_debug)

View File

@ -14,14 +14,6 @@
#include "gmock/gmock.h" #include "gmock/gmock.h"
#include "gtest-extra.h" #include "gtest-extra.h"
TEST(iterator_test, counting_iterator) {
auto it = fmt::detail::counting_iterator();
auto prev = it++;
EXPECT_EQ(prev.count(), 0);
EXPECT_EQ(it.count(), 1);
EXPECT_EQ((it + 41).count(), 42);
}
TEST(compile_test, compile_fallback) { TEST(compile_test, compile_fallback) {
// FMT_COMPILE should fallback on runtime formatting when `if constexpr` is // FMT_COMPILE should fallback on runtime formatting when `if constexpr` is
// not available. // not available.