Skip to content

Commit

Permalink
Take space into consideration while calculating the first matched ind…
Browse files Browse the repository at this point in the history
…ex (#3874)
  • Loading branch information
alekhyareddy28 committed Jun 1, 2020
1 parent 26aa972 commit a8d67a1
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/modules/launcher/Wox.Infrastructure/StringMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,18 @@ public MatchResult FuzzyMatch(string query, string stringToCompare, MatchOption
bool allSubstringsContainedInCompareString = true;

var indexList = new List<int>();
List<int> spaceIndices = new List<int>();

for (var compareStringIndex = 0; compareStringIndex < fullStringToCompareWithoutCase.Length; compareStringIndex++)
{

// To maintain a list of indices which correspond to spaces in the string to compare
// To populate the list only for the first query substring
if (fullStringToCompareWithoutCase[compareStringIndex].Equals(' ') && currentQuerySubstringIndex == 0)
{
spaceIndices.Add(compareStringIndex);
}

if (fullStringToCompareWithoutCase[compareStringIndex] != currentQuerySubstring[currentQuerySubstringCharacterIndex])
{
matchFoundInPreviousLoop = false;
Expand Down Expand Up @@ -147,14 +156,30 @@ public MatchResult FuzzyMatch(string query, string stringToCompare, MatchOption
// proceed to calculate score if every char or substring without whitespaces matched
if (allQuerySubstringsMatched)
{
var score = CalculateSearchScore(query, stringToCompare, firstMatchIndex, lastMatchIndex - firstMatchIndex, allSubstringsContainedInCompareString);
var nearestSpaceIndex = CalculateClosestSpaceIndex(spaceIndices, firstMatchIndex);
var score = CalculateSearchScore(query, stringToCompare, firstMatchIndex - nearestSpaceIndex - 1, lastMatchIndex - firstMatchIndex, allSubstringsContainedInCompareString);

return new MatchResult(true, UserSettingSearchPrecision, indexList, score);
}

return new MatchResult (false, UserSettingSearchPrecision);
}

// To get the index of the closest space which preceeds the first matching index
private int CalculateClosestSpaceIndex(List<int> spaceIndices, int firstMatchIndex)
{
if(spaceIndices.Count == 0)
{
return -1;
}
else
{
int? ind = spaceIndices.OrderBy(item => (firstMatchIndex - item)).Where(item => firstMatchIndex > item).FirstOrDefault();
int closestSpaceIndex = ind ?? -1;
return closestSpaceIndex;
}
}

private static bool AllPreviousCharsMatched(int startIndexToVerify, int currentQuerySubstringCharacterIndex,
string fullStringToCompareWithoutCase, string currentQuerySubstring)
{
Expand Down

0 comments on commit a8d67a1

Please sign in to comment.