Skip to content

Commit

Permalink
fzf-v2: fix bug in matches
Browse files Browse the repository at this point in the history
  • Loading branch information
noib3 committed Nov 12, 2023
1 parent 4b2cfb9 commit 5514f0a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
38 changes: 17 additions & 21 deletions src/algos/fzf/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,10 @@ fn matches<'idx>(

let mut last_matched_idx = MatchedIdx::default();

let mut byte_offset;

let mut matched_char;

loop {
let query_char = pattern.char(query_char_idx);

(byte_offset, matched_char) = utils::find_first(
let (byte_offset, matched_char) = utils::find_first(
query_char,
candidate,
is_candidate_ascii,
Expand All @@ -283,30 +279,30 @@ fn matches<'idx>(
candidate.get_unchecked(byte_offset + matched_char_byte_len..)
};

last_matched_idx +=
MatchedIdx { byte_offset: matched_char_byte_len, char_offset: 1 };

if query_char_idx + 1 < pattern.char_len() {
last_matched_idx += MatchedIdx {
byte_offset: matched_char_byte_len,
char_offset: 1,
};
query_char_idx += 1;
} else {
break;
}
}

(byte_offset, matched_char) = utils::find_last(
pattern.char(query_char_idx),
candidate,
is_candidate_ascii,
is_case_sensitive,
char_eq,
)
.unwrap_or((0, matched_char));
let last_char_offset_inclusive = last_matched_idx.byte_offset
+ if let Some((byte_offset, matched_char)) = utils::find_last(
pattern.char(query_char_idx),
candidate,
is_candidate_ascii,
is_case_sensitive,
char_eq,
) {
byte_offset + matched_char.len_utf8()
} else {
0
};

Some((
matched_idxs,
last_matched_idx.byte_offset + byte_offset + matched_char.len_utf8(),
))
Some((matched_idxs, last_char_offset_inclusive))
}

/// TODO: docs
Expand Down
16 changes: 15 additions & 1 deletion tests/fzf_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ fn fzf_v2_score_3() {
let mach =
fzf.distance(parser.parse("\0\0"), "\0#B\0\u{364}\0\0").unwrap();

assert_eq!(mach.matched_ranges().sorted(), [3..4, 6..7]);
assert_eq!(mach.matched_ranges().sorted(), [6..8]);
}

#[test]
Expand All @@ -278,3 +278,17 @@ fn fzf_v2_score_4() {

assert_eq!(mach.matched_ranges(), [1..2, 7..9]);
}

#[test]
fn fzf_v2_score_5() {
let mut fzf = FzfV2::new()
.with_case_sensitivity(CaseSensitivity::Insensitive)
.with_matched_ranges(true)
.with_normalization(true);

let mut parser = FzfParser::new();

let mach = fzf.distance(parser.parse("E"), "\u{364}E").unwrap();

assert_eq!(mach.matched_ranges(), [0..2]);
}

0 comments on commit 5514f0a

Please sign in to comment.