converting c's c* to d strings

torhu no at spam.invalid
Sat Jun 28 19:27:57 PDT 2008


Jarrett Billingsley wrote:
> "torhu" <no at spam.invalid> wrote in message 
> news:g46fa8$1hli$1 at digitalmars.com...
>> Xinok wrote:
>>> llee wrote:
>>>> Does anyone know how to convert from character array pointers in c to d 
>>>> strings using Phobos?
>>>
>>> You can convert any pointer type to an array type using slicing.
>>>
>>> import std.c.string;
>>> char* cstr;
>>> string dstr = cstr[0..strlen(cstr)];
>>
>> Or you can use std.string.toString().  It's cleaner, but slower because it 
>> copies the string.
> 
> No is isn't, it does the exact same thing as what was posted above (cstr[0 
> ... strlen(cstr)]). 
> 

Aaaaaaaargh!  It's toStringz that copies the string.  Can't remember the 
last time I was wrong about anything. :(

In my defence, phobos 2.0 toString(char*) does actually copy...



More information about the Digitalmars-d mailing list