add start of hostmask matching: mask collapsing
This commit is contained in:
parent
024deb8d0f
commit
8afadd1fdf
3 changed files with 37 additions and 0 deletions
20
ircrobots/glob.py
Normal file
20
ircrobots/glob.py
Normal file
|
@ -0,0 +1,20 @@
|
|||
|
||||
def _collapse(pattern: str) -> str:
|
||||
out = ""
|
||||
i = 0
|
||||
while i < len(pattern):
|
||||
seen_ast = False
|
||||
while pattern[i:] and pattern[i] in ["*", "?"]:
|
||||
if pattern[i] == "?":
|
||||
out += "?"
|
||||
elif pattern[i] == "*":
|
||||
seen_ast = True
|
||||
i += 1
|
||||
if seen_ast:
|
||||
out += "*"
|
||||
|
||||
if pattern[i:]:
|
||||
out += pattern[i]
|
||||
i += 1
|
||||
return out
|
||||
|
1
test/__init__.py
Normal file
1
test/__init__.py
Normal file
|
@ -0,0 +1 @@
|
|||
from .glob import *
|
16
test/glob.py
Normal file
16
test/glob.py
Normal file
|
@ -0,0 +1,16 @@
|
|||
import unittest
|
||||
from ircrobots import glob
|
||||
|
||||
class GlobTestCollapse(unittest.TestCase):
|
||||
def test(self):
|
||||
c1 = glob._collapse("**?*")
|
||||
self.assertEqual(c1, "?*")
|
||||
|
||||
c2 = glob._collapse("a**?a*")
|
||||
self.assertEqual(c2, "a?*a*")
|
||||
|
||||
c3 = glob._collapse("?*?*?*?*a")
|
||||
self.assertEqual(c3, "????*a")
|
||||
|
||||
c4 = glob._collapse("a*?*a?**")
|
||||
self.assertEqual(c4, "a?*a?*")
|
Loading…
Reference in a new issue