Skip to content

Commit 43ffbd1

Browse files
committed
ascii: Follow convention and return Option instead of failing
Use explicit unwrap() where needed. Basically rename: * to_ascii_opt -> to_ascii * into_ascii_opt -> into_ascii and get rid of the version which uses fail!
1 parent 6fe775e commit 43ffbd1

File tree

8 files changed

+76
-91
lines changed

8 files changed

+76
-91
lines changed

src/compiletest/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ fn parse_expected(line_num: uint, line: ~str) -> ~[ExpectedError] {
4949
while idx < len && line[idx] != (' ' as u8) { idx += 1u; }
5050

5151
let kind = line.slice(start_kind, idx);
52-
let kind = kind.to_ascii().to_lower().into_str();
52+
let kind = kind.to_ascii().unwrap().to_lower().into_str();
5353

5454
// Extract msg:
5555
while idx < len && line[idx] == (' ' as u8) { idx += 1u; }

src/compiletest/runtest.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -504,10 +504,9 @@ fn check_expected_errors(expected_errors: ~[errors::ExpectedError],
504504
fn to_lower( s : &str ) -> ~str {
505505
let i = s.chars();
506506
let c : ~[char] = i.map( |c| {
507-
if c.is_ascii() {
508-
c.to_ascii().to_lower().to_char()
509-
} else {
510-
c
507+
match c.to_ascii() {
508+
Some(ascii) => ascii.to_lower().to_char(),
509+
None => c
511510
}
512511
} ).collect();
513512
str::from_chars( c )

src/libglob/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -447,8 +447,8 @@ fn in_char_specifiers(specifiers: &[CharSpecifier], c: char, options: MatchOptio
447447
// FIXME: work with non-ascii chars properly (issue #1347)
448448
if !options.case_sensitive && c.is_ascii() && start.is_ascii() && end.is_ascii() {
449449

450-
let start = start.to_ascii().to_lower();
451-
let end = end.to_ascii().to_lower();
450+
let start = start.to_ascii().unwrap().to_lower();
451+
let end = end.to_ascii().unwrap().to_lower();
452452

453453
let start_up = start.to_upper();
454454
let end_up = end.to_upper();
@@ -458,7 +458,7 @@ fn in_char_specifiers(specifiers: &[CharSpecifier], c: char, options: MatchOptio
458458
if start != start_up && end != end_up {
459459
let start = start.to_char();
460460
let end = end.to_char();
461-
let c = c.to_ascii().to_lower().to_char();
461+
let c = c.to_ascii().unwrap().to_lower().to_char();
462462
if c >= start && c <= end {
463463
return true;
464464
}
@@ -481,7 +481,7 @@ fn chars_eq(a: char, b: char, case_sensitive: bool) -> bool {
481481
true
482482
} else if !case_sensitive && a.is_ascii() && b.is_ascii() {
483483
// FIXME: work with non-ascii chars properly (issue #1347)
484-
a.to_ascii().eq_ignore_case(b.to_ascii())
484+
a.to_ascii().unwrap().eq_ignore_case(b.to_ascii().unwrap())
485485
} else {
486486
a == b
487487
}

src/librustc/driver/driver.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ pub fn build_session_options(matches: &getopts::Matches)
754754
let level_name = lint::level_to_str(*level);
755755

756756
let level_short = level_name.slice_chars(0, 1);
757-
let level_short = level_short.to_ascii().to_upper().into_str();
757+
let level_short = level_short.to_ascii().unwrap().to_upper().into_str();
758758
let flags = vec::append(matches.opt_strs(level_short),
759759
matches.opt_strs(level_name));
760760
for lint_name in flags.iter() {

src/libstd/ascii.rs

Lines changed: 55 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -143,16 +143,9 @@ impl<'a> fmt::Show for Ascii {
143143

144144
/// Trait for converting into an ascii type.
145145
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-
153146
/// Convert to an ascii type, return None on non-ASCII input.
154147
#[inline]
155-
fn to_ascii_opt(&self) -> Option<T> {
148+
fn to_ascii(&self) -> Option<T> {
156149
if self.is_ascii() {
157150
Some(unsafe { self.to_ascii_nocheck() })
158151
} else {
@@ -223,16 +216,9 @@ pub trait OwnedAsciiCast {
223216
/// Check if convertible to ascii
224217
fn is_ascii(&self) -> bool;
225218

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-
233219
/// Take ownership and cast to an ascii vector. Return None on non-ASCII input.
234220
#[inline]
235-
fn into_ascii_opt(self) -> Option<~[Ascii]> {
221+
fn into_ascii(self) -> Option<~[Ascii]> {
236222
if self.is_ascii() {
237223
Some(unsafe { self.into_ascii_nocheck() })
238224
} else {
@@ -498,29 +484,29 @@ mod tests {
498484

499485
#[test]
500486
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());
524510

525511
assert!("banana".chars().all(|c| c.is_ascii()));
526512
assert!(!"ประเทศไทย中华Việt Nam".chars().all(|c| c.is_ascii()));
@@ -529,21 +515,21 @@ mod tests {
529515
#[test]
530516
fn test_ascii_vec() {
531517
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])));
534520
// FIXME: #5475 borrowchk error, owned vectors do not live long enough
535521
// 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])));
538524
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&?#");
541527
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:.;");
545531
546-
assert!("aBcDeF&?#".to_ascii().eq_ignore_case("AbCdEf&?#".to_ascii()));
532+
assert!("aBcDeF&?#".to_ascii().unwrap().eq_ignore_case("AbCdEf&?#".to_ascii().unwrap()));
547533
548534
assert!("".is_ascii());
549535
assert!("a".is_ascii());
@@ -553,8 +539,8 @@ mod tests {
553539

554540
#[test]
555541
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])));
558544
}
559545
560546
#[test]
@@ -574,46 +560,46 @@ mod tests {
574560
}
575561
576562
#[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(); }
578564
579565
#[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(); }
581567
582568
#[test] #[should_fail]
583-
fn test_ascii_fail_u8_slice() { 255u8.to_ascii(); }
569+
fn test_ascii_fail_u8_slice() { 255u8.to_ascii().unwrap(); }
584570
585571
#[test] #[should_fail]
586-
fn test_ascii_fail_char_slice() { 'λ'.to_ascii(); }
572+
fn test_ascii_fail_char_slice() { 'λ'.to_ascii().unwrap(); }
587573
588574
#[test]
589575
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);
592578
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);
595581
596-
assert_eq!("zoä华".to_ascii_opt(), None);
582+
assert_eq!("zoä华".to_ascii(), None);
597583
598584
let test1 = &[127u8, 128u8, 255u8];
599-
assert_eq!((test1).to_ascii_opt(), None);
585+
assert_eq!((test1).to_ascii(), None);
600586
601587
let v = [40u8, 32u8, 59u8];
602588
let v2 = v2ascii!(&[40, 32, 59]);
603-
assert_eq!(v.to_ascii_opt(), Some(v2));
589+
assert_eq!(v.to_ascii(), Some(v2));
604590
let v = [127u8, 128u8, 255u8];
605-
assert_eq!(v.to_ascii_opt(), None);
591+
assert_eq!(v.to_ascii(), None);
606592
607593
let v = "( ;";
608594
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);
611597
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);
614600
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);
617603
}
618604
619605
#[test]

src/libstd/path/windows.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,8 @@ impl GenericPathUnsafe for Path {
225225
fn shares_volume(me: &Path, path: &str) -> bool {
226226
// path is assumed to have a prefix of Some(DiskPrefix)
227227
match me.prefix {
228-
Some(DiskPrefix) => me.repr[0] == path[0].to_ascii().to_upper().to_byte(),
229-
Some(VerbatimDiskPrefix) => me.repr[4] == path[0].to_ascii().to_upper().to_byte(),
228+
Some(DiskPrefix) => me.repr[0] == path[0].to_ascii().unwrap().to_upper().to_byte(),
229+
Some(VerbatimDiskPrefix) => me.repr[4] == path[0].to_ascii().unwrap().to_upper().to_byte(),
230230
_ => false
231231
}
232232
}
@@ -655,14 +655,14 @@ impl Path {
655655
match (self.prefix, other.prefix) {
656656
(Some(DiskPrefix), Some(VerbatimDiskPrefix)) => {
657657
self.is_absolute() &&
658-
self.repr[0].to_ascii().eq_ignore_case(other.repr[4].to_ascii())
658+
self.repr[0].to_ascii().unwrap().eq_ignore_case(other.repr[4].to_ascii().unwrap())
659659
}
660660
(Some(VerbatimDiskPrefix), Some(DiskPrefix)) => {
661661
other.is_absolute() &&
662-
self.repr[4].to_ascii().eq_ignore_case(other.repr[0].to_ascii())
662+
self.repr[4].to_ascii().unwrap().eq_ignore_case(other.repr[0].to_ascii().unwrap())
663663
}
664664
(Some(VerbatimDiskPrefix), Some(VerbatimDiskPrefix)) => {
665-
self.repr[4].to_ascii().eq_ignore_case(other.repr[4].to_ascii())
665+
self.repr[4].to_ascii().unwrap().eq_ignore_case(other.repr[4].to_ascii().unwrap())
666666
}
667667
(Some(UNCPrefix(_,_)), Some(VerbatimUNCPrefix(_,_))) => {
668668
self.repr.slice(2, self.prefix_len()) == other.repr.slice(8, other.prefix_len())
@@ -729,7 +729,7 @@ impl Path {
729729
let mut s = s.slice_to(len).to_owned();
730730
unsafe {
731731
str::raw::as_owned_vec(&mut s)[0] =
732-
s[0].to_ascii().to_upper().to_byte();
732+
s[0].to_ascii().unwrap().to_upper().to_byte();
733733
}
734734
if is_abs {
735735
// normalize C:/ to C:\
@@ -744,7 +744,7 @@ impl Path {
744744
let mut s = s.slice_to(len).to_owned();
745745
unsafe {
746746
str::raw::as_owned_vec(&mut s)[4] =
747-
s[4].to_ascii().to_upper().to_byte();
747+
s[4].to_ascii().unwrap().to_upper().to_byte();
748748
}
749749
Some(s)
750750
}
@@ -765,12 +765,12 @@ impl Path {
765765
let mut s = str::with_capacity(n);
766766
match prefix {
767767
Some(DiskPrefix) => {
768-
s.push_char(prefix_[0].to_ascii().to_upper().to_char());
768+
s.push_char(prefix_[0].to_ascii().unwrap().to_upper().to_char());
769769
s.push_char(':');
770770
}
771771
Some(VerbatimDiskPrefix) => {
772772
s.push_str(prefix_.slice_to(4));
773-
s.push_char(prefix_[4].to_ascii().to_upper().to_char());
773+
s.push_char(prefix_[4].to_ascii().unwrap().to_upper().to_char());
774774
s.push_str(prefix_.slice_from(5));
775775
}
776776
Some(UNCPrefix(a,b)) => {

src/libterm/terminfo/parm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ fn format(val: Param, op: FormatOp, flags: Flags) -> Result<~[u8],~str> {
531531
}
532532
}
533533
FormatHEX => {
534-
s = s.into_ascii().to_upper().into_bytes();
534+
s = s.into_ascii().unwrap().to_upper().into_bytes();
535535
if flags.alternate {
536536
let s_ = replace(&mut s, ~['0' as u8, 'X' as u8]);
537537
s.push_all_move(s_);

src/test/bench/shootout-k-nucleotide-pipes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ fn sort_and_fmt(mm: &HashMap<~[u8], uint>, total: uint) -> ~str {
6868
for &(ref k, v) in pairs_sorted.iter() {
6969
unsafe {
7070
buffer.push_str(format!("{} {:0.3f}\n",
71-
k.to_ascii().to_upper().into_str(), v));
71+
k.to_ascii().unwrap().to_upper().into_str(), v));
7272
}
7373
}
7474

@@ -77,7 +77,7 @@ fn sort_and_fmt(mm: &HashMap<~[u8], uint>, total: uint) -> ~str {
7777

7878
// given a map, search for the frequency of a pattern
7979
fn find(mm: &HashMap<~[u8], uint>, key: ~str) -> uint {
80-
let key = key.into_ascii().to_lower().into_str();
80+
let key = key.into_ascii().unwrap().to_lower().into_str();
8181
match mm.find_equiv(&key.as_bytes()) {
8282
option::None => { return 0u; }
8383
option::Some(&num) => { return num; }

0 commit comments

Comments
 (0)