Skip to content

Commit 1cd5bf0

Browse files
lmittmannfjl
authored andcommitted
common: unify hex prefix check code (ethereum#19937)
1 parent 7c22994 commit 1cd5bf0

File tree

2 files changed

+5
-7
lines changed

2 files changed

+5
-7
lines changed

common/bytes.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,8 @@ func ToHexArray(b [][]byte) []string {
4343
// FromHex returns the bytes represented by the hexadecimal string s.
4444
// s may be prefixed with "0x".
4545
func FromHex(s string) []byte {
46-
if len(s) > 1 {
47-
if s[0:2] == "0x" || s[0:2] == "0X" {
48-
s = s[2:]
49-
}
46+
if has0xPrefix(s) {
47+
s = s[2:]
5048
}
5149
if len(s)%2 == 1 {
5250
s = "0" + s
@@ -65,8 +63,8 @@ func CopyBytes(b []byte) (copiedBytes []byte) {
6563
return
6664
}
6765

68-
// hasHexPrefix validates str begins with '0x' or '0X'.
69-
func hasHexPrefix(str string) bool {
66+
// has0xPrefix validates str begins with '0x' or '0X'.
67+
func has0xPrefix(str string) bool {
7068
return len(str) >= 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')
7169
}
7270

common/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func HexToAddress(s string) Address { return BytesToAddress(FromHex(s)) }
193193
// IsHexAddress verifies whether a string can represent a valid hex-encoded
194194
// Ethereum address or not.
195195
func IsHexAddress(s string) bool {
196-
if hasHexPrefix(s) {
196+
if has0xPrefix(s) {
197197
s = s[2:]
198198
}
199199
return len(s) == 2*AddressLength && isHex(s)

0 commit comments

Comments
 (0)