From 82464b703a0faa7505fc8613a545f31a0d7ae9b2 Mon Sep 17 00:00:00 2001 From: Terts Diepraam Date: Thu, 17 Nov 2022 01:25:10 +0100 Subject: [PATCH] stat: remove extend_digits and print_adjusted in favor of format! --- src/uu/stat/src/stat.rs | 107 ++++++++++------------------------------ 1 file changed, 26 insertions(+), 81 deletions(-) diff --git a/src/uu/stat/src/stat.rs b/src/uu/stat/src/stat.rs index cf89a7835..524114f45 100644 --- a/src/uu/stat/src/stat.rs +++ b/src/uu/stat/src/stat.rs @@ -21,10 +21,10 @@ use clap::{crate_version, Arg, ArgAction, ArgMatches, Command}; use std::borrow::Cow; use std::convert::AsRef; use std::ffi::{OsStr, OsString}; +use std::fs; use std::os::unix::fs::{FileTypeExt, MetadataExt}; use std::os::unix::prelude::OsStrExt; use std::path::Path; -use std::{cmp, fs, iter}; const ABOUT: &str = "Display file or file system status."; const USAGE: &str = "{} [OPTION]... FILE..."; @@ -61,22 +61,6 @@ fn check_bound(slice: &str, bound: usize, beg: usize, end: usize) -> UResult<()> Ok(()) } -/// pads the string with zeroes if supplied min is greater -/// then the length of the string, else returns the original string -fn extend_digits(string: &str, min: usize) -> Cow<'_, str> { - if min > string.len() { - let mut pad = String::with_capacity(min); - iter::repeat('0') - .take(min - string.len()) - .map(|_| pad.push('0')) - .all(|_| true); - pad.push_str(string); - pad.into() - } else { - string.into() - } -} - enum Padding { Zero, Space, @@ -100,30 +84,6 @@ fn pad_and_print(result: &str, left: bool, width: usize, padding: Padding) { }; } -/// prints the adjusted string after padding -/// `left` flag specifies the type of alignment of the string -/// `width` is the supplied padding width of the string needed -/// `prefix` & `need_prefix` are Optional, which adjusts the `field_width` accordingly, where -/// `field_width` is the max of supplied `width` and size of string -/// `padding`, specifies type of padding, which is '0' or ' ' in this case. -fn print_adjusted( - s: &str, - left: bool, - need_prefix: Option, - prefix: Option<&str>, - width: usize, - padding: Padding, -) { - let mut field_width = cmp::max(width, s.len()); - if let Some(p) = prefix { - if let Some(true) = need_prefix { - field_width -= p.len(); - } - pad_and_print(s, left, field_width, padding); - } else { - pad_and_print(s, left, field_width, padding); - } -} #[derive(Debug)] pub enum OutputType { Str(String), @@ -282,15 +242,13 @@ fn print_it(output: &OutputType, flags: Flags, width: usize, precision: Option { let s = match precision { Some(p) if p < s.len() => &s[..p], _ => s, }; - print_adjusted(s, flags.left, None, None, width, Padding::Space); + pad_and_print(s, flags.left, width, Padding::Space); } OutputType::Integer(num) => { let num = num.to_string(); @@ -299,57 +257,44 @@ fn print_it(output: &OutputType, flags: Flags, width: usize, precision: Optionprecision$}", + precision = precision.unwrap_or(0) ); + pad_and_print(&extended, flags.left, width, padding_char); } OutputType::Unsigned(num) => { let num = num.to_string(); - let arg = if flags.group { + let s = if flags.group { group_num(&num) } else { Cow::Borrowed(num.as_str()) }; - let extended = extend_digits(&arg, precision.unwrap_or(0)); - print_adjusted( - extended.as_ref(), - flags.left, - None, - None, - width, - padding_char, - ); + let s = format!("{s:0>precision$}", precision = precision.unwrap_or(0)); + pad_and_print(&s, flags.left, width, padding_char); } OutputType::UnsignedOct(num) => { - let num = format!("{:o}", num); - let extended = extend_digits(&num, precision.unwrap_or(0)); - print_adjusted( - extended.as_ref(), - flags.left, - Some(flags.alter), - Some("0"), - width, - padding_char, + let prefix = if flags.alter { "0" } else { "" }; + let s = format!( + "{prefix}{num:0>precision$o}", + precision = precision.unwrap_or(0) ); + pad_and_print(&s, flags.left, width, padding_char); } OutputType::UnsignedHex(num) => { - let num = format!("{:x}", num); - let extended = extend_digits(&num, precision.unwrap_or(0)); - print_adjusted( - extended.as_ref(), - flags.left, - Some(flags.alter), - Some("0x"), - width, - padding_char, + let prefix = if flags.alter { "0x" } else { "" }; + let s = format!( + "{prefix}{num:0>precision$x}", + precision = precision.unwrap_or(0) ); + pad_and_print(&s, flags.left, width, padding_char); } OutputType::Unknown => print!("?"), }