Skip to content

Commit 4fa58ce

Browse files
Consider blank lines at the end of a file
1 parent 4d7f6b2 commit 4fa58ce

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/tools.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,13 +229,17 @@ pub fn guess_language<P: AsRef<Path>>(buf: &[u8], path: P) -> (Option<LANG>, Str
229229
}
230230
}
231231

232+
/// Replaces \n and \r ending characters with a single generic \n
232233
pub(crate) fn remove_blank_lines(data: &mut Vec<u8>) {
233-
let count_trailing = data.iter().rev().take_while(|&c| *c == b'\n').count();
234+
let count_trailing = data
235+
.iter()
236+
.rev()
237+
.take_while(|&c| (*c == b'\n' || *c == b'\r'))
238+
.count();
234239
if count_trailing > 0 {
235-
data.truncate(data.len() - count_trailing + 1);
236-
} else {
237-
data.push(b'\n');
240+
data.truncate(data.len() - count_trailing);
238241
}
242+
data.push(b'\n');
239243
}
240244

241245
pub(crate) fn normalize_path<P: AsRef<Path>>(path: P) -> PathBuf {

0 commit comments

Comments
 (0)