converting D's string to use with C API with unicode

tsbockman thomas.bockman at gmail.com
Sun Dec 6 05:04:35 UTC 2020


On Sunday, 6 December 2020 at 02:07:10 UTC, Jack wrote:
> On Saturday, 5 December 2020 at 23:31:31 UTC, tsbockman wrote:
>> On Saturday, 5 December 2020 at 21:55:13 UTC, Jack wrote:
>>>>     wstring ws;
>>>>     transcode(output[i], ws);
>>>>     auto s = malloc(ws.length + 1);
>>>>     if(!s) {
>>>>         onOutOfMemoryError();
>>>>     }
>>>>     memcpy(s, ws.ptr, ws.length);
>>
>> `ws.length` is the length in `wchar`s, but `memcpy` expects 
>> the size in bytes. (This is because it takes `void*` pointers 
>> as inputs, and so does not know the element type or its size.)
>
> How do I get this size in bytes from wstring?

`ws.length * wchar.sizeof` should do it. `wstring` is just an 
alias for `immutable(wchar[])`, and the `length` property is the 
number of `wchar` elements in the slice.


More information about the Digitalmars-d-learn mailing list