Smart way to convert a C string to a D string

andrea9940 no at mail.plz
Wed Aug 7 11:50:17 PDT 2013


Hi, I have written a function to convert a C string to a D string.
I did not use to!string because it allocates a new string while 
what I want is a dynamic array of type char[] pointing to the C 
string.

auto cstr2dstr(inout(char)* cstr)
{
     import core.stdc.string: strlen;
     return cstr ? cstr[0 .. strlen(cstr)] : "";
}

As D newbie I'd like to know if this is the correct usage of 
inout (it is needed to accept without distinction char*, 
const(char)* and immutable(char)* right ?) and if there is 
already a function in the standard D library which do that 
conversion.


More information about the Digitalmars-d-learn mailing list