@@ -51,46 +51,13 @@ public MatchResult FuzzyMatch(string query, string stringToCompare, MatchOption
51
51
TranslationMapping map ;
52
52
( stringToCompare , map ) = _alphabet ? . Translate ( stringToCompare ) ?? ( stringToCompare , null ) ;
53
53
54
- var currentQueryIndex = 0 ;
54
+ var currentAcronymQueryIndex = 0 ;
55
55
var acronymMatchData = new List < int > ( ) ;
56
56
var queryWithoutCase = opt . IgnoreCase ? query . ToLower ( ) : query ;
57
57
58
+ // preset acronymScore
58
59
int acronymScore = 100 ;
59
60
60
- for ( int compareIndex = 0 ; compareIndex < stringToCompare . Length ; compareIndex ++ )
61
- {
62
- if ( currentQueryIndex >= queryWithoutCase . Length )
63
- break ;
64
-
65
-
66
- switch ( stringToCompare [ compareIndex ] )
67
- {
68
- case var c when ( compareIndex == 0 && queryWithoutCase [ currentQueryIndex ] ==
69
- char . ToLower ( stringToCompare [ compareIndex ] ) )
70
- || ( char . IsUpper ( c ) && char . ToLower ( c ) == queryWithoutCase [ currentQueryIndex ] )
71
- || ( char . IsWhiteSpace ( c ) && char . ToLower ( stringToCompare [ ++ compareIndex ] ) ==
72
- queryWithoutCase [ currentQueryIndex ] )
73
- || ( char . IsNumber ( c ) && c == queryWithoutCase [ currentQueryIndex ] ) :
74
- acronymMatchData . Add ( compareIndex ) ;
75
- currentQueryIndex ++ ;
76
- continue ;
77
-
78
- case var c when char . IsWhiteSpace ( c ) :
79
- compareIndex ++ ;
80
- acronymScore -= 10 ;
81
- break ;
82
- case var c when char . IsUpper ( c ) || char . IsNumber ( c ) :
83
- acronymScore -= 10 ;
84
- break ;
85
- }
86
- }
87
-
88
- if ( acronymMatchData . Count == query . Length && acronymScore >= 60 )
89
- {
90
- acronymMatchData = acronymMatchData . Select ( x => map ? . MapToOriginalIndex ( x ) ?? x ) . Distinct ( ) . ToList ( ) ;
91
- return new MatchResult ( true , UserSettingSearchPrecision , acronymMatchData , acronymScore ) ;
92
- }
93
-
94
61
var fullStringToCompareWithoutCase = opt . IgnoreCase ? stringToCompare . ToLower ( ) : stringToCompare ;
95
62
96
63
@@ -109,24 +76,72 @@ public MatchResult FuzzyMatch(string query, string stringToCompare, MatchOption
109
76
var indexList = new List < int > ( ) ;
110
77
List < int > spaceIndices = new List < int > ( ) ;
111
78
79
+ bool spaceMet = false ;
80
+
112
81
for ( var compareStringIndex = 0 ;
113
82
compareStringIndex < fullStringToCompareWithoutCase . Length ;
114
83
compareStringIndex ++ )
115
84
{
85
+ if ( currentAcronymQueryIndex >= queryWithoutCase . Length
86
+ || allQuerySubstringsMatched && acronymScore < ( int ) UserSettingSearchPrecision )
87
+ break ;
88
+
89
+
116
90
// To maintain a list of indices which correspond to spaces in the string to compare
117
91
// To populate the list only for the first query substring
118
- if ( fullStringToCompareWithoutCase [ compareStringIndex ] . Equals ( ' ' ) && currentQuerySubstringIndex == 0 )
92
+ if ( fullStringToCompareWithoutCase [ compareStringIndex ] == ' ' && currentQuerySubstringIndex == 0 )
119
93
{
120
94
spaceIndices . Add ( compareStringIndex ) ;
121
95
}
122
96
123
- if ( fullStringToCompareWithoutCase [ compareStringIndex ] !=
97
+ // Acronym check
98
+ if ( char . IsUpper ( stringToCompare [ compareStringIndex ] ) ||
99
+ char . IsNumber ( stringToCompare [ compareStringIndex ] ) ||
100
+ char . IsWhiteSpace ( stringToCompare [ compareStringIndex ] ) ||
101
+ spaceMet )
102
+ {
103
+ if ( fullStringToCompareWithoutCase [ compareStringIndex ] ==
104
+ queryWithoutCase [ currentAcronymQueryIndex ] )
105
+ {
106
+ currentAcronymQueryIndex ++ ;
107
+
108
+ if ( ! spaceMet )
109
+ {
110
+ char currentCompareChar = stringToCompare [ compareStringIndex ] ;
111
+ spaceMet = char . IsWhiteSpace ( currentCompareChar ) ;
112
+ // if is space, no need to check whether upper or digit, though insignificant
113
+ if ( ! spaceMet && compareStringIndex == 0 || char . IsUpper ( currentCompareChar ) ||
114
+ char . IsDigit ( currentCompareChar ) )
115
+ {
116
+ acronymMatchData . Add ( compareStringIndex ) ;
117
+ }
118
+ }
119
+ else if ( ! ( spaceMet = char . IsWhiteSpace ( stringToCompare [ compareStringIndex ] ) ) )
120
+ {
121
+ acronymMatchData . Add ( compareStringIndex ) ;
122
+ }
123
+ }
124
+ else
125
+ {
126
+ spaceMet = char . IsWhiteSpace ( stringToCompare [ compareStringIndex ] ) ;
127
+ // Acronym Penalty
128
+ if ( ! spaceMet )
129
+ {
130
+ acronymScore -= 10 ;
131
+ }
132
+ }
133
+ }
134
+ // Acronym end
135
+
136
+ if ( allQuerySubstringsMatched || fullStringToCompareWithoutCase [ compareStringIndex ] !=
124
137
currentQuerySubstring [ currentQuerySubstringCharacterIndex ] )
125
138
{
126
139
matchFoundInPreviousLoop = false ;
140
+
127
141
continue ;
128
142
}
129
143
144
+
130
145
if ( firstMatchIndex < 0 )
131
146
{
132
147
// first matched char will become the start of the compared string
@@ -174,15 +189,22 @@ public MatchResult FuzzyMatch(string query, string stringToCompare, MatchOption
174
189
175
190
allQuerySubstringsMatched =
176
191
AllQuerySubstringsMatched ( currentQuerySubstringIndex , querySubstrings . Length ) ;
192
+
177
193
if ( allQuerySubstringsMatched )
178
- break ;
194
+ continue ;
179
195
180
196
// otherwise move to the next query substring
181
197
currentQuerySubstring = querySubstrings [ currentQuerySubstringIndex ] ;
182
198
currentQuerySubstringCharacterIndex = 0 ;
183
199
}
184
200
}
185
201
202
+ // return acronym Match if possible
203
+ if ( acronymMatchData . Count == query . Length && acronymScore >= ( int ) UserSettingSearchPrecision )
204
+ {
205
+ acronymMatchData = acronymMatchData . Select ( x => map ? . MapToOriginalIndex ( x ) ?? x ) . Distinct ( ) . ToList ( ) ;
206
+ return new MatchResult ( true , UserSettingSearchPrecision , acronymMatchData , acronymScore ) ;
207
+ }
186
208
187
209
// proceed to calculate score if every char or substring without whitespaces matched
188
210
if ( allQuerySubstringsMatched )
@@ -249,6 +271,7 @@ private static List<int> GetUpdatedIndexList(int startIndexToVerify, int current
249
271
250
272
private static bool AllQuerySubstringsMatched ( int currentQuerySubstringIndex , int querySubstringsLength )
251
273
{
274
+ // Acronym won't utilize the substring to match
252
275
return currentQuerySubstringIndex >= querySubstringsLength ;
253
276
}
254
277
0 commit comments