github.com/GuanceCloud/cliutils@v1.1.21/pprofparser/tools/filepathtoolkit/format.go (about) 1 package filepathtoolkit 2 3 import ( 4 "os" 5 "path/filepath" 6 "strings" 7 ) 8 9 var ( 10 currentOsPathSep = string(os.PathSeparator) 11 otherOsPathSep = otherOsPathSeparator() 12 ) 13 14 func otherOsPathSeparator() string { 15 if os.PathSeparator == '/' { 16 return "\\" 17 } 18 return "/" 19 } 20 21 func BaseName(path string) string { 22 if !strings.Contains(path, currentOsPathSep) && strings.Contains(path, otherOsPathSep) { 23 path = strings.ReplaceAll(path, otherOsPathSep, currentOsPathSep) 24 } 25 return filepath.Base(path) 26 } 27 28 func DirName(path string) string { 29 dir := filepath.Dir(path) 30 31 // for other platforms 32 if dir == "." && !strings.Contains(path, currentOsPathSep) { 33 idx := strings.LastIndex(path, otherOsPathSep) 34 if idx >= 0 { 35 if idx == 0 || path[idx-1] == ':' { 36 return path[:idx+1] 37 } 38 return path[:idx] 39 } 40 } 41 if dir == "." && len(path) >= 2 && path[0] == '<' && path[len(path)-1] == '>' { 42 return path 43 } 44 return dir 45 }