summaryrefslogtreecommitdiff
path: root/internal/regex
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2021-10-09 21:10:29 +0300
committerPaul Buetow <paul@buetow.org>2021-10-10 13:36:41 +0300
commit97747ea0f3178f7f5890512d483fdccaa82846b0 (patch)
tree9ff1335ca26afc90e55fd6de416457e252d75a35 /internal/regex
parent7a7169791a64190e1002e38bc9c04ad0d5c1ce1f (diff)
vetting and linting and some code restyling
Diffstat (limited to 'internal/regex')
-rw-r--r--internal/regex/regex.go9
-rw-r--r--internal/regex/regex_test.go17
2 files changed, 12 insertions, 14 deletions
diff --git a/internal/regex/regex.go b/internal/regex/regex.go
index 352ffd6..eb6e1b3 100644
--- a/internal/regex/regex.go
+++ b/internal/regex/regex.go
@@ -48,9 +48,7 @@ func new(regexStr string, flags []Flag) (Regex, error) {
regexStr: regexStr,
flags: flags,
}
-
re, err := regexp.Compile(regexStr)
-
if err != nil {
return r, err
}
@@ -94,11 +92,9 @@ func (r Regex) Serialize() (string, error) {
for _, flag := range r.flags {
flags = append(flags, flag.String())
}
-
if !r.initialized {
return "", fmt.Errorf("Unable to serialize regex as not initialized properly: %v", r)
}
-
return fmt.Sprintf("regex:%s %s", strings.Join(flags, ","), r.regexStr), nil
}
@@ -109,12 +105,12 @@ func Deserialize(str string) (Regex, error) {
if len(s) < 2 {
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)
+ return Regex{}, fmt.Errorf("unable to deserialize regex '%s': should start "+
+ "with string 'regex'", str)
}
// Parse regex flags, e.g. "regex:flag1,flag2,flag3..."
@@ -129,6 +125,5 @@ func Deserialize(str string) (Regex, error) {
flags = append(flags, flag)
}
}
-
return new(regexStr, flags)
}
diff --git a/internal/regex/regex_test.go b/internal/regex/regex_test.go
index 2ce49ac..033a286 100644
--- a/internal/regex/regex_test.go
+++ b/internal/regex/regex_test.go
@@ -9,7 +9,8 @@ func TestRegex(t *testing.T) {
r := NewNoop()
if !r.MatchString(input) {
- t.Errorf("expected to match string '%s' with noop regex '%v' but didn't\n", input, r)
+ t.Errorf("expected to match string '%s' with noop regex '%v' but didn't\n",
+ input, r)
}
r, err := New(".hello", Default)
@@ -17,7 +18,8 @@ func TestRegex(t *testing.T) {
t.Errorf("unable to create regex: %v\n", err)
}
if r.MatchString(input) {
- t.Errorf("expected to match string '%s' with regex '%v' but didn't\n", input, r)
+ t.Errorf("expected to match string '%s' with regex '%v' but didn't\n",
+ input, r)
}
serialized, err := r.Serialize()
@@ -29,8 +31,8 @@ func TestRegex(t *testing.T) {
t.Errorf("unable to serialize deserialized regex: %v: %v\n", serialized, err)
}
if r.String() != r2.String() {
- t.Errorf("regex should be the same after deserialize(serialize(..)), got '%s' but expected '%s'.\n",
- r2.String(), r.String())
+ t.Errorf("regex should be the same after deserialize(serialize(..)), got "+
+ "'%s' but expected '%s'.\n", r2.String(), r.String())
}
r, err = New(".hello", Invert)
@@ -38,7 +40,8 @@ func TestRegex(t *testing.T) {
t.Errorf("unable to create regex: %v\n", err)
}
if !r.MatchString(input) {
- t.Errorf("expected to not match string '%s' with regex '%v' but matched\n", input, r)
+ t.Errorf("expected to not match string '%s' with regex '%v' but matched\n",
+ input, r)
}
serialized, err = r.Serialize()
@@ -50,7 +53,7 @@ func TestRegex(t *testing.T) {
t.Errorf("unable to serialize deserialized regex: %v: %v\n", serialized, err)
}
if r.String() != r2.String() {
- t.Errorf("regex should be the same after deserialize(serialize(..)), got '%s' but expected '%s'.\n",
- r2.String(), r.String())
+ t.Errorf("regex should be the same after deserialize(serialize(..)), got "+
+ "'%s' but expected '%s'.\n", r2.String(), r.String())
}
}