Why ElementType!(char[3]) == dchar instead of char?

FreeSlave via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Sep 2 01:30:24 PDT 2015


On Wednesday, 2 September 2015 at 05:00:42 UTC, drug wrote:
> 02.09.2015 00:08, Jonathan M Davis via Digitalmars-d-learn 
> пишет:
>> On Tuesday, September 01, 2015 20:05:18 drug via 
>> Digitalmars-d-learn wrote:
>>> My case is I don't know what type user will be using, because 
>>> I write a
>>> library. What's the best way to process char[..] in this case?
>>
>> char[] should never be anything other than UTF-8. Similarly, 
>> wchar[] is
>> UTF-16, and dchar[] is UTF-32. So, if you're getting something 
>> other than
>> UTF-8, it should not be char[]. It should be something more 
>> like ubyte[].
>> If you want to operate on it as char[], you should convert it 
>> to UTF-8.
>> std.encoding may or may not help with that. But pretty much 
>> everything in D
>> - certainly in the standard library - assumes that char, 
>> wchar, and dchar
>> are UTF-encoded, and the language spec basically defines them 
>> that way.
>> Technically, you _can_ put other encodings in them, but it's 
>> just asking for
>> trouble.
>>
>> - Jonathan M Davis
>>
> I see, thanks. So I should always treat char[] as UTF in D 
> itself, but because I need to pass char[], wchar[] or dchar[] 
> to a C library I should treat it as not UTF but ubytes sequence 
> or ushort or uint sequence - just to pass it correctly, right?

You should just keep in mind that strings returned by Phobos are 
UTF encoded. Does your C library have UTF support? Is it relevant 
at all? Maybe it just treats char array as binary data. But if it 
does some non-trivial string and character manipulations or talks 
to file system, then it surely should expect strings in some 
specific encoding, and if it's not UTF, you should re-encode data 
before passing from D to this library.

Also C does not have wchar and dchar, but has wchar_t which size 
is not fixed and depends on particular platform.


More information about the Digitalmars-d-learn mailing list