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: 1876627 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=byO0Dh/M; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Ss8zD36YNz1ySd for ; Sat, 16 Dec 2023 00:22:46 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A5C24386186C for ; Fri, 15 Dec 2023 13:22:44 +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+incoming=patchwork.ozlabs.org@gcc.gnu.org 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(); }