def fix_newlines(inval, outval):
    """
    Fixes submitted translations by filtering carriage returns and pairing
    newlines at the begging and end of the translated string with the original
    """
    if 0 == len(inval) or 0 == len(outval):
        return outval
    if '\r' in outval and '\r' not in inval:
        outval = outval.replace('\r', '')
    if '\n' == inval[0] and '\n' != outval[0]:
        outval = '\n' + outval
    elif '\n' != inval[0] and '\n' == outval[0]:
        outval = outval.lstrip()
    if '\n' == inval[-1] and '\n' != outval[-1]:
        outval = outval + '\n'
    elif '\n' != inval[-1] and '\n' == outval[-1]:
        outval = outval.rstrip()
    return outval
