www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - toUTFz again

I've completely lost track of what happened with the whole toUTF16z
story, but anyway since it's in std.utf why doesn't it just forward to

const(wchar)* toUTF16z(T)(T input)
    if (isSomeString!T)
    return toUTFz!(const(wchar)*)(input);

That way it can take any string argument and not just a UTF8 string.
Currently it only accepts UTF8 which is an unnecessary restriction.

Secondly, it's difficult to make an alias to 'toUTFz'. For example, if
you want a short version of 'toUTFz!(char*)' in your code, you would
typically write an alias like this:

alias toUTFz!(char*) toCharPtr;

However that won't work because toUTFz requires a second type
argument. If toUTFz was a template that forwarded to other
implementation templates it would make the above alias possible.
Here's what I mean:

// equivalent to one of toUTFz templates in std.utf
auto toUTFzImpl(P, S)(S s) { return null; }

template toUTFz(P)
    P toUTFz(S)(S str)
        return toUTFzImpl!(P)(str);

void main()
    alias toUTFz!(char*) toUTF8z;

That's much simpler than having to declare every possible combination
just to use a simple alias:
alias toUTFz!(const(char*), char[]) toUTF8z;
alias toUTFz!(const(char*), wchar[]) toUTF8z;
alias toUTFz!(const(char*), dchar[]) toUTF8z;
alias toUTFz!(const(char*), string) toUTF8z;
alias toUTFz!(const(char*), wstring) toUTF8z;
alias toUTFz!(const(char*), dstring) toUTF8z;
Mar 13 2012