Implement stepped slicing for strings

This commit is contained in:
Daniel Watkins
2018-08-03 20:53:31 -04:00
parent 862442b635
commit 705c1c7dee

View File

@@ -33,12 +33,10 @@ pub fn subscript(vm: &mut VirtualMachine, value: &String, b: PyObjectRef) -> PyR
// &Some(_) => panic!("Bad stop index for string slicing"),
&None => value.len() as usize,
};
let step2: usize = match step {
//Some(step) => step as usize,
&None => 1 as usize,
_ => unimplemented!(),
};
Ok(vm.new_str(value[start2..stop2].to_string()))
Ok(vm.new_str(match step {
&None | &Some(1) => value[start2..stop2].to_string(),
&Some(num) => value[start2..stop2].chars().step_by(num as usize).collect(),
}))
}
_ => panic!(
"TypeError: indexing type {:?} with index {:?} is not supported (yet?)",