Implement text since line break.

This commit is contained in:
Tom Alexander 2023-08-22 22:18:44 -04:00
parent bc29f1dfc0
commit edff1e089d
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE

View File

@ -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");
}
}