From 46b939721fd82edb2aa9d71f9f3faeea86856a6d Mon Sep 17 00:00:00 2001 From: ZapAnton Date: Sun, 3 Feb 2019 01:51:47 +0300 Subject: [PATCH] str: Added the isdecimal method --- vm/src/obj/objstr.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/vm/src/obj/objstr.rs b/vm/src/obj/objstr.rs index 707ce42ba..3887a3f33 100644 --- a/vm/src/obj/objstr.rs +++ b/vm/src/obj/objstr.rs @@ -53,6 +53,7 @@ pub fn init(context: &PyContext) { context.set_attr(&str_type, "isalnum", context.new_rustfunc(str_isalnum)); context.set_attr(&str_type, "isnumeric", context.new_rustfunc(str_isnumeric)); context.set_attr(&str_type, "isdigit", context.new_rustfunc(str_isdigit)); + context.set_attr(&str_type, "isdecimal", context.new_rustfunc(str_isdecimal)); context.set_attr(&str_type, "title", context.new_rustfunc(str_title)); context.set_attr(&str_type, "swapcase", context.new_rustfunc(str_swapcase)); context.set_attr(&str_type, "isalpha", context.new_rustfunc(str_isalpha)); @@ -938,6 +939,20 @@ fn str_isdigit(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { Ok(vm.ctx.new_bool(is_digit)) } +fn str_isdecimal(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { + arg_check!(vm, args, required = [(s, Some(vm.ctx.str_type()))]); + + let value = get_value(&s); + + let is_decimal = if !value.is_empty() { + value.chars().all(|c| c.is_ascii_digit()) + } else { + false + }; + + Ok(vm.ctx.new_bool(is_decimal)) +} + fn str_getitem(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { arg_check!( vm,