Skip to content

Commit

Permalink
limit pattern size
Browse files Browse the repository at this point in the history
  • Loading branch information
apetruhin committed Sep 11, 2024
1 parent be9523a commit db415a0
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
)

const (
minWordLen = 2
patternMaxDiff = 1
patternCheckFirstN = 100
patternMaxWords = 100
patterMinWordLen = 2
patternMaxDiff = 1
)

var (
Expand Down Expand Up @@ -57,9 +57,6 @@ func (p *Pattern) WeakEqual(other *Pattern) bool {
}
var diffs int
for i := range other.words {
if i > patternCheckFirstN {
return true
}
if p.words[i] != other.words[i] {
diffs++
if diffs > patternMaxDiff {
Expand All @@ -74,20 +71,28 @@ func NewPattern(input string) *Pattern {
pattern := &Pattern{}
for _, p := range strings.Fields(removeQuotedAndBrackets(input)) {
p = strings.TrimRight(p, "=:],;")
if len(p) < minWordLen {
if len(p) < patterMinWordLen {
continue
}
if hex.MatchString(p) || uuid.MatchString(p) {
continue
}
p = removeDigits(p)
if isWord(p) {
pattern.words = append(pattern.words, p)
if !isWord(p) {
continue
}
pattern.words = append(pattern.words, p)
if len(pattern.words) >= patternMaxWords {
break
}
}
return pattern
}

func NewPatternFromWords(input string) *Pattern {
return &Pattern{words: strings.Split(input, " ")}
}

// like regexp match to `^[a-zA-Z][a-zA-Z._-]*[a-zA-Z]$`, but much faster
func isWord(s string) bool {
l := len(s) - 1
Expand Down

0 comments on commit db415a0

Please sign in to comment.