Convert wchar* to wstring?

Thalamus via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Apr 5 15:19:59 PDT 2016


On Tuesday, 5 April 2016 at 19:19:10 UTC, ag0aep6g wrote:
> On 05.04.2016 20:44, Thalamus wrote:
>> [...]
>
> Aside: D has syntax for "// For wchar_t.": `import 
> core.stdc.stddef: wchar_t;`.
>
>> [...]
>
> wchar_t is not wchar. wstring is not (portably) compatible with 
> a wchar_t array.
>
> If you actually have a wchar_t* and you want a wstring as 
> opposed to a wchar_t[], then you will potentially have to do 
> some converting.
>
> If you have a wchar*, then don't use wcslen, as that's defined 
> in terms of wchar_t. There may be some function for finding the 
> first null wchar from a wchar*, but I don't know it, and 
> writing out a loop isn't exactly hard:
>
> ----
> wstring toWstring(const(wchar)* value)
> {
>     if (value is null) return null;
>     auto cursor = value;
>     while (*cursor != 0) ++cursor;
>     return value[0 .. cursor - value].dup;
> }
> ----

Thank you for the feedback. You are correct.


More information about the Digitalmars-d-learn mailing list