Re: Idea: Introduce zero-terminated string specifier

2012-10-04 Thread Regan Heath
On Thu, 04 Oct 2012 01:05:14 +0100, Steven Schveighoffer wrote: On Wed, 03 Oct 2012 08:37:14 -0400, Regan Heath wrote: On Tue, 02 Oct 2012 21:44:11 +0100, Steven Schveighoffer wrote: In fact, a better solution would be to define a C string type (other than char *), and just pretend t

Re: Idea: Introduce zero-terminated string specifier

2012-10-04 Thread Paulo Pinto
On Tuesday, 2 October 2012 at 13:07:46 UTC, deadalnix wrote: Le 01/10/2012 22:33, Vladimir Panteleev a écrit : On Monday, 1 October 2012 at 12:12:52 UTC, deadalnix wrote: Le 01/10/2012 13:29, Vladimir Panteleev a écrit : On Monday, 1 October 2012 at 10:56:36 UTC, deadalnix wrote: How does to!

Re: Idea: Introduce zero-terminated string specifier

2012-10-03 Thread Steven Schveighoffer
On Wed, 03 Oct 2012 08:37:14 -0400, Regan Heath wrote: On Tue, 02 Oct 2012 21:44:11 +0100, Steven Schveighoffer wrote: In fact, a better solution would be to define a C string type (other than char *), and just pretend those system calls return that. Then support that C string type in

Re: Idea: Introduce zero-terminated string specifier

2012-10-03 Thread Regan Heath
On Tue, 02 Oct 2012 21:44:11 +0100, Steven Schveighoffer wrote: In fact, a better solution would be to define a C string type (other than char *), and just pretend those system calls return that. Then support that C string type in writef. -Steve :D http://comments.gmane.org/gmane.comp.l

Re: Idea: Introduce zero-terminated string specifier

2012-10-02 Thread Jonathan M Davis
On Wednesday, October 03, 2012 07:35:23 Jakob Ovrum wrote: > > I suppose we could just use @trusted > > and call it a day. > > No, that would be abusing @trusted. The function would no longer > be safe, *because it contains possibly unsafe code*. @trusted is > for safe functions that the compiler

Re: Idea: Introduce zero-terminated string specifier

2012-10-02 Thread Jakob Ovrum
On Wednesday, 3 October 2012 at 05:04:01 UTC, H. S. Teoh wrote: Yes that's what I mean. If the format string is known at compile-time and known to involve only @safe code, then this would work. Something like this might work if CTFE is used to parse the format string piecemeal (i.e., translate

Re: Idea: Introduce zero-terminated string specifier

2012-10-02 Thread H. S. Teoh
On Tue, Oct 02, 2012 at 07:50:09PM -0700, Jonathan M Davis wrote: > On Tuesday, October 02, 2012 18:21:30 H. S. Teoh wrote: > > On Wed, Oct 03, 2012 at 03:07:14AM +0200, Andrej Mitrovic wrote: > > > On 10/3/12, Jakob Ovrum wrote: > > > > writefln cannot be @safe if it has to support an unsafe form

Re: Idea: Introduce zero-terminated string specifier

2012-10-02 Thread Jonathan M Davis
On Tuesday, October 02, 2012 18:21:30 H. S. Teoh wrote: > On Wed, Oct 03, 2012 at 03:07:14AM +0200, Andrej Mitrovic wrote: > > On 10/3/12, Jakob Ovrum wrote: > > > writefln cannot be @safe if it has to support an unsafe format > > > specifier. It's "hidden" because it affects every call to writefl

Re: Idea: Introduce zero-terminated string specifier

2012-10-02 Thread H. S. Teoh
On Wed, Oct 03, 2012 at 03:07:14AM +0200, Andrej Mitrovic wrote: > On 10/3/12, Jakob Ovrum wrote: > > writefln cannot be @safe if it has to support an unsafe format > > specifier. It's "hidden" because it affects every call to writefln, > > even if it doesn't use the unsafe format specifier. [...]

Re: Idea: Introduce zero-terminated string specifier

2012-10-02 Thread Andrej Mitrovic
On 10/3/12, Jakob Ovrum wrote: > writefln cannot be @safe if it has to support an unsafe format > specifier. It's "hidden" because it affects every call to > writefln, even if it doesn't use the unsafe format specifier. Ah damn I completely forgot about @safe. I tend to avoid recent features.. O

Re: Idea: Introduce zero-terminated string specifier

2012-10-02 Thread Jakob Ovrum
On Tuesday, 2 October 2012 at 21:30:35 UTC, Andrej Mitrovic wrote: On 10/2/12, Walter Bright 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

Re: Idea: Introduce zero-terminated string specifier

2012-10-02 Thread Andrej Mitrovic
On 10/2/12, Walter Bright 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)]; > Since %zs is in

Re: Idea: Introduce zero-terminated string specifier

2012-10-02 Thread Steven Schveighoffer
On Tue, 02 Oct 2012 15:35:47 -0400, David Nadlinger wrote: On Tuesday, 2 October 2012 at 19:34:31 UTC, David Nadlinger wrote: Well, make it to!char(char*) then! ;) Oh dear, this doesn't get better: Of course, I've meant to write »to!(char[])(char*)«. Right. I agree, this should not al

Re: Idea: Introduce zero-terminated string specifier

2012-10-02 Thread David Nadlinger
On Tuesday, 2 October 2012 at 19:34:31 UTC, David Nadlinger wrote: Well, make it to!char(char*) then! ;) Oh dear, this doesn't get better: Of course, I've meant to write »to!(char[])(char*)«. David

Re: Idea: Introduce zero-terminated string specifier

2012-10-02 Thread David Nadlinger
On Tuesday, 2 October 2012 at 19:31:33 UTC, Steven Schveighoffer wrote: On Tue, 02 Oct 2012 15:17:42 -0400, David Nadlinger wrote: On Tuesday, 2 October 2012 at 02:22:33 UTC, Steven Schveighoffer wrote: @system char[] zstr(char *s) { return s[0..strlen(s)]; } […] Does it make sense for Pho

Re: Idea: Introduce zero-terminated string specifier

2012-10-02 Thread Steven Schveighoffer
On Tue, 02 Oct 2012 15:17:42 -0400, David Nadlinger wrote: On Tuesday, 2 October 2012 at 02:22:33 UTC, Steven Schveighoffer wrote: @system char[] zstr(char *s) { return s[0..strlen(s)]; } […] Does it make sense for Phobos to provide such a shortcut in an obscure header somewhere? Like s

Re: Idea: Introduce zero-terminated string specifier

2012-10-02 Thread David Nadlinger
On Tuesday, 2 October 2012 at 02:22:33 UTC, Steven Schveighoffer wrote: @system char[] zstr(char *s) { return s[0..strlen(s)]; } […] 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

Re: Idea: Introduce zero-terminated string specifier

2012-10-02 Thread Steven Schveighoffer
On Tue, 02 Oct 2012 04:09:43 -0400, Walter Bright wrote: On 10/1/2012 7:22 PM, Steven Schveighoffer wrote: 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"? As a matter of princip

Re: Idea: Introduce zero-terminated string specifier

2012-10-02 Thread deadalnix
Le 02/10/2012 03:13, Walter Bright a écrit : 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 av

Re: Idea: Introduce zero-terminated string specifier

2012-10-02 Thread deadalnix
Le 01/10/2012 22:33, Vladimir Panteleev a écrit : On Monday, 1 October 2012 at 12:12:52 UTC, deadalnix wrote: Le 01/10/2012 13:29, Vladimir Panteleev a écrit : On Monday, 1 October 2012 at 10:56:36 UTC, deadalnix wrote: How does to!string know that the string is 0 terminated ? By convention

Re: Idea: Introduce zero-terminated string specifier

2012-10-02 Thread Andrei Alexandrescu
On 10/2/12 4:09 AM, Walter Bright wrote: On 10/1/2012 7:22 PM, Steven Schveighoffer wrote: 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"? As a matter of principle, I really don't l

Re: Idea: Introduce zero-terminated string specifier

2012-10-02 Thread Piotr Szturmaj
Andrej Mitrovic wrote: On 10/1/12, Piotr Szturmaj wrote: For example C binding writers may change: extern(C) char* getstr(); to extern(C) cstring getstr(); I don't think you can reliably do that because of semantics w.r.t. passing parameters on the stack vs in registers based on whether a

Re: Idea: Introduce zero-terminated string specifier

2012-10-02 Thread Walter Bright
On 10/1/2012 7:22 PM, Steven Schveighoffer wrote: However, we can't require an import to use a bizarre specifier, and you can't link un@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. Yup. Doe

Re: Idea: Introduce zero-terminated string specifier

2012-10-01 Thread Steven Schveighoffer
On Mon, 01 Oct 2012 21:13:47 -0400, Walter Bright 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

Re: Idea: Introduce zero-terminated string specifier

2012-10-01 Thread Walter Bright
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.

Re: Idea: Introduce zero-terminated string specifier

2012-10-01 Thread Vladimir Panteleev
On Monday, 1 October 2012 at 12:12:52 UTC, deadalnix wrote: Le 01/10/2012 13:29, Vladimir Panteleev a écrit : On Monday, 1 October 2012 at 10:56:36 UTC, deadalnix wrote: How does to!string know that the string is 0 terminated ? By convention (it doesn't). It is unsafe as hell oO Forcing t

Re: Idea: Introduce zero-terminated string specifier

2012-10-01 Thread Andrej Mitrovic
On 10/1/12, Andrej Mitrovic wrote: > but the D > compiler expected the parameters to be passed missing "in another way" there.

Re: Idea: Introduce zero-terminated string specifier

2012-10-01 Thread Andrej Mitrovic
On 10/1/12, Piotr Szturmaj wrote: > For example C binding writers may change: > > extern(C) char* getstr(); > > to > > extern(C) cstring getstr(); I don't think you can reliably do that because of semantics w.r.t. passing parameters on the stack vs in registers based on whether a type is a pointe

Re: Idea: Introduce zero-terminated string specifier

2012-10-01 Thread Piotr Szturmaj
Johannes Pfau wrote: struct CString(T) if (isSomeChar!T) { T* str; } @property auto cstring(S : T*, T)(S str) if (isSomeChar!T) { return CString!T(str); } string test = "abc"; immutable(char)* p = test.ptr; writefln("%s", p.cstring); // prints "abc" Here the char point

Re: Idea: Introduce zero-terminated string specifier

2012-10-01 Thread Steven Schveighoffer
On Mon, 01 Oct 2012 05:54:30 -0400, Jonathan M Davis 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

Re: Idea: Introduce zero-terminated string specifier

2012-10-01 Thread deadalnix
Le 01/10/2012 13:29, Vladimir Panteleev a écrit : On Monday, 1 October 2012 at 10:56:36 UTC, deadalnix wrote: Le 30/09/2012 21:58, Vladimir Panteleev a écrit : 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 slic

Re: Idea: Introduce zero-terminated string specifier

2012-10-01 Thread Johannes Pfau
Am Mon, 01 Oct 2012 13:22:46 +0200 schrieb Piotr Szturmaj : > Paulo Pinto wrote: > > On Monday, 1 October 2012 at 09:42:08 UTC, Piotr Szturmaj wrote: > >> Jakob Ovrum wrote: > >>> On Monday, 1 October 2012 at 09:17:52 UTC, Piotr Szturmaj wrote: > Adam D. Ruppe wrote: > > On Saturday, 29 S

Re: Idea: Introduce zero-terminated string specifier

2012-10-01 Thread Piotr Szturmaj
Jonathan M Davis wrote: On Monday, October 01, 2012 11:18:16 Piotr Szturmaj wrote: Adam D. Ruppe wrote: 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

Re: Idea: Introduce zero-terminated string specifier

2012-10-01 Thread Vladimir Panteleev
On Monday, 1 October 2012 at 10:56:36 UTC, deadalnix wrote: Le 30/09/2012 21:58, Vladimir Panteleev a écrit : 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* myZeroTerminatedStrin

Re: Idea: Introduce zero-terminated string specifier

2012-10-01 Thread Piotr Szturmaj
Paulo Pinto wrote: On Monday, 1 October 2012 at 09:42:08 UTC, Piotr Szturmaj wrote: Jakob Ovrum wrote: On Monday, 1 October 2012 at 09:17:52 UTC, Piotr Szturmaj wrote: Adam D. Ruppe wrote: On Saturday, 29 September 2012 at 02:11:12 UTC, Alex Rønne Petersen wrote: Also this reminds me of the u

Re: Idea: Introduce zero-terminated string specifier

2012-10-01 Thread deadalnix
Le 30/09/2012 21:58, Vladimir Panteleev a écrit : 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(myZeroTerminatedS

Re: Idea: Introduce zero-terminated string specifier

2012-10-01 Thread Paulo Pinto
On Monday, 1 October 2012 at 09:42:08 UTC, Piotr Szturmaj wrote: Jakob Ovrum wrote: On Monday, 1 October 2012 at 09:17:52 UTC, Piotr Szturmaj wrote: Adam D. Ruppe wrote: On Saturday, 29 September 2012 at 02:11:12 UTC, Alex Rønne Petersen wrote: Also this reminds me of the utter uselessness of

Re: Idea: Introduce zero-terminated string specifier

2012-10-01 Thread Jonathan M Davis
On Monday, October 01, 2012 11:18:16 Piotr Szturmaj wrote: > Adam D. Ruppe wrote: > > 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 hel

Re: Idea: Introduce zero-terminated string specifier

2012-10-01 Thread Piotr Szturmaj
Jakob Ovrum wrote: On Monday, 1 October 2012 at 09:17:52 UTC, Piotr Szturmaj wrote: Adam D. Ruppe wrote: On Saturday, 29 September 2012 at 02:11:12 UTC, Alex Rønne Petersen wrote: Also this reminds me of the utter uselessness of the current behavior of "%s" and a pointer - it prints the address

Re: Idea: Introduce zero-terminated string specifier

2012-10-01 Thread Jakob Ovrum
On Monday, 1 October 2012 at 09:17:52 UTC, Piotr Szturmaj wrote: Adam D. Ruppe wrote: On Saturday, 29 September 2012 at 02:11:12 UTC, Alex Rønne Petersen wrote: Also this reminds me of the utter uselessness of the current behavior of "%s" and a pointer - it prints the address. Why not specia

Re: Idea: Introduce zero-terminated string specifier

2012-10-01 Thread Piotr Szturmaj
Adam D. Ruppe wrote: 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!str

Re: Idea: Introduce zero-terminated string specifier

2012-10-01 Thread Rob T
On Monday, 1 October 2012 at 06:58:41 UTC, Paulo Pinto wrote: You should anyway wrap those APIs not to pollute D call with lower level APIs. I have to agree, esp when it applies to pointers. We should not forget that one of the objectives of D is to make coding "safe" by getting rid of the ne

Re: Idea: Introduce zero-terminated string specifier

2012-10-01 Thread Paulo Pinto
On Sunday, 30 September 2012 at 20:27:16 UTC, Andrej Mitrovic wrote: On 9/30/12, 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)];

Re: Idea: Introduce zero-terminated string specifier

2012-09-30 Thread Muhtar
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 ..

Re: Idea: Introduce zero-terminated string specifier

2012-09-30 Thread Andrej Mitrovic
On 9/30/12, 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. >

Re: Idea: Introduce zero-terminated string specifier

2012-09-30 Thread Vladimir Panteleev
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

Re: Idea: Introduce zero-terminated string specifier

2012-09-30 Thread Vladimir Panteleev
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

Re: Idea: Introduce zero-terminated string specifier

2012-09-30 Thread Vladimir Panteleev
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 s

Re: Idea: Introduce zero-terminated string specifier

2012-09-30 Thread Paulo Pinto
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 s

Re: Idea: Introduce zero-terminated string specifier

2012-09-30 Thread deadalnix
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.

Re: Idea: Introduce zero-terminated string specifier

2012-09-28 Thread Adam D. Ruppe
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

Re: Idea: Introduce zero-terminated string specifier

2012-09-28 Thread Alex Rønne Petersen
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 p