From a1d1ddbc65eb194e72752721fc847edd7d1b1324 Mon Sep 17 00:00:00 2001 From: Riccardo Mazzarini Date: Fri, 24 Nov 2023 13:05:54 +0100 Subject: [PATCH] fzf: fix matched range calculation in `prefix_match` --- src/algos/fzf/fzf.rs | 9 ++------- tests/fzf_v1.rs | 23 ++++++++++++++++++----- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/algos/fzf/fzf.rs b/src/algos/fzf/fzf.rs index c0ef4e6..d7bd907 100644 --- a/src/algos/fzf/fzf.rs +++ b/src/algos/fzf/fzf.rs @@ -293,8 +293,6 @@ fn prefix_match( 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()) @@ -302,9 +300,6 @@ fn prefix_match( if !char_eq(pattern_ch, candidate_ch) { return None; } - if RANGES { - match_byte_len += candidate_ch.len_utf8(); - } } if pattern_chars.next().is_some() { @@ -313,7 +308,7 @@ fn prefix_match( let matched_range = { let start = ignored_leading_spaces; - let end = start + match_byte_len; + let end = start + pattern.char_len(); start..end }; @@ -327,7 +322,7 @@ fn prefix_match( ); if RANGES { - ranges.insert(matched_range); + ranges.insert(candidate.to_byte_range(matched_range)); } Some(score) diff --git a/tests/fzf_v1.rs b/tests/fzf_v1.rs index 0ed5311..c255513 100644 --- a/tests/fzf_v1.rs +++ b/tests/fzf_v1.rs @@ -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(); @@ -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()); @@ -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()); @@ -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(); @@ -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 !"), @@ -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]); +}