From 9501da5796d350e23f0ef6f217930cb35e67b650 Mon Sep 17 00:00:00 2001 From: Walter Gray Date: Tue, 10 Nov 2020 15:59:54 -0800 Subject: [PATCH 01/13] Always use FMT_STRING internally where possible --- include/fmt/chrono.h | 21 +++++++++++---------- include/fmt/color.h | 2 +- include/fmt/format-inl.h | 15 ++++++++------- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index 1ea9ad627f62..a5e1ac92e49c 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -764,13 +764,15 @@ inline std::chrono::duration get_milliseconds( template OutputIt format_duration_value(OutputIt out, Rep val, int precision) { - const Char pr_f[] = {'{', ':', '.', '{', '}', 'f', '}', 0}; - if (precision >= 0) return format_to(out, pr_f, val, precision); - const Char fp_f[] = {'{', ':', 'g', '}', 0}; - const Char format[] = {'{', '}', 0}; - return format_to(out, std::is_floating_point::value ? fp_f : format, - val); + if (precision >= 0) + return format_to(out, FMT_STRING("{:.{}f}"), val, precision); + if (std::is_floating_point::value) { + return format_to(out, FMT_STRING("{:g}"), val); + } else { + return format_to(out, FMT_STRING("{}"), val); + } } + template OutputIt copy_unit(string_view unit, OutputIt out, Char) { return std::copy(unit.begin(), unit.end(), out); @@ -788,10 +790,9 @@ template OutputIt format_duration_unit(OutputIt out) { if (const char* unit = get_units()) return copy_unit(string_view(unit), out, Char()); - const Char num_f[] = {'[', '{', '}', ']', 's', 0}; - if (const_check(Period::den == 1)) return format_to(out, num_f, Period::num); - const Char num_def_f[] = {'[', '{', '}', '/', '{', '}', ']', 's', 0}; - return format_to(out, num_def_f, Period::num, Period::den); + if (const_check(Period::den == 1)) + return format_to(out, FMT_STRING("[{}]s"), Period::num); + return format_to(out, FMT_STRING("[{}/{}]s"), Period::num, Period::den); } template vformat( */ template > inline std::basic_string format(const text_style& ts, const S& format_str, - const Args&... args) { + Args&&... args) { return vformat(ts, to_string_view(format_str), fmt::make_args_checked(format_str, args...)); } diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h index 9046de91f07c..d1f5f395dcb8 100644 --- a/include/fmt/format-inl.h +++ b/include/fmt/format-inl.h @@ -145,8 +145,8 @@ FMT_FUNC void format_error_code(detail::buffer& out, int error_code, error_code_size += detail::to_unsigned(detail::count_digits(abs_value)); auto it = buffer_appender(out); if (message.size() <= inline_buffer_size - error_code_size) - format_to(it, "{}{}", message, SEP); - format_to(it, "{}{}", ERROR_STR, error_code); + format_to(it, FMT_STRING("{}{}"), message, SEP); + format_to(it, FMT_STRING("{}{}"), ERROR_STR, error_code); assert(out.size() <= inline_buffer_size); } @@ -2662,14 +2662,15 @@ template <> struct formatter { for (auto i = n.bigits_.size(); i > 0; --i) { auto value = n.bigits_[i - 1u]; if (first) { - out = format_to(out, "{:x}", value); + out = format_to(out, FMT_STRING("{:x}"), value); first = false; continue; } - out = format_to(out, "{:08x}", value); + out = format_to(out, FMT_STRING("{:08x}"), value); } if (n.exp_ > 0) - out = format_to(out, "p{}", n.exp_ * detail::bigint::bigit_bits); + out = format_to(out, FMT_STRING("p{}"), + n.exp_ * detail::bigint::bigit_bits); return out; } }; @@ -2715,8 +2716,8 @@ FMT_FUNC void format_system_error(detail::buffer& out, int error_code, int result = detail::safe_strerror(error_code, system_message, buf.size()); if (result == 0) { - format_to(detail::buffer_appender(out), "{}: {}", message, - system_message); + format_to(detail::buffer_appender(out), FMT_STRING("{}: {}"), + message, system_message); return; } if (result != ERANGE) From 854bf9a6b871ffd9b7496d053be7f4a97436a698 Mon Sep 17 00:00:00 2001 From: Walter Gray Date: Tue, 10 Nov 2020 16:19:14 -0800 Subject: [PATCH 02/13] fixed chrono.h changes --- include/fmt/chrono.h | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index a5e1ac92e49c..2b80d1368d44 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -764,15 +764,13 @@ inline std::chrono::duration get_milliseconds( template OutputIt format_duration_value(OutputIt out, Rep val, int precision) { - if (precision >= 0) - return format_to(out, FMT_STRING("{:.{}f}"), val, precision); - if (std::is_floating_point::value) { - return format_to(out, FMT_STRING("{:g}"), val); - } else { - return format_to(out, FMT_STRING("{}"), val); - } + const Char pr_f[] = {'{', ':', '.', '{', '}', 'f', '}', 0}; + if (precision >= 0) return vformat_to(out, pr_f, make_format_args(val, precision)); + const Char fp_f[] = {'{', ':', 'g', '}', 0}; + const Char format[] = {'{', '}', 0}; + return vformat_to(out, std::is_floating_point::value ? fp_f : format, + make_format_args(val)); } - template OutputIt copy_unit(string_view unit, OutputIt out, Char) { return std::copy(unit.begin(), unit.end(), out); @@ -790,9 +788,10 @@ template OutputIt format_duration_unit(OutputIt out) { if (const char* unit = get_units()) return copy_unit(string_view(unit), out, Char()); - if (const_check(Period::den == 1)) - return format_to(out, FMT_STRING("[{}]s"), Period::num); - return format_to(out, FMT_STRING("[{}/{}]s"), Period::num, Period::den); + const Char num_f[] = {'[', '{', '}', ']', 's', 0}; + if (const_check(Period::den == 1)) return vformat_to(out, num_f, make_format_args(Period::num)); + const Char num_def_f[] = {'[', '{', '}', '/', '{', '}', ']', 's', 0}; + return vformat_to(out, num_def_f, make_format_args(Period::num, Period::den)); } template Date: Tue, 10 Nov 2020 17:35:50 -0800 Subject: [PATCH 03/13] fixup type errors --- include/fmt/chrono.h | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index 2b80d1368d44..2349fd8c75a2 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -764,13 +764,20 @@ inline std::chrono::duration get_milliseconds( template OutputIt format_duration_value(OutputIt out, Rep val, int precision) { + using context = FMT_BUFFER_CONTEXT(type_identity_t); + const Char pr_f[] = {'{', ':', '.', '{', '}', 'f', '}', 0}; - if (precision >= 0) return vformat_to(out, pr_f, make_format_args(val, precision)); + if (precision >= 0) { + return vformat_to(out, to_string_view(pr_f), + make_format_args(val, precision)); + } const Char fp_f[] = {'{', ':', 'g', '}', 0}; const Char format[] = {'{', '}', 0}; - return vformat_to(out, std::is_floating_point::value ? fp_f : format, - make_format_args(val)); + return vformat_to( + out, to_string_view(std::is_floating_point::value ? fp_f : format), + make_format_args(val)); } + template OutputIt copy_unit(string_view unit, OutputIt out, Char) { return std::copy(unit.begin(), unit.end(), out); @@ -786,12 +793,19 @@ OutputIt copy_unit(string_view unit, OutputIt out, wchar_t) { template OutputIt format_duration_unit(OutputIt out) { - if (const char* unit = get_units()) + using context = FMT_BUFFER_CONTEXT(type_identity_t); + + if (const char* unit = get_units()) { return copy_unit(string_view(unit), out, Char()); + } const Char num_f[] = {'[', '{', '}', ']', 's', 0}; - if (const_check(Period::den == 1)) return vformat_to(out, num_f, make_format_args(Period::num)); + if (const_check(Period::den == 1)) { + return vformat_to(out, to_string_view(num_f), + make_format_args(Period::num)); + } const Char num_def_f[] = {'[', '{', '}', '/', '{', '}', ']', 's', 0}; - return vformat_to(out, num_def_f, make_format_args(Period::num, Period::den)); + return vformat_to(out, to_string_view(num_def_f), + make_format_args(Period::num, Period::den)); } template Date: Thu, 12 Nov 2020 14:24:32 -0800 Subject: [PATCH 04/13] revert removal of const --- include/fmt/color.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/fmt/color.h b/include/fmt/color.h index 42eaf71898ed..42008f511c84 100644 --- a/include/fmt/color.h +++ b/include/fmt/color.h @@ -578,7 +578,7 @@ inline std::basic_string vformat( */ template > inline std::basic_string format(const text_style& ts, const S& format_str, - Args&&... args) { + const Args&... args) { return vformat(ts, to_string_view(format_str), fmt::make_args_checked(format_str, args...)); } From 45c7c29bd5be241e67ddc39237ad7c77609c8793 Mon Sep 17 00:00:00 2001 From: Walter Gray Date: Fri, 13 Nov 2020 11:34:29 -0800 Subject: [PATCH 05/13] use constexpr --- include/fmt/chrono.h | 36 +++++++++++++----------------------- 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index 2349fd8c75a2..e7c464a4b748 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -764,18 +764,15 @@ inline std::chrono::duration get_milliseconds( template OutputIt format_duration_value(OutputIt out, Rep val, int precision) { - using context = FMT_BUFFER_CONTEXT(type_identity_t); - - const Char pr_f[] = {'{', ':', '.', '{', '}', 'f', '}', 0}; - if (precision >= 0) { - return vformat_to(out, to_string_view(pr_f), - make_format_args(val, precision)); + static constexpr const Char pr_f[] = {'{', ':', '.', '{', '}', 'f', '}', 0}; + if (precision >= 0) return format_to(out, FMT_STRING(pr_f), val, precision); + static constexpr const Char fp_f[] = {'{', ':', 'g', '}', 0}; + static constexpr const Char format[] = {'{', '}', 0}; + if(std::is_floating_point::value) { + return format_to(out, FMT_STRING(fp_f), val); + } else { + return format_to(out, FMT_STRING(format), val); } - const Char fp_f[] = {'{', ':', 'g', '}', 0}; - const Char format[] = {'{', '}', 0}; - return vformat_to( - out, to_string_view(std::is_floating_point::value ? fp_f : format), - make_format_args(val)); } template @@ -793,19 +790,12 @@ OutputIt copy_unit(string_view unit, OutputIt out, wchar_t) { template OutputIt format_duration_unit(OutputIt out) { - using context = FMT_BUFFER_CONTEXT(type_identity_t); - - if (const char* unit = get_units()) { + if (const char* unit = get_units()) return copy_unit(string_view(unit), out, Char()); - } - const Char num_f[] = {'[', '{', '}', ']', 's', 0}; - if (const_check(Period::den == 1)) { - return vformat_to(out, to_string_view(num_f), - make_format_args(Period::num)); - } - const Char num_def_f[] = {'[', '{', '}', '/', '{', '}', ']', 's', 0}; - return vformat_to(out, to_string_view(num_def_f), - make_format_args(Period::num, Period::den)); + static constexpr const Char num_f[] = {'[', '{', '}', ']', 's', 0}; + if (const_check(Period::den == 1)) return format_to(out, FMT_STRING(num_f), Period::num); + static constexpr const Char num_def_f[] = {'[', '{', '}', '/', '{', '}', ']', 's', 0}; + return format_to(out, FMT_STRING(num_def_f), Period::num, Period::den); } template Date: Fri, 13 Nov 2020 11:45:38 -0800 Subject: [PATCH 06/13] use FMT_CONSTEXPR_DECL --- include/fmt/chrono.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index e7c464a4b748..7af0b759fed8 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -764,10 +764,10 @@ inline std::chrono::duration get_milliseconds( template OutputIt format_duration_value(OutputIt out, Rep val, int precision) { - static constexpr const Char pr_f[] = {'{', ':', '.', '{', '}', 'f', '}', 0}; + static FMT_CONSTEXPR_DECL const Char pr_f[] = {'{', ':', '.', '{', '}', 'f', '}', 0}; if (precision >= 0) return format_to(out, FMT_STRING(pr_f), val, precision); - static constexpr const Char fp_f[] = {'{', ':', 'g', '}', 0}; - static constexpr const Char format[] = {'{', '}', 0}; + static FMT_CONSTEXPR_DECL const Char fp_f[] = {'{', ':', 'g', '}', 0}; + static FMT_CONSTEXPR_DECL const Char format[] = {'{', '}', 0}; if(std::is_floating_point::value) { return format_to(out, FMT_STRING(fp_f), val); } else { @@ -792,9 +792,9 @@ template OutputIt format_duration_unit(OutputIt out) { if (const char* unit = get_units()) return copy_unit(string_view(unit), out, Char()); - static constexpr const Char num_f[] = {'[', '{', '}', ']', 's', 0}; + static FMT_CONSTEXPR_DECL const Char num_f[] = {'[', '{', '}', ']', 's', 0}; if (const_check(Period::den == 1)) return format_to(out, FMT_STRING(num_f), Period::num); - static constexpr const Char num_def_f[] = {'[', '{', '}', '/', '{', '}', ']', 's', 0}; + static FMT_CONSTEXPR_DECL const Char num_def_f[] = {'[', '{', '}', '/', '{', '}', ']', 's', 0}; return format_to(out, FMT_STRING(num_def_f), Period::num, Period::den); } From 93f1e22a7e2fb5be04d1f11dfb717d3da23de3d7 Mon Sep 17 00:00:00 2001 From: Walter Gray Date: Fri, 13 Nov 2020 15:55:46 -0800 Subject: [PATCH 07/13] use FMT_ENABLE_IF for compile-time if branching --- include/fmt/chrono.h | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index 7af0b759fed8..7d2ffe67e900 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -762,17 +762,20 @@ inline std::chrono::duration get_milliseconds( return std::chrono::duration(static_cast(ms)); } -template +template ::value)> +OutputIt format_duration_value(OutputIt out, Rep val, int precision) { + static FMT_CONSTEXPR_DECL const Char format[] = {'{', '}', 0}; + return format_to(out, FMT_STRING(format), val); +} + +template ::value)> OutputIt format_duration_value(OutputIt out, Rep val, int precision) { static FMT_CONSTEXPR_DECL const Char pr_f[] = {'{', ':', '.', '{', '}', 'f', '}', 0}; if (precision >= 0) return format_to(out, FMT_STRING(pr_f), val, precision); static FMT_CONSTEXPR_DECL const Char fp_f[] = {'{', ':', 'g', '}', 0}; - static FMT_CONSTEXPR_DECL const Char format[] = {'{', '}', 0}; - if(std::is_floating_point::value) { - return format_to(out, FMT_STRING(fp_f), val); - } else { - return format_to(out, FMT_STRING(format), val); - } + return format_to(out, FMT_STRING(fp_f), val); } template From 03c29ded73b2903b660023551ab3638be65aa1ab Mon Sep 17 00:00:00 2001 From: Walter Gray Date: Fri, 13 Nov 2020 16:16:49 -0800 Subject: [PATCH 08/13] ignore unusable param in non-float overload --- include/fmt/chrono.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index 7d2ffe67e900..1548f5da6d7d 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -764,7 +764,7 @@ inline std::chrono::duration get_milliseconds( template ::value)> -OutputIt format_duration_value(OutputIt out, Rep val, int precision) { +OutputIt format_duration_value(OutputIt out, Rep val, int) { static FMT_CONSTEXPR_DECL const Char format[] = {'{', '}', 0}; return format_to(out, FMT_STRING(format), val); } From 2c271ac98c12aef3f2f17ca768ed175dcd6e8acf Mon Sep 17 00:00:00 2001 From: Walter Gray Date: Fri, 13 Nov 2020 17:16:08 -0800 Subject: [PATCH 09/13] workaround MSVC bug, add test --- include/fmt/chrono.h | 44 +++++++++++++++++++++++++++++++------------- test/format-test.cc | 6 ++++++ 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index 1548f5da6d7d..da6267d26492 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -763,19 +763,29 @@ inline std::chrono::duration get_milliseconds( } template ::value)> + FMT_ENABLE_IF(std::is_integral::value && std::is_same::value)> OutputIt format_duration_value(OutputIt out, Rep val, int) { - static FMT_CONSTEXPR_DECL const Char format[] = {'{', '}', 0}; - return format_to(out, FMT_STRING(format), val); + return format_to(out, FMT_STRING("{}"), val); } template ::value)> + FMT_ENABLE_IF(std::is_integral::value && std::is_same::value)> +OutputIt format_duration_value(OutputIt out, Rep val, int) { + return format_to(out, FMT_STRING(L"{}"), val); +} + +template ::value && std::is_same::value)> OutputIt format_duration_value(OutputIt out, Rep val, int precision) { - static FMT_CONSTEXPR_DECL const Char pr_f[] = {'{', ':', '.', '{', '}', 'f', '}', 0}; - if (precision >= 0) return format_to(out, FMT_STRING(pr_f), val, precision); - static FMT_CONSTEXPR_DECL const Char fp_f[] = {'{', ':', 'g', '}', 0}; - return format_to(out, FMT_STRING(fp_f), val); + if (precision >= 0) return format_to(out, FMT_STRING("{:.{}f}"), val, precision); + return format_to(out, FMT_STRING("{:g}"), val); +} + +template ::value && std::is_same::value)> +OutputIt format_duration_value(OutputIt out, Rep val, int precision) { + if (precision >= 0) return format_to(out, FMT_STRING("L{:.{}f}"), val, precision); + return format_to(out, FMT_STRING("L{:g}"), val); } template @@ -791,14 +801,22 @@ OutputIt copy_unit(string_view unit, OutputIt out, wchar_t) { return std::copy(u.c_str(), u.c_str() + u.size(), out); } -template +template ::value)> +OutputIt format_duration_unit(OutputIt out) { + if (const char* unit = get_units()) + return copy_unit(string_view(unit), out, Char()); + if (const_check(Period::den == 1)) return format_to(out, FMT_STRING("[{}]s"), Period::num); + return format_to(out, FMT_STRING("[{}/{}]s"), Period::num, Period::den); +} + +template ::value)> OutputIt format_duration_unit(OutputIt out) { if (const char* unit = get_units()) return copy_unit(string_view(unit), out, Char()); - static FMT_CONSTEXPR_DECL const Char num_f[] = {'[', '{', '}', ']', 's', 0}; - if (const_check(Period::den == 1)) return format_to(out, FMT_STRING(num_f), Period::num); - static FMT_CONSTEXPR_DECL const Char num_def_f[] = {'[', '{', '}', '/', '{', '}', ']', 's', 0}; - return format_to(out, FMT_STRING(num_def_f), Period::num, Period::den); + if (const_check(Period::den == 1)) return format_to(out, FMT_STRING(L"[{}]s"), Period::num); + return format_to(out, FMT_STRING(L"[{}/{}]s"), Period::num, Period::den); } template = 201703L From 45b9f9637e20d9eb494a9725a9481073c7570823 Mon Sep 17 00:00:00 2001 From: Walter Gray Date: Fri, 13 Nov 2020 17:40:32 -0800 Subject: [PATCH 10/13] #ifdef test broken in MSVC --- test/format-test.cc | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/format-test.cc b/test/format-test.cc index 230a061343fd..435b137b1bf1 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1815,8 +1815,11 @@ TEST(FormatTest, CompileTimeString) { EXPECT_EQ("42", fmt::format(FMT_STRING("{}"), 42)); EXPECT_EQ(L"42", fmt::format(FMT_STRING(L"{}"), 42)); EXPECT_EQ("foo", fmt::format(FMT_STRING("{}"), string_like())); + +#if defined(_MSC_VER) && _MSC_VER <= 1927 EXPECT_EQ("42", fmt::format(FMT_STRING(static_with_null), 42)); EXPECT_EQ(L"42", fmt::format(FMT_STRING(static_with_null_wide), 42)); +#endif (void)with_null; (void)no_null; From 98a35576c81d717cc6af1881ed8864e93238530b Mon Sep 17 00:00:00 2001 From: Walter Gray Date: Fri, 13 Nov 2020 17:47:40 -0800 Subject: [PATCH 11/13] improve test, fix conditional --- test/format-test.cc | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/test/format-test.cc b/test/format-test.cc index 435b137b1bf1..594523e38702 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1807,18 +1807,25 @@ fmt::string_view to_string_view(string_like) { return "foo"; } constexpr char with_null[3] = {'{', '}', '\0'}; constexpr char no_null[2] = {'{', '}'}; +static FMT_CONSTEXPR_DECL const char static_with_null[3] = {'{', '}', '\0'}; +static FMT_CONSTEXPR_DECL const wchar_t static_with_null_wide[3] = {'{', '}', '\0'}; +static FMT_CONSTEXPR_DECL const char static_no_null[2] = {'{', '}'}; +static FMT_CONSTEXPR_DECL const wchar_t static_no_null_wide[2] = {'{', '}'}; TEST(FormatTest, CompileTimeString) { - static FMT_CONSTEXPR_DECL const char static_with_null[3] = {'{', '}', '\0'}; - static FMT_CONSTEXPR_DECL const wchar_t static_with_null_wide[3] = {'{', '}', '\0'}; - EXPECT_EQ("42", fmt::format(FMT_STRING("{}"), 42)); EXPECT_EQ(L"42", fmt::format(FMT_STRING(L"{}"), 42)); EXPECT_EQ("foo", fmt::format(FMT_STRING("{}"), string_like())); -#if defined(_MSC_VER) && _MSC_VER <= 1927 + (void)static_with_null; + (void)static_with_null_wide; + (void)static_no_null; + (void)static_no_null_wide; +#if !defined(_MSC_VER) EXPECT_EQ("42", fmt::format(FMT_STRING(static_with_null), 42)); EXPECT_EQ(L"42", fmt::format(FMT_STRING(static_with_null_wide), 42)); + EXPECT_EQ("42", fmt::format(FMT_STRING(static_no_null), 42)); + EXPECT_EQ(L"42", fmt::format(FMT_STRING(static_no_null_wide), 42)); #endif (void)with_null; From fc3832ffa2dbb6d62ba9f0affaa497ffc6a99bf9 Mon Sep 17 00:00:00 2001 From: Walter Gray Date: Fri, 13 Nov 2020 18:10:35 -0800 Subject: [PATCH 12/13] attempt simplification to work around MSVC problem --- include/fmt/chrono.h | 44 +++++++++++++------------------------------- 1 file changed, 13 insertions(+), 31 deletions(-) diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index da6267d26492..522cf4956df0 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -763,29 +763,19 @@ inline std::chrono::duration get_milliseconds( } template ::value && std::is_same::value)> -OutputIt format_duration_value(OutputIt out, Rep val, int) { - return format_to(out, FMT_STRING("{}"), val); -} - -template ::value && std::is_same::value)> + FMT_ENABLE_IF(std::is_integral::value)> OutputIt format_duration_value(OutputIt out, Rep val, int) { - return format_to(out, FMT_STRING(L"{}"), val); + static FMT_CONSTEXPR_DECL const Char format[] = {'{', '}', 0}; + return format_to(out, compile_string_to_view(format), val); } template ::value && std::is_same::value)> -OutputIt format_duration_value(OutputIt out, Rep val, int precision) { - if (precision >= 0) return format_to(out, FMT_STRING("{:.{}f}"), val, precision); - return format_to(out, FMT_STRING("{:g}"), val); -} - -template ::value && std::is_same::value)> + FMT_ENABLE_IF(std::is_floating_point::value)> OutputIt format_duration_value(OutputIt out, Rep val, int precision) { - if (precision >= 0) return format_to(out, FMT_STRING("L{:.{}f}"), val, precision); - return format_to(out, FMT_STRING("L{:g}"), val); + static FMT_CONSTEXPR_DECL const Char pr_f[] = {'{', ':', '.', '{', '}', 'f', '}', 0}; + if (precision >= 0) return format_to(out, compile_string_to_view(pr_f), val, precision); + static FMT_CONSTEXPR_DECL const Char fp_f[] = {'{', ':', 'g', '}', 0}; + return format_to(out, compile_string_to_view(fp_f), val); } template @@ -801,22 +791,14 @@ OutputIt copy_unit(string_view unit, OutputIt out, wchar_t) { return std::copy(u.c_str(), u.c_str() + u.size(), out); } -template ::value)> -OutputIt format_duration_unit(OutputIt out) { - if (const char* unit = get_units()) - return copy_unit(string_view(unit), out, Char()); - if (const_check(Period::den == 1)) return format_to(out, FMT_STRING("[{}]s"), Period::num); - return format_to(out, FMT_STRING("[{}/{}]s"), Period::num, Period::den); -} - -template ::value)> +template OutputIt format_duration_unit(OutputIt out) { if (const char* unit = get_units()) return copy_unit(string_view(unit), out, Char()); - if (const_check(Period::den == 1)) return format_to(out, FMT_STRING(L"[{}]s"), Period::num); - return format_to(out, FMT_STRING(L"[{}/{}]s"), Period::num, Period::den); + static FMT_CONSTEXPR_DECL const Char num_f[] = {'[', '{', '}', ']', 's', 0}; + if (const_check(Period::den == 1)) return format_to(out, compile_string_to_view(num_f), Period::num); + static FMT_CONSTEXPR_DECL const Char num_def_f[] = {'[', '{', '}', '/', '{', '}', ']', 's', 0}; + return format_to(out, compile_string_to_view(num_def_f), Period::num, Period::den); } template Date: Sat, 14 Nov 2020 13:25:40 -0800 Subject: [PATCH 13/13] clang-format --- include/fmt/chrono.h | 15 ++++++++++----- test/format-test.cc | 19 +++++++++---------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index 522cf4956df0..24347bf977b2 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -772,8 +772,10 @@ OutputIt format_duration_value(OutputIt out, Rep val, int) { template ::value)> OutputIt format_duration_value(OutputIt out, Rep val, int precision) { - static FMT_CONSTEXPR_DECL const Char pr_f[] = {'{', ':', '.', '{', '}', 'f', '}', 0}; - if (precision >= 0) return format_to(out, compile_string_to_view(pr_f), val, precision); + static FMT_CONSTEXPR_DECL const Char pr_f[] = {'{', ':', '.', '{', + '}', 'f', '}', 0}; + if (precision >= 0) + return format_to(out, compile_string_to_view(pr_f), val, precision); static FMT_CONSTEXPR_DECL const Char fp_f[] = {'{', ':', 'g', '}', 0}; return format_to(out, compile_string_to_view(fp_f), val); } @@ -796,9 +798,12 @@ OutputIt format_duration_unit(OutputIt out) { if (const char* unit = get_units()) return copy_unit(string_view(unit), out, Char()); static FMT_CONSTEXPR_DECL const Char num_f[] = {'[', '{', '}', ']', 's', 0}; - if (const_check(Period::den == 1)) return format_to(out, compile_string_to_view(num_f), Period::num); - static FMT_CONSTEXPR_DECL const Char num_def_f[] = {'[', '{', '}', '/', '{', '}', ']', 's', 0}; - return format_to(out, compile_string_to_view(num_def_f), Period::num, Period::den); + if (const_check(Period::den == 1)) + return format_to(out, compile_string_to_view(num_f), Period::num); + static FMT_CONSTEXPR_DECL const Char num_def_f[] = {'[', '{', '}', '/', '{', + '}', ']', 's', 0}; + return format_to(out, compile_string_to_view(num_def_f), Period::num, + Period::den); } template -class allocator_max_size: public Allocator { +class allocator_max_size : public Allocator { public: using typename Allocator::value_type; - size_t max_size() const FMT_NOEXCEPT { - return MaxSize; - } + size_t max_size() const FMT_NOEXCEPT { return MaxSize; } value_type* allocate(size_t n) { if (n > max_size()) { throw std::length_error("size > max_size"); } return std::allocator_traits::allocate( - *static_cast(this), n); + *static_cast(this), n); } void deallocate(value_type* p, size_t n) { - std::allocator_traits::deallocate( - *static_cast(this), p, n); + std::allocator_traits::deallocate(*static_cast(this), + p, n); } }; @@ -383,7 +381,7 @@ TEST(MemoryBufferTest, AllocatorMaxSize) { try { // new_capacity = 128 + 128/2 = 192 > 160 buffer.resize(160); - } catch (const std::exception &) { + } catch (const std::exception&) { throws_on_resize = true; } EXPECT_FALSE(throws_on_resize); @@ -395,7 +393,7 @@ TEST(MemoryBufferTest, AllocatorMaxSizeOverflow) { bool throws_on_resize = false; try { buffer.resize(161); - } catch (const std::exception &) { + } catch (const std::exception&) { throws_on_resize = true; } EXPECT_TRUE(throws_on_resize); @@ -1808,7 +1806,8 @@ fmt::string_view to_string_view(string_like) { return "foo"; } constexpr char with_null[3] = {'{', '}', '\0'}; constexpr char no_null[2] = {'{', '}'}; static FMT_CONSTEXPR_DECL const char static_with_null[3] = {'{', '}', '\0'}; -static FMT_CONSTEXPR_DECL const wchar_t static_with_null_wide[3] = {'{', '}', '\0'}; +static FMT_CONSTEXPR_DECL const wchar_t static_with_null_wide[3] = {'{', '}', + '\0'}; static FMT_CONSTEXPR_DECL const char static_no_null[2] = {'{', '}'}; static FMT_CONSTEXPR_DECL const wchar_t static_no_null_wide[2] = {'{', '}'};