forked from gofiber/fiber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparams.go
165 lines (148 loc) · 3.64 KB
/
params.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
162
163
164
165
// ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
// 📝 Github Repository: https://github.com/gofiber/fiber
// 📌 API Documentation: https://docs.gofiber.io
// ⚠️ This path parser was based on urlpath by @ucarion (MIT License).
// 💖 Modified for the Fiber router by @renanbastos93 & @renewerner87
// 🤖 ucarion/urlpath - renanbastos93/fastpath - renewerner87/fastpath
package fiber
import (
"strings"
)
// paramsParser holds the path segments and param names
type parsedParams struct {
Segs []paramSeg
Keys []string
}
// paramsSeg holds the segment metadata
type paramSeg struct {
Param string
Const string
IsParam bool
IsOptional bool
IsLast bool
}
var paramsDummy = make([]string, 100, 100)
// New ...
func parseParams(pattern string) (p parsedParams) {
if pattern[0] != '/' {
pattern = "/" + pattern
}
var patternCount int
aPattern := []string{""}
if pattern != "" {
aPattern = strings.Split(pattern, "/")[1:] // every route starts with an "/"
}
patternCount = len(aPattern)
var out = make([]paramSeg, 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] = paramSeg{
Param: paramTrimmer(aPattern[i]),
IsParam: true,
IsOptional: aPattern[i] == "*" || 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] = paramSeg{
Const: aPattern[i],
}
}
}
segIndex++
}
if segIndex == 0 {
segIndex++
}
out[segIndex-1].IsLast = true
p = parsedParams{Segs: out[:segIndex:segIndex], Keys: params}
// fmt.Printf("%+v\n", p)
return
}
// Match ...
func (p *parsedParams) matchParams(s string) ([]string, bool) {
lenKeys := len(p.Keys)
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.IsLast {
i = partLen
} else if segment.Param == "*" {
// 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:]
}
}
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
}