use more efficient item type parsing for files

Signed-off-by: kim (grufwub) <grufwub@gmail.com>
development
kim (grufwub) 4 years ago
parent c7a3f33660
commit f91493663b

@ -1,6 +1,9 @@
package gopher
import "strings"
import (
"path"
"strings"
)
// ItemType specifies a gopher item type char
type ItemType byte
@ -134,30 +137,25 @@ var fileExtMap = map[string]ItemType{
// getItemType is an internal function to get an ItemType for a file name string
func getItemType(name string) ItemType {
// Split, name MUST be lower
split := strings.Split(strings.ToLower(name), ".")
// First we look at how many '.' in name string
splitLen := len(split)
switch splitLen {
case 0:
// We cannot tell the file type, return default
// Get file extension (lower!)
ext := strings.ToLower(path.Ext(name))
// Empty, cannot tell so return default
if ext == "" {
return typeDefault
}
default:
// get index of str after last '.', look up in fileExtMap
fileType, ok := fileExtMap["."+split[splitLen-1]]
if ok {
return fileType
}
// Lookup in map, return value or default
itemType, ok := fileExtMap["."+ext]
if !ok {
return typeDefault
}
return itemType
}
// parseLineType parses a gophermap's line type based on first char and contents
func parseLineType(line string) ItemType {
lineLen := len(line)
if lineLen == 0 {
return typeInfoNotStated
}

Loading…
Cancel
Save