def chomp(text):
    """ Removes the trailing newline character, if there is one. """
    if not text:
        return text
    if text[-2:] == '\n\r':
        return text[:-2]
    if text[-1] == '\n':
        return text[:-1]
    return text