clean up utils and add test

pull/219/head
Minh Doan 6 years ago committed by Minh Doan
parent cbe4764d44
commit 5daf5964b2
  1. 47
      internal/utils/bytes.go
  2. 17
      internal/utils/bytes_test.go

@ -2,18 +2,6 @@ package utils
import "encoding/hex" import "encoding/hex"
// ToHex returns the hex representation of b, prefixed with '0x'.
// For empty slices, the return value is "0x0".
//
// Deprecated: use hexutil.Encode instead.
func ToHex(b []byte) string {
hex := Bytes2Hex(b)
if len(hex) == 0 {
hex = "0"
}
return "0x" + hex
}
// FromHex returns the bytes represented by the hexadecimal string s. // FromHex returns the bytes represented by the hexadecimal string s.
// s may be prefixed with "0x". // s may be prefixed with "0x".
func FromHex(s string) []byte { func FromHex(s string) []byte {
@ -39,11 +27,6 @@ func CopyBytes(b []byte) (copiedBytes []byte) {
return return
} }
// hasHexPrefix validates str begins with '0x' or '0X'.
func hasHexPrefix(str string) bool {
return len(str) >= 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')
}
// isHexCharacter returns bool of c being a valid hexadecimal. // isHexCharacter returns bool of c being a valid hexadecimal.
func isHexCharacter(c byte) bool { func isHexCharacter(c byte) bool {
return ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F') return ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')
@ -62,31 +45,12 @@ func isHex(str string) bool {
return true return true
} }
// Bytes2Hex returns the hexadecimal encoding of d.
func Bytes2Hex(d []byte) string {
return hex.EncodeToString(d)
}
// Hex2Bytes returns the bytes represented by the hexadecimal string str. // Hex2Bytes returns the bytes represented by the hexadecimal string str.
func Hex2Bytes(str string) []byte { func Hex2Bytes(str string) []byte {
h, _ := hex.DecodeString(str) h, _ := hex.DecodeString(str)
return h return h
} }
// Hex2BytesFixed returns bytes of a specified fixed length flen.
func Hex2BytesFixed(str string, flen int) []byte {
h, _ := hex.DecodeString(str)
if len(h) == flen {
return h
}
if len(h) > flen {
return h[len(h)-flen:]
}
hh := make([]byte, flen)
copy(hh[flen-len(h):flen], h[:])
return hh
}
// RightPadBytes zero-pads slice to the right up to length l. // RightPadBytes zero-pads slice to the right up to length l.
func RightPadBytes(slice []byte, l int) []byte { func RightPadBytes(slice []byte, l int) []byte {
if l <= len(slice) { if l <= len(slice) {
@ -110,14 +74,3 @@ func LeftPadBytes(slice []byte, l int) []byte {
return padded return padded
} }
// Get32BytesFromString parses the string representation of hex into 32 byte array
func Get32BytesFromString(hashString string) ([32]byte, error) {
bytes, err := hex.DecodeString(hashString)
if err != nil {
return [32]byte{}, err
}
bytesArray := [32]byte{}
copy(bytesArray[:], bytes)
return bytesArray, nil
}

@ -87,3 +87,20 @@ func TestNoPrefixShortHexOddLength(t *testing.T) {
t.Errorf("Expected %x got %x", expected, result) t.Errorf("Expected %x got %x", expected, result)
} }
} }
func TestCopyBytes(t *testing.T) {
expected := []byte{1, 2, 3}
result := CopyBytes(expected)
if !bytes.Equal(expected, result) {
t.Errorf("Expected %x got %x", expected, result)
}
expected[0] = 0
if result[0] == 0 {
t.Errorf("should not be 0")
}
}
// hasHexPrefix validates str begins with '0x' or '0X'.
func hasHexPrefix(str string) bool {
return len(str) >= 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')
}

Loading…
Cancel
Save