Skip to content

Commit

Permalink
feat: support empty exclusion string
Browse files Browse the repository at this point in the history
  • Loading branch information
YC committed Jan 13, 2024
1 parent 46b9ad4 commit 16b350a
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ struct Args {
long = "exclude",
value_name = "exclude-characters",
value_delimiter = ',',
long_help = "Comma-delimited decimal values of characters to print.
long_help = "Comma-delimited decimal values of non-printable characters to print (empty string for none).
(9 is HT (tab), 10 is NL (newline), 13 is CR (carriage return), 32 is SP (space))",
num_args(1),
required = false,
Expand Down Expand Up @@ -188,6 +188,11 @@ fn parse_exclude(exclusions: Vec<String>) -> Result<[bool; 256], Box<dyn error::
// Initialize to false
let mut exclude: [bool; 256] = [false; 256];

// Don't print any non-printable characters
if exclusions.len() == 1 && exclusions.first().unwrap() == "" {
return Ok(exclude);
}

// Split by comma, parse into int, set index of exclude array
for exclusion in exclusions {
if let Ok(i) = str::parse::<u8>(&exclusion) {
Expand Down Expand Up @@ -368,7 +373,27 @@ mod cli {
.unwrap();

assert_eq!("", String::from_utf8(process.stdout).unwrap());
assert!(String::from_utf8(process.stderr).unwrap().contains("non-exist.file: No such file or directory"));
assert!(String::from_utf8(process.stderr)
.unwrap()
.contains("non-exist.file: No such file or directory"));
assert_eq!(1, process.status.code().unwrap());
}

#[test]
fn empty_exclusion() {
let program_path = get_program_path();

let mut process = Command::new(&program_path)
.args(["-x", ""])
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
.unwrap();
let stdin = process.stdin.as_mut().unwrap();
stdin.write_all(&[0, 10, 32, 48]).unwrap();
let output = process.wait_with_output().unwrap();

let expected = "(NUL)(LF)(SP)0";
assert_eq!(expected, String::from_utf8(output.stdout).unwrap());
}
}

0 comments on commit 16b350a

Please sign in to comment.