diff options
Diffstat (limited to 'lib/parse/match.go')
-rw-r--r-- | lib/parse/match.go | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/lib/parse/match.go b/lib/parse/match.go new file mode 100644 index 00000000..ac43673e --- /dev/null +++ b/lib/parse/match.go @@ -0,0 +1,30 @@ +package parse + +import ( + "regexp" + "sync" + + "git.sr.ht/~rjarry/aerc/log" +) + +var reCache sync.Map + +// Check if a string matches the specified regular expression. +// The regexp is compiled only once and stored in a cache for future use. +func MatchCache(s, expr string) bool { + var re interface{} + var found bool + + if re, found = reCache.Load(expr); !found { + var err error + re, err = regexp.Compile(expr) + if err != nil { + log.Errorf("`%s` invalid regexp: %s", expr, err) + } + reCache.Store(expr, re) + } + if re, ok := re.(*regexp.Regexp); ok && re != nil { + return re.MatchString(s) + } + return false +} |