U
    {Zg%                     @   s   d Z ddlZdd Zdd Ze Zdd edd	D Zee ed
d edd	D  dd Ze	dddZ
edZe	edddZdS )zEncoding related utilities.    Nc                 C   s   | dko| dk S )N        )ir   r   A/tmp/pip-unpacked-wheel-fw8d5lna/google/protobuf/text_encoding.py_AsciiIsPrint   s    r   c                  C   st   i } t ddD ]}t|sd| | |< qd| td< d| td< d| td	< d
| td< d| td< d| td< | S )Nr      \%03oz\t	z\n
z\rz\""z\''z\\\)ranger   ord)retr   r   r   r   _MakeStrEscapes   s    r   c                 C   s   i | ]}|t |qS r   )chr.0r   r   r   r   
<dictcomp>   s      r      c                 C   s   i | ]}|d | qS )r	   r   r   r   r   r   r   !   s      r   c              
   C   s   d}| rz||  dt7 }d} W q tk
r } zF|| d |j  dt7 }|t| |j  7 }| |jd d  } W 5 d }~X Y qX q|S )N utf-8   )decode	translate_str_escapesUnicodeDecodeErrorstart_byte_escapes)Z
text_bytesr   er   r   r   _DecodeUtf8EscapeErrors$   s    &r#   )returnc                 C   sL   t | t}|r&|r| tS t| S n"|r4| d} ddd | D S dS )am  Escape a bytes string for use in an text protocol buffer.

  Args:
    text: A byte string to be escaped.
    as_utf8: Specifies if result may contain non-ASCII characters.
        In Python 3 this allows unescaped non-ASCII Unicode characters.
        In Python 2 the return value will be valid UTF-8 rather than only ASCII.
  Returns:
    Escaped string (str).
  r   r   c                 S   s   g | ]}t | qS r   )r!   )r   cr   r   r   
<listcomp>J   s     zCEscape.<locals>.<listcomp>N)
isinstancestrr   r   r#   encodejoin)textZas_utf8Ztext_is_unicoder   r   r   CEscape1   s    



r,   z"(\\+)x([0-9a-fA-F])(?![0-9a-fA-F]))r+   r$   c                 C   s>   dd }t || }|dd}|dd}|dS )zUnescape a text string with C-style escape sequences to UTF-8 bytes.

  Args:
    text: The data to parse in a str.
  Returns:
    A byte string.
  c                 S   s4   t | dd@ r*| dd | d S | dS )Nr   Zx0   r   )lengroup)mr   r   r   
ReplaceHexY   s    zCUnescape.<locals>.ReplaceHexZraw_unicode_escaper   Zunicode_escapezlatin-1)_CUNESCAPE_HEXsubr)   r   )r+   r1   resultr   r   r   	CUnescapeP   s
    		r5   )__doc__rer   r   r   r   r!   updater#   r(   r,   compiler2   bytesr5   r   r   r   r   <module>   s   

