summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <note20@mx.buetow.org>2020-08-30 14:38:43 +0300
committerPaul Buetow <note20@mx.buetow.org>2020-08-30 14:38:43 +0300
commit2bb621846f784d8ef66a4ffa1a6412e996d7b615 (patch)
tree056a8b38db5a07b924bf2a026e783c960fc1f921
parentc5a0ba7d29da7effa0ae18bffa10fc0be359b8e7 (diff)
initial regex package
-rw-r--r--internal/regex/regex.go69
-rw-r--r--internal/regex/regex_test.go16
-rw-r--r--internal/version/version.go4
3 files changed, 87 insertions, 2 deletions
diff --git a/internal/regex/regex.go b/internal/regex/regex.go
new file mode 100644
index 0000000..35685a2
--- /dev/null
+++ b/internal/regex/regex.go
@@ -0,0 +1,69 @@
+package regex
+
+import (
+ "regexp"
+)
+
+type Flag int
+
+const (
+ // Default is the default regex mode (positive matching)
+ Default = iota
+ // Negative negates the regex
+ Negative = iota
+ // Noop means no regex matching enabled, all defaults to true
+ Noop = iota
+)
+
+type Regex struct {
+ str string
+ re *regexp.Regexp
+ flag Flag
+}
+
+func (r Regex) Noop() Regex {
+ return Regex{
+ flag: Noop,
+ }
+}
+
+func New(str string, flag Flag) (Regex, error) {
+ r := Regex{
+ str: str,
+ flag: flag,
+ }
+
+ re, err := regexp.Compile(str)
+
+ if err != nil {
+ return r, err
+ }
+
+ r.re = re
+ return r, nil
+}
+
+func (r Regex) MatchString(str string) bool {
+ switch r.flag {
+ case Default:
+ return r.re.MatchString(str)
+ case Negative:
+ return !r.re.MatchString(str)
+ case Noop:
+ return true
+ default:
+ return false
+ }
+}
+
+/*
+func (r Regex) Serialize() string {
+ // TODO: Serialize to hex encoded str
+ return fmt.Sprintf("%b,%s",r.negate,r,str)
+}
+
+func (r Regex) Deserialize(input string) (Regex, error) {
+
+}
+
+*/
diff --git a/internal/regex/regex_test.go b/internal/regex/regex_test.go
new file mode 100644
index 0000000..a90aee2
--- /dev/null
+++ b/internal/regex/regex_test.go
@@ -0,0 +1,16 @@
+package regex
+
+import "testing"
+
+func TestRegex(t *testing.T) {
+
+ input := "hello"
+ r, err := New(".hello", Default)
+ if err != nil {
+ t.Errorf("error: unable to create regex: %v\n", err)
+ }
+
+ if r.MatchString(input) {
+ t.Errorf("error: expected to match string '%s' with regex '%v' but didnt\n", input, r)
+ }
+}
diff --git a/internal/version/version.go b/internal/version/version.go
index f97c4bb..36ef62c 100644
--- a/internal/version/version.go
+++ b/internal/version/version.go
@@ -11,9 +11,9 @@ const (
// Name of DTail.
Name string = "DTail"
// Version of DTail.
- Version string = "3.0.0"
+ Version string = "3.1.0"
// Additional information for DTail
- Additional string = ""
+ Additional string = "develop"
// ProtocolCompat -ibility version.
ProtocolCompat string = "3"
)