def detect_nl(string_or_lines, line_end=None):
    """If needed, auto-detect line end using a given string or lines."""
    if line_end is None:
        line_end = '\n' if (string_or_lines and
                            string_or_lines[-1].endswith('\n')) else ''
    return line_end