aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/parse/match.go30
-rw-r--r--lib/templates/functions.go2
2 files changed, 32 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
+}
diff --git a/lib/templates/functions.go b/lib/templates/functions.go
index 1cbdc85d..8bd8f37b 100644
--- a/lib/templates/functions.go
+++ b/lib/templates/functions.go
@@ -11,6 +11,7 @@ import (
"time"
"git.sr.ht/~rjarry/aerc/lib/format"
+ "git.sr.ht/~rjarry/aerc/lib/parse"
"github.com/emersion/go-message/mail"
)
@@ -271,4 +272,5 @@ var templateFuncs = template.FuncMap{
"join": join,
"trimSignature": trimSignature,
"compactDir": compactDir,
+ "match": parse.MatchCache,
}