1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-08-01 21:47:46 +00:00

yes: match the speed of GNU yes (on my machine) and remove allocs

This commit is contained in:
Alex Lyon 2018-03-12 21:14:16 -07:00
parent bd557c87fb
commit f359507b58

View file

@ -46,26 +46,34 @@ pub fn uumain(args: Vec<String>) -> i32 {
let string = if let Some(values) = matches.values_of("STRING") { let string = if let Some(values) = matches.values_of("STRING") {
let mut result = values.fold(String::new(), |res, s| res + s + " "); let mut result = values.fold(String::new(), |res, s| res + s + " ");
result.pop(); result.pop();
result.push('\n');
Cow::from(result) Cow::from(result)
} else { } else {
Cow::from("y") Cow::from("y\n")
}; };
let mut multistring = String::with_capacity(BUF_SIZE); let mut buffer = [0; BUF_SIZE];
while multistring.len() < BUF_SIZE - string.len() - 1 { let bytes = if string.len() < BUF_SIZE / 2 {
multistring.push_str(&string); let mut size = 0;
multistring.push_str("\n"); while size < BUF_SIZE - string.len() {
} let (_, right) = buffer.split_at_mut(size);
right[..string.len()].copy_from_slice(string.as_bytes());
size += string.len();
}
&buffer[..size]
} else {
string.as_bytes()
};
exec(&multistring[..]); exec(bytes);
0 0
} }
pub fn exec(string: &str) { pub fn exec(bytes: &[u8]) {
let stdout_raw = io::stdout(); let stdout_raw = io::stdout();
let mut stdout = stdout_raw.lock(); let mut stdout = stdout_raw.lock();
loop { loop {
writeln!(stdout, "{}", string).unwrap(); stdout.write_all(bytes).unwrap();
} }
} }