From f520217b4d71372040dcb2ea2fa9af7247bd14fc Mon Sep 17 00:00:00 2001 From: Riccardo Mazzarini Date: Fri, 1 Dec 2023 20:40:47 +0100 Subject: [PATCH] truncate CR instead of `"r"` --- CHANGELOG.md | 5 +++++ src/rope/gap_slice.rs | 27 ++++++++++++++++++++++----- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ddc47f..84e80e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ ## [Unreleased] +### Bug fixes + +- fixed a typo that caused `"r"` to be stripped instead of the carriage return + character (`"\r"`) when truncating trailing line breaks; + ## [0.4.0] - Oct 11 2023 ### Additions diff --git a/src/rope/gap_slice.rs b/src/rope/gap_slice.rs index 87a083d..a60803b 100644 --- a/src/rope/gap_slice.rs +++ b/src/rope/gap_slice.rs @@ -135,9 +135,8 @@ impl<'a> GapSlice<'a> { } } - /// Removes the trailing line break (if it has one), returning the number - /// of bytes that were removed: 0 if there was no trailing line break, 1 - /// if it was a LF, or 2 if it was a CRLF. + /// Removes the trailing line break (if it has one), returning the new + /// summary. #[inline] pub(super) fn truncate_trailing_line_break( &mut self, @@ -151,7 +150,7 @@ impl<'a> GapSlice<'a> { let mut new_summary = self.truncate_last_char(summary); - if self.last_chunk().ends_with('r') { + if self.last_chunk().ends_with('\r') { new_summary = self.truncate_last_char(new_summary) } @@ -365,11 +364,29 @@ impl Summarize for GapSlice<'_> { #[cfg(test)] mod tests { use crate::rope::gap_buffer::GapBuffer; - use crate::tree::AsSlice; + use crate::tree::{AsSlice, Summarize}; #[test] fn debug_slice() { let buffer = GapBuffer::<10>::from("Hello"); assert_eq!("\"He~~~~~llo\"", format!("{:?}", buffer.as_slice())); } + + #[test] + fn truncate_trailing_crlf() { + let buffer = GapBuffer::<5>::from("bar\r\n"); + let mut slice = buffer.as_slice(); + let summary = slice.summarize(); + slice.truncate_trailing_line_break(summary); + assert_eq!("bar", slice); + } + + #[test] + fn truncate_trailing_lf() { + let buffer = GapBuffer::<5>::from("bar\n"); + let mut slice = buffer.as_slice(); + let summary = slice.summarize(); + slice.truncate_trailing_line_break(summary); + assert_eq!("bar", slice); + } }