mutable string

ag0aep6g via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jul 10 14:51:19 PDT 2016


On 07/10/2016 11:38 PM, Adam Sansier wrote:
> Using this code
>
> import core.stdc.wchar_; // For wcslen.

Hint: D has special syntax for importing only specific parts of a module:

     import core.std.wchar_: wcslen;

> wstring toWstring(wchar[] value)
> {
>       return value ? cast(wstring) value[0..wcslen(cast(wchar*)value)] :
> null;
> }

You're casting mutable data to immutable. That's only valid if you 
ensure that the data in value is never going to be mutated ever again. 
Better just return the `value[0 .. wcslen(...)]` slice without casting. 
Or use .idup if you really need a wstring, but you haven't indicated 
that you need a wstring.

Instead of `cast(wchar*)value` you can use `value.ptr`. Means exactly 
the same but looks nicer and is less bug-prone.

> works and sets the length. Must be a bug in to!wstring!

I doubt it. But it's possible, of course. Show code that demonstrates 
the bug and we can check.


More information about the Digitalmars-d-learn mailing list