From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 6106E3857C43; Thu, 22 Dec 2022 23:34:53 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6106E3857C43 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1671752093; bh=QRNkPgKaoCrNfslluyHc9uioyIc8oTGg8JmV9Yy7l5k=; h=From:To:Subject:Date:From; b=rMGwwG0qbLuFqnA8kqo54y1M3Cqf9YcTIXndqYNixo/WzBxEnL1lsrJ8IHQcx8OIT C2ZRS0sII9p6JeI/9UHTjeX9zFLHnNvs/2chqp07ntr0Dtxokx+FYV7wlqXn3kq1RO cnuwV4K+2MCIFPAST/f+AwcNxxIOghY/OQY/PYzc= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r13-4855] libstdc++: Add helper function in X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/master X-Git-Oldrev: d33a250f708e88c6cb447b6d0a202590fbc0b4b6 X-Git-Newrev: 9247402a29d73dc09830a48d39a27768d40e46e0 Message-Id: <20221222233453.6106E3857C43@sourceware.org> Date: Thu, 22 Dec 2022 23:34:53 +0000 (GMT) List-Id: https://gcc.gnu.org/g:9247402a29d73dc09830a48d39a27768d40e46e0 commit r13-4855-g9247402a29d73dc09830a48d39a27768d40e46e0 Author: Jonathan Wakely Date: Thu Dec 22 00:57:54 2022 +0000 libstdc++: Add helper function in Add a new __format::__write_padded_as_spec helper to remove duplicated code in formatter specializations. libstdc++-v3/ChangeLog: * include/std/format (__format::__write_padded_as_spec): New function. (__format::__formatter_str, __format::__formatter_int::format) (formatter): Use it. Diff: --- libstdc++-v3/include/std/format | 75 +++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 41 deletions(-) diff --git a/libstdc++-v3/include/std/format b/libstdc++-v3/include/std/format index 9c928371415..98421e8c123 100644 --- a/libstdc++-v3/include/std/format +++ b/libstdc++-v3/include/std/format @@ -597,6 +597,7 @@ namespace __format return __dest; } + // Write STR to OUT (and do so efficiently if OUT is a _Sink_iter). template requires output_iterator<_Out, const _CharT&> inline _Out @@ -668,6 +669,30 @@ namespace __format return __out; } + // Write STR to OUT, with alignment and padding as determined by SPEC. + // pre: __spec._M_align != _Align_default || __align != _Align_default + template + _Out + __write_padded_as_spec(basic_string_view> __str, + size_t __estimated_width, + basic_format_context<_Out, _CharT>& __fc, + const _Spec<_CharT>& __spec, + _Align __align = _Align_left) + { + size_t __width = __spec._M_get_width(__fc); + + if (__width <= __estimated_width) + return __format::__write(__fc.out(), __str); + + const size_t __nfill = __width - __estimated_width; + + if (__spec._M_align) + __align = __spec._M_align; + + return __format::__write_padded(__fc.out(), __str, __align, __nfill, + __spec._M_fill); + } + // A lightweight optional. struct _Optional_locale { @@ -799,7 +824,7 @@ namespace __format } template - typename basic_format_context<_Out, _CharT>::iterator + _Out format(basic_string_view<_CharT> __s, basic_format_context<_Out, _CharT>& __fc) const { @@ -824,16 +849,8 @@ namespace __format } } - size_t __width = _M_spec._M_get_width(__fc); - - if (__width <= __estimated_width) - return __format::__write(__fc.out(), __s); - - const size_t __nfill = __width - __estimated_width; - _Align __align = _M_spec._M_align ? _M_spec._M_align : _Align_left; - - return __format::__write_padded(__fc.out(), __s, - __align, __nfill, _M_spec._M_fill); + return __format::__write_padded_as_spec(__s, __estimated_width, + __fc, _M_spec); } #if __cpp_lib_format_ranges @@ -1089,32 +1106,16 @@ namespace __format __est_width = __s.size(); } - return _M_format_str(__s, __est_width, __fc); + return __format::__write_padded_as_spec(__s, __est_width, __fc, + _M_spec); } template typename basic_format_context<_Out, _CharT>::iterator _M_format_character(_CharT __c, basic_format_context<_Out, _CharT>& __fc) const - { return _M_format_str({&__c, 1u}, 1, __fc); } - - template - typename basic_format_context<_Out, _CharT>::iterator - _M_format_str(basic_string_view<_CharT> __str, size_t __est_width, - basic_format_context<_Out, _CharT>& __fc) const { - // TODO: this is identical to last part of __formatter_str::format - // so refactor to reuse the same code. - - size_t __width = _M_spec._M_get_width(__fc); - - if (__width <= __est_width) - return __format::__write(__fc.out(), __str); - - size_t __nfill = __width - __est_width; - _Align __align = _M_spec._M_align ? _M_spec._M_align : _Align_left; - return __format::__write_padded(__fc.out(), __str, - __align, __nfill, _M_spec._M_fill); + return __format::__write_padded_as_spec({&__c, 1u}, 1, __fc, _M_spec); } template @@ -2135,20 +2136,12 @@ namespace __format __str = wstring_view(__p, __n); } - size_t __width = _M_spec._M_get_width(__fc); - - if (__width <= (size_t)__n) - return __format::__write(__fc.out(), __str); - - size_t __nfill = __width - __n; - __format::_Align __align - = _M_spec._M_align ? _M_spec._M_align : __format::_Align_right; - return __format::__write_padded(__fc.out(), __str, - __align, __nfill, _M_spec._M_fill); + return __format::__write_padded_as_spec(__str, __n, __fc, _M_spec, + __format::_Align_right); } private: - __format::_Spec<_CharT> _M_spec{}; // XXX don't need full spec? + __format::_Spec<_CharT> _M_spec{}; }; template<__format::__char _CharT>