Skip to content

Commit bd78f7e

Browse files
committed
add capitalize
1 parent 9d25a21 commit bd78f7e

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

tests/snippets/bytes.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,14 @@
109109
assert bytes(b"Is Title Case").istitle()
110110
assert not bytes(b"is Not title casE").istitle()
111111

112-
# upper lower
112+
# upper lower, capitalize
113113
l = bytes(b"lower")
114114
b = bytes(b"UPPER")
115115
assert l.lower().islower()
116116
assert b.upper().isupper()
117+
assert l.capitalize() == b"Lower"
118+
assert b.capitalize() == b"Upper"
119+
assert bytes().capitalize() == bytes()
117120

118121
# hex from hex
119122
assert bytes([0, 1, 9, 23, 90, 234]).hex() == "000109175aea"
@@ -127,3 +130,4 @@
127130
bytes.fromhex("6Z2")
128131
except ValueError as e:
129132
str(e) == "non-hexadecimal number found in fromhex() arg at position 1"
133+

vm/src/obj/objbyteinner.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,15 @@ impl PyByteInner {
305305
self.elements.to_ascii_uppercase()
306306
}
307307

308+
pub fn capitalize(&self, _vm: &VirtualMachine) -> Vec<u8> {
309+
let mut new: Vec<u8> = Vec::new();
310+
if let Some((first, second)) = self.elements.split_first() {
311+
new.push(first.to_ascii_uppercase());
312+
second.iter().for_each(|x| new.push(x.to_ascii_lowercase()));
313+
}
314+
new
315+
}
316+
308317
pub fn hex(&self, vm: &VirtualMachine) -> PyResult {
309318
let bla = self
310319
.elements

vm/src/obj/objbytes.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,11 @@ impl PyBytesRef {
209209
Ok(vm.ctx.new_bytes(self.inner.upper(vm)))
210210
}
211211

212+
#[pymethod(name = "capitalize")]
213+
fn capitalize(self, vm: &VirtualMachine) -> PyResult {
214+
Ok(vm.ctx.new_bytes(self.inner.capitalize(vm)))
215+
}
216+
212217
#[pymethod(name = "hex")]
213218
fn hex(self, vm: &VirtualMachine) -> PyResult {
214219
self.inner.hex(vm)

0 commit comments

Comments
 (0)