Skip to content

Commit

Permalink
Merge pull request #5789 from samueltardieu/issue-5766
Browse files Browse the repository at this point in the history
Fix seq output
  • Loading branch information
sylvestre authored Jan 6, 2024
2 parents c867d6b + b309d64 commit a60c342
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 11 deletions.
47 changes: 36 additions & 11 deletions src/uucore/src/lib/features/format/num_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,13 @@ impl Formatter for Float {

let precision = match precision {
Some(CanAsterisk::Fixed(x)) => x,
None => 0,
None => {
if matches!(variant, FloatVariant::Shortest) {
6
} else {
0
}
}
Some(CanAsterisk::Asterisk) => return Err(FormatError::WrongSpecType),
};

Expand Down Expand Up @@ -405,7 +411,7 @@ fn format_float_shortest(
let mut normalized = format!("{normalized:.*}", precision);

if force_decimal == ForceDecimal::No {
strip_zeros_and_dot(&mut normalized);
strip_fractional_zeroes_and_dot(&mut normalized);
}

let exp_char = match case {
Expand All @@ -418,16 +424,17 @@ fn format_float_shortest(
// Decimal-ish notation with a few differences:
// - The precision works differently and specifies the total number
// of digits instead of the digits in the fractional part.
// - If we don't force the decimal, '0' and `.` are trimmed.
let decimal_places = (precision as i32).saturating_sub(exponent) as usize;
// - If we don't force the decimal, `.` and trailing `0` in the fractional part
// are trimmed.
let decimal_places = (precision as i32 - exponent) as usize;
let mut formatted = if decimal_places == 0 && force_decimal == ForceDecimal::Yes {
format!("{f:.0}.")
} else {
format!("{f:.*}", decimal_places)
};

if force_decimal == ForceDecimal::No {
strip_zeros_and_dot(&mut formatted);
strip_fractional_zeroes_and_dot(&mut formatted);
}

formatted
Expand Down Expand Up @@ -463,12 +470,16 @@ fn format_float_hexadecimal(
s
}

fn strip_zeros_and_dot(s: &mut String) {
while s.ends_with('0') {
s.pop();
}
if s.ends_with('.') {
s.pop();
fn strip_fractional_zeroes_and_dot(s: &mut String) {
let mut trim_to = s.len();
for (pos, c) in s.char_indices().rev() {
if pos + c.len_utf8() == trim_to && (c == '0' || c == '.') {
trim_to = pos;
}
if c == '.' {
s.truncate(trim_to);
break;
}
}
}

Expand Down Expand Up @@ -574,4 +585,18 @@ mod test {
assert_eq!(f(1000000.0), "1.e+06");
assert_eq!(f(99999999.0), "1.e+08");
}

#[test]
fn strip_insignificant_end() {
use super::strip_fractional_zeroes_and_dot;
let f = |s| {
let mut s = String::from(s);
strip_fractional_zeroes_and_dot(&mut s);
s
};
assert_eq!(&f("1000"), "1000");
assert_eq!(&f("1000."), "1000");
assert_eq!(&f("1000.02030"), "1000.0203");
assert_eq!(&f("1000.00000"), "1000");
}
}
20 changes: 20 additions & 0 deletions tests/by-util/test_seq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,26 @@ fn test_invalid_zero_increment_value() {
.usage_error("invalid Zero increment value: '0'");
}

#[test]
fn test_power_of_ten_display() {
new_ucmd!()
.args(&["-f", "%.2g", "10", "10"])
.succeeds()
.stdout_only("10\n");
}

#[test]
fn test_default_g_precision() {
new_ucmd!()
.args(&["-f", "%010g", "1e5", "1e5"])
.succeeds()
.stdout_only("0000100000\n");
new_ucmd!()
.args(&["-f", "%010g", "1e6", "1e6"])
.succeeds()
.stdout_only("000001e+06\n");
}

#[test]
fn test_invalid_format() {
new_ucmd!()
Expand Down

0 comments on commit a60c342

Please sign in to comment.