mirror of
https://github.com/fluencelabs/tendermint
synced 2025-07-03 22:51:37 +00:00
Merge pull request #142 from tendermint/ishex-fragility
common: IsHex should be able to handle 0X prefixed strings
This commit is contained in:
@ -29,7 +29,7 @@ func LeftPadString(s string, totalLength int) string {
|
|||||||
|
|
||||||
// IsHex returns true for non-empty hex-string prefixed with "0x"
|
// IsHex returns true for non-empty hex-string prefixed with "0x"
|
||||||
func IsHex(s string) bool {
|
func IsHex(s string) bool {
|
||||||
if len(s) > 2 && s[:2] == "0x" {
|
if len(s) > 2 && strings.EqualFold(s[:2], "0x") {
|
||||||
_, err := hex.DecodeString(s[2:])
|
_, err := hex.DecodeString(s[2:])
|
||||||
return err == nil
|
return err == nil
|
||||||
}
|
}
|
||||||
|
@ -12,3 +12,21 @@ func TestStringInSlice(t *testing.T) {
|
|||||||
assert.True(t, StringInSlice("", []string{""}))
|
assert.True(t, StringInSlice("", []string{""}))
|
||||||
assert.False(t, StringInSlice("", []string{}))
|
assert.False(t, StringInSlice("", []string{}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIsHex(t *testing.T) {
|
||||||
|
notHex := []string{
|
||||||
|
"", " ", "a", "x", "0", "0x", "0X", "0x ", "0X ", "0X a",
|
||||||
|
"0xf ", "0x f", "0xp", "0x-",
|
||||||
|
"0xf", "0XBED", "0xF", "0xbed", // Odd lengths
|
||||||
|
}
|
||||||
|
for _, v := range notHex {
|
||||||
|
assert.False(t, IsHex(v), "%q is not hex", v)
|
||||||
|
}
|
||||||
|
hex := []string{
|
||||||
|
"0x00", "0x0a", "0x0F", "0xFFFFFF", "0Xdeadbeef", "0x0BED",
|
||||||
|
"0X12", "0X0A",
|
||||||
|
}
|
||||||
|
for _, v := range hex {
|
||||||
|
assert.True(t, IsHex(v), "%q is hex", v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user