mirror of
https://github.com/volatilityfoundation/volatility3.git
synced 2026-09-26 19:44:50 +02:00
Relented on the strict import of direct objects/classes for the typing module only. Typing module components can be directly imported because it makes the code really painful to read and write otherwise. This is still in-line with the python style guide adopted from Google at http://google.github.io/styleguide/pyguide.html section 2.2.
30 lines
914 B
Python
30 lines
914 B
Python
import re
|
|
from typing import Generator, List, Tuple, Union
|
|
|
|
|
|
class MultiRegexp(object):
|
|
"""Algorithm for multi-string matching"""
|
|
|
|
def __init__(self) -> None:
|
|
self._pattern_strings = [] # type: List[bytes]
|
|
self._regex = re.compile(b'')
|
|
|
|
def add_pattern(self, pattern: bytes) -> None:
|
|
self._pattern_strings.append(pattern)
|
|
|
|
def preprocess(self) -> None:
|
|
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")
|
|
for match in re.finditer(self._regex, haystack):
|
|
yield (match.start(0), match.group())
|
|
|
|
|
|
if __name__ == '__main__':
|
|
import multistring_testrig
|
|
|
|
multistring_testrig.tester(MultiRegexp())
|