o
    ^i3                  
   @   s  U d Z ddlZddlZddlmZ ddlmZmZ ddlmZ 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ejZeed< 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edefd#d$Zed%d&d'edee fd(d)Zdedefd*d+Zd,ed-ee d.ede ee ef fd/d0Z!d1eddfd2d3Z"ed4d&d5ede#fd6d7Z$d8ede#fd9d:Z%d8ed;e#de#fd<d=Z&dS )>zN
Simple formatting on strings. Further string formatting code is in trans.py.
    N)	lru_cache)MatchPattern)Final)WIDTH_TABLE)Leaf
fturbFTURBSTRING_PREFIX_CHARSz^([z]*)(.*)$STRING_PREFIX_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   R/var/www/html/stock_analysis/be/venv/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   >   """''')lstripr	   )r   
raw_stringr   r   r   has_triple_quotes&   s   
r   sc                 C   sx   g }|   D ])}| }|r||kr|| qt|t| }|d|  }|||  q| dr:|d |S )zh
    Splits string into lines and expands only leading tabs (following the normal
    Python rules)
    N
 )
splitlinesr   appendlen
expandtabsendswith)r   lineslinestripped_lineprefix_lengthprefixr   r   r    lines_with_leading_tabs_expanded/   s   

r(   	docstringr'   c           
      C   s   | sJ dt | }tj}|dd  D ]}| }|r&t|t|t| }q|d  g}|tjk rat|d }t|dd  D ]\}}||d   }	|	sS||kr[|	||	  qA|	d qAd
|S )Nz4INTERNAL ERROR: Multiline docstrings cannot be empty   r      r   r   )r(   sysmaxsizer   minr    strip	enumeraterstripr   join)
r)   r'   r#   indentr$   strippedtrimmedlast_line_idxir%   r   r   r   fix_multiline_docstringB   s"   

r8   c                 C   s4   t |  g }| D ]}|tv r|| q d|S )z
    Pre-conditions:
        * assert_is_leaf_string(@string)

    Returns:
        @string's prefix (e.g. '', 'r', 'f', or 'rf').
    r   )assert_is_leaf_stringr	   r   r2   )r   r'   charr   r   r   get_string_prefixY   s   
r;   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 ").)r=   r<   z/ is missing an ending quote character (' or ").Nz is NOT a subset of .)findmaxr.   r    setissubsetr	   )r   
dquote_idx
squote_idx	quote_idxr   r   r   r9   l   s&   




r9   c                 C   s   t | }|dusJ d| |d}|dddddd	d
d	}t|dkr<|d  dkr<|ddd }| |d S )z#Make all string prefixes lowercase.Nzfailed to match string r*   FfBbUr   ur+   r   rr>   )r
   matchgroupreplacer    lower)r   rN   orig_prefix
new_prefixr   r   r   normalize_string_prefix   s   


rT   @   )r-   patternc                 C   s
   t | S )N)recompile)rV   r   r   r   _cached_compile   s   
rY   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s;J d	| | d| }td
| }td| }td| }| |t| t|  }	d| v rs||	rp| 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 )vPrefer double quotes but only if it doesn't cause more escaping.

    Adds or removes backslashes as appropriate.
    Nr   r   r   r   r<   r=   r>   z!INTERNAL ERROR: Malformed string (([^\\]|^)(\\\\)*)([^\\]|^)\\((?:\\\\)*)rM   \1\2\1\\rH   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-}
            \\")r   r	   r@   rY   r    casefoldsearchr   rW   findallVERBOSEstrcount)r   value
orig_quote	new_quotefirst_quote_posr'   unescaped_new_quoteescaped_new_quoteescaped_orig_quotebodynew_bodymatchesmorig_escape_countnew_escape_countr   r   r   normalize_string_quotes   s^   


	

rt   quotemiddlesis_raw_fstringc                 C   s  | dkr|| fS | dkrd}n	| dkrd}nd}t d| }t d| }t d|  }|rD|D ]}||jr?|| f  S q1|dfS g }|D ],}|j}	t|d| |	}
|	|
kr]|
|_t|d|  |
}
t|d| |
}
||
 qH|dkr|d	 dr|d	 d
d	 d |d	< t||D ]\}}
|jd}|
d}q||kr|| fS ||kr| dkr|| fS t||D ]\}}
|
|_q||fS )rZ   r   r   r<   r=   r[   r\   r]   r^   r>   Nr`   r_   )rY   rb   rg   r   r   r"   ziprf   )ru   rv   rw   ri   rk   rl   rm   middlenew_segmentssegmentnew_segmentrr   rs   r   r   r   normalize_fstring_quotes   sJ   	r}   leafc                 C   sH   | j }t|}d| v rdS dtt dtfdd}tt||| _ dS )zLReplace hex codes in Unicode escape sequences with lowercase representation.rM   Nrq   r   c                 S   s   |   }|d }t|d dkr||d  S |d r$|d |d   S |d r2|d |d   S |d r@|d |d   S |d sKJ d	|  |d
 |d   d S )Nbackslashesr+   r   rn   rL   rK   xNzUnexpected match: zN{})	groupdictr    rQ   upper)rq   groupsback_slashesr   r   r   rP   :  s   z3normalize_unicode_escape_sequences.<locals>.replace)rg   r;   rQ   r   re   rW   r   r   )r~   textr'   rP   r   r   r   "normalize_unicode_escape_sequences3  s   r   i   r:   c           	      C   s   t }t| }t|d }d}|d }	 || \}}}||k r#|d }n||kr,|d }n|dk r2dS |S ||k r;	 dS || d }q)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    )	r:   table	codepointhighestlowestidxstart_codepointend_codepointwidthr   r   r   
char_widthR  s"   

r   line_strc                 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mapr   )r   r   r   r   	str_widthm  s   r   	max_widthc                 C   sB   d}t | D ]\}}t|}|| |kr|  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   r   r    )r   r   total_widthr7   r:   r   r   r   r   count_chars_in_widthz  s   
r   )'__doc__rW   r,   	functoolsr   r   r   typingr   black._width_tabler   blib2to3.pytreer   r	   __annotations__rX   DOTALLr
   rd   r   re   r   boolr   listr(   r8   r;   r9   rT   rY   rt   tupler}   r   intr   r   r   r   r   r   r   <module>   sP    		#I
A