-
Notifications
You must be signed in to change notification settings - Fork 2
/
path.go
59 lines (47 loc) · 1.02 KB
/
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package netpath
import (
"strings"
)
func Path(sep string, elems ...string) string {
return strings.Join(elems, sep)
}
func Split(sep, path string) (dir, file string) {
idx := strings.LastIndex(path, sep)
if idx < 0 {
return "", path
}
return path[:idx], path[idx+len(sep):]
}
func Base(sep, path string) string {
idx := strings.LastIndex(path, sep)
if idx < 0 {
return path
}
return path[idx+len(sep):]
}
func Dir(sep, path string) string {
idx := strings.LastIndex(path, sep)
if idx < 0 {
return ""
}
return path[:idx]
}
func InDir(sep, path, dir string) bool {
path = strings.TrimSuffix(path, sep)
dir = strings.TrimSuffix(dir, sep)
if !strings.HasPrefix(path, dir) {
return false
}
return strings.HasPrefix(path[len(dir):], sep)
}
func SameDir(sep, path, dir string) bool {
path = strings.TrimSuffix(path, sep)
dir = strings.TrimSuffix(dir, sep)
if !strings.HasPrefix(path, dir) {
return false
}
if len(path) <= len(dir) {
return true
}
return strings.HasPrefix(path[len(dir):], sep)
}