From 459897940657569679a37d4bd08f1d71ebd0f69a Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 26 Jul 2020 03:53:52 +0200 Subject: Suggestion.apply_to(): Move CRLF check to a new function --- src/lib.rs | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 3074dda..0624722 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -168,15 +168,9 @@ impl Suggestion { match line { Ok(l) => { // Determine which line endings the file uses by looking at - // the first line. If the second-to-last character on the - // first line is "\r", assume CRLF. Otherwise, default to - // LF. - if line_number == 1 { - if let Some(c) = l.chars().rev().nth(2) { - if c == '\r' { - line_ending = LineEnding::CrLf; - } - } + // the first line. + if line_number == 1 && is_line_crlf(&l) { + line_ending = LineEnding::CrLf; } if line_number == self.original_end_line { @@ -199,6 +193,20 @@ impl Suggestion { } } +/// Determine the line ending for `line`. +/// +/// If the second-to-last character on the first line is "\r", assume CRLF. +/// Otherwise, default to LF. +fn is_line_crlf(line: &str) -> bool { + if let Some(c) = line.chars().rev().nth(2) { + if c == '\r' { + return true; + } + } + + false +} + #[cfg(test)] mod tests { use super::*; -- cgit v1.2.3