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
This commit is contained in:
Riccardo Mazzarini 2023-12-17 17:27:10 +01:00 committed by GitHub
parent abf7454f4b
commit b41c989daf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 6 deletions

View File

@ -1,5 +1,13 @@
# Changelog # 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 # [0.2.1] - 2023-09-02
## Bugfixes ## Bugfixes

View File

@ -27,7 +27,7 @@ impl Matcher {
max_pos = i as u32; max_pos = i as u32;
max_score = score; max_score = score;
// can't get better than this // can't get better than this
if score >= self.config.bonus_boundary_white { if bonus >= self.config.bonus_boundary_white {
break; break;
} }
} }
@ -45,7 +45,7 @@ impl Matcher {
max_pos = i as u32; max_pos = i as u32;
max_score = score; max_score = score;
// can't get better than this // can't get better than this
if score >= self.config.bonus_boundary_white { if bonus >= self.config.bonus_boundary_white {
break; break;
} }
} }
@ -88,7 +88,7 @@ impl Matcher {
max_pos = i; max_pos = i;
max_score = score; max_score = score;
// can't get better than this // can't get better than this
if score >= self.config.bonus_boundary_white { if bonus >= self.config.bonus_boundary_white {
break; break;
} }
} }
@ -163,7 +163,7 @@ impl Matcher {
max_pos = i; max_pos = i;
max_score = score; max_score = score;
// can't get better than this // can't get better than this
if score >= self.config.bonus_boundary_white { if bonus >= self.config.bonus_boundary_white {
break; break;
} }
} }
@ -207,7 +207,7 @@ impl Matcher {
max_pos = i as u32; max_pos = i as u32;
max_score = score; max_score = score;
// can't get better than this // can't get better than this
if score >= self.config.bonus_boundary_white { if bonus >= self.config.bonus_boundary_white {
break; break;
} }
} }
@ -253,7 +253,7 @@ impl Matcher {
max_pos = i; max_pos = i;
max_score = score; max_score = score;
// can't get better than this // can't get better than this
if score >= self.config.bonus_boundary_white { if bonus >= self.config.bonus_boundary_white {
break; break;
} }
} }

View File

@ -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,
)],
);
}