"""Expression-string sanitization.

Strips trailing whitespace/escape sequences and removes empty parameters
that would otherwise crash ``ast.parse``.
"""


def sanitize_formula(expr: str) -> str:
    """Clean up a rendered expression string before evaluation."""
    expr = expr.strip()
    expr = expr.replace("\\n", "").replace("\\r", "").replace("\\t", "")
    expr = expr.strip()
    expr = expr.replace("(,", "(")
    while ",," in expr:
        expr = expr.replace(",,", ",")
    expr = expr.replace(",)", ")")
    return expr
