Skip to content
Closed
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
30 changes: 5 additions & 25 deletions src/uu/echo/src/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
use clap::builder::ValueParser;
use clap::{Arg, ArgAction, Command};
use std::env;
use std::ffi::{OsStr, OsString};
use std::ffi::OsString;
use std::io::{self, StdoutLock, Write};
use uucore::error::{UResult, USimpleError};
use uucore::error::UResult;
use uucore::format::{FormatChar, OctalParsing, parse_escape_only};
use uucore::{format_usage, help_about, help_section, help_usage};
use uucore::{format_usage, help_about, help_section, help_usage, os_str_as_bytes};

const ABOUT: &str = help_about!("echo.md");
const USAGE: &str = help_usage!("echo.md");
Expand Down Expand Up @@ -178,13 +178,9 @@ fn execute(
escaped: bool,
) -> UResult<()> {
for (i, input) in arguments_after_options.into_iter().enumerate() {
let Some(bytes) = bytes_from_os_string(input.as_os_str()) else {
return Err(USimpleError::new(
1,
"Non-UTF-8 arguments provided, but this platform does not support them",
));
};
let bytes = os_str_as_bytes(&input)?;

// Don't print a space before the first argument
if i > 0 {
stdout_lock.write_all(b" ")?;
}
Expand All @@ -206,19 +202,3 @@ fn execute(

Ok(())
}

fn bytes_from_os_string(input: &OsStr) -> Option<&[u8]> {
#[cfg(target_family = "unix")]
{
use std::os::unix::ffi::OsStrExt;

Some(input.as_bytes())
}

#[cfg(not(target_family = "unix"))]
{
// TODO
// Verify that this works correctly on these platforms
input.to_str().map(|st| st.as_bytes())
}
}
20 changes: 11 additions & 9 deletions src/uu/printf/src/printf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
use clap::{Arg, ArgAction, Command};
use std::ffi::OsString;
use std::io::stdout;
use std::ops::ControlFlow;
use uucore::error::{UResult, UUsageError};
Expand All @@ -19,21 +20,19 @@ mod options {
pub const FORMAT: &str = "FORMAT";
pub const ARGUMENT: &str = "ARGUMENT";
}

#[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let matches = uu_app().get_matches_from(args);

let format = matches
.get_one::<std::ffi::OsString>(options::FORMAT)
.get_one::<OsString>(options::FORMAT)
.ok_or_else(|| UUsageError::new(1, "missing operand"))?;
let format = os_str_as_bytes(format)?;

let values: Vec<_> = match matches.get_many::<std::ffi::OsString>(options::ARGUMENT) {
// FIXME: use os_str_as_bytes once FormatArgument supports Vec<u8>
let values: Vec<_> = match matches.get_many::<OsString>(options::ARGUMENT) {
Some(s) => s
.map(|os_string| {
FormatArgument::Unparsed(std::ffi::OsStr::to_string_lossy(os_string).to_string())
})
.map(|os_string| FormatArgument::Unparsed(os_string.to_owned()))
.collect(),
None => vec![],
};
Expand All @@ -59,7 +58,10 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let Some(FormatArgument::Unparsed(arg_str)) = args.peek_arg() else {
unreachable!("All args are transformed to Unparsed")
};
show_warning!("ignoring excess arguments, starting with '{arg_str}'");
show_warning!(
"ignoring excess arguments, starting with '{}'",
arg_str.to_string_lossy()
);
}
return Ok(());
}
Expand Down Expand Up @@ -98,10 +100,10 @@ pub fn uu_app() -> Command {
.help("Print version information")
.action(ArgAction::Version),
)
.arg(Arg::new(options::FORMAT).value_parser(clap::value_parser!(std::ffi::OsString)))
.arg(Arg::new(options::FORMAT).value_parser(clap::value_parser!(OsString)))
.arg(
Arg::new(options::ARGUMENT)
.action(ArgAction::Append)
.value_parser(clap::value_parser!(std::ffi::OsString)),
.value_parser(clap::value_parser!(OsString)),
)
}
2 changes: 1 addition & 1 deletion src/uucore/src/lib/features/checksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,7 @@ fn process_checksum_line(
cached_line_format: &mut Option<LineFormat>,
last_algo: &mut Option<String>,
) -> Result<(), LineCheckError> {
let line_bytes = os_str_as_bytes(line)?;
let line_bytes = os_str_as_bytes(line).map_err(|e| LineCheckError::UError(Box::new(e)))?;

// Early return on empty or commented lines.
if line.is_empty() || line_bytes.starts_with(b"#") {
Expand Down
12 changes: 12 additions & 0 deletions src/uucore/src/lib/features/extendedbigdecimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ impl From<f64> for ExtendedBigDecimal {
}
}

impl From<u8> for ExtendedBigDecimal {
fn from(val: u8) -> Self {
Self::BigDecimal(val.into())
}
}

impl From<u32> for ExtendedBigDecimal {
fn from(val: u32) -> Self {
Self::BigDecimal(val.into())
}
}

impl ExtendedBigDecimal {
pub fn zero() -> Self {
Self::BigDecimal(0.into())
Expand Down
Loading
Loading