1// Copyright 2021 The Gitea Authors. All rights reserved. 2// Use of this source code is governed by a MIT-style 3// license that can be found in the LICENSE file. 4 5package analyze 6 7import ( 8 "regexp" 9 "sort" 10 "strings" 11 12 "github.com/go-enry/go-enry/v2/data" 13) 14 15var isVendorRegExp *regexp.Regexp 16 17func init() { 18 matchers := data.VendorMatchers 19 20 caretStrings := make([]string, 0, 10) 21 caretShareStrings := make([]string, 0, 10) 22 23 matcherStrings := make([]string, 0, len(matchers)) 24 for _, matcher := range matchers { 25 str := matcher.String() 26 if str[0] == '^' { 27 caretStrings = append(caretStrings, str[1:]) 28 } else if str[0:5] == "(^|/)" { 29 caretShareStrings = append(caretShareStrings, str[5:]) 30 } else { 31 matcherStrings = append(matcherStrings, str) 32 } 33 } 34 35 sort.Strings(caretShareStrings) 36 sort.Strings(caretStrings) 37 sort.Strings(matcherStrings) 38 39 sb := &strings.Builder{} 40 sb.WriteString("(?:^(?:") 41 sb.WriteString(caretStrings[0]) 42 for _, matcher := range caretStrings[1:] { 43 sb.WriteString(")|(?:") 44 sb.WriteString(matcher) 45 } 46 sb.WriteString("))") 47 sb.WriteString("|") 48 sb.WriteString("(?:(?:^|/)(?:") 49 sb.WriteString(caretShareStrings[0]) 50 for _, matcher := range caretShareStrings[1:] { 51 sb.WriteString(")|(?:") 52 sb.WriteString(matcher) 53 } 54 sb.WriteString("))") 55 sb.WriteString("|") 56 sb.WriteString("(?:") 57 sb.WriteString(matcherStrings[0]) 58 for _, matcher := range matcherStrings[1:] { 59 sb.WriteString(")|(?:") 60 sb.WriteString(matcher) 61 } 62 sb.WriteString(")") 63 combined := sb.String() 64 isVendorRegExp = regexp.MustCompile(combined) 65} 66 67// IsVendor returns whether or not path is a vendor path. 68func IsVendor(path string) bool { 69 return isVendorRegExp.MatchString(path) 70} 71