File tree Expand file tree Collapse file tree 2 files changed +35
-35
lines changed Expand file tree Collapse file tree 2 files changed +35
-35
lines changed Original file line number Diff line number Diff line change @@ -590,6 +590,40 @@ pub pure fn split_str_nonempty(s: &a/str, sep: &b/str) -> ~[~str] {
590
590
result
591
591
}
592
592
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
+
593
627
/**
594
628
* Splits a string into a vector of the substrings separated by LF ('\n')
595
629
*/
Original file line number Diff line number Diff line change @@ -4830,44 +4830,10 @@ pub impl Resolver {
4830
4830
}
4831
4831
}
4832
4832
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
-
4867
4833
let mut smallest = 0;
4868
4834
for vec::eachi(maybes) |i, &other| {
4869
4835
4870
- values[i] = distance (name, other);
4836
+ values[i] = str::levdistance (name, other);
4871
4837
4872
4838
if values[i] <= values[smallest] {
4873
4839
smallest = i;
You can’t perform that action at this time.
0 commit comments