6161//! line (which it can't) and so naturally place the content on its own line to
6262//! avoid combining it with other lines and making matters even worse.
6363
64+ use std:: collections:: VecDeque ;
6465use std:: fmt;
6566use std:: io;
6667
@@ -164,7 +165,7 @@ pub fn mk_printer<'a>(out: Box<io::Write+'a>, linewidth: usize) -> Printer<'a> {
164165 debug ! ( "mk_printer {}" , linewidth) ;
165166 let token = vec ! [ Token :: Eof ; n] ;
166167 let size = vec ! [ 0 ; n] ;
167- let scan_stack = vec ! [ 0 ; n ] ;
168+ let scan_stack = VecDeque :: with_capacity ( n ) ;
168169 Printer {
169170 out : out,
170171 buf_len : n,
@@ -177,9 +178,6 @@ pub fn mk_printer<'a>(out: Box<io::Write+'a>, linewidth: usize) -> Printer<'a> {
177178 left_total : 0 ,
178179 right_total : 0 ,
179180 scan_stack : scan_stack,
180- scan_stack_empty : true ,
181- top : 0 ,
182- bottom : 0 ,
183181 print_stack : Vec :: new ( ) ,
184182 pending_indentation : 0
185183 }
@@ -241,9 +239,8 @@ pub fn mk_printer<'a>(out: Box<io::Write+'a>, linewidth: usize) -> Printer<'a> {
241239/// approximation for purposes of line breaking).
242240///
243241/// The "input side" of the printer is managed as an abstract process called
244- /// SCAN, which uses 'scan_stack', 'scan_stack_empty', 'top' and 'bottom', to
245- /// manage calculating 'size'. SCAN is, in other words, the process of
246- /// calculating 'size' entries.
242+ /// SCAN, which uses 'scan_stack', to manage calculating 'size'. SCAN is, in
243+ /// other words, the process of calculating 'size' entries.
247244///
248245/// The "output side" of the printer is managed by an abstract process called
249246/// PRINT, which uses 'print_stack', 'margin' and 'space' to figure out what to
@@ -286,13 +283,7 @@ pub struct Printer<'a> {
286283 /// Begin (if there is any) on top of it. Stuff is flushed off the
287284 /// bottom as it becomes irrelevant due to the primary ring-buffer
288285 /// advancing.
289- scan_stack : Vec < usize > ,
290- /// Top==bottom disambiguator
291- scan_stack_empty : bool ,
292- /// Index of top of scan_stack
293- top : usize ,
294- /// Index of bottom of scan_stack
295- bottom : usize ,
286+ scan_stack : VecDeque < usize > ,
296287 /// Stack of blocks-in-progress being flushed by print
297288 print_stack : Vec < PrintStackElem > ,
298289 /// Buffered indentation to avoid writing trailing whitespace
@@ -311,15 +302,15 @@ impl<'a> Printer<'a> {
311302 debug ! ( "pp Vec<{},{}>" , self . left, self . right) ;
312303 match token {
313304 Token :: Eof => {
314- if !self . scan_stack_empty {
305+ if !self . scan_stack . is_empty ( ) {
315306 self . check_stack ( 0 ) ;
316307 self . advance_left ( ) ?;
317308 }
318309 self . indent ( 0 ) ;
319310 Ok ( ( ) )
320311 }
321312 Token :: Begin ( b) => {
322- if self . scan_stack_empty {
313+ if self . scan_stack . is_empty ( ) {
323314 self . left_total = 1 ;
324315 self . right_total = 1 ;
325316 self . left = 0 ;
@@ -334,7 +325,7 @@ impl<'a> Printer<'a> {
334325 Ok ( ( ) )
335326 }
336327 Token :: End => {
337- if self . scan_stack_empty {
328+ if self . scan_stack . is_empty ( ) {
338329 debug ! ( "pp End/print Vec<{},{}>" , self . left, self . right) ;
339330 self . print ( token, 0 )
340331 } else {
@@ -348,7 +339,7 @@ impl<'a> Printer<'a> {
348339 }
349340 }
350341 Token :: Break ( b) => {
351- if self . scan_stack_empty {
342+ if self . scan_stack . is_empty ( ) {
352343 self . left_total = 1 ;
353344 self . right_total = 1 ;
354345 self . left = 0 ;
@@ -365,7 +356,7 @@ impl<'a> Printer<'a> {
365356 Ok ( ( ) )
366357 }
367358 Token :: String ( s, len) => {
368- if self . scan_stack_empty {
359+ if self . scan_stack . is_empty ( ) {
369360 debug ! ( "pp String('{}')/print Vec<{},{}>" ,
370361 s, self . left, self . right) ;
371362 self . print ( Token :: String ( s, len) , len)
@@ -387,12 +378,10 @@ impl<'a> Printer<'a> {
387378 if self . right_total - self . left_total > self . space {
388379 debug ! ( "scan window is {}, longer than space on line ({})" ,
389380 self . right_total - self . left_total, self . space) ;
390- if !self . scan_stack_empty {
391- if self . left == self . scan_stack [ self . bottom ] {
392- debug ! ( "setting {} to infinity and popping" , self . left) ;
393- let scanned = self . scan_pop_bottom ( ) ;
394- self . size [ scanned] = SIZE_INFINITY ;
395- }
381+ if Some ( & self . left ) == self . scan_stack . back ( ) {
382+ debug ! ( "setting {} to infinity and popping" , self . left) ;
383+ let scanned = self . scan_pop_bottom ( ) ;
384+ self . size [ scanned] = SIZE_INFINITY ;
396385 }
397386 self . advance_left ( ) ?;
398387 if self . left != self . right {
@@ -403,38 +392,16 @@ impl<'a> Printer<'a> {
403392 }
404393 pub fn scan_push ( & mut self , x : usize ) {
405394 debug ! ( "scan_push {}" , x) ;
406- if self . scan_stack_empty {
407- self . scan_stack_empty = false ;
408- } else {
409- self . top += 1 ;
410- self . top %= self . buf_len ;
411- assert ! ( self . top != self . bottom) ;
412- }
413- self . scan_stack [ self . top ] = x;
395+ self . scan_stack . push_front ( x) ;
414396 }
415397 pub fn scan_pop ( & mut self ) -> usize {
416- assert ! ( !self . scan_stack_empty) ;
417- let x = self . scan_stack [ self . top ] ;
418- if self . top == self . bottom {
419- self . scan_stack_empty = true ;
420- } else {
421- self . top += self . buf_len - 1 ; self . top %= self . buf_len ;
422- }
423- x
398+ self . scan_stack . pop_front ( ) . unwrap ( )
424399 }
425400 pub fn scan_top ( & mut self ) -> usize {
426- assert ! ( !self . scan_stack_empty) ;
427- self . scan_stack [ self . top ]
401+ * self . scan_stack . front ( ) . unwrap ( )
428402 }
429403 pub fn scan_pop_bottom ( & mut self ) -> usize {
430- assert ! ( !self . scan_stack_empty) ;
431- let x = self . scan_stack [ self . bottom ] ;
432- if self . top == self . bottom {
433- self . scan_stack_empty = true ;
434- } else {
435- self . bottom += 1 ; self . bottom %= self . buf_len ;
436- }
437- x
404+ self . scan_stack . pop_back ( ) . unwrap ( )
438405 }
439406 pub fn advance_right ( & mut self ) {
440407 self . right += 1 ;
@@ -476,7 +443,7 @@ impl<'a> Printer<'a> {
476443 Ok ( ( ) )
477444 }
478445 pub fn check_stack ( & mut self , k : isize ) {
479- if !self . scan_stack_empty {
446+ if !self . scan_stack . is_empty ( ) {
480447 let x = self . scan_top ( ) ;
481448 match self . token [ x] {
482449 Token :: Begin ( _) => {
0 commit comments