summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWilfred Hughes <me@wilfred.me.uk>2022-01-22 13:40:12 -0800
committerWilfred Hughes <me@wilfred.me.uk>2022-01-22 13:40:12 -0800
commite2bf7741a0f74338283ffa1e490ed488f861bf75 (patch)
tree6e53dbb15100fbc213de0530a226d08fe189a430
parent504221b908532bd0912750842a7385d13ac8c885 (diff)
Fix .max_line() when source has a trailing newline
Fixes a crash when testing #90 against trunk.
-rw-r--r--src/lines.rs8
1 files changed, 7 insertions, 1 deletions
diff --git a/src/lines.rs b/src/lines.rs
index b64d243ce1..63b755b6ab 100644
--- a/src/lines.rs
+++ b/src/lines.rs
@@ -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()
}
}
@@ -239,6 +239,12 @@ mod tests {
}
#[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();