Converting from C const(dchar*) to dstring

Chris Cain via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jun 24 11:34:30 PDT 2014


On Tuesday, 24 June 2014 at 18:17:07 UTC, Danyal Zia wrote:
> On Tuesday, 24 June 2014 at 17:59:41 UTC, Steven Schveighoffer 
> wrote:
>> const(dchar *)x = ...;
>>
>> // assuming 0 terminated
>> dstring text = x[0..x.strlen].idup;
>>
>> -Steve
> const(dchar)* x = "Hello\0";
> dstring text = x[0..x.strlen].idup;
> writeln(text);
>
> Error: no property 'strlen' for type 'const(dchar)*'

You can do what he said, but you'll have to write your own strlen 
function:

something like:

     size_t strlen(in dchar* s) pure @system nothrow
     {
         size_t pos = 0;
         dchar term = '\0';
         while(s[pos] != term)
             ++pos;
         return pos;
     }
     const(dchar)* ds = "hello\0";
     dstring text = ds[0..strlen(ds)].idup;
     writeln(text);

works


More information about the Digitalmars-d-learn mailing list