Skip to content

Commit d650476

Browse files
committed
Add additional methods to string objects
1 parent 2d7b0e0 commit d650476

2 files changed

Lines changed: 91 additions & 0 deletions

File tree

tests/snippets/strings.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,17 @@
2222
assert repr('\n\t') == "'\\n\\t'"
2323

2424
assert str(["a", "b", "can't"]) == "['a', 'b', \"can't\"]"
25+
26+
a = 'Hallo'
27+
assert a.lower() == 'hallo'
28+
assert a.upper() == 'HALLO'
29+
assert a.split('al') == ['H', 'lo']
30+
assert a.startswith('H')
31+
assert not a.startswith('f')
32+
assert a.endswith('llo')
33+
assert not a.endswith('on')
34+
35+
b = ' hallo '
36+
assert b.strip() == 'hallo'
37+
assert b.lstrip() == 'hallo '
38+
assert b.rstrip() == ' hallo'

vm/src/obj/objstr.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ pub fn init(context: &PyContext) {
1515
str_type.set_attr("__new__", context.new_rustfunc(str_new));
1616
str_type.set_attr("__str__", context.new_rustfunc(str_str));
1717
str_type.set_attr("__repr__", context.new_rustfunc(str_repr));
18+
str_type.set_attr("lower", context.new_rustfunc(str_lower));
19+
str_type.set_attr("upper", context.new_rustfunc(str_upper));
20+
str_type.set_attr("split", context.new_rustfunc(str_split));
21+
str_type.set_attr("strip", context.new_rustfunc(str_strip));
22+
str_type.set_attr("lstrip", context.new_rustfunc(str_lstrip));
23+
str_type.set_attr("rstrip", context.new_rustfunc(str_rstrip));
24+
str_type.set_attr("endswith", context.new_rustfunc(str_endswith));
25+
str_type.set_attr("startswith", context.new_rustfunc(str_startswith));
1826
}
1927

2028
pub fn get_value(obj: &PyObjectRef) -> String {
@@ -120,6 +128,75 @@ fn str_mul(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
120128
}
121129
}
122130

131+
fn str_upper(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
132+
arg_check!(vm, args, required = [(s, Some(vm.ctx.str_type()))]);
133+
let value = get_value(&s).to_uppercase();
134+
Ok(vm.ctx.new_str(value))
135+
}
136+
137+
fn str_lower(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
138+
arg_check!(vm, args, required = [(s, Some(vm.ctx.str_type()))]);
139+
let value = get_value(&s).to_lowercase();
140+
Ok(vm.ctx.new_str(value))
141+
}
142+
143+
fn str_split(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
144+
arg_check!(
145+
vm,
146+
args,
147+
required = [(s, Some(vm.ctx.str_type())), (pat, Some(vm.ctx.str_type()))]
148+
);
149+
let value = get_value(&s);
150+
// if some
151+
let pat = get_value(&pat);
152+
let str_pat = pat.as_str();
153+
let elements = value
154+
.split(str_pat)
155+
.map(|o| vm.ctx.new_str(o.to_string()))
156+
.collect();
157+
Ok(vm.ctx.new_list(elements))
158+
}
159+
160+
fn str_strip(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
161+
arg_check!(vm, args, required = [(s, Some(vm.ctx.str_type()))]);
162+
let value = get_value(&s).trim().to_string();
163+
Ok(vm.ctx.new_str(value))
164+
}
165+
166+
fn str_lstrip(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
167+
arg_check!(vm, args, required = [(s, Some(vm.ctx.str_type()))]);
168+
let value = get_value(&s).trim_left().to_string();
169+
Ok(vm.ctx.new_str(value))
170+
}
171+
172+
fn str_rstrip(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
173+
arg_check!(vm, args, required = [(s, Some(vm.ctx.str_type()))]);
174+
let value = get_value(&s).trim_right().to_string();
175+
Ok(vm.ctx.new_str(value))
176+
}
177+
178+
fn str_endswith(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
179+
arg_check!(
180+
vm,
181+
args,
182+
required = [(s, Some(vm.ctx.str_type())), (pat, Some(vm.ctx.str_type()))]
183+
);
184+
let value = get_value(&s);
185+
let pat = get_value(&pat);
186+
Ok(vm.ctx.new_bool(value.ends_with(pat.as_str())))
187+
}
188+
189+
fn str_startswith(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
190+
arg_check!(
191+
vm,
192+
args,
193+
required = [(s, Some(vm.ctx.str_type())), (pat, Some(vm.ctx.str_type()))]
194+
);
195+
let value = get_value(&s);
196+
let pat = get_value(&pat);
197+
Ok(vm.ctx.new_bool(value.starts_with(pat.as_str())))
198+
}
199+
123200
// TODO: should with following format
124201
// class str(object='')
125202
// class str(object=b'', encoding='utf-8', errors='strict')

0 commit comments

Comments
 (0)