-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathmatch.go
More file actions
46 lines (44 loc) · 810 Bytes
/
match.go
File metadata and controls
46 lines (44 loc) · 810 Bytes
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
package utils
func Match(pattern, s string) bool {
if pattern == "" {
return s == pattern
}
if pattern == "*" {
return true
}
rs := make([]rune, 0, len(s))
rpattern := make([]rune, 0, len(pattern))
for _, r := range s {
rs = append(rs, r)
}
for _, r := range pattern {
rpattern = append(rpattern, r)
}
return matchRune(rs, rpattern)
}
func matchRune(rs, pattern []rune) bool {
for len(pattern) > 0 {
n := len(rs)
switch pattern[0] {
default:
if n == 0 || rs[0] != pattern[0] {
return false
}
case '?':
if n == 0 {
return false
}
case '*':
if matchRune(rs, pattern[1:]) {
return true
}
if n == 0 {
return false
}
return matchRune(rs[1:], pattern)
}
rs = rs[1:]
pattern = pattern[1:]
}
return len(rs) == 0 && len(pattern) == 0
}