summaryrefslogtreecommitdiffstats
path: root/src/go/plugin/go.d/pkg/matcher/string.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/go/plugin/go.d/pkg/matcher/string.go')
-rw-r--r--src/go/plugin/go.d/pkg/matcher/string.go48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/go/plugin/go.d/pkg/matcher/string.go b/src/go/plugin/go.d/pkg/matcher/string.go
new file mode 100644
index 000000000..43ba43eb3
--- /dev/null
+++ b/src/go/plugin/go.d/pkg/matcher/string.go
@@ -0,0 +1,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)) }