Skip to content

Commit b833c08

Browse files
committed
Add bytearray.{lower, upper}
1 parent e058179 commit b833c08

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

tests/snippets/bytearray.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,19 @@
1818
assert bytearray(b'1234567890').isdigit()
1919
assert not bytearray(b'12ab').isdigit()
2020

21-
assert bytearray(b'lower').islower()
21+
l = bytearray(b'lower')
22+
assert l.islower()
23+
assert not l.isupper()
24+
assert l.upper().isupper()
2225
assert not bytearray(b'Super Friends').islower()
2326

2427
assert bytearray(b' \n\t').isspace()
2528
assert not bytearray(b'\td\n').isspace()
2629

27-
assert bytearray(b'UPPER').isupper()
30+
b = bytearray(b'UPPER')
31+
assert b.isupper()
32+
assert not b.islower()
33+
assert b.lower().islower()
2834
assert not bytearray(b'tuPpEr').isupper()
2935

3036
assert bytearray(b'Is Title Case').istitle()

vm/src/obj/objbytearray.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,16 @@ pub fn init(context: &PyContext) {
102102
context.new_rustfunc(bytearray_clear),
103103
);
104104
context.set_attr(&bytearray_type, "pop", context.new_rustfunc(bytearray_pop));
105+
context.set_attr(
106+
&bytearray_type,
107+
"lower",
108+
context.new_rustfunc(bytearray_lower),
109+
);
110+
context.set_attr(
111+
&bytearray_type,
112+
"upper",
113+
context.new_rustfunc(bytearray_upper),
114+
);
105115
}
106116

107117
fn bytearray_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
@@ -188,7 +198,7 @@ fn bytearray_islower(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
188198
!bytes.is_empty()
189199
&& bytes
190200
.iter()
191-
.filter(|x| char::from(**x).is_whitespace())
201+
.filter(|x| !char::from(**x).is_whitespace())
192202
.all(|x| char::from(*x).is_lowercase()),
193203
))
194204
}
@@ -295,3 +305,21 @@ fn bytearray_pop(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
295305
Err(vm.new_index_error("pop from empty bytearray".to_string()))
296306
}
297307
}
308+
309+
fn bytearray_lower(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
310+
arg_check!(vm, args, required = [(obj, Some(vm.ctx.bytearray_type()))]);
311+
let value = get_value(obj).to_vec().to_ascii_lowercase();
312+
Ok(PyObject::new(
313+
PyObjectPayload::Bytes { value },
314+
vm.ctx.bytearray_type(),
315+
))
316+
}
317+
318+
fn bytearray_upper(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
319+
arg_check!(vm, args, required = [(obj, Some(vm.ctx.bytearray_type()))]);
320+
let value = get_value(obj).to_vec().to_ascii_uppercase();
321+
Ok(PyObject::new(
322+
PyObjectPayload::Bytes { value },
323+
vm.ctx.bytearray_type(),
324+
))
325+
}

0 commit comments

Comments
 (0)