@@ -263,13 +263,13 @@ pub(crate) mod printf {
263263 }
264264
265265 impl Num {
266- fn from_str ( s : & str , arg : Option < & str > ) -> Self {
266+ fn from_str ( s : & str , arg : Option < & str > ) -> Option < Self > {
267267 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) )
269269 } else if s == "*" {
270- Num :: Next
270+ Some ( Num :: Next )
271271 } else {
272- Num :: Num ( s. parse ( ) . unwrap_or_else ( |_| panic ! ( "invalid format num `{s:?}`" ) ) )
272+ s. parse ( ) . ok ( ) . map ( |num| Num :: Num ( num) )
273273 }
274274 }
275275
@@ -421,7 +421,10 @@ pub(crate) mod printf {
421421 state = Prec ;
422422 parameter = None ;
423423 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+ }
425428 move_to ! ( end) ;
426429 }
427430 // It's invalid, is what it is.
@@ -452,7 +455,10 @@ pub(crate) mod printf {
452455 '1' ..='9' => {
453456 let end = at_next_cp_while ( next, char:: is_ascii_digit) ;
454457 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+ }
456462 move_to ! ( end) ;
457463 }
458464 _ => {
@@ -468,7 +474,7 @@ pub(crate) mod printf {
468474 match end. next_cp ( ) {
469475 Some ( ( '$' , end2) ) => {
470476 state = Prec ;
471- width = Some ( Num :: from_str ( "" , Some ( at. slice_between ( end) . unwrap ( ) ) ) ) ;
477+ width = Num :: from_str ( "" , at. slice_between ( end) ) ;
472478 move_to ! ( end2) ;
473479 }
474480 _ => {
@@ -500,7 +506,7 @@ pub(crate) mod printf {
500506 match end. next_cp ( ) {
501507 Some ( ( '$' , end2) ) => {
502508 state = Length ;
503- precision = Some ( Num :: from_str ( "*" , next. slice_between ( end) ) ) ;
509+ precision = Num :: from_str ( "*" , next. slice_between ( end) ) ;
504510 move_to ! ( end2) ;
505511 }
506512 _ => {
@@ -513,7 +519,7 @@ pub(crate) mod printf {
513519 '0' ..='9' => {
514520 let end = at_next_cp_while ( next, char:: is_ascii_digit) ;
515521 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 ) ) ;
517523 move_to ! ( end) ;
518524 }
519525 _ => return fallback ( ) ,
0 commit comments