
    i!              	          d Z ddlZddlZddlmZ deez  deez  fdZdeez  deez  fdZdeez  deez  dz  fdZ	deez  deez  dz  fd	Z
ddedededefdZdedefdZdedefdZ ej        d          ZdeddfdZg dZdS )z-Functions for manipulating strings and bytes.    N)DEFAULT_ENCODINGtextreturnc                 2   t          | t          t          f          sJ |                     dd                              dd                              dd                              dd                              d	d
                              dd
          S )a  Format value according to iCalendar TEXT escaping rules.

    Escapes special characters in text values according to :rfc:`5545#section-3.3.11`
    rules.
    The order of replacements matters to avoid double-escaping.

    Parameters:
        text: The text to escape.

    Returns:
        The escaped text with special characters escaped.

    Note:
        The replacement order is critical:

        1. ``\N`` -> ``\n`` (normalize newlines to lowercase)
        2. ``\`` -> ``\\`` (escape backslashes)
        3. ``;`` -> ``\;`` (escape semicolons)
        4. ``,`` -> ``\,`` (escape commas)
        5. ``\r\n`` -> ``\n`` (normalize line endings)
        6. ``"\n"`` -> ``r"\n"`` (transform a newline character to a literal, or raw,
           newline character)
    \N
\\\;\;,\,
\n
isinstancestrbytesreplacer   s    R/root/projects/butler/venv/lib/python3.11/site-packages/icalendar/parser/string.py_escape_charr   	   s    0 dS%L))))) 	UD!!	v			e			e						u		    c                 X    t          j        dt          d           t          |           S )a  Format value according to iCalendar TEXT escaping rules.

    .. deprecated:: 7.0.0
        Use the private :func:`_escape_char` internally. For external use,
        this function is deprecated. Please use alternative escaping methods
        or contact the maintainers.

    Escapes special characters in text values according to :rfc:`5545#section-3.3.11`
    rules.
    The order of replacements matters to avoid double-escaping.

    Parameters:
        text: The text to escape.

    Returns:
        The escaped text with special characters escaped.

    Note:
        The replacement order is critical:

        1. ``\N`` -> ``\n`` (normalize newlines to lowercase)
        2. ``\`` -> ``\\`` (escape backslashes)
        3. ``;`` -> ``\;`` (escape semicolons)
        4. ``,`` -> ``\,`` (escape commas)
        5. ``\r\n`` -> ``\n`` (normalize line endings)
        6. ``"\n"`` -> ``r"\n"`` (transform a newline character to a literal, or raw,
           newline character)
    zescape_char is deprecated and will be removed in a future version. If you are using this function externally, please contact the maintainers.   
stacklevel)warningswarnDeprecationWarningr   r   s    r   escape_charr!   -   s9    : M	U	    r   c                 ~   t          | t          t          f          sJ t          | t                    rz|                     dd                              dd                              dd                              dd                              dd                              d	d
          S t          | t                    rz|                     dd                              dd                              dd                              dd                              dd                              dd          S dS )a  Unescape iCalendar TEXT values.

    Reverses the escaping applied by :func:`_escape_char` according to
    :rfc:`5545#section-3.3.11` TEXT escaping rules.

    Parameters:
        text: The escaped text.

    Returns:
        The unescaped text, or ``None`` if ``text`` is neither ``str`` nor ``bytes``.

    Note:
        The replacement order is critical to avoid double-unescaping:

        1. ``\N`` -> ``\n`` (intermediate step)
        2. ``\r\n`` -> ``\n`` (normalize line endings)
        3. ``\n`` -> newline (unescape newlines)
        4. ``\,`` -> ``,`` (unescape commas)
        5. ``\;`` -> ``;`` (unescape semicolons)
        6. ``\\`` -> ``\`` (unescape backslashes last)
    r   r   r   r   r   r   r   r   r
   r	   s   \Ns   \ns   
   
s   \,   ,s   \;   ;s   \\   \Nr   r   s    r   _unescape_charr'   S   s   , dS%L)))))$ 
LL&&WVT""WUD!!WUC  WUC  WVT""	
 $ 
LL((WWe$$WVU##WVT""WVT""WWe$$	
 4r   c                 X    t          j        dt          d           t          |           S )a  Unescape iCalendar TEXT values.

    .. deprecated:: 7.0.0
        Use the private :func:`_unescape_char` internally. For external use,
        this function is deprecated. Please use alternative unescaping methods
        or contact the maintainers.

    Reverses the escaping applied by :func:`escape_char` according to
    :rfc:`5545#section-3.3.11` TEXT escaping rules.

    Parameters:
        text: The escaped text.

    Returns:
        The unescaped text, or ``None`` if ``text`` is neither ``str`` nor ``bytes``.

    Note:
        The replacement order is critical to avoid double-unescaping:

        1. ``\N`` -> ``\n`` (intermediate step)
        2. ``\r\n`` -> ``\n`` (normalize line endings)
        3. ``\n`` -> newline (unescape newlines)
        4. ``\,`` -> ``,`` (unescape commas)
        5. ``\;`` -> ``;`` (unescape semicolons)
        6. ``\\`` -> ``\`` (unescape backslashes last)
    zunescape_char is deprecated and will be removed in a future version. If you are using this function externally, please contact the maintainers.r   r   )r   r   r    r'   r   s    r   unescape_charr)      s9    6 M	U	    $r   K   
 linelimitfold_sepc           	          t           t                    sJ d vsJ 	                      d           |                     fdt	          dt                     dz
            D                       S # t          t          f$ r Y nw xY wg }d} D ]`}t          |                    t                              }||z  }|k    r|	                    |           |}|	                    |           ad                    |          S )a  Make a string folded as defined in RFC5545
    Lines of text SHOULD NOT be longer than 75 octets, excluding the line
    break.  Long content lines SHOULD be split into a multiple line
    representations using a line "folding" technique.  That is, a long
    line can be split between any two characters by inserting a CRLF
    immediately followed by a single linear white-space character (i.e.,
    SPACE or HTAB).
    r   asciic              3   8   K   | ]}||z   d z
           V  dS )   N ).0ir-   r,   s     r   	<genexpr>zfoldline.<locals>.<genexpr>   sF       
 
()DQY]"#
 
 
 
 
 
r   r   r2    )
r   r   encodejoinrangelenUnicodeEncodeErrorUnicodeDecodeErrorr   append)r,   r-   r.   	ret_chars
byte_countcharchar_byte_lens   ``     r   foldlinerC      sQ    dC     t
G }} 
 
 
 
 
-21c$ii-K-K
 
 
 
 
 	
  23    IJ  DKK(899::m#
X&&&&J779s   A7 7B
Bvalc                     |                      dd                               dd                               dd                               dd          S )	a  Escape backslash sequences to URL-encoded hex values.

    Converts backslash-escaped characters to their percent-encoded hex
    equivalents. This is used for parameter parsing to preserve escaped
    characters during processing.

    Parameters:
        val: The string with backslash escapes.

    Returns:
        The string with backslash escapes converted to percent encoding.

    Note:
        Conversions:

        - ``\,`` -> ``%2C``
        - ``\:`` -> ``%3A``
        - ``\;`` -> ``%3B``
        - ``\\`` -> ``%5C``
    r   %2Cz\:%3Ar   %3Br
   %5Cr   rD   s    r   escape_stringrL      sG    . 	E5!!										r   c                     |                      dd                               dd                               dd                               dd          S )	a  Unescape URL-encoded hex values to their original characters.

    Reverses :func:`escape_string` by converting percent-encoded hex values
    back to their original characters. This is used for parameter parsing.

    Parameters:
        val: The string with percent-encoded values.

    Returns:
        The string with percent encoding converted to characters.

    Note:
        Conversions:

        - ``%2C`` -> ``,``
        - ``%3A`` -> ``:``
        - ``%3B`` -> ``;``
        - ``%5C`` -> ``\``
    rF   r   rG   :rH   r   rI   r	   rJ   rK   s    r   unescape_stringrO      sG    * 	E3										r   z[\w.-]+namec                     t                               |           }t          |          dk    r| |d         k    rdS t          |           )a   Validate that a name is a valid iCalendar token.

    Checks if the name matches the :rfc:`5545` token syntax using the NAME
    regex pattern (``[\w.-]+``).

    Parameters:
        name: The token name to validate.

    Raises:
        ValueError: If the name is not a valid token.
    r2   r   N)NAMEfindallr;   
ValueError)rP   matchs     r   validate_tokenrV     sE     LLE
5zzQ458++
T

r   )r   r'   r!   rL   rC   r)   rO   rV   )r*   r+   )__doc__rer   icalendar.parser_toolsr   r   r   r   r!   r'   r)   intrC   rL   rO   compilerR   rV   __all__r3   r   r   <module>r]      s   3 3 				  3 3 3 3 3 3!sU{ !sU{ ! ! ! !H#cEk #cEk # # # #L*u *ut); * * * *Z! e ! ed(: !  !  !  ! H   3  s  3  S        Fs s    <     < rz*     $	 	 	r   