mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
zfill to common
This commit is contained in:
1 parent
2d01cffbf9
commit
8ea113608b
2 files changed
+19
-16
No files matched your search
@@ -11,6 +11,24 @@ pub fn get_chars(s: &str, range: std::ops::Range<usize>) -> &str {
|
||||
&start[..start.len() - end.len()]
|
||||
}
|
||||
|
||||
pub fn zfill(bytes: &[u8], width: usize) -> Vec<u8> {
|
||||
if width <= bytes.len() {
|
||||
bytes.to_vec()
|
||||
} else {
|
||||
let (sign, s) = match bytes.first() {
|
||||
Some(_sign @ b'+') | Some(_sign @ b'-') => {
|
||||
(unsafe { bytes.get_unchecked(..1) }, &bytes[1..])
|
||||
}
|
||||
_ => (&b""[..], bytes),
|
||||
};
|
||||
let mut filled = Vec::new();
|
||||
filled.extend_from_slice(sign);
|
||||
filled.extend(std::iter::repeat(b'0').take(width - bytes.len()));
|
||||
filled.extend_from_slice(s);
|
||||
filled
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
+1
-16
@@ -393,23 +393,8 @@ where
|
||||
}
|
||||
|
||||
fn py_zfill(&self, width: isize) -> Vec<u8> {
|
||||
let bytes = self.as_bytes();
|
||||
let width = width.to_usize().unwrap_or(0);
|
||||
if width <= bytes.len() {
|
||||
bytes.to_vec()
|
||||
} else {
|
||||
let (sign, s) = match bytes.first() {
|
||||
Some(_sign @ b'+') | Some(_sign @ b'-') => {
|
||||
(unsafe { bytes.get_unchecked(..1) }, &bytes[1..])
|
||||
}
|
||||
_ => (&b""[..], bytes),
|
||||
};
|
||||
let mut filled = Vec::new();
|
||||
filled.extend_from_slice(sign);
|
||||
filled.extend(std::iter::repeat(b'0').take(width - bytes.len()));
|
||||
filled.extend_from_slice(s);
|
||||
filled
|
||||
}
|
||||
rustpython_common::str::zfill(self.as_bytes(), width)
|
||||
}
|
||||
|
||||
fn py_iscase<F, G>(&'s self, is_case: F, is_opposite: G) -> bool
|
||||
|
||||
Reference in new issue
Block a user