diff --git a/src/uu/factor/src/cli.rs b/src/uu/factor/src/cli.rs index bba470cb635..93f23c753da 100644 --- a/src/uu/factor/src/cli.rs +++ b/src/uu/factor/src/cli.rs @@ -13,7 +13,7 @@ use clap::{crate_version, Arg, ArgAction, Command}; use num_bigint::BigUint; use num_traits::FromPrimitive; use uucore::display::Quotable; -use uucore::error::{set_exit_code, FromIo, UResult}; +use uucore::error::{set_exit_code, UResult, USimpleError}; use uucore::{format_usage, help_about, help_usage, show_error, show_warning}; const ABOUT: &str = help_about!("factor.md"); @@ -29,7 +29,7 @@ fn print_factors_str( num_str: &str, w: &mut io::BufWriter, print_exponents: bool, -) -> io::Result<()> { +) -> UResult<()> { let x = match num_str.trim().parse::() { Ok(x) => x, Err(e) => { @@ -43,12 +43,19 @@ fn print_factors_str( write!(w, "{x}:")?; - let factorization = if x > BigUint::from_u32(1).unwrap() { - num_prime::nt_funcs::factorize(x.clone()) + let (factorization, remaining) = if x > BigUint::from_u32(1).unwrap() { + num_prime::nt_funcs::factors(x.clone(), None) } else { - BTreeMap::new() + (BTreeMap::new(), None) }; + if let Some(_remaining) = remaining { + return Err(USimpleError::new( + 1, + "Factorization incomplete. Remainders exists.", + )); + } + // If print_exponents is true, use the alternate format specifier {:#} from fmt to print the factors // of x in the form of p^e. for (factor, n) in factorization { @@ -65,7 +72,8 @@ fn print_factors_str( } } writeln!(w)?; - w.flush() + w.flush()?; + Ok(()) } #[uucore::main] @@ -81,8 +89,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { if let Some(values) = matches.get_many::(options::NUMBER) { for number in values { - print_factors_str(number, &mut w, print_exponents) - .map_err_context(|| "write error".into())?; + print_factors_str(number, &mut w, print_exponents)?; } } else { let stdin = stdin(); @@ -91,8 +98,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { match line { Ok(line) => { for number in line.split_whitespace() { - print_factors_str(number, &mut w, print_exponents) - .map_err_context(|| "write error".into())?; + print_factors_str(number, &mut w, print_exponents)?; } } Err(e) => { diff --git a/tests/benches/factor/benches/table.rs b/tests/benches/factor/benches/table.rs index 90a61c0fb10..cc05ee0ca6b 100644 --- a/tests/benches/factor/benches/table.rs +++ b/tests/benches/factor/benches/table.rs @@ -2,12 +2,11 @@ // // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. + +// spell-checker:ignore funcs + use array_init::array_init; use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput}; -use std::{collections::BTreeMap, mem}; -use num_bigint::BigUint; -use num_traits::FromPrimitive; - fn table(c: &mut Criterion) { #[cfg(target_os = "linux")] @@ -31,11 +30,8 @@ fn table(c: &mut Criterion) { let a_str = format!("{:?}", a); group.bench_with_input(BenchmarkId::new("factor", &a_str), &a, |b, &a| { b.iter(|| { - let mut n_s = a; - let mut f_s: [BTreeMap; INPUT_SIZE] = array_init(|_| BTreeMap::new()); - for (&mut n, f) in n_s.iter_mut().zip(f_s.iter_mut()) { - let r: BTreeMap = num_prime::nt_funcs::factorize(BigUint::from_u64(n).unwrap()); - _ = mem::replace(f, r); + for n in a { + let _r = num_prime::nt_funcs::factors(n, None); } }); });