aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2020-07-26 03:53:52 +0200
committerTeddy Wing2020-07-26 03:53:52 +0200
commit459897940657569679a37d4bd08f1d71ebd0f69a (patch)
tree2323896b19183cc4f7a7543df8c78319136bfe2d
parent985b115cc519c67575d98d1b89b9a293ada67fbb (diff)
downloadgit-suggestion-459897940657569679a37d4bd08f1d71ebd0f69a.tar.bz2
Suggestion.apply_to(): Move CRLF check to a new function
-rw-r--r--src/lib.rs26
1 files changed, 17 insertions, 9 deletions
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::*;