auto merge of #15085 : brson/rust/stridx, r=alexcrichton
Being able to index into the bytes of a string encourages poor UTF-8 hygiene. To get a view of `&[u8]` from either a `String` or `&str` slice, use the `as_bytes()` method. Closes #12710. [breaking-change] If the diffstat is any indication this shouldn't have a huge impact but it will have some. Most changes in the `str` and `path` module. A lot of the existing usages were in tests where ascii is expected. There are a number of other legit uses where the characters are known to be ascii.
This commit is contained in:
commit
89259b34c0
26 changed files with 101 additions and 87 deletions
|
|
@ -183,7 +183,7 @@ fn main() {
|
|||
|
||||
if line.len() == 0u { continue; }
|
||||
|
||||
match (line.as_slice()[0] as char, proc_mode) {
|
||||
match (line.as_bytes()[0] as char, proc_mode) {
|
||||
|
||||
// start processing if this is the one
|
||||
('>', false) => {
|
||||
|
|
|
|||
|
|
@ -17,10 +17,10 @@ pub fn main() {
|
|||
assert_eq!(v.as_slice()[3u32], 3); //~ ERROR: mismatched types
|
||||
assert_eq!(v.as_slice()[3i32], 3); //~ ERROR: mismatched types
|
||||
println!("{}", v.as_slice()[3u8]); //~ ERROR: mismatched types
|
||||
assert_eq!(s.as_slice()[3u], 'd' as u8);
|
||||
assert_eq!(s.as_slice()[3u8], 'd' as u8); //~ ERROR: mismatched types
|
||||
assert_eq!(s.as_slice()[3i8], 'd' as u8); //~ ERROR: mismatched types
|
||||
assert_eq!(s.as_slice()[3u32], 'd' as u8); //~ ERROR: mismatched types
|
||||
assert_eq!(s.as_slice()[3i32], 'd' as u8); //~ ERROR: mismatched types
|
||||
println!("{}", s.as_slice()[3u8]); //~ ERROR: mismatched types
|
||||
assert_eq!(s.as_bytes()[3u], 'd' as u8);
|
||||
assert_eq!(s.as_bytes()[3u8], 'd' as u8); //~ ERROR: mismatched types
|
||||
assert_eq!(s.as_bytes()[3i8], 'd' as u8); //~ ERROR: mismatched types
|
||||
assert_eq!(s.as_bytes()[3u32], 'd' as u8); //~ ERROR: mismatched types
|
||||
assert_eq!(s.as_bytes()[3i32], 'd' as u8); //~ ERROR: mismatched types
|
||||
println!("{}", s.as_bytes()[3u8]); //~ ERROR: mismatched types
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,8 +11,6 @@
|
|||
extern crate debug;
|
||||
|
||||
pub fn main() {
|
||||
let s = "hello".to_string();
|
||||
let c: u8 = s.as_slice()[4];
|
||||
println!("{:?}", c);
|
||||
assert_eq!(c, 0x6f as u8);
|
||||
let s: &str = "hello";
|
||||
let c: u8 = s[4]; //~ ERROR cannot index a value of type `&str`
|
||||
}
|
||||
|
|
@ -14,5 +14,5 @@ fn main() {
|
|||
let s: String = "hello".to_string();
|
||||
|
||||
// Bounds-check failure.
|
||||
assert_eq!(s.as_slice()[5], 0x0 as u8);
|
||||
assert_eq!(s.as_bytes()[5], 0x0 as u8);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,8 +17,8 @@ pub fn main() {
|
|||
println!("{}", x);
|
||||
println!("{}", y);
|
||||
|
||||
assert_eq!(x[0], 'h' as u8);
|
||||
assert_eq!(x[4], 'o' as u8);
|
||||
assert_eq!(x.as_bytes()[0], 'h' as u8);
|
||||
assert_eq!(x.as_bytes()[4], 'o' as u8);
|
||||
|
||||
let z : &str = "thing";
|
||||
assert_eq!(v, x);
|
||||
|
|
|
|||
|
|
@ -15,6 +15,6 @@ pub fn main() {
|
|||
let _y : String = "there".to_string();
|
||||
let mut z = "thing".to_string();
|
||||
z = x;
|
||||
assert_eq!(z.as_slice()[0], ('h' as u8));
|
||||
assert_eq!(z.as_slice()[4], ('o' as u8));
|
||||
assert_eq!(z.as_bytes()[0], ('h' as u8));
|
||||
assert_eq!(z.as_bytes()[4], ('o' as u8));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,5 +16,5 @@ pub fn main() {
|
|||
let b: String = "world".to_string();
|
||||
let s: String = format!("{}{}", a, b);
|
||||
println!("{}", s.clone());
|
||||
assert_eq!(s.as_slice()[9], 'd' as u8);
|
||||
assert_eq!(s.as_bytes()[9], 'd' as u8);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -39,10 +39,10 @@ fn test_str() {
|
|||
let s0 = "test".to_string();
|
||||
tx.send(s0);
|
||||
let s1 = rx.recv();
|
||||
assert_eq!(s1.as_slice()[0], 't' as u8);
|
||||
assert_eq!(s1.as_slice()[1], 'e' as u8);
|
||||
assert_eq!(s1.as_slice()[2], 's' as u8);
|
||||
assert_eq!(s1.as_slice()[3], 't' as u8);
|
||||
assert_eq!(s1.as_bytes()[0], 't' as u8);
|
||||
assert_eq!(s1.as_bytes()[1], 'e' as u8);
|
||||
assert_eq!(s1.as_bytes()[2], 's' as u8);
|
||||
assert_eq!(s1.as_bytes()[3], 't' as u8);
|
||||
}
|
||||
|
||||
#[deriving(Show)]
|
||||
|
|
|
|||
|
|
@ -99,7 +99,7 @@ fn check_legs(arc: Arc<Vec<Box<Pet+Share+Send>>>) {
|
|||
fn check_names(arc: Arc<Vec<Box<Pet+Share+Send>>>) {
|
||||
for pet in arc.iter() {
|
||||
pet.name(|name| {
|
||||
assert!(name[0] == 'a' as u8 && name[1] == 'l' as u8);
|
||||
assert!(name.as_bytes()[0] == 'a' as u8 && name.as_bytes()[1] == 'l' as u8);
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ pub fn main() {
|
|||
for ab in a.as_slice().bytes() {
|
||||
println!("{}", i);
|
||||
println!("{}", ab);
|
||||
let bb: u8 = b.as_slice()[i as uint];
|
||||
let bb: u8 = b.as_bytes()[i as uint];
|
||||
println!("{}", bb);
|
||||
assert_eq!(ab, bb);
|
||||
i += 1;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue