Skip to content

Commit

Permalink
Explain next_pos wonkiness
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed Jun 2, 2023
1 parent 6c13445 commit ca962a9
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/reader/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,20 +399,29 @@ impl PullParser {

#[inline]
fn next_pos(&mut self) {
if self.pos.len() > 1 {
self.pos.remove(0);
} else {
self.pos[0] = self.lexer.position();
// unfortunately calls to next_pos will never be perfectly balanced with push_pos,
// at very least because parse errors and EOF can happen unexpectedly without a prior push.
if self.pos.len() > 0 {
if self.pos.len() > 1 {
self.pos.remove(0);
} else {
self.pos[0] = self.lexer.position();
}
}
}

#[inline]
#[track_caller]
fn push_pos(&mut self) {
debug_assert!(self.pos.len() != self.pos.capacity(), "How did you get a document that weird? Please file a bug");
debug_assert!(self.pos.len() != self.pos.capacity(), "You've found a bug in xml-rs, caused by calls to push_pos() in states that don't end up emitting events.
This case is ignored in release mode, and merely causes document positions to be out of sync.
Please file a bug and include the XML document that triggers this assert.");

// it has capacity preallocated for more than it ever needs, so this reduces code size
if self.pos.len() != self.pos.capacity() {
self.pos.push(self.lexer.position());
} else if self.pos.len() > 1 {
self.pos.remove(0); // this mitigates the excessive push_pos() call
}
}

Expand Down

0 comments on commit ca962a9

Please sign in to comment.