Skip to content

Commit

Permalink
got rid of regexp in guards (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardoaraujor authored and luca-moser committed Nov 14, 2019
1 parent ee9fd46 commit 827705f
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions guards/guards.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,56 @@ 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]
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
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)
Expand Down

0 comments on commit 827705f

Please sign in to comment.