@@ -264,9 +264,7 @@ impl<'a> Iterator for Parser<'a> {
264264 }
265265 } else {
266266 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 ( ) ) ;
270268 if self . line_spans . last ( ) != Some ( & span) {
271269 self . line_spans . push ( span) ;
272270 }
@@ -384,6 +382,12 @@ impl<'a> Parser<'a> {
384382 InnerOffset ( raw + pos + 1 )
385383 }
386384
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+
387391 /// Forces consumption of the specified character. If the character is not
388392 /// found, an error is emitted.
389393 fn must_consume ( & mut self , c : char ) -> Option < usize > {
@@ -472,9 +476,7 @@ impl<'a> Parser<'a> {
472476 return & self . input [ start..pos] ;
473477 }
474478 '\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) ) ;
478480 self . cur_line_start = pos + 1 ;
479481 self . cur . next ( ) ;
480482 }
@@ -537,6 +539,10 @@ impl<'a> Parser<'a> {
537539 }
538540 }
539541
542+ fn current_pos ( & mut self ) -> usize {
543+ if let Some ( & ( pos, _) ) = self . cur . peek ( ) { pos } else { self . input . len ( ) }
544+ }
545+
540546 /// Parses a format specifier at the current position, returning all of the
541547 /// relevant information in the `FormatSpec` struct.
542548 fn format ( & mut self ) -> FormatSpec < ' a > {
@@ -590,39 +596,37 @@ impl<'a> Parser<'a> {
590596 // no '0' flag and '0$' as the width instead.
591597 if let Some ( end) = self . consume_pos ( '$' ) {
592598 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 ) ) ;
597600 havewidth = true ;
598- spec. width_span = Some ( self . to_span_index ( end - 1 ) . to ( self . to_span_index ( end + 1 ) ) ) ;
599601 } else {
600602 spec. flags |= 1 << ( FlagSignAwareZeroPad as u32 ) ;
601603 }
602604 }
605+
603606 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+ }
608613 }
609614
610615 if let Some ( start) = self . consume_pos ( '.' ) {
611- if let Some ( end ) = self . consume_pos ( '*' ) {
616+ if self . consume ( '*' ) {
612617 // Resolve `CountIsNextParam`.
613618 // We can do this immediately as `position` is resolved later.
614619 let i = self . curarg ;
615620 self . curarg += 1 ;
616621 spec. precision = CountIsParam ( i) ;
617- spec. precision_span =
618- Some ( self . to_span_index ( start) . to ( self . to_span_index ( end + 1 ) ) ) ;
619622 } else {
620- let ( p, sp) = self . count ( start) ;
621- spec. precision = p;
622- spec. precision_span = sp;
623+ spec. precision = self . count ( start + 1 ) ;
623624 }
625+ let end = self . current_pos ( ) ;
626+ spec. precision_span = Some ( self . span ( start, end) ) ;
624627 }
625- let ty_span_start = self . cur . peek ( ) . map ( |( pos, _) | * pos) ;
628+
629+ let ty_span_start = self . current_pos ( ) ;
626630 // Optional radix followed by the actual format specifier
627631 if self . consume ( 'x' ) {
628632 if self . consume ( '?' ) {
@@ -642,11 +646,9 @@ impl<'a> Parser<'a> {
642646 spec. ty = "?" ;
643647 } else {
644648 spec. ty = self . word ( ) ;
645- let ty_span_end = self . cur . peek ( ) . map ( |( pos, _) | * pos) ;
646649 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) ) ;
650652 }
651653 }
652654 spec
@@ -670,13 +672,11 @@ impl<'a> Parser<'a> {
670672 return spec;
671673 }
672674
673- let ty_span_start = self . cur . peek ( ) . map ( | ( pos , _ ) | * pos ) ;
675+ let ty_span_start = self . current_pos ( ) ;
674676 spec. ty = self . word ( ) ;
675- let ty_span_end = self . cur . peek ( ) . map ( |( pos, _) | * pos) ;
676677 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) ) ;
680680 }
681681
682682 spec
@@ -685,26 +685,21 @@ impl<'a> Parser<'a> {
685685 /// Parses a `Count` parameter at the current position. This does not check
686686 /// for 'CountIsNextParam' because that is only used in precision, not
687687 /// width.
688- fn count ( & mut self , start : usize ) -> ( Count < ' a > , Option < InnerSpan > ) {
688+ fn count ( & mut self , start : usize ) -> Count < ' a > {
689689 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) }
696691 } else {
697692 let tmp = self . cur . clone ( ) ;
698693 let word = self . word ( ) ;
699694 if word. is_empty ( ) {
700695 self . cur = tmp;
701- ( CountImplied , None )
696+ CountImplied
702697 } 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 )
705700 } else {
706701 self . cur = tmp;
707- ( CountImplied , None )
702+ CountImplied
708703 }
709704 }
710705 }
@@ -737,7 +732,7 @@ impl<'a> Parser<'a> {
737732 "invalid argument name `_`" ,
738733 "invalid argument name" ,
739734 "argument name cannot be a single underscore" ,
740- self . to_span_index ( start) . to ( self . to_span_index ( end) ) ,
735+ self . span ( start, end) ,
741736 ) ;
742737 }
743738 word
0 commit comments