On Tuesday, January 22, 2019 2:49:00 PM MST bauss via Digitalmars-d-learn 
wrote:
> 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;

toUTFz is the generic solution. toStringz exists specifically for UTF-8
strings, and it exists primarily because it attempts to avoid actually
appending or allocating to the string by checking to see if there's a null
character after the end of the string (which is done, because that's always
the case with string literals). If you don't care about it trying to avoid
that allocation, then there arguably isn't much point to toStringz, and you
might as well just to (str ~ '\0').ptr

- Jonathan M Davis



Reply via email to