-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathcmp.rs
More file actions
1226 lines (1109 loc) · 35.5 KB
/
cmp.rs
File metadata and controls
1226 lines (1109 loc) · 35.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This file is part of the uutils diffutils package.
//
// For the full copyright and license information, please view the LICENSE-*
// files that was distributed with this source code.
use crate::utils::format_failure_to_read_input_file;
use std::env::{self, ArgsOs};
use std::ffi::OsString;
use std::io::{BufRead, BufReader, BufWriter, Read, Write};
use std::iter::Peekable;
use std::process::ExitCode;
use std::{cmp, fs, io};
#[cfg(not(target_os = "windows"))]
use std::os::fd::{AsRawFd, FromRawFd};
#[cfg(not(target_os = "windows"))]
use std::os::unix::fs::MetadataExt;
#[cfg(target_os = "windows")]
use std::os::windows::fs::MetadataExt;
/// for --bytes, so really large number limits can be expressed, like 1Y.
pub type BytesLimitU64 = u64;
// ignore initial is currently limited to u64, as take(skip) is used.
pub type SkipU64 = u64;
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct Params {
executable: OsString,
from: OsString,
to: OsString,
print_bytes: bool,
skip_a: Option<SkipU64>,
skip_b: Option<SkipU64>,
max_bytes: Option<BytesLimitU64>,
verbose: bool,
quiet: bool,
}
#[inline]
fn usage_string(executable: &str) -> String {
format!("Usage: {executable} <from> <to>")
}
#[cfg(not(target_os = "windows"))]
fn is_stdout_dev_null() -> bool {
let Ok(dev_null) = fs::metadata("/dev/null") else {
return false;
};
let stdout_fd = io::stdout().lock().as_raw_fd();
// SAFETY: we have exclusive access to stdout right now.
let stdout_file = unsafe { fs::File::from_raw_fd(stdout_fd) };
let Ok(stdout) = stdout_file.metadata() else {
return false;
};
let is_dev_null = stdout.dev() == dev_null.dev() && stdout.ino() == dev_null.ino();
// Don't let File close the fd. It's unfortunate that File doesn't have a leak_fd().
std::mem::forget(stdout_file);
is_dev_null
}
pub fn parse_params<I: Iterator<Item = OsString>>(mut opts: Peekable<I>) -> Result<Params, String> {
let Some(executable) = opts.next() else {
return Err("Usage: <exe> <from> <to>".to_string());
};
let executable_str = executable.to_string_lossy().to_string();
let parse_skip = |param: &str, skip_desc: &str| -> Result<SkipU64, String> {
let suffix_start = param
.find(|b: char| !b.is_ascii_digit())
.unwrap_or(param.len());
let mut num = match param[..suffix_start].parse::<SkipU64>() {
Ok(num) => num,
Err(e) if *e.kind() == std::num::IntErrorKind::PosOverflow => SkipU64::MAX,
Err(_) => {
return Err(format!(
"{executable_str}: invalid --ignore-initial value '{skip_desc}'"
))
}
};
if suffix_start != param.len() {
// Note that GNU cmp advertises supporting up to Y, but fails if you try
// to actually use anything beyond E.
let multiplier: SkipU64 = match ¶m[suffix_start..] {
"kB" => 1_000,
"K" => 1_024,
"MB" => 1_000_000,
"M" => 1_048_576,
"GB" => 1_000_000_000,
"G" => 1_073_741_824,
"TB" => 1_000_000_000_000,
"T" => 1_099_511_627_776,
"PB" => 1_000_000_000_000_000,
"P" => 1_125_899_906_842_624,
"EB" => 1_000_000_000_000_000_000,
"E" => 1_152_921_504_606_846_976,
// TODO setting usize:MAX does not mimic GNU cmp behavior, it should be an error.
"ZB" => SkipU64::MAX, // 1_000_000_000_000_000_000_000,
"Z" => SkipU64::MAX, // 1_180_591_620_717_411_303_424,
"YB" => SkipU64::MAX, // 1_000_000_000_000_000_000_000_000,
"Y" => SkipU64::MAX, // 1_208_925_819_614_629_174_706_176,
_ => {
return Err(format!(
"{executable_str}: invalid --ignore-initial value '{skip_desc}'"
));
}
};
num = match num.overflowing_mul(multiplier) {
(n, false) => n,
_ => SkipU64::MAX,
}
}
Ok(num)
};
let mut params = Params {
executable,
..Default::default()
};
let mut from = None;
let mut to = None;
let mut skip_pos1 = None;
let mut skip_pos2 = None;
while let Some(param) = opts.next() {
if param == "--" {
break;
}
if param == "-" {
if from.is_none() {
from = Some(param);
} else if to.is_none() {
to = Some(param);
} else {
return Err(usage_string(&executable_str));
}
continue;
}
if param == "-b" || param == "--print-bytes" {
params.print_bytes = true;
continue;
}
if param == "-l" || param == "--verbose" {
params.verbose = true;
continue;
}
if param == "-lb" || param == "-bl" {
params.print_bytes = true;
params.verbose = true;
continue;
}
let param_str = param.to_string_lossy().to_string();
if param == "-n" || param_str.starts_with("--bytes=") {
let max_bytes = if param == "-n" {
opts.next()
.ok_or_else(|| usage_string(&executable_str))?
.to_string_lossy()
.to_string()
} else {
let (_, arg) = param_str.split_once('=').unwrap();
arg.to_string()
};
let max_bytes = match max_bytes.parse::<BytesLimitU64>() {
Ok(num) => num,
// TODO limit to MAX is dangerous, this should become an error like in GNU cmp.
Err(e) if *e.kind() == std::num::IntErrorKind::PosOverflow => BytesLimitU64::MAX,
Err(_) => {
return Err(format!(
"{executable_str}: invalid --bytes value '{max_bytes}'"
))
}
};
params.max_bytes = Some(max_bytes);
continue;
}
if param == "-i" || param_str.starts_with("--ignore-initial=") {
let skip_desc = if param == "-i" {
opts.next()
.ok_or_else(|| usage_string(&executable_str))?
.to_string_lossy()
.to_string()
} else {
let (_, arg) = param_str.split_once('=').unwrap();
arg.to_string()
};
let (skip_a, skip_b) = if let Some((skip_a, skip_b)) = skip_desc.split_once(':') {
(
parse_skip(skip_a, &skip_desc)?,
parse_skip(skip_b, &skip_desc)?,
)
} else {
let skip = parse_skip(&skip_desc, &skip_desc)?;
(skip, skip)
};
params.skip_a = Some(skip_a);
params.skip_b = Some(skip_b);
continue;
}
if param == "-s" || param == "--quiet" || param == "--silent" {
params.quiet = true;
continue;
}
if param == "--help" {
println!("{}", usage_string(&executable_str));
std::process::exit(0);
}
if param_str.starts_with('-') {
return Err(format!("unrecognized option '{}'", param.to_string_lossy()));
}
if from.is_none() {
from = Some(param);
} else if to.is_none() {
to = Some(param);
} else if skip_pos1.is_none() {
skip_pos1 = Some(parse_skip(¶m_str, ¶m_str)?);
} else if skip_pos2.is_none() {
skip_pos2 = Some(parse_skip(¶m_str, ¶m_str)?);
} else {
return Err(usage_string(&executable_str));
}
}
// Do as GNU cmp, and completely disable printing if we are
// outputting to /dev/null.
#[cfg(not(target_os = "windows"))]
if is_stdout_dev_null() {
params.quiet = true;
params.verbose = false;
params.print_bytes = false;
}
if params.quiet && params.verbose {
return Err(format!(
"{executable_str}: options -l and -s are incompatible"
));
}
params.from = if let Some(from) = from {
from
} else if let Some(param) = opts.next() {
param
} else {
return Err(usage_string(&executable_str));
};
params.to = if let Some(to) = to {
to
} else if let Some(param) = opts.next() {
param
} else {
OsString::from("-")
};
// GNU cmp ignores positional skip arguments if -i is provided.
if params.skip_a.is_none() {
if skip_pos1.is_some() {
params.skip_a = skip_pos1;
} else if let Some(param) = opts.next() {
let param_str = param.to_string_lossy().to_string();
params.skip_a = Some(parse_skip(¶m_str, ¶m_str)?);
}
};
if params.skip_b.is_none() {
if skip_pos2.is_some() {
params.skip_b = skip_pos2;
} else if let Some(param) = opts.next() {
let param_str = param.to_string_lossy().to_string();
params.skip_b = Some(parse_skip(¶m_str, ¶m_str)?);
}
}
Ok(params)
}
fn prepare_reader(
path: &OsString,
skip: &Option<SkipU64>,
params: &Params,
) -> Result<Box<dyn BufRead>, String> {
let mut reader: Box<dyn BufRead> = if path == "-" {
Box::new(BufReader::new(io::stdin()))
} else {
match fs::File::open(path) {
Ok(file) => Box::new(BufReader::new(file)),
Err(e) => {
return Err(format_failure_to_read_input_file(
¶ms.executable,
path,
&e,
));
}
}
};
if let Some(skip) = skip {
// cast as u64 must remain, because value of IgnInit data type could be changed.
if let Err(e) = io::copy(&mut reader.by_ref().take(*skip), &mut io::sink()) {
return Err(format_failure_to_read_input_file(
¶ms.executable,
path,
&e,
));
}
}
Ok(reader)
}
#[derive(Debug)]
pub enum Cmp {
Equal,
Different,
}
pub fn cmp(params: &Params) -> Result<Cmp, String> {
let mut from = prepare_reader(¶ms.from, ¶ms.skip_a, params)?;
let mut to = prepare_reader(¶ms.to, ¶ms.skip_b, params)?;
let mut offset_width = params.max_bytes.unwrap_or(BytesLimitU64::MAX);
if let (Ok(a_meta), Ok(b_meta)) = (fs::metadata(¶ms.from), fs::metadata(¶ms.to)) {
#[cfg(not(target_os = "windows"))]
let (a_size, b_size) = (a_meta.size(), b_meta.size());
#[cfg(target_os = "windows")]
let (a_size, b_size) = (a_meta.file_size(), b_meta.file_size());
// If the files have different sizes, we already know they are not identical. If we have not
// been asked to show even the first difference, we can quit early.
if params.quiet && a_size != b_size {
return Ok(Cmp::Different);
}
let smaller = cmp::min(a_size, b_size) as BytesLimitU64;
offset_width = cmp::min(smaller, offset_width);
}
let offset_width = 1 + offset_width.checked_ilog10().unwrap_or(1) as usize;
// Capacity calc: at_byte width + 2 x 3-byte octal numbers + 2 x 4-byte value + 4 spaces
let mut output = Vec::<u8>::with_capacity(offset_width + 3 * 2 + 4 * 2 + 4);
let mut at_byte: BytesLimitU64 = 1;
let mut at_line: u64 = 1;
let mut start_of_line = true;
let mut stdout = BufWriter::new(io::stdout().lock());
let mut compare = Cmp::Equal;
loop {
// Fill up our buffers.
let from_buf = match from.fill_buf() {
Ok(buf) => buf,
Err(e) => {
return Err(format_failure_to_read_input_file(
¶ms.executable,
¶ms.from,
&e,
));
}
};
let to_buf = match to.fill_buf() {
Ok(buf) => buf,
Err(e) => {
return Err(format_failure_to_read_input_file(
¶ms.executable,
¶ms.to,
&e,
));
}
};
// Check for EOF conditions.
if from_buf.is_empty() && to_buf.is_empty() {
break;
}
if from_buf.is_empty() || to_buf.is_empty() {
let eof_on = if from_buf.is_empty() {
¶ms.from.to_string_lossy()
} else {
¶ms.to.to_string_lossy()
};
report_eof(at_byte, at_line, start_of_line, eof_on, params);
return Ok(Cmp::Different);
}
// Fast path - for long files in which almost all bytes are the same we
// can do a direct comparison to let the compiler optimize.
let consumed = std::cmp::min(from_buf.len(), to_buf.len());
if from_buf[..consumed] == to_buf[..consumed] {
let last = from_buf[..consumed].last().unwrap();
at_byte += consumed as BytesLimitU64;
at_line += (from_buf[..consumed].iter().filter(|&c| *c == b'\n').count()) as u64;
start_of_line = *last == b'\n';
if let Some(max_bytes) = params.max_bytes {
if at_byte > max_bytes {
break;
}
}
from.consume(consumed);
to.consume(consumed);
continue;
}
// Iterate over the buffers, the zip iterator will stop us as soon as the
// first one runs out.
for (&from_byte, &to_byte) in from_buf.iter().zip(to_buf.iter()) {
if from_byte != to_byte {
compare = Cmp::Different;
if params.verbose {
format_verbose_difference(
from_byte,
to_byte,
at_byte,
offset_width,
&mut output,
params,
)?;
stdout.write_all(output.as_slice()).map_err(|e| {
format!(
"{}: error printing output: {e}",
params.executable.to_string_lossy()
)
})?;
output.clear();
} else {
report_difference(from_byte, to_byte, at_byte, at_line, params);
return Ok(Cmp::Different);
}
}
start_of_line = from_byte == b'\n';
if start_of_line {
at_line += 1;
}
at_byte += 1;
if let Some(max_bytes) = params.max_bytes {
if at_byte > max_bytes {
break;
}
}
}
// Notify our readers about the bytes we went over.
from.consume(consumed);
to.consume(consumed);
}
Ok(compare)
}
// Exit codes are documented at
// https://www.gnu.org/software/diffutils/manual/html_node/Invoking-cmp.html
// An exit status of 0 means no differences were found,
// 1 means some differences were found,
// and 2 means trouble.
pub fn main(opts: Peekable<ArgsOs>) -> ExitCode {
let params = match parse_params(opts) {
Ok(param) => param,
Err(e) => {
eprintln!("{e}");
return ExitCode::from(2);
}
};
if params.from == "-" && params.to == "-"
|| same_file::is_same_file(¶ms.from, ¶ms.to).unwrap_or(false)
{
return ExitCode::SUCCESS;
}
match cmp(¶ms) {
Ok(Cmp::Equal) => ExitCode::SUCCESS,
Ok(Cmp::Different) => ExitCode::from(1),
Err(e) => {
if !params.quiet {
eprintln!("{e}");
}
ExitCode::from(2)
}
}
}
#[inline]
fn format_octal(byte: u8, buf: &mut [u8; 3]) -> &str {
*buf = [b' ', b' ', b'0'];
let mut num = byte;
let mut idx = 2; // Start at the last position in the buffer
// Generate octal digits
while num > 0 {
buf[idx] = b'0' + num % 8;
num /= 8;
idx = idx.saturating_sub(1);
}
// SAFETY: the operations we do above always land within ascii range.
unsafe { std::str::from_utf8_unchecked(&buf[..]) }
}
#[inline]
fn write_visible_byte(output: &mut Vec<u8>, byte: u8) -> usize {
match byte {
// Control characters: ^@, ^A, ..., ^_
0..=31 => {
output.push(b'^');
output.push(byte + 64);
2
}
// Printable ASCII (space through ~)
32..=126 => {
output.push(byte);
1
}
// DEL: ^?
127 => {
output.extend_from_slice(b"^?");
2
}
// High bytes with control equivalents: M-^@, M-^A, ..., M-^_
128..=159 => {
output.push(b'M');
output.push(b'-');
output.push(b'^');
output.push(byte - 64);
4
}
// High bytes: M-<space>, M-!, ..., M-~
160..=254 => {
output.push(b'M');
output.push(b'-');
output.push(byte - 128);
3
}
// Byte 255: M-^?
255 => {
output.extend_from_slice(b"M-^?");
4
}
}
}
/// Writes a byte in visible form with right-padding to 4 spaces.
#[inline]
fn write_visible_byte_padded(output: &mut Vec<u8>, byte: u8) {
const SPACES: &[u8] = b" ";
const WIDTH: usize = SPACES.len();
let display_width = write_visible_byte(output, byte);
// Add right-padding spaces
let padding = WIDTH.saturating_sub(display_width);
output.extend_from_slice(&SPACES[..padding]);
}
/// Formats a byte as a visible string (for non-performance-critical path)
#[inline]
fn format_visible_byte(byte: u8) -> String {
let mut result = Vec::with_capacity(4);
write_visible_byte(&mut result, byte);
// SAFETY: the checks and shifts in write_visible_byte match what cat and GNU
// cmp do to ensure characters fall inside the ascii range.
unsafe { String::from_utf8_unchecked(result) }
}
// This function has been optimized to not use the Rust fmt system, which
// leads to a massive speed up when processing large files: cuts the time
// for comparing 2 ~36MB completely different files in half on an M1 Max.
#[inline]
fn format_verbose_difference(
from_byte: u8,
to_byte: u8,
at_byte: BytesLimitU64,
offset_width: usize,
output: &mut Vec<u8>,
params: &Params,
) -> Result<(), String> {
assert!(!params.quiet);
let mut at_byte_buf = itoa::Buffer::new();
let mut from_oct = [0u8; 3]; // for octal conversions
let mut to_oct = [0u8; 3];
if params.print_bytes {
// "{:>width$} {:>3o} {:4} {:>3o} {}",
let at_byte_str = at_byte_buf.format(at_byte);
let at_byte_padding = offset_width.saturating_sub(at_byte_str.len());
for _ in 0..at_byte_padding {
output.push(b' ')
}
output.extend_from_slice(at_byte_str.as_bytes());
output.push(b' ');
output.extend_from_slice(format_octal(from_byte, &mut from_oct).as_bytes());
output.push(b' ');
write_visible_byte_padded(output, from_byte);
output.push(b' ');
output.extend_from_slice(format_octal(to_byte, &mut to_oct).as_bytes());
output.push(b' ');
write_visible_byte(output, to_byte);
output.push(b'\n');
} else {
// "{:>width$} {:>3o} {:>3o}"
let at_byte_str = at_byte_buf.format(at_byte);
let at_byte_padding = offset_width - at_byte_str.len();
for _ in 0..at_byte_padding {
output.push(b' ')
}
output.extend_from_slice(at_byte_str.as_bytes());
output.push(b' ');
output.extend_from_slice(format_octal(from_byte, &mut from_oct).as_bytes());
output.push(b' ');
output.extend_from_slice(format_octal(to_byte, &mut to_oct).as_bytes());
output.push(b'\n');
}
Ok(())
}
#[inline]
fn report_eof(
at_byte: BytesLimitU64,
at_line: u64,
start_of_line: bool,
eof_on: &str,
params: &Params,
) {
if params.quiet {
return;
}
if at_byte == 1 {
eprintln!(
"{}: EOF on '{}' which is empty",
params.executable.to_string_lossy(),
eof_on
);
} else if params.verbose {
eprintln!(
"{}: EOF on '{}' after byte {}",
params.executable.to_string_lossy(),
eof_on,
at_byte - 1,
);
} else if start_of_line {
eprintln!(
"{}: EOF on '{}' after byte {}, line {}",
params.executable.to_string_lossy(),
eof_on,
at_byte - 1,
at_line - 1
);
} else {
eprintln!(
"{}: EOF on '{}' after byte {}, in line {}",
params.executable.to_string_lossy(),
eof_on,
at_byte - 1,
at_line
);
}
}
fn is_posix_locale() -> bool {
let locale = if let Ok(locale) = env::var("LC_ALL") {
locale
} else if let Ok(locale) = env::var("LC_MESSAGES") {
locale
} else if let Ok(locale) = env::var("LANG") {
locale
} else {
"C".to_string()
};
locale == "C" || locale == "POSIX"
}
#[inline]
fn report_difference(
from_byte: u8,
to_byte: u8,
at_byte: BytesLimitU64,
at_line: u64,
params: &Params,
) {
if params.quiet {
return;
}
let term = if is_posix_locale() && !params.print_bytes {
"char"
} else {
"byte"
};
print!(
"{} {} differ: {term} {}, line {}",
¶ms.from.to_string_lossy(),
¶ms.to.to_string_lossy(),
at_byte,
at_line
);
if params.print_bytes {
let char_width = if to_byte >= 0x7F { 2 } else { 1 };
print!(
" is {:>3o} {:char_width$} {:>3o} {:char_width$}",
from_byte,
format_visible_byte(from_byte),
to_byte,
format_visible_byte(to_byte)
);
}
println!();
}
#[cfg(test)]
mod tests {
use super::*;
fn os(s: &str) -> OsString {
OsString::from(s)
}
#[test]
fn positional() {
assert_eq!(
Ok(Params {
executable: os("cmp"),
from: os("foo"),
to: os("bar"),
..Default::default()
}),
parse_params([os("cmp"), os("foo"), os("bar")].iter().cloned().peekable())
);
assert_eq!(
Ok(Params {
executable: os("cmp"),
from: os("foo"),
to: os("-"),
..Default::default()
}),
parse_params([os("cmp"), os("foo")].iter().cloned().peekable())
);
assert_eq!(
Ok(Params {
executable: os("cmp"),
from: os("foo"),
to: os("--help"),
..Default::default()
}),
parse_params(
[os("cmp"), os("foo"), os("--"), os("--help")]
.iter()
.cloned()
.peekable()
)
);
assert_eq!(
Ok(Params {
executable: os("cmp"),
from: os("foo"),
to: os("bar"),
skip_a: Some(1),
skip_b: None,
..Default::default()
}),
parse_params(
[os("cmp"), os("foo"), os("bar"), os("1")]
.iter()
.cloned()
.peekable()
)
);
assert_eq!(
Ok(Params {
executable: os("cmp"),
from: os("foo"),
to: os("bar"),
skip_a: Some(1),
skip_b: Some(SkipU64::MAX),
..Default::default()
}),
parse_params(
[os("cmp"), os("foo"), os("bar"), os("1"), os("2Y")]
.iter()
.cloned()
.peekable()
)
);
// Bad positional arguments.
assert_eq!(
Err("Usage: cmp <from> <to>".to_string()),
parse_params(
[os("cmp"), os("foo"), os("bar"), os("1"), os("2"), os("3")]
.iter()
.cloned()
.peekable()
)
);
assert_eq!(
Err("Usage: cmp <from> <to>".to_string()),
parse_params([os("cmp")].iter().cloned().peekable())
);
}
#[test]
fn execution_modes() {
let print_bytes = Params {
executable: os("cmp"),
from: os("foo"),
to: os("bar"),
print_bytes: true,
..Default::default()
};
assert_eq!(
Ok(print_bytes.clone()),
parse_params(
[os("cmp"), os("-b"), os("foo"), os("bar")]
.iter()
.cloned()
.peekable()
)
);
assert_eq!(
Ok(print_bytes),
parse_params(
[os("cmp"), os("--print-bytes"), os("foo"), os("bar")]
.iter()
.cloned()
.peekable()
)
);
let verbose = Params {
executable: os("cmp"),
from: os("foo"),
to: os("bar"),
verbose: true,
..Default::default()
};
assert_eq!(
Ok(verbose.clone()),
parse_params(
[os("cmp"), os("-l"), os("foo"), os("bar")]
.iter()
.cloned()
.peekable()
)
);
assert_eq!(
Ok(verbose),
parse_params(
[os("cmp"), os("--verbose"), os("foo"), os("bar")]
.iter()
.cloned()
.peekable()
)
);
let verbose_and_print_bytes = Params {
executable: os("cmp"),
from: os("foo"),
to: os("bar"),
print_bytes: true,
verbose: true,
..Default::default()
};
assert_eq!(
Ok(verbose_and_print_bytes.clone()),
parse_params(
[os("cmp"), os("-l"), os("-b"), os("foo"), os("bar")]
.iter()
.cloned()
.peekable()
)
);
assert_eq!(
Ok(verbose_and_print_bytes.clone()),
parse_params(
[os("cmp"), os("-lb"), os("foo"), os("bar")]
.iter()
.cloned()
.peekable()
)
);
assert_eq!(
Ok(verbose_and_print_bytes),
parse_params(
[os("cmp"), os("-bl"), os("foo"), os("bar")]
.iter()
.cloned()
.peekable()
)
);
assert_eq!(
Ok(Params {
executable: os("cmp"),
from: os("foo"),
to: os("bar"),
quiet: true,
..Default::default()
}),
parse_params(
[os("cmp"), os("-s"), os("foo"), os("bar")]
.iter()
.cloned()
.peekable()
)
);
// Some options do not mix.
assert_eq!(
Err("cmp: options -l and -s are incompatible".to_string()),
parse_params(
[os("cmp"), os("-l"), os("-s"), os("foo"), os("bar")]
.iter()
.cloned()
.peekable()
)
);
}
#[test]
fn max_bytes() {
let max_bytes = Params {
executable: os("cmp"),
from: os("foo"),
to: os("bar"),
max_bytes: Some(1),
..Default::default()
};
assert_eq!(
Ok(max_bytes.clone()),
parse_params(
[os("cmp"), os("-n"), os("1"), os("foo"), os("bar")]
.iter()
.cloned()
.peekable()
)
);
assert_eq!(
Ok(max_bytes),
parse_params(
[os("cmp"), os("--bytes=1"), os("foo"), os("bar")]
.iter()
.cloned()
.peekable()
)
);
assert_eq!(
Ok(Params {
executable: os("cmp"),
from: os("foo"),
to: os("bar"),
max_bytes: Some(BytesLimitU64::MAX),
..Default::default()
}),
parse_params(
[
os("cmp"),