Improve file extension checking performance

pull/291/head
Henrique Dias 2017-11-15 21:03:50 +00:00
parent 8f5eaee952
commit c76c69c256
1 changed files with 35 additions and 23 deletions

58
file.go
View File

@ -261,15 +261,12 @@ func (i *File) GetFileType(checkContent bool) error {
// If the type isn't text (and is blob for example), it will check some // If the type isn't text (and is blob for example), it will check some
// common types that are mistaken not to be text. // common types that are mistaken not to be text.
for _, extension := range textExtensions { if isInTextExtensions(i.Name) {
if strings.HasSuffix(i.Name, extension) { i.Type = "text"
i.Type = "text" } else {
goto End i.Type = "blob"
}
} }
i.Type = "blob"
End: End:
// If the file type is text, save its content. // If the file type is text, save its content.
if i.Type == "text" { if i.Type == "text" {
@ -415,23 +412,38 @@ func (l byModified) Less(i, j int) bool {
return iModified.Sub(jModified) < 0 return iModified.Sub(jModified) < 0
} }
var textExtensions = [...]string{ // textExtensions is the sorted list of text extensions which
".md", ".markdown", ".mdown", ".mmark", // can be edited.
".asciidoc", ".adoc", ".ad", var textExtensions = []string{
".rst", ".ad", ".ada", ".adoc", ".asciidoc",
".tml", ".yml", ".bas", ".bash", ".bat",
".json", ".toml", ".yaml", ".csv", ".xml", ".rss", ".conf", ".ini", ".c", ".cc", ".cmd", ".conf", ".cpp", ".cr", ".cs", ".css", ".csv",
".tex", ".sty", ".d",
".css", ".sass", ".scss", ".f", ".f90",
".js", ".h", ".hh", ".hpp", ".htaccess", ".html",
".html", ".ini",
".txt", ".rtf", ".java", ".js", ".json",
".sh", ".bash", ".ps1", ".bat", ".cmd", ".markdown", ".md", ".mdown", ".mmark",
".php", ".pl", ".py", ".nim",
".php", ".pl", ".ps1", ".py",
".rss", ".rst", ".rtf",
".sass", ".scss", ".sh", ".sty",
".tex", ".tml", ".toml", ".txt",
".vala", ".vapi",
".xml",
".yaml", ".yml",
"Caddyfile", "Caddyfile",
".htaccess", }
".c", ".cc", ".h", ".hh", ".cpp", ".hpp", ".f90",
".f", ".bas", ".d", ".ada", ".nim", ".cr", ".java", ".cs", ".vala", ".vapi", // isInTextExtensions checks if a file can be edited by its extensions.
func isInTextExtensions(name string) bool {
search := filepath.Ext(name)
if search == "" {
search = name
}
i := sort.SearchStrings(textExtensions, search)
return i < len(textExtensions) && textExtensions[i] == search
} }
// hasRune checks if the file has the frontmatter rune // hasRune checks if the file has the frontmatter rune