mirror of
https://github.com/uutils/coreutils.git
synced 2025-12-23 08:47:37 +00:00
Merge pull request #8372 from drinkcat/basename-simplify
basename: Simply logic, do not trim path separators `/`
This commit is contained in:
commit
b6a219ab52
1 changed files with 2 additions and 11 deletions
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
use clap::{Arg, ArgAction, Command};
|
||||
use std::collections::HashMap;
|
||||
use std::path::{PathBuf, is_separator};
|
||||
use std::path::PathBuf;
|
||||
use uucore::display::Quotable;
|
||||
use uucore::error::{UResult, UUsageError};
|
||||
use uucore::format_usage;
|
||||
|
|
@ -119,17 +119,8 @@ pub fn uu_app() -> Command {
|
|||
}
|
||||
|
||||
fn basename(fullname: &str, suffix: &str) -> String {
|
||||
// Remove all platform-specific path separators from the end.
|
||||
let path = fullname.trim_end_matches(is_separator);
|
||||
|
||||
// If the path contained *only* suffix characters (for example, if
|
||||
// `fullname` were "///" and `suffix` were "/"), then `path` would
|
||||
// be left with the empty string. In that case, we set `path` to be
|
||||
// the original `fullname` to avoid returning the empty path.
|
||||
let path = if path.is_empty() { fullname } else { path };
|
||||
|
||||
// Convert to path buffer and get last path component
|
||||
let pb = PathBuf::from(path);
|
||||
let pb = PathBuf::from(fullname);
|
||||
|
||||
pb.components().next_back().map_or_else(String::new, |c| {
|
||||
let name = c.as_os_str().to_str().unwrap();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue