From patchwork Fri Dec 15 13:21:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 179346 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:3b04:b0:fb:cd0c:d3e with SMTP id c4csp9333729dys; Fri, 15 Dec 2023 06:58:10 -0800 (PST) X-Google-Smtp-Source: AGHT+IFvDjl3u+BvgLw//84w0Y25bwR3ALfvuwPpfmtfdepdatAXWC9RYmWnx/rU42WAL/mqtkXb X-Received: by 2002:a05:6808:6549:b0:3ba:10b1:7a32 with SMTP id fn9-20020a056808654900b003ba10b17a32mr10199602oib.23.1702652290751; Fri, 15 Dec 2023 06:58:10 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1702652290; cv=pass; d=google.com; s=arc-20160816; b=Y4Up6udGL6CiYoSa+MdpE7Jjk8vuW1BkaQA1ciFXTaGRzAJ9o3XPibUxPxn+sWOIsV voDjf1Z0bCMaqfXfSBbFcphmwG3MoouPvlgZllvqYze+FmviP9JCQUvOSub6K46KGr7V AIMjigcwalTO+GHDc1idxhVO/NW9RdRTC4s9A+VVhYTxMDQ8279vIiPZjYlf+J7icCss 8QwCpc33lWHJUVu35f3fzn+XhkJXS1a0BvBHAvnNxKxJSPkVNaF6AwodFxXjet1h08Ed PzYJ6W2igLF4t/xKdZrjm/hcolwM/U+MFb3vv3yQTK/eyUfW80R3E7Cla8Ia7LZ9q9yf ONkw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=a8600kdkaJCLozH6MJeQKpEjzX/rgL6HpNdYJVxM7aw=; fh=sJ+2/4g29YdyXkoRrFZSpsL2zxijepB7X/1rB0LDDh8=; b=epkf4ybKgbs+ldSzKVqzFSrP/Ta5ruBmJlkbUwMW/tCbO053U41DrS6ytvPMAHzVrE LiZny9sULw5Wq6OnJ0ZNA4wf4/D3+sDVJXNKolFrL36STO11bb5QHi0HWWN/DLXrR6Pa mkeoASgvLU2ri0HAXGYltSw0T4D1IY9MIXB3TR9ixjklRpVrO18Kkz7H0ubdXe45ozAW 7PTiMWbhEf1kEuFCd4H6tNWZaqtzM4pCLORZ+ZFNy8rIcvecJxXr1U1JHYbIhqn9t78A eZ6rtD25+kkc0MhTjSaXB2XZD6tzTw1dPd/lMPDPdSTL51b2mCsrg8n9HXt/zBeXwdo6 OZUA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="byO0Dh/M"; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id x11-20020a05620a448b00b007759f9416easi18041794qkp.635.2023.12.15.06.58.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Dec 2023 06:58:10 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="byO0Dh/M"; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 569DD3870938 for ; Fri, 15 Dec 2023 13:22:57 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 67ED43858430 for ; Fri, 15 Dec 2023 13:22:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 67ED43858430 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 67ED43858430 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702646554; cv=none; b=eeK0RIbGdkZUnhVDqjMKnwoKAmbAs3wWxf8DJMe6YXp/VIdSJgRCXpkxDqIJf7/QouTqm2sD7CvsWyEQalqJ7+XQyo1QZ6gWBnneBljI+KcLkX52DYS1alsx3lAcC3diSC5bzOREVLa5mmmTXCXwBzADCc1hJWbpu5qfeWxZ2io= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702646554; c=relaxed/simple; bh=KgPKLU5zZ/tsPv8dYZfOeGe5JyFeOXG+zM968ePu3qc=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=XJk9M/QMwfpR2w8ZaD4IfxbtUxGbzNgaz5CBaYBRlw7bUhb8R1NV3CWOEhB3gITs/ljkOPHTbf4xbDFcion+wf2TbWHZ3sV64+AfTCpRtqWqrA71JF6j0skCeuQApLcm1f1V6LdbEyM0AhCSZLd4GygbkaEGpJf6LTv8B9qcfnE= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1702646553; 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=a8600kdkaJCLozH6MJeQKpEjzX/rgL6HpNdYJVxM7aw=; b=byO0Dh/M/cXBBhT99rfB2BwNPP0C9auEUitKSl0wkbHvFijiouJ8BdUjhJAUD+j61jpfLd WeOdoO4FAve4olBPWhlXUwJmIJTHP44qHqvVK7h6rqM7w68kEql5zi7vsrFTdZNyeuJqus Z03YESlwNVZKUKo91+F2kzhvZLb2YEM= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-367-gZx85QNNNR6-y_p4mImHgQ-1; Fri, 15 Dec 2023 08:22:30 -0500 X-MC-Unique: gZx85QNNNR6-y_p4mImHgQ-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B01BD1C2B664; Fri, 15 Dec 2023 13:22:28 +0000 (UTC) Received: from localhost (unknown [10.42.28.145]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7AE3C492BC6; Fri, 15 Dec 2023 13:22:28 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Do not add padding for std::print to std::ostream Date: Fri, 15 Dec 2023 13:21:51 +0000 Message-ID: <20231215132227.2139454-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785360328709103168 X-GMAIL-MSGID: 1785360328709103168 Tested x86_64-linux and x86_64-w64-mingw. Pushed to trunk. -- >8 -- Tim Song pointed out that although std::print behaves as a formatted output function, it does "determine padding" using the stream's flags. libstdc++-v3/ChangeLog: * include/std/ostream (vprint_nonunicode, vprint_unicode): Do not insert padding. * testsuite/27_io/basic_ostream/print/1.cc: Adjust expected behaviour. --- libstdc++-v3/include/std/ostream | 46 +------------------ .../testsuite/27_io/basic_ostream/print/1.cc | 10 ++-- 2 files changed, 8 insertions(+), 48 deletions(-) diff --git a/libstdc++-v3/include/std/ostream b/libstdc++-v3/include/std/ostream index 4f1cdc281a3..0cac293e4d6 100644 --- a/libstdc++-v3/include/std/ostream +++ b/libstdc++-v3/include/std/ostream @@ -891,21 +891,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __try { - const streamsize __w = __os.width(); - const streamsize __n = __out.size(); - if (__w > __n) - { - const bool __left - = (__os.flags() & ios_base::adjustfield) == ios_base::left; - if (!__left) - std::__ostream_fill(__os, __w - __n); - if (__os.good()) - std::__ostream_write(__os, __out.data(), __n); - if (__left && __os.good()) - std::__ostream_fill(__os, __w - __n); - } - else - std::__ostream_write(__os, __out.data(), __n); + std::__ostream_write(__os, __out.data(), __out.size()); } __catch(const __cxxabiv1::__forced_unwind&) { @@ -923,11 +909,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ostream::sentry __cerb(__os); if (__cerb) { - - const streamsize __w = __os.width(); - const bool __left - = (__os.flags() & ios_base::adjustfield) == ios_base::left; - __format::_Str_sink __buf; std::vformat_to(__buf.out(), __os.getloc(), __fmt, __args); auto __out = __buf.view(); @@ -938,18 +919,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // If stream refers to a terminal, write a Unicode string to it. if (auto __term = __open_terminal(__os.rdbuf())) { - __format::_Str_sink __buf2; - if (__w != 0) - { - char __fmt[] = "{0:..{1}}"; - __fmt[3] == __os.fill(); - __fmt[4] == __left ? '<' : '>'; - string_view __str(__out); - std::vformat_to(__buf2.out(), // N.B. no need to use getloc() - __fmt, std::make_format_args(__str, __w)); - __out = __buf2.view(); - } - ios_base::iostate __err = ios_base::goodbit; __try { @@ -981,18 +950,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // Otherwise just insert the string as normal. __try { - const streamsize __n = __out.size(); - if (__w > __n) - { - if (!__left) - std::__ostream_fill(__os, __w - __n); - if (__os.good()) - std::__ostream_write(__os, __out.data(), __n); - if (__left && __os.good()) - std::__ostream_fill(__os, __w - __n); - } - else - std::__ostream_write(__os, __out.data(), __n); + std::__ostream_write(__os, __out.data(), __out.size()); } __catch(const __cxxabiv1::__forced_unwind&) { diff --git a/libstdc++-v3/testsuite/27_io/basic_ostream/print/1.cc b/libstdc++-v3/testsuite/27_io/basic_ostream/print/1.cc index 28dc8af33e6..b3abc570d1e 100644 --- a/libstdc++-v3/testsuite/27_io/basic_ostream/print/1.cc +++ b/libstdc++-v3/testsuite/27_io/basic_ostream/print/1.cc @@ -42,14 +42,16 @@ test_print_raw() } void -test_print_formatted() +test_print_no_padding() { + // [ostream.formatted.print] does not say this function "determines padding", + // see https://gcc.gnu.org/pipermail/gcc-patches/2023-December/640680.html char buf[64]; std::spanstream os(buf); - os << std::setw(20) << std::setfill('*') << std::right; + os << std::setw(60) << std::setfill('?') << std::right; // should be ignored std::print(os, "{} Luftballons", 99); std::string_view txt(os.span()); - VERIFY( txt == "******99 Luftballons" ); + VERIFY( txt == "99 Luftballons" ); } void @@ -106,7 +108,7 @@ int main() test_print_ostream(); test_println_ostream(); test_print_raw(); - test_print_formatted(); + test_print_no_padding(); test_vprint_nonunicode(); test_locale(); }