Skip to content
This repository was archived by the owner on May 5, 2023. It is now read-only.

Commit e1a519c

Browse files
author
Shing Lyu
committed
More string slicing
1 parent 19c6c23 commit e1a519c

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

RustPython/src/main.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -554,8 +554,21 @@ impl VirtualMachine {
554554
let idx = (index + s.len() as i32) % s.len() as i32;
555555
curr_frame.stack.push(NativeType::Str(s.chars().nth(idx as usize).unwrap().to_string()));
556556
},
557-
(&NativeType::Str(ref s), &NativeType::Slice(Some(ref start), Some(ref stop), None)) => {
558-
curr_frame.stack.push(NativeType::Str(s[*start as usize..*stop as usize].to_string()));
557+
(&NativeType::Str(ref s), &NativeType::Slice(ref opt_start, ref opt_stop, ref opt_step)) => {
558+
let start = match opt_start {
559+
&Some(start) => ((start + s.len() as i32) % s.len() as i32) as usize,
560+
&None => 0,
561+
};
562+
let stop = match opt_stop {
563+
&Some(stop) => ((stop + s.len() as i32) % s.len() as i32) as usize,
564+
&None => s.len() as usize,
565+
};
566+
let step = match opt_step {
567+
//Some(step) => step as usize,
568+
&None => 1 as usize,
569+
_ => unimplemented!(),
570+
};
571+
curr_frame.stack.push(NativeType::Str(s[start..stop].to_string()));
559572
},
560573
// TODO: implement other Slice possibilities
561574
_ => panic!("TypeError: indexing type {:?} with index {:?} is not supported (yet?)", tos1, tos)

tests/3.1.2.13.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
word = "Python"
2+
3+
assert "Python" == word[:2] + word[2:]
4+
assert "Python" == word[:4] + word[4:]
5+
6+
assert "Py" == word[:2]
7+
assert "on" == word[4:]
8+
assert "on" == word[-2:]
9+
assert "Py" == word[:-4]
10+
# assert "Py" == word[::2]

0 commit comments

Comments
 (0)