mirror of
https://gitee.com/openharmony/third_party_rust_strsim-rs
synced 2024-11-26 17:31:18 +00:00
Refactor Levenshtein implementation
This commit is contained in:
parent
f8d8c1fff8
commit
454834f300
13
src/lib.rs
13
src/lib.rs
@ -180,18 +180,19 @@ pub fn jaro_winkler_against_vec(a: &str, v: &[&str]) -> Vec<f64> {
|
||||
/// assert_eq!(3, levenshtein("kitten", "sitting"));
|
||||
/// ```
|
||||
pub fn levenshtein(a: &str, b: &str) -> usize {
|
||||
if a == b { return 0; }
|
||||
|
||||
let a_len = a.chars().count();
|
||||
let b_len = b.chars().count();
|
||||
if a == b { return 0; }
|
||||
else if a_len == 0 { return b_len; }
|
||||
else if b_len == 0 { return a_len; }
|
||||
|
||||
if a_len == 0 { return b_len; }
|
||||
if b_len == 0 { return a_len; }
|
||||
|
||||
let mut curr_distances = vec![0; b_len + 1];
|
||||
|
||||
let mut prev_distances: Vec<usize> = Vec::with_capacity(b_len + 1);
|
||||
let mut curr_distances: Vec<usize> = Vec::with_capacity(b_len + 1);
|
||||
|
||||
for i in 0..(b_len + 1) {
|
||||
prev_distances.push(i);
|
||||
curr_distances.push(0);
|
||||
}
|
||||
|
||||
for (i, a_char) in a.chars().enumerate() {
|
||||
|
Loading…
Reference in New Issue
Block a user