Skip to content

Commit 7ceb080

Browse files
committed
Use inner loop to evaluate acronym match (Big Change)
Don't end loop before acronym match end since if acronym match exist, we will use that one.
1 parent 75b9941 commit 7ceb080

File tree

1 file changed

+61
-38
lines changed

1 file changed

+61
-38
lines changed

Flow.Launcher.Infrastructure/StringMatcher.cs

Lines changed: 61 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -51,46 +51,13 @@ public MatchResult FuzzyMatch(string query, string stringToCompare, MatchOption
5151
TranslationMapping map;
5252
(stringToCompare, map) = _alphabet?.Translate(stringToCompare) ?? (stringToCompare, null);
5353

54-
var currentQueryIndex = 0;
54+
var currentAcronymQueryIndex = 0;
5555
var acronymMatchData = new List<int>();
5656
var queryWithoutCase = opt.IgnoreCase ? query.ToLower() : query;
5757

58+
// preset acronymScore
5859
int acronymScore = 100;
5960

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-
9461
var fullStringToCompareWithoutCase = opt.IgnoreCase ? stringToCompare.ToLower() : stringToCompare;
9562

9663

@@ -109,24 +76,72 @@ public MatchResult FuzzyMatch(string query, string stringToCompare, MatchOption
10976
var indexList = new List<int>();
11077
List<int> spaceIndices = new List<int>();
11178

79+
bool spaceMet = false;
80+
11281
for (var compareStringIndex = 0;
11382
compareStringIndex < fullStringToCompareWithoutCase.Length;
11483
compareStringIndex++)
11584
{
85+
if (currentAcronymQueryIndex >= queryWithoutCase.Length
86+
|| allQuerySubstringsMatched && acronymScore < (int) UserSettingSearchPrecision)
87+
break;
88+
89+
11690
// To maintain a list of indices which correspond to spaces in the string to compare
11791
// To populate the list only for the first query substring
118-
if (fullStringToCompareWithoutCase[compareStringIndex].Equals(' ') && currentQuerySubstringIndex == 0)
92+
if (fullStringToCompareWithoutCase[compareStringIndex] == ' ' && currentQuerySubstringIndex == 0)
11993
{
12094
spaceIndices.Add(compareStringIndex);
12195
}
12296

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] !=
124137
currentQuerySubstring[currentQuerySubstringCharacterIndex])
125138
{
126139
matchFoundInPreviousLoop = false;
140+
127141
continue;
128142
}
129143

144+
130145
if (firstMatchIndex < 0)
131146
{
132147
// first matched char will become the start of the compared string
@@ -174,15 +189,22 @@ public MatchResult FuzzyMatch(string query, string stringToCompare, MatchOption
174189

175190
allQuerySubstringsMatched =
176191
AllQuerySubstringsMatched(currentQuerySubstringIndex, querySubstrings.Length);
192+
177193
if (allQuerySubstringsMatched)
178-
break;
194+
continue;
179195

180196
// otherwise move to the next query substring
181197
currentQuerySubstring = querySubstrings[currentQuerySubstringIndex];
182198
currentQuerySubstringCharacterIndex = 0;
183199
}
184200
}
185201

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+
}
186208

187209
// proceed to calculate score if every char or substring without whitespaces matched
188210
if (allQuerySubstringsMatched)
@@ -249,6 +271,7 @@ private static List<int> GetUpdatedIndexList(int startIndexToVerify, int current
249271

250272
private static bool AllQuerySubstringsMatched(int currentQuerySubstringIndex, int querySubstringsLength)
251273
{
274+
// Acronym won't utilize the substring to match
252275
return currentQuerySubstringIndex >= querySubstringsLength;
253276
}
254277

0 commit comments

Comments
 (0)