mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-17 09:23:48 +00:00
common: fixup IsHexAddress
Also unexport IsHex, HasHexPrefix because IsHexAddress is the only caller.
This commit is contained in:
parent
84d6d87ff5
commit
1ce722c919
3 changed files with 29 additions and 36 deletions
|
|
@ -53,25 +53,19 @@ func CopyBytes(b []byte) (copiedBytes []byte) {
|
|||
return
|
||||
}
|
||||
|
||||
func HasHexPrefix(str string) bool {
|
||||
l := len(str)
|
||||
return l >= 2 && (str[0:2] == "0x" || str[0:2] == "0X")
|
||||
func hasHexPrefix(str string) bool {
|
||||
return len(str) >= 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')
|
||||
}
|
||||
|
||||
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')
|
||||
}
|
||||
|
||||
func IsHex(str string) bool {
|
||||
if HasHexPrefix(str) {
|
||||
str = str[2:]
|
||||
}
|
||||
func isHex(str string) bool {
|
||||
if len(str)%2 != 0 {
|
||||
return false
|
||||
}
|
||||
for _, c := range []byte(src) {
|
||||
for _, c := range []byte(str) {
|
||||
if !isHexCharacter(c) {
|
||||
return false
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,28 +34,6 @@ func (s *BytesSuite) TestCopyBytes(c *checker.C) {
|
|||
c.Assert(res1, checker.DeepEquals, exp1)
|
||||
}
|
||||
|
||||
func (s *BytesSuite) TestIsHex(c *checker.C) {
|
||||
data1 := "a9e67e"
|
||||
exp1 := true
|
||||
res1 := IsHex(data1)
|
||||
c.Assert(res1, checker.DeepEquals, exp1)
|
||||
|
||||
data2 := "0xa9e67e00"
|
||||
exp2 := true
|
||||
res2 := IsHex(data2)
|
||||
c.Assert(res2, checker.DeepEquals, exp2)
|
||||
|
||||
data3 := "0xa9e67e001"
|
||||
exp3 := false
|
||||
res3 := IsHex(data3)
|
||||
c.Assert(res3, checker.DeepEquals, exp3)
|
||||
|
||||
data4 := "0xHELLO_MY_NAME_IS_STEVEN_@#$^&*"
|
||||
exp4 := false
|
||||
res4 := IsHex(data4)
|
||||
c.Assert(res4, checker.DeepEquals, exp4)
|
||||
}
|
||||
|
||||
func (s *BytesSuite) TestLeftPadBytes(c *checker.C) {
|
||||
val1 := []byte{1, 2, 3, 4}
|
||||
exp1 := []byte{0, 0, 0, 0, 1, 2, 3, 4}
|
||||
|
|
@ -87,6 +65,27 @@ func TestFromHex(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestIsHex(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
ok bool
|
||||
}{
|
||||
{"", true},
|
||||
{"0", false},
|
||||
{"00", true},
|
||||
{"a9e67e", true},
|
||||
{"A9E67E", true},
|
||||
{"0xa9e67e", false},
|
||||
{"a9e67e001", false},
|
||||
{"0xHELLO_MY_NAME_IS_STEVEN_@#$^&*", false},
|
||||
}
|
||||
for _, test := range tests {
|
||||
if ok := isHex(test.input); ok != test.ok {
|
||||
t.Error("isHex(%q) = %v, want %v", test.input, ok, test.ok)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFromHexOddLength(t *testing.T) {
|
||||
input := "0x1"
|
||||
expected := []byte{1}
|
||||
|
|
|
|||
|
|
@ -150,10 +150,10 @@ func HexToAddress(s string) Address { return BytesToAddress(FromHex(s)) }
|
|||
// IsHexAddress verifies whether a string can represent a valid hex-encoded
|
||||
// Ethereum address or not.
|
||||
func IsHexAddress(s string) bool {
|
||||
if HasHexPrefix(s) {
|
||||
return len(s) == 2+2*AddressLength && IsHex(s[2:])
|
||||
if hasHexPrefix(s) {
|
||||
s = s[2:]
|
||||
}
|
||||
return len(s) == 2*AddressLength && IsHex(s)
|
||||
return len(s) == 2*AddressLength && isHex(s)
|
||||
}
|
||||
|
||||
// Get the string representation of the underlying address
|
||||
|
|
|
|||
Loading…
Reference in a new issue