@@ -264,9 +264,7 @@ impl<'a> Iterator for Parser<'a> {
264
264
}
265
265
} else {
266
266
if self . is_literal {
267
- let start = self . to_span_index ( self . cur_line_start ) ;
268
- let end = self . to_span_index ( self . input . len ( ) ) ;
269
- let span = start. to ( end) ;
267
+ let span = self . span ( self . cur_line_start , self . input . len ( ) ) ;
270
268
if self . line_spans . last ( ) != Some ( & span) {
271
269
self . line_spans . push ( span) ;
272
270
}
@@ -384,6 +382,12 @@ impl<'a> Parser<'a> {
384
382
InnerOffset ( raw + pos + 1 )
385
383
}
386
384
385
+ fn span ( & self , start_pos : usize , end_pos : usize ) -> InnerSpan {
386
+ let start = self . to_span_index ( start_pos) ;
387
+ let end = self . to_span_index ( end_pos) ;
388
+ start. to ( end)
389
+ }
390
+
387
391
/// Forces consumption of the specified character. If the character is not
388
392
/// found, an error is emitted.
389
393
fn must_consume ( & mut self , c : char ) -> Option < usize > {
@@ -472,9 +476,7 @@ impl<'a> Parser<'a> {
472
476
return & self . input [ start..pos] ;
473
477
}
474
478
'\n' if self . is_literal => {
475
- let start = self . to_span_index ( self . cur_line_start ) ;
476
- let end = self . to_span_index ( pos) ;
477
- self . line_spans . push ( start. to ( end) ) ;
479
+ self . line_spans . push ( self . span ( self . cur_line_start , pos) ) ;
478
480
self . cur_line_start = pos + 1 ;
479
481
self . cur . next ( ) ;
480
482
}
@@ -537,6 +539,10 @@ impl<'a> Parser<'a> {
537
539
}
538
540
}
539
541
542
+ fn current_pos ( & mut self ) -> usize {
543
+ if let Some ( & ( pos, _) ) = self . cur . peek ( ) { pos } else { self . input . len ( ) }
544
+ }
545
+
540
546
/// Parses a format specifier at the current position, returning all of the
541
547
/// relevant information in the `FormatSpec` struct.
542
548
fn format ( & mut self ) -> FormatSpec < ' a > {
@@ -590,39 +596,37 @@ impl<'a> Parser<'a> {
590
596
// no '0' flag and '0$' as the width instead.
591
597
if let Some ( end) = self . consume_pos ( '$' ) {
592
598
spec. width = CountIsParam ( 0 ) ;
593
-
594
- if let Some ( ( pos, _) ) = self . cur . peek ( ) . cloned ( ) {
595
- spec. width_span = Some ( self . to_span_index ( pos - 2 ) . to ( self . to_span_index ( pos) ) ) ;
596
- }
599
+ spec. width_span = Some ( self . span ( end - 1 , end + 1 ) ) ;
597
600
havewidth = true ;
598
- spec. width_span = Some ( self . to_span_index ( end - 1 ) . to ( self . to_span_index ( end + 1 ) ) ) ;
599
601
} else {
600
602
spec. flags |= 1 << ( FlagSignAwareZeroPad as u32 ) ;
601
603
}
602
604
}
605
+
603
606
if !havewidth {
604
- let width_span_start = if let Some ( ( pos, _) ) = self . cur . peek ( ) { * pos } else { 0 } ;
605
- let ( w, sp) = self . count ( width_span_start) ;
606
- spec. width = w;
607
- spec. width_span = sp;
607
+ let start = self . current_pos ( ) ;
608
+ spec. width = self . count ( start) ;
609
+ if spec. width != CountImplied {
610
+ let end = self . current_pos ( ) ;
611
+ spec. width_span = Some ( self . span ( start, end) ) ;
612
+ }
608
613
}
609
614
610
615
if let Some ( start) = self . consume_pos ( '.' ) {
611
- if let Some ( end ) = self . consume_pos ( '*' ) {
616
+ if self . consume ( '*' ) {
612
617
// Resolve `CountIsNextParam`.
613
618
// We can do this immediately as `position` is resolved later.
614
619
let i = self . curarg ;
615
620
self . curarg += 1 ;
616
621
spec. precision = CountIsParam ( i) ;
617
- spec. precision_span =
618
- Some ( self . to_span_index ( start) . to ( self . to_span_index ( end + 1 ) ) ) ;
619
622
} else {
620
- let ( p, sp) = self . count ( start) ;
621
- spec. precision = p;
622
- spec. precision_span = sp;
623
+ spec. precision = self . count ( start + 1 ) ;
623
624
}
625
+ let end = self . current_pos ( ) ;
626
+ spec. precision_span = Some ( self . span ( start, end) ) ;
624
627
}
625
- let ty_span_start = self . cur . peek ( ) . map ( |( pos, _) | * pos) ;
628
+
629
+ let ty_span_start = self . current_pos ( ) ;
626
630
// Optional radix followed by the actual format specifier
627
631
if self . consume ( 'x' ) {
628
632
if self . consume ( '?' ) {
@@ -642,11 +646,9 @@ impl<'a> Parser<'a> {
642
646
spec. ty = "?" ;
643
647
} else {
644
648
spec. ty = self . word ( ) ;
645
- let ty_span_end = self . cur . peek ( ) . map ( |( pos, _) | * pos) ;
646
649
if !spec. ty . is_empty ( ) {
647
- spec. ty_span = ty_span_start
648
- . and_then ( |s| ty_span_end. map ( |e| ( s, e) ) )
649
- . map ( |( start, end) | self . to_span_index ( start) . to ( self . to_span_index ( end) ) ) ;
650
+ let ty_span_end = self . current_pos ( ) ;
651
+ spec. ty_span = Some ( self . span ( ty_span_start, ty_span_end) ) ;
650
652
}
651
653
}
652
654
spec
@@ -670,13 +672,11 @@ impl<'a> Parser<'a> {
670
672
return spec;
671
673
}
672
674
673
- let ty_span_start = self . cur . peek ( ) . map ( | ( pos , _ ) | * pos ) ;
675
+ let ty_span_start = self . current_pos ( ) ;
674
676
spec. ty = self . word ( ) ;
675
- let ty_span_end = self . cur . peek ( ) . map ( |( pos, _) | * pos) ;
676
677
if !spec. ty . is_empty ( ) {
677
- spec. ty_span = ty_span_start
678
- . and_then ( |s| ty_span_end. map ( |e| ( s, e) ) )
679
- . map ( |( start, end) | self . to_span_index ( start) . to ( self . to_span_index ( end) ) ) ;
678
+ let ty_span_end = self . current_pos ( ) ;
679
+ spec. ty_span = Some ( self . span ( ty_span_start, ty_span_end) ) ;
680
680
}
681
681
682
682
spec
@@ -685,26 +685,21 @@ impl<'a> Parser<'a> {
685
685
/// Parses a `Count` parameter at the current position. This does not check
686
686
/// for 'CountIsNextParam' because that is only used in precision, not
687
687
/// width.
688
- fn count ( & mut self , start : usize ) -> ( Count < ' a > , Option < InnerSpan > ) {
688
+ fn count ( & mut self , start : usize ) -> Count < ' a > {
689
689
if let Some ( i) = self . integer ( ) {
690
- if let Some ( end) = self . consume_pos ( '$' ) {
691
- let span = self . to_span_index ( start) . to ( self . to_span_index ( end + 1 ) ) ;
692
- ( CountIsParam ( i) , Some ( span) )
693
- } else {
694
- ( CountIs ( i) , None )
695
- }
690
+ if self . consume ( '$' ) { CountIsParam ( i) } else { CountIs ( i) }
696
691
} else {
697
692
let tmp = self . cur . clone ( ) ;
698
693
let word = self . word ( ) ;
699
694
if word. is_empty ( ) {
700
695
self . cur = tmp;
701
- ( CountImplied , None )
696
+ CountImplied
702
697
} else if let Some ( end) = self . consume_pos ( '$' ) {
703
- let span = self . to_span_index ( start + 1 ) . to ( self . to_span_index ( end) ) ;
704
- ( CountIsName ( word, span ) , None )
698
+ let name_span = self . span ( start, end) ;
699
+ CountIsName ( word, name_span )
705
700
} else {
706
701
self . cur = tmp;
707
- ( CountImplied , None )
702
+ CountImplied
708
703
}
709
704
}
710
705
}
@@ -737,7 +732,7 @@ impl<'a> Parser<'a> {
737
732
"invalid argument name `_`" ,
738
733
"invalid argument name" ,
739
734
"argument name cannot be a single underscore" ,
740
- self . to_span_index ( start) . to ( self . to_span_index ( end) ) ,
735
+ self . span ( start, end) ,
741
736
) ;
742
737
}
743
738
word
0 commit comments