mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-17 02:10:46 +00:00
common: add public method to check for valid hex string
This commit is contained in:
parent
7a659c9e7d
commit
47d4bf4bf6
1 changed files with 25 additions and 0 deletions
|
|
@ -185,6 +185,31 @@ func EncodeBig(bigint *big.Int) string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add more comprehensive check to validate a hex string
|
||||||
|
func IsValidHexString(input string) bool {
|
||||||
|
if !has0xPrefix(input) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
hexPart := input[2:]
|
||||||
|
if len(hexPart) == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, c := range hexPart {
|
||||||
|
if !isHexChar(c) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func isHexChar(c rune) bool {
|
||||||
|
return ('0' <= c && c <= '9') ||
|
||||||
|
('a' <= c && c <= 'f') ||
|
||||||
|
('A' <= c && c <= 'F')
|
||||||
|
}
|
||||||
|
|
||||||
func has0xPrefix(input string) bool {
|
func has0xPrefix(input string) bool {
|
||||||
return len(input) >= 2 && input[0] == '0' && (input[1] == 'x' || input[1] == 'X')
|
return len(input) >= 2 && input[0] == '0' && (input[1] == 'x' || input[1] == 'X')
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue