@@ -2,30 +2,15 @@ use super::objsequence;
22use super :: pyobject:: { PyObjectKind , PyObjectRef , PyResult } ;
33use super :: vm:: VirtualMachine ;
44
5- fn get_slice_items ( value : & String , slice : & PyObjectRef ) -> String {
6- match & ( slice. borrow ( ) ) . kind {
7- PyObjectKind :: Slice { start, stop, step } => {
8- let start2: usize = match start {
9- // &Some(_) => panic!("Bad start index for string slicing {:?}", start),
10- & Some ( start) => objsequence:: get_pos ( value. len ( ) , start) ,
11- & None => 0 ,
12- } ;
13- let stop2: usize = match stop {
14- & Some ( stop) => objsequence:: get_pos ( value. len ( ) , stop) ,
15- // &Some(_) => panic!("Bad stop index for string slicing"),
16- & None => value. len ( ) as usize ,
17- } ;
18- match step {
19- & None | & Some ( 1 ) => value[ start2..stop2] . to_string ( ) ,
20- & Some ( num) => {
21- if num < 0 {
22- unimplemented ! ( "negative step indexing not yet supported" )
23- } ;
24- value[ start2..stop2] . chars ( ) . step_by ( num as usize ) . collect ( )
25- }
26- }
27- }
28- kind => panic ! ( "get_slice_items called with non-slice: {:?}" , kind) ,
5+ impl objsequence:: PySliceableSequence for String {
6+ fn do_slice ( & self , start : usize , stop : usize ) -> Self {
7+ self [ start..stop] . to_string ( )
8+ }
9+ fn do_stepped_slice ( & self , start : usize , stop : usize , step : usize ) -> Self {
10+ self [ start..stop] . chars ( ) . step_by ( step) . collect ( )
11+ }
12+ fn len ( & self ) -> usize {
13+ self . len ( )
2914 }
3015}
3116
@@ -40,7 +25,7 @@ pub fn subscript(vm: &mut VirtualMachine, value: &String, b: PyObjectRef) -> PyR
4025 start : _,
4126 stop : _,
4227 step : _,
43- } => Ok ( vm. new_str ( get_slice_items ( value, & b) ) ) ,
28+ } => Ok ( vm. new_str ( objsequence :: get_slice_items ( value, & b) ) ) ,
4429 _ => panic ! (
4530 "TypeError: indexing type {:?} with index {:?} is not supported (yet?)" ,
4631 value, b
0 commit comments