Skip to content

Commit db415a0

Browse files
committed
limit pattern size
1 parent be9523a commit db415a0

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

pattern.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import (
99
)
1010

1111
const (
12-
minWordLen = 2
13-
patternMaxDiff = 1
14-
patternCheckFirstN = 100
12+
patternMaxWords = 100
13+
patterMinWordLen = 2
14+
patternMaxDiff = 1
1515
)
1616

1717
var (
@@ -57,9 +57,6 @@ func (p *Pattern) WeakEqual(other *Pattern) bool {
5757
}
5858
var diffs int
5959
for i := range other.words {
60-
if i > patternCheckFirstN {
61-
return true
62-
}
6360
if p.words[i] != other.words[i] {
6461
diffs++
6562
if diffs > patternMaxDiff {
@@ -74,20 +71,28 @@ func NewPattern(input string) *Pattern {
7471
pattern := &Pattern{}
7572
for _, p := range strings.Fields(removeQuotedAndBrackets(input)) {
7673
p = strings.TrimRight(p, "=:],;")
77-
if len(p) < minWordLen {
74+
if len(p) < patterMinWordLen {
7875
continue
7976
}
8077
if hex.MatchString(p) || uuid.MatchString(p) {
8178
continue
8279
}
8380
p = removeDigits(p)
84-
if isWord(p) {
85-
pattern.words = append(pattern.words, p)
81+
if !isWord(p) {
82+
continue
83+
}
84+
pattern.words = append(pattern.words, p)
85+
if len(pattern.words) >= patternMaxWords {
86+
break
8687
}
8788
}
8889
return pattern
8990
}
9091

92+
func NewPatternFromWords(input string) *Pattern {
93+
return &Pattern{words: strings.Split(input, " ")}
94+
}
95+
9196
// like regexp match to `^[a-zA-Z][a-zA-Z._-]*[a-zA-Z]$`, but much faster
9297
func isWord(s string) bool {
9398
l := len(s) - 1

0 commit comments

Comments
 (0)