diff --git a/guards/guards.go b/guards/guards.go index 4470db8da..ad27599a9 100644 --- a/guards/guards.go +++ b/guards/guards.go @@ -6,7 +6,6 @@ import ( "github.com/iotaledger/iota.go/curl" . "github.com/iotaledger/iota.go/trinary" - "regexp" ) // IsTrytes checks if input is correct trytes consisting of [9A-Z] @@ -14,17 +13,25 @@ func IsTrytes(trytes Trytes) bool { if len(trytes) < 1 { return false } - match, _ := regexp.MatchString("^[9A-Z]+$", string(trytes)) - return match + for _, runeVal := range trytes { + if (runeVal < 'A' || runeVal > 'Z') && runeVal != '9' { + return false + } + } + return true } // IsTrytesOfExactLength checks if input is correct trytes consisting of [9A-Z] and given length func IsTrytesOfExactLength(trytes Trytes, length int) bool { - if len(trytes) != length { + if len(trytes) != length || len(trytes) == 0 { return false } - match, _ := regexp.MatchString("^[9A-Z]+$", string(trytes)) - return match + for _, runeVal := range trytes { + if (runeVal < 'A' || runeVal > 'Z') && runeVal != '9' { + return false + } + } + return true } // IsTrytesOfMaxLength checks if input is correct trytes consisting of [9A-Z] and length <= maxLength @@ -32,15 +39,23 @@ func IsTrytesOfMaxLength(trytes Trytes, max int) bool { if len(trytes) > max || len(trytes) < 1 { return false } - match, _ := regexp.MatchString("^[9A-Z]+$", string(trytes)) - return match + for _, runeVal := range trytes { + if (runeVal < 'A' || runeVal > 'Z') && runeVal != '9' { + return false + } + } + return true } -var onlyNinesRegex = regexp.MustCompile("^[9]+$") - // IsEmptyTrytes checks if input is null (all 9s trytes) func IsEmptyTrytes(trytes Trytes) bool { - return onlyNinesRegex.MatchString(string(trytes)) + for _, runeVal := range trytes { + if runeVal != '9' { + return false + } + } + + return true } // IsHash checks if input is correct hash (81 trytes or 90)