| 411 | } |
| 412 | |
| 413 | pub 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. |