From 1daae555b342157fd9e2cda37fef371e8b233212 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Wed, 14 Jun 2023 21:46:15 +0300 Subject: [PATCH] Optimize format string compilation --- include/fmt/compile.h | 7 ++++++- include/fmt/core.h | 7 +++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/include/fmt/compile.h b/include/fmt/compile.h index 46cbc8fc319f..57f3a961445b 100644 --- a/include/fmt/compile.h +++ b/include/fmt/compile.h @@ -221,7 +221,12 @@ template struct field { template constexpr OutputIt format(OutputIt out, const Args&... args) const { - return write(out, get_arg_checked(args...)); + const T& arg = get_arg_checked(args...); + if constexpr (std::is_convertible_v>) { + auto s = basic_string_view(arg); + return copy_str(s.begin(), s.end(), out); + } + return write(out, arg); } }; diff --git a/include/fmt/core.h b/include/fmt/core.h index 4961310a311f..966033fe02b0 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -1486,6 +1486,13 @@ auto copy_str(InputIt begin, InputIt end, appender out) -> appender { get_container(out).append(begin, end); return out; } +template +auto copy_str(InputIt begin, InputIt end, + std::back_insert_iterator out) + -> std::back_insert_iterator { + get_container(out).append(begin, end); + return out; +} template FMT_CONSTEXPR auto copy_str(R&& rng, OutputIt out) -> OutputIt {