Skip to content

Commit 07336fa

Browse files
committed
initial fork commit
1 parent 95092be commit 07336fa

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

vm/src/obj/objstr.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ pub fn init(context: &PyContext) {
3131
str_type.set_attr("rstrip", context.new_rustfunc(str_rstrip));
3232
str_type.set_attr("endswith", context.new_rustfunc(str_endswith));
3333
str_type.set_attr("startswith", context.new_rustfunc(str_startswith));
34+
str_type.set_attr("title", context.new_rustfunc(str_title));
35+
36+
// str_type.set_attr("center", context.new_rustfunc(str_center));
3437
}
3538

3639
pub fn get_value(obj: &PyObjectRef) -> String {
@@ -226,6 +229,39 @@ fn str_endswith(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
226229
Ok(vm.ctx.new_bool(value.ends_with(pat.as_str())))
227230
}
228231

232+
fn str_title(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
233+
arg_check!(vm, args, required = [(s, Some(vm.ctx.str_type()))]);
234+
let value = get_value(&s);
235+
let titled_str = value
236+
.split(' ')
237+
.map(|w| {
238+
let mut c = w.chars();
239+
match c.next() {
240+
None => String::new(),
241+
Some(f) => f
242+
.to_uppercase()
243+
.chain(c.flat_map(|t| t.to_lowercase()))
244+
.collect(),
245+
}
246+
})
247+
.collect::<Vec<_>>()
248+
.join(" ");
249+
Ok(vm.ctx.new_str(titled_str))
250+
}
251+
252+
// fn str_center(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
253+
// arg_check!(
254+
// vm,
255+
// args,
256+
// required = [(s, Some(vm.ctx.str_type())), (len, Some(vm.ctx.int_type()))],
257+
// optional = [(chars, None)]
258+
// );
259+
// let value = get_value(&s);
260+
// let len = get_value(&len).parse::<usize>();
261+
// let chars = args.get_kwargs
262+
// Ok(vm.ctx.new_str(value))
263+
// }
264+
229265
fn str_startswith(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
230266
arg_check!(
231267
vm,

0 commit comments

Comments
 (0)