From b41c989daf90ff757bd7d678317ffbc8b248b7a4 Mon Sep 17 00:00:00 2001 From: Riccardo Mazzarini Date: Sun, 17 Dec 2023 17:27:10 +0100 Subject: [PATCH] fix short circuiting logic when doing exact matches (#32) * fix short circuiting logic in the `exact` module * add tests for single char needles * add bugfix to the CHANGELOG --- CHANGELOG.md | 8 ++++++++ matcher/src/exact.rs | 12 ++++++------ matcher/src/tests.rs | 30 ++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e4a3082..b20882d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # Changelog +# Unreleased + +## Bugfixes + +* 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 + in the haystack + # [0.2.1] - 2023-09-02 ## Bugfixes diff --git a/matcher/src/exact.rs b/matcher/src/exact.rs index 4fc17d4..4c7d5dd 100644 --- a/matcher/src/exact.rs +++ b/matcher/src/exact.rs @@ -27,7 +27,7 @@ impl Matcher { max_pos = i as u32; max_score = score; // can't get better than this - if score >= self.config.bonus_boundary_white { + if bonus >= self.config.bonus_boundary_white { break; } } @@ -45,7 +45,7 @@ impl Matcher { max_pos = i as u32; max_score = score; // can't get better than this - if score >= self.config.bonus_boundary_white { + if bonus >= self.config.bonus_boundary_white { break; } } @@ -88,7 +88,7 @@ impl Matcher { max_pos = i; max_score = score; // can't get better than this - if score >= self.config.bonus_boundary_white { + if bonus >= self.config.bonus_boundary_white { break; } } @@ -163,7 +163,7 @@ impl Matcher { max_pos = i; max_score = score; // can't get better than this - if score >= self.config.bonus_boundary_white { + if bonus >= self.config.bonus_boundary_white { break; } } @@ -207,7 +207,7 @@ impl Matcher { max_pos = i as u32; max_score = score; // can't get better than this - if score >= self.config.bonus_boundary_white { + if bonus >= self.config.bonus_boundary_white { break; } } @@ -253,7 +253,7 @@ impl Matcher { max_pos = i; max_score = score; // can't get better than this - if score >= self.config.bonus_boundary_white { + if bonus >= self.config.bonus_boundary_white { break; } } diff --git a/matcher/src/tests.rs b/matcher/src/tests.rs index 058b497..67f69c1 100644 --- a/matcher/src/tests.rs +++ b/matcher/src/tests.rs @@ -668,3 +668,33 @@ fn test_prefer_prefix() { ], ); } + +#[test] +fn test_single_char_needle() { + assert_matches( + &[FuzzyOptimal], + false, + false, + false, + false, + &[( + "foO", + "o", + &[2], + BONUS_FIRST_CHAR_MULTIPLIER * BONUS_CAMEL123, + )], + ); + assert_matches( + &[FuzzyOptimal], + false, + false, + false, + false, + &[( + "föÖ", + "ö", + &[2], + BONUS_FIRST_CHAR_MULTIPLIER * BONUS_CAMEL123, + )], + ); +}