MCPcopy Index your code
hub / github.com/RustPython/RustPython / zfill

Function zfill

crates/common/src/str.rs:413–429  ·  view source on GitHub ↗
(bytes: &[u8], width: usize)

Source from the content-addressed store, hash-verified

411}
412
413pub fn zfill(bytes: &[u8], width: usize) -> Vec<u8> {
414 if width <= bytes.len() {
415 bytes.to_vec()
416 } else {
417 let (sign, s) = match bytes.first() {
418 Some(_sign @ b'+') | Some(_sign @ b'-') => {
419 (unsafe { bytes.get_unchecked(..1) }, &bytes[1..])
420 }
421 _ => (&b""[..], bytes),
422 };
423 let mut filled = Vec::new();
424 filled.extend_from_slice(sign);
425 filled.extend(core::iter::repeat_n(b'0', width - bytes.len()));
426 filled.extend_from_slice(s);
427 filled
428 }
429}
430
431/// Convert a string to ascii compatible, escaping unicode-s into escape
432/// sequences.

Callers 1

py_zfillMethod · 0.85

Calls 6

newFunction · 0.85
to_vecMethod · 0.80
get_uncheckedMethod · 0.80
lenMethod · 0.45
firstMethod · 0.45
extendMethod · 0.45

Tested by

no test coverage detected