A use case for fromStringz

Andrej Mitrovic andrej.mitrovich at gmail.com
Fri Apr 15 21:09:03 PDT 2011


Hmm.. now I need a function that converts a wchar* to a wchar[] or
wstring. There doesn't seem to be anything in Phobos for this type of
conversion. Or maybe I haven't looked hard enough?

I don't know whether this is safe since I'm not sure how the null
terminator is represented in utf16, but it does seem to work ok from a
few test cases:

wstring fromWStringz(wchar* value)
{
    if (value is null)
        return "";

    auto oldPos = value;

    uint nullPos;
    while (*value++ != '\0')
    {
        nullPos++;
    }

    if (nullPos == 0)
        return "";

    return to!wstring(oldPos[0..nullPos]);
}

I thought we would pay more attention to interfacing with C code.
Since D is supposed to work side-by-side with C, we should have more
functions that convert common data types between the two languages.


More information about the Digitalmars-d-learn mailing list