From 00982c93749e23b8820f21dae1d25b6b5b57e865 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Mon, 2 Jun 2025 22:38:54 +0200 Subject: [PATCH] l10n: Move the prefix removal into a function --- src/bin/coreutils.rs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/bin/coreutils.rs b/src/bin/coreutils.rs index a910f8a16..5b4c734d5 100644 --- a/src/bin/coreutils.rs +++ b/src/bin/coreutils.rs @@ -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, +) -> 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)] fn main() { uucore::panic::mute_sigpipe_panic(); @@ -89,13 +101,9 @@ fn main() { // binary name equals prefixed util name? // * 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. - let util_name = if let Some(util) = utils.keys().find(|util| { - binary_as_util.ends_with(*util) - && !binary_as_util[..binary_as_util.len() - (*util).len()] - .ends_with(char::is_alphanumeric) - }) { + let util_name = if let Some(util) = find_prefixed_util(binary_as_util, utils.keys().copied()) { // prefixed util => replace 0th (aka, executable name) argument - Some(OsString::from(*util)) + Some(OsString::from(util)) } else { // unmatched binary name => regard as multi-binary container and advance argument list uucore::set_utility_is_second_arg();