-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
43 lines (34 loc) · 1011 Bytes
/
utils.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
package alox
import (
"path"
"strings"
)
func ShiftHead(value string) (head, tail string) {
value = path.Clean("/" + value)
splitIndex := strings.Index(value[1:], "/") + 1
if splitIndex <= 0 {
return value[1:], "/"
}
return value[1:splitIndex], value[splitIndex:]
}
func ShiftAndAssertHead(value string, assert func(head string) bool) (passed bool, tail string) {
head, tail := ShiftHead(value)
return assert(head), tail
}
func ShiftAndMatchHead(value string, head string) (matched bool, tail string) {
return ShiftAndAssertHead(value, func(actualHead string) bool {
return actualHead == head
})
}
func AssertHead(value string, assert func(head string) bool) (passed bool) {
head, _ := ShiftHead(value)
return assert(head)
}
func MatchHead(value string, head string) (matched bool) {
return AssertHead(value, func(actualHead string) bool {
return actualHead == head
})
}
func HasPrefix(value string, prefix string) bool {
return strings.HasPrefix(path.Clean("/"+value), prefix)
}