Re: A use case for fromStringz

2011-04-16 Thread Andrej Mitrovic
Yeah I basically took the idea from the existing D implementation. Although D's arrays are a struct with a length and a pointer (I think so).

Re: A use case for fromStringz

2011-04-16 Thread spir
On 04/16/2011 06:55 AM, Andrej Mitrovic wrote: I wonder.. in all these years.. have they ever thought about using a convention in C where the length is embedded as a 32/64bit value at the pointed location of a pointer, followed by the array contents? Sometimes called "Pascal strings" (actually,

Re: A use case for fromStringz

2011-04-15 Thread Andrej Mitrovic
Microsoft has some of the most ridiculous functions. This one (GetEnvironmentStrings) returns a pointer to a block of null-terminated strings, with no information on the count of strings returned. Each string ends with a null-terminator, standard stuff. But only when you find two null terminators i

Re: A use case for fromStringz

2011-04-15 Thread Andrej Mitrovic
Hmm.. now I need a function that converts a wchar* to a wchar[] or wstring. There doesn't seem to be anything in Phobos for this type of conversion. Or maybe I haven't looked hard enough? I don't know whether this is safe since I'm not sure how the null terminator is represented in utf16, but it d

Re: A use case for fromStringz

2011-04-01 Thread Andrej Mitrovic
On 4/1/11, Jacob Carlborg wrote: > In those cases, doesn't the function return the length of the filled > data or something like that? I know what you mean. I would expect a C function to do just that, but in this case it does not. Its lame but I have to deal with it.

Re: A use case for fromStringz

2011-04-01 Thread Jacob Carlborg
On 3/31/11 11:18 PM, Andrej Mitrovic wrote: Actually, this still suffers from the problem when the returned char* doesn't have a null terminator. It really sucks when C code does that, and I've just experienced that. There is a solution though: In those cases, doesn't the function return the le

Re: A use case for fromStringz

2011-03-31 Thread Andrej Mitrovic
Oh I'm not trying to get this into Phobos, I just needed the function so I wrote it and sharing it here. Maybe it should throw. For my purposes I don't need it to throw. :)

Re: A use case for fromStringz

2011-03-31 Thread Jesse Phillips
Andrej Mitrovic Wrote: > Actually, this still suffers from the problem when the returned char* > doesn't have a null terminator. It really sucks when C code does that, > and I've just experienced that. There is a solution though: > > Since we can detect the length of the D array passed into > `fr

Re: A use case for fromStringz

2011-03-31 Thread Andrej Mitrovic
On 3/31/11, Jesse Phillips wrote: > Why not: > > string getNameOld() > { > static char[256] name; > cDispatch(name.ptr, kGetProductName); > return to!string(name.ptr); > } > Nice catch! But see my second reply. If a null terminator is missing and we know we're operating on a D a

Re: A use case for fromStringz

2011-03-31 Thread Andrej Mitrovic
Actually, this still suffers from the problem when the returned char* doesn't have a null terminator. It really sucks when C code does that, and I've just experienced that. There is a solution though: Since we can detect the length of the D array passed into `fromStringz`, we can do the job of to!

Re: A use case for fromStringz

2011-03-31 Thread Jesse Phillips
Why not: string getNameOld() { static char[256] name; cDispatch(name.ptr, kGetProductName); return to!string(name.ptr); }

A use case for fromStringz

2011-03-31 Thread Andrej Mitrovic
There are situations where you have to call a C dispatch function, and pass it a void* and a selector. The selector lets you choose what the C function does, for example an enum constant selector `kGetProductName` could ask the C function to fill a null-terminated string at the location of the v