diff --git a/ircrobots/glob.py b/ircrobots/glob.py new file mode 100644 index 0000000..f5eebdf --- /dev/null +++ b/ircrobots/glob.py @@ -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 + diff --git a/test/__init__.py b/test/__init__.py new file mode 100644 index 0000000..edb458e --- /dev/null +++ b/test/__init__.py @@ -0,0 +1 @@ +from .glob import * diff --git a/test/glob.py b/test/glob.py new file mode 100644 index 0000000..d1ff48c --- /dev/null +++ b/test/glob.py @@ -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?*")