Skip to content

Commit

Permalink
Add debug_assert! in a few places to protect invariants
Browse files Browse the repository at this point in the history
  • Loading branch information
dralley committed Jul 24, 2022
1 parent a2298b3 commit 33b6e6d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/reader/buffered_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ impl<'b, R: BufRead> XmlSource<'b, &'b mut Vec<u8>> for R {
buf: &'b mut Vec<u8>,
position: &mut usize,
) -> Result<Option<&'b [u8]>> {
// search byte must be within the ascii range
debug_assert!(byte.is_ascii());

let mut read = 0;
let mut done = false;
let start = buf.len();
Expand Down Expand Up @@ -397,6 +400,9 @@ impl<'b, R: BufRead> XmlSource<'b, &'b mut Vec<u8>> for R {
/// Consume and discard one character if it matches the given byte. Return
/// true if it matched.
fn skip_one(&mut self, byte: u8, position: &mut usize) -> Result<bool> {
// search byte must be within the ascii range
debug_assert!(byte.is_ascii());

match self.peek_one()? {
Some(b) if b == byte => {
*position += 1;
Expand Down
4 changes: 4 additions & 0 deletions src/reader/slice_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ impl<'a> XmlSource<'a, ()> for &'a [u8] {
_buf: (),
position: &mut usize,
) -> Result<Option<&'a [u8]>> {
// search byte must be within the ascii range
debug_assert!(byte.is_ascii());
if self.is_empty() {
return Ok(None);
}
Expand Down Expand Up @@ -217,6 +219,8 @@ impl<'a> XmlSource<'a, ()> for &'a [u8] {
}

fn skip_one(&mut self, byte: u8, position: &mut usize) -> Result<bool> {
// search byte must be within the ascii range
debug_assert!(byte.is_ascii());
if self.first() == Some(&byte) {
*self = &self[1..];
*position += 1;
Expand Down

0 comments on commit 33b6e6d

Please sign in to comment.