mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-17 18:30:45 +00:00
common: unify hex prefix check code (#19937)
This commit is contained in:
parent
94b69fdf1e
commit
7f63a690ee
2 changed files with 19 additions and 25 deletions
|
|
@ -34,13 +34,10 @@ func ToHex(b []byte) string {
|
||||||
// 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 {
|
||||||
if len(s) > 1 {
|
if has0xPrefix(s) {
|
||||||
if s[0:2] == "0x" || s[0:2] == "0X" {
|
s = s[2:]
|
||||||
s = s[2:]
|
} else if hasXdcPrefix(s) {
|
||||||
}
|
s = s[3:]
|
||||||
if (s[0] == 'x' || s[0] == 'X') && (s[1] == 'd' || s[1] == 'D') && (s[2] == 'c' || s[2] == 'C') {
|
|
||||||
s = s[3:]
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if len(s)%2 == 1 {
|
if len(s)%2 == 1 {
|
||||||
s = "0" + s
|
s = "0" + s
|
||||||
|
|
@ -59,14 +56,14 @@ func CopyBytes(b []byte) (copiedBytes []byte) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// hasXDCPrefix validates str begins with 'xdc' or 'XDC'.
|
// has0xPrefix validates str begins with '0x' or '0X'.
|
||||||
func hasXDCPrefix(str string) bool {
|
func has0xPrefix(str string) bool {
|
||||||
return len(str) >= 3 && (str[0] == 'x' || str[0] == 'X') && (str[1] == 'd' || str[1] == 'D') && (str[2] == 'c' || str[2] == 'C')
|
return len(str) >= 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')
|
||||||
}
|
}
|
||||||
|
|
||||||
// hasHexPrefix validates str begins with '0x' or '0X'.
|
// hasXdcPrefix validates s begins with 'xdc' or 'XDC'.
|
||||||
func hasHexPrefix(str string) bool {
|
func hasXdcPrefix(s string) bool {
|
||||||
return len(str) >= 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')
|
return len(s) >= 3 && (s[0] == 'x' || s[0] == 'X') && (s[1] == 'd' || s[1] == 'D') && (s[2] == 'c' || s[2] == 'C')
|
||||||
}
|
}
|
||||||
|
|
||||||
// isHexCharacter returns bool of c being a valid hexadecimal.
|
// isHexCharacter returns bool of c being a valid hexadecimal.
|
||||||
|
|
@ -103,15 +100,13 @@ func Hex2BytesFixed(str string, flen int) []byte {
|
||||||
h, _ := hex.DecodeString(str)
|
h, _ := hex.DecodeString(str)
|
||||||
if len(h) == flen {
|
if len(h) == flen {
|
||||||
return h
|
return h
|
||||||
} else {
|
|
||||||
if len(h) > flen {
|
|
||||||
return h[len(h)-flen:]
|
|
||||||
} else {
|
|
||||||
hh := make([]byte, flen)
|
|
||||||
copy(hh[flen-len(h):flen], h[:])
|
|
||||||
return hh
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
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.
|
||||||
|
|
|
||||||
|
|
@ -209,11 +209,10 @@ func HexToAddress(s string) Address { return BytesToAddress(FromHex(s)) }
|
||||||
// IsHexAddress verifies whether a string can represent a valid hex-encoded
|
// IsHexAddress verifies whether a string can represent a valid hex-encoded
|
||||||
// Ethereum address or not.
|
// Ethereum address or not.
|
||||||
func IsHexAddress(s string) bool {
|
func IsHexAddress(s string) bool {
|
||||||
if hasXDCPrefix(s) {
|
if has0xPrefix(s) {
|
||||||
s = s[3:]
|
|
||||||
}
|
|
||||||
if hasHexPrefix(s) {
|
|
||||||
s = s[2:]
|
s = s[2:]
|
||||||
|
} else if hasXdcPrefix(s) {
|
||||||
|
s = s[3:]
|
||||||
}
|
}
|
||||||
return len(s) == 2*AddressLength && isHex(s)
|
return len(s) == 2*AddressLength && isHex(s)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue