printf() metaprogramming challenge

Jacob Carlborg doob at me.com
Fri May 24 19:15:44 UTC 2019


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".

> 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.

> 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.

[3] https://github.com/UplinkCoder/fpconv/blob/master/src/fpconv_ctfe.d

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list