Move condition checking into functions

- Moved if statement that checks if all query substrings are matched into a funciton
- convert into shorthand expression the if statement that checks if all words are fully matched
This commit is contained in:
Jeremy Wu 2020-01-06 20:51:27 +11:00
parent 42a938b50b
commit e453dceacd

View file

@ -109,34 +109,28 @@ namespace Wox.Infrastructure
currentQuerySubstringCharacterIndex++; currentQuerySubstringCharacterIndex++;
// if finished looping through every character in the substring // if finished looping through every character in the current substring
if (currentQuerySubstringCharacterIndex == currentQuerySubstring.Length) if (currentQuerySubstringCharacterIndex == currentQuerySubstring.Length)
{ {
currentQuerySubstringIndex++; currentQuerySubstringIndex++;
// if all query substrings are matched allQuerySubstringsMatched = AllQuerySubstringsMatched(currentQuerySubstringIndex, querySubstrings.Length);
if (currentQuerySubstringIndex >= querySubstrings.Length) if (allQuerySubstringsMatched)
{
allQuerySubstringsMatched = true;
break; break;
}
// otherwise move to the next query substring // otherwise move to the next query substring
currentQuerySubstring = querySubstrings[currentQuerySubstringIndex]; currentQuerySubstring = querySubstrings[currentQuerySubstringIndex];
currentQuerySubstringCharacterIndex = 0; currentQuerySubstringCharacterIndex = 0;
if (!matchFoundInPreviousLoop) // if any of the substrings was not matched then consider as all are not matched
{ allWordsFullyMatched = !matchFoundInPreviousLoop ? false : allWordsFullyMatched;
// if any of the words was not fully matched all are not fully matched
allWordsFullyMatched = false;
}
} }
} }
// return rendered string if we have a match for every char or all substring without whitespaces matched // return rendered string if we have a match for every char or all substring without whitespaces matched
if (allQuerySubstringsMatched) if (allQuerySubstringsMatched)
{ {
// check if all query string was contained in string to compare // check if all query substrings were contained in the string to compare
bool containedFully = lastMatchIndex - firstMatchIndex == queryWithoutCase.Length; bool containedFully = lastMatchIndex - firstMatchIndex == queryWithoutCase.Length;
var score = CalculateSearchScore(query, stringToCompare, firstMatchIndex, lastMatchIndex - firstMatchIndex, containedFully, allWordsFullyMatched); var score = CalculateSearchScore(query, stringToCompare, firstMatchIndex, lastMatchIndex - firstMatchIndex, containedFully, allWordsFullyMatched);
var pinyinScore = ScoreForPinyin(stringToCompare, query); var pinyinScore = ScoreForPinyin(stringToCompare, query);
@ -186,6 +180,11 @@ namespace Wox.Infrastructure
return updatedList; return updatedList;
} }
private static bool AllQuerySubstringsMatched(int currentQuerySubstringIndex, int querySubstringsLength)
{
return currentQuerySubstringIndex >= querySubstringsLength;
}
private static int CalculateSearchScore(string query, string stringToCompare, int firstIndex, int matchLen, private static int CalculateSearchScore(string query, string stringToCompare, int firstIndex, int matchLen,
bool isFullyContained, bool allWordsFullyMatched) bool isFullyContained, bool allWordsFullyMatched)
{ {