o
    b                     @   s   d Z ddlZddlZddlmZmZ dZdee dededefd	d
Z	dede
fddZdedee fddZdededefddZdedefddZdeddfddZddede
defddZdedefddZdS ) zN
Simple formatting on strings. Further string formatting code is in trans.py.
    N)ListPatternZfurbFURB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	   L/var/www/secure340b-portal/env/lib/python3.10/site-packages/black/strings.py	sub_twice   s   r   stringc                 C   s   |  t}|dd dv S )zS
    Returns:
        True iff @string starts with three quotation characters.
    N   >   """''')lstripSTRING_PREFIX_CHARS)r   Z
raw_stringr	   r	   r
   has_triple_quotes   s   
r   sc                 C   s^   g }|   D ]&}td|}|r'|d}||d|  ||d   q|| q|S )zh
    Splits string into lines and expands only leading tabs (following the normal
    Python rules)
    z\s*\t+\s*(\S)   N)
splitlinesrematchstartappend
expandtabs)r   linesliner   Zfirst_non_whitespace_idxr	   r	   r
    lines_with_leading_tabs_expanded   s   

r   	docstringprefixc           
      C   s   | sdS t | }tj}|dd  D ]}| }|r$t|t|t| }q|d  g}|tjk r_t|d }t|dd  D ]\}}||d   }	|	sQ||krY|	||	  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_docstring5   s$   

r.   c                 C   s@   t |  d}d}| | tv r|| | 7 }|d7 }| | tv s|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_prefixM   s   r0   c                 C   s   |  d}|  d}d||fv rt||}nt||}d|  kr)t| d k s1n J | d| d dv s>J | dt| d	| tts]J 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 ").)r2   r1   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
   r/   `   s&   




r/   Fremove_u_prefixc                 C   sv   t dt d | t j}|dusJ d| |d}|dddd	d
d}|r2|dd}| |d S )zuMake all string prefixes lowercase.

    If remove_u_prefix is given, also removes any u prefix from the string.
    z^([z]*)(.*)$Nzfailed to match string r   FfBbUur    r!   )r   r   r   DOTALLgroupreplace)r   r9   r   Zorig_prefix
new_prefixr	   r	   r
   normalize_string_prefix   s   
rD   c                 C   s  |  t}|dd dkr| S |dd dkrd}d}n|d dkr'd}d}nd}d}| |}|dkr6| S | d| }td	| }td
| }td
| }| |t| t|  }	d| v rq||	rn| S |	}
n+t|d| |	}
|	|
kr|
}	| | |	 | } t|d| |
}
t|d| |
}
d| v rt	d|
tj
}|D ]}dt|v r|   S q|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   r1   r2   r3   z(([^\\]|^)(\\\\)*)z([^\\]|^)\\((?:\\\\)*)rz\1\2z\1\\r;   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   r5   r   compiler&   casefoldsearchr   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`   


	

rR   )F)__doc__r   r   r#   typingr   r   r   rL   r   boolr   r   r.   r0   r/   rD   rR   r	   r	   r	   r
   <module>   s    		#