File: match.go

package info (click to toggle)
aerc 0.21.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,892 kB
  • sloc: ansic: 1,181; python: 1,000; sh: 553; awk: 360; makefile: 23
file content (30 lines) | stat: -rw-r--r-- 603 bytes parent folder | download
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
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
}