From b858b4a2b9a7f0f1c7bbce70d064c5cc9eb57796 Mon Sep 17 00:00:00 2001 From: William Bain Date: Sun, 30 Jun 2024 20:12:08 -0400 Subject: [PATCH] do not compute line offsets after the last token (#1023) --- pest/src/iterators/flat_pairs.rs | 3 ++- pest/src/iterators/pairs.rs | 20 ++++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/pest/src/iterators/flat_pairs.rs b/pest/src/iterators/flat_pairs.rs index 6c4f16c1..8af70fd2 100644 --- a/pest/src/iterators/flat_pairs.rs +++ b/pest/src/iterators/flat_pairs.rs @@ -32,13 +32,14 @@ pub struct FlatPairs<'i, R> { pub fn new<'i, R: RuleType>( queue: Rc>>, input: &'i str, + line_index: Rc, start: usize, end: usize, ) -> FlatPairs<'i, R> { FlatPairs { queue, input, - line_index: Rc::new(LineIndex::new(input)), + line_index, start, end, } diff --git a/pest/src/iterators/pairs.rs b/pest/src/iterators/pairs.rs index a98ceb71..d8106299 100644 --- a/pest/src/iterators/pairs.rs +++ b/pest/src/iterators/pairs.rs @@ -51,7 +51,17 @@ pub fn new<'i, R: RuleType>( ) -> Pairs<'i, R> { let line_index = match line_index { Some(line_index) => line_index, - None => Rc::new(LineIndex::new(input)), + None => { + let last_input_pos = queue + .last() + .map(|token| match *token { + QueueableToken::Start { input_pos, .. } + | QueueableToken::End { input_pos, .. } => input_pos, + }) + .unwrap_or(0); + + Rc::new(LineIndex::new(&input[..last_input_pos])) + } }; let mut pairs_count = 0; @@ -205,7 +215,13 @@ impl<'i, R: RuleType> Pairs<'i, R> { /// ``` #[inline] pub fn flatten(self) -> FlatPairs<'i, R> { - flat_pairs::new(self.queue, self.input, self.start, self.end) + flat_pairs::new( + self.queue, + self.input, + self.line_index, + self.start, + self.end, + ) } /// Finds the first pair that has its node or branch tagged with the provided