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
|
// SPDX-License-Identifier: GPL-3.0-or-later
package matcher
import (
"bytes"
"strings"
)
type (
// stringFullMatcher implements Matcher, it uses "==" to match.
stringFullMatcher string
// stringPartialMatcher implements Matcher, it uses strings.Contains to match.
stringPartialMatcher string
// stringPrefixMatcher implements Matcher, it uses strings.HasPrefix to match.
stringPrefixMatcher string
// stringSuffixMatcher implements Matcher, it uses strings.HasSuffix to match.
stringSuffixMatcher string
)
// NewStringMatcher create a new matcher with string format
func NewStringMatcher(s string, startWith, endWith bool) (Matcher, error) {
if startWith {
if endWith {
return stringFullMatcher(s), nil
}
return stringPrefixMatcher(s), nil
}
if endWith {
return stringSuffixMatcher(s), nil
}
return stringPartialMatcher(s), nil
}
func (m stringFullMatcher) Match(b []byte) bool { return string(m) == string(b) }
func (m stringFullMatcher) MatchString(line string) bool { return string(m) == line }
func (m stringPartialMatcher) Match(b []byte) bool { return bytes.Contains(b, []byte(m)) }
func (m stringPartialMatcher) MatchString(line string) bool { return strings.Contains(line, string(m)) }
func (m stringPrefixMatcher) Match(b []byte) bool { return bytes.HasPrefix(b, []byte(m)) }
func (m stringPrefixMatcher) MatchString(line string) bool { return strings.HasPrefix(line, string(m)) }
func (m stringSuffixMatcher) Match(b []byte) bool { return bytes.HasSuffix(b, []byte(m)) }
func (m stringSuffixMatcher) MatchString(line string) bool { return strings.HasSuffix(line, string(m)) }
|