From 02d4942648ec7b77b3118fda423c7b4bb83d2b34 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sun, 18 Apr 2021 16:18:32 -0700 Subject: [PATCH 1/3] Fix more code bloat issues in --- stl/inc/format | 103 ++++++++++-------- .../test.cpp | 7 +- 2 files changed, 61 insertions(+), 49 deletions(-) diff --git a/stl/inc/format b/stl/inc/format index c928871602..2a70d3d9a7 100644 --- a/stl/inc/format +++ b/stl/inc/format @@ -2707,8 +2707,11 @@ struct _Arg_formatter { }; // The top level set of parsing "actions". -template +template struct _Format_handler { + using _OutputIt = back_insert_iterator<_Fmt_buffer<_CharT>>; + using _Context = basic_format_context<_OutputIt, _CharT>; + basic_format_parse_context<_CharT> _Parse_context; _Context _Ctx; @@ -2859,101 +2862,107 @@ _NODISCARD auto make_wformat_args(const _Args&... _Vals) { return _Format_arg_store{_Vals...}; } -template -using format_args_t = basic_format_args>; - template _OutputIt> -_OutputIt vformat_to( - _OutputIt _Out, const string_view _Fmt, const format_args_t, char> _Args) { - _Format_handler<_OutputIt, char, basic_format_context<_OutputIt, char>> _Handler(_STD move(_Out), _Fmt, _Args); - _Parse_format_string(_Fmt, _Handler); - return _Handler._Ctx.out(); +_OutputIt vformat_to(_OutputIt _Out, const string_view _Fmt, const format_args _Args) { + if constexpr (std::is_same_v<_OutputIt, _Fmt_it>) { + _Format_handler _Handler(_Out, _Fmt, _Args, _Lazy_locale{}); + _Parse_format_string(_Fmt, _Handler); + return _Out; + } else { + _Fmt_iterator_buffer<_OutputIt, char> _Buf(_STD move(_Out)); + _Format_handler _Handler(_Fmt_it{_Buf}, _Fmt, _Args, _Lazy_locale{}); + _Parse_format_string(_Fmt, _Handler); + return _Buf._Out(); + } } template _OutputIt> -_OutputIt vformat_to( - _OutputIt _Out, const wstring_view _Fmt, const format_args_t, wchar_t> _Args) { - _Format_handler<_OutputIt, wchar_t, basic_format_context<_OutputIt, wchar_t>> _Handler( - _STD move(_Out), _Fmt, _Args); - _Parse_format_string(_Fmt, _Handler); - return _Handler._Ctx.out(); +_OutputIt vformat_to(_OutputIt _Out, const wstring_view _Fmt, const wformat_args _Args) { + if constexpr (std::is_same_v<_OutputIt, _Fmt_wit>) { + _Format_handler _Handler(_Out, _Fmt, _Args, _Lazy_locale{}); + _Parse_format_string(_Fmt, _Handler); + return _Out; + } else { + _Fmt_iterator_buffer<_OutputIt, wchar_t> _Buf(_STD move(_Out)); + _Format_handler _Handler(_Fmt_wit{_Buf}, _Fmt, _Args, _Lazy_locale{}); + _Parse_format_string(_Fmt, _Handler); + return _Buf._Out(); + } } template _OutputIt> -_OutputIt vformat_to(_OutputIt _Out, const locale& _Loc, const string_view _Fmt, - const format_args_t, char> _Args) { - _Format_handler<_OutputIt, char, basic_format_context<_OutputIt, char>> _Handler( - _STD move(_Out), _Fmt, _Args, _Lazy_locale{_Loc}); - _Parse_format_string(_Fmt, _Handler); - return _Handler._Ctx.out(); +_OutputIt vformat_to(_OutputIt _Out, const locale& _Loc, const string_view _Fmt, const format_args _Args) { + if constexpr (std::is_same_v<_OutputIt, _Fmt_it>) { + _Format_handler _Handler(_Out, _Fmt, _Args, _Lazy_locale{_Loc}); + _Parse_format_string(_Fmt, _Handler); + return _Out; + } else { + _Fmt_iterator_buffer<_OutputIt, char> _Buf(_STD move(_Out)); + _Format_handler _Handler(_Fmt_it{_Buf}, _Fmt, _Args, _Lazy_locale{_Loc}); + _Parse_format_string(_Fmt, _Handler); + return _Buf._Out(); + } } template _OutputIt> -_OutputIt vformat_to(_OutputIt _Out, const locale& _Loc, const wstring_view _Fmt, - const format_args_t, wchar_t> _Args) { - _Format_handler<_OutputIt, wchar_t, basic_format_context<_OutputIt, wchar_t>> _Handler( - _STD move(_Out), _Fmt, _Args, _Lazy_locale{_Loc}); - _Parse_format_string(_Fmt, _Handler); - return _Handler._Ctx.out(); +_OutputIt vformat_to(_OutputIt _Out, const locale& _Loc, const wstring_view _Fmt, const wformat_args _Args) { + if constexpr (std::is_same_v<_OutputIt, _Fmt_wit>) { + _Format_handler _Handler(_Out, _Fmt, _Args, _Lazy_locale{_Loc}); + _Parse_format_string(_Fmt, _Handler); + return _Out; + } else { + _Fmt_iterator_buffer<_OutputIt, wchar_t> _Buf(_STD move(_Out)); + _Format_handler _Handler(_Fmt_wit{_Buf}, _Fmt, _Args, _Lazy_locale{_Loc}); + _Parse_format_string(_Fmt, _Handler); + return _Buf._Out(); + } } template _OutputIt, class... _Types> _OutputIt format_to(_OutputIt _Out, const string_view _Fmt, const _Types&... _Args) { - _Fmt_iterator_buffer<_OutputIt, char> _Buf(_STD move(_Out)); - _STD vformat_to(_Fmt_it{_Buf}, _Fmt, _STD make_format_args(_Args...)); - return _Buf._Out(); + return _STD vformat_to(_STD move(_Out), _Fmt, _STD make_format_args(_Args...)); } template _OutputIt, class... _Types> _OutputIt format_to(_OutputIt _Out, const wstring_view _Fmt, const _Types&... _Args) { - _Fmt_iterator_buffer<_OutputIt, wchar_t> _Buf(_STD move(_Out)); - _STD vformat_to(_Fmt_wit{_Buf}, _Fmt, _STD make_wformat_args(_Args...)); - return _Buf._Out(); + return _STD vformat_to(_STD move(_Out), _Fmt, _STD make_wformat_args(_Args...)); } template _OutputIt, class... _Types> _OutputIt format_to(_OutputIt _Out, const locale& _Loc, const string_view _Fmt, const _Types&... _Args) { - _Fmt_iterator_buffer<_OutputIt, char> _Buf(_STD move(_Out)); - _STD vformat_to(_Fmt_it{_Buf}, _Loc, _Fmt, _STD make_format_args(_Args...)); - return _Buf._Out(); + return _STD vformat_to(_STD move(_Out), _Loc, _Fmt, _STD make_format_args(_Args...)); } template _OutputIt, class... _Types> _OutputIt format_to(_OutputIt _Out, const locale& _Loc, const wstring_view _Fmt, const _Types&... _Args) { - _Fmt_iterator_buffer<_OutputIt, wchar_t> _Buf(_STD move(_Out)); - _STD vformat_to(_Fmt_wit{_Buf}, _Loc, _Fmt, _STD make_wformat_args(_Args...)); - return _Buf._Out(); + return _STD vformat_to(_STD move(_Out), _Loc, _Fmt, _STD make_wformat_args(_Args...)); } _NODISCARD inline string vformat(const string_view _Fmt, const format_args _Args) { string _Str; _Str.reserve(_Fmt.size() + _Args._Estimate_required_capacity()); - _Fmt_iterator_buffer, char> _Buf(back_insert_iterator{_Str}); - _STD vformat_to(_Fmt_it{_Buf}, _Fmt, _Args); + _STD vformat_to(_STD back_inserter(_Str), _Fmt, _Args); return _Str; } _NODISCARD inline wstring vformat(const wstring_view _Fmt, const wformat_args _Args) { wstring _Str; _Str.reserve(_Fmt.size() + _Args._Estimate_required_capacity()); - _Fmt_iterator_buffer, wchar_t> _Buf(back_insert_iterator{_Str}); - _STD vformat_to(_Fmt_wit{_Buf}, _Fmt, _Args); + _STD vformat_to(_STD back_inserter(_Str), _Fmt, _Args); return _Str; } _NODISCARD inline string vformat(const locale& _Loc, const string_view _Fmt, const format_args _Args) { string _Str; _Str.reserve(_Fmt.size() + _Args._Estimate_required_capacity()); - _Fmt_iterator_buffer, char> _Buf(back_insert_iterator{_Str}); - _STD vformat_to(_Fmt_it{_Buf}, _Loc, _Fmt, _Args); + _STD vformat_to(_STD back_inserter(_Str), _Loc, _Fmt, _Args); return _Str; } _NODISCARD inline wstring vformat(const locale& _Loc, const wstring_view _Fmt, const wformat_args _Args) { wstring _Str; _Str.reserve(_Fmt.size() + _Args._Estimate_required_capacity()); - _Fmt_iterator_buffer, wchar_t> _Buf(back_insert_iterator{_Str}); - _STD vformat_to(_Fmt_wit{_Buf}, _Loc, _Fmt, _Args); + _STD vformat_to(_STD back_inserter(_Str), _Loc, _Fmt, _Args); return _Str; } diff --git a/tests/std/tests/P0645R10_text_formatting_formatting/test.cpp b/tests/std/tests/P0645R10_text_formatting_formatting/test.cpp index 0b89939326..0a409eb57a 100644 --- a/tests/std/tests/P0645R10_text_formatting_formatting/test.cpp +++ b/tests/std/tests/P0645R10_text_formatting_formatting/test.cpp @@ -47,8 +47,11 @@ struct choose_literal { template auto make_testing_format_args(Args&&... vals) { - using context = basic_format_context>, charT>; - return make_format_args(forward(vals)...); + if constexpr (is_same_v) { + return make_wformat_args(forward(vals)...); + } else { + return make_format_args(forward(vals)...); + } } template From 95d5d586ba28f2adc5b3986766955e64dfe5cb54 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Tue, 27 Apr 2021 21:08:09 -0700 Subject: [PATCH 2/3] Remove std:: and replace back_inserter with back_insert_iterator --- stl/inc/format | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/stl/inc/format b/stl/inc/format index 2a70d3d9a7..97dd1233d7 100644 --- a/stl/inc/format +++ b/stl/inc/format @@ -2864,7 +2864,7 @@ _NODISCARD auto make_wformat_args(const _Args&... _Vals) { template _OutputIt> _OutputIt vformat_to(_OutputIt _Out, const string_view _Fmt, const format_args _Args) { - if constexpr (std::is_same_v<_OutputIt, _Fmt_it>) { + if constexpr (is_same_v<_OutputIt, _Fmt_it>) { _Format_handler _Handler(_Out, _Fmt, _Args, _Lazy_locale{}); _Parse_format_string(_Fmt, _Handler); return _Out; @@ -2878,7 +2878,7 @@ _OutputIt vformat_to(_OutputIt _Out, const string_view _Fmt, const format_args _ template _OutputIt> _OutputIt vformat_to(_OutputIt _Out, const wstring_view _Fmt, const wformat_args _Args) { - if constexpr (std::is_same_v<_OutputIt, _Fmt_wit>) { + if constexpr (is_same_v<_OutputIt, _Fmt_wit>) { _Format_handler _Handler(_Out, _Fmt, _Args, _Lazy_locale{}); _Parse_format_string(_Fmt, _Handler); return _Out; @@ -2892,7 +2892,7 @@ _OutputIt vformat_to(_OutputIt _Out, const wstring_view _Fmt, const wformat_args template _OutputIt> _OutputIt vformat_to(_OutputIt _Out, const locale& _Loc, const string_view _Fmt, const format_args _Args) { - if constexpr (std::is_same_v<_OutputIt, _Fmt_it>) { + if constexpr (is_same_v<_OutputIt, _Fmt_it>) { _Format_handler _Handler(_Out, _Fmt, _Args, _Lazy_locale{_Loc}); _Parse_format_string(_Fmt, _Handler); return _Out; @@ -2906,7 +2906,7 @@ _OutputIt vformat_to(_OutputIt _Out, const locale& _Loc, const string_view _Fmt, template _OutputIt> _OutputIt vformat_to(_OutputIt _Out, const locale& _Loc, const wstring_view _Fmt, const wformat_args _Args) { - if constexpr (std::is_same_v<_OutputIt, _Fmt_wit>) { + if constexpr (is_same_v<_OutputIt, _Fmt_wit>) { _Format_handler _Handler(_Out, _Fmt, _Args, _Lazy_locale{_Loc}); _Parse_format_string(_Fmt, _Handler); return _Out; @@ -2941,28 +2941,28 @@ _OutputIt format_to(_OutputIt _Out, const locale& _Loc, const wstring_view _Fmt, _NODISCARD inline string vformat(const string_view _Fmt, const format_args _Args) { string _Str; _Str.reserve(_Fmt.size() + _Args._Estimate_required_capacity()); - _STD vformat_to(_STD back_inserter(_Str), _Fmt, _Args); + _STD vformat_to(back_insert_iterator{_Str}, _Fmt, _Args); return _Str; } _NODISCARD inline wstring vformat(const wstring_view _Fmt, const wformat_args _Args) { wstring _Str; _Str.reserve(_Fmt.size() + _Args._Estimate_required_capacity()); - _STD vformat_to(_STD back_inserter(_Str), _Fmt, _Args); + _STD vformat_to(back_insert_iterator{_Str}, _Fmt, _Args); return _Str; } _NODISCARD inline string vformat(const locale& _Loc, const string_view _Fmt, const format_args _Args) { string _Str; _Str.reserve(_Fmt.size() + _Args._Estimate_required_capacity()); - _STD vformat_to(_STD back_inserter(_Str), _Loc, _Fmt, _Args); + _STD vformat_to(back_insert_iterator{_Str}, _Loc, _Fmt, _Args); return _Str; } _NODISCARD inline wstring vformat(const locale& _Loc, const wstring_view _Fmt, const wformat_args _Args) { wstring _Str; _Str.reserve(_Fmt.size() + _Args._Estimate_required_capacity()); - _STD vformat_to(_STD back_inserter(_Str), _Loc, _Fmt, _Args); + _STD vformat_to(back_insert_iterator{_Str}, _Loc, _Fmt, _Args); return _Str; } From 6dce8bd5cc1abe8df40d5dab729c9c19665d363f Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Thu, 29 Apr 2021 06:32:36 -0700 Subject: [PATCH 3/3] Remove _Lazy_locale{} --- stl/inc/format | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/stl/inc/format b/stl/inc/format index 97dd1233d7..1ff7beae52 100644 --- a/stl/inc/format +++ b/stl/inc/format @@ -2865,12 +2865,12 @@ _NODISCARD auto make_wformat_args(const _Args&... _Vals) { template _OutputIt> _OutputIt vformat_to(_OutputIt _Out, const string_view _Fmt, const format_args _Args) { if constexpr (is_same_v<_OutputIt, _Fmt_it>) { - _Format_handler _Handler(_Out, _Fmt, _Args, _Lazy_locale{}); + _Format_handler _Handler(_Out, _Fmt, _Args); _Parse_format_string(_Fmt, _Handler); return _Out; } else { _Fmt_iterator_buffer<_OutputIt, char> _Buf(_STD move(_Out)); - _Format_handler _Handler(_Fmt_it{_Buf}, _Fmt, _Args, _Lazy_locale{}); + _Format_handler _Handler(_Fmt_it{_Buf}, _Fmt, _Args); _Parse_format_string(_Fmt, _Handler); return _Buf._Out(); } @@ -2879,12 +2879,12 @@ _OutputIt vformat_to(_OutputIt _Out, const string_view _Fmt, const format_args _ template _OutputIt> _OutputIt vformat_to(_OutputIt _Out, const wstring_view _Fmt, const wformat_args _Args) { if constexpr (is_same_v<_OutputIt, _Fmt_wit>) { - _Format_handler _Handler(_Out, _Fmt, _Args, _Lazy_locale{}); + _Format_handler _Handler(_Out, _Fmt, _Args); _Parse_format_string(_Fmt, _Handler); return _Out; } else { _Fmt_iterator_buffer<_OutputIt, wchar_t> _Buf(_STD move(_Out)); - _Format_handler _Handler(_Fmt_wit{_Buf}, _Fmt, _Args, _Lazy_locale{}); + _Format_handler _Handler(_Fmt_wit{_Buf}, _Fmt, _Args); _Parse_format_string(_Fmt, _Handler); return _Buf._Out(); }