1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-31 04:57:45 +00:00

yes: switch to clap and try to decrease allocs

This commit is contained in:
Alex Lyon 2018-03-12 16:27:21 -07:00
parent 880a4973c1
commit 155fea53b2
3 changed files with 40 additions and 38 deletions

2
Cargo.lock generated
View file

@ -1928,7 +1928,7 @@ dependencies = [
name = "yes" name = "yes"
version = "0.0.1" version = "0.0.1"
dependencies = [ dependencies = [
"getopts 0.2.17 (registry+https://github.com/rust-lang/crates.io-index)", "clap 2.31.1 (registry+https://github.com/rust-lang/crates.io-index)",
"uucore 0.0.1", "uucore 0.0.1",
] ]

View file

@ -2,6 +2,7 @@
name = "yes" name = "yes"
version = "0.0.1" version = "0.0.1"
authors = [] authors = []
description = "Repeatedly output a line with all specified STRING(s), or 'y'."
build = "../../mkmain.rs" build = "../../mkmain.rs"
[lib] [lib]
@ -9,8 +10,11 @@ name = "uu_yes"
path = "yes.rs" path = "yes.rs"
[dependencies] [dependencies]
getopts = "0.2.14" clap = "2.31"
uucore = { path="../uucore" }
[dependencies.uucore]
path = "../uucore"
default-features = false
[[bin]] [[bin]]
name = "yes" name = "yes"

View file

@ -11,54 +11,50 @@
/* last synced with: yes (GNU coreutils) 8.13 */ /* last synced with: yes (GNU coreutils) 8.13 */
extern crate getopts; #[macro_use]
extern crate clap;
#[macro_use] #[macro_use]
extern crate uucore; extern crate uucore;
use getopts::Options; use clap::Arg;
use std::borrow::Cow;
use std::io::{self, Write};
static NAME: &'static str = "yes"; // force a re-build whenever Cargo.toml changes
static VERSION: &'static str = env!("CARGO_PKG_VERSION"); const _CARGO_TOML: &'static str = include_str!("Cargo.toml");
const BUF_SIZE: usize = 8192; const BUF_SIZE: usize = 8192;
pub fn uumain(args: Vec<String>) -> i32 { pub fn uumain(args: Vec<String>) -> i32 {
let mut opts = Options::new(); let app = app_from_crate!().arg(Arg::with_name("STRING").index(1).multiple(true));
opts.optflag("h", "help", "display this help and exit"); let matches = match app.get_matches_from_safe(args) {
opts.optflag("V", "version", "output version information and exit");
let matches = match opts.parse(&args[1..]) {
Ok(m) => m, Ok(m) => m,
Err(f) => crash!(1, "invalid options\n{}", f), Err(ref e)
}; if e.kind == clap::ErrorKind::HelpDisplayed
if matches.opt_present("help") { || e.kind == clap::ErrorKind::VersionDisplayed =>
println!("{} {}", NAME, VERSION); {
println!(""); println!("{}", e);
println!("Usage:"); return 0;
println!(" {0} [STRING]... [OPTION]...", NAME); }
println!(""); Err(f) => {
print!( show_error!("{}", f);
"{}", return 1;
opts.usage("Repeatedly output a line with all specified STRING(s), or 'y'.") }
);
return 0;
}
if matches.opt_present("version") {
println!("{} {}", NAME, VERSION);
return 0;
}
let string = if matches.free.is_empty() {
"y".to_owned()
} else {
matches.free.join(" ")
}; };
let mut multistring = string.clone(); let string = if let Some(values) = matches.values_of("STRING") {
let mut result = values.fold(String::new(), |res, s| res + s + " ");
result.pop();
Cow::from(result)
} else {
Cow::from("y")
};
let mut multistring = String::with_capacity(BUF_SIZE);
while multistring.len() < BUF_SIZE - string.len() - 1 { while multistring.len() < BUF_SIZE - string.len() - 1 {
multistring.push_str("\n");
multistring.push_str(&string); multistring.push_str(&string);
multistring.push_str("\n");
} }
exec(&multistring[..]); exec(&multistring[..]);
@ -67,7 +63,9 @@ pub fn uumain(args: Vec<String>) -> i32 {
} }
pub fn exec(string: &str) { pub fn exec(string: &str) {
let stdout_raw = io::stdout();
let mut stdout = stdout_raw.lock();
loop { loop {
println!("{}", string) writeln!(stdout, "{}", string).unwrap();
} }
} }