From 85fb8028aeb1f59c15b917d731e13e4c6e17fefd Mon Sep 17 00:00:00 2001 From: Edgar HIPP Date: Sun, 30 Aug 2015 17:06:52 +0200 Subject: [PATCH] Match logo@2x.png but not email adress --- extractor.go | 2 +- pe_test.go | 5 +++++ validators.go | 21 ++++++++++++++++++++- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/extractor.go b/extractor.go index 0b03664..7d13650 100644 --- a/extractor.go +++ b/extractor.go @@ -17,7 +17,7 @@ func GetAllMatches(input string) []string { matches = pathExtractor(input) for _, match := range matches { s = string(match[1]) - if isDate(s) || isVersion(s) || isGitRange(s) || isGitInstruction(s) || containsInvalidString(s) || len(s) <= 2 { + if isEmail(s) || isDate(s) || isVersion(s) || isGitRange(s) || isGitInstruction(s) || containsInvalidString(s) || len(s) <= 2 { continue } if isGitPath(s) { diff --git a/pe_test.go b/pe_test.go index cc8ef4d..c10655a 100644 --- a/pe_test.go +++ b/pe_test.go @@ -18,6 +18,11 @@ func TestGitIgnore(t *testing.T) { t.Errorf("Matches email adresses", output[0]) } + output = GetAllMatches(" logo@2x.png ") + if len(output) == 0 { + t.Errorf("Doesn't match retina asset", output[0]) + } + output = GetAllMatches("and/or") if len(output) != 0 { t.Errorf("Matches and/or adresses", output[0]) diff --git a/validators.go b/validators.go index cb09c40..bef5f65 100644 --- a/validators.go +++ b/validators.go @@ -5,6 +5,15 @@ import ( "strings" ) +func stringInSlice(str string, list []string) bool { + for _, v := range list { + if v == str { + return true + } + } + return false +} + func isGitRange(input string) bool { r := regexp.MustCompile("[0-9a-f]{3,}\\.\\.[0-9a-f]{3,}") return r.Match([]byte(input)) @@ -15,6 +24,16 @@ func isGitPath(input string) bool { return r.Match([]byte(input)) } +func isEmail(input string) bool { + r := regexp.MustCompile("[a-zA-Z0-9._%+-]+@(?:[a-zA-Z0-9-]+.)+([a-zA-Z]{2,4})") + result := r.FindSubmatch([]byte(input)) + if result == nil { + return false + } + fileExtensions := []string{"png", "bmp", "jpeg"} + return !stringInSlice(string(result[1]), fileExtensions) +} + func isDate(input string) bool { r := regexp.MustCompile("^[0-9]+/[0-9]+/[0-9]+") return r.Match([]byte(input)) @@ -38,7 +57,7 @@ func isVersion(input string) bool { } func containsInvalidString(input string) bool { - invalidStrings := []string{"(", ")", "@", "and/or", "origin/", "{", "}", "<", ">", "$", "*"} + invalidStrings := []string{"(", ")", "and/or", "origin/", "{", "}", "<", ">", "$", "*"} for _, s := range invalidStrings { if strings.Contains(input, s) { return true -- 2.44.0