o
    .j&                  	   @   s  d Z ddlZddlZddlmZmZ deeB defddZdeeB defdd	Z	deeB deeB dB fd
dZ
deeB deeB dB fddZd#dedededefddZdedefddZdedefddZdedefddZdedefddZedZdeddfd d!Zg d"ZdS )$z-Functions for manipulating strings and bytes.    N)DEFAULT_ENCODING
to_unicodetextreturnc                 C   sN   t | ttfs	J t| } | 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bytesr   replacer    r   L/home/thesage/.local/lib/python3.10/site-packages/icalendar/parser/string.py_escape_char	   s   
r   c                 C      t jdtd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   r   r   r   escape_char.   s   r   c                 C   s   t | ttfs	J t | tr(| ddddddddddd	d
S t | trG| 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   \\   \N)r   r   r   r   r   r   r   r   _unescape_charT   s$   



r$   c                 C   r   )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   r   r   r   unescape_char   s   r%   K   
 linelimitfold_sepc              	      s   t tsJ dvsJ zd W n ttfy   Y nw | fddtdt d D S g }d}D ]}t|t}||7 }| krS|	| |}|	| q;d|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   s$    | ]}||  d   V  qdS )   Nr   ).0ir)   r(   r   r   	<genexpr>   s    
zfoldline.<locals>.<genexpr>r   r,    )
r   r   encodeUnicodeEncodeErrorUnicodeDecodeErrorjoinrangelenr   append)r(   r)   r*   	ret_chars
byte_countcharchar_byte_lenr   r/   r   foldline   s(   	

r=   valc                 C   $   |  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   r>   r   r   r   _escape_string   s
   
rF   c                 C   r   )zEscape backslash sequences to URL-encoded hex values.

    .. deprecated:: 7.0.0
        Use the private :func:`_escape_string` internally. For external use,
        this function is deprecated.
    zescape_string is deprecated and will be removed in icalendar 8. If you are using this function externally, please contact the maintainers.r   r   )r   r   r   rF   rE   r   r   r   escape_string      rG   c                 C   r?   )	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`` -> ``\``
    r@   r   rA   :rB   r
   rC   r   rD   rE   r   r   r   _unescape_string   s
   
rJ   c                 C   r   )zUnescape URL-encoded hex values to their original characters.

    .. deprecated:: 7.0.0
        Use the private :func:`_unescape_string` internally. For external use,
        this function is deprecated.
    zunescape_string is deprecated and will be removed in icalendar 8. If you are using this function externally, please contact the maintainers.r   r   )r   r   r   rJ   rE   r   r   r   unescape_string  rH   rK   z[\w.-]+namec                 C   s.   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.
    r,   r   N)NAMEfindallr7   
ValueError)rL   matchr   r   r   validate_token'  s   
rQ   )
r   rF   r$   rJ   r   rG   r=   r%   rK   rQ   )r&   r'   )__doc__rer   icalendar.parser_toolsr   r   r   r   r   r   r$   r%   intr=   rF   rG   rJ   rK   compilerM   rQ   __all__r   r   r   r   <module>   s     %&-$#
