Skip to content

Commit

Permalink
regexp: Replace use of MustCompile and MatchString with function hasD…
Browse files Browse the repository at this point in the history
…igit
  • Loading branch information
peterhellberg committed Aug 24, 2023
1 parent 9fdaa7e commit 4e29f28
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions sqids.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"math"
"regexp"
"strings"
)

Expand Down Expand Up @@ -332,15 +331,14 @@ func contains(slice []string, str string) bool {

func (s *Sqids) isBlockedID(id string) bool {
id = strings.ToLower(id)
r := regexp.MustCompile(`\d`)

for _, word := range s.blocklist {
if len(word) <= len(id) {
if len(id) <= 3 || len(word) <= 3 {
if id == word {
return true
}
} else if r.MatchString(word) {
} else if hasDigit(word) {
if strings.HasPrefix(id, word) || strings.HasSuffix(id, word) {
return true
}
Expand All @@ -352,3 +350,13 @@ func (s *Sqids) isBlockedID(id string) bool {

return false
}

func hasDigit(word string) bool {
for _, r := range word {
if r >= '0' && r <= '9' {
return true
}
}

return false
}

0 comments on commit 4e29f28

Please sign in to comment.