Simplify ring buffer pushes

This commit is contained in:
David Tolnay 2022-01-18 19:07:12 -08:00
parent e219b2b5f9
commit 50d722a691
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
2 changed files with 12 additions and 7 deletions

View File

@ -288,12 +288,11 @@ fn scan_begin(&mut self, b: BeginToken) {
self.left_total = 1;
self.right_total = 1;
self.right = self.left;
self.buf.truncate(1);
self.buf.clear();
} else {
self.right += 1;
self.buf.advance_right();
}
self.buf[self.right] = BufEntry { token: Token::Begin(b), size: -self.right_total };
self.buf.push(BufEntry { token: Token::Begin(b), size: -self.right_total });
self.scan_stack.push_front(self.right);
}
@ -302,8 +301,7 @@ fn scan_end(&mut self) {
self.print_end();
} else {
self.right += 1;
self.buf.advance_right();
self.buf[self.right] = BufEntry { token: Token::End, size: -1 };
self.buf.push(BufEntry { token: Token::End, size: -1 });
self.scan_stack.push_front(self.right);
}
}
@ -329,9 +327,8 @@ fn scan_string(&mut self, s: Cow<'static, str>) {
self.print_string(s);
} else {
self.right += 1;
self.buf.advance_right();
let len = s.len() as isize;
self.buf[self.right] = BufEntry { token: Token::String(s), size: len };
self.buf.push(BufEntry { token: Token::String(s), size: len });
self.right_total += len;
self.check_stream();
}

View File

@ -22,6 +22,10 @@ pub fn new() -> Self {
RingBuffer { data: VecDeque::new(), offset: 0 }
}
pub fn push(&mut self, value: T) {
self.data.push_back(value);
}
pub fn advance_right(&mut self)
where
T: Default,
@ -34,6 +38,10 @@ pub fn advance_left(&mut self) {
self.offset += 1;
}
pub fn clear(&mut self) {
self.data.clear();
}
pub fn truncate(&mut self, len: usize) {
self.data.truncate(len);
}