src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/edit/filter/filter.go (about) 1 package filter 2 3 import ( 4 "regexp" 5 "strings" 6 ) 7 8 // Filter represents a compiled filter, which can be used to match text. 9 type Filter interface { 10 Match(s string) bool 11 } 12 13 type andFilter struct { 14 queries []Filter 15 } 16 17 func (aq andFilter) Match(s string) bool { 18 for _, q := range aq.queries { 19 if !q.Match(s) { 20 return false 21 } 22 } 23 return true 24 } 25 26 type orFilter struct { 27 queries []Filter 28 } 29 30 func (oq orFilter) Match(s string) bool { 31 for _, q := range oq.queries { 32 if q.Match(s) { 33 return true 34 } 35 } 36 return false 37 } 38 39 type substringFilter struct { 40 pattern string 41 ignoreCase bool 42 } 43 44 func (sq substringFilter) Match(s string) bool { 45 if sq.ignoreCase { 46 s = strings.ToLower(s) 47 } 48 return strings.Contains(s, sq.pattern) 49 } 50 51 type regexpFilter struct { 52 pattern *regexp.Regexp 53 } 54 55 func (rq regexpFilter) Match(s string) bool { 56 return rq.pattern.MatchString(s) 57 }