o
    5ti)                     @   s`   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e
defd	d
ZG dd dZdS )zA
.. codeauthor:: Tsuyoshi Hombashi <tsuyoshi.hombashi@gmail.com>
    N)Sequence)FinalOptional   )to_codec_namesreturnc                 C   s
   |  dS )Nzlatin-1)encode)r    r
   N/home/ubuntu/.local/lib/python3.10/site-packages/mbstrdecoder/_mbstrdecoder.pyb   s   
r   c                   @   s   e Zd ZU dZg dZeed< ee	dZ
edefddZedee fdd	Zd deee  dd
fddZdefddZd!ddZdefddZdefddZdefddZedee fddZdee fddZdd ZdefddZd
S )"MultiByteStrDecoderzJ
    Reference:
        https://docs.python.org/3/library/codecs.html
    )kutf_7utf_8	utf_8_sigutf_16	utf_16_be	utf_16_leutf_32	utf_32_be	utf_32_lebig5	big5hkscscp037cp424cp437cp500cp720cp737cp775cp850cp852cp855cp856cp857cp858cp860cp861cp862cp863cp864cp865cp866cp869cp874cp875cp932cp949cp950cp1006cp1026cp1140cp1250cp1251cp1252cp1253cp1254cp1255cp1256cp1257cp1258euc_jpeuc_jis_2004euc_jisx0213euc_krgb2312gbkgb18030hz
iso2022_jpiso2022_jp_1iso2022_jp_2iso2022_jp_2004iso2022_jp_3iso2022_jp_ext
iso2022_krlatin_1	iso8859_2	iso8859_3	iso8859_4	iso8859_5	iso8859_6	iso8859_7	iso8859_8	iso8859_9
iso8859_10
iso8859_11
iso8859_13
iso8859_14
iso8859_15
iso8859_16johabkoi8_rkoi8_umac_cyrillic	mac_greekmac_iceland
mac_latin2	mac_romanmac_turkishptcp154	shift_jisshift_jis_2004shift_jisx0213base64_codec	bz2_codec	hex_codecidnambcspalmospunycodequopri_codecraw_unicode_escaperot_13string_escapeunicode_escapeunicode_internaluu_codec
zlib_codec_MultiByteStrDecoder__CODECSz	[+].*?[-]r   c                 C      | j S N)!_MultiByteStrDecoder__unicode_strselfr
   r
   r   unicode_str      zMultiByteStrDecoder.unicode_strc                 C   rz   r{   )_MultiByteStrDecoder__codecr}   r
   r
   r   codec   r   zMultiByteStrDecoder.codecNcodec_candidatesc                 C   s<   || _ d | _|d u rg | _nt|| _|   |  | _d S r{   )!_MultiByteStrDecoder__encoded_strr   *_MultiByteStrDecoder__codec_candidate_listlist"_MultiByteStrDecoder__validate_str _MultiByteStrDecoder__to_unicoder|   )r~   valuer   r
   r
   r   __init__   s   
zMultiByteStrDecoder.__init__c                 C   s   d| j dd| jdS )Nzcodec=r   z
, unicode=)r   r   r}   r
   r
   r   __repr__   s   zMultiByteStrDecoder.__repr__c                 C   s(   t | jttfr
d S tdt| j )Nzvalue must be a string: actual=)
isinstancer   strbytes
ValueErrortyper}   r
   r
   r   __validate_str   s   z"MultiByteStrDecoder.__validate_strc                 C   s   t | jtS r{   )r   r   
memoryviewr}   r
   r
   r   __is_buffer   s   zMultiByteStrDecoder.__is_bufferc                 C   sR   | j dkrdS |td}|dkrdS ||tdkrdS |t| j|kS )Nr   F+r   -)r   countr   len_MultiByteStrDecoder__RE_UTF7findall)r~   encoded_strutf7_symbol_countr
   r
   r   __is_multibyte_utf7   s   
z'MultiByteStrDecoder.__is_multibyte_utf7c                 C   s   |   r	t| jS | jS r{   )_MultiByteStrDecoder__is_bufferr   r   r}   r
   r
   r   __get_encoded_str   s   
z%MultiByteStrDecoder.__get_encoded_strc                 C   s^   dd l }z|| }W n ty   i }Y nw |d}|d}|dvr-|r-|dkr-|S d S )Nr   encoding
confidence)asciizutf-8gffffff?)chardetdetect	TypeErrorget)r   r   r   detect_encodingr   r
   r
   r   __detect_encoding_helper   s   

z,MultiByteStrDecoder.__detect_encoding_helperc              	   C   s~   t | j}| |}|r$z|| W n	 ty   Y nw |d| | jD ]}z|| W q' ty9   Y q'w | j| S )Nr   )copydeepcopyry   ,_MultiByteStrDecoder__detect_encoding_helperremover   insertr   )r~   r   codec_candidate_listr   codec_candidater
   r
   r   __get_codec_candidate_list   s    


z.MultiByteStrDecoder.__get_codec_candidate_listc                 C   s,  |   }|dkrd| _dS | |D ]Q}|sqzt|| _||}W  nd ty0   d | _Y q tyc   t|trLd| _|sF| Y   S | Y   S d | _z| W  Y   S  tyb   t	dw w d | _zd| }W n ty~   dt
| }Y nw tdddt||| jd	kr| ||S |S )
N    unicode zargument must be a stringzunknown codec: encoded_str=zunknown codec: value-type=unknownr   r   )%_MultiByteStrDecoder__get_encoded_strr   ._MultiByteStrDecoder__get_codec_candidate_listr   decodeUnicodeDecodeErrorAttributeErrorr   r   r   r   r   r   "_MultiByteStrDecoder__process_utf7)r~   r   r   decoded_strmessager
   r
   r   __to_unicode   sJ   



z MultiByteStrDecoder.__to_unicodec                 C   s\   |sd| _ |S | |r&z|d d| _ |dW S  ty%   | Y S w d| _ |dS )Nr   r   )r   '_MultiByteStrDecoder__is_multibyte_utf7r	   r   UnicodeEncodeError)r~   r   r   r
   r
   r   __process_utf7  s   


z"MultiByteStrDecoder.__process_utf7r{   )r   N)__name__
__module____qualname____doc__ry   r   __annotations__recompiler   r   propertyr   r   r   r   r   r   r   r   boolr   r   r   staticmethodr   r   r   r   r   r
   r
   r
   r   r      s&   
 m
3r   )r   r   r   collections.abcr   typingr   r   _funcr   r   r   r   r   r
   r
   r
   r   <module>   s    