Thread overview
Is there an equivalent to toStringz for wide strings?
Apr 04, 2011
Andrej Mitrovic
Apr 04, 2011
Jonathan M Davis
Apr 04, 2011
Andrej Mitrovic
Apr 04, 2011
Jonathan M Davis
Apr 05, 2011
Andrej Mitrovic
Apr 05, 2011
Kagamin
Apr 05, 2011
Andrej Mitrovic
April 04, 2011
I think I'd need a toWStringz function, or maybe toStringz can be made more clever and figure out that I'm passing a wstring and return a null-terminated wchar*.

Currently I'm using wstrings and appending the null terminator by hand, e.g.:

wstring appName  = "DMapp" ~ "\0";

Some WinAPI functions in unicode form, such as CreateWindowExW, expect a wchar*. So in the above case I just pass `appName.ptr`. But it would be convenient not having to append the null terminator prematurely.
April 04, 2011
On 2011-04-04 16:17, Andrej Mitrovic wrote:
> I think I'd need a toWStringz function, or maybe toStringz can be made more clever and figure out that I'm passing a wstring and return a null-terminated wchar*.
> 
> Currently I'm using wstrings and appending the null terminator by hand, e.g.:
> 
> wstring appName  = "DMapp" ~ "\0";
> 
> Some WinAPI functions in unicode form, such as CreateWindowExW, expect a wchar*. So in the above case I just pass `appName.ptr`. But it would be convenient not having to append the null terminator prematurely.

Almost all of the string functions are string-specific and don't deal with wstring or dstring. Some of them work with multiple string types, but most don't - though that may change. In this case, I'd just append the null character and be done with it. Not to mention, in the case above, a null character should automatically be appended onto the end of the string, since it's as string literal (unless wstring or dstring literals are treated differently for some reason - you might want to check that). All string literals have a null character at one past their end so that they can be passed to C functions directly.

Regardless, you should watch out for cases where the function being called keeps the pointer that it's passed without copying it but you didn't keep the string around, which could cause it to be garbage collected. I've screwed up with that before.

In any case, there is no toWStringz in Phobos. You can open up an enhancement request for one though.

- Jonathan M Davis
April 04, 2011
On 4/5/11, Jonathan M Davis <jmdavisProg@gmx.com> wrote:
> Not to mention, in the case above, a null
> character should automatically be appended onto the end of the string, since
> it's as string literal.

I've always found this confusing. So If I have this char array: char[] foostring = "foo".dup;

null is appended to it? So what happens to the null terminator if I append another string to foostring? And if I do a copy, e.g. foostring[0..$], does this still copy a null terminator?
April 04, 2011
On 2011-04-04 16:44, Andrej Mitrovic wrote:
> On 4/5/11, Jonathan M Davis <jmdavisProg@gmx.com> wrote:
> > Not to mention, in the case above, a null
> > character should automatically be appended onto the end of the string,
> > since it's as string literal.
> 
> I've always found this confusing. So If I have this char array: char[] foostring = "foo".dup;
> 
> null is appended to it? So what happens to the null terminator if I append another string to foostring? And if I do a copy, e.g. foostring[0..$], does this still copy a null terminator?

The null terminator is not actually part of the string. It's one beyond the end. In effect, it's "foo"[$] (though that would probably throw as long as array bounds checking is turned on). So, any array operations that you do on the string will lose the null terminator. It's just that if you pass it to a C function or some other function that's going completely off of the pointer (rather than the D array) and which hasn't been told the length of the string, it's going to see the null terminator at one past the end of the actual array.

So, you can do something like

printf("hello world\n");

or

auto s = "hello world";
printf(s.ptr);

But as soon as you do anything which would cause the array to reallocate or be copied, you lose the null terminator. It's not really part of the string. It's one past the end of the string and is there purely for convenience when dealing with C functions. So, it's lost as soon as you start treating the string a as D string and do any array or string operations on it which would copy or change it.

- Jonathan M Davis
April 05, 2011
Ok that makes sense. Thank you.
April 05, 2011
Andrej Mitrovic Wrote:

> I think I'd need a toWStringz function, or maybe toStringz can be made more clever and figure out that I'm passing a wstring and return a null-terminated wchar*.
> 
> Currently I'm using wstrings and appending the null terminator by hand, e.g.:
> 
> wstring appName  = "DMapp" ~ "\0";
> 
> Some WinAPI functions in unicode form, such as CreateWindowExW, expect a wchar*. So in the above case I just pass `appName.ptr`. But it would be convenient not having to append the null terminator prematurely.

toUTF16z
it was in windows examples somewhere, I suppose.
April 05, 2011
On 4/5/11, Kagamin <spam@here.lot> wrote:
> toUTF16z
> it was in windows examples somewhere, I suppose.
>

Great, it's in std.utf. Thanks!