Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Jaro and Jaro-Winkler panic #6

Merged
merged 1 commit into from
Aug 24, 2016
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
35 changes: 30 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

//! This library implements string similarity metrics. Currently includes
//! Hamming, Levenshtein, Jaro, and Jaro-Winkler.
//! This library implements string similarity metrics. Includes Hamming,
//! Levenshtein, Jaro, and Jaro-Winkler.

use std::char;
use std::cmp::{max, min};
Expand Down Expand Up @@ -48,9 +47,14 @@ pub fn jaro(a: &str, b: &str) -> f64 {

let a_len = a.chars().count();
let b_len = b.chars().count();
if a_len == 0 || b_len == 0 { return 0.0; }

let search_range = max(0, (max(a_len, b_len) / 2) - 1);
// The check for lengths of one here is to prevent integer overflow when
// calculating the search range.
if a_len == 0 || b_len == 0 || (a_len == 1 && b_len == 1) {
return 0.0;
}

let search_range = (max(a_len, b_len) / 2) - 1;

let mut b_consumed = Vec::with_capacity(b_len);
for _ in 0..b_len {
Expand Down Expand Up @@ -388,6 +392,21 @@ mod tests {
assert!((0.767 - jaro("dixon", "dicksonx")).abs() < 0.001);
}

#[test]
fn jaro_diff_one_character() {
assert_eq!(0.0, jaro("a", "b"));
}

#[test]
fn jaro_diff_one_and_two() {
assert!((0.83 - jaro("a", "ab")).abs() < 0.01);
}

#[test]
fn jaro_diff_two_and_one() {
assert!((0.83 - jaro("ab", "a")).abs() < 0.01);
}

#[test]
fn jaro_diff_no_transposition() {
assert!((0.822 - jaro("dwayne", "duane")).abs() < 0.001);
Expand Down Expand Up @@ -438,6 +457,11 @@ mod tests {
assert!((0.813 - jaro_winkler("dicksonx", "dixon")).abs() < 0.001);
}

#[test]
fn jaro_winkler_diff_one_character() {
assert_eq!(0.0, jaro_winkler("a", "b"));
}

#[test]
fn jaro_winkler_diff_no_transposition() {
assert!((0.840 - jaro_winkler("dwayne", "duane")).abs() < 0.001);
Expand Down Expand Up @@ -697,3 +721,4 @@ mod tests {
assert!(equal_float_vecs(result, expected));
}
}