Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid computing line offsets after the last token #1023

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pest/src/iterators/flat_pairs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ pub struct FlatPairs<'i, R> {
pub fn new<'i, R: RuleType>(
queue: Rc<Vec<QueueableToken<'i, R>>>,
input: &'i str,
line_index: Rc<LineIndex>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

even though this method is pub, it's not exposed, so it should be ok semver-wise

start: usize,
end: usize,
) -> FlatPairs<'i, R> {
FlatPairs {
queue,
input,
line_index: Rc::new(LineIndex::new(input)),
line_index,
start,
end,
}
Expand Down
20 changes: 18 additions & 2 deletions pest/src/iterators/pairs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Loading