-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
73 lines (57 loc) · 1.55 KB
/
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
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
package alox
import (
"path"
"strings"
"golang.org/x/net/html"
)
func findNode(node *html.Node, predicate func(node *html.Node) bool) *html.Node {
if node == nil {
return nil
}
if predicate(node) {
return node
}
for child := node.FirstChild; child != nil; child = child.NextSibling {
if foundNode := findNode(child, predicate); foundNode != nil {
return foundNode
}
}
return nil
}
func findElement(node *html.Node, name string) *html.Node {
return findNode(node, func(node *html.Node) bool {
if node.Type == html.ElementNode && node.Data == name {
return true
}
return false
})
}
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)
}