C++ Logo

std-proposals

Advanced search

Re: +AFs-std-proposals+AF0 Newlines in F-strings

From: Hadriel Kaplan <hkaplan_at_[hidden]>
Date: Wed, 25 Oct 2023 09:42:05 +0000
Juniper Public
+AD4- From: Marcin Jaczewski +ADw-marcinjaczewski86+AEA-gmail.com +ADw-mailto:marcinjaczewski86+AEA-gmail.com+AD4APg-
+AD4- Date: Wednesday, October 25, 2023 at 4:44 AM
+AD4-
+AD4- I think extracted arguments should be literal new lines characters not
+AD4- +AGAAXABg- and +AGA-n+AGA-.

OK, and I think I agree with you - especially if we support embedded escaped double-quotes within an expression.

Right now the next-rev of the draft does not support double-quotes within the expression, but to even make it possible in the future we'd need to be consistent from now... which means converting those +ACIAXA-n+ACI- back to literal newlines as you said.


+AD4- +AD4- If you think it should compile, what should the format-string be after interpolation?:
+AD4- +AD4-
+AD4- +AD4- 1. +ACI-name+AD0AewB9-, time+AD0Aew-:+ACU-H:+ACU-M:+ACU-S+AFw-n+AH0AIg-
+AD4- +AD4- or
+AD4- +AD4- 2. +ACI-name+AD0AewB9-, time+AD0Aew-:+ACU-H:+ACU-M:+ACU-S+AH0AIg-
+AD4- +AD4-
+AD4- +AD4- And if (2), how do we know the programmer didn't want the newline? It is perfectly legal to have one there for chrono formatting, I believe.
+AD4-
+AD4- how formater handle any other white spaces?

The formatter can do whatever it likes with them, per the standard.

An example is std::chrono's formatter specializations.

chrono allows almost anything to be in the format-spec, and they're copied to the output string, while only filling-in/replacing the conversions that were prefixed with '+ACU-'.


For example, if you do this:

    auto now +AD0- std::chrono::system+AF8-clock::now()+ADs-
    std::cout +ADwAPA- std::format(+ACIAew-:+ACU-H:+ACU-M+AFw-nfoo+AH0AIg-, now)+ADs-

It will output this if 'now' is a sys+AF8-time of 9:20:

09:20
foo


Of course the std::chrono formatter doesn't see it as two characters +ACIAXA-n+ACI- when it parses the format-spec, but rather as a single newline - since they would have been converted to a newline prior to the formatter's parse function being invoked. But really its formatter doesn't care. It just blindly copies characters except for percent-escaped ones.

-hadriel

Received on 2023-10-25 09:42:15