Skip to content

Commit

Permalink
fzf: fix matched range calculation in prefix_match
Browse files Browse the repository at this point in the history
  • Loading branch information
noib3 committed Nov 24, 2023
1 parent d09beb2 commit a1d1ddb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
9 changes: 2 additions & 7 deletions src/algos/fzf/fzf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,18 +293,13 @@ fn prefix_match<const RANGES: bool>(
let ignored_leading_spaces =
ignored_candidate_leading_spaces(pattern, candidate)?;

let mut match_byte_len = 0;

for (candidate_ch, pattern_ch) in candidate
.chars_from(ignored_leading_spaces)
.zip(pattern_chars.by_ref())
{
if !char_eq(pattern_ch, candidate_ch) {
return None;
}
if RANGES {
match_byte_len += candidate_ch.len_utf8();
}
}

if pattern_chars.next().is_some() {
Expand All @@ -313,7 +308,7 @@ fn prefix_match<const RANGES: bool>(

let matched_range = {
let start = ignored_leading_spaces;
let end = start + match_byte_len;
let end = start + pattern.char_len();
start..end
};

Expand All @@ -327,7 +322,7 @@ fn prefix_match<const RANGES: bool>(
);

if RANGES {
ranges.insert(matched_range);
ranges.insert(candidate.to_byte_range(matched_range));
}

Some(score)
Expand Down
23 changes: 18 additions & 5 deletions tests/fzf_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,6 @@ fn fzf_v1_score_1() {

let _ = fzf
.with_case_sensitivity(CaseSensitivity::Sensitive)
.with_matched_ranges(true)
.distance_and_ranges(parser.parse("ZZ"), "ӥZZZ", &mut ranges)
.unwrap();

Expand All @@ -243,7 +242,6 @@ fn fzf_v1_score_2() {

let mach = fzf
.with_case_sensitivity(CaseSensitivity::Sensitive)
.with_matched_ranges(true)
.distance(query, "\0");

assert!(mach.is_none());
Expand All @@ -259,7 +257,6 @@ fn fzf_v1_score_3() {

let mach = fzf
.with_case_sensitivity(CaseSensitivity::Sensitive)
.with_matched_ranges(true)
.distance(query, " ");

assert!(mach.is_none());
Expand All @@ -279,7 +276,6 @@ fn fzf_v1_score_4() {

let _ = fzf
.with_case_sensitivity(CaseSensitivity::Insensitive)
.with_matched_ranges(true)
.distance_and_ranges(query, candidate, &mut ranges)
.unwrap();

Expand All @@ -296,7 +292,6 @@ fn fzf_v1_score_5() {

let _ = fzf
.with_case_sensitivity(CaseSensitivity::Sensitive)
.with_matched_ranges(true)
.with_normalization(true)
.distance_and_ranges(
parser.parse("e !"),
Expand All @@ -307,3 +302,21 @@ fn fzf_v1_score_5() {

assert_eq!(ranges.as_slice(), [1..2, 7..9]);
}

#[test]
fn fzf_v1_score_6() {
let mut fzf = FzfV1::new();

let mut parser = FzfParser::new();

let mut ranges = norm::MatchedRanges::default();

let query = parser.parse("^e");

let _ = fzf
.with_case_sensitivity(CaseSensitivity::Insensitive)
.with_normalization(true)
.distance_and_ranges(query, "\u{364}", &mut ranges);

assert_eq!(ranges.as_slice(), [0..2]);
}

0 comments on commit a1d1ddb

Please sign in to comment.