Skip to content

Commit eed618d

Browse files
authored
Fix str.zfill() width calculation for non-ASCII strings (RustPython#7534)
1 parent 87fc454 commit eed618d

2 files changed

Lines changed: 7 additions & 0 deletions

File tree

crates/vm/src/anystr.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,10 @@ pub trait AnyStr {
396396

397397
fn py_zfill(&self, width: isize) -> Vec<u8> {
398398
let width = width.to_usize().unwrap_or(0);
399+
let char_len = self.elements().count();
400+
let width = self
401+
.bytes_len()
402+
.saturating_add(width.saturating_sub(char_len));
399403
rustpython_common::str::zfill(self.as_bytes(), width)
400404
}
401405

extra_tests/snippets/builtin_str.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@
6161
assert not a.endswith("on")
6262
assert not a.endswith(("A", "ll"))
6363
assert a.zfill(8) == "000Hallo"
64+
assert "á".zfill(4) == "000á"
65+
assert "🙂".zfill(5) == "0000🙂"
66+
assert "+あ".zfill(5) == "+000あ"
6467
assert a.isalnum()
6568
assert not a.isdigit()
6669
assert not a.isdecimal()

0 commit comments

Comments
 (0)