-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathglob.go
175 lines (162 loc) · 3.73 KB
/
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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package main
import (
"net/http"
"path"
"regexp"
"strings"
)
type globMatcherContext struct {
fs http.FileSystem
path string
restOfComponents []string
resultCollector func(string) error
}
func doMatch(context globMatcherContext) error {
if len(context.restOfComponents) == 0 {
return context.resultCollector(context.path)
}
f, err := context.fs.Open(context.path)
if err != nil {
return err
}
defer f.Close()
info, err := f.Stat()
if err != nil {
return err
}
if !info.IsDir() {
return nil
}
entries, err := f.Readdir(-1)
if err != nil {
return err
}
for _, entry := range entries {
name := entry.Name()
matched, err := path.Match(context.restOfComponents[0], name)
if err != nil {
return err
}
if matched {
err := doMatch(globMatcherContext{
fs: context.fs,
path: path.Join(context.path, name),
restOfComponents: context.restOfComponents[1:],
resultCollector: context.resultCollector,
})
if err != nil {
return err
}
}
}
return nil
}
func Glob(fs http.FileSystem, pattern string) ([]string, error) {
retval := make([]string, 0)
components := strings.Split(pattern, "/")
var path_ string
if len(components) > 0 && components[0] == "" {
path_ = "/"
components = components[1:]
} else {
path_ = "."
}
return retval, doMatch(globMatcherContext{
fs: fs,
path: path_,
restOfComponents: components,
resultCollector: func(match string) error {
retval = append(retval, match)
return nil
},
})
}
type PatternError struct {
message string
}
func (self *PatternError) Error() string {
return self.message
}
func buildRegexpFromGlobPatternInner(pattern string, startPos int) (string, int, error) {
state := 0
chunk := ""
patternLength := len(pattern)
var i int
for i = startPos; i < patternLength; i += 1 {
if state == 0 {
c := pattern[i]
if c == '*' {
state = 2
} else if c == '}' || c == ',' {
break
} else if c == '{' {
chunk += "(?:"
first := true
for {
i += 1
subchunk, lastPos, err := buildRegexpFromGlobPatternInner(pattern, i)
if err != nil {
return "", 0, err
}
if lastPos == patternLength {
return "", 0, &PatternError{"unexpected end of pattern (in expectation of '}' or ',')"}
}
if !first {
chunk += "|"
}
i = lastPos
chunk += "(?:" + subchunk + ")"
first = false
if pattern[lastPos] == '}' {
break
} else if pattern[lastPos] != ',' {
return "", 0, &PatternError{"never get here"}
}
}
chunk += ")"
} else {
chunk += regexp.QuoteMeta(string(c))
}
} else if state == 1 {
// escape
c := pattern[i]
chunk += regexp.QuoteMeta(string(c))
state = 0
} else if state == 2 {
c := pattern[i]
if c == '*' {
state = 3
} else {
chunk += "[^.]*" + regexp.QuoteMeta(string(c))
state = 0
}
} else if state == 3 {
// recursive any
c := pattern[i]
if c == '*' {
return "", 0, &PatternError{"unexpected *"}
} else if c == '.' {
chunk += "(?:.*\\.|^)"
} else {
chunk += ".*" + regexp.QuoteMeta(string(c))
}
state = 0
}
}
if state == 2 {
chunk += "[^.]*"
} else if state == 3 {
chunk += ".*"
}
return chunk, i, nil
}
func BuildRegexpFromGlobPattern(pattern string) (string, error) {
chunk, pos, err := buildRegexpFromGlobPatternInner(pattern, 0)
if err != nil {
return "", err
}
if pos != len(pattern) {
return "", &PatternError{"unexpected '" + string(pattern[pos]) + "'"}
}
return "^" + chunk + "$", nil
}