Refactorings: Optimised the Token::multiCompare function

This commit is contained in:
Daniel Marjamäki 2010-09-19 15:14:13 +02:00
parent 78bd66cd5c
commit 78e13f098e
1 changed files with 33 additions and 33 deletions

View File

@ -200,20 +200,21 @@ std::string Token::strAt(int index) const
int Token::multiCompare(const char *haystack, const char *needle) int Token::multiCompare(const char *haystack, const char *needle)
{ {
bool emptyStringFound = false; bool emptyStringFound = false;
bool noMatch = false;
const char *needlePointer = needle; const char *needlePointer = needle;
for (; *haystack && *haystack != ' '; ++haystack) while (true)
{ {
if (*haystack == '|') if (*needlePointer == *haystack)
{ {
if (noMatch) if (*needlePointer == '\0')
{ return 1;
// We didn't have a match at this round ++needlePointer;
noMatch = false; ++haystack;
} }
else if (*needlePointer == 0) else if (*haystack == '|')
{ {
// If needle and haystack are both at the end, we have a match. if (*needlePointer == 0)
{
// If needle is at the end, we have a match.
return 1; return 1;
} }
else if (needlePointer == needle) else if (needlePointer == needle)
@ -224,38 +225,37 @@ int Token::multiCompare(const char *haystack, const char *needle)
} }
needlePointer = needle; needlePointer = needle;
continue; ++haystack;
}
else if (*haystack == ' ' || *haystack == '\0')
{
if (needlePointer == needle)
return 0;
break;
} }
if (noMatch)
continue;
// If haystack and needle don't share the same character, // If haystack and needle don't share the same character,
// find next '|' character. // find next '|' character.
if (*needlePointer != *haystack) else
{ {
noMatch = true; needlePointer = needle;
continue;
do
{
++haystack;
}
while (*haystack != ' ' && *haystack != '|' && *haystack);
if (*haystack == ' ' || *haystack == '\0')
{
return emptyStringFound ? 0 : -1;
} }
// All characters in haystack and needle have matched this far ++haystack;
++needlePointer; }
} }
if (*needlePointer == '\0')
if (!noMatch)
{
if (*needlePointer == 0)
{
// If both needle and haystack are at the end, then we have a match.
return 1; return 1;
}
else if (needlePointer == needle)
{
// Last string in haystack was empty string e.g. "one|two|"
return 0;
}
}
// If empty string was found earlier from the haystack // If empty string was found earlier from the haystack
if (emptyStringFound) if (emptyStringFound)