Improve the protection of empty regular expressions (improves issue #61).

This commit is contained in:
Mike Auty
2019-01-18 11:27:24 +00:00
parent 3078141352
commit 0ee335237a
@@ -33,13 +33,15 @@ class MultiRegexp(object):
self._pattern_strings.append(pattern)
def preprocess(self) -> None:
if not self._pattern_strings:
raise ValueError("No strings to compile into a regular expression")
self._regex = re.compile(b'|'.join(map(re.escape, self._pattern_strings)))
def search(self, haystack: bytes) \
-> Generator[Tuple[int, Union[str, bytes]], None, None]:
if not isinstance(haystack, bytes):
raise TypeError("Search haystack must be a byte string")
if not self._regex:
if not self._regex.pattern:
raise ValueError("MultiRegexp cannot be used with an empty set of search strings")
for match in re.finditer(self._regex, haystack):
yield (match.start(0), match.group())