Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Changeset 3161

Show
Ignore:
Timestamp:
02/10/08 20:37:56 (10 months ago)
Author:
kris
Message:

fixes #846 : ByteSwap? usability

thanks to Mandel

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/tango/core/ByteSwap.d

    r2356 r3161  
    2323        faster overall than a traditional 'shift' implementation. 
    2424 
     25        ubyte[] x = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]; 
     26 
     27        swap16 (x.ptr, 8) -> 02 01 04 03 06 05 08 07      
     28        swap32 (x.ptr, 8) -> 04 03 02 01 08 07 06 05 
     29        swap64 (x.ptr, 8) -> 08 07 06 05 04 03 02 01 
     30 
    2531*******************************************************************************/ 
    2632 
     
    2935        /*********************************************************************** 
    3036 
     37                Reverses two-byte sequences. Parameter bytes specifies the  
     38                number of bytes, which should be a multiple of 2 
     39 
     40        ***********************************************************************/ 
     41 
     42        final static void swap16 (void[] dst) 
     43        { 
     44                swap16 (dst.ptr, dst.length); 
     45        } 
     46 
     47        /*********************************************************************** 
     48 
     49                Reverses four-byte sequences. Parameter bytes specifies the   
     50                number of bytes, which should be a multiple of 4 
     51 
     52        ***********************************************************************/ 
     53 
     54        final static void swap32 (void[] dst) 
     55        { 
     56                swap32 (dst.ptr, dst.length); 
     57        } 
     58 
     59        /*********************************************************************** 
     60 
     61                Reverse eight-byte sequences. Parameter bytes specifies the  
     62                number of bytes, which should be a multiple of 8 
     63 
     64        ***********************************************************************/ 
     65 
     66        final static void swap64 (void[] dst) 
     67        { 
     68                swap64 (dst.ptr, dst.length); 
     69        } 
     70 
     71        /*********************************************************************** 
     72 
     73                Reverse ten-byte sequences. Parameter bytes specifies the  
     74                number of bytes, which should be a multiple of 10 
     75 
     76        ***********************************************************************/ 
     77 
     78        final static void swap80 (void[] dst) 
     79        { 
     80                swap80 (dst.ptr, dst.length); 
     81        } 
     82 
     83        /*********************************************************************** 
     84 
     85                Reverses two-byte sequences. Parameter bytes specifies the  
     86                number of bytes, which should be a multiple of 2 
     87 
    3188        ***********************************************************************/ 
    3289 
    3390        final static void swap16 (void *dst, uint bytes) 
    3491        { 
    35                 ubyte* p = cast(ubyte*) dst; 
     92                assert (bytes & 0x01 is 0); 
     93 
     94                auto p = cast(ubyte*) dst; 
    3695                while (bytes) 
    3796                      { 
     
    47106        /*********************************************************************** 
    48107 
     108                Reverses four-byte sequences. Parameter bytes specifies the   
     109                number of bytes, which should be a multiple of 4 
     110 
    49111        ***********************************************************************/ 
    50112 
    51113        final static void swap32 (void *dst, uint bytes) 
    52114        { 
    53                 uint* p = cast(uint*) dst; 
     115                assert (bytes & 0x03 is 0); 
     116 
     117                auto p = cast(uint*) dst; 
    54118                while (bytes) 
    55119                      { 
    56120                      *p = bswap(*p); 
    57                       p ++
     121                      ++p
    58122                      bytes -= int.sizeof; 
    59123                      } 
     
    62126        /*********************************************************************** 
    63127 
     128                Reverse eight-byte sequences. Parameter bytes specifies the  
     129                number of bytes, which should be a multiple of 8 
     130 
    64131        ***********************************************************************/ 
    65132 
    66133        final static void swap64 (void *dst, uint bytes) 
    67134        { 
    68                 uint* p = cast(uint*) dst; 
     135                assert (bytes & 0x07 is 0); 
     136 
     137                auto p = cast(uint*) dst; 
    69138                while (bytes) 
    70139                      { 
     
    80149        /*********************************************************************** 
    81150 
     151                Reverse ten-byte sequences. Parameter bytes specifies the  
     152                number of bytes, which should be a multiple of 10 
     153 
    82154        ***********************************************************************/ 
    83155 
    84156        final static void swap80 (void *dst, uint bytes) 
    85157        { 
    86                 ubyte* p = cast(ubyte*) dst; 
     158                assert ((bytes % 10) is 0); 
     159                
     160                auto p = cast(ubyte*) dst; 
    87161                while (bytes) 
    88162                      {