Use buffering in to_string to avoid bloat

This commit is contained in:
Victor Zverovich
2022-09-21 17:11:43 -07:00
parent 4c4f99a583
commit 0b5cb18b71
3 changed files with 9 additions and 13 deletions

View File

@ -1444,9 +1444,8 @@ template <> struct formatter<detail::bigint> {
return ctx.begin(); return ctx.begin();
} }
template <typename FormatContext> auto format(const detail::bigint& n, format_context& ctx) const
auto format(const detail::bigint& n, FormatContext& ctx) const -> -> format_context::iterator {
typename FormatContext::iterator {
auto out = ctx.out(); auto out = ctx.out();
bool first = true; bool first = true;
for (auto i = n.bigits_.size(); i > 0; --i) { for (auto i = n.bigits_.size(); i > 0; --i) {

View File

@ -3426,8 +3426,8 @@ template <typename Char, typename OutputIt, typename T,
FMT_CONSTEXPR auto write(OutputIt out, const T& value) -> enable_if_t< FMT_CONSTEXPR auto write(OutputIt out, const T& value) -> enable_if_t<
std::is_class<T>::value && !is_string<T>::value && std::is_class<T>::value && !is_string<T>::value &&
!is_floating_point<T>::value && !std::is_same<T, Char>::value && !is_floating_point<T>::value && !std::is_same<T, Char>::value &&
!std::is_same<const T&, !std::is_same<T, remove_cvref_t<decltype(arg_mapper<Context>().map(
decltype(arg_mapper<Context>().map(value))>::value, value))>>::value,
OutputIt> { OutputIt> {
return write<Char>(out, arg_mapper<Context>().map(value)); return write<Char>(out, arg_mapper<Context>().map(value));
} }
@ -4106,9 +4106,9 @@ auto join(Range&& range, string_view sep)
*/ */
template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value)> template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value)>
inline auto to_string(const T& value) -> std::string { inline auto to_string(const T& value) -> std::string {
auto result = std::string(); auto buffer = memory_buffer();
detail::write<char>(std::back_inserter(result), value); detail::write<char>(appender(buffer), value);
return result; return {buffer.data(), buffer.size()};
} }
template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)> template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>

View File

@ -1970,8 +1970,7 @@ template <typename, typename OutputIt> void write(OutputIt, foo) = delete;
FMT_BEGIN_NAMESPACE FMT_BEGIN_NAMESPACE
template <> template <>
struct formatter<adl_test::fmt::detail::foo> : formatter<std::string> { struct formatter<adl_test::fmt::detail::foo> : formatter<std::string> {
template <typename FormatContext> auto format(adl_test::fmt::detail::foo, format_context& ctx)
auto format(adl_test::fmt::detail::foo, FormatContext& ctx)
-> decltype(ctx.out()) { -> decltype(ctx.out()) {
return formatter<std::string>::format("foo", ctx); return formatter<std::string>::format("foo", ctx);
} }
@ -1979,9 +1978,7 @@ struct formatter<adl_test::fmt::detail::foo> : formatter<std::string> {
FMT_END_NAMESPACE FMT_END_NAMESPACE
struct convertible_to_int { struct convertible_to_int {
operator int() const { return value; } operator int() const { return 42; }
int value = 42;
}; };
TEST(format_test, to_string) { TEST(format_test, to_string) {