def _rstrip_unescaped(rule):
    """Remove trailing whitespace that isn't escaped."""
    i = len(rule) - 1
    last = len(rule)
    while i >= 0:
        if rule[i] == '\n' or rule[i] == '\r':
            last = i
        elif rule[i] != ' ':
            break
        elif i == 0 or rule[i - 1] != '\\':
            last = i
        i -= 1
    rule = rule[:last]
    return rule