Idea: Introduce zero-terminated string specifier

Jonathan M Davis jmdavisProg at gmx.com
Mon Oct 1 02:54:30 PDT 2012


On Monday, October 01, 2012 11:18:16 Piotr Szturmaj wrote:
> Adam D. Ruppe wrote:
> > On Saturday, 29 September 2012 at 02:11:12 UTC, Alex Rønne Petersen wrote:
> >> While the idea is reasonable, the problem then becomes that if you
> >> accidentally pass a non-zero terminated char* to %sz, all hell breaks
> >> loose just like with printf.
> > 
> > That's the same risk with to!string(), yes? We aren't really losing
> > anything by adding it.
> > 
> > Also this reminds me of the utter uselessness of the current behavior of
> > "%s" and a pointer - it prints the address.
> 
> Why not specialize current "%s" for character pointer types so it will
> print null terminated strings? It's always possible to cast to void* to
> print an address.

Honestly? One of Phobos' best features is the fact that %s works for 
_everything_. Specializing it for _anything_ would be horrible. It would also 
break a _ton_ of code. Who even uses %d, %f, etc. if they don't need to use 
format specifiers? It's just way simpler to always use %s.

I'm not completely against the idea of %zs, but I confess that I have to 
wonder what someone is doing if they really need to print zero-terminated 
strings all that often in D for anything other than quick debugging (in which 
case to!string works just fine), since only stuff directly interacting with C 
code will even care. And if it's really that big a deal, and you're constantly 
interacting with C code like that, you can always use the appropriate C 
function - printf - and then it's a non-issue.

- Jonathan M Davis


More information about the Digitalmars-d mailing list