-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfastpath.go
161 lines (144 loc) · 3.19 KB
/
fastpath.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
package fastpath
import (
"strings"
)
type seg struct {
Param string
Const string
IsParam bool
IsOptional bool
IsLast bool
}
// Path ...
type Path struct {
Segs []seg
Params []string
}
const wildcardParam string = "*"
// dummy slice with string, we use it to avoid the slice creation
var paramsDummy = make([]string, 100, 100)
// New ...
func New(pattern string) (p Path) {
var patternCount int
aPattern := []string{""}
if pattern != "" {
aPattern = strings.Split(pattern, "/")[1:] // every route starts with an "/"
}
patternCount = len(aPattern)
var out = make([]seg, patternCount)
var params []string
var segIndex int
for i := 0; i < patternCount; i++ {
partLen := len(aPattern[i])
if partLen == 0 { // skip empty parts
continue
}
// is parameter ?
if aPattern[i][0] == '*' || aPattern[i][0] == ':' {
out[segIndex] = seg{
Param: paramTrimmer(aPattern[i]),
IsParam: true,
IsOptional: aPattern[i] == wildcardParam || aPattern[i][partLen-1] == '?',
}
params = append(params, out[segIndex].Param)
} else {
// combine const segments
if segIndex > 0 && out[segIndex-1].IsParam == false {
segIndex--
out[segIndex].Const += "/" + aPattern[i]
// create new const segment
} else {
out[segIndex] = seg{
Const: aPattern[i],
}
}
}
segIndex++
}
if segIndex == 0 {
segIndex++
}
out[segIndex-1].IsLast = true
p = Path{Segs: out[:segIndex:segIndex], Params: params}
return
}
// Match ...
func (p *Path) Match(s string) ([]string, bool) {
lenKeys := len(p.Params)
params := paramsDummy[0:lenKeys:lenKeys]
var i, j, paramsIterator, partLen int
if len(s) > 0 {
s = s[1:]
}
for index, segment := range p.Segs {
partLen = len(s)
// check parameter
if segment.IsParam {
// determine parameter length
if segment.Param == wildcardParam {
if segment.IsLast {
i = partLen
} else {
// for the expressjs behavior -> "/api/*/:param" - "/api/joker/batman/robin/1" -> "joker/batman/robin", "1"
i = findCharPos(s, '/', strings.Count(s, "/")-(len(p.Segs)-(index+1))+1)
}
} else {
i = strings.IndexByte(s, '/')
}
if i == -1 {
i = partLen
}
if false == segment.IsOptional && i == 0 {
return nil, false
}
params[paramsIterator] = s[:i]
paramsIterator++
} else {
// check const segment
i = len(segment.Const)
if partLen < i || (i == 0 && partLen > 0) || s[:i] != segment.Const || (partLen > i && s[i] != '/') {
return nil, false
}
}
// reduce founded part from the string
if partLen > 0 {
j = i + 1
if segment.IsLast || partLen < j {
j = i
}
s = s[j:]
}
}
if len(s) != 0 {
return nil, false
}
return params, true
}
func paramTrimmer(param string) string {
start := 0
end := len(param)
if param[start] != ':' { // is not a param
return param
}
start++
if param[end-1] == '?' { // is ?
end--
}
return param[start:end]
}
func findCharPos(s string, char byte, matchCount int) int {
if matchCount == 0 {
matchCount = 1
}
endPos, pos := 0, 0
for matchCount > 0 && pos != -1 {
if pos > 0 {
s = s[pos+1:]
endPos++
}
pos = strings.IndexByte(s, char)
endPos += pos
matchCount--
}
return endPos
}