@@ -387,29 +387,15 @@ impl Tm {
387
387
/// Parses the time from the string according to the format string.
388
388
pub fn strptime ( s : & str , format : & str ) -> Result < Tm , String > {
389
389
fn match_str ( s : & str , pos : uint , needle : & str ) -> bool {
390
- let mut i = pos;
391
- for ch in needle. bytes ( ) {
392
- if s. as_bytes ( ) [ i] != ch {
393
- return false ;
394
- }
395
- i += 1 u;
396
- }
397
- return true ;
390
+ return s. slice_from ( pos) . starts_with ( needle) ;
398
391
}
399
392
400
- fn match_strs ( ss : & str , pos : uint , strs : & [ ( String , i32 ) ] )
393
+ fn match_strs ( ss : & str , pos : uint , strs : & [ ( & str , i32 ) ] )
401
394
-> Option < ( i32 , uint ) > {
402
- let mut i = 0 u;
403
- let len = strs. len ( ) ;
404
- while i < len {
405
- match strs[ i] { // can't use let due to let-pattern bugs
406
- ( ref needle, value) => {
407
- if match_str ( ss, pos, needle. as_slice ( ) ) {
408
- return Some ( ( value, pos + needle. len ( ) ) ) ;
409
- }
410
- }
395
+ for & ( needle, value) in strs. iter ( ) {
396
+ if match_str ( ss, pos, needle) {
397
+ return Some ( ( value, pos + needle. len ( ) ) ) ;
411
398
}
412
- i += 1 u;
413
399
}
414
400
415
401
None
@@ -493,59 +479,59 @@ pub fn strptime(s: &str, format: &str) -> Result<Tm, String> {
493
479
-> Result < uint , String > {
494
480
match ch {
495
481
'A' => match match_strs ( s, pos, [
496
- ( "Sunday" . to_string ( ) , 0_i32 ) ,
497
- ( "Monday" . to_string ( ) , 1_i32 ) ,
498
- ( "Tuesday" . to_string ( ) , 2_i32 ) ,
499
- ( "Wednesday" . to_string ( ) , 3_i32 ) ,
500
- ( "Thursday" . to_string ( ) , 4_i32 ) ,
501
- ( "Friday" . to_string ( ) , 5_i32 ) ,
502
- ( "Saturday" . to_string ( ) , 6_i32 )
482
+ ( "Sunday" , 0_i32 ) ,
483
+ ( "Monday" , 1_i32 ) ,
484
+ ( "Tuesday" , 2_i32 ) ,
485
+ ( "Wednesday" , 3_i32 ) ,
486
+ ( "Thursday" , 4_i32 ) ,
487
+ ( "Friday" , 5_i32 ) ,
488
+ ( "Saturday" , 6_i32 )
503
489
] ) {
504
490
Some ( item) => { let ( v, pos) = item; tm. tm_wday = v; Ok ( pos) }
505
491
None => Err ( "Invalid day" . to_string ( ) )
506
492
} ,
507
493
'a' => match match_strs ( s, pos, [
508
- ( "Sun" . to_string ( ) , 0_i32 ) ,
509
- ( "Mon" . to_string ( ) , 1_i32 ) ,
510
- ( "Tue" . to_string ( ) , 2_i32 ) ,
511
- ( "Wed" . to_string ( ) , 3_i32 ) ,
512
- ( "Thu" . to_string ( ) , 4_i32 ) ,
513
- ( "Fri" . to_string ( ) , 5_i32 ) ,
514
- ( "Sat" . to_string ( ) , 6_i32 )
494
+ ( "Sun" , 0_i32 ) ,
495
+ ( "Mon" , 1_i32 ) ,
496
+ ( "Tue" , 2_i32 ) ,
497
+ ( "Wed" , 3_i32 ) ,
498
+ ( "Thu" , 4_i32 ) ,
499
+ ( "Fri" , 5_i32 ) ,
500
+ ( "Sat" , 6_i32 )
515
501
] ) {
516
502
Some ( item) => { let ( v, pos) = item; tm. tm_wday = v; Ok ( pos) }
517
503
None => Err ( "Invalid day" . to_string ( ) )
518
504
} ,
519
505
'B' => match match_strs ( s, pos, [
520
- ( "January" . to_string ( ) , 0_i32 ) ,
521
- ( "February" . to_string ( ) , 1_i32 ) ,
522
- ( "March" . to_string ( ) , 2_i32 ) ,
523
- ( "April" . to_string ( ) , 3_i32 ) ,
524
- ( "May" . to_string ( ) , 4_i32 ) ,
525
- ( "June" . to_string ( ) , 5_i32 ) ,
526
- ( "July" . to_string ( ) , 6_i32 ) ,
527
- ( "August" . to_string ( ) , 7_i32 ) ,
528
- ( "September" . to_string ( ) , 8_i32 ) ,
529
- ( "October" . to_string ( ) , 9_i32 ) ,
530
- ( "November" . to_string ( ) , 10_i32 ) ,
531
- ( "December" . to_string ( ) , 11_i32 )
506
+ ( "January" , 0_i32 ) ,
507
+ ( "February" , 1_i32 ) ,
508
+ ( "March" , 2_i32 ) ,
509
+ ( "April" , 3_i32 ) ,
510
+ ( "May" , 4_i32 ) ,
511
+ ( "June" , 5_i32 ) ,
512
+ ( "July" , 6_i32 ) ,
513
+ ( "August" , 7_i32 ) ,
514
+ ( "September" , 8_i32 ) ,
515
+ ( "October" , 9_i32 ) ,
516
+ ( "November" , 10_i32 ) ,
517
+ ( "December" , 11_i32 )
532
518
] ) {
533
519
Some ( item) => { let ( v, pos) = item; tm. tm_mon = v; Ok ( pos) }
534
520
None => Err ( "Invalid month" . to_string ( ) )
535
521
} ,
536
522
'b' | 'h' => match match_strs ( s, pos, [
537
- ( "Jan" . to_string ( ) , 0_i32 ) ,
538
- ( "Feb" . to_string ( ) , 1_i32 ) ,
539
- ( "Mar" . to_string ( ) , 2_i32 ) ,
540
- ( "Apr" . to_string ( ) , 3_i32 ) ,
541
- ( "May" . to_string ( ) , 4_i32 ) ,
542
- ( "Jun" . to_string ( ) , 5_i32 ) ,
543
- ( "Jul" . to_string ( ) , 6_i32 ) ,
544
- ( "Aug" . to_string ( ) , 7_i32 ) ,
545
- ( "Sep" . to_string ( ) , 8_i32 ) ,
546
- ( "Oct" . to_string ( ) , 9_i32 ) ,
547
- ( "Nov" . to_string ( ) , 10_i32 ) ,
548
- ( "Dec" . to_string ( ) , 11_i32 )
523
+ ( "Jan" , 0_i32 ) ,
524
+ ( "Feb" , 1_i32 ) ,
525
+ ( "Mar" , 2_i32 ) ,
526
+ ( "Apr" , 3_i32 ) ,
527
+ ( "May" , 4_i32 ) ,
528
+ ( "Jun" , 5_i32 ) ,
529
+ ( "Jul" , 6_i32 ) ,
530
+ ( "Aug" , 7_i32 ) ,
531
+ ( "Sep" , 8_i32 ) ,
532
+ ( "Oct" , 9_i32 ) ,
533
+ ( "Nov" , 10_i32 ) ,
534
+ ( "Dec" , 11_i32 )
549
535
] ) {
550
536
Some ( item) => { let ( v, pos) = item; tm. tm_mon = v; Ok ( pos) }
551
537
None => Err ( "Invalid month" . to_string ( ) )
@@ -659,13 +645,13 @@ pub fn strptime(s: &str, format: &str) -> Result<Tm, String> {
659
645
}
660
646
'n' => parse_char ( s, pos, '\n' ) ,
661
647
'P' => match match_strs ( s, pos,
662
- [ ( "am" . to_string ( ) , 0_i32 ) , ( "pm" . to_string ( ) , 12_i32 ) ] ) {
648
+ [ ( "am" , 0_i32 ) , ( "pm" , 12_i32 ) ] ) {
663
649
664
650
Some ( item) => { let ( v, pos) = item; tm. tm_hour += v; Ok ( pos) }
665
651
None => Err ( "Invalid hour" . to_string ( ) )
666
652
} ,
667
653
'p' => match match_strs ( s, pos,
668
- [ ( "AM" . to_string ( ) , 0_i32 ) , ( "PM" . to_string ( ) , 12_i32 ) ] ) {
654
+ [ ( "AM" , 0_i32 ) , ( "PM" , 12_i32 ) ] ) {
669
655
670
656
Some ( item) => { let ( v, pos) = item; tm. tm_hour += v; Ok ( pos) }
671
657
None => Err ( "Invalid hour" . to_string ( ) )
0 commit comments