| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package matcher |
| 4 | |
| 5 | import ( |
| 6 | "bytes" |
| 7 | "strings" |
| 8 | ) |
| 9 | |
| 10 | type ( |
| 11 | // stringFullMatcher implements Matcher, it uses "==" to match. |
| 12 | stringFullMatcher string |
| 13 | |
| 14 | // stringPartialMatcher implements Matcher, it uses strings.Contains to match. |
| 15 | stringPartialMatcher string |
| 16 | |
| 17 | // stringPrefixMatcher implements Matcher, it uses strings.HasPrefix to match. |
| 18 | stringPrefixMatcher string |
| 19 | |
| 20 | // stringSuffixMatcher implements Matcher, it uses strings.HasSuffix to match. |
| 21 | stringSuffixMatcher string |
| 22 | ) |
| 23 | |
| 24 | // NewStringMatcher create a new matcher with string format |
| 25 | func NewStringMatcher(s string, startWith, endWith bool) (Matcher, error) { |
| 26 | if startWith { |
| 27 | if endWith { |
| 28 | return stringFullMatcher(s), nil |
| 29 | } |
| 30 | return stringPrefixMatcher(s), nil |
| 31 | } |
| 32 | if endWith { |
| 33 | return stringSuffixMatcher(s), nil |
| 34 | } |
| 35 | return stringPartialMatcher(s), nil |
| 36 | } |
| 37 | |
| 38 | func (m stringFullMatcher) Match(b []byte) bool { return string(m) == string(b) } |
| 39 | func (m stringFullMatcher) MatchString(line string) bool { return string(m) == line } |
| 40 | |
| 41 | func (m stringPartialMatcher) Match(b []byte) bool { return bytes.Contains(b, []byte(m)) } |
| 42 | func (m stringPartialMatcher) MatchString(line string) bool { return strings.Contains(line, string(m)) } |
| 43 | |
| 44 | func (m stringPrefixMatcher) Match(b []byte) bool { return bytes.HasPrefix(b, []byte(m)) } |
| 45 | func (m stringPrefixMatcher) MatchString(line string) bool { return strings.HasPrefix(line, string(m)) } |
| 46 | |
| 47 | func (m stringSuffixMatcher) Match(b []byte) bool { return bytes.HasSuffix(b, []byte(m)) } |
| 48 | func (m stringSuffixMatcher) MatchString(line string) bool { return strings.HasSuffix(line, string(m)) } |