MCPcopy Create free account
hub / github.com/ChrisFeldmeier/OpenCodeRust / levenshtein

Function levenshtein

crates/opencode-tool/src/edit/replacers.rs:12–45  ·  view source on GitHub ↗
(a: &str, b: &str)

Source from the content-addressed store, hash-verified

10const MULTIPLE_CANDIDATES_SIMILARITY_THRESHOLD: f64 = 0.3;
11
12fn levenshtein(a: &str, b: &str) -> usize {
13 if a.is_empty() || b.is_empty() {
14 return a.len().max(b.len());
15 }
16
17 let a_chars: Vec<char> = a.chars().collect();
18 let b_chars: Vec<char> = b.chars().collect();
19 let a_len = a_chars.len();
20 let b_len = b_chars.len();
21
22 let mut matrix = vec![vec![0; b_len + 1]; a_len + 1];
23
24 for i in 0..=a_len {
25 matrix[i][0] = i;
26 }
27 for j in 0..=b_len {
28 matrix[0][j] = j;
29 }
30
31 for i in 1..=a_len {
32 for j in 1..=b_len {
33 let cost = if a_chars[i - 1] == b_chars[j - 1] {
34 0
35 } else {
36 1
37 };
38 matrix[i][j] = (matrix[i - 1][j] + 1)
39 .min(matrix[i][j - 1] + 1)
40 .min(matrix[i - 1][j - 1] + cost);
41 }
42 }
43
44 matrix[a_len][b_len]
45}
46
47#[derive(Debug, Clone)]
48pub struct Replacement {

Callers 1

findMethod · 0.85

Calls 1

is_emptyMethod · 0.80

Tested by

no test coverage detected