github.com/anchore/syft@v1.38.2/syft/pkg/cataloger/internal/licenses/names.go (about) 1 package licenses 2 3 import ( 4 "math" 5 "regexp" 6 "slices" 7 "strings" 8 9 "github.com/scylladb/go-set/strset" 10 11 "github.com/anchore/syft/internal/licenses" 12 ) 13 14 var licenseRegexp = regexp.MustCompile(`^(?i)(?:(?:UN|MIT-)?LICEN[S|C]E|COPYING|NOTICE).*$`) 15 16 // lowerFileNames is a strset.Set of lowercased filenames 17 var lowerFileNames = func() *strset.Set { 18 lowerNames := strset.New() 19 for _, fileName := range licenses.FileNames() { 20 lowerNames.Add(strings.ToLower(fileName)) 21 } 22 return lowerNames 23 }() 24 25 // lowerFileNamesSorted is a sorted slice of lowercased filenames 26 var lowerFileNamesSorted = func() []string { 27 out := lowerFileNames.List() 28 slices.Sort(out) 29 return out 30 }() 31 32 // remove duplicate names that match the regex, keep any extras to test after regex check 33 var minLength, extraFileNames = func() (int, []string) { 34 minSize := math.MaxInt 35 var extras []string 36 for _, name := range lowerFileNamesSorted { 37 if len(name) < minSize { 38 minSize = len(name) 39 } 40 if licenseRegexp.MatchString(name) { 41 continue 42 } 43 extras = append(extras, name) 44 } 45 return minSize, extras 46 }() 47 48 // IsLicenseFile returns true if the name matches known license file name patterns 49 func IsLicenseFile(name string) bool { 50 if len(name) < minLength { 51 return false 52 } 53 if licenseRegexp.MatchString(name) { 54 return true 55 } 56 for _, licenseFile := range extraFileNames { 57 if strings.EqualFold(licenseFile, name) { 58 return true 59 } 60 } 61 return false 62 }