@@ -143,16 +143,9 @@ impl<'a> fmt::Show for Ascii {
143
143
144
144
/// Trait for converting into an ascii type.
145
145
pub trait AsciiCast < T > {
146
- /// Convert to an ascii type, fail on non-ASCII input.
147
- #[ inline]
148
- fn to_ascii ( & self ) -> T {
149
- assert ! ( self . is_ascii( ) ) ;
150
- unsafe { self . to_ascii_nocheck ( ) }
151
- }
152
-
153
146
/// Convert to an ascii type, return None on non-ASCII input.
154
147
#[ inline]
155
- fn to_ascii_opt ( & self ) -> Option < T > {
148
+ fn to_ascii ( & self ) -> Option < T > {
156
149
if self . is_ascii ( ) {
157
150
Some ( unsafe { self . to_ascii_nocheck ( ) } )
158
151
} else {
@@ -223,16 +216,9 @@ pub trait OwnedAsciiCast {
223
216
/// Check if convertible to ascii
224
217
fn is_ascii ( & self ) -> bool ;
225
218
226
- /// Take ownership and cast to an ascii vector. Fail on non-ASCII input.
227
- #[ inline]
228
- fn into_ascii ( self ) -> ~[ Ascii ] {
229
- assert ! ( self . is_ascii( ) ) ;
230
- unsafe { self . into_ascii_nocheck ( ) }
231
- }
232
-
233
219
/// Take ownership and cast to an ascii vector. Return None on non-ASCII input.
234
220
#[ inline]
235
- fn into_ascii_opt ( self ) -> Option < ~[ Ascii ] > {
221
+ fn into_ascii ( self ) -> Option < ~[ Ascii ] > {
236
222
if self . is_ascii ( ) {
237
223
Some ( unsafe { self . into_ascii_nocheck ( ) } )
238
224
} else {
@@ -498,29 +484,29 @@ mod tests {
498
484
499
485
#[ test]
500
486
fn test_ascii ( ) {
501
- assert_eq ! ( 65u8 . to_ascii( ) . to_byte( ) , 65u8 ) ;
502
- assert_eq ! ( 65u8 . to_ascii( ) . to_char( ) , 'A' ) ;
503
- assert_eq ! ( 'A' . to_ascii( ) . to_char( ) , 'A' ) ;
504
- assert_eq ! ( 'A' . to_ascii( ) . to_byte( ) , 65u8 ) ;
505
-
506
- assert_eq ! ( 'A' . to_ascii( ) . to_lower( ) . to_char( ) , 'a' ) ;
507
- assert_eq ! ( 'Z' . to_ascii( ) . to_lower( ) . to_char( ) , 'z' ) ;
508
- assert_eq ! ( 'a' . to_ascii( ) . to_upper( ) . to_char( ) , 'A' ) ;
509
- assert_eq ! ( 'z' . to_ascii( ) . to_upper( ) . to_char( ) , 'Z' ) ;
510
-
511
- assert_eq ! ( '@' . to_ascii( ) . to_lower( ) . to_char( ) , '@' ) ;
512
- assert_eq ! ( '[' . to_ascii( ) . to_lower( ) . to_char( ) , '[' ) ;
513
- assert_eq ! ( '`' . to_ascii( ) . to_upper( ) . to_char( ) , '`' ) ;
514
- assert_eq ! ( '{' . to_ascii( ) . to_upper( ) . to_char( ) , '{' ) ;
515
-
516
- assert ! ( '0' . to_ascii( ) . is_digit( ) ) ;
517
- assert ! ( '9' . to_ascii( ) . is_digit( ) ) ;
518
- assert ! ( !'/' . to_ascii( ) . is_digit( ) ) ;
519
- assert ! ( !':' . to_ascii( ) . is_digit( ) ) ;
520
-
521
- assert ! ( ( 0x1fu8 ) . to_ascii( ) . is_control( ) ) ;
522
- assert ! ( !' ' . to_ascii( ) . is_control( ) ) ;
523
- assert ! ( ( 0x7fu8 ) . to_ascii( ) . is_control( ) ) ;
487
+ assert_eq ! ( 65u8 . to_ascii( ) . unwrap ( ) . to_byte( ) , 65u8 ) ;
488
+ assert_eq ! ( 65u8 . to_ascii( ) . unwrap ( ) . to_char( ) , 'A' ) ;
489
+ assert_eq ! ( 'A' . to_ascii( ) . unwrap ( ) . to_char( ) , 'A' ) ;
490
+ assert_eq ! ( 'A' . to_ascii( ) . unwrap ( ) . to_byte( ) , 65u8 ) ;
491
+
492
+ assert_eq ! ( 'A' . to_ascii( ) . unwrap ( ) . to_lower( ) . to_char( ) , 'a' ) ;
493
+ assert_eq ! ( 'Z' . to_ascii( ) . unwrap ( ) . to_lower( ) . to_char( ) , 'z' ) ;
494
+ assert_eq ! ( 'a' . to_ascii( ) . unwrap ( ) . to_upper( ) . to_char( ) , 'A' ) ;
495
+ assert_eq ! ( 'z' . to_ascii( ) . unwrap ( ) . to_upper( ) . to_char( ) , 'Z' ) ;
496
+
497
+ assert_eq ! ( '@' . to_ascii( ) . unwrap ( ) . to_lower( ) . to_char( ) , '@' ) ;
498
+ assert_eq ! ( '[' . to_ascii( ) . unwrap ( ) . to_lower( ) . to_char( ) , '[' ) ;
499
+ assert_eq ! ( '`' . to_ascii( ) . unwrap ( ) . to_upper( ) . to_char( ) , '`' ) ;
500
+ assert_eq ! ( '{' . to_ascii( ) . unwrap ( ) . to_upper( ) . to_char( ) , '{' ) ;
501
+
502
+ assert ! ( '0' . to_ascii( ) . unwrap ( ) . is_digit( ) ) ;
503
+ assert ! ( '9' . to_ascii( ) . unwrap ( ) . is_digit( ) ) ;
504
+ assert ! ( !'/' . to_ascii( ) . unwrap ( ) . is_digit( ) ) ;
505
+ assert ! ( !':' . to_ascii( ) . unwrap ( ) . is_digit( ) ) ;
506
+
507
+ assert ! ( ( 0x1fu8 ) . to_ascii( ) . unwrap ( ) . is_control( ) ) ;
508
+ assert ! ( !' ' . to_ascii( ) . unwrap ( ) . is_control( ) ) ;
509
+ assert ! ( ( 0x7fu8 ) . to_ascii( ) . unwrap ( ) . is_control( ) ) ;
524
510
525
511
assert ! ( "banana" . chars( ) . all( |c| c. is_ascii( ) ) ) ;
526
512
assert ! ( !"ประเทศไทย中华Việt Nam" . chars( ) . all( |c| c. is_ascii( ) ) ) ;
@@ -529,21 +515,21 @@ mod tests {
529
515
#[ test]
530
516
fn test_ascii_vec ( ) {
531
517
let test = & [ 40u8 , 32u8 , 59u8 ] ;
532
- assert_eq ! ( test. to_ascii( ) , v2ascii!( [ 40 , 32 , 59 ] ) ) ;
533
- assert_eq ! ( "( ;" . to_ascii( ) , v2ascii!( [ 40 , 32 , 59 ] ) ) ;
518
+ assert_eq ! ( test. to_ascii( ) , Some ( v2ascii!( [ 40 , 32 , 59 ] ) ) ) ;
519
+ assert_eq ! ( "( ;" . to_ascii( ) , Some ( v2ascii!( [ 40 , 32 , 59 ] ) ) ) ;
534
520
// FIXME: #5475 borrowchk error, owned vectors do not live long enough
535
521
// if chained-from directly
536
- let v = ~[ 40u8 , 32u8 , 59u8 ] ; assert_eq ! ( v. to_ascii( ) , v2ascii!( [ 40 , 32 , 59 ] ) ) ;
537
- let v = ~"( ; "; assert_eq!(v.to_ascii(), v2ascii!([40, 32, 59]));
522
+ let v = ~[ 40u8 , 32u8 , 59u8 ] ; assert_eq ! ( v. to_ascii( ) , Some ( v2ascii!( [ 40 , 32 , 59 ] ) ) ) ;
523
+ let v = ~"( ; "; assert_eq!(v.to_ascii(), Some( v2ascii!([40, 32, 59]) ));
538
524
539
- assert_eq!(" abCDef& ?#". to_ascii ( ) . to_lower ( ) . into_str ( ) , ~"abcdef& ?#") ;
540
- assert_eq ! ( "abCDef&?#" . to_ascii( ) . to_upper( ) . into_str( ) , ~"ABCDEF & ?#");
525
+ assert_eq!(" abCDef& ?#". to_ascii ( ) . unwrap ( ) . to_lower ( ) . into_str ( ) , ~"abcdef& ?#") ;
526
+ assert_eq ! ( "abCDef&?#" . to_ascii( ) . unwrap ( ) . to_upper( ) . into_str( ) , ~"ABCDEF & ?#");
541
527
542
- assert_eq!(" ".to_ascii().to_lower().into_str(), ~" ");
543
- assert_eq!(" YMCA ".to_ascii().to_lower().into_str(), ~" ymca");
544
- assert_eq!(" abcDEFxyz: . ; ".to_ascii().to_upper().into_str(), ~" ABCDEFXYZ : . ; ");
528
+ assert_eq!(" ".to_ascii().unwrap(). to_lower().into_str(), ~" ");
529
+ assert_eq!(" YMCA ".to_ascii().unwrap(). to_lower().into_str(), ~" ymca");
530
+ assert_eq!(" abcDEFxyz: . ; ".to_ascii().unwrap(). to_upper().into_str(), ~" ABCDEFXYZ : . ; ");
545
531
546
- assert!(" aBcDeF& ?#".to_ascii().eq_ignore_case(" AbCdEf & ?#".to_ascii()));
532
+ assert!(" aBcDeF& ?#".to_ascii().unwrap(). eq_ignore_case(" AbCdEf & ?#".to_ascii().unwrap ()));
547
533
548
534
assert!(" ".is_ascii());
549
535
assert!(" a".is_ascii());
@@ -553,8 +539,8 @@ mod tests {
553
539
554
540
#[ test]
555
541
fn test_owned_ascii_vec ( ) {
556
- assert_eq!( ( ~"( ; ").into_ascii(), v2ascii!(~[40, 32, 59]));
557
- assert_eq!((~[40u8, 32u8, 59u8]).into_ascii(), v2ascii!(~[40, 32, 59]));
542
+ assert_eq!( ( ~"( ; ").into_ascii(), Some( v2ascii!(~[40, 32, 59]) ));
543
+ assert_eq!((~[40u8, 32u8, 59u8]).into_ascii(), Some( v2ascii!(~[40, 32, 59]) ));
558
544
}
559
545
560
546
#[test]
@@ -574,46 +560,46 @@ mod tests {
574
560
}
575
561
576
562
#[test] #[should_fail]
577
- fn test_ascii_vec_fail_u8_slice() { (&[127u8, 128u8, 255u8]).to_ascii(); }
563
+ fn test_ascii_vec_fail_u8_slice() { (&[127u8, 128u8, 255u8]).to_ascii().unwrap() ; }
578
564
579
565
#[test] #[should_fail]
580
- fn test_ascii_vec_fail_str_slice() { " zoä华".to_ascii(); }
566
+ fn test_ascii_vec_fail_str_slice() { " zoä华".to_ascii().unwrap() ; }
581
567
582
568
#[test] #[should_fail]
583
- fn test_ascii_fail_u8_slice() { 255u8.to_ascii(); }
569
+ fn test_ascii_fail_u8_slice() { 255u8.to_ascii().unwrap() ; }
584
570
585
571
#[test] #[should_fail]
586
- fn test_ascii_fail_char_slice() { 'λ'.to_ascii(); }
572
+ fn test_ascii_fail_char_slice() { 'λ'.to_ascii().unwrap() ; }
587
573
588
574
#[test]
589
575
fn test_opt() {
590
- assert_eq!(65u8.to_ascii_opt (), Some(Ascii { chr: 65u8 }));
591
- assert_eq!(255u8.to_ascii_opt (), None);
576
+ assert_eq!(65u8.to_ascii (), Some(Ascii { chr: 65u8 }));
577
+ assert_eq!(255u8.to_ascii (), None);
592
578
593
- assert_eq!('A'.to_ascii_opt (), Some(Ascii { chr: 65u8 }));
594
- assert_eq!('λ'.to_ascii_opt (), None);
579
+ assert_eq!('A'.to_ascii (), Some(Ascii { chr: 65u8 }));
580
+ assert_eq!('λ'.to_ascii (), None);
595
581
596
- assert_eq!(" zoä华".to_ascii_opt (), None);
582
+ assert_eq!(" zoä华".to_ascii (), None);
597
583
598
584
let test1 = &[127u8, 128u8, 255u8];
599
- assert_eq!((test1).to_ascii_opt (), None);
585
+ assert_eq!((test1).to_ascii (), None);
600
586
601
587
let v = [40u8, 32u8, 59u8];
602
588
let v2 = v2ascii!(&[40, 32, 59]);
603
- assert_eq!(v.to_ascii_opt (), Some(v2));
589
+ assert_eq!(v.to_ascii (), Some(v2));
604
590
let v = [127u8, 128u8, 255u8];
605
- assert_eq!(v.to_ascii_opt (), None);
591
+ assert_eq!(v.to_ascii (), None);
606
592
607
593
let v = " ( ; ";
608
594
let v2 = v2ascii!(&[40, 32, 59]);
609
- assert_eq!(v.to_ascii_opt (), Some(v2));
610
- assert_eq!(" zoä华".to_ascii_opt (), None);
595
+ assert_eq!(v.to_ascii (), Some(v2));
596
+ assert_eq!(" zoä华".to_ascii (), None);
611
597
612
- assert_eq!((~[40u8, 32u8, 59u8]).into_ascii_opt (), Some(v2ascii!(~[40, 32, 59])));
613
- assert_eq!((~[127u8, 128u8, 255u8]).into_ascii_opt (), None);
598
+ assert_eq!((~[40u8, 32u8, 59u8]).into_ascii (), Some(v2ascii!(~[40, 32, 59])));
599
+ assert_eq!((~[127u8, 128u8, 255u8]).into_ascii (), None);
614
600
615
- assert_eq!((~" ( ; ").into_ascii_opt (), Some(v2ascii!(~[40, 32, 59])));
616
- assert_eq!((~" zoä华").into_ascii_opt (), None);
601
+ assert_eq!((~" ( ; ").into_ascii (), Some(v2ascii!(~[40, 32, 59])));
602
+ assert_eq!((~" zoä华").into_ascii (), None);
617
603
}
618
604
619
605
#[test]
0 commit comments