-
Notifications
You must be signed in to change notification settings - Fork 7
/
interceptor.go
58 lines (48 loc) · 1.14 KB
/
interceptor.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
// SPDX-License-Identifier: MIT
package syntax
import "fmt"
// InterceptorFunc 拦截器的处理函数
type InterceptorFunc func(string) bool
type Interceptors struct {
funcs map[string]InterceptorFunc
}
func NewInterceptors() *Interceptors {
return &Interceptors{
funcs: map[string]InterceptorFunc{},
}
}
func (i *Interceptors) Add(f InterceptorFunc, name ...string) {
if len(name) == 0 {
panic("参数 name 不能为空")
}
for _, n := range name {
if _, found := i.funcs[n]; found {
panic(fmt.Errorf("%s 已经存在", n))
}
i.funcs[n] = f
}
}
// MatchAny 匹配任意非空内容
func MatchAny(path string) bool { return len(path) > 0 }
// MatchDigit 匹配数值字符
//
// 与正则表达式中的 [0-9]+ 是相同的。
func MatchDigit(path string) bool {
for _, c := range path {
if c < '0' || c > '9' {
return false
}
}
return len(path) > 0
}
// MatchWord 匹配单词
//
// 与正则表达式中的 [a-zA-Z0-9]+ 是相同的。
func MatchWord(path string) bool {
for _, c := range path {
if (c < '0' || c > '9') && (c < 'a' || c > 'z') && (c < 'A' || c > 'Z') {
return false
}
}
return len(path) > 0
}