From 8afadd1fdf0538863d0303f9d4a1951f00f5aaa3 Mon Sep 17 00:00:00 2001 From: jesopo Date: Mon, 27 Apr 2020 00:41:17 +0100 Subject: [PATCH] add start of hostmask matching: mask collapsing --- ircrobots/glob.py | 20 ++++++++++++++++++++ test/__init__.py | 1 + test/glob.py | 16 ++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 ircrobots/glob.py create mode 100644 test/__init__.py create mode 100644 test/glob.py 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?*")