
    zPj3                        d Z ddlmZ ddlmZ ddlmZmZmZm	Z	 	 ddl
Z
ddlmZ ddlmZmZmZmZ ddlmZ d	d
lmZmZmZ d	dlmZ  G d de      Zy# e$ r dZ
Y =w xY w)z
This module provides the :module:`hyperscan` backend for :class:`~pathspec.gitignore.GitIgnoreSpec`.

WARNING: The *pathspec._backends.hyperscan* package is not part of the public
API. Its contents and structure are likely to change.
    )annotations)Sequence)AnyCallableOptionalUnionN)RegexPattern)GitIgnoreSpecPattern_BYTES_ENCODING_DIR_MARK_CG_DIR_MARK_OPT)override   )HS_FLAGSHyperscanExprDatHyperscanExprDebug)HyperscanPsBackendc                       e Zd ZU dZded<   ddd	 	 	 	 	 	 	 d
 fdZee	 	 	 	 	 	 	 	 	 	 dd              Zedd       Z	e	 	 	 	 	 	 	 	 	 	 	 	 dd	       Z
 xZS )HyperscanGiBackendz
	The :class:`HyperscanGiBackend` class is the :module:`hyperscan`
	implementation used by :class:`~pathspec.gitignore.GitIgnoreSpec`. The
	Hyperscan database uses block mode for matching files.
	ztuple[Optional[bool], int, int]_outN_debug_exprs
_test_sortc               8    t         |   |||       d| _        y)z
		Initialize the :class:`HyperscanMatcher` instance.

		*patterns* (:class:`Sequence` of :class:`.RegexPattern`) contains the
		compiled patterns.
		r   Nr   N)super__init__r   )selfpatternsr   r   	__class__s       ^/root/$HERMES_HOME/venv/lib/python3.12/site-packages/pathspec/_backends/hyperscan/gitignore.pyr   zHyperscanGiBackend.__init__2   s%     '8,:N$)    c           
        |sJ |       g }g }|D ]  \  }}|j                   	J ||f       |j                  	J ||f       t        |t              sJ |       |j                  j                  }g }	t        |t
              rt        |t              r|}
n,t        |t              sJ |       |j                  t              }
t        |
v r~|
j                  t              r@|
dt        t                }|	j                  | ddf       |	j                  | ddf       n)|
j                  t        d      }|	j                  |df       |	s|	j                  |df       |	D ]  \  }}t        |t              r|}n(t        |t              sJ |       |j!                  d      }|r)|j                  t#        |j                   |||             n'|j                  t%        |j                   ||             |j                  |         t'        t)        t        |                  }| ||       |D cg c]  }||   	 }}| j+                  ||t        |      t,        	       |S c c}w )
a  
		Create the Hyperscan database from the given patterns.

		*db* (:class:`hyperscan.Hyperscan`) is the Hyperscan database.

		*debug* (:class:`bool`) is whether to include additional debugging
		information for the expressions.

		*patterns* (:class:`~collections.abc.Sequence` of :class:`.RegexPattern`)
		contains the patterns.

		*sort_ids* (:class:`callable` or :data:`None`) is a function used to sort
		the compiled expression ids. This is used during testing to ensure the order
		of expressions is not accidentally relied on.

		Returns a :class:`list` indexed by expression id (:class:`int`) to its data
		(:class:`HyperscanExprDat`).
		N/T$Futf8)includeindexis_dir_patternregex)r(   r)   r*   )expressionsidselementsflags)r(   r+   
isinstancer	   patternr
   strbytesdecoder   r   endswithr   lenappendreplaceencoder   r   listrangecompiler   )dbdebugr    sort_ids	expr_dataexprspattern_indexr1   r+   use_regexes	regex_str
base_regexr*   regex_bytesr-   _HyperscanGiBackend__ids                   r"   _init_dbzHyperscanGiBackend._init_dbL   sy   8 
8 '))% ( 9mW
//
%?w'??
%
--
#=mW%==
# Wl
+4W4
+==  579;./ %Yue$+e+$o.Yy =) 1s=112jZL*D12ZL*E23 $$\37j*d+,
u~& + un%[uc")E)"<<'[(oo#	  &oo#  
LL+I9x 	U3u:#C=$'(DE$K(5( **
J		   
 )s   0I"c                    | j                   }|yd| _        |j                  |j                  d      | j                         | j                  dd \  }}|dk(  rd}||fS )a  
		Check the file against the patterns.

		*file* (:class:`str`) is the normalized file path to check.

		Returns a :class:`tuple` containing whether to include *file* (:class:`bool`
		or :data:`None`), and the index of the last matched pattern (:class:`int` or
		:data:`None`).
		N)NNr   r'   )match_event_handler   r   )_dbr   scanr9   _HyperscanGiBackend__on_match)r   filer=   out_include	out_indexs        r"   
match_filezHyperscanGiBackend.match_file   sh     xx"Z $)''$++f
4??'C  99Ra=+y"_9
y	!!r#   c                   | j                   |   }|j                  }|rd}nd}|j                  }	|j                  }
| j                  d   }| j                  d   }|	r|r|
|kD  s||k(  r|
|kD  s||kD  r|	|j                  |f}|| _        y)zn
		Called on each match.

		*expr_id* (:class:`int`) is the expression id (index) of the matched
		pattern.
		r   rK   N)
_expr_datar*   r(   r)   r   )r   expr_id_from_to_flags_contextexpr_datr*   priorityr(   r)   
prev_indexprev_priorityout_tups                 r"   
__on_matchzHyperscanGiBackend.__on_match   s     __W%(**.8 8 '
..%yy|*))A,-5:#5= UZ%7x~~x0749	r#   )r    zSequence[RegexPattern]r   Optional[bool]r   z Optional[Callable[[list], None]]returnNone)
r=   zhyperscan.Databaser>   boolr    zlist[tuple[int, RegexPattern]]r?   z%Optional[Callable[[list[int]], None]]ra   zlist[HyperscanExprDat])rO   r2   ra   z$tuple[Optional[bool], Optional[int]])rU   intrV   rd   rW   rd   rX   rd   rY   r   ra   r`   )__name__
__module____qualname____doc____annotations__r   r   staticmethodrH   rR   rN   __classcell__)r!   s   @r"   r   r   (   s     '& "&15" 	
 / 4 gg	g +g 2	g
 g  gR " "8 %% 
% 	%
 % % % %r#   r   )rh   
__future__r   collections.abcr   typingr   r   r   r   	hyperscanModuleNotFoundErrorpathspec.patternr	    pathspec.patterns.gitignore.specr
   r   r   r   pathspec._typingr   _baser   r   r   pathspecr   r    r#   r"   <module>rw      sn    #  
 R+ R+  s   A AA