From ba36a04811c5f73681a8e1c1e28539182e6a211b Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Mon, 22 Jul 2024 16:01:18 -0700 Subject: [PATCH] Remove counting_iterator --- include/fmt/base.h | 2 +- include/fmt/compile.h | 9 ++------- include/fmt/format.h | 46 +++++-------------------------------------- test/compile-test.cc | 8 -------- 4 files changed, 8 insertions(+), 57 deletions(-) diff --git a/include/fmt/base.h b/include/fmt/base.h index 57750ca7..19cf4916 100644 --- a/include/fmt/base.h +++ b/include/fmt/base.h @@ -1103,7 +1103,7 @@ template class counting_buffer : public buffer { } public: - counting_buffer() : buffer(grow, data_, 0, buffer_size) {} + FMT_CONSTEXPR counting_buffer() : buffer(grow, data_, 0, buffer_size) {} auto count() -> size_t { return count_ + this->size(); } }; diff --git a/include/fmt/compile.h b/include/fmt/compile.h index b33a40db..8a632f63 100644 --- a/include/fmt/compile.h +++ b/include/fmt/compile.h @@ -21,12 +21,6 @@ FMT_EXPORT class compiled_string {}; namespace detail { -template -FMT_CONSTEXPR inline auto copy(InputIt begin, InputIt end, counting_iterator it) - -> counting_iterator { - return it + (end - begin); -} - template struct is_compiled_string : std::is_base_of {}; @@ -496,7 +490,8 @@ template ::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 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(n); - return it; - } - - FMT_CONSTEXPR auto operator*() const -> value_type { return {}; } -}; - template FMT_CONSTEXPR auto write(OutputIt out, basic_string_view s, const format_specs& specs) -> OutputIt { @@ -2285,7 +2245,11 @@ FMT_CONSTEXPR auto write(OutputIt out, basic_string_view 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(basic_appender(buf), s); + size = buf.count(); + } if (specs.width != 0) { if (is_debug) diff --git a/test/compile-test.cc b/test/compile-test.cc index ff2d573b..e44eee9f 100644 --- a/test/compile-test.cc +++ b/test/compile-test.cc @@ -14,14 +14,6 @@ #include "gmock/gmock.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) { // FMT_COMPILE should fallback on runtime formatting when `if constexpr` is // not available.