Skip to content

Commit 0f40286

Browse files
committed
fix incorrect index calculation in hashmap
1 parent 2b8ec1c commit 0f40286

10 files changed

+70
-5
lines changed

CHANGELOG.rst

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
Changelog
22
---------
33

4+
[0.3.1] - 2023-11-29
5+
^^^^^^^^^^^^^^^^^^^^
6+
Fixed
7+
~~~~~
8+
* fixed crash inside hashmap lookup function leading to a crash in the
9+
Damerau-Levenshtein implementation
10+
411
[0.3.0] - 2023-11-27
5-
^^^^^^^^^^^^^^^^^^^^^
12+
^^^^^^^^^^^^^^^^^^^^
613
Previous versions only existed for testing purposed years ago. This is a complete
714
rewrite porting a subset of the features provided in the C++ implementation of
815
rapidfuzz. The remaining features will be added in later releases.
916

1017
Added
11-
~~~~~~~
18+
~~~~~
1219
* added implementations of the following string metrics:
1320

1421
* Levenshtein distance

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
description="rapid fuzzy string matching library"
33
name = "rapidfuzz"
4-
version = "0.3.0"
4+
version = "0.3.1"
55
authors = ["maxbachmann <[email protected]>"]
66
edition = "2021"
77
readme = "Readme.md"

src/details/growing_hashmap.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ where
100100
/// strategy to `CPython` and `Ruby`
101101
fn lookup(&self, key: u64) -> usize {
102102
let hash = key;
103-
let mut i = (hash & self.mask as u64) as usize;
103+
let mut i = hash as usize & self.mask as usize;
104104

105105
let map = self
106106
.map
@@ -113,7 +113,7 @@ where
113113

114114
let mut perturb = key;
115115
loop {
116-
i = (i * 5 + perturb as usize + 1) % 128;
116+
i = (i * 5 + perturb as usize + 1) & self.mask as usize;
117117

118118
if map[i].value == Default::default() || map[i].key == key {
119119
return i;

src/distance/damerau_levenshtein.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,4 +628,12 @@ mod tests {
628628
0.0001
629629
);
630630
}
631+
632+
#[test]
633+
fn unicode() {
634+
assert_eq!(
635+
Some(5),
636+
_test_distance("Иванко".chars(), "Петрунко".chars(), None, None)
637+
);
638+
}
631639
}

src/distance/indel.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,4 +790,12 @@ mod tests {
790790

791791
assert_eq!(Some(231), test_distance_ascii(s1, s2, None, None));
792792
}
793+
794+
#[test]
795+
fn unicode() {
796+
assert_eq!(
797+
Some(8),
798+
test_distance("Иванко".chars(), "Петрунко".chars(), None, None)
799+
);
800+
}
793801
}

src/distance/jaro.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,4 +1111,13 @@ mod tests {
11111111
}
11121112
}
11131113
}
1114+
1115+
#[test]
1116+
fn unicode() {
1117+
assert_delta!(
1118+
Some(0.375),
1119+
_test_distance("Иванко".chars(), "Петрунко".chars(), None, None),
1120+
0.0001
1121+
);
1122+
}
11141123
}

src/distance/jaro_winkler.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,4 +715,13 @@ mod tests {
715715
}
716716
}
717717
}
718+
719+
#[test]
720+
fn unicode() {
721+
assert_delta!(
722+
Some(0.375),
723+
_test_distance("Иванко".chars(), "Петрунко".chars(), None, None),
724+
0.0001
725+
);
726+
}
718727
}

src/distance/lcs_seq.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,4 +1208,12 @@ mod tests {
12081208
let b = "220";
12091209
assert_eq!(Some(1), test_similarity_ascii(a, b, None, None));
12101210
}
1211+
1212+
#[test]
1213+
fn unicode() {
1214+
assert_eq!(
1215+
Some(5),
1216+
test_distance("Иванко".chars(), "Петрунко".chars(), None, None)
1217+
);
1218+
}
12111219
}

src/distance/levenshtein.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2115,4 +2115,12 @@ mod tests {
21152115
distance(OCR_EXAMPLE1.iter(), OCR_EXAMPLE2.iter(), None, None, 0)
21162116
);
21172117
}
2118+
2119+
#[test]
2120+
fn unicode() {
2121+
assert_eq!(
2122+
Some(5),
2123+
_test_distance("Иванко".chars(), "Петрунко".chars(), None, None, None)
2124+
);
2125+
}
21182126
}

src/distance/osa.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,4 +606,12 @@ mod tests {
606606
let s2 = "b".to_string() + &filler + "AC" + &filler + "b";
607607
assert_eq!(Some(3), _test_distance_ascii(&s1, &s2, None, None));
608608
}
609+
610+
#[test]
611+
fn unicode() {
612+
assert_eq!(
613+
Some(5),
614+
_test_distance("Иванко".chars(), "Петрунко".chars(), None, None)
615+
);
616+
}
609617
}

0 commit comments

Comments
 (0)