The Case For Autodecode

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Fri Jun 3 12:09:43 PDT 2016


On 6/3/16 2:55 PM, ag0aep6g wrote:
> On 06/03/2016 08:36 PM, Steven Schveighoffer wrote:
>> but a direct cast
>> of the bits from char does NOT mean the same thing as a dchar.
>
> That gives me an idea. A bitwise reinterpretation of int to float is
> nonsensical, too. Yet int implicitly converts to float and (for small
> values) preserves the meaning. I mean, implicit conversion doesn't have
> to mean bitwise reinterpretation.

I'm pretty sure the CPU handles this, though.

> How about replacing non-standalone code units with replacement character
> (U+FFFD) in implicit widening conversions?
>
> For example:
>
> ----
> char c = "รถ"[0];
> wchar w = c;
> assert(w == '\uFFFD');
> ----
>
> Would probably just be band-aid, though.

Except many chars *do* properly convert. This should work:

char c = 'a';
dchar d = c;
assert(d == 'a');

As I mentioned in my earlier reply, some kind of "bounds checking" for 
the conversion could be a possibility.

Hm... an interesting possiblity:

dchar _dchar_convert(char c)
{
    return cast(int)cast(byte)c; // get sign extension for non-ASCII
}

-Steve


More information about the Digitalmars-d mailing list