Skip to content

Commit c36fc32

Browse files
authored
Merge pull request #13 from jamaks/speg-vis-exp
feat: create optimization
2 parents 9d1e26d + b114b8a commit c36fc32

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

rd/rd_parser.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,21 @@ func String(rule string) ParserFunc {
178178
}
179179
}
180180

181+
var regexMap = make(map[string]*regexp.Regexp)
182+
183+
func getRegex(rule string) *regexp.Regexp {
184+
if regexMap[rule] == nil {
185+
r, _ := regexp.Compile(rule)
186+
regexMap[rule] = r
187+
}
188+
189+
return regexMap[rule]
190+
}
191+
181192
func RegexChar(rule string) ParserFunc {
182193
return func(state *State) (Ast, bool) {
183194
text := state.Text[state.Position:]
184-
r, _ := regexp.Compile(rule)
195+
r := getRegex(rule)
185196
loc := r.FindStringIndex(text)
186197

187198
if len(loc) > 0 && loc[0] == 0 {

0 commit comments

Comments
 (0)