forked from 3JoB/vfs
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath.go
31 lines (27 loc) · 717 Bytes
/
path.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package vfs
import (
"strings"
)
// SplitPath splits the given path in segments:
//
// "/" -> []string{""}
// "./file" -> []string{".", "file"}
// "file" -> []string{".", "file"}
// "/usr/src/linux/" -> []string{"", "usr", "src", "linux"}
//
// The returned slice of path segments consists of one more more segments.
func SplitPath(path string, sep string) []string {
path = strings.TrimSpace(path)
path = strings.TrimSuffix(path, sep)
if path == "" { // was "/"
return []string{""}
}
if path == "." {
return []string{"."}
}
if len(path) > 0 && !strings.HasPrefix(path, sep) && !strings.HasPrefix(path, "."+sep) {
path = "./" + path
}
parts := strings.Split(path, sep)
return parts
}