-
Notifications
You must be signed in to change notification settings - Fork 0
/
components.go
59 lines (48 loc) · 936 Bytes
/
components.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 trail
import (
"bytes"
)
type state int64
const (
startDir state = iota
body
done
)
type Components struct {
hasPhysicalRoot bool
trail []byte
front state
back state
}
func (c *Components) Next() []byte {
switch c.front {
case startDir:
c.front = body // move up one state
if c.hasPhysicalRoot {
c.trail = c.trail[1:]
return []byte{rootDir}
}
return c.parseNextComponent()
case body:
return c.parseNextComponent()
case done:
return []byte{}
}
return []byte{}
}
func (c *Components) parseNextComponent() []byte {
index := bytes.Index(c.trail, []byte(string(separator)))
if index < 0 {
c.front = done
return c.trail
}
component := c.trail[:index]
c.trail = c.trail[index+1:]
return component
}
func (c *Components) finished() bool {
return c.front == done || c.back == done || c.front > c.back
}
func isSepByte(b byte) bool {
return b == '/'
}