Idea: Introduce zero-terminated string specifier

Steven Schveighoffer schveiguy at yahoo.com
Mon Oct 1 19:22:49 PDT 2012


On Mon, 01 Oct 2012 21:13:47 -0400, Walter Bright  
<newshound1 at digitalmars.com> wrote:

> On 9/30/2012 11:31 AM, deadalnix wrote:
>> If you know that a string is 0 terminated, you can easily create a slice
>> from it as follow :
>>
>> char* myZeroTerminatedString;
>> char[] myZeroTerminatedString[0 .. strlen(myZeroTerminatedString)];
>>
>> It is clean and avoid to modify the stdlib in an unsafe way.
>
>
> Of course, using strlen() is always going to be unsafe. But having %zs  
> is equally unsafe for the same reason.
>
> deadalnix's example shows that adding a new format specifier %zs adds  
> little value, but it gets much worse. Since %zs is inherently unsafe, it  
> hides such unsafety in a commonly used library function, which will  
> infect everything else that transitively calls writefln with unsafety.
>
> This makes %zs an unacceptable feature.

What about %s just working with zero-terminated strings?

I was going to argue this point, but I just thought of a very very good  
counter-case for this.

string x = "abc".idup; // no zero-terminator!

writefln("%s", x.ptr);

What we don't want is for writefln to try and interpret the pointer as a C  
string.  Not only is it bad, but even the code seems to suggest "Hey, this  
should print a pointer!"

The large underlying issue here is that C considers char * to be a  
zero-terminated string, and D considers it to be a pointer.

This means any code which uses C calls heavily will have to awkwardly  
dance between both worlds.  I think there is some value in providing  
something that is *not* common to do the above work (convert char * to  
char[]).

Hm...

@system char[] zstr(char *s) { return s[0..strlen(s)]; }

provides:

writefln("%s", zstr(s));

vs.

writefln("%zs", s);

Arguably, nobody uses %zs, so even though writefln is common, the  
specifier is not.  However, we can't require an import to use a bizarre  
specifier, and you can't link un at safe code to a specifier, so the zstr  
concept is far superior in requiring the user to know what he is doing,  
and having the compiler enforce that.

Does it make sense for Phobos to provide such a shortcut in an obscure  
header somewhere?  Like std.cstring?  Or should we just say "roll your own  
if you need it"?

-Steve


More information about the Digitalmars-d mailing list