From dfe6bc531e7388759ad703459dfe96a91c0c4bee Mon Sep 17 00:00:00 2001 From: Mahmoud Al-Qudsi Date: Tue, 16 Oct 2018 21:45:04 -0500 Subject: [PATCH] Enable case-insensitive substring fuzzy matching Adds a new match mode for `string_fuzzy_match_t` that matches against a case-insensitive subsequence within a string, e.g. `LL` now (partially) matches against `hello`. This is implemented as a separate mode, given a lower priority of match than a same-case match (when present). Note that `fuzzy_match_subsequence_insertions_only` has purposely not been extended with a case-insensitive version as that would be a) unlikely to match often, and b) adding a second inefficient fuzzy search to something that's queried a lot. Perhaps `subsequence_insertions_only` can simply be changed to be a case-insensitive comparison in the future? Closes #1196. Affects #3978. --- src/common.cpp | 7 +++++++ src/common.h | 3 +++ 2 files changed, 10 insertions(+) diff --git a/src/common.cpp b/src/common.cpp index 2befc3f6f..a9037ca8f 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -1835,6 +1835,13 @@ string_fuzzy_match_t string_fuzzy_match_string(const wcstring &string, assert(match_against.size() >= string.size()); result.match_distance_first = match_against.size() - string.size(); result.match_distance_second = location; // prefer earlier matches + } else if (limit_type >= fuzzy_match_substring && + (location = ifind(match_against, string)) != wcstring::npos) { + // A case-insensitive version of the string is in the match against. + result.type = fuzzy_match_substring_case_insensitive; + assert(match_against.size() >= string.size()); + result.match_distance_first = match_against.size() - string.size(); + result.match_distance_second = location; // prefer earlier matches } else if (limit_type >= fuzzy_match_subsequence_insertions_only && subsequence_in_string(string, match_against)) { result.type = fuzzy_match_subsequence_insertions_only; diff --git a/src/common.h b/src/common.h index f8a5fd0ac..7f61a611d 100644 --- a/src/common.h +++ b/src/common.h @@ -399,6 +399,9 @@ enum fuzzy_match_type_t { // We match a substring of the string: OOBA matches FOOBAR. fuzzy_match_substring, + // We match a substring of the string: ooBA matches FOOBAR. + fuzzy_match_substring_case_insensitive, + // A subsequence match with insertions only: FBR matches FOOBAR. fuzzy_match_subsequence_insertions_only,