def safe_decode(byte_data: bytes, encoding: str | None = None) -> str:
    """Decode a byte string safely, removing invalid sequences.

    Args:
        byte_data: The byte string to decode.
        encoding: The encoding to use when decoding the byte string.

    Returns:
        The decoded string.
    """
    if not byte_data:
        return ""

    encodings = [encoding, detect(byte_data).get("encoding", ""), "utf-8"]

    for enc in [e for e in encodings if e]:  # pragma: no cover
        with suppress(UnicodeDecodeError, LookupError):
            return byte_data.decode(enc)

    return byte_data.decode("latin-1", errors="replace")
Decode a byte string safely, removing invalid sequences.

    Args:
        byte_data: The byte string to decode.
        encoding: The encoding to use when decoding the byte string.

    Returns:
        The decoded string.
    
kreuzberg/_utils/_string.py