@@ -263,13 +263,13 @@ pub(crate) mod printf {
263
263
}
264
264
265
265
impl Num {
266
- fn from_str ( s : & str , arg : Option < & str > ) -> Self {
266
+ fn from_str ( s : & str , arg : Option < & str > ) -> Option < Self > {
267
267
if let Some ( arg) = arg {
268
- Num :: Arg ( arg. parse ( ) . unwrap_or_else ( |_| panic ! ( "invalid format arg `{arg:?}`" ) ) )
268
+ arg. parse ( ) . ok ( ) . map ( |arg| Num :: Arg ( arg) )
269
269
} else if s == "*" {
270
- Num :: Next
270
+ Some ( Num :: Next )
271
271
} else {
272
- Num :: Num ( s. parse ( ) . unwrap_or_else ( |_| panic ! ( "invalid format num `{s:?}`" ) ) )
272
+ s. parse ( ) . ok ( ) . map ( |num| Num :: Num ( num) )
273
273
}
274
274
}
275
275
@@ -421,7 +421,10 @@ pub(crate) mod printf {
421
421
state = Prec ;
422
422
parameter = None ;
423
423
flags = "" ;
424
- width = Some ( Num :: from_str ( at. slice_between ( end) . unwrap ( ) , None ) ) ;
424
+ width = at. slice_between ( end) . and_then ( |num| Num :: from_str ( num, None ) ) ;
425
+ if width. is_none ( ) {
426
+ return fallback ( ) ;
427
+ }
425
428
move_to ! ( end) ;
426
429
}
427
430
// It's invalid, is what it is.
@@ -452,7 +455,10 @@ pub(crate) mod printf {
452
455
'1' ..='9' => {
453
456
let end = at_next_cp_while ( next, char:: is_ascii_digit) ;
454
457
state = Prec ;
455
- width = Some ( Num :: from_str ( at. slice_between ( end) . unwrap ( ) , None ) ) ;
458
+ width = at. slice_between ( end) . and_then ( |num| Num :: from_str ( num, None ) ) ;
459
+ if width. is_none ( ) {
460
+ return fallback ( ) ;
461
+ }
456
462
move_to ! ( end) ;
457
463
}
458
464
_ => {
@@ -468,7 +474,7 @@ pub(crate) mod printf {
468
474
match end. next_cp ( ) {
469
475
Some ( ( '$' , end2) ) => {
470
476
state = Prec ;
471
- width = Some ( Num :: from_str ( "" , Some ( at. slice_between ( end) . unwrap ( ) ) ) ) ;
477
+ width = Num :: from_str ( "" , at. slice_between ( end) ) ;
472
478
move_to ! ( end2) ;
473
479
}
474
480
_ => {
@@ -500,7 +506,7 @@ pub(crate) mod printf {
500
506
match end. next_cp ( ) {
501
507
Some ( ( '$' , end2) ) => {
502
508
state = Length ;
503
- precision = Some ( Num :: from_str ( "*" , next. slice_between ( end) ) ) ;
509
+ precision = Num :: from_str ( "*" , next. slice_between ( end) ) ;
504
510
move_to ! ( end2) ;
505
511
}
506
512
_ => {
@@ -513,7 +519,7 @@ pub(crate) mod printf {
513
519
'0' ..='9' => {
514
520
let end = at_next_cp_while ( next, char:: is_ascii_digit) ;
515
521
state = Length ;
516
- precision = Some ( Num :: from_str ( at. slice_between ( end) . unwrap ( ) , None ) ) ;
522
+ precision = at. slice_between ( end) . and_then ( |num| Num :: from_str ( num , None ) ) ;
517
523
move_to ! ( end) ;
518
524
}
519
525
_ => return fallback ( ) ,
0 commit comments