public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: Tim Song <t.canens.cpp@gmail.com>
Cc: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: Re: [committed] libstdc++: Implement C++23 <print> header [PR107760]
Date: Fri, 15 Dec 2023 10:42:59 +0000	[thread overview]
Message-ID: <CACb0b4k80f84LCX7=GmHyiQi+hK-8ic9mTncC5aN12Y=9trLzw@mail.gmail.com> (raw)
In-Reply-To: <CAPQZVxsPnJ6_4YK7PkdV8UnDJb9VCHmJwcY6PNQHivxM1p4VxQ@mail.gmail.com>

On Fri, 15 Dec 2023 at 01:17, Tim Song wrote:
>
> On Thu, Dec 14, 2023 at 6:05 PM Jonathan Wakely <jwakely@redhat.com> wrote:
>> +  inline void
>> +  vprint_unicode(ostream& __os, string_view __fmt, format_args __args)
>> +  {
>> +    ostream::sentry __cerb(__os);
>> +    if (__cerb)
>> +      {
>> +
>> +       const streamsize __w = __os.width();
>> +       const bool __left
>> +         = (__os.flags() & ios_base::adjustfield) == ios_base::left;
>
>
> I'm pretty sure - when I wrote this wording anyway - that the intent was that it was just an unformatted write at the end. The wording in [ostream.formatted.print] doesn't use the "determines padding" words of power that would invoke [ostream.formatted.reqmts]/3.

Ah, OK. I misunderstood "formatted output function" as implying
padding, failing to notice that we need those words of power to be
present. My thinking was that if the stream has padding set in its
format flags, it could be surprising if they're ignored by a formatted
output function. And padding in the format string applies to
individual replacement fields, not the whole string, and it's hard to
use the stream's fill character and alignment.

You can do this to use the ostream's width:

std::print("{0:{1}}", std::format(...), os.width());

But to reuse its fill char and adjustfield you need to do something
awful like I did in the committed code:

std::string_view align;
if (os.flags() & ios::adjustfield) == ios::right)
  align = ">"
auto fs = std::format("{{:{}{}{}}}", os.fill(), align, os.width());
std::vprint_nonunicode(os, fs, std::make_args(std::format(...)));

And now you have to hardcode a choice between vprint_unicode and
vprint_nonunicode, instead of letting std::print decide it. Let's hope
nobody ever needs to do any of that ;-)

I'll remove the code for padding the padding, thanks for checking the patch.


  reply	other threads:[~2023-12-15 10:43 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-15  0:02 Jonathan Wakely
2023-12-15  1:16 ` Tim Song
2023-12-15 10:42   ` Jonathan Wakely [this message]
2023-12-15 14:48     ` Tim Song
2023-12-16  9:10       ` Jonathan Wakely
2023-12-16 21:24 David Edelsohn
2023-12-16 21:44 ` Jakub Jelinek
2023-12-16 23:06   ` David Edelsohn
2023-12-17  0:02     ` Jonathan Wakely
2023-12-17  0:04       ` Jonathan Wakely
2023-12-17  0:15         ` David Edelsohn

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CACb0b4k80f84LCX7=GmHyiQi+hK-8ic9mTncC5aN12Y=9trLzw@mail.gmail.com' \
    --to=jwakely@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@gcc.gnu.org \
    --cc=t.canens.cpp@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).