Optimisation: optimised Token::Match

This commit is contained in:
Daniel Marjamäki 2010-09-20 20:15:07 +02:00
parent bc4c8e6e94
commit 7e954ebb57
1 changed files with 35 additions and 26 deletions

View File

@ -346,17 +346,8 @@ bool Token::Match(const Token *tok, const char pattern[], unsigned int varid)
{ {
const char *p = pattern; const char *p = pattern;
bool firstpattern = true; bool firstpattern = true;
bool first = true;
while (*p) while (*p)
{ {
if (!first)
{
while (*p && *p != ' ')
++p;
}
first = false;
// Skip spaces in pattern.. // Skip spaces in pattern..
while (*p == ' ') while (*p == ' ')
++p; ++p;
@ -369,20 +360,27 @@ bool Token::Match(const Token *tok, const char pattern[], unsigned int varid)
{ {
// If we have no tokens, pattern "!!else" should return true // If we have no tokens, pattern "!!else" should return true
if (p[1] == '!' && p[0] == '!' && p[2] != '\0') if (p[1] == '!' && p[0] == '!' && p[2] != '\0')
{
while (*p && *p != ' ')
++p;
continue; continue;
}
else else
return false; return false;
} }
// If we are in the first token, we skip all initial !! patterns // If we are in the first token, we skip all initial !! patterns
if (firstpattern && !tok->previous() && tok->next() && p[1] == '!' && p[0] == '!' && p[2] != '\0') if (firstpattern && !tok->previous() && tok->next() && p[1] == '!' && p[0] == '!' && p[2] != '\0')
{
while (*p && *p != ' ')
++p;
continue; continue;
}
firstpattern = false; firstpattern = false;
// Compare the first character of the string for optimization reasons // Compare the first character of the string for optimization reasons
// before doing more detailed checks. // before doing more detailed checks.
bool patternIdentified = false;
if (p[0] == '%') if (p[0] == '%')
{ {
// TODO: %var% should match only for // TODO: %var% should match only for
@ -393,8 +391,7 @@ bool Token::Match(const Token *tok, const char pattern[], unsigned int varid)
{ {
if (!tok->isName()) if (!tok->isName())
return false; return false;
p += 5;
patternIdentified = true;
} }
// Type.. // Type..
@ -409,13 +406,13 @@ bool Token::Match(const Token *tok, const char pattern[], unsigned int varid)
if (tok->str() == "delete") if (tok->str() == "delete")
return false; return false;
patternIdentified = true; p += 6;
} }
// Accept any token // Accept any token
else if (firstWordEquals(p, "%any%") == 0) else if (firstWordEquals(p, "%any%") == 0)
{ {
patternIdentified = true; p += 5;
} }
else if (firstWordEquals(p, "%varid%") == 0) else if (firstWordEquals(p, "%varid%") == 0)
@ -433,41 +430,46 @@ bool Token::Match(const Token *tok, const char pattern[], unsigned int varid)
if (tok->varId() != varid) if (tok->varId() != varid)
return false; return false;
patternIdentified = true; p += 7;
} }
else if (firstWordEquals(p, "%num%") == 0) else if (firstWordEquals(p, "%num%") == 0)
{ {
if (!tok->isNumber()) if (!tok->isNumber())
return false; return false;
p += 5;
patternIdentified = true;
} }
else if (firstWordEquals(p, "%bool%") == 0) else if (firstWordEquals(p, "%bool%") == 0)
{ {
if (!tok->isBoolean()) if (!tok->isBoolean())
return false; return false;
p += 6;
patternIdentified = true;
} }
else if (firstWordEquals(p, "%str%") == 0) else if (firstWordEquals(p, "%str%") == 0)
{ {
if (tok->_str[0] != '\"') if (tok->_str[0] != '\"')
return false; return false;
p += 5;
patternIdentified = true;
}
} }
if (patternIdentified) else if (firstWordEquals(p, tok->_str.c_str()))
{ {
// Pattern was identified already above. p += tok->_str.length();
}
else
{
return false;
}
tok = tok->next();
continue;
} }
// [.. => search for a one-character token.. // [.. => search for a one-character token..
else if (p[0] == '[' && chrInFirstWord(p, ']') && tok->_str.length() == 1) else if (p[0] == '[' && tok->_str.length() == 1 && chrInFirstWord(p, ']'))
{ {
const char *temp = p + 1; const char *temp = p + 1;
bool chrFound = false; bool chrFound = false;
@ -507,6 +509,8 @@ bool Token::Match(const Token *tok, const char pattern[], unsigned int varid)
if (res == 0) if (res == 0)
{ {
// Empty alternative matches, use the same token on next round // Empty alternative matches, use the same token on next round
while (*p && *p != ' ')
++p;
continue; continue;
} }
else if (res == -1) else if (res == -1)
@ -524,7 +528,12 @@ bool Token::Match(const Token *tok, const char pattern[], unsigned int varid)
} }
else if (firstWordEquals(p, tok->_str.c_str()) != 0) else if (firstWordEquals(p, tok->_str.c_str()) != 0)
{
return false; return false;
}
while (*p && *p != ' ')
++p;
tok = tok->next(); tok = tok->next();
} }