-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpath.go
105 lines (95 loc) · 2.39 KB
/
path.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
// by yyrdl ,MIT License
package gbeta
import (
"strings"
)
//定义_Path,而不直接是string对比的原因在于为以后再路径中添加正则表达式留接口
type _Path struct {
path string
more_op bool
}
//maybe will rewrite the path code to support path param or regexp
func newPath(path string) *_Path {
var p = new(_Path)
p.path = path
p.more_op = needMoreOp(path)
return p
}
//more_op表示该路径不能只是简单的比较字符串,可能需要正则
func needMoreOp(path string) bool {
if len(path) > 0 {
if path[:1] == ":" { //路径寻找的时候用到路由上路径的长度,若路由上出现参数则会出错
return true
}
}
return false
}
//若日后想要添加正则表达式功能直接修改这个path的相应函数即可
func (p *_Path) Match(subPath string) (is_match bool, key string, value string, left_path string) {
if p.path[:1] == ":" {
var i int = 0
for ; i < len(subPath); i++ {
if subPath[i] == 47 { //"/"
return true, p.path[1:], subPath[:i], subPath[i+1:]
}
if subPath[i] == 63 { // "?"
return true, p.path[1:], subPath[:i], ""
}
}
return true, p.path[1:], subPath, ""
}
return false, "", "", subPath
}
//在pathNode.go的addNode函数里用到,用来比较俩个子路径是否一致
func (p *_Path) IsSimillar(path string) bool {
return p.path == path
}
//在生成路由树的时候用到,格式化路径,避免"path/p" "/sa///asa/"这样的路径出现
//这是最耗时的操作,在路由请求时不再会用到,路由请求时默认req.URL.Path是标准的
//对"ds/ds","/ds/ds","//ds//ds"都将输出[]string{"","ds","ds"}
func formatPath(url string) []string {
var (
start int = 0
num int = 1
)
url = strings.Join(strings.Split(url, " "), "")
temp := make([]string, 10)
temp[0] = ""
for i := 0; i < len(url); i++ {
if url[i:i+1] == "/" {
if start != i {
if num < 10 {
temp[num] = url[start:i]
} else {
temp = append(temp, url[start:i])
}
num += 1
}
start = i + 1
continue
}
if i == len(url)-1 {
if start != i {
if num < 10 {
temp[num] = url[start:]
} else {
temp = append(temp, url[start:])
}
num += 1
}
break
}
if url[i:i+1] == "?" {
if start != i {
if num < 10 {
temp[num] = url[start:i]
} else {
temp = append(temp, url[start:i])
}
num += 1
}
break
}
}
return temp[:num]
}