Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,19 @@ capabilities = ["FileSystem", "Environment", "Process", "Network"]
}
}

fn levenshtein(a: &str, b: &str) -> usize {
let mut cache = vec![0; b.len() + 1];
for (i, val) in cache.iter_mut().enumerate() {
fn levenshtein(a: &str, b: &str, cache: &mut [usize]) -> usize {
let b_len = b.len();

// We only need bytes since commands are ascii
let a_bytes = a.as_bytes();
let b_bytes = b.as_bytes();

for (i, val) in cache[..=b_len].iter_mut().enumerate() {
*val = i;
}
for (i, ca) in a.chars().enumerate() {
for (i, &ca) in a_bytes.iter().enumerate() {
let mut temp = i + 1;
for (j, cb) in b.chars().enumerate() {
for (j, &cb) in b_bytes.iter().enumerate() {
let next = if ca == cb {
cache[j]
} else {
Expand All @@ -283,9 +288,9 @@ fn levenshtein(a: &str, b: &str) -> usize {
cache[j] = temp;
temp = next;
}
cache[b.len()] = temp;
cache[b_len] = temp;
}
cache[b.len()]
cache[b_len]
}

fn suggest_subcommand(unknown: &str) {
Expand Down Expand Up @@ -322,9 +327,10 @@ fn suggest_subcommand(unknown: &str) {
"self",
];

let mut cache = [0; 32];
let mut matches = Vec::new();
for cmd in SUBCOMMANDS {
let dist = levenshtein(unknown, cmd);
let dist = levenshtein(unknown, cmd, &mut cache);
if dist <= 3 {
matches.push(*cmd);
}
Expand Down
Loading