From 617342ffb45c529ffb6d41fb12b9960fe031383e Mon Sep 17 00:00:00 2001 From: Riccardo Mazzarini Date: Sun, 12 Nov 2023 13:09:41 +0100 Subject: [PATCH] fzf: return a null score if pattern is empty --- src/algos/fzf/common.rs | 16 ++++++++++++++++ src/algos/fzf/v1.rs | 4 ++++ src/algos/fzf/v2.rs | 4 ++++ tests/fzf_v2.rs | 13 +++++++++++++ 4 files changed, 37 insertions(+) diff --git a/src/algos/fzf/common.rs b/src/algos/fzf/common.rs index 62f0a86..e089374 100644 --- a/src/algos/fzf/common.rs +++ b/src/algos/fzf/common.rs @@ -110,6 +110,10 @@ pub(super) fn exact_match( with_matched_ranges: bool, matched_ranges: &mut MatchedRanges, ) -> Option { + if pattern.is_empty() { + return Some(0); + } + // TODO: docs let mut best_bonus: i64 = -1; @@ -209,6 +213,10 @@ pub(super) fn prefix_match( with_matched_ranges: bool, matched_ranges: &mut MatchedRanges, ) -> Option { + if pattern.is_empty() { + return Some(0); + } + let mut pattern_chars = pattern.chars(); let ignored_leading_spaces = @@ -256,6 +264,10 @@ pub(super) fn suffix_match( with_matched_ranges: bool, matched_ranges: &mut MatchedRanges, ) -> Option { + if pattern.is_empty() { + return Some(0); + } + let mut pattern_chars = pattern.chars().rev(); let up_to_ignored_spaces = candidate.len() @@ -305,6 +317,10 @@ pub(super) fn equal_match( with_matched_ranges: bool, matched_ranges: &mut MatchedRanges, ) -> Option { + if pattern.is_empty() { + return Some(0); + } + let ignored_leading_spaces = ignored_candidate_leading_spaces(pattern, candidate)?; diff --git a/src/algos/fzf/v1.rs b/src/algos/fzf/v1.rs index 2bd804d..113b22d 100644 --- a/src/algos/fzf/v1.rs +++ b/src/algos/fzf/v1.rs @@ -172,6 +172,10 @@ pub(super) fn fzf_v1( is_candidate_ascii: bool, matched_ranges: &mut MatchedRanges, ) -> Option { + if pattern.is_empty() { + return Some(0); + } + let range_forward = forward_pass( pattern, candidate, diff --git a/src/algos/fzf/v2.rs b/src/algos/fzf/v2.rs index 4346a9d..971bfa6 100644 --- a/src/algos/fzf/v2.rs +++ b/src/algos/fzf/v2.rs @@ -173,6 +173,10 @@ pub(super) fn fzf_v2( (slab, is_candidate_ascii): (&mut V2Slab, bool), ranges: &mut MatchedRanges, ) -> Option { + if pattern.is_empty() { + return Some(0); + } + let (matches, last_match_offset) = matches( &mut slab.matched_indices, pattern, diff --git a/tests/fzf_v2.rs b/tests/fzf_v2.rs index 7bcfb29..a83a05b 100644 --- a/tests/fzf_v2.rs +++ b/tests/fzf_v2.rs @@ -237,3 +237,16 @@ fn fzf_v2_score_1() { assert_eq!(mach.matched_ranges().sorted(), [0..5]); } + +#[test] +fn fzf_v2_score_2() { + let mut fzf = FzfV2::new() + .with_case_sensitivity(CaseSensitivity::Sensitive) + .with_matched_ranges(true); + + let mut parser = FzfParser::new(); + + let mach = fzf.distance(parser.parse("!$"), "$$2"); + + assert!(mach.is_none()); +}