convert ubyte[k..k + 1] to int

Artur Skawina art.08.09 at gmail.com
Thu May 17 01:39:03 PDT 2012


On 05/17/12 10:15, Roman D. Boiko wrote:
> 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[].

Only if C.ptr ends up properly aligned. There are also aliasing
issues, which i don't think are sufficiently defined for D (for
C, it would be legal only because char* is allowed to alias anything).

artur


More information about the Digitalmars-d-learn mailing list