Skip to content

Commit 1270aee

Browse files
committed
Add is* methods to bytearray
1 parent 35a06bc commit 1270aee

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed

vm/src/obj/objbytearray.rs

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,46 @@ pub fn init(context: &PyContext) {
3737
"__len__",
3838
context.new_rustfunc(bytesarray_len),
3939
);
40+
context.set_attr(
41+
&bytearray_type,
42+
"isalnum",
43+
context.new_rustfunc(bytearray_isalnum),
44+
);
45+
context.set_attr(
46+
&bytearray_type,
47+
"isalpha",
48+
context.new_rustfunc(bytearray_isalpha),
49+
);
50+
context.set_attr(
51+
&bytearray_type,
52+
"isascii",
53+
context.new_rustfunc(bytearray_isascii),
54+
);
55+
context.set_attr(
56+
&bytearray_type,
57+
"isdigit",
58+
context.new_rustfunc(bytearray_isdigit),
59+
);
60+
context.set_attr(
61+
&bytearray_type,
62+
"islower",
63+
context.new_rustfunc(bytearray_islower),
64+
);
65+
context.set_attr(
66+
&bytearray_type,
67+
"isspace",
68+
context.new_rustfunc(bytearray_isspace),
69+
);
70+
context.set_attr(
71+
&bytearray_type,
72+
"isupper",
73+
context.new_rustfunc(bytearray_isupper),
74+
);
75+
context.set_attr(
76+
&bytearray_type,
77+
"istitle",
78+
context.new_rustfunc(bytearray_istitle),
79+
);
4080
}
4181

4282
fn bytearray_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
@@ -92,6 +132,87 @@ fn bytearray_eq(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
92132
Ok(vm.ctx.new_bool(result))
93133
}
94134

135+
fn bytearray_isalnum(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
136+
arg_check!(vm, args, required = [(zelf, Some(vm.ctx.bytearray_type()))]);
137+
let bytes = get_value(zelf);
138+
Ok(vm.new_bool(bytes.iter().all(|x| char::from(*x).is_alphanumeric())))
139+
}
140+
141+
fn bytearray_isalpha(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
142+
arg_check!(vm, args, required = [(zelf, Some(vm.ctx.bytearray_type()))]);
143+
let bytes = get_value(zelf);
144+
Ok(vm.new_bool(bytes.iter().all(|x| char::from(*x).is_alphabetic())))
145+
}
146+
147+
fn bytearray_isascii(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
148+
arg_check!(vm, args, required = [(zelf, Some(vm.ctx.bytearray_type()))]);
149+
let bytes = get_value(zelf);
150+
Ok(vm.new_bool(bytes.iter().all(|x| char::from(*x).is_ascii())))
151+
}
152+
153+
fn bytearray_isdigit(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
154+
arg_check!(vm, args, required = [(zelf, Some(vm.ctx.bytearray_type()))]);
155+
let bytes = get_value(zelf);
156+
Ok(vm.new_bool(bytes.iter().all(|x| char::from(*x).is_digit(10))))
157+
}
158+
159+
fn bytearray_islower(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
160+
arg_check!(vm, args, required = [(zelf, Some(vm.ctx.bytearray_type()))]);
161+
let bytes = get_value(zelf);
162+
Ok(vm.new_bool(bytes.iter().all(|x| char::from(*x).is_lowercase())))
163+
}
164+
165+
fn bytearray_isspace(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
166+
arg_check!(vm, args, required = [(zelf, Some(vm.ctx.bytearray_type()))]);
167+
let bytes = get_value(zelf);
168+
Ok(vm.new_bool(bytes.iter().all(|x| char::from(*x).is_whitespace())))
169+
}
170+
171+
fn bytearray_isupper(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
172+
arg_check!(vm, args, required = [(zelf, Some(vm.ctx.bytearray_type()))]);
173+
let bytes = get_value(zelf);
174+
Ok(vm.new_bool(bytes.iter().all(|x| char::from(*x).is_uppercase())))
175+
}
176+
177+
fn bytearray_istitle(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
178+
arg_check!(vm, args, required = [(zelf, Some(vm.ctx.bytearray_type()))]);
179+
let bytes = get_value(zelf);
180+
181+
if bytes.is_empty() {
182+
Ok(vm.new_bool(false))
183+
} else {
184+
let mut iter = bytes.iter().peekable();
185+
let mut prev_cased = false;
186+
187+
while let Some(c) = iter.next() {
188+
let current = char::from(*c);
189+
let next = if let Some(k) = iter.peek() {
190+
char::from(**k)
191+
} else {
192+
if current.is_uppercase() {
193+
return Ok(vm.new_bool(!prev_cased));
194+
} else {
195+
return Ok(vm.new_bool(prev_cased));
196+
}
197+
};
198+
199+
if is_cased(current) && next.is_uppercase() && !prev_cased {
200+
return Ok(vm.new_bool(false));
201+
} else if !is_cased(current) && next.is_lowercase() {
202+
return Ok(vm.new_bool(false));
203+
}
204+
prev_cased = is_cased(current);
205+
}
206+
207+
Ok(vm.new_bool(true))
208+
}
209+
}
210+
211+
// helper function for istitle
212+
fn is_cased(c: char) -> bool {
213+
c.to_uppercase().next().unwrap() != c || c.to_lowercase().next().unwrap() != c
214+
}
215+
95216
/*
96217
fn bytearray_getitem(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
97218
arg_check!(

0 commit comments

Comments
 (0)