Fixed bug in TOKEN::multiCompare. "abc" and "a" were matched.
This commit is contained in:
parent
92a651acd3
commit
b2e55216ab
|
@ -229,7 +229,7 @@ private:
|
||||||
ASSERT_EQUALS( -1, TOKEN::multiCompare( "one|two", "notfound" ) );
|
ASSERT_EQUALS( -1, TOKEN::multiCompare( "one|two", "notfound" ) );
|
||||||
ASSERT_EQUALS( -1, TOKEN::multiCompare( "verybig|two", "s" ) );
|
ASSERT_EQUALS( -1, TOKEN::multiCompare( "verybig|two", "s" ) );
|
||||||
ASSERT_EQUALS( -1, TOKEN::multiCompare( "one|two", "ne" ) );
|
ASSERT_EQUALS( -1, TOKEN::multiCompare( "one|two", "ne" ) );
|
||||||
// TODO ASSERT_EQUALS( -1, TOKEN::multiCompare( "abc|def", "a" ) );
|
ASSERT_EQUALS( -1, TOKEN::multiCompare( "abc|def", "a" ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
void match1()
|
void match1()
|
||||||
|
|
27
token.cpp
27
token.cpp
|
@ -94,10 +94,16 @@ int TOKEN::multiCompare( const char *needle, const char *haystack )
|
||||||
{
|
{
|
||||||
bool emptyStringFound = false;
|
bool emptyStringFound = false;
|
||||||
bool findNextOr = false;
|
bool findNextOr = false;
|
||||||
|
const char *haystackPointer = haystack;
|
||||||
for( ; *needle; ++needle )
|
for( ; *needle; ++needle )
|
||||||
{
|
{
|
||||||
if( *needle == '|' )
|
if( *needle == '|' )
|
||||||
{
|
{
|
||||||
|
// If needle and haystack are both at the end, we have a match.
|
||||||
|
if( *haystackPointer == 0 )
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
haystackPointer = haystack;
|
||||||
if( findNextOr )
|
if( findNextOr )
|
||||||
findNextOr = false;
|
findNextOr = false;
|
||||||
else
|
else
|
||||||
|
@ -105,16 +111,27 @@ int TOKEN::multiCompare( const char *needle, const char *haystack )
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
else if( findNextOr )
|
|
||||||
|
if( findNextOr )
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// If this part of needle equals to the haystack
|
// If haystack and needle don't share the same character, reset
|
||||||
else if( strncmp( haystack, needle, strlen( haystack ) ) == 0 )
|
// haystackpointer and find next '|' character.
|
||||||
return 1;
|
if( *haystackPointer != *needle )
|
||||||
else
|
{
|
||||||
|
haystackPointer = haystack;
|
||||||
findNextOr = true;
|
findNextOr = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// All characters in haystack and needle have matched this far
|
||||||
|
haystackPointer++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If both needle and haystack are at the end, then we have a match.
|
||||||
|
if( *haystackPointer == 0 )
|
||||||
|
return 1;
|
||||||
|
|
||||||
// If empty string was found or if last character in needle was '|'
|
// If empty string was found or if last character in needle was '|'
|
||||||
if( emptyStringFound || findNextOr == false )
|
if( emptyStringFound || findNextOr == false )
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Reference in New Issue