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 ;
65
+ use std:: fmt;
64
66
use std:: io;
65
- use std:: string;
66
67
67
68
#[ derive( Clone , Copy , PartialEq ) ]
68
69
pub enum Breaks {
@@ -112,35 +113,30 @@ impl Token {
112
113
}
113
114
}
114
115
115
- pub fn tok_str ( token : & Token ) -> String {
116
- match * token {
117
- Token :: String ( ref s, len) => format ! ( "STR({},{})" , s, len) ,
118
- Token :: Break ( _) => "BREAK" . to_string ( ) ,
119
- Token :: Begin ( _) => "BEGIN" . to_string ( ) ,
120
- Token :: End => "END" . to_string ( ) ,
121
- Token :: Eof => "EOF" . to_string ( )
116
+ impl fmt:: Display for Token {
117
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
118
+ match * self {
119
+ Token :: String ( ref s, len) => write ! ( f, "STR({},{})" , s, len) ,
120
+ Token :: Break ( _) => f. write_str ( "BREAK" ) ,
121
+ Token :: Begin ( _) => f. write_str ( "BEGIN" ) ,
122
+ Token :: End => f. write_str ( "END" ) ,
123
+ Token :: Eof => f. write_str ( "EOF" ) ,
124
+ }
122
125
}
123
126
}
124
127
125
- pub fn buf_str ( toks : & [ Token ] ,
126
- szs : & [ isize ] ,
127
- left : usize ,
128
- right : usize ,
129
- lim : usize )
130
- -> String {
128
+ fn buf_str ( toks : & [ Token ] , szs : & [ isize ] , left : usize , right : usize , lim : usize ) -> String {
131
129
let n = toks. len ( ) ;
132
130
assert_eq ! ( n, szs. len( ) ) ;
133
131
let mut i = left;
134
132
let mut l = lim;
135
- let mut s = string :: String :: from ( "[" ) ;
133
+ let mut s = String :: from ( "[" ) ;
136
134
while i != right && l != 0 {
137
135
l -= 1 ;
138
136
if i != left {
139
137
s. push_str ( ", " ) ;
140
138
}
141
- s. push_str ( & format ! ( "{}={}" ,
142
- szs[ i] ,
143
- tok_str( & toks[ i] ) ) ) ;
139
+ s. push_str ( & format ! ( "{}={}" , szs[ i] , & toks[ i] ) ) ;
144
140
i += 1 ;
145
141
i %= n;
146
142
}
@@ -169,7 +165,7 @@ pub fn mk_printer<'a>(out: Box<io::Write+'a>, linewidth: usize) -> Printer<'a> {
169
165
debug ! ( "mk_printer {}" , linewidth) ;
170
166
let token = vec ! [ Token :: Eof ; n] ;
171
167
let size = vec ! [ 0 ; n] ;
172
- let scan_stack = vec ! [ 0 ; n ] ;
168
+ let scan_stack = VecDeque :: with_capacity ( n ) ;
173
169
Printer {
174
170
out : out,
175
171
buf_len : n,
@@ -182,9 +178,6 @@ pub fn mk_printer<'a>(out: Box<io::Write+'a>, linewidth: usize) -> Printer<'a> {
182
178
left_total : 0 ,
183
179
right_total : 0 ,
184
180
scan_stack : scan_stack,
185
- scan_stack_empty : true ,
186
- top : 0 ,
187
- bottom : 0 ,
188
181
print_stack : Vec :: new ( ) ,
189
182
pending_indentation : 0
190
183
}
@@ -246,9 +239,8 @@ pub fn mk_printer<'a>(out: Box<io::Write+'a>, linewidth: usize) -> Printer<'a> {
246
239
/// approximation for purposes of line breaking).
247
240
///
248
241
/// The "input side" of the printer is managed as an abstract process called
249
- /// SCAN, which uses 'scan_stack', 'scan_stack_empty', 'top' and 'bottom', to
250
- /// manage calculating 'size'. SCAN is, in other words, the process of
251
- /// 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.
252
244
///
253
245
/// The "output side" of the printer is managed by an abstract process called
254
246
/// PRINT, which uses 'print_stack', 'margin' and 'space' to figure out what to
@@ -291,13 +283,7 @@ pub struct Printer<'a> {
291
283
/// Begin (if there is any) on top of it. Stuff is flushed off the
292
284
/// bottom as it becomes irrelevant due to the primary ring-buffer
293
285
/// advancing.
294
- scan_stack : Vec < usize > ,
295
- /// Top==bottom disambiguator
296
- scan_stack_empty : bool ,
297
- /// Index of top of scan_stack
298
- top : usize ,
299
- /// Index of bottom of scan_stack
300
- bottom : usize ,
286
+ scan_stack : VecDeque < usize > ,
301
287
/// Stack of blocks-in-progress being flushed by print
302
288
print_stack : Vec < PrintStackElem > ,
303
289
/// Buffered indentation to avoid writing trailing whitespace
@@ -316,15 +302,15 @@ impl<'a> Printer<'a> {
316
302
debug ! ( "pp Vec<{},{}>" , self . left, self . right) ;
317
303
match token {
318
304
Token :: Eof => {
319
- if !self . scan_stack_empty {
305
+ if !self . scan_stack . is_empty ( ) {
320
306
self . check_stack ( 0 ) ;
321
307
self . advance_left ( ) ?;
322
308
}
323
309
self . indent ( 0 ) ;
324
310
Ok ( ( ) )
325
311
}
326
312
Token :: Begin ( b) => {
327
- if self . scan_stack_empty {
313
+ if self . scan_stack . is_empty ( ) {
328
314
self . left_total = 1 ;
329
315
self . right_total = 1 ;
330
316
self . left = 0 ;
@@ -339,7 +325,7 @@ impl<'a> Printer<'a> {
339
325
Ok ( ( ) )
340
326
}
341
327
Token :: End => {
342
- if self . scan_stack_empty {
328
+ if self . scan_stack . is_empty ( ) {
343
329
debug ! ( "pp End/print Vec<{},{}>" , self . left, self . right) ;
344
330
self . print ( token, 0 )
345
331
} else {
@@ -353,7 +339,7 @@ impl<'a> Printer<'a> {
353
339
}
354
340
}
355
341
Token :: Break ( b) => {
356
- if self . scan_stack_empty {
342
+ if self . scan_stack . is_empty ( ) {
357
343
self . left_total = 1 ;
358
344
self . right_total = 1 ;
359
345
self . left = 0 ;
@@ -370,7 +356,7 @@ impl<'a> Printer<'a> {
370
356
Ok ( ( ) )
371
357
}
372
358
Token :: String ( s, len) => {
373
- if self . scan_stack_empty {
359
+ if self . scan_stack . is_empty ( ) {
374
360
debug ! ( "pp String('{}')/print Vec<{},{}>" ,
375
361
s, self . left, self . right) ;
376
362
self . print ( Token :: String ( s, len) , len)
@@ -392,12 +378,10 @@ impl<'a> Printer<'a> {
392
378
if self . right_total - self . left_total > self . space {
393
379
debug ! ( "scan window is {}, longer than space on line ({})" ,
394
380
self . right_total - self . left_total, self . space) ;
395
- if !self . scan_stack_empty {
396
- if self . left == self . scan_stack [ self . bottom ] {
397
- debug ! ( "setting {} to infinity and popping" , self . left) ;
398
- let scanned = self . scan_pop_bottom ( ) ;
399
- self . size [ scanned] = SIZE_INFINITY ;
400
- }
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 ;
401
385
}
402
386
self . advance_left ( ) ?;
403
387
if self . left != self . right {
@@ -408,43 +392,21 @@ impl<'a> Printer<'a> {
408
392
}
409
393
pub fn scan_push ( & mut self , x : usize ) {
410
394
debug ! ( "scan_push {}" , x) ;
411
- if self . scan_stack_empty {
412
- self . scan_stack_empty = false ;
413
- } else {
414
- self . top += 1 ;
415
- self . top %= self . buf_len ;
416
- assert ! ( ( self . top != self . bottom) ) ;
417
- }
418
- self . scan_stack [ self . top ] = x;
395
+ self . scan_stack . push_front ( x) ;
419
396
}
420
397
pub fn scan_pop ( & mut self ) -> usize {
421
- assert ! ( ( !self . scan_stack_empty) ) ;
422
- let x = self . scan_stack [ self . top ] ;
423
- if self . top == self . bottom {
424
- self . scan_stack_empty = true ;
425
- } else {
426
- self . top += self . buf_len - 1 ; self . top %= self . buf_len ;
427
- }
428
- return x;
398
+ self . scan_stack . pop_front ( ) . unwrap ( )
429
399
}
430
400
pub fn scan_top ( & mut self ) -> usize {
431
- assert ! ( ( !self . scan_stack_empty) ) ;
432
- return self . scan_stack [ self . top ] ;
401
+ * self . scan_stack . front ( ) . unwrap ( )
433
402
}
434
403
pub fn scan_pop_bottom ( & mut self ) -> usize {
435
- assert ! ( ( !self . scan_stack_empty) ) ;
436
- let x = self . scan_stack [ self . bottom ] ;
437
- if self . top == self . bottom {
438
- self . scan_stack_empty = true ;
439
- } else {
440
- self . bottom += 1 ; self . bottom %= self . buf_len ;
441
- }
442
- return x;
404
+ self . scan_stack . pop_back ( ) . unwrap ( )
443
405
}
444
406
pub fn advance_right ( & mut self ) {
445
407
self . right += 1 ;
446
408
self . right %= self . buf_len ;
447
- assert ! ( ( self . right != self . left) ) ;
409
+ assert ! ( self . right != self . left) ;
448
410
}
449
411
pub fn advance_left ( & mut self ) -> io:: Result < ( ) > {
450
412
debug ! ( "advance_left Vec<{},{}>, sizeof({})={}" , self . left, self . right,
@@ -481,7 +443,7 @@ impl<'a> Printer<'a> {
481
443
Ok ( ( ) )
482
444
}
483
445
pub fn check_stack ( & mut self , k : isize ) {
484
- if !self . scan_stack_empty {
446
+ if !self . scan_stack . is_empty ( ) {
485
447
let x = self . scan_top ( ) ;
486
448
match self . token [ x] {
487
449
Token :: Begin ( _) => {
@@ -512,19 +474,16 @@ impl<'a> Printer<'a> {
512
474
let ret = write ! ( self . out, "\n " ) ;
513
475
self . pending_indentation = 0 ;
514
476
self . indent ( amount) ;
515
- return ret;
477
+ ret
516
478
}
517
479
pub fn indent ( & mut self , amount : isize ) {
518
480
debug ! ( "INDENT {}" , amount) ;
519
481
self . pending_indentation += amount;
520
482
}
521
483
pub fn get_top ( & mut self ) -> PrintStackElem {
522
- let print_stack = & mut self . print_stack ;
523
- let n = print_stack. len ( ) ;
524
- if n != 0 {
525
- ( * print_stack) [ n - 1 ]
526
- } else {
527
- PrintStackElem {
484
+ match self . print_stack . last ( ) {
485
+ Some ( el) => * el,
486
+ None => PrintStackElem {
528
487
offset : 0 ,
529
488
pbreak : PrintStackBreak :: Broken ( Breaks :: Inconsistent )
530
489
}
@@ -538,7 +497,7 @@ impl<'a> Printer<'a> {
538
497
write ! ( self . out, "{}" , s)
539
498
}
540
499
pub fn print ( & mut self , token : Token , l : isize ) -> io:: Result < ( ) > {
541
- debug ! ( "print {} {} (remaining line space={})" , tok_str ( & token) , l,
500
+ debug ! ( "print {} {} (remaining line space={})" , token, l,
542
501
self . space) ;
543
502
debug ! ( "{}" , buf_str( & self . token,
544
503
& self . size,
@@ -566,7 +525,7 @@ impl<'a> Printer<'a> {
566
525
Token :: End => {
567
526
debug ! ( "print End -> pop End" ) ;
568
527
let print_stack = & mut self . print_stack ;
569
- assert ! ( ( !print_stack. is_empty( ) ) ) ;
528
+ assert ! ( !print_stack. is_empty( ) ) ;
570
529
print_stack. pop ( ) . unwrap ( ) ;
571
530
Ok ( ( ) )
572
531
}
@@ -603,12 +562,12 @@ impl<'a> Printer<'a> {
603
562
}
604
563
}
605
564
}
606
- Token :: String ( s, len) => {
565
+ Token :: String ( ref s, len) => {
607
566
debug ! ( "print String({})" , s) ;
608
567
assert_eq ! ( l, len) ;
609
568
// assert!(l <= space);
610
569
self . space -= len;
611
- self . print_str ( & s [ .. ] )
570
+ self . print_str ( s )
612
571
}
613
572
Token :: Eof => {
614
573
// Eof should never get here.
@@ -652,15 +611,15 @@ pub fn eof(p: &mut Printer) -> io::Result<()> {
652
611
}
653
612
654
613
pub fn word ( p : & mut Printer , wrd : & str ) -> io:: Result < ( ) > {
655
- p. pretty_print ( Token :: String ( /* bad */ wrd. to_string ( ) , wrd. len ( ) as isize ) )
614
+ p. pretty_print ( Token :: String ( wrd. to_string ( ) , wrd. len ( ) as isize ) )
656
615
}
657
616
658
617
pub fn huge_word ( p : & mut Printer , wrd : & str ) -> io:: Result < ( ) > {
659
- p. pretty_print ( Token :: String ( /* bad */ wrd. to_string ( ) , SIZE_INFINITY ) )
618
+ p. pretty_print ( Token :: String ( wrd. to_string ( ) , SIZE_INFINITY ) )
660
619
}
661
620
662
621
pub fn zero_word ( p : & mut Printer , wrd : & str ) -> io:: Result < ( ) > {
663
- p. pretty_print ( Token :: String ( /* bad */ wrd. to_string ( ) , 0 ) )
622
+ p. pretty_print ( Token :: String ( wrd. to_string ( ) , 0 ) )
664
623
}
665
624
666
625
pub fn spaces ( p : & mut Printer , n : usize ) -> io:: Result < ( ) > {
0 commit comments