Jump to page: 1 26  
Page
Thread overview
Idea: Introduce zero-terminated string specifier
Sep 29, 2012
Andrej Mitrovic
Sep 29, 2012
Adam D. Ruppe
Oct 01, 2012
Piotr Szturmaj
Oct 01, 2012
Jakob Ovrum
Oct 01, 2012
Piotr Szturmaj
Oct 01, 2012
Paulo Pinto
Oct 01, 2012
Piotr Szturmaj
Oct 01, 2012
Johannes Pfau
Oct 01, 2012
Piotr Szturmaj
Oct 01, 2012
Andrej Mitrovic
Oct 02, 2012
Piotr Szturmaj
Oct 01, 2012
Andrej Mitrovic
Oct 01, 2012
Jonathan M Davis
Oct 01, 2012
Piotr Szturmaj
Sep 30, 2012
deadalnix
Sep 30, 2012
Paulo Pinto
Sep 30, 2012
Vladimir Panteleev
Sep 30, 2012
Vladimir Panteleev
Sep 30, 2012
Muhtar
Oct 01, 2012
deadalnix
Oct 01, 2012
Vladimir Panteleev
Oct 01, 2012
deadalnix
Oct 01, 2012
Vladimir Panteleev
Oct 02, 2012
deadalnix
Oct 04, 2012
Paulo Pinto
Sep 30, 2012
Andrej Mitrovic
Oct 01, 2012
Paulo Pinto
Oct 01, 2012
Rob T
Oct 02, 2012
Walter Bright
Oct 02, 2012
Walter Bright
Oct 02, 2012
David Nadlinger
Oct 02, 2012
David Nadlinger
Oct 02, 2012
David Nadlinger
Oct 03, 2012
Regan Heath
Oct 04, 2012
Regan Heath
Oct 02, 2012
deadalnix
Oct 02, 2012
Andrej Mitrovic
Oct 02, 2012
Jakob Ovrum
Oct 03, 2012
Andrej Mitrovic
Oct 03, 2012
H. S. Teoh
Sep 30, 2012
Vladimir Panteleev
Oct 03, 2012
Jonathan M Davis
Oct 03, 2012
H. S. Teoh
Oct 03, 2012
Jakob Ovrum
Oct 03, 2012
Jonathan M Davis
September 29, 2012
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
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
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
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
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
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
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
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
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
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 6