Skip to content

Commit c3e2553

Browse files
committed
chore(factor): deduplicate printing code
Now there is a single polymorphic function that just takes displayable data, not concrete datatypes. It's two opaque Display types since one of the cases has mismatched number types.
1 parent 999fb70 commit c3e2553

1 file changed

Lines changed: 7 additions & 53 deletions

File tree

src/uu/factor/src/factor.rs

Lines changed: 7 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ fn write_factors_str(
4848
// use num_prime's factorize64 algorithm for u64 integers
4949
if x <= BigUint::from_u64(u64::MAX).unwrap() {
5050
let prime_factors = num_prime::nt_funcs::factorize64(x.clone().to_u64_digits()[0]);
51-
write_result_u64(w, &x, prime_factors, print_exponents)
51+
write_result(w, &x, prime_factors, print_exponents)
5252
.map_err_context(|| translate!("factor-error-write-error"))?;
5353
}
5454
// use num_prime's factorize128 algorithm for u128 integers
@@ -59,7 +59,7 @@ fn write_factors_str(
5959
return Ok(());
6060
};
6161
let prime_factors = num_prime::nt_funcs::factorize128(x);
62-
write_result_u128(w, &x, prime_factors, print_exponents)
62+
write_result(w, &x, prime_factors, print_exponents)
6363
.map_err_context(|| translate!("factor-error-write-error"))?;
6464
}
6565
// use num_prime's fallible factorization for anything greater than u128::MAX
@@ -71,12 +71,12 @@ fn write_factors_str(
7171
translate!("factor-error-factorization-incomplete"),
7272
));
7373
}
74-
write_result_big_uint(w, &x, prime_factors, print_exponents)
74+
write_result(w, &x, prime_factors, print_exponents)
7575
.map_err_context(|| translate!("factor-error-write-error"))?;
7676
}
7777
} else {
7878
let empty_primes: BTreeMap<BigUint, usize> = BTreeMap::new();
79-
write_result_big_uint(w, &x, empty_primes, print_exponents)
79+
write_result(w, &x, empty_primes, print_exponents)
8080
.map_err_context(|| translate!("factor-error-write-error"))?;
8181
}
8282

@@ -133,56 +133,10 @@ impl Display for NumError<'_> {
133133
}
134134

135135
/// Writing out the prime factors for u64 integers
136-
fn write_result_u64(
136+
fn write_result(
137137
w: &mut io::BufWriter<impl Write>,
138-
x: &BigUint,
139-
factorization: BTreeMap<u64, usize>,
140-
print_exponents: bool,
141-
) -> io::Result<()> {
142-
write!(w, "{x}:")?;
143-
for (factor, n) in factorization {
144-
if print_exponents {
145-
if n > 1 {
146-
write!(w, " {factor}^{n}")?;
147-
} else {
148-
write!(w, " {factor}")?;
149-
}
150-
} else {
151-
w.write_all(format!(" {factor}").repeat(n).as_bytes())?;
152-
}
153-
}
154-
writeln!(w)?;
155-
w.flush()
156-
}
157-
158-
/// Writing out the prime factors for u128 integers
159-
fn write_result_u128(
160-
w: &mut io::BufWriter<impl Write>,
161-
x: &u128,
162-
factorization: BTreeMap<u128, usize>,
163-
print_exponents: bool,
164-
) -> io::Result<()> {
165-
write!(w, "{x}:")?;
166-
for (factor, n) in factorization {
167-
if print_exponents {
168-
if n > 1 {
169-
write!(w, " {factor}^{n}")?;
170-
} else {
171-
write!(w, " {factor}")?;
172-
}
173-
} else {
174-
w.write_all(format!(" {factor}").repeat(n).as_bytes())?;
175-
}
176-
}
177-
writeln!(w)?;
178-
w.flush()
179-
}
180-
181-
/// Writing out the prime factors for BigUint integers
182-
fn write_result_big_uint(
183-
w: &mut io::BufWriter<impl Write>,
184-
x: &BigUint,
185-
factorization: BTreeMap<BigUint, usize>,
138+
x: &impl Display,
139+
factorization: BTreeMap<impl Display, usize>,
186140
print_exponents: bool,
187141
) -> io::Result<()> {
188142
write!(w, "{x}:")?;

0 commit comments

Comments
 (0)