mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
od: suppress duplicates
This commit is contained in:
parent
80386ef04f
commit
4770690823
2 changed files with 44 additions and 7 deletions
27
src/od/od.rs
27
src/od/od.rs
|
@ -237,14 +237,20 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
line_bytes = min_bytes;
|
line_bytes = min_bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
odfunc(line_bytes, &input_offset_base, &inputs, &formats[..])
|
let output_duplicates = matches.opt_present("v");
|
||||||
|
|
||||||
|
odfunc(line_bytes, &input_offset_base, &inputs, &formats[..], output_duplicates)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn odfunc(line_bytes: usize, input_offset_base: &Radix, fnames: &[InputSource], formats: &[OdFormat]) -> i32 {
|
fn odfunc(line_bytes: usize, input_offset_base: &Radix,
|
||||||
|
fnames: &[InputSource], formats: &[OdFormat], output_duplicates: bool) -> i32 {
|
||||||
|
|
||||||
let mut mf = MultifileReader::new(fnames);
|
let mut mf = MultifileReader::new(fnames);
|
||||||
let mut addr = 0;
|
let mut addr = 0;
|
||||||
let mut bytes: Vec<u8> = vec![b'\x00'; line_bytes];
|
let mut bytes: Vec<u8> = vec![b'\x00'; line_bytes];
|
||||||
|
let mut previous_bytes = Vec::<u8>::with_capacity(line_bytes);
|
||||||
|
let mut duplicate_line = false;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
// print each line data (or multi-format raster of several lines describing the same data).
|
// print each line data (or multi-format raster of several lines describing the same data).
|
||||||
|
|
||||||
|
@ -261,9 +267,20 @@ fn odfunc(line_bytes: usize, input_offset_base: &Radix, fnames: &[InputSource],
|
||||||
bytes[i] = 0;
|
bytes[i] = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
print_bytes(&bytes, n, &print_with_radix(input_offset_base, addr), formats);
|
if !output_duplicates && previous_bytes == bytes && n == line_bytes {
|
||||||
|
if !duplicate_line {
|
||||||
|
duplicate_line = true;
|
||||||
|
println!("*");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
duplicate_line = false;
|
||||||
|
previous_bytes.clone_from(&bytes);
|
||||||
|
|
||||||
|
print_bytes(&bytes, n, &print_with_radix(input_offset_base, addr), formats);
|
||||||
|
}
|
||||||
|
|
||||||
addr += n;
|
addr += n;
|
||||||
}
|
}
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
|
|
|
@ -266,7 +266,7 @@ fn test_width(){
|
||||||
0000010
|
0000010
|
||||||
");
|
");
|
||||||
|
|
||||||
let result = new_ucmd!().arg("-w4").run_piped_stdin(&input[..]);
|
let result = new_ucmd!().arg("-w4").arg("-v").run_piped_stdin(&input[..]);
|
||||||
|
|
||||||
assert_empty_stderr!(result);
|
assert_empty_stderr!(result);
|
||||||
assert!(result.success);
|
assert!(result.success);
|
||||||
|
@ -283,7 +283,7 @@ fn test_invalid_width(){
|
||||||
0000004
|
0000004
|
||||||
");
|
");
|
||||||
|
|
||||||
let result = new_ucmd!().arg("-w5").run_piped_stdin(&input[..]);
|
let result = new_ucmd!().arg("-w5").arg("-v").run_piped_stdin(&input[..]);
|
||||||
|
|
||||||
assert_eq!(result.stderr, "od: warning: invalid width 5; using 2 instead\n");
|
assert_eq!(result.stderr, "od: warning: invalid width 5; using 2 instead\n");
|
||||||
assert!(result.success);
|
assert!(result.success);
|
||||||
|
@ -306,3 +306,23 @@ fn test_width_without_value(){
|
||||||
assert!(result.success);
|
assert!(result.success);
|
||||||
assert_eq!(result.stdout, expected_output);
|
assert_eq!(result.stdout, expected_output);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_suppress_duplicates(){
|
||||||
|
|
||||||
|
let input = [0u8 ; 41];
|
||||||
|
let expected_output = unindent("
|
||||||
|
0000000 000000000000
|
||||||
|
0000 0000
|
||||||
|
*
|
||||||
|
0000050 000000000000
|
||||||
|
0000
|
||||||
|
0000051
|
||||||
|
");
|
||||||
|
|
||||||
|
let result = new_ucmd!().arg("-w4").arg("-O").arg("-x").run_piped_stdin(&input[..]);
|
||||||
|
|
||||||
|
assert_empty_stderr!(result);
|
||||||
|
assert!(result.success);
|
||||||
|
assert_eq!(result.stdout, expected_output);
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue