How to ensure string compatibility In D?

bauss jj_1337 at live.dk
Tue Jan 22 21:49:00 UTC 2019


On Tuesday, 22 January 2019 at 19:14:43 UTC, Jonathan M Davis 
wrote:
> On Tuesday, January 22, 2019 12:05:32 PM MST Stefan Koch via 
> Digitalmars-d- learn wrote:
>> On Tuesday, 22 January 2019 at 16:47:45 UTC, FrankLike wrote:
>> > On Tuesday, 22 January 2019 at 16:18:17 UTC, Adam D. Ruppe
>> >
>> > wrote:
>> >> Use "mystring"w, notice the w after the closing quote.
>> >
>> > Or toStringz is not work like c_str() in C++?
>>
>> stringz creates a char*
>> but you need a wchar*
>
> std.utf.toUTF16z or toUTFz can do that for you, though if your 
> string is already a wstring, then you can also just concatenate 
> '\0' to it. the big advantage toUTF16z is that it will also 
> convert strings of other character types rather than just 
> wstrings. So, you can write your program using proper UTF-8 
> strings and then only convert to UTF-16 for the Windows stuff 
> when you have to.
>
> https://dlang.org/phobos/std_utf.html#toUTF16z 
> https://dlang.org/phobos/std_utf.html#toUTFz
>
> - Jonathan M Davis

Is there a reason we cannot implement toStringz like:

immutable(TChar)* toStringz(TChar = char)(scope const(TChar)[] s) 
@trusted pure nothrow;
// Couldn't find a way to get the char type of a string, so 
couldn't make the following generic:
immutable(char)* toStringz(return scope string s) @trusted pure 
nothrow;
immutable(wchar)* toStringz(return scope wstring s) @trusted pure 
nothrow;
immutable(dchar)* toStringz(return scope dstring s) @trusted pure 
nothrow;



More information about the Digitalmars-d-learn mailing list