From 5daf5964b2e880eaa0e405b6547601c411029b42 Mon Sep 17 00:00:00 2001 From: Minh Doan Date: Fri, 4 Jan 2019 13:46:52 -0800 Subject: [PATCH] clean up utils and add test --- internal/utils/bytes.go | 47 ------------------------------------ internal/utils/bytes_test.go | 17 +++++++++++++ 2 files changed, 17 insertions(+), 47 deletions(-) diff --git a/internal/utils/bytes.go b/internal/utils/bytes.go index fecc34172..0549cb682 100644 --- a/internal/utils/bytes.go +++ b/internal/utils/bytes.go @@ -2,18 +2,6 @@ package utils 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. // s may be prefixed with "0x". func FromHex(s string) []byte { @@ -39,11 +27,6 @@ func CopyBytes(b []byte) (copiedBytes []byte) { 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. func isHexCharacter(c byte) bool { return ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F') @@ -62,31 +45,12 @@ func isHex(str string) bool { 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. func Hex2Bytes(str string) []byte { h, _ := hex.DecodeString(str) 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. func RightPadBytes(slice []byte, l int) []byte { if l <= len(slice) { @@ -110,14 +74,3 @@ func LeftPadBytes(slice []byte, l int) []byte { 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 -} diff --git a/internal/utils/bytes_test.go b/internal/utils/bytes_test.go index 10f73739c..d752cfec0 100644 --- a/internal/utils/bytes_test.go +++ b/internal/utils/bytes_test.go @@ -87,3 +87,20 @@ func TestNoPrefixShortHexOddLength(t *testing.T) { 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') +}