Skip to content

Commit

Permalink
fzf: pass mutable references MatchedRanges instead of returning a n…
Browse files Browse the repository at this point in the history
…ew one every time
  • Loading branch information
noib3 committed Nov 10, 2023
1 parent ac2fd4e commit 735da2f
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 117 deletions.
55 changes: 27 additions & 28 deletions src/algos/fzf/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ pub(super) fn calculate_score(
scheme: &Scheme,
char_eq: CharEq,
with_matched_ranges: bool,
) -> (Score, MatchedRanges) {
matched_ranges: &mut MatchedRanges,
) -> Score {
// TODO: docs
let mut is_in_gap = false;

Expand All @@ -39,8 +40,6 @@ pub(super) fn calculate_score(

let mut score: Score = 0;

let mut matched_ranges = MatchedRanges::default();

for (offset, candidate_ch) in candidate[range].char_indices() {
let ch_class = char_class(candidate_ch, scheme);

Expand Down Expand Up @@ -106,7 +105,7 @@ pub(super) fn calculate_score(
prev_class = ch_class;
}

(score, matched_ranges)
score
}

/// TODO: docs
Expand All @@ -117,7 +116,8 @@ pub(super) fn exact_match(
scheme: &Scheme,
char_eq: CharEq,
with_matched_ranges: bool,
) -> Option<(Score, MatchedRanges)> {
matched_ranges: &mut MatchedRanges,
) -> Option<Score> {
// TODO: docs
let mut best_bonus: i64 = -1;

Expand Down Expand Up @@ -190,22 +190,21 @@ pub(super) fn exact_match(
end - pattern.byte_len..end
};

let (score, _) = calculate_score(
let score = calculate_score(
pattern,
candidate,
matched_range.clone(),
scheme,
char_eq,
false,
matched_ranges,
);

let mut ranges = MatchedRanges::default();

if with_matched_ranges {
ranges.push(matched_range);
matched_ranges.push(matched_range);
}

Some((score, ranges))
Some(score)
}

/// TODO: docs
Expand All @@ -216,7 +215,8 @@ pub(super) fn prefix_match(
scheme: &Scheme,
char_eq: CharEq,
with_matched_ranges: bool,
) -> Option<(Score, MatchedRanges)> {
matched_ranges: &mut MatchedRanges,
) -> Option<Score> {
let mut pattern_chars = pattern.chars();

let ignored_leading_spaces =
Expand All @@ -237,22 +237,21 @@ pub(super) fn prefix_match(
let matched_range =
ignored_leading_spaces..ignored_leading_spaces + pattern.byte_len;

let (score, _) = calculate_score(
let score = calculate_score(
pattern,
candidate,
matched_range.clone(),
scheme,
char_eq,
false,
matched_ranges,
);

let mut ranges = MatchedRanges::default();

if with_matched_ranges {
ranges.push(matched_range);
matched_ranges.push(matched_range);
}

Some((score, ranges))
Some(score)
}

/// TODO: docs
Expand All @@ -263,7 +262,8 @@ pub(super) fn suffix_match(
scheme: &Scheme,
char_eq: CharEq,
with_matched_ranges: bool,
) -> Option<(Score, MatchedRanges)> {
matched_ranges: &mut MatchedRanges,
) -> Option<Score> {
let mut pattern_chars = pattern.chars().rev();

let up_to_ignored_spaces = candidate.len()
Expand All @@ -286,22 +286,21 @@ pub(super) fn suffix_match(
let matched_range =
up_to_ignored_spaces - pattern.byte_len..up_to_ignored_spaces;

let (score, _) = calculate_score(
let score = calculate_score(
pattern,
candidate,
matched_range.clone(),
scheme,
char_eq,
false,
matched_ranges,
);

let mut ranges = MatchedRanges::default();

if with_matched_ranges {
ranges.push(matched_range);
matched_ranges.push(matched_range);
}

Some((score, ranges))
Some(score)
}

/// TODO: docs
Expand All @@ -312,7 +311,8 @@ pub(super) fn equal_match(
scheme: &Scheme,
char_eq: CharEq,
with_matched_ranges: bool,
) -> Option<(Score, MatchedRanges)> {
matched_ranges: &mut MatchedRanges,
) -> Option<Score> {
let ignored_leading_spaces =
ignored_candidate_leading_spaces(pattern, candidate)?;

Expand Down Expand Up @@ -344,22 +344,21 @@ pub(super) fn equal_match(
return None;
}

let (score, _) = calculate_score(
let score = calculate_score(
pattern,
candidate,
matched_range.clone(),
scheme,
char_eq,
false,
matched_ranges,
);

let mut ranges = MatchedRanges::default();

if with_matched_ranges {
ranges.push(matched_range);
matched_ranges.push(matched_range);
}

Some((score, ranges))
Some(score)
}

/// TODO: docs
Expand Down
17 changes: 13 additions & 4 deletions src/algos/fzf/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ type FuzzyAlgo<T> = fn(
bool,
bool,
T,
) -> Option<(Score, MatchedRanges)>;
&mut MatchedRanges,
) -> Option<Score>;

/// TODO: docs.
#[derive(Clone, Copy)]
Expand Down Expand Up @@ -304,10 +305,13 @@ impl<'a> Pattern<'a> {
scheme: &Scheme,
char_eq: CharEq,
is_case_sensitive: bool,
with_matched_ranges: bool,
mut with_matched_ranges: bool,
extras: Extras,
matched_ranges: &mut MatchedRanges,
fuzzy_algo: FuzzyAlgo<Extras>,
) -> Option<(Score, MatchedRanges)> {
) -> Option<Score> {
with_matched_ranges &= !self.is_inverse;

let result = match self.match_type {
MatchType::Fuzzy => fuzzy_algo(
self,
Expand All @@ -317,6 +321,7 @@ impl<'a> Pattern<'a> {
is_case_sensitive,
with_matched_ranges,
extras,
matched_ranges,
),

MatchType::Exact => exact_match(
Expand All @@ -325,6 +330,7 @@ impl<'a> Pattern<'a> {
scheme,
char_eq,
with_matched_ranges,
matched_ranges,
),

MatchType::PrefixExact => prefix_match(
Expand All @@ -333,6 +339,7 @@ impl<'a> Pattern<'a> {
scheme,
char_eq,
with_matched_ranges,
matched_ranges,
),

MatchType::SuffixExact => suffix_match(
Expand All @@ -341,6 +348,7 @@ impl<'a> Pattern<'a> {
scheme,
char_eq,
with_matched_ranges,
matched_ranges,
),

MatchType::EqualExact => equal_match(
Expand All @@ -349,13 +357,14 @@ impl<'a> Pattern<'a> {
scheme,
char_eq,
with_matched_ranges,
matched_ranges,
),
};

match (result.is_some(), self.is_inverse) {
(true, false) => result,

(false, true) => Some((0, MatchedRanges::default())),
(false, true) => Some(0),

_ => None,
}
Expand Down
61 changes: 30 additions & 31 deletions src/algos/fzf/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ impl Metric for FzfV1 {

let is_candidate_ascii = candidate.is_ascii();

let mut matched_ranges = MatchedRanges::default();

let conditions = match query.search_mode {
SearchMode::NotExtended(pattern) => {
let is_case_sensitive = match self.case_sensitivity {
Expand All @@ -105,14 +107,15 @@ impl Metric for FzfV1 {
let char_eq =
utils::char_eq(is_case_sensitive, self.normalization);

let (score, matched_ranges) = fzf_v1(
let score = fzf_v1(
pattern,
candidate,
&self.scheme,
char_eq,
is_case_sensitive,
self.with_matched_ranges,
is_candidate_ascii,
&mut matched_ranges,
)?;

let distance = FzfDistance::from_score(score);
Expand All @@ -125,36 +128,30 @@ impl Metric for FzfV1 {

let mut total_score = 0;

let mut matched_ranges = MatchedRanges::default();

for condition in conditions {
let (score, ranges) =
condition.or_patterns().find_map(|pattern| {
let is_case_sensitive = match self.case_sensitivity {
CaseSensitivity::Sensitive => true,
CaseSensitivity::Insensitive => false,
CaseSensitivity::Smart => pattern.has_uppercase,
};

let char_eq =
utils::char_eq(is_case_sensitive, self.normalization);

pattern.score(
candidate,
&self.scheme,
char_eq,
is_case_sensitive,
self.with_matched_ranges,
is_candidate_ascii,
fzf_v1,
)
})?;
let score = condition.or_patterns().find_map(|pattern| {
let is_case_sensitive = match self.case_sensitivity {
CaseSensitivity::Sensitive => true,
CaseSensitivity::Insensitive => false,
CaseSensitivity::Smart => pattern.has_uppercase,
};

total_score += score;
let char_eq =
utils::char_eq(is_case_sensitive, self.normalization);

pattern.score(
candidate,
&self.scheme,
char_eq,
is_case_sensitive,
self.with_matched_ranges,
is_candidate_ascii,
&mut matched_ranges,
fzf_v1,
)
})?;

if self.with_matched_ranges {
matched_ranges.join(ranges);
}
total_score += score;
}

let distance = FzfDistance::from_score(total_score);
Expand All @@ -173,7 +170,8 @@ pub(super) fn fzf_v1(
is_case_sensitive: bool,
with_matched_ranges: bool,
is_candidate_ascii: bool,
) -> Option<(Score, MatchedRanges)> {
matched_ranges: &mut MatchedRanges,
) -> Option<Score> {
let range_forward = forward_pass(
pattern,
candidate,
Expand All @@ -192,16 +190,17 @@ pub(super) fn fzf_v1(

let range = range_forward.start + start_backward..range_forward.end;

let (score, matched_ranges) = calculate_score(
let score = calculate_score(
pattern,
candidate,
range,
scheme,
char_eq,
with_matched_ranges,
matched_ranges,
);

Some((score, matched_ranges))
Some(score)
}

/// TODO: docs
Expand Down
Loading

0 comments on commit 735da2f

Please sign in to comment.