From fad493c79f4cf3bf8abcfb4428c5303e77fd1091 Mon Sep 17 00:00:00 2001 From: Akshay Verma Date: Thu, 18 Oct 2018 16:14:35 +0200 Subject: [PATCH 1/3] Added capitalize method to string --- vm/src/obj/objstr.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/vm/src/obj/objstr.rs b/vm/src/obj/objstr.rs index 009a0d9c5..b3ecde4ff 100644 --- a/vm/src/obj/objstr.rs +++ b/vm/src/obj/objstr.rs @@ -18,6 +18,7 @@ pub fn init(context: &PyContext) { str_type.set_attr("__repr__", context.new_rustfunc(str_repr)); str_type.set_attr("lower", context.new_rustfunc(str_lower)); str_type.set_attr("upper", context.new_rustfunc(str_upper)); + str_type.set_attr("capitalize", context.new_rustfunc(str_capitalize)); str_type.set_attr("split", context.new_rustfunc(str_split)); str_type.set_attr("strip", context.new_rustfunc(str_strip)); str_type.set_attr("lstrip", context.new_rustfunc(str_lstrip)); @@ -141,6 +142,14 @@ fn str_lower(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { Ok(vm.ctx.new_str(value)) } +fn str_capitalize(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { + arg_check!(vm, args, required = [(s, Some(vm.ctx.str_type()))]); + let value = get_value(&s); + let (first_part, lower_str) = value.split_at(1); + let capitalized = format!("{}{}", first_part.to_uppercase().to_string(), lower_str); + Ok(vm.ctx.new_str(capitalized)) +} + fn str_split(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { arg_check!( vm, From 8c04cb5dc305ef6b81ba2ef6e30330a0c051364d Mon Sep 17 00:00:00 2001 From: Akshay Verma Date: Thu, 18 Oct 2018 16:25:27 +0200 Subject: [PATCH 2/3] Added tests for capitalize method to string --- tests/snippets/strings.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/snippets/strings.py b/tests/snippets/strings.py index 3ebec9b3d..024b7b5a9 100644 --- a/tests/snippets/strings.py +++ b/tests/snippets/strings.py @@ -36,3 +36,6 @@ b = ' hallo ' assert b.strip() == 'hallo' assert b.lstrip() == 'hallo ' assert b.rstrip() == ' hallo' + +c = 'hallo' +assert b.capitalize() == 'Hallo' From 4da6c94602d5080448a439c2a9885591ab9b88dc Mon Sep 17 00:00:00 2001 From: Akshay Verma Date: Thu, 18 Oct 2018 16:46:43 +0200 Subject: [PATCH 3/3] Added test for capitalize method to string --- tests/snippets/strings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/snippets/strings.py b/tests/snippets/strings.py index 024b7b5a9..77ebabaf0 100644 --- a/tests/snippets/strings.py +++ b/tests/snippets/strings.py @@ -38,4 +38,4 @@ assert b.lstrip() == 'hallo ' assert b.rstrip() == ' hallo' c = 'hallo' -assert b.capitalize() == 'Hallo' +assert c.capitalize() == 'Hallo'