gitee.com/lh-her-team/common@v1.5.1/bytehelper/bytes.go (about)

     1  package bytehelper
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/binary"
     6  	"unsafe"
     7  )
     8  
     9  // BytesToInt le bytes to int32, little endian
    10  func BytesToInt(b []byte) (int32, error) {
    11  	bytesBuffer := bytes.NewBuffer(b)
    12  	var x int32
    13  	err := binary.Read(bytesBuffer, binary.LittleEndian, &x)
    14  	if err != nil {
    15  		return -1, err
    16  	}
    17  	return x, nil
    18  }
    19  
    20  // BytesToInt64 le bytes to int64, little endian
    21  func BytesToInt64(b []byte) (int64, error) {
    22  	bytesBuffer := bytes.NewBuffer(b)
    23  	var x int64
    24  	err := binary.Read(bytesBuffer, binary.LittleEndian, &x)
    25  	if err != nil {
    26  		return -1, err
    27  	}
    28  	return x, nil
    29  }
    30  
    31  // IntToBytes int32 to le bytes, little endian
    32  func IntToBytes(x int32) []byte {
    33  	bytesBuffer := bytes.NewBuffer([]byte{})
    34  	err := binary.Write(bytesBuffer, binary.LittleEndian, x)
    35  	if err != nil {
    36  		return nil
    37  	}
    38  	return bytesBuffer.Bytes()
    39  }
    40  
    41  // Int64ToBytes int64 to le bytes, little endian
    42  func Int64ToBytes(x int64) ([]byte, error) {
    43  	bytesBuffer := bytes.NewBuffer([]byte{})
    44  	err := binary.Write(bytesBuffer, binary.LittleEndian, x)
    45  	if err != nil {
    46  		return nil, err
    47  	}
    48  	return bytesBuffer.Bytes(), nil
    49  }
    50  
    51  // BytesToUint64 le bytes to uint64, little endian
    52  func BytesToUint64(b []byte) (uint64, error) {
    53  	bytesBuffer := bytes.NewBuffer(b)
    54  	var x uint64
    55  	err := binary.Read(bytesBuffer, binary.LittleEndian, &x)
    56  	if err != nil {
    57  		return 0, err
    58  	}
    59  	return x, nil
    60  }
    61  
    62  // Uint64ToBytes uint64 to le bytes, little endian
    63  func Uint64ToBytes(x uint64) ([]byte, error) {
    64  	bytesBuffer := bytes.NewBuffer([]byte{})
    65  	err := binary.Write(bytesBuffer, binary.LittleEndian, x)
    66  	if err != nil {
    67  		return nil, err
    68  	}
    69  	return bytesBuffer.Bytes(), nil
    70  }
    71  
    72  // BytesPrefix returns key range that satisfy the given prefix.
    73  // This only applicable for the standard 'bytes comparer'.
    74  func BytesPrefix(prefix []byte) ([]byte, []byte) {
    75  	var limit []byte
    76  	for i := len(prefix) - 1; i >= 0; i-- {
    77  		c := prefix[i]
    78  		if c < 0xff {
    79  			limit = make([]byte, i+1)
    80  			copy(limit, prefix)
    81  			limit[i] = c + 1
    82  			break
    83  		}
    84  	}
    85  	return prefix, limit
    86  }
    87  
    88  // StringToBytes converts string to byte slice without a memory allocation.
    89  func StringToBytes(s string) []byte {
    90  	return *(*[]byte)(unsafe.Pointer(
    91  		&struct {
    92  			string
    93  			Cap int
    94  		}{s, len(s)},
    95  	))
    96  }
    97  
    98  // BytesToString converts byte slice to string without a memory allocation.
    99  func BytesToString(b []byte) string {
   100  	return *(*string)(unsafe.Pointer(&b))
   101  }