@@ -144,6 +144,8 @@ pub struct Parser<'a> {
144
144
cur : iter:: Peekable < str:: CharIndices < ' a > > ,
145
145
/// Error messages accumulated during parsing
146
146
pub errors : Vec < string:: String > ,
147
+ /// Current position of implicit positional argument pointer
148
+ curarg : usize ,
147
149
}
148
150
149
151
impl < ' a > Iterator for Parser < ' a > {
@@ -186,6 +188,7 @@ impl<'a> Parser<'a> {
186
188
input : s,
187
189
cur : s. char_indices ( ) . peekable ( ) ,
188
190
errors : vec ! [ ] ,
191
+ curarg : 0 ,
189
192
}
190
193
}
191
194
@@ -259,9 +262,41 @@ impl<'a> Parser<'a> {
259
262
/// Parses an Argument structure, or what's contained within braces inside
260
263
/// the format string
261
264
fn argument ( & mut self ) -> Argument < ' a > {
265
+ let mut pos = self . position ( ) ;
266
+ let mut format = self . format ( ) ;
267
+
268
+ // Resolve CountIsNextParam's into absolute references.
269
+ // Current argument's position must be known so this is done after
270
+ // format parsing.
271
+ // Curiously, currently {:.*} for named arguments is implemented,
272
+ // and it consumes a positional arg slot just like a positional {:.*}
273
+ // does. The current behavior is reproduced to prevent any
274
+ // incompatibilities.
275
+ match format. precision {
276
+ CountIsNextParam => {
277
+ // eat the current implicit arg
278
+ let i = self . curarg ;
279
+ self . curarg += 1 ;
280
+ format. precision = CountIsParam ( i) ;
281
+ }
282
+ _ => { }
283
+ }
284
+
285
+ // Resolve ArgumentNext's into absolute references.
286
+ // This must come after count resolution because we may consume one
287
+ // more arg if precision is CountIsNextParam.
288
+ match pos {
289
+ ArgumentNext => {
290
+ let i = self . curarg ;
291
+ self . curarg += 1 ;
292
+ pos = ArgumentIs ( i) ;
293
+ }
294
+ _ => { }
295
+ }
296
+
262
297
Argument {
263
- position : self . position ( ) ,
264
- format : self . format ( ) ,
298
+ position : pos ,
299
+ format : format,
265
300
}
266
301
}
267
302
@@ -487,7 +522,7 @@ mod tests {
487
522
fn format_nothing ( ) {
488
523
same ( "{}" ,
489
524
& [ NextArgument ( Argument {
490
- position : ArgumentNext ,
525
+ position : ArgumentIs ( 0 ) ,
491
526
format : fmtdflt ( ) ,
492
527
} ) ] ) ;
493
528
}
@@ -565,7 +600,7 @@ mod tests {
565
600
fn format_counts ( ) {
566
601
same ( "{:10s}" ,
567
602
& [ NextArgument ( Argument {
568
- position : ArgumentNext ,
603
+ position : ArgumentIs ( 0 ) ,
569
604
format : FormatSpec {
570
605
fill : None ,
571
606
align : AlignUnknown ,
@@ -577,7 +612,7 @@ mod tests {
577
612
} ) ] ) ;
578
613
same ( "{:10$.10s}" ,
579
614
& [ NextArgument ( Argument {
580
- position : ArgumentNext ,
615
+ position : ArgumentIs ( 0 ) ,
581
616
format : FormatSpec {
582
617
fill : None ,
583
618
align : AlignUnknown ,
@@ -589,19 +624,19 @@ mod tests {
589
624
} ) ] ) ;
590
625
same ( "{:.*s}" ,
591
626
& [ NextArgument ( Argument {
592
- position : ArgumentNext ,
627
+ position : ArgumentIs ( 1 ) ,
593
628
format : FormatSpec {
594
629
fill : None ,
595
630
align : AlignUnknown ,
596
631
flags : 0 ,
597
- precision : CountIsNextParam ,
632
+ precision : CountIsParam ( 0 ) ,
598
633
width : CountImplied ,
599
634
ty : "s" ,
600
635
} ,
601
636
} ) ] ) ;
602
637
same ( "{:.10$s}" ,
603
638
& [ NextArgument ( Argument {
604
- position : ArgumentNext ,
639
+ position : ArgumentIs ( 0 ) ,
605
640
format : FormatSpec {
606
641
fill : None ,
607
642
align : AlignUnknown ,
@@ -613,7 +648,7 @@ mod tests {
613
648
} ) ] ) ;
614
649
same ( "{:a$.b$s}" ,
615
650
& [ NextArgument ( Argument {
616
- position : ArgumentNext ,
651
+ position : ArgumentIs ( 0 ) ,
617
652
format : FormatSpec {
618
653
fill : None ,
619
654
align : AlignUnknown ,
@@ -628,7 +663,7 @@ mod tests {
628
663
fn format_flags ( ) {
629
664
same ( "{:-}" ,
630
665
& [ NextArgument ( Argument {
631
- position : ArgumentNext ,
666
+ position : ArgumentIs ( 0 ) ,
632
667
format : FormatSpec {
633
668
fill : None ,
634
669
align : AlignUnknown ,
@@ -640,7 +675,7 @@ mod tests {
640
675
} ) ] ) ;
641
676
same ( "{:+#}" ,
642
677
& [ NextArgument ( Argument {
643
- position : ArgumentNext ,
678
+ position : ArgumentIs ( 0 ) ,
644
679
format : FormatSpec {
645
680
fill : None ,
646
681
align : AlignUnknown ,
0 commit comments