Convert fuzzy match positions to byte offsets

This commit is contained in:
Max Brunsfeld 2021-05-19 23:07:16 -07:00 committed by Antonio Scandurra
parent 92b938f4a8
commit 4bc1b0fa6f

View file

@ -323,9 +323,16 @@ fn score_match(
let path_len = path.len() + prefix.len();
let mut cur_start = 0;
let mut byte_ix = 0;
let mut char_ix = 0;
for i in 0..query.len() {
match_positions[i] = best_position_matrix[i * path_len + cur_start];
cur_start = match_positions[i] + 1;
let match_char_ix = best_position_matrix[i * path_len + cur_start];
while char_ix < match_char_ix {
byte_ix += path[char_ix].len_utf8();
char_ix += 1;
}
cur_start = match_char_ix + 1;
match_positions[i] = byte_ix;
}
score
@ -550,6 +557,26 @@ mod tests {
);
}
#[test]
fn test_match_multibyte_path_entries() {
let paths = vec!["aαbβ/cγ", "αβγδ/bcde", "c1⃣2⃣3⃣/d4⃣5⃣6⃣/e7⃣8⃣9⃣/f", "/d/🆒/h"];
assert_eq!("1".len(), 7);
assert_eq!(
match_query("bcd", false, &paths),
vec![
("αβγδ/bcde", vec![9, 10, 11]),
("aαbβ/cγ", vec![3, 7, 10]),
]
);
assert_eq!(
match_query("cde", false, &paths),
vec![
("αβγδ/bcde", vec![10, 11, 12]),
("c1⃣2⃣3⃣/d4⃣5⃣6⃣/e7⃣8⃣9⃣/f", vec![0, 23, 46]),
]
);
}
fn match_query<'a>(
query: &str,
smart_case: bool,