From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 6F3A0385B515 for ; Thu, 22 Dec 2022 23:38:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 6F3A0385B515 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1671752299; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=3rKM2Rgvhihq4hsXqSMmehs3K6J20eBOpUBHHk1RsJg=; b=MsokJtWst9ARwy/9kube1FRzrQhLaB95FewmXwsJ5drjBHJPgH2pwyw0YsH4uK3+u5e6u2 lrPL09/O3IqfmPT/gdYCnusaJRC26E9vI8uEmfwYTLKx6ypPtr0e/OWphZ2Xqh+QAT3vD4 rscSM0XZhznryhXWCm+2eBUyqOrZtjQ= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-316-OOnO8jc5Op61BOxhTkScsw-1; Thu, 22 Dec 2022 18:38:17 -0500 X-MC-Unique: OOnO8jc5Op61BOxhTkScsw-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 1E4313C01D86; Thu, 22 Dec 2022 23:38:17 +0000 (UTC) Received: from localhost (unknown [10.33.36.34]) by smtp.corp.redhat.com (Postfix) with ESMTP id D823540C2064; Thu, 22 Dec 2022 23:38:16 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Add helper function in Date: Thu, 22 Dec 2022 23:38:16 +0000 Message-Id: <20221222233816.772318-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Tested x86_64-linux, sparc-solaris2.11, powerpc-aix. Pushed to trunk. -- >8 -- 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. --- 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> -- 2.38.1