Skip to content

Commit

Permalink
Fix .max_line() when source has a trailing newline
Browse files Browse the repository at this point in the history
Fixes a crash when testing #90 against trunk.
  • Loading branch information
Wilfred committed Jan 22, 2022
1 parent 504221b commit e2bf774
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ pub trait MaxLine {

impl<S: AsRef<str>> MaxLine for S {
fn max_line(&self) -> LineNumber {
(max(1, self.as_ref().lines().count()) - 1).into()
(max(1, self.as_ref().split('\n').count()) - 1).into()
}
}

Expand Down Expand Up @@ -238,6 +238,12 @@ mod tests {
assert_eq!(line.max_line().0, 0);
}

#[test]
fn str_max_line_trailing_newline() {
let line: String = "foo\nbar\n".into();
assert_eq!(line.max_line().0, 2);
}

#[test]
fn from_offsets_relative_to() {
let newline_positions: NewlinePositions = "foo\nbar".into();
Expand Down

0 comments on commit e2bf774

Please sign in to comment.