diff --git a/pathspec/_backends/simple/gitignore.py b/pathspec/_backends/simple/gitignore.py index 1036fcb..2c93c55 100644 --- a/pathspec/_backends/simple/gitignore.py +++ b/pathspec/_backends/simple/gitignore.py @@ -5,6 +5,7 @@ Its contents and structure are likely to change. """ +import re from collections.abc import ( Sequence) from typing import ( @@ -81,13 +82,17 @@ def _match( file_include: Optional[bool] = None file_index: Optional[int] = None - for index, pattern in self._patterns: - if ( - (include := pattern.include) is not None - and (match := pattern.match_file(file)) is not None - ): + for index, include, regex, pattern in self._tests: + if regex is not None: + match = regex.search(file) + elif (result := pattern.match_file(file)) is not None: + match = result.match + else: + match = None + + if match is not None: # Pattern matched. - if match.match.groupdict().get(_DIR_MARK): + if match.groupdict().get(_DIR_MARK): # A pattern can match both a strict ancestor of the file and the # file itself. For anchored patterns there is only one match, but # `**/` compiles to the unanchored `(?P/)`: on "a/b/" it diff --git a/pathspec/_backends/simple/pathspec.py b/pathspec/_backends/simple/pathspec.py index 2ded1be..ca8cb30 100644 --- a/pathspec/_backends/simple/pathspec.py +++ b/pathspec/_backends/simple/pathspec.py @@ -5,6 +5,8 @@ Its contents and structure are likely to change. """ +import platform +import re from collections.abc import ( Sequence) from typing import ( @@ -13,15 +15,21 @@ from pathspec.backend import ( _Backend) from pathspec.pattern import ( - Pattern) + Pattern, + RegexPattern) from pathspec._typing import ( override) # Added in 3.12. -from pathspec.util import ( - check_match_file) from .._utils import ( enumerate_patterns) +_USE_REGEX = platform.python_implementation() != 'PyPy' +""" +*_USE_REGEX* (:class:`bool`) is whether to test a pattern with its regex +directly. It is faster on CPython, but not on PyPy, where the JIT already +removes the cost of :meth:`.RegexPattern.match_file`. +""" + class SimplePsBackend(_Backend): """ @@ -62,6 +70,26 @@ def __init__( patterns. """ + self._tests: list[tuple[int, bool, Optional[re.Pattern], Pattern]] = [ + (index, pattern.include, ( + pattern.regex + if _USE_REGEX + and type(pattern).match_file is RegexPattern.match_file + and type(pattern.regex) is re.Pattern + else None + ), pattern) + for index, pattern in self._patterns + if pattern.include is not None + ] + """ + *_tests* (:class:`list` of :class:`tuple`) contains the index, include, + regex, and pattern of each pattern with an include. The regex is set when + the pattern uses :meth:`.RegexPattern.match_file` with a :class:`re.Pattern`. + Then the file is tested with the regex search, which gives the same result + without creating a :class:`.RegexMatchResult`. Otherwise, the regex is + :data:`None` and the file is tested with :meth:`.Pattern.match_file`. + """ + @override def match_file(self, file: str) -> tuple[Optional[bool], Optional[int]]: """ @@ -73,4 +101,25 @@ def match_file(self, file: str) -> tuple[Optional[bool], Optional[int]]: or :data:`None`), and the index of the last matched pattern (:class:`int` or :data:`None`). """ - return check_match_file(self._patterns, file, self._is_reversed) + tests = self._tests + if self._is_reversed: + # The first pattern that matches takes precedence. + for index, include, regex, pattern in tests: + if ( + regex.search(file) if regex is not None else pattern.match_file(file) + ) is not None: + return (include, index) + + return (None, None) + + # The last pattern that matches takes precedence. + out_include: Optional[bool] = None + out_index: Optional[int] = None + for index, include, regex, pattern in tests: + if ( + regex.search(file) if regex is not None else pattern.match_file(file) + ) is not None: + out_include = include + out_index = index + + return (out_include, out_index)