D1: UTF8 char[] casting to wchar[] array cast misalignment ERROR

monarch_dodra via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jun 17 00:49:58 PDT 2014


On Tuesday, 17 June 2014 at 06:44:40 UTC, Jacob Carlborg wrote:
> On 17/06/14 04:27, jicman wrote:
>>
>> Greetings!
>>
>> I have a bunch of files plain ASCII, UTF8 and UTF16 with and 
>> without BOM
>> (Byte Order Mark).  I had, "I thought", a nice way of figuring 
>> out what
>> type of encoding the file was (ASCII, UTF8 or UTF16) when the 
>> BOM was
>> missing, by reading the content and applying the 
>> std.utf.validate
>> function to the char[] or, wchar[] string.  The problem is 
>> that lately,
>> I am hitting into a wall with the "array cast misalignment" 
>> when casting
>> wchar[].  ie.
>>
>> auto text = cast(string) file.read();
>> wchar[] temp = cast(wchar[]) text;
>
> How about casting to "wchar[]" directory, instead of going 
> through "string".

No, the issue is that the OP is taking an array of smaller 
elements (probably containing an *ODD* amount of elements), and 
casting that as a bigger element type. If the original array size 
is not a multiple of the target element size, then it'll end up 
"slicing" the last element, and trigger said "array cast 
misalignment".

The error message (IMO), is unclear, since "misalignement" 
usually refers to *position* in memory. I think "array cast 
length mismatch" would be a better error message.

In any case, OP, something like:

auto text = file.read().assumeUnique;
size_t u = text.length % wchar.sizeof/char.sizeof;
if (u != 0) {
     // text MUST be of "char[]" type
} else {
   //OK! Here, the cast is legal: "text" *can* be of type 
"wchar[]" type.
}


More information about the Digitalmars-d-learn mailing list