From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 2E54E3858D1E; Fri, 15 Dec 2023 13:21:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2E54E3858D1E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1702646511; bh=IunmbhHf3opWGOpFzu0x9WYoLlGGHxQt8nB2aLo1YXc=; h=From:To:Subject:Date:From; b=vuw4osnZAOcMFO7/FP7rNXbDP9q6q/L8VkmvRbXjuWODF8pTgw4TZdMBMld/5c1DJ yiibT4Hg2jLY4bhONbwis+NVA2UFFhep8o3J8vre9scHVrVqx9wQhzrJw5ijCp+QVf +rtIMj4sUSTTNexl/Os37IzCTpMXLfC9qV3pyIC4= 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 r14-6590] libstdc++: Do not add padding for std::print to std::ostream X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/master X-Git-Oldrev: 3fa689f6ed8387d315e58169bb9bace3bd508c0a X-Git-Newrev: 8a5cac92e7c20e13f01ca0cfa5dc00d014caf0d3 Message-Id: <20231215132151.2E54E3858D1E@sourceware.org> Date: Fri, 15 Dec 2023 13:21:51 +0000 (GMT) List-Id: https://gcc.gnu.org/g:8a5cac92e7c20e13f01ca0cfa5dc00d014caf0d3 commit r14-6590-g8a5cac92e7c20e13f01ca0cfa5dc00d014caf0d3 Author: Jonathan Wakely Date: Fri Dec 15 12:21:36 2023 +0000 libstdc++: Do not add padding for std::print to std::ostream 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. Diff: --- 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(); }