Merge pull request #8372 from drinkcat/basename-simplify

basename: Simply logic, do not trim path separators `/`
This commit is contained in:
Daniel Hofstetter 2025-07-23 11:09:43 +02:00 committed by GitHub
commit b6a219ab52
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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();