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,6 +36,8 @@ impl Cmd for Base32 {
} }
_ => ColorChoice::Never, _ => ColorChoice::Never,
}; };
if let Some(files) = matches.get_many::<String>("files") {
let (len, _) = files.size_hint();
for (index, file) in files.into_iter().enumerate() { for (index, file) in files.into_iter().enumerate() {
if { len > 1 || matches.get_flag("verbose") } && !matches.get_flag("QUIET") { if { len > 1 || matches.get_flag("verbose") } && !matches.get_flag("QUIET") {
let mut stdout = StandardStream::stdout(color); let mut stdout = StandardStream::stdout(color);
@ -62,12 +59,15 @@ impl Cmd for Base32 {
match matches.get_one::<usize>("WRAP") { match matches.get_one::<usize>("WRAP") {
Some(c) => *c, Some(c) => *c,
None => { None => {
return Err(io::Error::new(io::ErrorKind::Other, "Invalid wrap").into()) 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,7 +35,9 @@ 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") {
let (len, _) = files.size_hint();
for (index, file) in files.enumerate() {
if { len > 1 || matches.get_flag("VERBOSE") } && !matches.get_flag("QUIET") { if { len > 1 || matches.get_flag("VERBOSE") } && !matches.get_flag("QUIET") {
let mut stdout = StandardStream::stdout(color); let mut stdout = StandardStream::stdout(color);
stdout.set_color(ColorSpec::new().set_fg(Some(Color::Green)))?; stdout.set_color(ColorSpec::new().set_fg(Some(Color::Green)))?;
@ -65,6 +62,7 @@ impl Cmd for Base64 {
); );
} }
} }
}
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") {
let (len, _) = files.size_hint();
let header = !matches.get_flag("QUIET") && { len > 1 || matches.get_flag("HEADER") };
for (index, file) in files.enumerate() {
if index == 1 && header { if index == 1 && header {
println!(); 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,8 +41,9 @@ 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() {
if matches.get_flag("HEADER") {
let mut stdout = StandardStream::stdout(color); let mut stdout = StandardStream::stdout(color);
stdout.set_color(ColorSpec::new().set_fg(Some(Color::Green)))?; stdout.set_color(ColorSpec::new().set_fg(Some(Color::Green)))?;
match index { match index {
@ -56,6 +54,7 @@ impl Cmd for Rev {
} }
rev_file(&file)?; rev_file(&file)?;
} }
}
Ok(()) Ok(())
} }