/
bpath.go
157 lines (144 loc) · 2.76 KB
/
bpath.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
package bpath
import "ustr"
// allocation-less pathparts
type Pathparts_t struct {
path ustr.Ustr
loc int
}
func (pp *Pathparts_t) Pp_init(path ustr.Ustr) {
pp.path = path
pp.loc = 0
}
func (pp *Pathparts_t) Next() (ustr.Ustr, bool) {
ret := ustr.MkUstr()
for len(ret) == 0 {
if pp.loc == len(pp.path) {
return ustr.MkUstr(), false
}
ret = pp.path[pp.loc:]
nloc := ustr.Ustr.IndexByte(ret, '/')
if nloc != -1 {
ret = ret[:nloc]
pp.loc += nloc + 1
} else {
pp.loc += len(ret)
}
}
return ret, true
}
func Sdirname(path ustr.Ustr) (ustr.Ustr, ustr.Ustr) {
fn := path
l := len(fn)
// strip all trailing slashes
for i := l - 1; i >= 0; i-- {
if fn[i] != uint8('/') {
break
}
fn = fn[:i]
l--
}
var s ustr.Ustr
for i := l - 1; i >= 0; i-- {
if fn[i] == '/' {
// remove the rightmost slash only if it is not the
// first char (the root).
if i == 0 {
s = fn[0:1]
} else {
s = fn[:i]
}
fn = fn[i+1:]
break
}
}
return s, fn
}
const MaxSlash = 60
type canonicalize_t struct {
path ustr.Ustr
slash []int
d int
index int
}
func (canon *canonicalize_t) reset(d int) {
if d >= 0 {
canon.index = canon.slash[d] + 1
canon.d = d
} else {
if canon.path[0] == '/' {
canon.index = 1
canon.d = 1
} else {
canon.d = 0
canon.index = 0
}
}
// fmt.Printf("reset index to %d %d\n", d, canon.index)
}
func (canon *canonicalize_t) add(r uint8) {
canon.path[canon.index] = r
canon.index++
}
func (canon *canonicalize_t) addslash() {
if canon.index > 0 && canon.path[canon.index-1] == '/' {
// skip /
} else {
canon.slash[canon.d] = canon.index
canon.d++
canon.add('/')
}
}
func (canon *canonicalize_t) deltrailingslash() {
if canon.index > 1 && canon.path[canon.index-1] == '/' {
canon.index--
}
}
// Assume utf encoding of characters
func Canonicalize(path ustr.Ustr) ustr.Ustr {
// fmt.Printf("canon: %s\n", path)
canon := canonicalize_t{}
canon.slash = make([]int, MaxSlash)
canon.path = path
lastisdot := false
lastisdotdot := false
for _, u := range path {
// fmt.Printf("%d %#U d %d %v\n", i, u, canon.d, canon.slash)
if u == '/' {
if lastisdot {
canon.reset(canon.d - 1)
}
if lastisdotdot {
canon.reset(canon.d - 2)
}
canon.addslash()
lastisdot = false
lastisdotdot = false
} else if u == '.' {
if lastisdot {
lastisdotdot = true
lastisdot = false
} else {
lastisdot = true
}
continue
} else {
if lastisdot {
canon.add('.')
lastisdot = false
}
if lastisdotdot {
canon.add('.')
canon.add('.')
lastisdotdot = false
}
canon.add(u)
}
}
if lastisdotdot {
canon.reset(canon.d - 2)
}
canon.deltrailingslash()
path = canon.path[:canon.index]
// fmt.Printf("res: %s\n", path)
return path
}