summaryrefslogtreecommitdiff
path: root/internal/regex/regex.go
blob: f668c38be892479ce914963ab6912e2b1025e43a (plain)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package regex

import (
	"fmt"
	"regexp"
	"strings"

	"github.com/mimecast/dtail/internal/io/logger"
)

type Regex struct {
	// The original regex string
	regexStr string
	// The Golang regexp object
	re *regexp.Regexp
	// For now only use the first flag at flags[0], but in the future we can
	// set and use multiple flags.
	flags       []Flag
	initialized bool
}

func (r Regex) String() string {
	return fmt.Sprintf("Regex(regexStr:%s,flags:%s,initialized:%t,re==nil:%t)",
		r.regexStr, r.flags, r.initialized, r.re == nil)
}

func NewNoop() Regex {
	return Regex{
		flags:       []Flag{Noop},
		initialized: true,
	}
}

func New(regexStr string, flag Flag) (Regex, error) {
	if regexStr == "" || regexStr == "." || regexStr == ".*" {
		return NewNoop(), nil
	}
	return new(regexStr, []Flag{flag})
}

func new(regexStr string, flags []Flag) (Regex, error) {
	if len(flags) == 0 {
		flags = append(flags, Default)
	}

	r := Regex{
		regexStr: regexStr,
		flags:    flags,
	}

	re, err := regexp.Compile(regexStr)

	if err != nil {
		return r, err
	}

	r.re = re
	r.initialized = true
	return r, nil
}

func (r Regex) Match(bytes []byte) bool {
	switch r.flags[0] {
	case Default:
		return r.re.Match(bytes)
	case Invert:
		return !r.re.Match(bytes)
	case Noop:
		return true
	default:
		return false
	}
}

func (r Regex) MatchString(str string) bool {
	switch r.flags[0] {
	case Default:
		return r.re.MatchString(str)
	case Invert:
		return !r.re.MatchString(str)
	case Noop:
		return true
	default:
		return false
	}
}

func (r Regex) Serialize() string {
	var flags []string
	for _, flag := range r.flags {
		flags = append(flags, flag.String())
	}

	if !r.initialized {
		logger.FatalExit("Unable to serialize regex as not initialized properly", r)
	}

	return fmt.Sprintf("regex:%s %s", strings.Join(flags, ","), r.regexStr)
}

func Deserialize(str string) (Regex, error) {
	// Get regex string
	s := strings.SplitN(str, " ", 2)
	if len(s) < 2 {
		logger.Debug("Using noop regex", str)
		return NewNoop(), nil
	}

	flagsStr := s[0]
	regexStr := s[1]

	if !strings.HasPrefix(flagsStr, "regex") {
		return Regex{}, fmt.Errorf("unable to deserialize regex '%s': should start with string 'regex'", str)
	}

	// Parse regex flags, e.g. "regex:flag1,flag2,flag3..."
	var flags []Flag
	if strings.Contains(flagsStr, ":") {
		s := strings.SplitN(flagsStr, ":", 2)
		for _, flagStr := range strings.Split(s[1], ",") {
			flag, err := NewFlag(flagStr)
			if err != nil {
				logger.Error("ignoring flag", err)
				continue
			}
			logger.Debug("Adding regex flag", flag)
			flags = append(flags, flag)
		}
	}

	return new(regexStr, flags)
}