def strip(text):
    """Remove trailing spaces and linefeeds.
    """
    if not text: return text

    start = 0
    while text[start] in "\n ":
        start += 1
    end = 0
    while text[end-1] in "\n ":
        end -= 1

    if end:
        text = text[start:end]
    else:
        text = text[start:]

    return text