-
Notifications
You must be signed in to change notification settings - Fork 0
/
path-calc.go
162 lines (126 loc) Β· 3.12 KB
/
path-calc.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package nef
import (
"path/filepath"
"strings"
)
// Path
type PathCalc interface {
Base(path string) string
Clean(path string) string
Dir(name string) string
Elements(path string) []string
Join(elements ...string) string
Split(path string) (dir, file string)
Truncate(path string) string
}
type AbsoluteCalc struct {
}
// Base returns the last element of the path
func (c *AbsoluteCalc) Base(path string) string {
return filepath.Base(path)
}
// Clean returns the shortest path name equivalent to path
// by purely lexical processing.
func (c *AbsoluteCalc) Clean(path string) string {
return filepath.Clean(path)
}
// Dir returns all but the last element of the path
func (c *AbsoluteCalc) Dir(name string) string {
return filepath.Dir(name)
}
// Join joins any number of path elements into a single path
func (c *AbsoluteCalc) Join(elements ...string) string {
return filepath.Join(elements...)
}
// Split splits the path immediately following the final separator
func (c *AbsoluteCalc) Split(path string) (dir, file string) {
return filepath.Split(path)
}
func (c *AbsoluteCalc) Truncate(path string) string {
if path == "" {
return "."
}
var (
separator = string(filepath.Separator)
)
if !strings.HasSuffix(path, separator) {
return path
}
return path[:strings.LastIndex(path, separator)]
}
func (c *AbsoluteCalc) Elements(path string) []string {
if path == "" {
return []string{}
}
return strings.Split(path, string(filepath.Separator))
}
const (
separator = '/'
)
var (
separatorStr = string(separator)
)
type RelativeCalc struct {
Root string
}
// Base returns the last element of the path
func (c *RelativeCalc) Base(path string) string {
if path == "" {
return "."
}
if !strings.Contains(path, separatorStr) {
return path
}
return path[strings.LastIndex(path, separatorStr)+1:]
}
// Clean returns the shortest path name equivalent to path
// by purely lexical processing.
func (c *RelativeCalc) Clean(path string) string {
clean := filepath.Clean(path)
if clean == separatorStr {
return "."
}
if strings.HasPrefix(clean, separatorStr) {
clean = clean[1:]
}
return clean
}
func (c *RelativeCalc) Elements(path string) []string {
if path == "" {
return []string{}
}
return strings.Split(path, separatorStr)
}
// Dir returns all but the last element of the path
func (c *RelativeCalc) Dir(path string) string {
if path == "" {
return "."
}
if !strings.Contains(path, separatorStr) {
return "."
}
return path[:strings.LastIndex(path, separatorStr)]
}
// Join joins any number of path elements into a single path
func (c *RelativeCalc) Join(elements ...string) string {
return strings.Join(elements, separatorStr)
}
// Split splits the path immediately following the final separator
func (c *RelativeCalc) Split(path string) (dir, file string) {
if path == "" {
return "", ""
}
if !strings.Contains(path, separatorStr) {
return "", path
}
return c.Dir(path), c.Base(path)
}
func (c *RelativeCalc) Truncate(path string) string {
if path == "" {
return "."
}
if !strings.HasSuffix(path, separatorStr) {
return path
}
return path[:strings.LastIndex(path, separatorStr)]
}