On Thursday, 17 May 2012 at 07:07:58 UTC, Roman D. Boiko wrote:
And what about the following code:

// This implementation is optimized for speed via swapping
endianness in-place
pure immutable(C)[] fixEndian(C, Endian blobEndian =
endian)(ubyte[] blob) if(is(CharTypeOf!C))
{
     import std.bitmanip, std.system;
     auto data = cast(C[]) blob;
     static if(blobEndian != endian)
     {
         static assert(!is(typeof(C) == char)); // UTF-8 doesn't
have endianness
         foreach(ref ch; data) ch = swapEndian(ch);
     }
     return cast(immutable) data;
}
I mean, is it safe (assuming that we are allowed to mutate blob, and its length is a multiple of C.sizeof)?

I do casting from ubyte[] to C[].

Reply via email to