Skip to content

Commit cccbbaf

Browse files
committed
Implement simple globbing
1 parent e68c47d commit cccbbaf

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
lines changed

base/exports.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,6 +1146,7 @@ export
11461146
splitdir,
11471147
splitdrive,
11481148
splitext,
1149+
glob,
11491150

11501151
# filesystem operations
11511152
cd,

base/path.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,25 @@ end
122122
if c == '/' return ENV["HOME"]*path[i:end] end
123123
error("~user tilde expansion not yet implemented")
124124
end
125+
126+
# This globs * and ?. [] seems problematic because of line 511 of pkg.jl,
127+
# so square brackets are protected.
128+
# Limited to globbing in the basename
129+
function glob(path::String)
130+
dir, pattern = splitdir(path)
131+
dir = isempty(dir) ? "." : dir
132+
names = readdir(dir)
133+
patterndot = replace(pattern, ['.', '[', ']'], s->string("\\",s))
134+
re = Regex(string('^', replace(replace(patterndot, "?", "."), "*", ".*"), '$'))
135+
paths = Array(ASCIIString, 0)
136+
for name in names
137+
m = match(re, name)
138+
if !is(m, nothing)
139+
push!(paths, joinpath(dir, name))
140+
end
141+
end
142+
if isempty(paths)
143+
error("Cannot access ", path, ": no such file or directory")
144+
end
145+
paths
146+
end

base/string.jl

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -830,19 +830,33 @@ function shell_parse(raw::String, interp::Bool)
830830
j = i
831831

832832
function update_arg(x)
833-
if !isa(x,String)
833+
if !isa(x,String) || !isempty(x)
834834
push!(arg, x)
835-
elseif !isempty(x)
836-
if x[1] == '~'
837-
push!(arg, string(ENV["HOME"], x[2:end]))
838-
else
839-
push!(arg, x)
840-
end
841835
end
842836
end
843837
function append_arg()
844-
if isempty(arg); arg = {"",}; end
845-
push!(args, arg)
838+
if isempty(arg)
839+
push!(args, {"",})
840+
elseif length(arg) == 1
841+
x = arg[1]
842+
if isa(x, String) && !isempty(x)
843+
if x[1] == '~'
844+
x = string(ENV["HOME"], x[2:end])
845+
end
846+
if search(x, ['*', '?']) != 0
847+
paths = glob(x)
848+
for p in paths
849+
push!(args, {p,})
850+
end
851+
else
852+
push!(args, {x,})
853+
end
854+
else
855+
push!(args, arg)
856+
end
857+
else
858+
push!(args, arg)
859+
end
846860
arg = {}
847861
end
848862

0 commit comments

Comments
 (0)