Skip to content

Commit

Permalink
Remove counting_iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Jul 22, 2024
1 parent f6b4a23 commit 2b40c8b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 49 deletions.
2 changes: 1 addition & 1 deletion include/fmt/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -1103,7 +1103,7 @@ template <typename T = char> class counting_buffer : public buffer<T> {
}

public:
counting_buffer() : buffer<T>(grow, data_, 0, buffer_size) {}
FMT_CONSTEXPR20 counting_buffer() : buffer<T>(grow, data_, 0, buffer_size) {}

auto count() -> size_t { return count_ + this->size(); }
};
Expand Down
9 changes: 2 additions & 7 deletions include/fmt/compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ FMT_EXPORT class compiled_string {};

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>
struct is_compiled_string : std::is_base_of<compiled_string, S> {};

Expand Down Expand Up @@ -496,7 +490,8 @@ template <typename S, typename... Args,
FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
auto formatted_size(const S& fmt, const Args&... args) -> size_t {
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,
Expand Down
46 changes: 5 additions & 41 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -2235,46 +2235,6 @@ FMT_CONSTEXPR FMT_INLINE auto write(OutputIt out, T value,
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>
FMT_CONSTEXPR auto write(OutputIt out, basic_string_view<Char> s,
const format_specs& specs) -> OutputIt {
Expand All @@ -2285,7 +2245,11 @@ FMT_CONSTEXPR auto write(OutputIt out, basic_string_view<Char> s,
bool is_debug = specs.type == presentation_type::debug;
size_t width = 0;

if (is_debug) size = write_escaped_string(counting_iterator{}, s).count();
if (is_debug) {
auto buf = counting_buffer<>();
write_escaped_string(appender(buf), s);
size = buf.count();
}

if (specs.width != 0) {
if (is_debug)
Expand Down

0 comments on commit 2b40c8b

Please sign in to comment.