Debian Codegraph @

lib/parse/match.go

raw
1 package parse
2
3 import (
4 "regexp"
5 "sync"
6
7 "git.sr.ht/~rjarry/aerc/lib/log"
8 )
9
10 var reCache sync.Map
11
12 // Check if a string matches the specified regular expression.
13 // The regexp is compiled only once and stored in a cache for future use.
14 func MatchCache(s, expr string) bool {
15 var re any
16 var found bool
17
18 if re, found = reCache.Load(expr); !found {
19 var err error
20 re, err = regexp.Compile(expr)
21 if err != nil {
22 log.Errorf("`%s` invalid regexp: %s", expr, err)
23 }
24 reCache.Store(expr, re)
25 }
26 if re, ok := re.(*regexp.Regexp); ok && re != nil {
27 return re.MatchString(s)
28 }
29 return false
30 }