Remove some allocations

This commit is contained in:
Nathan Fisher 2023-01-27 00:15:04 -05:00
parent 754f604603
commit fcc8abb67b
5 changed files with 86 additions and 85 deletions

View File

@ -24,11 +24,6 @@ impl Cmd for Base32 {
let Some(matches) = matches else { let Some(matches) = matches else {
return Err(io::Error::new(io::ErrorKind::Other, "No input").into()); return Err(io::Error::new(io::ErrorKind::Other, "No input").into());
}; };
let files: Vec<_> = match matches.get_many::<String>("INPUT") {
Some(c) => c.cloned().collect(),
None => vec![String::from("-")],
};
let len = files.len();
let color = match matches.get_one::<String>("color").map(String::as_str) { let color = match matches.get_one::<String>("color").map(String::as_str) {
Some("always") => ColorChoice::Always, Some("always") => ColorChoice::Always,
Some("ansi") => ColorChoice::AlwaysAnsi, Some("ansi") => ColorChoice::AlwaysAnsi,
@ -41,31 +36,36 @@ impl Cmd for Base32 {
} }
_ => ColorChoice::Never, _ => ColorChoice::Never,
}; };
for (index, file) in files.into_iter().enumerate() { if let Some(files) = matches.get_many::<String>("files") {
if { len > 1 || matches.get_flag("verbose") } && !matches.get_flag("QUIET") { let (len, _) = files.size_hint();
let mut stdout = StandardStream::stdout(color); for (index, file) in files.into_iter().enumerate() {
stdout.set_color(ColorSpec::new().set_fg(Some(Color::Green)))?; if { len > 1 || matches.get_flag("verbose") } && !matches.get_flag("QUIET") {
match index { let mut stdout = StandardStream::stdout(color);
0 => writeln!(stdout, "===> {file} <==="), stdout.set_color(ColorSpec::new().set_fg(Some(Color::Green)))?;
_ => writeln!(stdout, "\n===> {file} <==="), match index {
}?; 0 => writeln!(stdout, "===> {file} <==="),
stdout.reset()?; _ => writeln!(stdout, "\n===> {file} <==="),
} else if index > 0 { }?;
println!(); stdout.reset()?;
} } else if index > 0 {
let contents = get_contents(&file)?; println!();
if matches.get_flag("DECODE") { }
decode_base32(contents, matches.get_flag("IGNORE"))?; let contents = get_contents(&file)?;
} else { if matches.get_flag("DECODE") {
encode_base32( decode_base32(contents, matches.get_flag("IGNORE"))?;
&contents, } else {
match matches.get_one::<usize>("WRAP") { encode_base32(
Some(c) => *c, &contents,
None => { match matches.get_one::<usize>("WRAP") {
return Err(io::Error::new(io::ErrorKind::Other, "Invalid wrap").into()) Some(c) => *c,
} None => {
}, return Err(
); io::Error::new(io::ErrorKind::Other, "Invalid wrap").into()
)
}
},
);
}
} }
} }
Ok(()) Ok(())
@ -80,7 +80,8 @@ pub fn args() -> [Arg; 7] {
[ [
Arg::new("INPUT") Arg::new("INPUT")
.help("The input file to use") .help("The input file to use")
.num_args(1..), .num_args(1..)
.default_value("-"),
Arg::new("DECODE") Arg::new("DECODE")
.help("Decode rather than encode") .help("Decode rather than encode")
.short('d') .short('d')

View File

@ -23,11 +23,6 @@ impl Cmd for Base64 {
let Some(matches) = matches else { let Some(matches) = matches else {
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "No input"))); return Err(Box::new(io::Error::new(io::ErrorKind::Other, "No input")));
}; };
let files: Vec<_> = match matches.get_many::<String>("INPUT") {
Some(c) => c.cloned().collect(),
None => vec![String::from("-")],
};
let len = files.len();
let color = match matches.get_one::<String>("color").map(String::as_str) { let color = match matches.get_one::<String>("color").map(String::as_str) {
Some("always") => ColorChoice::Always, Some("always") => ColorChoice::Always,
Some("ansi") => ColorChoice::AlwaysAnsi, Some("ansi") => ColorChoice::AlwaysAnsi,
@ -40,29 +35,32 @@ impl Cmd for Base64 {
} }
_ => ColorChoice::Never, _ => ColorChoice::Never,
}; };
for (index, file) in files.into_iter().enumerate() { if let Some(files) = matches.get_many::<String>("INPUT") {
if { len > 1 || matches.get_flag("VERBOSE") } && !matches.get_flag("QUIET") { let (len, _) = files.size_hint();
let mut stdout = StandardStream::stdout(color); for (index, file) in files.enumerate() {
stdout.set_color(ColorSpec::new().set_fg(Some(Color::Green)))?; if { len > 1 || matches.get_flag("VERBOSE") } && !matches.get_flag("QUIET") {
match index { let mut stdout = StandardStream::stdout(color);
0 => writeln!(stdout, "===> {file} <==="), stdout.set_color(ColorSpec::new().set_fg(Some(Color::Green)))?;
_ => writeln!(stdout, "\n===> {file} <==="), match index {
}?; 0 => writeln!(stdout, "===> {file} <==="),
stdout.reset()?; _ => writeln!(stdout, "\n===> {file} <==="),
} else if index > 0 { }?;
println!(); stdout.reset()?;
} } else if index > 0 {
let contents = get_contents(&file)?; println!();
if matches.get_flag("DECODE") { }
decode_base64(contents, matches.get_flag("IGNORE"))?; let contents = get_contents(&file)?;
} else { if matches.get_flag("DECODE") {
encode_base64( decode_base64(contents, matches.get_flag("IGNORE"))?;
&contents, } else {
match matches.get_one("WRAP") { encode_base64(
Some(c) => *c, &contents,
None => 76, match matches.get_one("WRAP") {
}, Some(c) => *c,
); None => 76,
},
);
}
} }
} }
Ok(()) Ok(())

View File

@ -25,6 +25,7 @@ impl Cmd for Head {
.args([ .args([
Arg::new("FILES") Arg::new("FILES")
.help("The input file to use") .help("The input file to use")
.default_value("-")
.num_args(1..), .num_args(1..),
Arg::new("BYTES") Arg::new("BYTES")
.help("Count bytes instead of lines") .help("Count bytes instead of lines")
@ -44,7 +45,10 @@ impl Cmd for Head {
.allow_negative_numbers(false) .allow_negative_numbers(false)
.conflicts_with("num") .conflicts_with("num")
.value_parser(value_parser!(usize)), .value_parser(value_parser!(usize)),
args::color(), Arg::new("color")
.short('C')
.long("color")
.value_parser(["always", "ansi", "auto", "never"]),
Arg::new("num") Arg::new("num")
.short('1') .short('1')
.short_aliases(['2', '3', '4', '5', '6', '7', '8', '9']) .short_aliases(['2', '3', '4', '5', '6', '7', '8', '9'])
@ -82,12 +86,6 @@ impl Cmd for Head {
if let Some(l) = matches.get_one("LINES") { if let Some(l) = matches.get_one("LINES") {
lines = *l; lines = *l;
} }
let files = match matches.get_many::<String>("FILES") {
Some(c) => c.map(std::string::ToString::to_string).collect(),
None => vec!["-".to_string()],
};
let header =
!matches.get_flag("QUIET") && { files.len() > 1 || matches.get_flag("HEADER") };
let color = match matches.get_one::<String>("color").map(String::as_str) { let color = match matches.get_one::<String>("color").map(String::as_str) {
Some("always") => ColorChoice::Always, Some("always") => ColorChoice::Always,
Some("ansi") => ColorChoice::AlwaysAnsi, Some("ansi") => ColorChoice::AlwaysAnsi,
@ -100,11 +98,15 @@ impl Cmd for Head {
} }
_ => ColorChoice::Never, _ => ColorChoice::Never,
}; };
for (index, file) in files.into_iter().enumerate() { if let Some(files) = matches.get_many::<String>("FILES") {
if index == 1 && header { let (len, _) = files.size_hint();
println!(); let header = !matches.get_flag("QUIET") && { len > 1 || matches.get_flag("HEADER") };
for (index, file) in files.enumerate() {
if index == 1 && header {
println!();
}
head(file, lines, header, matches.get_flag("BYTES"), color)?;
} }
head(&file, lines, header, matches.get_flag("BYTES"), color)?;
} }
Ok(()) Ok(())
} }

View File

@ -16,6 +16,7 @@ mod date;
mod dd; mod dd;
mod dirname; mod dirname;
mod echo; mod echo;
mod expand;
mod factor; mod factor;
mod r#false; mod r#false;
mod fold; mod fold;

View File

@ -20,7 +20,8 @@ impl Cmd for Rev {
args::color(), args::color(),
Arg::new("file") Arg::new("file")
.help("if file is '-' read from stdin") .help("if file is '-' read from stdin")
.num_args(0..), .num_args(1..)
.default_value("-"),
]) ])
} }
@ -28,10 +29,6 @@ impl Cmd for Rev {
let Some(matches) = matches else { let Some(matches) = matches else {
return Err(Box::new(io::Error::new(ErrorKind::Other, "No input"))); return Err(Box::new(io::Error::new(ErrorKind::Other, "No input")));
}; };
let files: Vec<_> = match matches.get_many::<String>("file") {
Some(c) => c.cloned().collect(),
None => vec![String::from("-")],
};
let color = match matches.get_one::<String>("color").map(String::as_str) { let color = match matches.get_one::<String>("color").map(String::as_str) {
Some("always") => ColorChoice::Always, Some("always") => ColorChoice::Always,
Some("ansi") => ColorChoice::AlwaysAnsi, Some("ansi") => ColorChoice::AlwaysAnsi,
@ -44,17 +41,19 @@ impl Cmd for Rev {
} }
_ => ColorChoice::Never, _ => ColorChoice::Never,
}; };
for (index, file) in files.into_iter().enumerate() { if let Some(files) = matches.get_many::<String>("file") {
if matches.get_flag("header") { for (index, file) in files.enumerate() {
let mut stdout = StandardStream::stdout(color); if matches.get_flag("HEADER") {
stdout.set_color(ColorSpec::new().set_fg(Some(Color::Green)))?; let mut stdout = StandardStream::stdout(color);
match index { stdout.set_color(ColorSpec::new().set_fg(Some(Color::Green)))?;
0 => writeln!(stdout, "===> {file} <==="), match index {
_ => writeln!(stdout, "\n===> {file} <==="), 0 => writeln!(stdout, "===> {file} <==="),
}?; _ => writeln!(stdout, "\n===> {file} <==="),
stdout.reset()?; }?;
stdout.reset()?;
}
rev_file(&file)?;
} }
rev_file(&file)?;
} }
Ok(()) Ok(())
} }