From 0b5cb18b71719d78b228eafd1d29fac592fde86c Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Wed, 21 Sep 2022 17:11:43 -0700 Subject: [PATCH] Use buffering in to_string to avoid bloat --- include/fmt/format-inl.h | 5 ++--- include/fmt/format.h | 10 +++++----- test/format-test.cc | 7 ++----- 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h index 2d3a4d61..eadf0da3 100644 --- a/include/fmt/format-inl.h +++ b/include/fmt/format-inl.h @@ -1444,9 +1444,8 @@ template <> struct formatter { return ctx.begin(); } - template - auto format(const detail::bigint& n, FormatContext& ctx) const -> - typename FormatContext::iterator { + auto format(const detail::bigint& n, format_context& ctx) const + -> format_context::iterator { auto out = ctx.out(); bool first = true; for (auto i = n.bigits_.size(); i > 0; --i) { diff --git a/include/fmt/format.h b/include/fmt/format.h index 4b26f926..f1757531 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -3426,8 +3426,8 @@ template enable_if_t< std::is_class::value && !is_string::value && !is_floating_point::value && !std::is_same::value && - !std::is_same().map(value))>::value, + !std::is_same().map( + value))>>::value, OutputIt> { return write(out, arg_mapper().map(value)); } @@ -4106,9 +4106,9 @@ auto join(Range&& range, string_view sep) */ template ::value)> inline auto to_string(const T& value) -> std::string { - auto result = std::string(); - detail::write(std::back_inserter(result), value); - return result; + auto buffer = memory_buffer(); + detail::write(appender(buffer), value); + return {buffer.data(), buffer.size()}; } template ::value)> diff --git a/test/format-test.cc b/test/format-test.cc index 27a21303..4db3c9c4 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1970,8 +1970,7 @@ template void write(OutputIt, foo) = delete; FMT_BEGIN_NAMESPACE template <> struct formatter : formatter { - template - auto format(adl_test::fmt::detail::foo, FormatContext& ctx) + auto format(adl_test::fmt::detail::foo, format_context& ctx) -> decltype(ctx.out()) { return formatter::format("foo", ctx); } @@ -1979,9 +1978,7 @@ struct formatter : formatter { FMT_END_NAMESPACE struct convertible_to_int { - operator int() const { return value; } - - int value = 42; + operator int() const { return 42; } }; TEST(format_test, to_string) {