-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpath.go
76 lines (63 loc) · 1.14 KB
/
rpath.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package rpath
import (
"path"
)
func IsDir(p string) bool {
if p == "" {
return false
}
return p[len(p)-1] == '/'
}
func SetFile(p string) string {
p = path.Clean(p)
if len(p) > 0 && p[len(p)-1] == '/' {
return p[:len(p)-1]
}
return p
}
func SetDir(p string) string {
p = path.Clean(p)
if len(p) > 0 && p[len(p)-1] != '/' {
return p + "/"
}
return p
}
func SetType(p string, is_dir bool) string {
if is_dir {
return SetDir(p)
}
return SetFile(p)
}
func Base(p string) string {
is_dir := IsDir(p)
return SetType(path.Base(p), is_dir)
}
func Clean(p string) string {
is_dir := IsDir(p)
return SetType(path.Clean(p), is_dir)
}
func Dir(p string) string {
return SetDir(path.Dir(p))
}
func Ext(p string) string {
if IsDir(p) {
return ""
}
return path.Ext(p)
}
var IsAbs = path.IsAbs
func Split(p string) (string, string) {
is_dir := IsDir(p)
d, n := path.Split(p)
return SetDir(d), SetType(path.Clean(n), is_dir)
}
func Match(pat, n string) (bool, error) {
n = Clean(n)
return path.Match(pat, n)
}
func Join(pe ...string) string {
if len(pe) == 0 {
return ""
}
return SetType(path.Join(pe...), IsDir(pe[len(pe)-1]))
}