July 10, 2016
On 07/10/2016 11:26 PM, Adam Sansier wrote:
> For example, I'm trying to compare a wchar buffer with a wstring using
> slices:
>
> x[0..$] == y[0..$]
>
> It fails. I think because x has length 1024. If do
>
> x[0..y.length] == str[0..y.length]
>
> it fails, also because y has length 1024(since it was generated from a
> buffer and the length wasn't set correctly).

So you fill the buffer with a call to some Windows function. The Windows function either returns the amount of data it wrote, and/or it zero-terminates the string which it writes.

If it returns the length, you use that when slicing the buffer. Then you can compare.

If it zero-terminates, you search for zero in the buffer and count along to determine the length. I wanted to say that there's a function for this in phobos, but fromStringz [1] seems to be for char arrays only.


[1] https://dlang.org/phobos/std_string.html#.fromStringz
July 10, 2016
On 07/10/2016 11:38 PM, Adam Sansier wrote:
> Using this code
>
> import core.stdc.wchar_; // For wcslen.

Hint: D has special syntax for importing only specific parts of a module:

    import core.std.wchar_: wcslen;

> wstring toWstring(wchar[] value)
> {
>       return value ? cast(wstring) value[0..wcslen(cast(wchar*)value)] :
> null;
> }

You're casting mutable data to immutable. That's only valid if you ensure that the data in value is never going to be mutated ever again. Better just return the `value[0 .. wcslen(...)]` slice without casting. Or use .idup if you really need a wstring, but you haven't indicated that you need a wstring.

Instead of `cast(wchar*)value` you can use `value.ptr`. Means exactly the same but looks nicer and is less bug-prone.

> works and sets the length. Must be a bug in to!wstring!

I doubt it. But it's possible, of course. Show code that demonstrates the bug and we can check.
July 10, 2016
On Sunday, July 10, 2016 23:38:26 ag0aep6g via Digitalmars-d-learn wrote:
> On 07/10/2016 11:26 PM, Adam Sansier wrote:
> > For example, I'm trying to compare a wchar buffer with a wstring using slices:
> >
> > x[0..$] == y[0..$]
> >
> > It fails. I think because x has length 1024. If do
> >
> > x[0..y.length] == str[0..y.length]
> >
> > it fails, also because y has length 1024(since it was generated from a buffer and the length wasn't set correctly).
>
> So you fill the buffer with a call to some Windows function. The Windows function either returns the amount of data it wrote, and/or it zero-terminates the string which it writes.
>
> If it returns the length, you use that when slicing the buffer. Then you can compare.
>
> If it zero-terminates, you search for zero in the buffer and count along to determine the length. I wanted to say that there's a function for this in phobos, but fromStringz [1] seems to be for char arrays only.
>
>
> [1] https://dlang.org/phobos/std_string.html#.fromStringz

There's std.utf.toUTFz for converting to arbitrary, null-terminated, strings, but I don't think that there's a fromUTFz. fromStringz is a more recent addition, I believe, and whoever added it, didn't add the more general version.

However, there _is_ a function to get the length of a zero-terminated wchar*/wchar[]. You can use core.stdc.wchar_.wcslen and pass it a pointer to the buffer - e.g.

auto length = wcslen(buffer.ptr);

So, when you call the Windows function, you end up with something like

wchar[1024] buffer;
auto result = someWindowsFunc(buffer.ptr, someArg, someOtherArg);
if(/+ result is error +/)
    throw new Exception("some error message");
buffer[$ - 1] = '/0'; // just in case
wstring str = buffer[0 .. wcslen(buffer)].idup;

- Jonathan M Davis

1 2
Next ›   Last »