@@ -8,6 +8,10 @@ import (
8
8
"regexp"
9
9
)
10
10
11
+ var (
12
+ regexes = make (map [string ]* regexp.Regexp )
13
+ )
14
+
11
15
// A Hound contains the local configuration filename and all regexp patterns
12
16
// used for sniffing git-diffs.
13
17
type Hound struct {
@@ -38,13 +42,13 @@ func (h *Hound) New() bool {
38
42
func (h * Hound ) Sniff (fileName string , hunk * diff.Hunk , smells chan <- smell , done chan <- bool ) {
39
43
defer func () { done <- true }()
40
44
41
- rxFileName , _ := regexp . Compile (`^\w+\/` )
45
+ rxFileName , _ := h . regexp (`^\w+\/` )
42
46
fileName = rxFileName .ReplaceAllString (fileName , "" )
43
47
if _ , ok := h .matchPatterns (h .Skips , []byte (fileName )); ok {
44
48
return
45
49
}
46
50
47
- rxModLines , _ := regexp . Compile (`(?m)^\+\s*(.+)$` )
51
+ rxModLines , _ := h . regexp (`(?m)^\+\s*(.+)$` )
48
52
matches := rxModLines .FindAllSubmatch (hunk .Body , - 1 )
49
53
50
54
for _ , match := range matches {
@@ -84,9 +88,26 @@ func (h *Hound) parse(config []byte) error {
84
88
return yaml .Unmarshal (config , h )
85
89
}
86
90
91
+ // getRegexp looks for the specified pattern in Hound's regexes cache, and if
92
+ // it is available, it will fetch from it. If it is not available, it
93
+ // will compile the pattern and store it in the cache. Returns a Regexp
94
+ // and an error.
95
+ func (h * Hound ) regexp (pattern string ) (* regexp.Regexp , error ) {
96
+ if regexes [pattern ] != nil {
97
+ return regexes [pattern ], nil
98
+ }
99
+
100
+ r , err := regexp .Compile (pattern )
101
+ if err == nil {
102
+ regexes [pattern ] = r
103
+ }
104
+
105
+ return r , err
106
+ }
107
+
87
108
// match matches a byte array against a regexp pattern and returns a bool.
88
109
func (h * Hound ) match (pattern string , subject []byte ) bool {
89
- r , err := regexp . Compile (pattern )
110
+ r , err := h . regexp (pattern )
90
111
if err != nil {
91
112
panic (err )
92
113
}
0 commit comments