Update variable names

Make variables more descriptive of the state they represent
This commit is contained in:
Jeremy Wu 2020-01-03 07:58:20 +11:00
parent f6d0738c79
commit 84d6fc2787

View File

@ -53,99 +53,89 @@ namespace Wox.Infrastructure
var queryWithoutCase = opt.IgnoreCase ? query.ToLower() : query; var queryWithoutCase = opt.IgnoreCase ? query.ToLower() : query;
var separatedqueryStrings = queryWithoutCase.Split(' '); var querySubstrings = queryWithoutCase.Split(' ');
int currentSeparatedQueryStringIndex = 0; int currentQuerySubstringIndex = 0;
var currentSeparatedQueryString = separatedqueryStrings[currentSeparatedQueryStringIndex]; var currentQuerySubstring = querySubstrings[currentQuerySubstringIndex];
var currentQuerySubstringCharacterIndex = 0;
var queryIndex = 0;
var firstMatchIndex = -1; var firstMatchIndex = -1;
var firstMatchIndexInWord = -1; var firstMatchIndexInWord = -1;
var lastMatchIndex = 0; var lastMatchIndex = 0;
bool allMatched = false; bool allQuerySubstringsMatched = false;
bool isFullWordMatched = false; bool matchFoundInPreviousLoop = false;
bool allWordsFullyMatched = true; bool allWordsFullyMatched = true;
var indexList = new List<int>(); var indexList = new List<int>();
for (var index = 0; index < fullStringToCompareWithoutCase.Length; index++) for (var compareStringIndex = 0; compareStringIndex < fullStringToCompareWithoutCase.Length; compareStringIndex++)
{ {
var ch = stringToCompare[index]; if (fullStringToCompareWithoutCase[compareStringIndex] == currentQuerySubstring[currentQuerySubstringCharacterIndex])
if (fullStringToCompareWithoutCase[index] == currentSeparatedQueryString[queryIndex])
{ {
if (firstMatchIndex < 0) if (firstMatchIndex < 0)
{ // first matched char will become the start of the compared string {
firstMatchIndex = index; // first matched char will become the start of the compared string
firstMatchIndex = compareStringIndex;
} }
if (queryIndex == 0) if (currentQuerySubstringCharacterIndex == 0)
{ // first letter of current word {
isFullWordMatched = true; // first letter of current word
firstMatchIndexInWord = index; matchFoundInPreviousLoop = true;
firstMatchIndexInWord = compareStringIndex;
} }
else if (!isFullWordMatched) else if (!matchFoundInPreviousLoop)
{ // we want to verify that there is not a better match if this is not a full word {
// we want to verify that there is not a better match if this is not a full word
// in order to do so we need to verify all previous chars are part of the pattern // in order to do so we need to verify all previous chars are part of the pattern
int startIndexToVerify = index - queryIndex; var startIndexToVerify = compareStringIndex - currentQuerySubstringCharacterIndex;
bool allMatch = true;
for (int indexToCheck = 0; indexToCheck < queryIndex; indexToCheck++) if (AllPreviousCharsMatched(startIndexToVerify, currentQuerySubstringCharacterIndex, fullStringToCompareWithoutCase, currentQuerySubstring))
{ {
if (fullStringToCompareWithoutCase[startIndexToVerify + indexToCheck] != matchFoundInPreviousLoop = true;
currentSeparatedQueryString[indexToCheck])
// if it's the begining character of the first query substring that is matched then we need to update start index
firstMatchIndex = currentQuerySubstringIndex == 0 ? startIndexToVerify : firstMatchIndex;
indexList = GetUpdatedIndexList(startIndexToVerify, currentQuerySubstringCharacterIndex, firstMatchIndexInWord, indexList);
}
}
lastMatchIndex = compareStringIndex + 1;
indexList.Add(compareStringIndex);
currentQuerySubstringCharacterIndex++;
// if finished looping through every character in the substring
if (currentQuerySubstringCharacterIndex == currentQuerySubstring.Length)
{ {
allMatch = false; currentQuerySubstringIndex++;
}
}
if (allMatch) // if all query substrings are matched
{ // update to this as a full word if (currentQuerySubstringIndex >= querySubstrings.Length)
isFullWordMatched = true;
if (currentSeparatedQueryStringIndex == 0)
{ // first word so we need to update start index
firstMatchIndex = startIndexToVerify;
}
indexList.RemoveAll(x => x >= firstMatchIndexInWord);
for (int indexToCheck = 0; indexToCheck < queryIndex; indexToCheck++)
{ // update the index list
indexList.Add(startIndexToVerify + indexToCheck);
}
}
}
lastMatchIndex = index + 1;
indexList.Add(index);
queryIndex++;
// increase the pattern matched index and check if everything was matched
if (queryIndex == currentSeparatedQueryString.Length)
{ {
currentSeparatedQueryStringIndex++; allQuerySubstringsMatched = true;
if (currentSeparatedQueryStringIndex >= separatedqueryStrings.Length)
{ // moved over all the words
allMatched = true;
break; break;
} }
// otherwise move to the next word // otherwise move to the next query substring
currentSeparatedQueryString = separatedqueryStrings[currentSeparatedQueryStringIndex]; currentQuerySubstring = querySubstrings[currentQuerySubstringIndex];
queryIndex = 0; currentQuerySubstringCharacterIndex = 0;
if (!isFullWordMatched)
{ // if any of the words was not fully matched all are not fully matched if (!matchFoundInPreviousLoop)
{
// if any of the words was not fully matched all are not fully matched
allWordsFullyMatched = false; allWordsFullyMatched = false;
} }
} }
} }
else else
{ {
isFullWordMatched = false; matchFoundInPreviousLoop = 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 (allMatched) if (allQuerySubstringsMatched)
{ {
// check if all query string was contained in string to compare // check if all query string was contained in string to compare
bool containedFully = lastMatchIndex - firstMatchIndex == queryWithoutCase.Length; bool containedFully = lastMatchIndex - firstMatchIndex == queryWithoutCase.Length;