Skip to content

Commit b1f0166

Browse files
author
Gunter Schmidt
committed
removed feature for 128 bit
u64 seems the max file size with Rust, which is an Exabyte. Renamed type to Skip as this seems preferred.
1 parent 2e9d395 commit b1f0166

1 file changed

Lines changed: 45 additions & 37 deletions

File tree

src/cmp.rs

Lines changed: 45 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,19 @@ use std::os::unix::fs::MetadataExt;
2121
use std::os::windows::fs::MetadataExt;
2222

2323
/// for --bytes, so really large number limits can be expressed, like 1Y.
24-
#[cfg(not(feature = "cmp_bytes_limit_128_bit"))]
25-
pub type Bytes = u64;
26-
#[cfg(feature = "cmp_bytes_limit_128_bit")]
27-
pub type Bytes = u128;
24+
pub type BytesLimitU64 = u64;
2825
// ignore initial is currently limited to u64, as take(skip) is used.
29-
pub type IgnInit = u64;
26+
pub type SkipU64 = u64;
3027

3128
#[derive(Clone, Debug, Default, Eq, PartialEq)]
3229
pub struct Params {
3330
executable: OsString,
3431
from: OsString,
3532
to: OsString,
3633
print_bytes: bool,
37-
skip_a: Option<IgnInit>,
38-
skip_b: Option<IgnInit>,
39-
max_bytes: Option<Bytes>,
34+
skip_a: Option<SkipU64>,
35+
skip_b: Option<SkipU64>,
36+
max_bytes: Option<BytesLimitU64>,
4037
verbose: bool,
4138
quiet: bool,
4239
}
@@ -74,13 +71,13 @@ pub fn parse_params<I: Iterator<Item = OsString>>(mut opts: Peekable<I>) -> Resu
7471
};
7572
let executable_str = executable.to_string_lossy().to_string();
7673

77-
let parse_skip = |param: &str, skip_desc: &str| -> Result<IgnInit, String> {
74+
let parse_skip = |param: &str, skip_desc: &str| -> Result<SkipU64, String> {
7875
let suffix_start = param
7976
.find(|b: char| !b.is_ascii_digit())
8077
.unwrap_or(param.len());
81-
let mut num = match param[..suffix_start].parse::<IgnInit>() {
78+
let mut num = match param[..suffix_start].parse::<SkipU64>() {
8279
Ok(num) => num,
83-
Err(e) if *e.kind() == std::num::IntErrorKind::PosOverflow => IgnInit::MAX,
80+
Err(e) if *e.kind() == std::num::IntErrorKind::PosOverflow => SkipU64::MAX,
8481
Err(_) => {
8582
return Err(format!(
8683
"{executable_str}: invalid --ignore-initial value '{skip_desc}'"
@@ -91,7 +88,7 @@ pub fn parse_params<I: Iterator<Item = OsString>>(mut opts: Peekable<I>) -> Resu
9188
if suffix_start != param.len() {
9289
// Note that GNU cmp advertises supporting up to Y, but fails if you try
9390
// to actually use anything beyond E.
94-
let multiplier: IgnInit = match &param[suffix_start..] {
91+
let multiplier: SkipU64 = match &param[suffix_start..] {
9592
"kB" => 1_000,
9693
"K" => 1_024,
9794
"MB" => 1_000_000,
@@ -105,10 +102,10 @@ pub fn parse_params<I: Iterator<Item = OsString>>(mut opts: Peekable<I>) -> Resu
105102
"EB" => 1_000_000_000_000_000_000,
106103
"E" => 1_152_921_504_606_846_976,
107104
// TODO setting usize:MAX does not mimic GNU cmp behavior, it should be an error.
108-
"ZB" => IgnInit::MAX, // 1_000_000_000_000_000_000_000,
109-
"Z" => IgnInit::MAX, // 1_180_591_620_717_411_303_424,
110-
"YB" => IgnInit::MAX, // 1_000_000_000_000_000_000_000_000,
111-
"Y" => IgnInit::MAX, // 1_208_925_819_614_629_174_706_176,
105+
"ZB" => SkipU64::MAX, // 1_000_000_000_000_000_000_000,
106+
"Z" => SkipU64::MAX, // 1_180_591_620_717_411_303_424,
107+
"YB" => SkipU64::MAX, // 1_000_000_000_000_000_000_000_000,
108+
"Y" => SkipU64::MAX, // 1_208_925_819_614_629_174_706_176,
112109
_ => {
113110
return Err(format!(
114111
"{executable_str}: invalid --ignore-initial value '{skip_desc}'"
@@ -118,7 +115,7 @@ pub fn parse_params<I: Iterator<Item = OsString>>(mut opts: Peekable<I>) -> Resu
118115

119116
num = match num.overflowing_mul(multiplier) {
120117
(n, false) => n,
121-
_ => IgnInit::MAX,
118+
_ => SkipU64::MAX,
122119
}
123120
}
124121

@@ -172,10 +169,10 @@ pub fn parse_params<I: Iterator<Item = OsString>>(mut opts: Peekable<I>) -> Resu
172169
let (_, arg) = param_str.split_once('=').unwrap();
173170
arg.to_string()
174171
};
175-
let max_bytes = match max_bytes.parse::<Bytes>() {
172+
let max_bytes = match max_bytes.parse::<BytesLimitU64>() {
176173
Ok(num) => num,
177174
// TODO limit to MAX is dangerous, this should become an error like in GNU cmp.
178-
Err(e) if *e.kind() == std::num::IntErrorKind::PosOverflow => Bytes::MAX,
175+
Err(e) if *e.kind() == std::num::IntErrorKind::PosOverflow => BytesLimitU64::MAX,
179176
Err(_) => {
180177
return Err(format!(
181178
"{executable_str}: invalid --bytes value '{max_bytes}'"
@@ -285,7 +282,7 @@ pub fn parse_params<I: Iterator<Item = OsString>>(mut opts: Peekable<I>) -> Resu
285282

286283
fn prepare_reader(
287284
path: &OsString,
288-
skip: &Option<IgnInit>,
285+
skip: &Option<SkipU64>,
289286
params: &Params,
290287
) -> Result<Box<dyn BufRead>, String> {
291288
let mut reader: Box<dyn BufRead> = if path == "-" {
@@ -305,8 +302,7 @@ fn prepare_reader(
305302

306303
if let Some(skip) = skip {
307304
// cast as u64 must remain, because value of IgnInit data type could be changed.
308-
#[allow(clippy::unnecessary_cast)]
309-
if let Err(e) = io::copy(&mut reader.by_ref().take(*skip as u64), &mut io::sink()) {
305+
if let Err(e) = io::copy(&mut reader.by_ref().take(*skip), &mut io::sink()) {
310306
return Err(format_failure_to_read_input_file(
311307
&params.executable,
312308
path,
@@ -328,7 +324,7 @@ pub fn cmp(params: &Params) -> Result<Cmp, String> {
328324
let mut from = prepare_reader(&params.from, &params.skip_a, params)?;
329325
let mut to = prepare_reader(&params.to, &params.skip_b, params)?;
330326

331-
let mut offset_width = params.max_bytes.unwrap_or(Bytes::MAX);
327+
let mut offset_width = params.max_bytes.unwrap_or(BytesLimitU64::MAX);
332328

333329
if let (Ok(a_meta), Ok(b_meta)) = (fs::metadata(&params.from), fs::metadata(&params.to)) {
334330
#[cfg(not(target_os = "windows"))]
@@ -343,7 +339,7 @@ pub fn cmp(params: &Params) -> Result<Cmp, String> {
343339
return Ok(Cmp::Different);
344340
}
345341

346-
let smaller = cmp::min(a_size, b_size) as Bytes;
342+
let smaller = cmp::min(a_size, b_size) as BytesLimitU64;
347343
offset_width = cmp::min(smaller, offset_width);
348344
}
349345

@@ -352,7 +348,7 @@ pub fn cmp(params: &Params) -> Result<Cmp, String> {
352348
// Capacity calc: at_byte width + 2 x 3-byte octal numbers + 2 x 4-byte value + 4 spaces
353349
let mut output = Vec::<u8>::with_capacity(offset_width + 3 * 2 + 4 * 2 + 4);
354350

355-
let mut at_byte: Bytes = 1;
351+
let mut at_byte: BytesLimitU64 = 1;
356352
let mut at_line: u64 = 1;
357353
let mut start_of_line = true;
358354
let mut stdout = BufWriter::new(io::stdout().lock());
@@ -403,7 +399,7 @@ pub fn cmp(params: &Params) -> Result<Cmp, String> {
403399
if from_buf[..consumed] == to_buf[..consumed] {
404400
let last = from_buf[..consumed].last().unwrap();
405401

406-
at_byte += consumed as Bytes;
402+
at_byte += consumed as BytesLimitU64;
407403
at_line += (from_buf[..consumed].iter().filter(|&c| *c == b'\n').count()) as u64;
408404

409405
start_of_line = *last == b'\n';
@@ -592,7 +588,7 @@ fn format_visible_byte(byte: u8) -> String {
592588
fn format_verbose_difference(
593589
from_byte: u8,
594590
to_byte: u8,
595-
at_byte: Bytes,
591+
at_byte: BytesLimitU64,
596592
offset_width: usize,
597593
output: &mut Vec<u8>,
598594
params: &Params,
@@ -657,7 +653,13 @@ fn format_verbose_difference(
657653
}
658654

659655
#[inline]
660-
fn report_eof(at_byte: Bytes, at_line: u64, start_of_line: bool, eof_on: &str, params: &Params) {
656+
fn report_eof(
657+
at_byte: BytesLimitU64,
658+
at_line: u64,
659+
start_of_line: bool,
660+
eof_on: &str,
661+
params: &Params,
662+
) {
661663
if params.quiet {
662664
return;
663665
}
@@ -709,7 +711,13 @@ fn is_posix_locale() -> bool {
709711
}
710712

711713
#[inline]
712-
fn report_difference(from_byte: u8, to_byte: u8, at_byte: Bytes, at_line: u64, params: &Params) {
714+
fn report_difference(
715+
from_byte: u8,
716+
to_byte: u8,
717+
at_byte: BytesLimitU64,
718+
at_line: u64,
719+
params: &Params,
720+
) {
713721
if params.quiet {
714722
return;
715723
}
@@ -806,7 +814,7 @@ mod tests {
806814
from: os("foo"),
807815
to: os("bar"),
808816
skip_a: Some(1),
809-
skip_b: Some(IgnInit::MAX),
817+
skip_b: Some(SkipU64::MAX),
810818
..Default::default()
811819
}),
812820
parse_params(
@@ -984,7 +992,7 @@ mod tests {
984992
executable: os("cmp"),
985993
from: os("foo"),
986994
to: os("bar"),
987-
max_bytes: Some(Bytes::MAX),
995+
max_bytes: Some(BytesLimitU64::MAX),
988996
..Default::default()
989997
}),
990998
parse_params(
@@ -1047,8 +1055,8 @@ mod tests {
10471055
executable: os("cmp"),
10481056
from: os("foo"),
10491057
to: os("bar"),
1050-
skip_a: Some(IgnInit::MAX),
1051-
skip_b: Some(IgnInit::MAX),
1058+
skip_a: Some(SkipU64::MAX),
1059+
skip_b: Some(SkipU64::MAX),
10521060
..Default::default()
10531061
}),
10541062
parse_params(
@@ -1119,12 +1127,12 @@ mod tests {
11191127
.enumerate()
11201128
{
11211129
let values = [
1122-
(1_000 as IgnInit)
1130+
(1_000 as SkipU64)
11231131
.checked_pow((i + 1) as u32)
1124-
.unwrap_or(IgnInit::MAX),
1125-
(1024 as IgnInit)
1132+
.unwrap_or(SkipU64::MAX),
1133+
(1024 as SkipU64)
11261134
.checked_pow((i + 1) as u32)
1127-
.unwrap_or(IgnInit::MAX),
1135+
.unwrap_or(SkipU64::MAX),
11281136
];
11291137
for (j, v) in values.iter().enumerate() {
11301138
assert_eq!(

0 commit comments

Comments
 (0)