From edff1e089d61027d6deb3075822343cddb13cc09 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Tue, 22 Aug 2023 22:18:44 -0400 Subject: [PATCH] Implement text since line break. --- src/main.rs | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 4ed11e4..7846d6b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,6 +31,12 @@ impl<'s> WrappedInput<'s> { preceding_line_break: None, } } + + /// Get the text since the line break preceding the start of this WrappedInput. + pub fn text_since_line_break(&self) -> &'s str { + let start = self.preceding_line_break.unwrap_or(0); + &self.full_source[start..self.start] + } } impl<'s> InputTake for WrappedInput<'s> { @@ -87,12 +93,17 @@ where panic!("Attempted to extend past the end of the WrappedInput.") } + let skipped_text = &self.full_source[self.start..new_start]; + let last_line_feed = skipped_text + .rfind('\n') + .map(|idx| idx + self.preceding_line_break.unwrap_or(0) + 1); + // TODO: calculate updated values for WrappedInput WrappedInput { full_source: self.full_source, start: new_start, end: new_end, - preceding_line_break: None, + preceding_line_break: last_line_feed, } } } @@ -158,4 +169,21 @@ mod tests { let first_cut = input.slice(6..17); first_cut.slice(4..14); } + + #[test] + fn line_break() { + let input = WrappedInput::new("lorem\nfoo\nbar\nbaz\nipsum"); + assert_eq!(input.slice(5..).preceding_line_break, None); + assert_eq!(input.slice(6..).preceding_line_break, Some(6)); + assert_eq!(input.slice(6..).slice(10..).preceding_line_break, Some(14)); + } + + #[test] + fn text_since_line_break() { + let input = WrappedInput::new("lorem\nfoo\nbar\nbaz\nipsum"); + assert_eq!(input.text_since_line_break(), ""); + assert_eq!(input.slice(5..).text_since_line_break(), "lorem"); + assert_eq!(input.slice(6..).text_since_line_break(), ""); + assert_eq!(input.slice(6..).slice(10..).text_since_line_break(), "ba"); + } }