Skip to content

Commit 3cf9102

Browse files
committed
Improve case sensitive substring search performance
1 parent c59ecf6 commit 3cf9102

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

internal/search/search.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@ package search
22

33
import (
44
"regexp"
5+
"strings"
56
"unicode"
67
)
78

89
type Search struct {
9-
findMe string
10+
findMe string
11+
12+
// If this is false it means the input has to be interpreted as a regexp.
13+
isSubstringSearch bool
14+
15+
hasUppercase bool
16+
1017
pattern *regexp.Regexp
1118
}
1219

@@ -26,6 +33,20 @@ func For(s string) Search {
2633

2734
func (search *Search) For(s string) {
2835
search.findMe = s
36+
37+
hasSpecialChars := regexp.QuoteMeta(s) != s
38+
_, err := regexp.Compile(s)
39+
isValidRegexp := err == nil
40+
search.isSubstringSearch = !hasSpecialChars || !isValidRegexp
41+
42+
search.hasUppercase = false
43+
for _, char := range s {
44+
if unicode.IsUpper(char) {
45+
search.hasUppercase = true
46+
break
47+
}
48+
}
49+
2950
search.pattern = toPattern(s)
3051
}
3152

@@ -46,6 +67,12 @@ func (search Search) Matches(line string) bool {
4667
if search.findMe == "" {
4768
return false
4869
}
70+
71+
if search.isSubstringSearch && search.hasUppercase {
72+
// Case sensitive substring search
73+
return strings.Contains(line, search.findMe)
74+
}
75+
4976
return search.pattern.MatchString(line)
5077
}
5178

0 commit comments

Comments
 (0)