File tree Expand file tree Collapse file tree 1 file changed +28
-1
lines changed
Expand file tree Collapse file tree 1 file changed +28
-1
lines changed Original file line number Diff line number Diff line change @@ -2,11 +2,18 @@ package search
22
33import (
44 "regexp"
5+ "strings"
56 "unicode"
67)
78
89type 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
2734func (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
You can’t perform that action at this time.
0 commit comments