-
Notifications
You must be signed in to change notification settings - Fork 568
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1278 from blacklanternsecurity/faster-regexes
Faster Regexes
- Loading branch information
Showing
40 changed files
with
415 additions
and
257 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import regex as re | ||
from . import misc | ||
|
||
|
||
class RegexHelper: | ||
""" | ||
Class for misc CPU-intensive regex operations | ||
Offloads regex processing to other CPU cores via GIL release + thread pool | ||
For quick, one-off regexes, you don't need to use this helper. | ||
Only use this helper if you're searching large bodies of text | ||
or if your regex is CPU-intensive | ||
""" | ||
|
||
def __init__(self, parent_helper): | ||
self.parent_helper = parent_helper | ||
|
||
def ensure_compiled_regex(self, r): | ||
""" | ||
Make sure a regex has been compiled | ||
""" | ||
if not isinstance(r, re.Pattern): | ||
raise ValueError("Regex must be compiled first!") | ||
|
||
def compile(self, *args, **kwargs): | ||
return re.compile(*args, **kwargs) | ||
|
||
async def search(self, compiled_regex, *args, **kwargs): | ||
self.ensure_compiled_regex(compiled_regex) | ||
return await self.parent_helper.run_in_executor(compiled_regex.search, *args, **kwargs) | ||
|
||
async def findall(self, compiled_regex, *args, **kwargs): | ||
self.ensure_compiled_regex(compiled_regex) | ||
return await self.parent_helper.run_in_executor(compiled_regex.findall, *args, **kwargs) | ||
|
||
async def finditer(self, compiled_regex, *args, **kwargs): | ||
self.ensure_compiled_regex(compiled_regex) | ||
return await self.parent_helper.run_in_executor(self._finditer, compiled_regex, *args, **kwargs) | ||
|
||
async def finditer_multi(self, compiled_regexes, *args, **kwargs): | ||
""" | ||
Same as finditer() but with multiple regexes | ||
""" | ||
for r in compiled_regexes: | ||
self.ensure_compiled_regex(r) | ||
return await self.parent_helper.run_in_executor(self._finditer_multi, compiled_regexes, *args, **kwargs) | ||
|
||
def _finditer_multi(self, compiled_regexes, *args, **kwargs): | ||
matches = [] | ||
for r in compiled_regexes: | ||
for m in r.finditer(*args, **kwargs): | ||
matches.append(m) | ||
return matches | ||
|
||
def _finditer(self, compiled_regex, *args, **kwargs): | ||
return list(compiled_regex.finditer(*args, **kwargs)) | ||
|
||
async def extract_params_html(self, *args, **kwargs): | ||
return await self.parent_helper.run_in_executor(misc.extract_params_html, *args, **kwargs) | ||
|
||
async def extract_emails(self, *args, **kwargs): | ||
return await self.parent_helper.run_in_executor(misc.extract_emails, *args, **kwargs) | ||
|
||
async def search_dict_values(self, *args, **kwargs): | ||
def _search_dict_values(*_args, **_kwargs): | ||
return list(misc.search_dict_values(*_args, **_kwargs)) | ||
|
||
return await self.parent_helper.run_in_executor(_search_dict_values, *args, **kwargs) | ||
|
||
async def recursive_decode(self, *args, **kwargs): | ||
return await self.parent_helper.run_in_executor(misc.recursive_decode, *args, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.