From b732f28c007b0bd940359e9f3517f2caab7bd1a5 Mon Sep 17 00:00:00 2001 From: Orivej Desh Date: Thu, 10 Oct 2019 22:42:54 +0000 Subject: [PATCH] Deduplicate color vformat and vprint After #1351 they became essentially the same. --- include/fmt/color.h | 53 ++++++++++++++------------------------------- 1 file changed, 16 insertions(+), 37 deletions(-) diff --git a/include/fmt/color.h b/include/fmt/color.h index 6c64b43b..dbaafb2a 100644 --- a/include/fmt/color.h +++ b/include/fmt/color.h @@ -470,41 +470,10 @@ inline void reset_color(basic_memory_buffer& buffer) FMT_NOEXCEPT { } template -std::basic_string vformat(const text_style& ts, - basic_string_view format_str, - basic_format_args > args) { - basic_memory_buffer buffer; +void vformat_to(basic_memory_buffer& buf, const text_style& ts, + basic_string_view format_str, + basic_format_args> args) { bool has_style = false; - if (ts.has_emphasis()) { - has_style = true; - ansi_color_escape escape = make_emphasis(ts.get_emphasis()); - buffer.append(escape.begin(), escape.end()); - } - if (ts.has_foreground()) { - has_style = true; - ansi_color_escape escape = - make_foreground_color(ts.get_foreground()); - buffer.append(escape.begin(), escape.end()); - } - if (ts.has_background()) { - has_style = true; - ansi_color_escape escape = - make_background_color(ts.get_background()); - buffer.append(escape.begin(), escape.end()); - } - internal::vformat_to(buffer, format_str, args); - if (has_style) { - reset_color(buffer); - } - return fmt::to_string(buffer); -} -} // namespace internal - -template > -void vprint(std::FILE* f, const text_style& ts, const S& format, - basic_format_args > args) { - bool has_style = false; - basic_memory_buffer buf; if (ts.has_emphasis()) { has_style = true; auto emphasis = internal::make_emphasis(ts.get_emphasis()); @@ -522,10 +491,18 @@ void vprint(std::FILE* f, const text_style& ts, const S& format, internal::make_background_color(ts.get_background()); buf.append(background.begin(), background.end()); } - vformat_to(buf, format, args); + vformat_to(buf, format_str, args); if (has_style) { internal::reset_color(buf); } +} +} // namespace internal + +template > +void vprint(std::FILE* f, const text_style& ts, const S& format, + basic_format_args > args) { + basic_memory_buffer buf; + internal::vformat_to(buf, ts, to_string_view(format), args); buf.push_back(Char(0)); internal::fputs(buf.data(), f); } @@ -564,7 +541,9 @@ template > inline std::basic_string vformat( const text_style& ts, const S& format_str, basic_format_args > args) { - return internal::vformat(ts, to_string_view(format_str), args); + basic_memory_buffer buf; + internal::vformat_to(buf, ts, to_string_view(format_str), args); + return fmt::to_string(buf); } /** @@ -582,7 +561,7 @@ inline std::basic_string vformat( template > inline std::basic_string format(const text_style& ts, const S& format_str, const Args&... args) { - return internal::vformat( + return vformat( ts, to_string_view(format_str), {internal::make_args_checked(format_str, args...)}); }