Skip to content

Commit 1ffc813

Browse files
committed
refactor(sort): simplify legacy args preprocessing
Remove intermediate vector creation in preprocess_legacy_args and streamline the handling of legacy '+' prefixed arguments to improve efficiency and readability without altering functionality. Additionally, refactor uumain to directly index legacy warnings when present.
1 parent 82c53a4 commit 1ffc813

File tree

1 file changed

+35
-51
lines changed

1 file changed

+35
-51
lines changed

src/uu/sort/src/sort.rs

Lines changed: 35 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,22 +1272,9 @@ where
12721272
return (args.into_iter().map(Into::into).collect(), Vec::new());
12731273
}
12741274

1275-
let mut args_vec: Vec<OsString> = Vec::new();
1276-
let mut has_plus = false;
1277-
for arg in args {
1278-
let os_arg: OsString = arg.into();
1279-
if !has_plus && starts_with_plus(&os_arg) {
1280-
has_plus = true;
1281-
}
1282-
args_vec.push(os_arg);
1283-
}
1284-
if !has_plus {
1285-
return (args_vec, Vec::new());
1286-
}
1287-
12881275
let mut processed = Vec::new();
12891276
let mut legacy_warnings = Vec::new();
1290-
let mut iter = args_vec.into_iter().peekable();
1277+
let mut iter = args.into_iter().map(Into::into).peekable();
12911278

12921279
while let Some(arg) = iter.next() {
12931280
if arg == "--" {
@@ -1296,39 +1283,41 @@ where
12961283
break;
12971284
}
12981285

1299-
let as_str = arg.to_string_lossy();
1300-
if let Some(from_spec) = as_str.strip_prefix('+') {
1301-
if let Some(from) = parse_legacy_part(from_spec) {
1302-
let mut to_part = None;
1303-
1304-
let next_candidate = iter.peek().map(|next| next.to_string_lossy().to_string());
1305-
1306-
if let Some(next_str) = next_candidate {
1307-
if let Some(stripped) = next_str.strip_prefix('-') {
1308-
if stripped.starts_with(|c: char| c.is_ascii_digit()) {
1309-
let next_arg = iter.next().unwrap();
1310-
if let Some(parsed) = parse_legacy_part(stripped) {
1311-
to_part = Some(parsed);
1312-
} else {
1313-
processed.push(arg);
1314-
processed.push(next_arg);
1315-
continue;
1286+
if starts_with_plus(&arg) {
1287+
let as_str = arg.to_string_lossy();
1288+
if let Some(from_spec) = as_str.strip_prefix('+') {
1289+
if let Some(from) = parse_legacy_part(from_spec) {
1290+
let mut to_part = None;
1291+
1292+
let next_candidate = iter.peek().map(|next| next.to_string_lossy().to_string());
1293+
1294+
if let Some(next_str) = next_candidate {
1295+
if let Some(stripped) = next_str.strip_prefix('-') {
1296+
if stripped.starts_with(|c: char| c.is_ascii_digit()) {
1297+
let next_arg = iter.next().unwrap();
1298+
if let Some(parsed) = parse_legacy_part(stripped) {
1299+
to_part = Some(parsed);
1300+
} else {
1301+
processed.push(arg);
1302+
processed.push(next_arg);
1303+
continue;
1304+
}
13161305
}
13171306
}
13181307
}
1319-
}
13201308

1321-
let keydef = legacy_key_to_k(&from, to_part.as_ref());
1322-
let arg_index = processed.len();
1323-
legacy_warnings.push(LegacyKeyWarning {
1324-
arg_index,
1325-
key_index: None,
1326-
from_field: from.field,
1327-
to_field: to_part.as_ref().map(|p| p.field),
1328-
to_char: to_part.as_ref().map(|p| p.char_pos),
1329-
});
1330-
processed.push(OsString::from(format!("-k{keydef}")));
1331-
continue;
1309+
let keydef = legacy_key_to_k(&from, to_part.as_ref());
1310+
let arg_index = processed.len();
1311+
legacy_warnings.push(LegacyKeyWarning {
1312+
arg_index,
1313+
key_index: None,
1314+
from_field: from.field,
1315+
to_field: to_part.as_ref().map(|p| p.field),
1316+
to_char: to_part.as_ref().map(|p| p.char_pos),
1317+
});
1318+
processed.push(OsString::from(format!("-k{keydef}")));
1319+
continue;
1320+
}
13321321
}
13331322
}
13341323

@@ -1647,11 +1636,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
16471636
let mut settings = GlobalSettings::default();
16481637

16491638
let (processed_args, mut legacy_warnings) = preprocess_legacy_args(args);
1650-
let processed_args_for_debug = if legacy_warnings.is_empty() {
1651-
None
1652-
} else {
1653-
Some(processed_args.clone())
1654-
};
1639+
if !legacy_warnings.is_empty() {
1640+
index_legacy_warnings(&processed_args, &mut legacy_warnings);
1641+
}
16551642
let matches =
16561643
uucore::clap_localization::handle_clap_result_with_exit_code(uu_app(), processed_args, 2)?;
16571644

@@ -1943,9 +1930,6 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
19431930
let output = Output::new(matches.get_one::<OsString>(options::OUTPUT))?;
19441931

19451932
if settings.debug {
1946-
if let Some(ref processed) = processed_args_for_debug {
1947-
index_legacy_warnings(processed, &mut legacy_warnings);
1948-
}
19491933
let global_flags = GlobalOptionFlags::from_matches(&matches);
19501934
emit_debug_warnings(&settings, &global_flags, &legacy_warnings);
19511935
}

0 commit comments

Comments
 (0)