Convert string to wchar.

Jacob Carlborg doob at me.com
Tue Aug 2 23:29:09 PDT 2011


On 2011-08-02 19:51, Jonathan M Davis wrote:
>> I tried to convert a string into a wchar, but that didn't compile
>> because of this template constraint:
>>
>> https://github.com/D-Programming-Language/phobos/blob/master/std/conv.d#L17
>> 70
>>
>> Is there a way to convert a string into a wchar?
>
> Does that even make sense? What do you want it to do, convert the first code
> point to a wchar and throw if there's more than one character in the string?
> That's like asking whether you can covert between a container of ints and an
> int. I would never expect std.conv.to to support that. Not to mention, you
> shouldn't normally be using char or wchar by themselves, because they might
> not be valid code points. Normally, only dchar should be used when
> representing an individual character. If you want this, I'd suggest that you
> simply do something like
>
> cast(wchar)str.front
>
> What you're asking for is inherently unsafe as far as unicode goes.
>
> - Jonathan M Davis

I'm working on a serialization library and I intend to support as many 
types as possible. So if someone serializes a single wchar I need to be 
able to deserialize it. Since the serialized data is represented by a 
string, in this case, I need to convert a string containing a single 
character to a wchar when deserializing.

Yes, convert the first code point to a wchar and then throw if there's 
more the one character in the string.

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list