From d571398e2e62f83284353cae632ecb5d436ff8b6 Mon Sep 17 00:00:00 2001 From: Gusted Date: Fri, 6 May 2022 01:29:07 +0200 Subject: [PATCH] Simplify `IsVendor` - Use the provided function by go-enry. This file was actually more or less duplicated code from go-enry: https://github.com/go-enry/go-enry/blob/ed2adad15906ed7f262e64b502eb91de08176314/utils.go#L139-L246 --- modules/analyze/vendor.go | 60 ++------------------------------------- 1 file changed, 2 insertions(+), 58 deletions(-) diff --git a/modules/analyze/vendor.go b/modules/analyze/vendor.go index 12ae8dbd8075c..976a6ddc7bf21 100644 --- a/modules/analyze/vendor.go +++ b/modules/analyze/vendor.go @@ -5,66 +5,10 @@ package analyze import ( - "regexp" - "sort" - "strings" - - "github.com/go-enry/go-enry/v2/data" + "github.com/go-enry/go-enry/v2" ) -var isVendorRegExp *regexp.Regexp - -func init() { - matchers := data.VendorMatchers - - caretStrings := make([]string, 0, 10) - caretShareStrings := make([]string, 0, 10) - - matcherStrings := make([]string, 0, len(matchers)) - for _, matcher := range matchers { - str := matcher.String() - if str[0] == '^' { - caretStrings = append(caretStrings, str[1:]) - } else if str[0:5] == "(^|/)" { - caretShareStrings = append(caretShareStrings, str[5:]) - } else { - matcherStrings = append(matcherStrings, str) - } - } - - sort.Strings(caretShareStrings) - sort.Strings(caretStrings) - sort.Strings(matcherStrings) - - sb := &strings.Builder{} - sb.WriteString("(?:^(?:") - sb.WriteString(caretStrings[0]) - for _, matcher := range caretStrings[1:] { - sb.WriteString(")|(?:") - sb.WriteString(matcher) - } - sb.WriteString("))") - sb.WriteString("|") - sb.WriteString("(?:(?:^|/)(?:") - sb.WriteString(caretShareStrings[0]) - for _, matcher := range caretShareStrings[1:] { - sb.WriteString(")|(?:") - sb.WriteString(matcher) - } - sb.WriteString("))") - sb.WriteString("|") - sb.WriteString("(?:") - sb.WriteString(matcherStrings[0]) - for _, matcher := range matcherStrings[1:] { - sb.WriteString(")|(?:") - sb.WriteString(matcher) - } - sb.WriteString(")") - combined := sb.String() - isVendorRegExp = regexp.MustCompile(combined) -} - // IsVendor returns whether or not path is a vendor path. func IsVendor(path string) bool { - return isVendorRegExp.MatchString(path) + return enry.IsVendor(path) }