File: filter.go

package info (click to toggle)
elvish 0.21.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,372 kB
  • sloc: javascript: 236; sh: 130; python: 104; makefile: 88; xml: 9
file content (57 lines) | stat: -rw-r--r-- 869 bytes parent folder | download | duplicates (2)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package filter

import (
	"regexp"
	"strings"
)

// Filter represents a compiled filter, which can be used to match text.
type Filter interface {
	Match(s string) bool
}

type andFilter struct {
	queries []Filter
}

func (aq andFilter) Match(s string) bool {
	for _, q := range aq.queries {
		if !q.Match(s) {
			return false
		}
	}
	return true
}

type orFilter struct {
	queries []Filter
}

func (oq orFilter) Match(s string) bool {
	for _, q := range oq.queries {
		if q.Match(s) {
			return true
		}
	}
	return false
}

type substringFilter struct {
	pattern    string
	ignoreCase bool
}

func (sq substringFilter) Match(s string) bool {
	if sq.ignoreCase {
		s = strings.ToLower(s)
	}
	return strings.Contains(s, sq.pattern)
}

type regexpFilter struct {
	pattern *regexp.Regexp
}

func (rq regexpFilter) Match(s string) bool {
	return rq.pattern.MatchString(s)
}