"""Small generic helpers shared across the codebase."""


def format_size(size_in_bytes: int) -> str:
    """Converts a size in bytes to a human-readable string (KB, MB)."""
    if size_in_bytes < 1024:  # noqa: PLR2004
        return f"{size_in_bytes} octets"
    if size_in_bytes < 1024 * 1024:
        return f"{size_in_bytes / 1024:.2f} Ko"
    return f"{size_in_bytes / (1024 * 1024):.2f} Mo"


def sanitize_kv_value(value: object) -> str:
    """Returns a constant value to be written within double quotes in a .cfg / KeyValues file."""
    text = str(value)
    text = text.replace('"', "")
    text = text.replace("\r", " ").replace("\n", " ")
    return text.strip()
