1
- // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1
+ // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2
2
// file at the top-level directory of this distribution and at
3
3
// http://rust-lang.org/COPYRIGHT.
4
4
//
@@ -789,16 +789,18 @@ pub fn each_split_within<'a>(ss: &'a str,
789
789
790
790
/// Convert a string to lowercase. ASCII only
791
791
pub fn to_lower ( s : & str ) -> ~str {
792
- map ( s,
793
- |c| unsafe { ( libc:: tolower ( c as libc:: c_char ) ) as char }
794
- )
792
+ do map ( s) |c| {
793
+ assert ! ( char :: is_ascii( c) ) ;
794
+ ( unsafe { libc:: tolower ( c as libc:: c_char ) } ) as char
795
+ }
795
796
}
796
797
797
798
/// Convert a string to uppercase. ASCII only
798
799
pub fn to_upper ( s : & str ) -> ~str {
799
- map ( s,
800
- |c| unsafe { ( libc:: toupper ( c as libc:: c_char ) ) as char }
801
- )
800
+ do map ( s) |c| {
801
+ assert ! ( char :: is_ascii( c) ) ;
802
+ ( unsafe { libc:: toupper ( c as libc:: c_char ) } ) as char
803
+ }
802
804
}
803
805
804
806
/**
@@ -2317,20 +2319,20 @@ pub mod raw {
2317
2319
}
2318
2320
2319
2321
/// Removes the last byte from a string and returns it. (Not UTF-8 safe).
2320
- pub fn pop_byte ( s : & mut ~str ) -> u8 {
2322
+ pub unsafe fn pop_byte ( s : & mut ~str ) -> u8 {
2321
2323
let len = len ( * s) ;
2322
2324
assert ! ( ( len > 0 u) ) ;
2323
2325
let b = s[ len - 1 u] ;
2324
- unsafe { set_len ( s, len - 1 u) } ;
2326
+ set_len ( s, len - 1 u) ;
2325
2327
return b;
2326
2328
}
2327
2329
2328
2330
/// Removes the first byte from a string and returns it. (Not UTF-8 safe).
2329
- pub fn shift_byte ( s : & mut ~str ) -> u8 {
2331
+ pub unsafe fn shift_byte ( s : & mut ~str ) -> u8 {
2330
2332
let len = len ( * s) ;
2331
2333
assert ! ( ( len > 0 u) ) ;
2332
2334
let b = s[ 0 ] ;
2333
- * s = unsafe { raw:: slice_bytes_owned ( * s, 1 u, len) } ;
2335
+ * s = raw:: slice_bytes_owned ( * s, 1 u, len) ;
2334
2336
return b;
2335
2337
}
2336
2338
@@ -3096,12 +3098,11 @@ mod tests {
3096
3098
3097
3099
#[test]
3098
3100
fn test_to_lower() {
3099
- unsafe {
3100
- assert!(~" " == map(~" ",
3101
- |c| libc::tolower(c as c_char) as char));
3102
- assert!(~" ymca" == map(~" YMCA ",
3103
- |c| libc::tolower(c as c_char) as char));
3104
- }
3101
+ // libc::tolower, and hence str::to_lower
3102
+ // are culturally insensitive: they only work for ASCII
3103
+ // (see Issue #1347)
3104
+ assert!(~" " == to_lower(" "));
3105
+ assert!(~" ymca" == to_lower(" YMCA "));
3105
3106
}
3106
3107
3107
3108
#[test]
@@ -3346,15 +3347,15 @@ mod tests {
3346
3347
#[ test]
3347
3348
fn test_shift_byte( ) {
3348
3349
let mut s = ~"ABC ";
3349
- let b = raw::shift_byte(&mut s);
3350
+ let b = unsafe{ raw::shift_byte(&mut s)} ;
3350
3351
assert!((s == ~" BC "));
3351
3352
assert!((b == 65u8));
3352
3353
}
3353
3354
3354
3355
#[test]
3355
3356
fn test_pop_byte() {
3356
3357
let mut s = ~" ABC ";
3357
- let b = raw::pop_byte(&mut s);
3358
+ let b = unsafe{ raw::pop_byte(&mut s)} ;
3358
3359
assert!((s == ~" AB "));
3359
3360
assert!((b == 67u8));
3360
3361
}
@@ -3666,12 +3667,8 @@ mod tests {
3666
3667
3667
3668
#[ test]
3668
3669
fn test_map( ) {
3669
- unsafe {
3670
- assert!( ~"" == map( ~"", |c|
3671
- libc:: toupper( c as c_char) as char ) ) ;
3672
- assert!( ~"YMCA " == map(~" ymca",
3673
- |c| libc::toupper(c as c_char) as char));
3674
- }
3670
+ assert!( ~"" == map( ~"", |c| unsafe { libc:: toupper( c as c_char) } as char ) ) ;
3671
+ assert!( ~"YMCA " == map(~" ymca", |c| unsafe {libc::toupper(c as c_char)} as char));
3675
3672
}
3676
3673
3677
3674
#[test]
@@ -3685,11 +3682,11 @@ mod tests {
3685
3682
3686
3683
#[test]
3687
3684
fn test_any() {
3688
- assert!(false == any(~" ", char::is_uppercase));
3685
+ assert!(false == any(~" ", char::is_uppercase));
3689
3686
assert!(false == any(~" ymca", char::is_uppercase));
3690
3687
assert!(true == any(~" YMCA ", char::is_uppercase));
3691
- assert!(true == any(~" yMCA", char::is_uppercase));
3692
- assert!(true == any(~" Ymcy ", char::is_uppercase));
3688
+ assert!(true == any(~" yMCA", char::is_uppercase));
3689
+ assert!(true == any(~" Ymcy ", char::is_uppercase));
3693
3690
}
3694
3691
3695
3692
#[test]
0 commit comments