Improve file extension checking performance

Former-commit-id: 30aaff2f0bee9dd9743d4acd6a68aafc90addef9 [formerly 967fc0b47a5fb0d1b5e79a8b926f4fdd61b8477a] [formerly 5eaecd069cd3de9ebb5e3e6d0feb04844fc473c3 [formerly c76c69c256]]
Former-commit-id: 11d0c1ae276859eccb7949d96dedbd0cd3204ffd [formerly 63de350fea7eb0a6d621b029c9c8866866462d31]
Former-commit-id: 422edd1298a7ba86dd958dcc7a513a0a3e6d83a3
This commit is contained in:
Henrique Dias 2017-11-15 21:03:50 +00:00
parent c947228ac4
commit 4ddb3f5a34

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
// common types that are mistaken not to be text.
for _, extension := range textExtensions {
if strings.HasSuffix(i.Name, extension) {
i.Type = "text"
goto End
}
if isInTextExtensions(i.Name) {
i.Type = "text"
} else {
i.Type = "blob"
}
i.Type = "blob"
End:
// If the file type is text, save its content.
if i.Type == "text" {
@ -415,23 +412,38 @@ func (l byModified) Less(i, j int) bool {
return iModified.Sub(jModified) < 0
}
var textExtensions = [...]string{
".md", ".markdown", ".mdown", ".mmark",
".asciidoc", ".adoc", ".ad",
".rst",
".tml", ".yml",
".json", ".toml", ".yaml", ".csv", ".xml", ".rss", ".conf", ".ini",
".tex", ".sty",
".css", ".sass", ".scss",
".js",
".html",
".txt", ".rtf",
".sh", ".bash", ".ps1", ".bat", ".cmd",
".php", ".pl", ".py",
// textExtensions is the sorted list of text extensions which
// can be edited.
var textExtensions = []string{
".ad", ".ada", ".adoc", ".asciidoc",
".bas", ".bash", ".bat",
".c", ".cc", ".cmd", ".conf", ".cpp", ".cr", ".cs", ".css", ".csv",
".d",
".f", ".f90",
".h", ".hh", ".hpp", ".htaccess", ".html",
".ini",
".java", ".js", ".json",
".markdown", ".md", ".mdown", ".mmark",
".nim",
".php", ".pl", ".ps1", ".py",
".rss", ".rst", ".rtf",
".sass", ".scss", ".sh", ".sty",
".tex", ".tml", ".toml", ".txt",
".vala", ".vapi",
".xml",
".yaml", ".yml",
"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