a
    g	g+                     @   s  U d Z ddlZddlZddlmZ ddlmZmZmZ ddl	m
Z
 ejdk rZddlmZ nddlmZ ddlmZ d	Zeed
< ede d ejZeed< edZeed< edejZeed< ee eeedddZeedddZeee dddZeeedddZeedddZeddd d!Zeedd"d#Z ed$d%eee d&d'd(Z!eedd)d*Z"e
dd+d,d-Z#ed.d%ee$d/d0d1Z%ee$d2d3d4Z&ee$e$d5d6d7Z'dS )8zN
Simple formatting on strings. Further string formatting code is in trans.py.
    N)	lru_cache)ListMatchPattern)Leaf)      )Final)WIDTH_TABLEZfurbFURBSTRING_PREFIX_CHARSz^([z]*)(.*)$STRING_PREFIX_REz\s*\t+\s*(\S)FIRST_NON_WHITESPACE_REz(?P<backslashes>\\+)(?P<body>(u(?P<u>[a-fA-F0-9]{4}))|(U(?P<U>[a-fA-F0-9]{8}))|(x(?P<x>[a-fA-F0-9]{2}))|(N\{(?P<N>[a-zA-Z0-9 \-]{2,})\}))UNICODE_ESCAPE_RE)regexreplacementoriginalreturnc                 C   s   |  ||  ||S )zReplace `regex` with `replacement` twice on `original`.

    This is used by string normalization to perform replaces on
    overlapping matches.
    )sub)r   r   r    r   P/var/www/html/llm_bihealth/app/venv/lib/python3.9/site-packages/black/strings.py	sub_twice#   s    r   )stringr   c                 C   s   |  t}|dd dv S )zS
    Returns:
        True iff @string starts with three quotation characters.
    Nr   >   """''')lstripr   )r   Z
raw_stringr   r   r   has_triple_quotes,   s    
r   )sr   c                 C   s\   g }|   D ]J}t|}|rL|d}||d|  ||d   q|| q|S )zh
    Splits string into lines and expands only leading tabs (following the normal
    Python rules)
       N)
splitlinesr   matchstartappend
expandtabs)r   linesliner   Zfirst_non_whitespace_idxr   r   r    lines_with_leading_tabs_expanded5   s    


r%   )	docstringprefixr   c           
      C   s   | sdS t | }tj}|dd  D ]&}| }|r"t|t|t| }q"|d  g}|tjk rt|d }t|dd  D ]>\}}||d   }	|	s||kr|	||	  q~|	d q~d
|S )N r   r      
)r%   sysmaxsizer   minlenstrip	enumeraterstripr!   join)
r&   r'   r#   indentr$   strippedZtrimmedZlast_line_idxiZstripped_liner   r   r   fix_docstringK   s"    
r6   c                 C   s6   t |  d}d}| | tv r2|| | 7 }|d7 }q|S )z
    Pre-conditions:
        * assert_is_leaf_string(@string)

    Returns:
        @string's prefix (e.g. '', 'r', 'f', or 'rf').
    r(   r   r   )assert_is_leaf_stringr   )r   r'   Z
prefix_idxr   r   r   get_string_prefixc   s    
r8   c                 C   s   |  d}|  d}d||fv r,t||}n
t||}d|  krRt| d k sbn J | d| d dv s|J | dt| d	| ttsJ t| d	|  d
tt dd	S )aH  
    Checks the pre-condition that @string has the format that you would expect
    of `leaf.value` where `leaf` is some Leaf such that `leaf.type ==
    token.STRING`. A more precise description of the pre-conditions that are
    checked are listed below.

    Pre-conditions:
        * @string starts with either ', ", <prefix>', or <prefix>" where
        `set(<prefix>)` is some subset of `set(STRING_PREFIX_CHARS)`.
        * @string ends with a quote character (' or ").

    Raises:
        AssertionError(...) if the pre-conditions listed above are not
        satisfied.
    "'r   r   z0 is missing a starting quote character (' or ").)r:   r9   z/ is missing an ending quote character (' or ").Nz is NOT a subset of .)findmaxr-   r.   setissubsetr   )r   Z
dquote_idxZ
squote_idxZ	quote_idxr   r   r   r7   v   s&    




r7   c                 C   s   t | }|dus J d| |d}|dddddd	d
d	}t|dkrxd|d  krx|ddd }| |d S )z#Make all string prefixes lowercase.Nzfailed to match string r   FfBbUr(   ur)   rr   r;   )r   r   groupreplacer.   lower)r   r   Zorig_prefix
new_prefixr   r   r   normalize_string_prefix   s    

rL   @   )r,   )patternr   c                 C   s
   t | S )N)recompile)rN   r   r   r   _cached_compile   s    rQ   c                 C   s  |  t}|dd dkr| S |dd dkr8d}d}n|d dkrNd}d}nd}d}| |}|dkrl| S | d| }td	| }td
| }td
| }| |t| t|  }	d| v r||	r| S |	}
nXt|d| |	}
|	|
kr|
}	| | |	 | } t|d| |
}
t|d| |
}
d| v rtt	d|
tj
}|D ]}dt|v rV|   S qV|dkr|
dd dkr|
dd d }
|	d}|
d}||kr| S ||kr|dkr| S | | |
 | S )zPrefer double quotes but only if it doesn't cause more escaping.

    Adds or removes backslashes as appropriate. Doesn't parse and fix
    strings nested in f-strings.
    Nr   r   r   r   r9   r:   r;   z(([^\\]|^)(\\\\)*)z([^\\]|^)\\((?:\\\\)*)rG   z\1\2z\1\\rB   z
            (?:(?<!\{)|^)\{  # start of the string or a non-{ followed by a single {
                ([^{].*?)  # contents of the brackets except if begins with {{
            \}(?:(?!\})|$)  # A } followed by end of the string or a non-}
            \z\")r   r   r=   rQ   r.   casefoldsearchr   rO   findallVERBOSEstrcount)r   valueZ
orig_quoteZ	new_quoteZfirst_quote_posr'   Zunescaped_new_quoteZescaped_new_quoteZescaped_orig_quotebodyZnew_bodymatchesmZorig_escape_countZnew_escape_countr   r   r   normalize_string_quotes   s^    



	


r]   )leafr   c                 C   sF   | j }t|}d| v rdS tt tddd}tt||| _ dS )zLReplace hex codes in Unicode escape sequences with lowercase representation.rG   N)r\   r   c                 S   s   |   }|d }t|d dkr,||d  S |d rH|d |d   S |d rd|d |d   S |d r|d |d   S |d sJ d	|  |d
 |d   d S d S )NZbackslashesr)   r   rZ   rF   rE   xNzUnexpected match: zN{})	groupdictr.   rJ   upper)r\   groupsZback_slashesr   r   r   rI     s    z3normalize_unicode_escape_sequences.<locals>.replace)rY   r8   rJ   r   rW   rO   r   r   )r^   textr'   rI   r   r   r   "normalize_unicode_escape_sequences   s    rf   i   )charr   c           	      C   s   t }t| }t|d }d}|d }|| \}}}||k rD|d }n"||krV|d }n|dk rbdS |S ||k rpq~|| d }q$dS )a  Return the width of a single character as it would be displayed in a
    terminal or editor (which respects Unicode East Asian Width).

    Full width characters are counted as 2, while half width characters are
    counted as 1.  Also control characters are counted as 0.
    r   r   r)   )r
   ordr.   )	rg   table	codepointZhighestZlowestidxZstart_codepointZend_codepointwidthr   r   r   
char_width  s    

rm   )line_strr   c                 C   s   |   rt| S ttt| S )a  Return the width of `line_str` as it would be displayed in a terminal
    or editor (which respects Unicode East Asian Width).

    You could utilize this function to determine, for example, if a string
    is too wide to display in a terminal or editor.
    )isasciir.   summaprm   )rn   r   r   r   	str_width7  s    rr   )rn   	max_widthr   c                 C   sB   d}t | D ],\}}t|}|| |kr0|  S ||7 }qt| S )zCount the number of characters in `line_str` that would fit in a
    terminal or editor of `max_width` (which respects Unicode East Asian
    Width).
    r   )r0   rm   r.   )rn   rs   Ztotal_widthr5   rg   rl   r   r   r   count_chars_in_widthD  s    
rt   )(__doc__rO   r+   	functoolsr   typingr   r   r   Zblib2to3.pytreer   version_infoZtyping_extensionsr	   Zblack._width_tabler
   r   __annotations__rP   DOTALLr   r   rV   r   rW   r   boolr   r%   r6   r8   r7   rL   rQ   r]   rf   intrm   rr   rt   r   r   r   r   <module>   sB   
		#J