Skip to content

Commit 2f53d59

Browse files
committed
basename: Take in parameters as OSString
This will allow us to handle non-Unicode strings correctly. The logic needs to be updated, next.
1 parent cf3fece commit 2f53d59

1 file changed

Lines changed: 14 additions & 9 deletions

File tree

src/uu/basename/src/basename.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55

66
// spell-checker:ignore (ToDO) fullname
77

8+
use clap::builder::ValueParser;
89
use clap::{Arg, ArgAction, Command};
910
use std::collections::HashMap;
11+
use std::ffi::OsString;
1012
use std::path::PathBuf;
1113
use uucore::display::Quotable;
1214
use uucore::error::{UResult, UUsageError};
@@ -24,8 +26,6 @@ pub mod options {
2426

2527
#[uucore::main]
2628
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
27-
let args = args.collect_lossy();
28-
2929
//
3030
// Argument parsing
3131
//
@@ -34,7 +34,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
3434
let line_ending = LineEnding::from_zero_flag(matches.get_flag(options::ZERO));
3535

3636
let mut name_args = matches
37-
.get_many::<String>(options::NAME)
37+
.get_many::<OsString>(options::NAME)
3838
.unwrap_or_default()
3939
.collect::<Vec<_>>();
4040
if name_args.is_empty() {
@@ -43,18 +43,18 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
4343
get_message("basename-error-missing-operand"),
4444
));
4545
}
46-
let multiple_paths =
47-
matches.get_one::<String>(options::SUFFIX).is_some() || matches.get_flag(options::MULTIPLE);
46+
let multiple_paths = matches.get_one::<OsString>(options::SUFFIX).is_some()
47+
|| matches.get_flag(options::MULTIPLE);
4848
let suffix = if multiple_paths {
4949
matches
50-
.get_one::<String>(options::SUFFIX)
50+
.get_one::<OsString>(options::SUFFIX)
5151
.cloned()
5252
.unwrap_or_default()
5353
} else {
5454
// "simple format"
5555
match name_args.len() {
5656
0 => panic!("already checked"),
57-
1 => String::default(),
57+
1 => OsString::default(),
5858
2 => name_args.pop().unwrap().clone(),
5959
_ => {
6060
return Err(UUsageError::new(
@@ -96,6 +96,7 @@ pub fn uu_app() -> Command {
9696
.arg(
9797
Arg::new(options::NAME)
9898
.action(ArgAction::Append)
99+
.value_parser(ValueParser::os_string())
99100
.value_hint(clap::ValueHint::AnyPath)
100101
.hide(true)
101102
.trailing_var_arg(true),
@@ -105,6 +106,7 @@ pub fn uu_app() -> Command {
105106
.short('s')
106107
.long(options::SUFFIX)
107108
.value_name("SUFFIX")
109+
.value_parser(ValueParser::os_string())
108110
.help(get_message("basename-help-suffix"))
109111
.overrides_with(options::SUFFIX),
110112
)
@@ -118,13 +120,16 @@ pub fn uu_app() -> Command {
118120
)
119121
}
120122

121-
fn basename(fullname: &str, suffix: &str) -> String {
123+
fn basename(fullname: &OsString, suffix: &OsString) -> String {
124+
// TODO: Remove those 2 lines.
125+
let fullname = &fullname.to_string_lossy().to_string();
126+
let suffix = &suffix.to_string_lossy().to_string();
127+
122128
// Handle special case where path ends with /.
123129
if fullname.ends_with("/.") {
124130
return ".".to_string();
125131
}
126132

127-
// Convert to path buffer and get last path component
128133
let pb = PathBuf::from(fullname);
129134

130135
pb.components().next_back().map_or_else(String::new, |c| {

0 commit comments

Comments
 (0)