go-enry/shared/enry.go
Miguel Molina 8ff885a3a8
implement IsGenerated helper to filter out generated files
Closes #17

Implements the IsGenerated helper function to filter out generated
files using the rules and matchers in:
- https://github.com/github/linguist/blob/master/lib/linguist/generated.rb

Since the vast majority of matchers have very different logic, it cannot
be autogenerated directly from linguist like other logics in enry, so it's
translated by hand.

There are three different types of matchers in this implementation:
- By extension, which mark as generated based only in the extension. These
  are the fastest matchers, so they're done first.
- By file name, which matches patterns against the filename. These
  are performed in second place. Unlike linguist, we try to use string
  functions instead of regexps as much as possible.
- Finally, the rest of the matchers, which go into the content and try
  to identify if they're generated or not based on the content. Unlike
  linguist, we try to only read the content we need and not split it
  all unless it's necessary and use byte functions instead of regexps
  as much as possible.

Signed-off-by: Miguel Molina <miguel@erizocosmi.co>
2020-05-28 08:55:13 +02:00

146 lines
4.1 KiB
Go

// +build darwin,cgo linux,cgo
// +build amd64
package main
import "C"
import "github.com/go-enry/go-enry/v2"
//export GetLanguage
func GetLanguage(filename string, content []byte) string {
return enry.GetLanguage(filename, content)
}
//export GetLanguageByContent
func GetLanguageByContent(filename string, content []byte) (language string, safe bool) {
return enry.GetLanguageByContent(filename, content)
}
//export GetLanguageByEmacsModeline
func GetLanguageByEmacsModeline(content []byte) (language string, safe bool) {
return enry.GetLanguageByModeline(content)
}
//export GetLanguageByExtension
func GetLanguageByExtension(filename string) (language string, safe bool) {
return enry.GetLanguageByExtension(filename)
}
//export GetLanguageByFilename
func GetLanguageByFilename(filename string) (language string, safe bool) {
return enry.GetLanguageByFilename(filename)
}
//export GetLanguageByModeline
func GetLanguageByModeline(content []byte) (language string, safe bool) {
return enry.GetLanguageByModeline(content)
}
//export GetLanguageByShebang
func GetLanguageByShebang(content []byte) (language string, safe bool) {
return enry.GetLanguageByShebang(content)
}
//export GetLanguageByVimModeline
func GetLanguageByVimModeline(content []byte) (language string, safe bool) {
return enry.GetLanguageByVimModeline(content)
}
//export GetLanguageExtensions
func GetLanguageExtensions(language string, result *[]*C.char) {
strSliceCopy(result, enry.GetLanguageExtensions(language))
}
//export GetLanguages
func GetLanguages(filename string, content []byte, result *[]*C.char) {
strSliceCopy(result, enry.GetLanguages(filename, content))
}
//export GetLanguagesByContent
func GetLanguagesByContent(filename string, content []byte, candidates []string, result *[]*C.char) {
strSliceCopy(result, enry.GetLanguagesByContent(filename, content, candidates))
}
//export GetLanguagesByEmacsModeline
func GetLanguagesByEmacsModeline(filename string, content []byte, candidates []string, result *[]*C.char) {
strSliceCopy(result, enry.GetLanguagesByEmacsModeline(filename, content, candidates))
}
//export GetLanguagesByExtension
func GetLanguagesByExtension(filename string, content []byte, candidates []string, result *[]*C.char) {
strSliceCopy(result, enry.GetLanguagesByExtension(filename, content, candidates))
}
//export GetLanguagesByFilename
func GetLanguagesByFilename(filename string, content []byte, candidates []string, result *[]*C.char) {
strSliceCopy(result, enry.GetLanguagesByFilename(filename, content, candidates))
}
//export GetLanguagesByModeline
func GetLanguagesByModeline(filename string, content []byte, candidates []string, result *[]*C.char) {
strSliceCopy(result, enry.GetLanguagesByModeline(filename, content, candidates))
}
//export GetLanguagesByShebang
func GetLanguagesByShebang(filename string, content []byte, candidates []string, result *[]*C.char) {
strSliceCopy(result, enry.GetLanguagesByShebang(filename, content, candidates))
}
//export GetLanguagesByVimModeline
func GetLanguagesByVimModeline(filename string, content []byte, candidates []string, result *[]*C.char) {
strSliceCopy(result, enry.GetLanguagesByVimModeline(filename, content, candidates))
}
//export GetMimeType
func GetMimeType(path string, language string) string {
return enry.GetMIMEType(path, language)
}
//export IsBinary
func IsBinary(data []byte) bool {
return enry.IsBinary(data)
}
//export IsConfiguration
func IsConfiguration(path string) bool {
return enry.IsConfiguration(path)
}
//export IsDocumentation
func IsDocumentation(path string) bool {
return enry.IsDocumentation(path)
}
//export IsDotFile
func IsDotFile(path string) bool {
return enry.IsDotFile(path)
}
//export IsImage
func IsImage(path string) bool {
return enry.IsImage(path)
}
//export IsVendor
func IsVendor(path string) bool {
return enry.IsVendor(path)
}
//export IsGenerated
func IsGenerated(path string, content []byte) bool {
return enry.IsGenerated(path, content)
}
//export GetColor
func GetColor(language string) string {
return enry.GetColor(language)
}
func strSliceCopy(result *[]*C.char, slice []string) {
for _, str := range slice {
*result = append(*result, C.CString(str))
}
}
func main() {}