
    &$              
       
   d Z ddlZddlZddlZddlZddlZej                  Zej                  Z ej                  d      Z	dddddd	d
ddZ
 ed      D ]&  Z ee      Zee
vseej                  vsdez  e
e<   (  ej                  dj!                  e
j#                         D  cg c]  } ej%                  |        c}             Z ed      Z ed      Z G d de      Zd Zd Zd ZdZd ZddZd Zd Z d Z!yc c} w )z*Some common string manipulation utilities.    Nz[^\000-\177]z\bz\tz\nz\fz\rz\"z\'z\\)	
"'\   z\%03o|)truet1yesy)falsef0nonc                       e Zd ZdZy)Base64ValueErrorzIllegal Base64-encoded valueN)__name__
__module____qualname____doc__     ,platform/bq/third_party/pyglib/stringutil.pyr   r   1   s    #Ar   r   c                 0    t         j                  d |       S )a  Replaces each non-ASCII character in s with an escape sequence.

  Non-ASCII characters are substituted with their 6-character unicode
  escape sequence \uxxxx, where xxxx is a hex number.  The resulting
  string consists entirely of ASCII characters.  Existing escape
  sequences are unaffected, i.e., this operation is idempotent.

  Sample usage:
    >>> UnicodeEscape('asdf\xff')
    'asdf\\u00ff'

  This escaping differs from the built-in s.encode('unicode_escape').  The
  built-in escape function uses hex escape sequences (e.g., '\xe9') and escapes
  some control characters in lower ASCII (e.g., '\x00').

  Args:
    s: string to be escaped

  Returns:
    escaped string
  c                 <    dt        | j                  d            z  S )Nz\u%04xr   )ordgroupms    r   <lambda>zUnicodeEscape.<locals>.<lambda>J   s    IAGGAJ$?r   )_RE_NONASCIIsub)ss    r   UnicodeEscaper*   4   s    , 
		?	CCr   c                 F    t         j                  d |       }t        |      S )a/  Escapes a string so it can be inserted in a Java string or char literal.

  Follows the Java Language Specification for "Escape Sequences for Character
  and String Literals":

  https://docs.oracle.com/javase/tutorial/java/data/characters.html

  Escapes unprintable and non-ASCII characters.  The resulting string consists
  entirely of ASCII characters.

  This operation is NOT idempotent.

  Sample usage:
    >>> JavaEscape('single\'double"\n\x00')
    'single\\\'double\\"\\n\\000'

  Args:
    s: string to be escaped

  Returns:
    escaped string
  c                 2    t         | j                  d         S )Nr   )_JAVA_ESCAPE_MAPr#   r$   s    r   r&   zJavaEscape.<locals>.<lambda>d   s    (8(Dr   )_JAVA_ESCAPE_REr(   r*   )r)   s_escs     r   
JavaEscaper0   M   s$    . 

Da
H%
 
u	r   c                 V    t        j                  |       }|s|j                  d      }|S )a  Python implementation of the Google C library's WebSafeBase64Escape().

  Python implementation of the Google C library's WebSafeBase64Escape() (from
  strings/strutil.h), using Python's base64 API and string replacement.

  Args:
    unescaped: any data (byte) string (example: b"12345~6")
    do_padding: whether to add =-padding (example: false)

  Returns:
    The base64 encoding (with web-safe replacements) of unescaped,
    with =-padding depending on the value of do_padding
    (example: b"MTIzNDV-Ng")
     =)base64urlsafe_b64encoderstrip)	unescaped
do_paddingescapeds      r   WebSafeBase64Escaper9   t   s*     $$Y/'	nnT"G	.r   s   !!!!!!!!!     !!!!!!!!!!!!!!!!!! !!!!!!!!!!!!+!!0123456789!!!=!!!ABCDEFGHIJKLMNOPQRSTUVWXYZ!!!!/!abcdefghijklmnopqrstuvwxyz!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!c                 @   | j                  t              }|j                  d      dk\  rt        d| z        |j	                  d      st        |      dz  }|d|z  z  }	 t        j                  |      S # t        j                  $ r}t        | d|      d}~ww xY w)a  Python implementation of the Google C library's WebSafeBase64Unescape().

  Python implementation of the Google C library's WebSafeBase64Unescape() (from
  strings/strutil.h), using Python's base64 API and string replacement.

  Args:
    escaped: A base64 binary string using the web-safe encoding
        (example: b"MTIzNDV-Ng")

  Returns:
    The corresponding unescaped string (example: b"12345~6")

  Raises:
    Base64ValueError: Invalid character in encoding of string, escaped.
     !r   z(%r: Invalid character in encoded string.r2      z: N)		translate_BASE64_DECODE_TRANSLATIONfindr   endswithlenbinascii
a2b_base64Error)r8   escaped_standardpadding_lenmsgs       r   WebSafeBase64UnescaperH      s      &&'AB4 A%
EO
PP 
	"	"4	(&'!+K{**6/00	 6
w4
556s    A5 5BBBc                      |dk  rt        d      dk  rt        d       fdt        |t                     D        S )an  Break a string into chunks of a given size.

  Args:
    value: The value to split.
    size: The maximum size of a chunk.
    start: The index at which to start (defaults to 0).

  Returns:
    Iterable over string slices of as close to the given size as possible.
    Chunk('hello', 2) => 'he', 'll', 'o'

  Raises:
    ValueError: If start < 0 or if size <= 0.
  r   zinvalid starting positioninvalid chunk sizec              3   .   K   | ]  }||z      y w)Nr   ).0isizevalues     r   	<genexpr>zChunk.<locals>.<genexpr>   s     	D%C%!d(
%Cs   )
ValueErrorrangerA   )rO   rN   starts   `` r   ChunkrT      sC     QY
0
11	QY
)
**	DU5#e*d%C	DDr   c                 @     dk  rt        d       fd} |       S )a  Break a string into chunks of a given size, starting at the rear.

  Like chunk, except the smallest chunk comes at the beginning.

  Args:
    value: The value to split.
    size: The maximum size of a chunk.

  Returns:
    Iterable over string slices of as close to the given size as possible.
    ReverseChunk('hello', 2) => 'h', 'el', 'lo'

  Raises:
    ValueError: If size <= 0.
  r   rJ   c               3      K   d} t              z  r!dt              z    t              z  } t        |       D ]  }|  yw)zActually perform the chunking.r   N)rS   )rA   rT   )rS   chunkrN   rO   s     r   DoChunkzReverseChunk.<locals>.DoChunk   sW     E 5zD$3u:$%%%j4eud%0k 1s   A
A)rQ   )rO   rN   rX   s   `` r   ReverseChunkrY      s'    $ 
QY
)
**	 
r   c                     | yt        | t              st        dt        |       z        | r$| j	                         j                         t        v S y)a  Checks if the string is a commonly accepted True value.

  Useful if you want most strings to default to False except a few
  accepted values.  This method is case-insensitive.

  Args:
    value: The string to check for true.  Or None.

  Returns:
    True if the string is one of the commonly accepted true values.
    False if value is None.  False otherwise.

  Raises:
    ValueError: when value is something besides a string or None.
  Fz5IsCommonTrue() called with %s type.  Expected string.)
isinstancestrrQ   typestriplower_COMMON_TRUE_STRINGSrO   s    r   IsCommonTruerb      sS      ]	E3	
LE{# $ $
;;= $888	r   c                     | yt        | t              st        dt        |       z        | r$| j	                         j                         t        v S y)a  Checks if the string is a commonly accepted False value.

  Useful if you want most strings to default to True except a few
  accepted values.  This method is case-insensitive.

  Args:
    value: The string to check for true.  Or None.

  Returns:
    True if the string is one of the commonly accepted false values.
    True if value is None.  False otherwise.

  Raises:
    ValueError: when value is something besides a string or None.
  Tz6IsCommonFalse() called with %s type.  Expected string.)r[   r\   rQ   r]   r^   r_   _COMMON_FALSE_STRINGSra   s    r   IsCommonFalsere     sS      ]	E3	
ME{# $ $
;;= $999	r   )r   )"r   r3   rB   restringsix
ensure_strensure_binarycompiler'   r-   rR   rM   chrc	printablejoinkeysescaper.   	frozensetr`   rd   	Exceptionr   r*   r0   r9   r>   rH   rT   rY   rb   re   )rm   s   0r   <module>rt      sC   1   	  
 ^^
!!rzz/*
 



		
	   
sA	!f!1F,<,<#<"Q,Q 

 "**SXX+00232aRYYq\235 6 !!?@ !"@A  By AD2N:( 6BE,D4G 4s   &D 