Fix side-by-side display when source contains CRLF

Fixes #212
pull/222/head
Wilfred Hughes 2022-03-31 09:32:21 +07:00
parent 8d44b2476a
commit a1ec5e88e8
2 changed files with 17 additions and 2 deletions

@ -15,6 +15,8 @@ specified.
Fixed a crash when a text file ended with a multibyte character.
Fixed side-by-side display when source files contained CRLF.
### Parsing
Fixed an issue in C and C++ where blank lines were highlighted after

@ -17,12 +17,20 @@ use crate::{
const SPACER: &str = " ";
/// Split `s` by newlines. Always returns a non-empty vec.
/// Split `s` on \n or \r\n. Always returns a non-empty vec.
///
/// This differs from `str::lines`, which considers `""` to be zero
/// lines and `"foo\n"` to be one line.
fn split_on_newlines(s: &str) -> Vec<&str> {
s.split('\n').collect()
s.split('\n')
.map(|l| {
if let Some(l) = l.strip_suffix('\r') {
l
} else {
l
}
})
.collect()
}
fn format_line_num_padded(line_num: LineNumber, column_width: usize) -> String {
@ -621,6 +629,11 @@ mod tests {
assert_eq!(split_on_newlines("foo\nbar"), vec!["foo", "bar"]);
}
#[test]
fn test_split_line_with_crlf() {
assert_eq!(split_on_newlines("foo\r\nbar"), vec!["foo", "bar"]);
}
#[test]
fn test_split_line_with_trailing_newline() {
assert_eq!(split_on_newlines("foo\nbar\n"), vec!["foo", "bar", ""]);