Skip to content

Commit 19259cb

Browse files
committed
Improved match function
1 parent dda57a5 commit 19259cb

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

src/manage/pathutils.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -124,19 +124,18 @@ def match(self, pattern, full_match=False):
124124
if "*" not in p:
125125
return m.casefold() == p
126126

127-
must_start_with = not p.startswith("*")
127+
must_start_with = True
128128
for bit in p.split("*"):
129-
if not bit:
130-
continue
131-
try:
132-
i = m.index(bit)
133-
except ValueError:
134-
return False
135-
if must_start_with and i != 0:
136-
return False
137-
m = m[i + len(bit):]
129+
if bit:
130+
try:
131+
i = m.index(bit)
132+
except ValueError:
133+
return False
134+
if must_start_with and i != 0:
135+
return False
136+
m = m[i + len(bit):]
138137
must_start_with = False
139-
return True
138+
return not m or p.endswith("*")
140139

141140

142141
class Path(PurePath):

tests/test_pathutils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,10 @@ def test_path_match():
77
assert p.match("*.exe")
88
assert p.match("python*")
99
assert p.match("python*.exe")
10+
assert p.match("*hon3.*")
1011
assert p.match("p*3.*.exe")
12+
13+
assert not p.match("*.com")
14+
assert not p.match("example*")
15+
assert not p.match("example*.com")
16+
assert not p.match("*ple*")

0 commit comments

Comments
 (0)