Skip to content

Commit

Permalink
deduplicate the code by using a write_output fn
Browse files Browse the repository at this point in the history
  • Loading branch information
Kev1n8 committed Jul 6, 2024
1 parent 61b85a0 commit 6fd8ba8
Showing 1 changed file with 34 additions and 44 deletions.
78 changes: 34 additions & 44 deletions src/uucore/src/lib/features/format/num_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,35 +78,16 @@ pub struct SignedInt {
impl Formatter for SignedInt {
type Input = i64;

fn fmt(&self, mut writer: impl Write, x: Self::Input) -> std::io::Result<()> {
let mut s = if self.precision > 0 {
fn fmt(&self, writer: impl Write, x: Self::Input) -> std::io::Result<()> {
let s = if self.precision > 0 {
format!("{:0>width$}", x.abs(), width = self.precision)
} else {
x.abs().to_string()
};

let flag = get_flag(self.positive_sign, &x);

// Take length of `flag`, which could be 0 or 1, into consideration when padding
// by storing remaining_width indicating the actual width needed.
// Using min() because self.width could be 0, 0usize - 1usize should be avoided
let remaining_width = self.width - min(self.width, flag.len());
match self.alignment {
NumberAlignment::Left => write!(writer, "{flag}{s:<width$}", width = remaining_width),
NumberAlignment::RightSpace => {
let is_sign_flag = flag.starts_with("-") || flag.starts_with("+"); // When flag is in ['-', '+']
if is_sign_flag && remaining_width > 0 {
// Make sure flag is right next to number, e.g. "% +3.1d" 1 ==> $ +1
s = flag + s.as_str();
write!(writer, "{s:>width$}", width = remaining_width + 1) // Since we now add flag and s together, plus 1
} else {
write!(writer, "{flag}{s:>width$}", width = remaining_width)
}
}
NumberAlignment::RightZero => {
write!(writer, "{flag}{s:0>width$}", width = remaining_width)
}
}
write_output(writer, self.width, flag, s, self.alignment)
}

fn try_from_spec(s: Spec) -> Result<Self, FormatError> {
Expand Down Expand Up @@ -258,7 +239,7 @@ impl Default for Float {
impl Formatter for Float {
type Input = f64;

fn fmt(&self, mut writer: impl Write, x: Self::Input) -> std::io::Result<()> {
fn fmt(&self, writer: impl Write, x: Self::Input) -> std::io::Result<()> {
let mut s = if x.is_finite() {
match self.variant {
FloatVariant::Decimal => {
Expand All @@ -284,27 +265,7 @@ impl Formatter for Float {

let flag = get_flag(self.positive_sign, &x);

let remaining_width = if self.width >= flag.len() {
self.width - flag.len()
} else {
0
};
match self.alignment {
NumberAlignment::Left => write!(writer, "{flag}{s:<width$}", width = remaining_width),
NumberAlignment::RightSpace => {
let is_sign_flag = flag.starts_with("-") || flag.starts_with("+"); // When flag is in ['-', '+']
if is_sign_flag && remaining_width > 0 {
// Make sure flag is just next to number, e.g. "% +5.1f" 1 ==> $ +1.0
s = flag + s.as_str();
write!(writer, "{s:>width$}", width = remaining_width + 1) // Since we now add flag and s together, plus 1
} else {
write!(writer, "{flag}{s:>width$}", width = remaining_width)
}
}
NumberAlignment::RightZero => {
write!(writer, "{flag}{s:0>width$}", width = remaining_width)
}
}
write_output(writer, self.width, flag, s, self.alignment)
}

fn try_from_spec(s: Spec) -> Result<Self, FormatError>
Expand Down Expand Up @@ -541,6 +502,35 @@ fn strip_fractional_zeroes_and_dot(s: &mut String) {
}
}

fn write_output(
mut writer: impl Write,
width: usize,
flag: String,
mut s: String,
alignment: NumberAlignment,
) -> std::io::Result<()> {
// Take length of `flag`, which could be 0 or 1, into consideration when padding
// by storing remaining_width indicating the actual width needed.
// Using min() because self.width could be 0, 0usize - 1usize should be avoided
let remaining_width = width - min(width, flag.len());
match alignment {
NumberAlignment::Left => write!(writer, "{flag}{s:<width$}", width = remaining_width),
NumberAlignment::RightSpace => {
let is_sign_flag = flag.starts_with('-') || flag.starts_with('+'); // When flag is in ['-', '+']
if is_sign_flag && remaining_width > 0 {
// Make sure flag is just next to number, e.g. "% +5.1f" 1 ==> $ +1.0
s = flag + s.as_str();
write!(writer, "{s:>width$}", width = remaining_width + 1) // Since we now add flag and s together, plus 1
} else {
write!(writer, "{flag}{s:>width$}", width = remaining_width)
}
}
NumberAlignment::RightZero => {
write!(writer, "{flag}{s:0>width$}", width = remaining_width)
}
}
}

#[cfg(test)]
mod test {
use crate::format::num_format::{Case, ForceDecimal};
Expand Down

0 comments on commit 6fd8ba8

Please sign in to comment.