Skip to content

Commit dd2b4e3

Browse files
committed
added isalnum, isalpha, isdigit, swapcase
1 parent 07336fa commit dd2b4e3

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

vm/src/obj/objstr.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ use num_bigint::ToBigInt;
99
use num_traits::ToPrimitive;
1010
use std::hash::{Hash, Hasher};
1111

12+
// functions like isdigit also checks if exponents are digits
13+
// exponents from 0-9
14+
const VALID_UNICODES: &'static [&'static u16; 10] = &[
15+
&0x2070, &0x00B9, &0x00B2, &0x00B3, &0x2074, &0x2075, &0x2076, &0x2077, &0x2078, &0x2079,
16+
];
17+
1218
pub fn init(context: &PyContext) {
1319
let ref str_type = context.str_type;
1420
str_type.set_attr("__add__", context.new_rustfunc(str_add));
@@ -32,6 +38,10 @@ pub fn init(context: &PyContext) {
3238
str_type.set_attr("endswith", context.new_rustfunc(str_endswith));
3339
str_type.set_attr("startswith", context.new_rustfunc(str_startswith));
3440
str_type.set_attr("title", context.new_rustfunc(str_title));
41+
str_type.set_attr("swapcase", context.new_rustfunc(str_swapcase));
42+
str_type.set_attr("isalnum", context.new_rustfunc(str_isalnum));
43+
str_type.set_attr("isalpha", context.new_rustfunc(str_isalpha));
44+
str_type.set_attr("isdigit", context.new_rustfunc(str_isdigit));
3545

3646
// str_type.set_attr("center", context.new_rustfunc(str_center));
3747
}
@@ -229,6 +239,22 @@ fn str_endswith(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
229239
Ok(vm.ctx.new_bool(value.ends_with(pat.as_str())))
230240
}
231241

242+
fn str_swapcase(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
243+
arg_check!(vm, args, required = [(s, Some(vm.ctx.str_type()))]);
244+
let value = get_value(&s);
245+
let mut swapped_str = String::with_capacity(value.len());
246+
for c in value.chars() {
247+
if c.is_lowercase() {
248+
swapped_str.push(c.to_ascii_uppercase());
249+
} else if c.is_uppercase() {
250+
swapped_str.push(c.to_ascii_lowercase());
251+
} else {
252+
swapped_str.push(c);
253+
}
254+
}
255+
Ok(vm.ctx.new_str(swapped_str))
256+
}
257+
232258
fn str_title(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
233259
arg_check!(vm, args, required = [(s, Some(vm.ctx.str_type()))]);
234260
let value = get_value(&s);
@@ -287,6 +313,51 @@ fn str_contains(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
287313
Ok(vm.ctx.new_bool(value.contains(needle.as_str())))
288314
}
289315

316+
fn str_isalnum(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
317+
arg_check!(vm, args, required = [(s, Some(vm.ctx.str_type()))]);
318+
let value = get_value(&s);
319+
let mut is_alnum: bool = true;
320+
for c in value.chars() {
321+
if !c.is_alphanumeric() {
322+
is_alnum = false;
323+
break;
324+
}
325+
}
326+
Ok(vm.ctx.new_bool(is_alnum))
327+
}
328+
329+
fn str_isalpha(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
330+
arg_check!(vm, args, required = [(s, Some(vm.ctx.str_type()))]);
331+
let value = get_value(&s);
332+
let mut is_alpha: bool = true;
333+
for c in value.chars() {
334+
if !c.is_alphabetic() {
335+
is_alpha = false;
336+
break;
337+
}
338+
}
339+
Ok(vm.ctx.new_bool(is_alpha))
340+
}
341+
342+
fn str_isdigit(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
343+
arg_check!(vm, args, required = [(s, Some(vm.ctx.str_type()))]);
344+
let value = get_value(&s);
345+
let mut is_digit: bool = true;
346+
for c in value.chars() {
347+
if !c.is_digit(10) {
348+
// checking if char is exponent
349+
let char_as_uni: u16 = c as u16;
350+
if VALID_UNICODES.contains(&&char_as_uni) {
351+
continue;
352+
} else {
353+
is_digit = false;
354+
break;
355+
}
356+
}
357+
}
358+
Ok(vm.ctx.new_bool(is_digit))
359+
}
360+
290361
fn str_getitem(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
291362
arg_check!(
292363
vm,

0 commit comments

Comments
 (0)