From 01a4b581fab095fdc35788d0a81c06d694c31c6f Mon Sep 17 00:00:00 2001 From: zelig Date: Fri, 27 Nov 2015 10:59:43 +0000 Subject: [PATCH] fix address checking used in CLI --- common/bytes.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/common/bytes.go b/common/bytes.go index ba6987a9ec..c6e2cd3127 100644 --- a/common/bytes.go +++ b/common/bytes.go @@ -23,9 +23,14 @@ import ( "encoding/hex" "fmt" "math/big" + "regexp" "strings" ) +var ( + hexRe = regexp.MustCompile("^(0x)?([a-fA-f0-9]{2})+$") +) + func ToHex(b []byte) string { hex := Bytes2Hex(b) // Prefer output of "0x0" instead of "0x" @@ -139,8 +144,14 @@ func HasHexPrefix(str string) bool { } func IsHex(str string) bool { - l := len(str) - return l >= 4 && l%2 == 0 && str[0:2] == "0x" + return hexRe.MatchString(str) +} + +func NormaliseHex(str string) (string, bool) { + if HasHexPrefix(str) { + str = str[2:] + } + return str, IsHex(str) } func Bytes2Hex(d []byte) string { @@ -202,8 +213,8 @@ func ParseData(data ...interface{}) (ret []byte) { switch t := item.(type) { case string: var str []byte - if IsHex(t) { - str = Hex2Bytes(t[2:]) + if n, ok := NormaliseHex(t); ok { + str = Hex2Bytes(n) } else { str = []byte(t) }