From b0c65dffe6d58afe8446f5e293dfe6840b1abf52 Mon Sep 17 00:00:00 2001 From: Walter Gray Date: Mon, 21 Dec 2020 11:16:46 -0800 Subject: [PATCH] Refactor ranges, fix chrono, apply clang format to include/fmt/*.h --- include/fmt/chrono.h | 16 +++--- include/fmt/locale.h | 4 +- include/fmt/ranges.h | 76 +++++++++++++++-------------- test/enforce-compile-string-test.cc | 5 +- 4 files changed, 51 insertions(+), 50 deletions(-) diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index 9574a7e0..87f3a46f 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -798,19 +798,17 @@ OutputIt format_duration_unit(OutputIt out) { if (const char* unit = get_units()) return copy_unit(string_view(unit), out, Char()); - basic_memory_buffer buffer; - auto bufOut = std::back_inserter(buffer); - *bufOut++ = '['; - bufOut = write(bufOut, Period::num); + *out++ = '['; + out = write(out, Period::num); if (const_check(Period::den != 1)) { - *bufOut++ = '/'; - bufOut = write(bufOut, Period::den); + *out++ = '/'; + out = write(out, Period::den); } - *bufOut++ = ']'; - *bufOut++ = 's'; - return write(out, {buffer.data(), buffer.size()}); + *out++ = ']'; + *out++ = 's'; + return out; } template >::value> -inline auto format_to(OutputIt out, const std::locale& loc, - const S& format_str, Args&&... args) -> +inline auto format_to(OutputIt out, const std::locale& loc, const S& format_str, + Args&&... args) -> typename std::enable_if::type { const auto& vargs = fmt::make_args_checked(format_str, args...); return vformat_to(out, loc, to_string_view(format_str), vargs); diff --git a/include/fmt/ranges.h b/include/fmt/ranges.h index c4db83f6..d6c2eb11 100644 --- a/include/fmt/ranges.h +++ b/include/fmt/ranges.h @@ -247,31 +247,40 @@ template using value_type = remove_cvref_t()))>; -template ::type>::value)> -FMT_CONSTEXPR const char* format_str_quoted(bool add_space, const Arg&) { - return add_space ? " {}" : "{}"; +template +OutputIt add_range_formatting_spaces(OutputIt out, + const Formatting& formatting) { + if (const_check(formatting.add_prepostfix_space)) { + *out++ = ' '; + } + out = detail::copy(formatting.delimiter, out); + if (const_check(formatting.add_delimiter_spaces)) { + *out++ = ' '; + } + return out; } -template ::type>::value)> -FMT_CONSTEXPR const char* format_str_quoted(bool add_space, const Arg&) { - return add_space ? " \"{}\"" : "\"{}\""; +template +OutputIt write_range_entry(OutputIt out, const Arg& v) { + FMT_CONSTEXPR_DECL const char quote = []() { + if (const_check( + std::is_same::value || + is_like_std_string::type>::value)) { + return '"'; + } else if (const_check(std::is_same::value)) { + return '\''; + } else { + return '\0'; + } + }(); + + if (const_check(quote)) *out++ = quote; + out = write(out, v); + if (const_check(quote)) *out++ = quote; + + return out; } -FMT_CONSTEXPR const char* format_str_quoted(bool add_space, const char*) { - return add_space ? " \"{}\"" : "\"{}\""; -} -FMT_CONSTEXPR const wchar_t* format_str_quoted(bool add_space, const wchar_t*) { - return add_space ? L" \"{}\"" : L"\"{}\""; -} - -FMT_CONSTEXPR const char* format_str_quoted(bool add_space, const char) { - return add_space ? " '{}'" : "'{}'"; -} -FMT_CONSTEXPR const wchar_t* format_str_quoted(bool add_space, const wchar_t) { - return add_space ? L" '{}'" : L"'{}'"; -} } // namespace detail template struct is_tuple_like { @@ -286,15 +295,10 @@ struct formatter::value>> { template struct format_each { template void operator()(const T& v) { if (i > 0) { - if (formatting.add_prepostfix_space) { - *out++ = ' '; - } - out = detail::copy(formatting.delimiter, out); + out = add_range_formatting_spaces(out, formatting); } - out = vformat_to(out, - detail::format_str_quoted( - (formatting.add_delimiter_spaces && i > 0), v), - make_format_args(v)); + + out = detail::write_range_entry(out, v); ++i; } @@ -363,19 +367,19 @@ struct formatter< auto end = view.end(); for (; it != end; ++it) { if (i > 0) { - if (formatting.add_prepostfix_space) *out++ = ' '; - out = detail::copy(formatting.delimiter, out); + out = detail::add_range_formatting_spaces(out, formatting); } - out = vformat_to(out, - detail::format_str_quoted( - (formatting.add_delimiter_spaces && i > 0), *it), - make_format_args(*it)); + + out = detail::write_range_entry(out, *it); + if (++i > formatting.range_length_limit) { out = format_to(out, FMT_STRING("{}"), " ... "); break; } } - if (formatting.add_prepostfix_space) *out++ = ' '; + if (formatting.add_prepostfix_space) { + *out++ = ' '; + } return detail::copy(formatting.postfix, out); } }; diff --git a/test/enforce-compile-string-test.cc b/test/enforce-compile-string-test.cc index 3720ba9d..146e8d5e 100644 --- a/test/enforce-compile-string-test.cc +++ b/test/enforce-compile-string-test.cc @@ -52,8 +52,7 @@ void test_chrono() { void test_text_style() { fmt::print(fg(fmt::rgb(255, 20, 30)), FMT_STRING("{}"), "rgb(255,20,30)"); - fmt::format(fg(fmt::rgb(255, 20, 30)), FMT_STRING("{}"), - "rgb(255,20,30)"); + fmt::format(fg(fmt::rgb(255, 20, 30)), FMT_STRING("{}"), "rgb(255,20,30)"); fmt::text_style ts = fg(fmt::rgb(255, 20, 30)); std::string out; @@ -62,7 +61,7 @@ void test_text_style() { } void test_range() { - std::array hello = {'h','e','l','l','o'}; + std::array hello = {'h', 'e', 'l', 'l', 'o'}; fmt::format(FMT_STRING("{}"), hello); }