1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-27 19:17:43 +00:00

Merge pull request #3053 from snobee/stat-handle-negative-time

stat: allow formatting of negative numbers
This commit is contained in:
Sylvestre Ledru 2022-02-08 20:45:21 +01:00 committed by GitHub
commit 936ac0db38
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 1 deletions

View file

@ -193,11 +193,19 @@ impl ScanUtil for str {
}
pub fn group_num(s: &str) -> Cow<str> {
assert!(s.chars().all(char::is_numeric));
let is_negative = s.starts_with('-');
assert!(is_negative || s.chars().take(1).all(|c| c.is_digit(10)));
assert!(s.chars().skip(1).all(|c| c.is_digit(10)));
if s.len() < 4 {
return s.into();
}
let mut res = String::with_capacity((s.len() - 1) / 3);
let s = if is_negative {
res.push('-');
&s[1..]
} else {
s
};
let mut alone = (s.len() - 1) % 3 + 1;
res.push_str(&s[..alone]);
while alone != s.len() {

View file

@ -34,6 +34,8 @@ fn test_group_num() {
assert_eq!("24", group_num("24"));
assert_eq!("4", group_num("4"));
assert_eq!("", group_num(""));
assert_eq!("-5", group_num("-5"));
assert_eq!("-1,234", group_num("-1234"));
}
#[test]