-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglob.go
More file actions
82 lines (78 loc) · 1.42 KB
/
glob.go
File metadata and controls
82 lines (78 loc) · 1.42 KB
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
package gotcl
import "unicode/utf8"
func uncons(s string) (rune, string) {
head, sz := utf8.DecodeRuneInString(s)
if head == utf8.RuneError {
return head, ""
}
return head, s[sz:]
}
func matchcharset(pat, strin string) (bool, string, string) {
if strin == "" {
return false, "", ""
}
sh, str := uncons(strin)
ph, rest := uncons(pat)
got_match := false
for ph != ']' && ph != utf8.RuneError {
if !got_match {
if sh == ph {
got_match = true
} else if rh, rt := uncons(rest); rh == '-' {
var ph2 rune
ph2, rest = uncons(rt)
if ph2 == utf8.RuneError {
return false, "", ""
}
got_match = sh <= ph2 && sh >= ph
}
}
ph, rest = uncons(rest)
}
return got_match, rest, str
}
func GlobMatch(pat, str string) bool {
for pat != "" {
ph, rest := uncons(pat)
switch ph {
case '?':
if str == "" {
return false
}
_, str = uncons(str)
case '[':
is_match := false
is_match, rest, str = matchcharset(rest, str)
if !is_match {
return false
}
case '*':
if rest == "" {
return true
}
for ; str != ""; _, str = uncons(str) {
if GlobMatch(rest, str) {
return true
}
}
return false
default:
if str == "" {
return false
}
if ph == '\\' {
if rest == "" {
return false
}
ph, rest = uncons(rest)
}
var sh rune
sh, str = uncons(str)
if sh != ph {
return false
}
}
pat = rest
}
return str == ""
}