@@ -83,9 +83,18 @@ public MatchResult FuzzyMatch(string query, string stringToCompare, MatchOption
83
83
bool allSubstringsContainedInCompareString = true ;
84
84
85
85
var indexList = new List < int > ( ) ;
86
+ List < int > spaceIndices = new List < int > ( ) ;
86
87
87
88
for ( var compareStringIndex = 0 ; compareStringIndex < fullStringToCompareWithoutCase . Length ; compareStringIndex ++ )
88
89
{
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
+
89
98
if ( fullStringToCompareWithoutCase [ compareStringIndex ] != currentQuerySubstring [ currentQuerySubstringCharacterIndex ] )
90
99
{
91
100
matchFoundInPreviousLoop = false ;
@@ -147,14 +156,30 @@ public MatchResult FuzzyMatch(string query, string stringToCompare, MatchOption
147
156
// proceed to calculate score if every char or substring without whitespaces matched
148
157
if ( allQuerySubstringsMatched )
149
158
{
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 ) ;
151
161
152
162
return new MatchResult ( true , UserSettingSearchPrecision , indexList , score ) ;
153
163
}
154
164
155
165
return new MatchResult ( false , UserSettingSearchPrecision ) ;
156
166
}
157
167
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
+
158
183
private static bool AllPreviousCharsMatched ( int startIndexToVerify , int currentQuerySubstringCharacterIndex ,
159
184
string fullStringToCompareWithoutCase , string currentQuerySubstring )
160
185
{
0 commit comments