-
Notifications
You must be signed in to change notification settings - Fork 157
Expand file tree
/
Copy pathregex_cache.go
More file actions
37 lines (33 loc) · 1.07 KB
/
Copy pathregex_cache.go
File metadata and controls
37 lines (33 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package jid
import (
"regexp"
"strconv"
"sync"
)
// reCache memoizes successful regexp compiles for patterns that are built
// from user input on every keystroke (e.g. "(?i)^" + keyword). Growth is
// bounded by the distinct patterns typed in one interactive session.
var reCache sync.Map // string -> *regexp.Regexp
// compileCached is a drop-in replacement for regexp.Compile in hot paths.
// Failed compiles are not cached, so error behavior is identical to
// regexp.Compile.
func compileCached(pattern string) (*regexp.Regexp, error) {
if v, ok := reCache.Load(pattern); ok {
return v.(*regexp.Regexp), nil
}
re, err := regexp.Compile(pattern)
if err != nil {
return nil, err
}
reCache.Store(pattern, re)
return re, nil
}
// mustCompileCached is a drop-in replacement for regexp.MustCompile in hot
// paths: it panics on invalid patterns, matching MustCompile behavior.
func mustCompileCached(pattern string) *regexp.Regexp {
re, err := compileCached(pattern)
if err != nil {
panic(`regexp: Compile(` + strconv.Quote(pattern) + `): ` + err.Error())
}
return re
}