From aab4a1bfa2c3850a3bd83d68976d6d4b6a106648 Mon Sep 17 00:00:00 2001 From: sunmy <7743179+sun_mengyue@user.noreply.gitee.com> Date: Wed, 9 Jun 2021 21:49:07 +0800 Subject: [PATCH] using static constexpr char array as default format --- include/fmt/chrono.h | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index 163f2e4e..cf3bd08c 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -451,24 +451,16 @@ FMT_END_DETAIL_NAMESPACE template struct formatter, Char> : formatter { - FMT_CONSTEXPR FMT_INLINE void generate_default_spec(char) { - this->specs = {"%Y-%m-%d %H:%M:%S", 17}; - } - FMT_CONSTEXPR FMT_INLINE void generate_default_spec(wchar_t) { - this->specs = {L"%Y-%m-%d %H:%M:%S", 17}; - } - template FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { auto it = ctx.begin(); if (it != ctx.end() && *it == ':') ++it; auto end = it; while (end != ctx.end() && *end != '}') ++end; - if (end == it) { - generate_default_spec(typename ParseContext::char_type{}); - } else { + if (end == it) + this->specs = {default_spec, 17}; + else this->specs = {it, detail::to_unsigned(end - it)}; - } return end; } @@ -478,6 +470,10 @@ struct formatter, std::tm time = localtime(val); return formatter::format(time, ctx); } + + static constexpr Char default_spec[] = {'%', 'Y', '-', '%', 'm', '-', + '%', 'd', ' ', '%', 'H', ':', + '%', 'M', ':', '%', 'S'}; }; template struct formatter {