/* * Copyright (c) 2023–2024 Emma Tebibyte * SPDX-License-Identifier: AGPL-3.0-or-later * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU Affero General Public License as published by the Free * Software Foundation, either version 3 of the License, or (at your option) any * later version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more * details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see https://www.gnu.org/licenses/. */ use std::{ cmp::Ordering, env::args, io::{ stdin, stdout, Write }, process::{ ExitCode, exit }, }; extern crate strerror; extern crate sysexits; use strerror::c_error; use sysexits::{ EX_DATAERR, EX_IOERR, EX_SOFTWARE }; const LIST: [(u32, &str); 10] = [ (3, "k"), (6, "M"), (9, "G"), (12, "T"), (15, "P"), (18, "E"), (21, "Z"), (24, "Y"), (27, "R"), (30, "Q") ]; fn convert(input: u128) -> Result<(f64, (u32, &'static str)), String> { let mut out = (input as f64, (0_u32, "")); if input < 1000 { return Ok(out); } for (n, p) in LIST { let c = match 10_u128.checked_pow(n) { Some(c) => c, None => { return Err(format!("10^{}: Integer overflow", n.to_string())); }, }; match c.cmp(&input) { Ordering::Less => { out = (input as f64 / c as f64, (n, p)); }, Ordering::Equal => { return Ok((input as f64 / c as f64, (n, p))); }, Ordering::Greater => {}, }; } Ok(out) } fn main() -> ExitCode { let argv = args().collect::>(); let mut buf = String::new(); while let Ok(_) = stdin().read_line(&mut buf) { if buf.is_empty() { return ExitCode::SUCCESS; } let n: u128 = match buf.trim().parse() { Ok(f) => { buf.clear(); f }, Err(err) => { eprintln!("{}: {}", argv[0], err); return ExitCode::from(EX_DATAERR as u8); }, }; let (number, prefix) = match convert(n) { Ok(x) => x, Err(err) => { eprintln!("{}: {}", argv[0], err); return ExitCode::from(EX_SOFTWARE as u8); }, }; let si_prefix = format!("{}B", prefix.1); let out = ((number * 10.0).round() / 10.0).to_string(); stdout().write_all(format!("{} {}\n", out, si_prefix).as_bytes()) .unwrap_or_else(|e| { eprintln!("{}: {}", argv[0], c_error(e)); exit(EX_IOERR); }); } ExitCode::SUCCESS }