From 4d3faae5cd34cc3a34a7c01fb2f22c68e092e0eb Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Tue, 18 Jan 2022 20:19:10 -0800 Subject: [PATCH] Eliminate left and right cursors in favor of ring buffer --- compiler/rustc_ast_pretty/src/pp.rs | 43 ++++++++---------------- compiler/rustc_ast_pretty/src/pp/ring.rs | 20 ++++++++++- 2 files changed, 33 insertions(+), 30 deletions(-) diff --git a/compiler/rustc_ast_pretty/src/pp.rs b/compiler/rustc_ast_pretty/src/pp.rs index 0c9d4ef0e950d..bdd70148d85a0 100644 --- a/compiler/rustc_ast_pretty/src/pp.rs +++ b/compiler/rustc_ast_pretty/src/pp.rs @@ -206,10 +206,6 @@ pub struct Printer { margin: isize, /// Number of spaces left on line space: isize, - /// Index of left side of input stream - left: usize, - /// Index of right side of input stream - right: usize, /// Ring-buffer of tokens and calculated sizes buf: RingBuffer, /// Running size of stream "...left" @@ -245,8 +241,6 @@ impl Printer { out: String::new(), margin: linewidth as isize, space: linewidth as isize, - left: 0, - right: 0, buf: RingBuffer::new(), left_total: 0, right_total: 0, @@ -281,22 +275,18 @@ impl Printer { if self.scan_stack.is_empty() { self.left_total = 1; self.right_total = 1; - self.right = self.left; self.buf.clear(); - } else { - self.right += 1; } - self.buf.push(BufEntry { token: Token::Begin(b), size: -self.right_total }); - self.scan_stack.push_front(self.right); + let right = self.buf.push(BufEntry { token: Token::Begin(b), size: -self.right_total }); + self.scan_stack.push_front(right); } fn scan_end(&mut self) { if self.scan_stack.is_empty() { self.print_end(); } else { - self.right += 1; - self.buf.push(BufEntry { token: Token::End, size: -1 }); - self.scan_stack.push_front(self.right); + let right = self.buf.push(BufEntry { token: Token::End, size: -1 }); + self.scan_stack.push_front(right); } } @@ -304,14 +294,12 @@ impl Printer { if self.scan_stack.is_empty() { self.left_total = 1; self.right_total = 1; - self.right = self.left; self.buf.clear(); } else { self.check_stack(0); - self.right += 1; } - self.buf.push(BufEntry { token: Token::Break(b), size: -self.right_total }); - self.scan_stack.push_front(self.right); + let right = self.buf.push(BufEntry { token: Token::Break(b), size: -self.right_total }); + self.scan_stack.push_front(right); self.right_total += b.blank_space; } @@ -319,7 +307,6 @@ impl Printer { if self.scan_stack.is_empty() { self.print_string(&s); } else { - self.right += 1; let len = s.len() as isize; self.buf.push(BufEntry { token: Token::String(s), size: len }); self.right_total += len; @@ -329,22 +316,22 @@ impl Printer { fn check_stream(&mut self) { while self.right_total - self.left_total > self.space { - if self.scan_stack.back() == Some(&self.left) { + if *self.scan_stack.back().unwrap() == self.buf.index_of_first() { self.scan_stack.pop_back().unwrap(); - self.buf[self.left].size = SIZE_INFINITY; + self.buf.first_mut().unwrap().size = SIZE_INFINITY; } self.advance_left(); - if self.left == self.right { + if self.buf.is_empty() { break; } } } fn advance_left(&mut self) { - let mut left_size = self.buf[self.left].size; + let mut left_size = self.buf.first().unwrap().size; while left_size >= 0 { - let left = self.buf[self.left].token.clone(); + let left = self.buf.first().unwrap().token.clone(); let len = match left { Token::Break(b) => b.blank_space, @@ -360,14 +347,12 @@ impl Printer { self.left_total += len; - if self.left == self.right { + self.buf.advance_left(); + if self.buf.is_empty() { break; } - self.buf.advance_left(); - self.left += 1; - - left_size = self.buf[self.left].size; + left_size = self.buf.first().unwrap().size; } } diff --git a/compiler/rustc_ast_pretty/src/pp/ring.rs b/compiler/rustc_ast_pretty/src/pp/ring.rs index 86b87614c1986..d20142eb591fe 100644 --- a/compiler/rustc_ast_pretty/src/pp/ring.rs +++ b/compiler/rustc_ast_pretty/src/pp/ring.rs @@ -22,8 +22,14 @@ impl RingBuffer { RingBuffer { data: VecDeque::new(), offset: 0 } } - pub fn push(&mut self, value: T) { + pub fn is_empty(&self) -> bool { + self.data.is_empty() + } + + pub fn push(&mut self, value: T) -> usize { + let index = self.offset + self.data.len(); self.data.push_back(value); + index } pub fn advance_left(&mut self) { @@ -35,6 +41,18 @@ impl RingBuffer { self.data.clear(); } + pub fn index_of_first(&self) -> usize { + self.offset + } + + pub fn first(&self) -> Option<&T> { + self.data.front() + } + + pub fn first_mut(&mut self) -> Option<&mut T> { + self.data.front_mut() + } + pub fn last(&self) -> Option<&T> { self.data.back() }