Skip to content

Commit 30c37f0

Browse files
alekhyareddy28jjw24
authored andcommitted
Take space into consideration while calculating the first matched index (#3874)
1 parent bc8a1f6 commit 30c37f0

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

Flow.Launcher.Infrastructure/StringMatcher.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,18 @@ public MatchResult FuzzyMatch(string query, string stringToCompare, MatchOption
8383
bool allSubstringsContainedInCompareString = true;
8484

8585
var indexList = new List<int>();
86+
List<int> spaceIndices = new List<int>();
8687

8788
for (var compareStringIndex = 0; compareStringIndex < fullStringToCompareWithoutCase.Length; compareStringIndex++)
8889
{
90+
91+
// To maintain a list of indices which correspond to spaces in the string to compare
92+
// To populate the list only for the first query substring
93+
if (fullStringToCompareWithoutCase[compareStringIndex].Equals(' ') && currentQuerySubstringIndex == 0)
94+
{
95+
spaceIndices.Add(compareStringIndex);
96+
}
97+
8998
if (fullStringToCompareWithoutCase[compareStringIndex] != currentQuerySubstring[currentQuerySubstringCharacterIndex])
9099
{
91100
matchFoundInPreviousLoop = false;
@@ -147,14 +156,30 @@ public MatchResult FuzzyMatch(string query, string stringToCompare, MatchOption
147156
// proceed to calculate score if every char or substring without whitespaces matched
148157
if (allQuerySubstringsMatched)
149158
{
150-
var score = CalculateSearchScore(query, stringToCompare, firstMatchIndex, lastMatchIndex - firstMatchIndex, allSubstringsContainedInCompareString);
159+
var nearestSpaceIndex = CalculateClosestSpaceIndex(spaceIndices, firstMatchIndex);
160+
var score = CalculateSearchScore(query, stringToCompare, firstMatchIndex - nearestSpaceIndex - 1, lastMatchIndex - firstMatchIndex, allSubstringsContainedInCompareString);
151161

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

155165
return new MatchResult (false, UserSettingSearchPrecision);
156166
}
157167

168+
// To get the index of the closest space which preceeds the first matching index
169+
private int CalculateClosestSpaceIndex(List<int> spaceIndices, int firstMatchIndex)
170+
{
171+
if(spaceIndices.Count == 0)
172+
{
173+
return -1;
174+
}
175+
else
176+
{
177+
int? ind = spaceIndices.OrderBy(item => (firstMatchIndex - item)).Where(item => firstMatchIndex > item).FirstOrDefault();
178+
int closestSpaceIndex = ind ?? -1;
179+
return closestSpaceIndex;
180+
}
181+
}
182+
158183
private static bool AllPreviousCharsMatched(int startIndexToVerify, int currentQuerySubstringCharacterIndex,
159184
string fullStringToCompareWithoutCase, string currentQuerySubstring)
160185
{

0 commit comments

Comments
 (0)