Skip to content

Commit f460c2a

Browse files
committed
Move levenshtein distance fn to core::str.
1 parent 0a0fcdb commit f460c2a

File tree

2 files changed

+35
-35
lines changed

2 files changed

+35
-35
lines changed

src/libcore/str.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,40 @@ pub pure fn split_str_nonempty(s: &a/str, sep: &b/str) -> ~[~str] {
590590
result
591591
}
592592

593+
/// Levenshtein Distance between two strings
594+
pub fn levdistance(s: &str, t: &str) -> uint {
595+
596+
let slen = str::len(s);
597+
let tlen = str::len(t);
598+
599+
if slen == 0 { return tlen; }
600+
if tlen == 0 { return slen; }
601+
602+
let mut dcol = vec::from_fn(tlen + 1, |x| x);
603+
604+
for str::each_chari(s) |i, sc| {
605+
606+
let mut current = i;
607+
dcol[0] = current + 1;
608+
609+
for str::each_chari(t) |j, tc| {
610+
611+
let mut next = dcol[j + 1];
612+
613+
if sc == tc {
614+
dcol[j + 1] = current;
615+
} else {
616+
dcol[j + 1] = ::cmp::min(current, next);
617+
dcol[j + 1] = ::cmp::min(dcol[j + 1], dcol[j]) + 1;
618+
}
619+
620+
current = next;
621+
}
622+
}
623+
624+
return dcol[tlen];
625+
}
626+
593627
/**
594628
* Splits a string into a vector of the substrings separated by LF ('\n')
595629
*/

src/librustc/middle/resolve.rs

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4830,44 +4830,10 @@ pub impl Resolver {
48304830
}
48314831
}
48324832
4833-
// Levenshtein Distance between two strings
4834-
fn distance(s: &str, t: &str) -> uint {
4835-
4836-
let slen = str::len(s);
4837-
let tlen = str::len(t);
4838-
4839-
if slen == 0 { return tlen; }
4840-
if tlen == 0 { return slen; }
4841-
4842-
let mut dcol = vec::from_fn(tlen + 1, |x| x);
4843-
4844-
for str::each_chari(s) |i, sc| {
4845-
4846-
let mut current = i;
4847-
dcol[0] = current + 1;
4848-
4849-
for str::each_chari(t) |j, tc| {
4850-
4851-
let mut next = dcol[j + 1];
4852-
4853-
if sc == tc {
4854-
dcol[j + 1] = current;
4855-
} else {
4856-
dcol[j + 1] = cmp::min(current, next);
4857-
dcol[j + 1] = cmp::min(dcol[j + 1], dcol[j]) + 1;
4858-
}
4859-
4860-
current = next;
4861-
}
4862-
}
4863-
4864-
return dcol[tlen];
4865-
}
4866-
48674833
let mut smallest = 0;
48684834
for vec::eachi(maybes) |i, &other| {
48694835
4870-
values[i] = distance(name, other);
4836+
values[i] = str::levdistance(name, other);
48714837
48724838
if values[i] <= values[smallest] {
48734839
smallest = i;

0 commit comments

Comments
 (0)