View mode: basic / threaded / horizontal-split · Log in · Help
September 29, 2012
Idea: Introduce zero-terminated string specifier
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.
September 29, 2012
Re: Idea: Introduce zero-terminated string specifier
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@lycus.org
http://lycus.org
September 29, 2012
Re: Idea: Introduce zero-terminated string specifier
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.

I think this should be simply disallowed. If you want that, you 
can use %x, and if you want it printed, that's where the new %z 
comes in.
September 30, 2012
Re: Idea: Introduce zero-terminated string specifier
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.
September 30, 2012
Re: Idea: Introduce zero-terminated string specifier
On Sunday, 30 September 2012 at 18:31:00 UTC, 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.

+1

We don't need to preserve C's design errors regarding strings and 
vectors.

--
Paulo
September 30, 2012
Re: Idea: Introduce zero-terminated string specifier
On Sunday, 30 September 2012 at 18:31:00 UTC, 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.

That's what to!string already does.
September 30, 2012
Re: Idea: Introduce zero-terminated string specifier
On Saturday, 29 September 2012 at 02:07:38 UTC, 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?

I just checked and std.conv.to always allocates a copy, even when 
constness doesn't require it. It should not reallocate when 
constness doesn't change, or is a safe conversion (e.g. immutable 
-> const).

A discussion on a related topic (formatting of C strings results 
in unexpected behavior) is here: 
http://d.puremagic.com/issues/show_bug.cgi?id=8384
September 30, 2012
Re: Idea: Introduce zero-terminated string specifier
On Sunday, 30 September 2012 at 18:58:11 UTC, Paulo Pinto wrote:
> +1
>
> We don't need to preserve C's design errors regarding strings 
> and vectors.

The problem is that, unsurprisingly, most C APIs (not just libc, 
but also most C libraries and OS APIs) use zero-terminated 
strings. The philosophy of ignoring the existence of C strings 
throughout all of D makes working with such APIs needlessly 
verbose (and sometimes annoying, as D code will compile and 
produce unexpected results).
September 30, 2012
Re: Idea: Introduce zero-terminated string specifier
On 9/30/12, deadalnix <deadalnix@gmail.com> 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.
>

What does that have to do with writef()? You can call to!string, but
that's beside the point. The point was getting rid of this verbosity
when using C APIs.
September 30, 2012
Re: Idea: Introduce zero-terminated string specifier
On Sunday, 30 September 2012 at 19:58:16 UTC, Vladimir Panteleev 
wrote:
> On Sunday, 30 September 2012 at 18:31:00 UTC, 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.
>
> That's what to!string already does.

I aggere you... <a 
href="http://www.tercumesirketi.com/">Tercüme</a> || <a 
href="http://www.tercumesirketi.com/">Tercüme Büroları</a>
« First   ‹ Prev
1 2 3 4 5
Top | Discussion index | About this forum | D home