Skip to content

Commit

Permalink
fix substring matching Unicode haystacks (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalkuthe committed Dec 22, 2023
1 parent 1edf451 commit 34553f0
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

## Bugfixes

* avoid incorrect matches when searching for ASCII needles in a Unicode haystack
* correctly handle Unicode normalization when there are normalizable characters in the pattern, for example characters with umlauts
* when the needle is composed of a single char, return the score and index
of the best position instead of always returning the first matched character
Expand Down
3 changes: 3 additions & 0 deletions matcher/src/exact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,9 @@ impl Matcher {
}
}
}
if max_score == 0 {
return None;
}

let score = self.calculate_score::<INDICES, _, _>(
haystack,
Expand Down
11 changes: 2 additions & 9 deletions matcher/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,15 +496,8 @@ impl Matcher {
.substring_match_1_non_ascii::<INDICES>(haystack, needle, start, indices);
return Some(res);
}
let (start, end) = self.prefilter_non_ascii(haystack, needle_, false)?;
self.fuzzy_match_optimal::<INDICES, char, char>(
haystack,
needle,
start,
start + 1,
end,
indices,
)
let (start, _) = self.prefilter_non_ascii(haystack, needle_, false)?;
self.substring_match_non_ascii::<INDICES, _>(haystack, needle, start, indices)
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion matcher/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,13 @@ fn test_unicode() {
BONUS_BOUNDARY_WHITE * BONUS_FIRST_CHAR_MULTIPLIER - PENALTY_GAP_START,
),
],
)
);
assert_not_matches(
false,
false,
false,
&[("Flibbertigibbet / イタズラっ子たち", "lying")],
);
}

#[test]
Expand Down

0 comments on commit 34553f0

Please sign in to comment.