Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add globstar support #21

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Glob.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ function occursin(fn::FilenameMatch, s::AbstractString)
i = firstindex(s) # current index into s
starmatch = i
star = 0
is_starstar = false # If current star is a double star
period = periodfl
while true
matchnext = iterate(s, i)
Expand All @@ -66,6 +67,9 @@ function occursin(fn::FilenameMatch, s::AbstractString)
else
mc, mi = patnext
if mc == '*'
# Detect globstar, only in extended mode, two stars in a row
is_starstar = extended && star > 0 && star==mi-1

starmatch = i # backup the current search index
star = mi
c, _ = matchnext # peek-ahead
Expand Down Expand Up @@ -105,7 +109,7 @@ function occursin(fn::FilenameMatch, s::AbstractString)
if !match # try to backtrack and add another character to the last *
star == 0 && return false
c, i = something(iterate(s, starmatch)) # starmatch is strictly <= i, so it is known that it must be a valid index
if pathname & (c == '/')
if !is_starstar && pathname & (c == '/')
return false # * does not match /
end
mi = star
Expand Down
20 changes: 20 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,26 @@ end
@test occursin(fn"\?"e, "\\!")
@test !occursin(fn"\?"e, "?")

@testset "GlobStar" begin
@test occursin(fn"A/**"pdx, "A/b")
@test occursin(fn"A/**"pdx, "A/b/c")
@test !occursin(fn"A/**"pdx, "B/b")
@test !occursin(fn"A/**"pdx, "B/b/c")

@test occursin(fn"A/**c"pdx, "A/c")
@test occursin(fn"A/**c"pdx, "A/b/c")
@test occursin(fn"A/**c"pdx, "A/bc")
@test occursin(fn"A/**c"pdx, "A/x/bc")
@test occursin(fn"A/**c"pdx, "A/x/y/bc")

@test !occursin(fn"A/**/c"pdx, "A/c")
@test occursin(fn"A/**/c"pdx, "A/b/c")
@test !occursin(fn"A/**/c"pdx, "A/bc")
@test !occursin(fn"A/**/c"pdx, "A/x/bc")
@test !occursin(fn"A/**/c"pdx, "A/x/y/bc")
end


@test_types glob"ab/?/d".pattern (AbstractString, Glob.FilenameMatch, AbstractString)
@test_types glob"""ab/*/d""".pattern (AbstractString, Glob.FilenameMatch, AbstractString)
@test length(glob"ab/[/d".pattern) == 3
Expand Down