printf() metaprogramming challenge
Walter Bright
newshound2 at digitalmars.com
Fri May 24 22:46:22 UTC 2019
On 5/24/2019 12:15 PM, Jacob Carlborg wrote:
> On 2019-05-24 20:39, Walter Bright wrote:
>
>> C's sprintf is already @nogc nothrow and pure.
>
> Technically it's not pure because it access `errno`, that's what I meant with
> "various hacks".
The C standard doesn't say printf can set errno. Be that as it may, I did find
one printf that did:
"If a multibyte character encoding error occurs while writing wide characters,
errno is set to EILSEQ and a negative number is returned."
http://www.cplusplus.com/reference/cstdio/printf/
It's pure if not sending it malformed UTF.
>> Doing our own is not that easy, in particular, the floating point formatting
>> is a fair amount of tricky work.
> Stefan Koch has an implementation for that [3], even works at CTFE. Not sure if
> it's compatible with the C implementation though.
I have one, too, the DMC++ one, though it doesn't do the fp formatting exactly
right. I infer Stefan's doesn't, either, simply because his test suite spans
lines 574-583 and is completely inadequate.
You can get an idea of what is required by reading:
https://www.cs.tufts.edu/~nr/cs257/archive/florian-loitsch/printf.pdf
>> That wouldn't work with %.*s because the .length argument must be cast to int.
> Of course it works. The DMD code base is littered with calls to printf with D
> strings the manually way of passing the pointer and length separately, including
> the casting.
The compiler doesn't know to do the cast when passing `string` arguments by
.ptr/.length.
More information about the Digitalmars-d
mailing list