data: replace substring package with regex package

This commit is contained in:
Máximo Cuadros
2020-04-15 17:27:48 +02:00
parent b34576bd71
commit 29bc0a181b
10 changed files with 401 additions and 395 deletions

View File

@ -6,6 +6,7 @@ import (
"strings"
"github.com/go-enry/go-enry/v2/data"
"github.com/go-enry/go-enry/v2/regex"
)
const binSniffLen = 8000
@ -46,7 +47,7 @@ func GetMIMEType(path string, language string) string {
// IsDocumentation returns whether or not path is a documentation path.
func IsDocumentation(path string) bool {
return data.DocumentationMatchers.Match(path)
return matchRegexSlice(data.DocumentationMatchers, path)
}
// IsDotFile returns whether or not path has dot as a prefix.
@ -57,12 +58,12 @@ func IsDotFile(path string) bool {
// IsVendor returns whether or not path is a vendor path.
func IsVendor(path string) bool {
return data.VendorMatchers.Match(path)
return matchRegexSlice(data.VendorMatchers, path)
}
// IsTest returns whether or not path is a test path.
func IsTest(path string) bool {
return data.TestMatchers.Match(path)
return matchRegexSlice(data.TestMatchers, path)
}
// IsBinary detects if data is a binary value based on:
@ -91,3 +92,13 @@ func GetColor(language string) string {
return "#cccccc"
}
func matchRegexSlice(exprs []regex.EnryRegexp, str string) bool {
for _, expr := range exprs {
if expr.MatchString(str) {
return true
}
}
return false
}