1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-27 19:17:43 +00:00

l10n: Move the prefix removal into a function

This commit is contained in:
Sylvestre Ledru 2025-06-02 22:38:54 +02:00
parent b904f36636
commit 00982c9374

View file

@ -68,6 +68,18 @@ fn get_canonical_util_name(util_name: &str) -> &str {
} }
} }
fn find_prefixed_util<'a>(
binary_name: &str,
mut util_keys: impl Iterator<Item = &'a str>,
) -> Option<&'a str> {
util_keys.find(|util| {
binary_name.ends_with(*util)
&& binary_name.len() > util.len() // Ensure there's actually a prefix
&& !binary_name[..binary_name.len() - (*util).len()]
.ends_with(char::is_alphanumeric)
})
}
#[allow(clippy::cognitive_complexity)] #[allow(clippy::cognitive_complexity)]
fn main() { fn main() {
uucore::panic::mute_sigpipe_panic(); uucore::panic::mute_sigpipe_panic();
@ -89,13 +101,9 @@ fn main() {
// binary name equals prefixed util name? // binary name equals prefixed util name?
// * prefix/stem may be any string ending in a non-alphanumeric character // * prefix/stem may be any string ending in a non-alphanumeric character
// For example, if the binary is named `uu_test`, it will match `test` as a utility. // For example, if the binary is named `uu_test`, it will match `test` as a utility.
let util_name = if let Some(util) = utils.keys().find(|util| { let util_name = if let Some(util) = find_prefixed_util(binary_as_util, utils.keys().copied()) {
binary_as_util.ends_with(*util)
&& !binary_as_util[..binary_as_util.len() - (*util).len()]
.ends_with(char::is_alphanumeric)
}) {
// prefixed util => replace 0th (aka, executable name) argument // prefixed util => replace 0th (aka, executable name) argument
Some(OsString::from(*util)) Some(OsString::from(util))
} else { } else {
// unmatched binary name => regard as multi-binary container and advance argument list // unmatched binary name => regard as multi-binary container and advance argument list
uucore::set_utility_is_second_arg(); uucore::set_utility_is_second_arg();