1
0
Fork 0
mirror of https://git.sr.ht/~rjarry/aerc synced 2025-06-30 19:00:21 +02:00
aerc/lib/parse/match.go
Robin Jarry 0ca8087ec9 treewide: replace interface{} with any
Since go 1.18, interface{} can be replaced with any.

Signed-off-by: Robin Jarry <robin@jarry.cc>
Reviewed-by: Karel Balej <balejk@matfyz.cz>
2025-04-07 10:51:05 +02:00

30 lines
603 B
Go

package parse
import (
"regexp"
"sync"
"git.sr.ht/~rjarry/aerc/lib/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 any
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
}