github.com/tencent/goom@v1.0.1/internal/bytecode/binary.go (about)

     1  package bytecode
     2  
     3  // ByteOrder A ByteOrder specifies how to convert byte sequences into
     4  // 16-, 32-, or 64-bit unsigned integers.
     5  type ByteOrder interface {
     6  	Uint16([]byte) uint16
     7  	Uint32([]byte) uint32
     8  	Uint64([]byte) uint64
     9  	PutUint16([]byte, uint16)
    10  	PutUint32([]byte, uint32)
    11  	PutUint64([]byte, uint64)
    12  	String() string
    13  }
    14  
    15  // LittleEndian is the little-endian implementation of ByteOrder.
    16  var LittleEndian littleEndian
    17  
    18  // LittleEndian is the little-endian implementation of ByteOrder.
    19  type littleEndian struct{}
    20  
    21  // Int16 LittleEndian is the little-endian implementation of ByteOrder.
    22  func (littleEndian) Int16(b []byte) int16 {
    23  	_ = b[1] // bounds check hint to compiler; see golang.org/issue/14808
    24  	return int16(b[0]) | int16(b[1])<<8
    25  }
    26  
    27  // PutInt16 LittleEndian is the little-endian implementation of ByteOrder.
    28  func (littleEndian) PutInt16(b []byte, v int16) {
    29  	_ = b[1] // early bounds check to guarantee safety of writes below
    30  	b[0] = byte(v)
    31  	b[1] = byte(v >> 8)
    32  }
    33  
    34  // Int32 LittleEndian is the little-endian implementation of ByteOrder.
    35  func (littleEndian) Int32(b []byte) int32 {
    36  	_ = b[3] // bounds check hint to compiler; see golang.org/issue/14808
    37  	return int32(b[0]) | int32(b[1])<<8 | int32(b[2])<<16 | int32(b[3])<<24
    38  }
    39  
    40  // PutInt32 LittleEndian is the little-endian implementation of ByteOrder.
    41  func (littleEndian) PutInt32(b []byte, v int32) {
    42  	_ = b[3] // early bounds check to guarantee safety of writes below
    43  	b[0] = byte(v)
    44  	b[1] = byte(v >> 8)
    45  	b[2] = byte(v >> 16)
    46  	b[3] = byte(v >> 24)
    47  }
    48  
    49  // Int64 LittleEndian is the little-endian implementation of ByteOrder.
    50  func (littleEndian) Int64(b []byte) int64 {
    51  	_ = b[7] // bounds check hint to compiler; see golang.org/issue/14808
    52  	return int64(b[0]) | int64(b[1])<<8 | int64(b[2])<<16 | int64(b[3])<<24 |
    53  		int64(b[4])<<32 | int64(b[5])<<40 | int64(b[6])<<48 | int64(b[7])<<56
    54  }
    55  
    56  // PutInt64 LittleEndian is the little-endian implementation of ByteOrder.
    57  func (littleEndian) PutInt64(b []byte, v int64) {
    58  	_ = b[7] // early bounds check to guarantee safety of writes below
    59  	b[0] = byte(v)
    60  	b[1] = byte(v >> 8)
    61  	b[2] = byte(v >> 16)
    62  	b[3] = byte(v >> 24)
    63  	b[4] = byte(v >> 32)
    64  	b[5] = byte(v >> 40)
    65  	b[6] = byte(v >> 48)
    66  	b[7] = byte(v >> 56)
    67  }
    68  
    69  // String LittleEndian is the little-endian implementation of ByteOrder.
    70  func (littleEndian) String() string { return "LittleEndian" }
    71  
    72  // GoString LittleEndian is the little-endian implementation of ByteOrder.
    73  func (littleEndian) GoString() string { return "binary.LittleEndian" }