mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 12:07:46 +00:00
yes: choose between throughput and latency at compile-time
This commit is contained in:
parent
4941604362
commit
948dbd324e
2 changed files with 25 additions and 11 deletions
|
@ -13,6 +13,10 @@ path = "yes.rs"
|
|||
clap = "2.31"
|
||||
uucore = { path = "../uucore" }
|
||||
|
||||
[features]
|
||||
latency = []
|
||||
default = []
|
||||
|
||||
[[bin]]
|
||||
name = "yes"
|
||||
path = "../../uumain.rs"
|
||||
|
|
|
@ -55,23 +55,33 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
|||
};
|
||||
|
||||
let mut buffer = [0; BUF_SIZE];
|
||||
let bytes = if string.len() < BUF_SIZE / 2 {
|
||||
let mut size = 0;
|
||||
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()
|
||||
};
|
||||
let bytes = prepare_buffer(&string, &mut buffer);
|
||||
|
||||
exec(bytes);
|
||||
|
||||
0
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "latency"))]
|
||||
fn prepare_buffer<'a>(input: &'a str, buffer: &'a mut [u8; BUF_SIZE]) -> &'a [u8] {
|
||||
if input.len() < BUF_SIZE / 2 {
|
||||
let mut size = 0;
|
||||
while size < BUF_SIZE - input.len() {
|
||||
let (_, right) = buffer.split_at_mut(size);
|
||||
right[..input.len()].copy_from_slice(input.as_bytes());
|
||||
size += input.len();
|
||||
}
|
||||
&buffer[..size]
|
||||
} else {
|
||||
input.as_bytes()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "latency")]
|
||||
fn prepare_buffer<'a>(input: &'a str, _buffer: &'a mut [u8; BUF_SIZE]) -> &'a [u8] {
|
||||
input.as_bytes()
|
||||
}
|
||||
|
||||
pub fn exec(bytes: &[u8]) {
|
||||
let stdout_raw = io::stdout();
|
||||
let mut stdout = stdout_raw.lock();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue