(s: str)
return s
Escape special characters in `s`
Escape special characters in `s`
Escape special characters in s
def escape_special_characters(s: str) -> str:
    """Escape special characters in `s`"""
    matches = _ESC_REGEX.findall(s)
    if not matches:
        return s
    # Replace all special characters found in `s`. Performance should not be critical
    # so we do one pass per special character.
    all_special = set("".join(matches))
    # '\' is even more special: it needs to be replaced first, otherwise we will
    # mess up the other escaped characters.
    try:
        all_special.remove("\\")
    except KeyError:
        pass  # no '\' in the string
    else:
        s = s.replace("\\", "\\\\")
    for special_char in all_special:
        s = s.replace(special_char, f"\\{special_char}")
    return s
def escape_special_characters ( s : str ) -> str : matches = _ESC_REGEX . findall ( s ) if not matches : return s # Replace all special characters found in `s`. Performance should not be critical # so we do one pass per special character. all_special = set ( "" . join ( matches ) ) # '\' is even more special: it needs to be replaced first, otherwise we will # mess up the other escaped characters. try : all_special . remove ( "\\" ) except KeyError : pass # no '\' in the string else : s = s . replace ( "\\" , "\\\\" ) for special_char in all_special : s = s . replace ( special_char , f"\\{special_char}" ) return s