diff options
author | Teddy Wing | 2020-07-26 03:53:52 +0200 |
---|---|---|
committer | Teddy Wing | 2020-07-26 03:53:52 +0200 |
commit | 459897940657569679a37d4bd08f1d71ebd0f69a (patch) | |
tree | 2323896b19183cc4f7a7543df8c78319136bfe2d /src | |
parent | 985b115cc519c67575d98d1b89b9a293ada67fbb (diff) | |
download | git-suggestion-459897940657569679a37d4bd08f1d71ebd0f69a.tar.bz2 |
Suggestion.apply_to(): Move CRLF check to a new function
Diffstat (limited to 'src')
-rw-r--r-- | src/lib.rs | 26 |
1 files changed, 17 insertions, 9 deletions
@@ -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::*; |