Skip to content

Commit

Permalink
fix short circuiting logic when doing exact matches (#32)
Browse files Browse the repository at this point in the history
* fix short circuiting logic in the `exact` module

* add tests for single char needles

* add bugfix to the CHANGELOG
  • Loading branch information
noib3 authored Dec 17, 2023
1 parent abf7454 commit b41c989
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 6 additions & 6 deletions matcher/src/exact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
}
Expand Down
30 changes: 30 additions & 0 deletions matcher/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)],
);
}

0 comments on commit b41c989

Please sign in to comment.