added sloppy input classifier
This commit is contained in:
parent
2c5c3eea32
commit
563791cdea
@ -2,6 +2,7 @@ package main
|
||||
|
||||
// Some helper functions
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@ -14,3 +15,18 @@ func ContainsCi(s, substr string) bool {
|
||||
strings.ToLower(substr),
|
||||
)
|
||||
}
|
||||
|
||||
/*
|
||||
Check if something could be a prefix
|
||||
*/
|
||||
func MaybePrefix(s string) bool {
|
||||
s = strings.ToLower(s)
|
||||
|
||||
// Test using regex
|
||||
matches := regexp.MustCompile(`([a-f0-9/]+[\.:]?)+`).FindAllStringIndex(s, -1)
|
||||
if len(matches) == 1 {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
@ -5,9 +5,31 @@ import (
|
||||
)
|
||||
|
||||
func TestContainsCi(t *testing.T) {
|
||||
|
||||
if ContainsCi("foo bar", "BaR") != true {
|
||||
t.Error("An unexpected error occured.")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestMaybePrefix(t *testing.T) {
|
||||
expected := []struct {
|
||||
string
|
||||
bool
|
||||
}{
|
||||
{"10.0.0", true},
|
||||
{"23.42.11.42/23", true},
|
||||
{"200", true},
|
||||
{"2001:", true},
|
||||
{"A", true},
|
||||
{"A b", false},
|
||||
{"23 Foo", false},
|
||||
{"122.beef:", true}, // sloppy
|
||||
{"122.beef:", true}, // very
|
||||
{"122:beef", true}, // sloppy.
|
||||
}
|
||||
|
||||
for _, e := range expected {
|
||||
if MaybePrefix(e.string) != e.bool {
|
||||
t.Error("Expected", e.string, "to be prefix:", e.bool)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user