From 2a2faecff67ed8aab92907120f839e3b75b8c08f Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sun, 14 Jul 2024 09:10:16 -0700 Subject: [PATCH] Apply minor optimization --- include/fmt/format.h | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index cb88180b1aa35..78020c194de4d 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -546,6 +546,7 @@ constexpr auto to_pointer(OutputIt, size_t) -> T* { template auto to_pointer(basic_appender it, size_t n) -> T* { buffer& buf = get_container(it); auto size = buf.size(); + buf.try_reserve(size + n); if (buf.capacity() < size + n) return nullptr; buf.try_resize(size + n); return buf.data() + size; @@ -2317,15 +2318,13 @@ FMT_CONSTEXPR auto write(OutputIt out, T value) -> OutputIt { if (negative) abs_value = ~abs_value + 1; int num_digits = count_digits(abs_value); auto size = (negative ? 1 : 0) + static_cast(num_digits); - auto it = reserve(out, size); - if (auto ptr = to_pointer(it, size)) { + if (auto ptr = to_pointer(out, size)) { if (negative) *ptr++ = static_cast('-'); format_decimal(ptr, abs_value, num_digits); return out; } - if (negative) *it++ = static_cast('-'); - it = format_decimal(it, abs_value, num_digits).end; - return base_iterator(out, it); + if (negative) *out++ = static_cast('-'); + return format_decimal(out, abs_value, num_digits).end; } // DEPRECATED!