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

Commit 9995797

Browse files
author
Shing Lyu
committed
Added string len
1 parent e1a519c commit 9995797

5 files changed

Lines changed: 18 additions & 0 deletions

File tree

RustPython/src/builtins.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ pub fn len(args: Vec<NativeType>) -> NativeType {
3838
let len = match &args[0] {
3939
&NativeType::List(ref l) => l.len(),
4040
&NativeType::Tuple(ref t) => t.len(),
41+
&NativeType::Str(ref s) => s.len(),
4142
_ => panic!("TypeError: object of this type has no len()")
4243
};
4344
NativeType::Int(len as i32)

tests/3.1.2.16.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
word = "Python"
2+
assert "on" == word[4:42]
3+
assert "" == word[42:]

tests/3.1.2.17.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
word = "Python"
2+
3+
word[0] = "J" # Should raise a error, immutable
4+
word[2:] = "Jy" # Should raise a error, immutable
5+
6+
7+

tests/3.1.2.18.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
word = "Python"
2+
3+
assert "Jython" == "J" + word[1:]
4+
assert "Pypy" == word[:2] + "py"

tests/3.1.2.19.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
s = "abcdefg"
2+
3+
assert 7 == len(s)

0 commit comments

Comments
 (0)