ensure that newline is removed only imediately after \r if present