Skip to content

Commit a7f6acf

Browse files
authored
Merge pull request #5879 from therealprof/features/suggestions-use-insertion-sort
Replace another sort_by call by a manual insertion with a binary_search
2 parents 26f23e4 + 4f448df commit a7f6acf

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

clap_builder/src/parser/features/suggestions.rs

+21-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
#[cfg(feature = "suggestions")]
2-
use std::cmp::Ordering;
3-
41
// Internal
52
use crate::builder::Command;
63

@@ -13,15 +10,29 @@ where
1310
T: AsRef<str>,
1411
I: IntoIterator<Item = T>,
1512
{
16-
let mut candidates: Vec<(f64, String)> = possible_values
17-
.into_iter()
13+
use std::cmp::Ordering;
14+
15+
let mut candidates: Vec<(f64, String)> = Vec::new();
16+
for pv in possible_values {
1817
// GH #4660: using `jaro` because `jaro_winkler` implementation in `strsim-rs` is wrong
1918
// causing strings with common prefix >=10 to be considered perfectly similar
20-
.map(|pv| (strsim::jaro(v, pv.as_ref()), pv.as_ref().to_owned()))
21-
// Confidence of 0.7 so that bar -> baz is suggested
22-
.filter(|(confidence, _)| *confidence > 0.7)
23-
.collect();
24-
candidates.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(Ordering::Equal));
19+
let confidence = strsim::jaro(v, pv.as_ref());
20+
21+
if confidence > 0.7 {
22+
let new_elem = (confidence, pv.as_ref().to_owned());
23+
let pos = candidates
24+
.binary_search_by(|probe| {
25+
if probe.0 > confidence {
26+
Ordering::Greater
27+
} else {
28+
Ordering::Less
29+
}
30+
})
31+
.unwrap_or_else(|e| e);
32+
candidates.insert(pos, new_elem);
33+
}
34+
}
35+
2536
candidates.into_iter().map(|(_, pv)| pv).collect()
2637
}
2738

0 commit comments

Comments
 (0)