From 295f6c2cc60f290e4e7968a9d0a265de010d9238 Mon Sep 17 00:00:00 2001 From: rigel rozanski Date: Mon, 5 Jun 2017 15:50:11 -0400 Subject: [PATCH 1/2] IsHex and StripHex --- common/string.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/common/string.go b/common/string.go index a4d221b74..f053f0e87 100644 --- a/common/string.go +++ b/common/string.go @@ -1,6 +1,7 @@ package common import ( + "encoding/hex" "fmt" "strings" ) @@ -22,3 +23,22 @@ func LeftPadString(s string, totalLength int) string { } return s } + +// Returns true for non-empty hex-string prefixed with "0x" +func IsHex(s string) bool { + if len(s) > 2 && s[:2] == "0x" { + _, err := hex.DecodeString(s[2:]) + if err != nil { + return false + } + return true + } + return false +} + +func StripHex(s string) string { + if IsHex(s) { + return s[2:] + } + return s +} From 925f2b33504bc2f19932be1d474ade0c33619c96 Mon Sep 17 00:00:00 2001 From: rigel rozanski Date: Mon, 5 Jun 2017 16:22:01 -0400 Subject: [PATCH 2/2] golint corrections --- common/string.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/common/string.go b/common/string.go index f053f0e87..2818f5ed5 100644 --- a/common/string.go +++ b/common/string.go @@ -6,8 +6,10 @@ import ( "strings" ) +// Fmt shorthand, XXX DEPRECATED var Fmt = fmt.Sprintf +// RightPadString adds spaces to the right of a string to make it length totalLength func RightPadString(s string, totalLength int) string { remaining := totalLength - len(s) if remaining > 0 { @@ -16,6 +18,7 @@ func RightPadString(s string, totalLength int) string { return s } +// LeftPadString adds spaces to the left of a string to make it length totalLength func LeftPadString(s string, totalLength int) string { remaining := totalLength - len(s) if remaining > 0 { @@ -24,7 +27,7 @@ func LeftPadString(s string, totalLength int) string { return s } -// 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 { if len(s) > 2 && s[:2] == "0x" { _, err := hex.DecodeString(s[2:]) @@ -36,6 +39,7 @@ func IsHex(s string) bool { return false } +// StripHex returns hex string without leading "0x" func StripHex(s string) string { if IsHex(s) { return s[2:]