Skip to content

Commit

Permalink
fix globstar_period, simplify globstar loop
Browse files Browse the repository at this point in the history
  • Loading branch information
hhaensel committed Jun 9, 2024
1 parent 4880270 commit 783e3d9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
31 changes: 16 additions & 15 deletions src/Glob.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,14 @@ function occursin(fn::FilenameMatch, s::AbstractString)
starmatch = i

# globstar_index = 1
globstar_mi = Int[]
globstarmatch = Int[]
globstar = 0
globstar_mi = 0
globstarmatch = 0
period = periodfl
globstar_period = false
globstar_period = false # leading period detected during globstar match
while true
star = 0
match_fails = false
isempty(globstar_mi) ||(mi = globstar_mi[end]) # reset pattern index of the latest globstar pattern, if it exists
globstar_mi > 0 && (mi = globstar_mi[end]) # reset pattern index of the latest globstar pattern, if it exists
while true
matchnext = iterate(s, i)
matchnext === nothing && break
Expand All @@ -90,18 +89,20 @@ function occursin(fn::FilenameMatch, s::AbstractString)
match = false # string characters left to match, but no pattern left
else
mc, mi = patnext
if mc == '*' && pathname && length(pattern) > mi && pattern[mi:nextind(pattern, mi)] == "*/"
@debug s[i:end], pattern[mi:end]
if mc == '*' && pathname && length(pattern) > mi && pattern[mi:nextind(pattern, mi)] == "*/"
star = 0
mi += 2
push!(globstarmatch, i)
push!(globstar_mi, mi)
globstarmatch = i
globstar_mi = mi
c = '/' # fake previous character to indicate end of directory
match = true
elseif mc == '*'
starmatch = i # backup the current search index
star = mi
c, _ = matchnext # peek-ahead
if period & (c == '.')
globstar_period = globstarmatch > 0
(match_fails = true) && break
end
match = true
Expand All @@ -120,7 +121,7 @@ function occursin(fn::FilenameMatch, s::AbstractString)
(match_fails = true) && break
end
if period & (c == '.')
# match = false
globstar_period = globstarmatch > 0
(match_fails = true) && break
end
match = true
Expand All @@ -133,6 +134,7 @@ function occursin(fn::FilenameMatch, s::AbstractString)
end
match = ((c == mc) || (caseless && uppercase(c)==uppercase(mc)))
end
globstar_period = period && globstarmatch > 0 && (c == '.')
end
end
if !match # try to backtrack and add another character to the last *
Expand All @@ -145,7 +147,6 @@ function occursin(fn::FilenameMatch, s::AbstractString)
starmatch = i
end
period = (periodfl & pathname & (c == '/'))
globstar_period = period && !isempty(globstarmatch)
end
while true # allow trailing *'s
patnext = iterate(pattern, mi)
Expand All @@ -158,18 +159,18 @@ function occursin(fn::FilenameMatch, s::AbstractString)
end
if match_fails
# if in a globstar move to next directory, otherwise return false
if !isempty(globstarmatch)
if globstarmatch > 0
x = findnext('/', s, globstarmatch[end])
if x === nothing || globstar_period
pop!(globstarmatch)
pop!(globstar_mi)
globstarmatch = 0
globstar_mi = 0
globstar_period = false
else
globstarmatch[end] = i = x + 1
globstarmatch = i = x + 1
period = periodfl
end
end
isempty(globstarmatch) && return false
globstarmatch == 0 && return false
else
return true
end
Expand Down
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ end
@test !occursin(fn"*/**/*.png"d, "c.png")
@test !occursin(fn"**/*/*.png"d, "c.png")

@test occursin(fn"**/c/**/*png"dp, "a/b/c/d/e/*png")
@test occursin(fn"**/c/**/*png"d, "a/b/c/d/e/.png")
@test !occursin(fn"**/c/**/*png"dp, "a/b/c/d/e/.png")

function test_string(x1)
x2 = string(eval(Meta.parse(x1)))
x1 == x2 ? nothing : error(string(
Expand Down

0 comments on commit 783e3d9

Please sign in to comment.