1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-27 19:17:43 +00:00

Merge pull request #6877 from cakebaker/use_div_ceil_from_std

Use `div_ceil()` from `std`
This commit is contained in:
Sylvestre Ledru 2024-11-20 12:26:46 +01:00 committed by GitHub
commit fb1874bff9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 6 additions and 27 deletions

View file

@ -22,7 +22,7 @@ use uucore::{
format_usage, help_about, help_section, help_usage, format_usage, help_about, help_section, help_usage,
line_ending::LineEnding, line_ending::LineEnding,
os_str_as_bytes, show, os_str_as_bytes, show,
sum::{div_ceil, Digest}, sum::Digest,
}; };
const USAGE: &str = help_usage!("cksum.md"); const USAGE: &str = help_usage!("cksum.md");
@ -124,7 +124,7 @@ where
format!( format!(
"{} {}{}", "{} {}{}",
sum.parse::<u16>().unwrap(), sum.parse::<u16>().unwrap(),
div_ceil(sz, options.output_bits), sz.div_ceil(options.output_bits),
if not_file { "" } else { " " } if not_file { "" } else { " " }
), ),
!not_file, !not_file,
@ -134,7 +134,7 @@ where
format!( format!(
"{:0bsd_width$} {:bsd_width$}{}", "{:0bsd_width$} {:bsd_width$}{}",
sum.parse::<u16>().unwrap(), sum.parse::<u16>().unwrap(),
div_ceil(sz, options.output_bits), sz.div_ceil(options.output_bits),
if not_file { "" } else { " " } if not_file { "" } else { " " }
), ),
!not_file, !not_file,

View file

@ -562,7 +562,7 @@ impl StatPrinter {
size, size,
uucore::format::human::SizeFormat::Binary, uucore::format::human::SizeFormat::Binary,
), ),
SizeFormat::BlockSize(block_size) => div_ceil(size, block_size).to_string(), SizeFormat::BlockSize(block_size) => size.div_ceil(block_size).to_string(),
} }
} }
@ -583,13 +583,6 @@ impl StatPrinter {
} }
} }
// This can be replaced with u64::div_ceil once it is stabilized.
// This implementation approach is optimized for when `b` is a constant,
// particularly a power of two.
pub fn div_ceil(a: u64, b: u64) -> u64 {
(a + b - 1) / b
}
// Read file paths from the specified file, separated by null characters // Read file paths from the specified file, separated by null characters
fn read_files_from(file_name: &str) -> Result<Vec<PathBuf>, std::io::Error> { fn read_files_from(file_name: &str) -> Result<Vec<PathBuf>, std::io::Error> {
let reader: Box<dyn BufRead> = if file_name == "-" { let reader: Box<dyn BufRead> = if file_name == "-" {

View file

@ -16,13 +16,6 @@ use uucore::{format_usage, help_about, help_usage, show};
const USAGE: &str = help_usage!("sum.md"); const USAGE: &str = help_usage!("sum.md");
const ABOUT: &str = help_about!("sum.md"); const ABOUT: &str = help_about!("sum.md");
// This can be replaced with usize::div_ceil once it is stabilized.
// This implementation approach is optimized for when `b` is a constant,
// particularly a power of two.
const fn div_ceil(a: usize, b: usize) -> usize {
(a + b - 1) / b
}
fn bsd_sum(mut reader: Box<dyn Read>) -> (usize, u16) { fn bsd_sum(mut reader: Box<dyn Read>) -> (usize, u16) {
let mut buf = [0; 4096]; let mut buf = [0; 4096];
let mut bytes_read = 0; let mut bytes_read = 0;
@ -41,7 +34,7 @@ fn bsd_sum(mut reader: Box<dyn Read>) -> (usize, u16) {
} }
// Report blocks read in terms of 1024-byte blocks. // Report blocks read in terms of 1024-byte blocks.
let blocks_read = div_ceil(bytes_read, 1024); let blocks_read = bytes_read.div_ceil(1024);
(blocks_read, checksum) (blocks_read, checksum)
} }
@ -66,7 +59,7 @@ fn sysv_sum(mut reader: Box<dyn Read>) -> (usize, u16) {
ret = (ret & 0xffff) + (ret >> 16); ret = (ret & 0xffff) + (ret >> 16);
// Report blocks read in terms of 512-byte blocks. // Report blocks read in terms of 512-byte blocks.
let blocks_read = div_ceil(bytes_read, 512); let blocks_read = bytes_read.div_ceil(512);
(blocks_read, ret as u16) (blocks_read, ret as u16)
} }

View file

@ -207,13 +207,6 @@ impl Digest for CRC {
} }
} }
// This can be replaced with usize::div_ceil once it is stabilized.
// This implementation approach is optimized for when `b` is a constant,
// particularly a power of two.
pub fn div_ceil(a: usize, b: usize) -> usize {
(a + b - 1) / b
}
pub struct BSD { pub struct BSD {
state: u16, state: u16,
} }