Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/uu/numfmt/src/numfmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,16 +248,23 @@ fn parse_unit(s: &str, opt: &'static str) -> std::result::Result<Unit, ParseErro
/// Parses a unit size. Suffixes are turned into their integer representations. For example, 'K'
/// will return `Ok(1000)`, and '2K' will return `Ok(2000)`.
fn parse_unit_size(s: &str, opt: &'static str) -> std::result::Result<usize, ParseError> {
let split = s.find(|c: char| !c.is_ascii_digit()).unwrap_or(s.len());
let (number, suffix) = s.split_at(split);
// Leading blanks and one '+' may come before the digits.
let unsigned = s.trim_start_matches([' ', '\t', '\n', '\r', '\x0B', '\x0C']);
let unsigned = unsigned.strip_prefix('+').unwrap_or(unsigned);
let split = unsigned
.find(|c: char| !c.is_ascii_digit())
.unwrap_or(unsigned.len());
let (number, suffix) = unsigned.split_at(split);

// Reject all-zero numeric parts like "0" or "00K".
let all_zero = !number.is_empty() && number.bytes().all(|b| b == b'0');
if !all_zero && let Some(multiplier) = parse_unit_size_suffix(suffix) {
if number.is_empty() {
return Ok(multiplier);
}
if let Some(size) = number
// A bare suffix like "K" is a size, but "", " K" and "+K" are not.
if !s.is_empty() && suffix == s {
return Ok(multiplier);
}
} else if let Some(size) = number
.parse::<usize>()
.ok()
.and_then(|n| n.checked_mul(multiplier))
Expand Down Expand Up @@ -292,7 +299,7 @@ fn parse_unit_size_suffix(s: &str) -> Option<usize> {

let i = ['K', 'M', 'G', 'T', 'P', 'E']
.iter()
.position(|&ch| s.starts_with(ch))?;
.position(|&ch| s.starts_with(ch) || (ch == 'K' && s.starts_with('k')))?;

match s.len() {
1 => Some(SI_BASES[i + 1] as usize),
Expand Down
7 changes: 7 additions & 0 deletions tests/by-util/test_numfmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ fn test_invalid_arg() {
#[case::invalid_arg_number_with_ignore_returns_status_0(&["--invalid=ignore", "4Q"], "4Q\n")]
#[case::from_unit(&["--from-unit=512", "4"], "2048\n")]
#[case::to_unit(&["--to-unit=512", "2048"], "4\n")]
#[case::from_unit_with_plus_and_lowercase_k(&["--from-unit=+2k", "4"], "8000\n")]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please add a case with ki too? (e.g. --from-unit=2ki)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a --from-unit=2ki case (4 gives 8192).

#[case::from_unit_with_lowercase_ki(&["--from-unit=2ki", "4"], "8192\n")]
#[case::to_unit_with_leading_blanks(&["--to-unit= \t\x0B512", "2048"], "4\n")]
#[case::format(&["--format=--%f--", "50"], "--50--\n")]
#[case::format_with_separate_value(&["--format", "--%f--", "50"], "--50--\n")]
#[case::format_padding_with_prefix_and_suffix(&["--format=--%6f--", "50"], "-- 50--\n")]
Expand Down Expand Up @@ -730,6 +733,10 @@ fn test_invalid_unit_size() {
let invalid_sizes = vec![
"A",
"0",
"",
"+K",
" K",
"++5",
"18446744073709551616",
"18446744073709551615K",
"18014398509481984Ki",
Expand Down
Loading