-
Notifications
You must be signed in to change notification settings - Fork 23
/
glob.go
55 lines (43 loc) · 859 Bytes
/
glob.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
package irtt
import (
"strings"
)
const globChar = "*"
func globAny(patterns []string, subj string) bool {
for _, p := range patterns {
if glob(p, subj) {
return true
}
}
return false
}
func glob(pattern, subj string) bool {
if pattern == "" {
return subj == pattern
}
if pattern == globChar {
return true
}
parts := strings.Split(pattern, globChar)
if len(parts) == 1 {
return subj == pattern
}
leadingGlob := strings.HasPrefix(pattern, globChar)
trailingGlob := strings.HasSuffix(pattern, globChar)
end := len(parts) - 1
for i := 0; i < end; i++ {
idx := strings.Index(subj, parts[i])
switch i {
case 0:
if !leadingGlob && idx != 0 {
return false
}
default:
if idx < 0 {
return false
}
}
subj = subj[idx+len(parts[i]):]
}
return trailingGlob || strings.HasSuffix(subj, parts[end])
}