Idea: Introduce zero-terminated string specifier

Alex Rønne Petersen alex at lycus.org
Fri Sep 28 19:11:43 PDT 2012


On 29-09-2012 04:08, Andrej Mitrovic wrote:
> I've noticed I'm having to do a lot of to!string calls when I want to
> call the versatile writef() function. So I was thinking, why not
> introduce a special zero-terminated string specifier which would both
> alleviate the need to call to!string and would probably save on
> needless memory allocation. If all we want to do is print something,
> why waste time duplicating a string?
>
> Let's say we call the new specifier %zs (we can debate for the actual name):
>
> extern(C) const(void)* GetName();  // e.g. some C api functions..
> extern(C) const(void)* GetLastName();
>
> Before:
> writefln("Name %s, Last Name %s", to!string(GetName()),
> to!string(GetLastName()));
>
> After:
> writefln("Name %zs, Last Name %zs", GetName(), GetLastName());
>
> Of course in this simple case you could just use printf(), but
> remember that writef() is much more versatile and allows you to
> specify %s to match any type. It would be great to match printf's
> original meaning of %s with another specifier.
>

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.

-- 
Alex Rønne Petersen
alex at lycus.org
http://lycus.org


More information about the Digitalmars-d mailing list