Idea: Introduce zero-terminated string specifier

Steven Schveighoffer schveiguy at yahoo.com
Mon Oct 1 06:21:21 PDT 2012


On Mon, 01 Oct 2012 05:54:30 -0400, Jonathan M Davis <jmdavisProg at gmx.com>  
wrote:

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

to!string necessarily allocates, I think that is not a small problem.

I think %s should treat char * as if it is zero-terminated.

Invariably, you will have two approaches to this problem:

1. writefln("%s", mycstring); => 0xptrlocation
2. hm.., I guess I'll just use to!string => vulnerable to  
non-zero-terminated strings!

or

2. hm.., to!string will allocate, I guess I'll just use writefln("%s",  
mycstring[0..strlen(mycstring)]); => vulnerable to non-zero-terminated  
strings!

So how is forcing the user to use one of these methods any safer?  I don't  
see any casts in there...

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

Nobody should ever *ever* use printf, unless you are debugging druntime.

It's not a non-issue.  printf has no type checking whatsoever.  Using it  
means 1) non-typechecked code (i.e., accidentally pass an int instead of a  
string, or forget to pass an arg for a specifier, and you've crashed your  
code), and 2) you have locked yourself into using C's streams (something I  
hope to remedy in the future).

Besides, it doesn't *gain* you anything over having writef(ln) just  
support char *.

Bottom line -- if to!string(arg) is supported, writefln("%s", arg) should  
be supported, and do the same thing.

-Steve


More information about the Digitalmars-d mailing list