@@ -116,7 +116,7 @@ function mt:checkDirectory(catch, path, matcher)
116
116
end
117
117
118
118
function mt :simpleMatch (path )
119
- path = path : gsub ( ' ^[/ \\ ]+ ' , ' ' )
119
+ path = self : getRelativePath ( path )
120
120
for i = # self .matcher , 1 , - 1 do
121
121
local matcher = self .matcher [i ]
122
122
local catch = matcher (path )
@@ -148,48 +148,66 @@ function mt:finishMatch(path)
148
148
return false
149
149
end
150
150
151
- function mt :scan (root , callback )
151
+ function mt :getRelativePath (path )
152
+ local root = self .options .root or ' '
153
+ if self .options .ignoreCase then
154
+ path = path :lower ()
155
+ root = root :lower ()
156
+ end
157
+ path = path :gsub (' ^[/\\ ]+' , ' ' ):gsub (' [/\\ ]+' , ' /' )
158
+ root = root :gsub (' ^[/\\ ]+' , ' ' ):gsub (' [/\\ ]+' , ' /' )
159
+ if path :sub (1 , # root ) == root then
160
+ path = path :sub (# root + 1 )
161
+ path = path :gsub (' ^[/\\ ]+' , ' ' )
162
+ end
163
+ return path
164
+ end
165
+
166
+ function mt :scan (path , callback )
152
167
local files = {}
153
168
if type (callback ) ~= ' function' then
154
169
callback = nil
155
170
end
156
- local list = { root }
171
+ local list = {}
172
+
173
+ local function check (current )
174
+ local fileType = self :callInterface (' type' , current )
175
+ if fileType == ' file' then
176
+ if callback then
177
+ callback (current )
178
+ end
179
+ files [# files + 1 ] = current
180
+ elseif fileType == ' directory' then
181
+ local result = self :callInterface (' list' , current )
182
+ if type (result ) == ' table' then
183
+ for _ , path in ipairs (result ) do
184
+ local filename = path :match ' ([^/\\ ]+)[/\\ ]*$'
185
+ if filename
186
+ and filename ~= ' .'
187
+ and filename ~= ' ..' then
188
+ list [# list + 1 ] = path
189
+ end
190
+ end
191
+ end
192
+ end
193
+ end
194
+ check (path )
157
195
while # list > 0 do
158
196
local current = list [# list ]
159
197
if not current then
160
198
break
161
199
end
162
200
list [# list ] = nil
163
- if not self :simpleMatch (current ) then
164
- local fileType = self :callInterface (' type' , current )
165
- if fileType == ' file' then
166
- if callback then
167
- callback (current )
168
- end
169
- files [# files + 1 ] = current
170
- elseif fileType == ' directory' then
171
- local result = self :callInterface (' list' , current )
172
- if type (result ) == ' table' then
173
- for _ , path in ipairs (result ) do
174
- local filename = path :match ' ([^/\\ ]+)[/\\ ]*$'
175
- if filename
176
- and filename ~= ' .'
177
- and filename ~= ' ..' then
178
- list [# list + 1 ] = path
179
- end
180
- end
181
- end
182
- end
201
+ local stem = self :getRelativePath (current )
202
+ if not self :simpleMatch (stem ) then
203
+ check (current )
183
204
end
184
205
end
185
206
return files
186
207
end
187
208
188
209
function mt :__call (path )
189
- if self .options .ignoreCase then
190
- path = path :lower ()
191
- end
192
- path = path :gsub (' ^[/\\ ]+' , ' ' )
210
+ path = self :getRelativePath (path )
193
211
return self :finishMatch (path )
194
212
end
195
213
0 commit comments