Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/uu/yes/locales/en-US.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ yes-usage = yes [STRING]...
# Error messages
yes-error-standard-output = standard output: { $error }
yes-error-invalid-utf8 = arguments contain invalid UTF-8
1 change: 0 additions & 1 deletion src/uu/yes/locales/fr-FR.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ yes-usage = yes [CHAÎNE]...
# Messages d'erreur
yes-error-standard-output = sortie standard : { $error }
yes-error-invalid-utf8 = les arguments contiennent de l'UTF-8 invalide
31 changes: 7 additions & 24 deletions src/uu/yes/src/yes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let matches = uucore::clap_localization::handle_clap_result(uu_app(), args)?;

#[allow(clippy::unwrap_used, reason = "clap provides 'y' by default")]
let buffer = args_into_buffer(matches.get_many::<OsString>("STRING").unwrap())?;
let buffer = args_into_buffer(matches.get_many::<OsString>("STRING").unwrap());

match exec(buffer) {
Ok(()) => Ok(()),
Expand Down Expand Up @@ -54,31 +54,14 @@ pub fn uu_app() -> Command {
}

/// create a buffer filled by words `i` separated by spaces.
#[allow(clippy::unnecessary_wraps, reason = "needed on some platforms")]
fn args_into_buffer<'a>(i: impl Iterator<Item = &'a OsString>) -> UResult<Vec<u8>> {
#[cfg(unix)]
use std::os::unix::ffi::OsStrExt;
#[cfg(target_os = "wasi")]
use std::os::wasi::ffi::OsStrExt;

fn args_into_buffer<'a>(i: impl Iterator<Item = &'a OsString>) -> Vec<u8> {
let mut buf = Vec::with_capacity(BUF_SIZE);
// On Unix (and wasi), OsStrs are just &[u8]'s underneath...
#[cfg(any(unix, target_os = "wasi"))]
for part in itertools::intersperse(i.map(|a| a.as_bytes()), b" ") {
for part in itertools::intersperse(i.map(|a| a.as_encoded_bytes()), b" ") {
buf.extend_from_slice(part);
}
// But, on Windows, we must hop through a String.
#[cfg(not(any(unix, target_os = "wasi")))]
for part in itertools::intersperse(i.map(|a| a.to_str()), Some(" ")) {
let bytes = part
.ok_or_else(|| USimpleError::new(1, translate!("yes-error-invalid-utf8")))?
.as_bytes();
buf.extend_from_slice(bytes);
}

buf.push(b'\n');

Ok(buf)
buf
}

/// Assumes buf holds a single output line forged from the command line arguments, copies it
Expand Down Expand Up @@ -183,19 +166,19 @@ mod tests {
fn test_args_into_buf() {
{
let default_args = ["y".into()];
let v = args_into_buffer(default_args.iter()).unwrap();
let v = args_into_buffer(default_args.iter());
assert_eq!(String::from_utf8(v).unwrap(), "y\n");
}

{
let args = ["foo".into()];
let v = args_into_buffer(args.iter()).unwrap();
let v = args_into_buffer(args.iter());
assert_eq!(String::from_utf8(v).unwrap(), "foo\n");
}

{
let args = ["foo".into(), "bar baz".into(), "qux".into()];
let v = args_into_buffer(args.iter()).unwrap();
let v = args_into_buffer(args.iter());
assert_eq!(String::from_utf8(v).unwrap(), "foo bar baz qux\n");
}
}
Expand Down
Loading