61
61
//! line (which it can't) and so naturally place the content on its own line to
62
62
//! avoid combining it with other lines and making matters even worse.
63
63
64
+ use std:: collections:: VecDeque ;
64
65
use std:: fmt;
65
66
use std:: io;
66
67
@@ -164,7 +165,7 @@ pub fn mk_printer<'a>(out: Box<io::Write+'a>, linewidth: usize) -> Printer<'a> {
164
165
debug ! ( "mk_printer {}" , linewidth) ;
165
166
let token = vec ! [ Token :: Eof ; n] ;
166
167
let size = vec ! [ 0 ; n] ;
167
- let scan_stack = vec ! [ 0 ; n ] ;
168
+ let scan_stack = VecDeque :: with_capacity ( n ) ;
168
169
Printer {
169
170
out : out,
170
171
buf_len : n,
@@ -177,9 +178,6 @@ pub fn mk_printer<'a>(out: Box<io::Write+'a>, linewidth: usize) -> Printer<'a> {
177
178
left_total : 0 ,
178
179
right_total : 0 ,
179
180
scan_stack : scan_stack,
180
- scan_stack_empty : true ,
181
- top : 0 ,
182
- bottom : 0 ,
183
181
print_stack : Vec :: new ( ) ,
184
182
pending_indentation : 0
185
183
}
@@ -241,9 +239,8 @@ pub fn mk_printer<'a>(out: Box<io::Write+'a>, linewidth: usize) -> Printer<'a> {
241
239
/// approximation for purposes of line breaking).
242
240
///
243
241
/// 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.
247
244
///
248
245
/// The "output side" of the printer is managed by an abstract process called
249
246
/// PRINT, which uses 'print_stack', 'margin' and 'space' to figure out what to
@@ -286,13 +283,7 @@ pub struct Printer<'a> {
286
283
/// Begin (if there is any) on top of it. Stuff is flushed off the
287
284
/// bottom as it becomes irrelevant due to the primary ring-buffer
288
285
/// 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 > ,
296
287
/// Stack of blocks-in-progress being flushed by print
297
288
print_stack : Vec < PrintStackElem > ,
298
289
/// Buffered indentation to avoid writing trailing whitespace
@@ -311,15 +302,15 @@ impl<'a> Printer<'a> {
311
302
debug ! ( "pp Vec<{},{}>" , self . left, self . right) ;
312
303
match token {
313
304
Token :: Eof => {
314
- if !self . scan_stack_empty {
305
+ if !self . scan_stack . is_empty ( ) {
315
306
self . check_stack ( 0 ) ;
316
307
self . advance_left ( ) ?;
317
308
}
318
309
self . indent ( 0 ) ;
319
310
Ok ( ( ) )
320
311
}
321
312
Token :: Begin ( b) => {
322
- if self . scan_stack_empty {
313
+ if self . scan_stack . is_empty ( ) {
323
314
self . left_total = 1 ;
324
315
self . right_total = 1 ;
325
316
self . left = 0 ;
@@ -334,7 +325,7 @@ impl<'a> Printer<'a> {
334
325
Ok ( ( ) )
335
326
}
336
327
Token :: End => {
337
- if self . scan_stack_empty {
328
+ if self . scan_stack . is_empty ( ) {
338
329
debug ! ( "pp End/print Vec<{},{}>" , self . left, self . right) ;
339
330
self . print ( token, 0 )
340
331
} else {
@@ -348,7 +339,7 @@ impl<'a> Printer<'a> {
348
339
}
349
340
}
350
341
Token :: Break ( b) => {
351
- if self . scan_stack_empty {
342
+ if self . scan_stack . is_empty ( ) {
352
343
self . left_total = 1 ;
353
344
self . right_total = 1 ;
354
345
self . left = 0 ;
@@ -365,7 +356,7 @@ impl<'a> Printer<'a> {
365
356
Ok ( ( ) )
366
357
}
367
358
Token :: String ( s, len) => {
368
- if self . scan_stack_empty {
359
+ if self . scan_stack . is_empty ( ) {
369
360
debug ! ( "pp String('{}')/print Vec<{},{}>" ,
370
361
s, self . left, self . right) ;
371
362
self . print ( Token :: String ( s, len) , len)
@@ -387,12 +378,10 @@ impl<'a> Printer<'a> {
387
378
if self . right_total - self . left_total > self . space {
388
379
debug ! ( "scan window is {}, longer than space on line ({})" ,
389
380
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 ;
396
385
}
397
386
self . advance_left ( ) ?;
398
387
if self . left != self . right {
@@ -403,38 +392,16 @@ impl<'a> Printer<'a> {
403
392
}
404
393
pub fn scan_push ( & mut self , x : usize ) {
405
394
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) ;
414
396
}
415
397
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 ( )
424
399
}
425
400
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 ( )
428
402
}
429
403
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 ( )
438
405
}
439
406
pub fn advance_right ( & mut self ) {
440
407
self . right += 1 ;
@@ -476,7 +443,7 @@ impl<'a> Printer<'a> {
476
443
Ok ( ( ) )
477
444
}
478
445
pub fn check_stack ( & mut self , k : isize ) {
479
- if !self . scan_stack_empty {
446
+ if !self . scan_stack . is_empty ( ) {
480
447
let x = self . scan_top ( ) ;
481
448
match self . token [ x] {
482
449
Token :: Begin ( _) => {
0 commit comments