@@ -371,8 +371,8 @@ fn string(input: Cursor) -> Result<Cursor, Reject> {
371
371
}
372
372
}
373
373
374
- fn cooked_string ( input : Cursor ) -> Result < Cursor , Reject > {
375
- let mut chars = input. char_indices ( ) . peekable ( ) ;
374
+ fn cooked_string ( mut input : Cursor ) -> Result < Cursor , Reject > {
375
+ let mut chars = input. char_indices ( ) ;
376
376
377
377
while let Some ( ( i, ch) ) = chars. next ( ) {
378
378
match ch {
@@ -393,21 +393,10 @@ fn cooked_string(input: Cursor) -> Result<Cursor, Reject> {
393
393
Some ( ( _, 'u' ) ) => {
394
394
backslash_u ( & mut chars) ?;
395
395
}
396
- Some ( ( _, ch @ '\n' ) ) | Some ( ( _, ch @ '\r' ) ) => {
397
- let mut last = ch;
398
- loop {
399
- if last == '\r' && chars. next ( ) . map_or ( true , |( _, ch) | ch != '\n' ) {
400
- return Err ( Reject ) ;
401
- }
402
- match chars. peek ( ) {
403
- Some ( ( _, ch @ ' ' ) ) | Some ( ( _, ch @ '\t' ) ) | Some ( ( _, ch @ '\n' ) )
404
- | Some ( ( _, ch @ '\r' ) ) => {
405
- last = * ch;
406
- chars. next ( ) ;
407
- }
408
- _ => break ,
409
- }
410
- }
396
+ Some ( ( newline, ch @ '\n' ) ) | Some ( ( newline, ch @ '\r' ) ) => {
397
+ input = input. advance ( newline + 1 ) ;
398
+ trailing_backslash ( & mut input, ch as u8 ) ?;
399
+ chars = input. char_indices ( ) ;
411
400
}
412
401
_ => break ,
413
402
} ,
@@ -465,26 +454,9 @@ fn cooked_byte_string(mut input: Cursor) -> Result<Cursor, Reject> {
465
454
Some ( ( _, b'n' ) ) | Some ( ( _, b'r' ) ) | Some ( ( _, b't' ) ) | Some ( ( _, b'\\' ) )
466
455
| Some ( ( _, b'0' ) ) | Some ( ( _, b'\'' ) ) | Some ( ( _, b'"' ) ) => { }
467
456
Some ( ( newline, b @ b'\n' ) ) | Some ( ( newline, b @ b'\r' ) ) => {
468
- let mut last = b;
469
- let rest = input. advance ( newline + 1 ) ;
470
- let mut whitespace = rest. bytes ( ) . enumerate ( ) ;
471
- loop {
472
- if last == b'\r' && whitespace. next ( ) . map_or ( true , |( _, b) | b != b'\n' ) {
473
- return Err ( Reject ) ;
474
- }
475
- match whitespace. next ( ) {
476
- Some ( ( _, b @ b' ' ) ) | Some ( ( _, b @ b'\t' ) ) | Some ( ( _, b @ b'\n' ) )
477
- | Some ( ( _, b @ b'\r' ) ) => {
478
- last = b;
479
- }
480
- Some ( ( offset, _) ) => {
481
- input = rest. advance ( offset) ;
482
- bytes = input. bytes ( ) . enumerate ( ) ;
483
- break ;
484
- }
485
- None => return Err ( Reject ) ,
486
- }
487
- }
457
+ input = input. advance ( newline + 1 ) ;
458
+ trailing_backslash ( & mut input, b) ?;
459
+ bytes = input. bytes ( ) . enumerate ( ) ;
488
460
}
489
461
_ => break ,
490
462
} ,
@@ -567,8 +539,8 @@ fn raw_c_string(input: Cursor) -> Result<Cursor, Reject> {
567
539
Err ( Reject )
568
540
}
569
541
570
- fn cooked_c_string ( input : Cursor ) -> Result < Cursor , Reject > {
571
- let mut chars = input. char_indices ( ) . peekable ( ) ;
542
+ fn cooked_c_string ( mut input : Cursor ) -> Result < Cursor , Reject > {
543
+ let mut chars = input. char_indices ( ) ;
572
544
573
545
while let Some ( ( i, ch) ) = chars. next ( ) {
574
546
match ch {
@@ -591,21 +563,10 @@ fn cooked_c_string(input: Cursor) -> Result<Cursor, Reject> {
591
563
break ;
592
564
}
593
565
}
594
- Some ( ( _, ch @ '\n' ) ) | Some ( ( _, ch @ '\r' ) ) => {
595
- let mut last = ch;
596
- loop {
597
- if last == '\r' && chars. next ( ) . map_or ( true , |( _, ch) | ch != '\n' ) {
598
- return Err ( Reject ) ;
599
- }
600
- match chars. peek ( ) {
601
- Some ( ( _, ch @ ' ' ) ) | Some ( ( _, ch @ '\t' ) ) | Some ( ( _, ch @ '\n' ) )
602
- | Some ( ( _, ch @ '\r' ) ) => {
603
- last = * ch;
604
- chars. next ( ) ;
605
- }
606
- _ => break ,
607
- }
608
- }
566
+ Some ( ( newline, ch @ '\n' ) ) | Some ( ( newline, ch @ '\r' ) ) => {
567
+ input = input. advance ( newline + 1 ) ;
568
+ trailing_backslash ( & mut input, ch as u8 ) ?;
569
+ chars = input. char_indices ( ) ;
609
570
}
610
571
_ => break ,
611
572
} ,
@@ -730,6 +691,26 @@ where
730
691
Err ( Reject )
731
692
}
732
693
694
+ fn trailing_backslash ( input : & mut Cursor , mut last : u8 ) -> Result < ( ) , Reject > {
695
+ let mut whitespace = input. bytes ( ) . enumerate ( ) ;
696
+ loop {
697
+ if last == b'\r' && whitespace. next ( ) . map_or ( true , |( _, b) | b != b'\n' ) {
698
+ return Err ( Reject ) ;
699
+ }
700
+ match whitespace. next ( ) {
701
+ Some ( ( _, b @ b' ' ) ) | Some ( ( _, b @ b'\t' ) ) | Some ( ( _, b @ b'\n' ) )
702
+ | Some ( ( _, b @ b'\r' ) ) => {
703
+ last = b;
704
+ }
705
+ Some ( ( offset, _) ) => {
706
+ * input = input. advance ( offset) ;
707
+ return Ok ( ( ) ) ;
708
+ }
709
+ None => return Err ( Reject ) ,
710
+ }
711
+ }
712
+ }
713
+
733
714
fn float ( input : Cursor ) -> Result < Cursor , Reject > {
734
715
let mut rest = float_digits ( input) ?;
735
716
if let Some ( ch) = rest. chars ( ) . next ( ) {
0 commit comments