chore: optimize standardize path

This commit is contained in:
Noah Hsu
2022-06-23 17:04:37 +08:00
parent a65dcb48b4
commit ffa0bc294a
5 changed files with 24 additions and 12 deletions

View File

@ -1,10 +1,22 @@
package utils
import "strings"
import (
"path/filepath"
"runtime"
"strings"
)
// StandardizationPath convert path like '/' '/root' '/a/b'
func StandardizationPath(path string) string {
// StandardizePath convert path like '/' '/root' '/a/b'
func StandardizePath(path string) string {
path = strings.TrimSuffix(path, "/")
// windows abs path
if filepath.IsAbs(path) && runtime.GOOS == "windows" {
return path
}
// relative path with prefix '..'
if strings.HasPrefix(path, "..") {
return path
}
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
@ -13,5 +25,5 @@ func StandardizationPath(path string) string {
// PathEqual judge path is equal
func PathEqual(path1, path2 string) bool {
return StandardizationPath(path1) == StandardizationPath(path2)
return StandardizePath(path1) == StandardizePath(path2)
}