| 21 | } |
| 22 | |
| 23 | func NewRegexp(regexps []string, shouldMatch bool) (*Regexp, error) { |
| 24 | matchers := make([]*labels.FastRegexMatcher, 0, len(regexps)) |
| 25 | |
| 26 | for _, r := range regexps { |
| 27 | m, err := labels.NewFastRegexMatcher(r) |
| 28 | if err != nil { |
| 29 | return nil, err |
| 30 | } |
| 31 | matchers = append(matchers, m) |
| 32 | } |
| 33 | |
| 34 | // only memoize if there's a unoptimized matcher |
| 35 | // TODO: should we limit memoization to N values? |
| 36 | var matches map[string]bool |
| 37 | for _, m := range matchers { |
| 38 | if shouldMemoize(m) { |
| 39 | matches = make(map[string]bool) |
| 40 | break |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | return &Regexp{ |
| 45 | matchers: matchers, |
| 46 | matches: matches, |
| 47 | shouldMatch: shouldMatch, |
| 48 | }, nil |
| 49 | } |
| 50 | |
| 51 | func (r *Regexp) Match(b []byte) bool { |
| 52 | return r.MatchString(unsafe.String(unsafe.SliceData(b), len(b))) |