small `Token::Match()` optimizations (#4154)
* token.cpp: fixed `readability-else-after-return` warnings in `Match()` * token.cpp: removed some duplicated code from `Match()` * token.cpp: use `strchr()` instead of loop in `Match()` * token.cpp: added early exit and removed unnecessary loop condition in `Match()`
This commit is contained in:
parent
3d5d2e8fd8
commit
40bcbd47d6
|
@ -638,8 +638,11 @@ const char *Token::chrInFirstWord(const char *str, char c)
|
||||||
|
|
||||||
bool Token::Match(const Token *tok, const char pattern[], nonneg int varid)
|
bool Token::Match(const Token *tok, const char pattern[], nonneg int varid)
|
||||||
{
|
{
|
||||||
|
if (!(*pattern))
|
||||||
|
return true;
|
||||||
|
|
||||||
const char *p = pattern;
|
const char *p = pattern;
|
||||||
while (*p) {
|
while (true) {
|
||||||
// Skip spaces in pattern..
|
// Skip spaces in pattern..
|
||||||
while (*p == ' ')
|
while (*p == ' ')
|
||||||
++p;
|
++p;
|
||||||
|
@ -654,7 +657,8 @@ bool Token::Match(const Token *tok, const char pattern[], nonneg int varid)
|
||||||
while (*p && *p != ' ')
|
while (*p && *p != ' ')
|
||||||
++p;
|
++p;
|
||||||
continue;
|
continue;
|
||||||
} else
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -686,8 +690,6 @@ bool Token::Match(const Token *tok, const char pattern[], nonneg int varid)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
p = temp;
|
p = temp;
|
||||||
while (*p && *p != ' ')
|
|
||||||
++p;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse "not" options. Token can be anything except the given one
|
// Parse "not" options. Token can be anything except the given one
|
||||||
|
@ -695,8 +697,6 @@ bool Token::Match(const Token *tok, const char pattern[], nonneg int varid)
|
||||||
p += 2;
|
p += 2;
|
||||||
if (firstWordEquals(p, tok->str().c_str()))
|
if (firstWordEquals(p, tok->str().c_str()))
|
||||||
return false;
|
return false;
|
||||||
while (*p && *p != ' ')
|
|
||||||
++p;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse multi options, such as void|int|char (accept token which is one of these 3)
|
// Parse multi options, such as void|int|char (accept token which is one of these 3)
|
||||||
|
@ -707,14 +707,16 @@ bool Token::Match(const Token *tok, const char pattern[], nonneg int varid)
|
||||||
while (*p && *p != ' ')
|
while (*p && *p != ' ')
|
||||||
++p;
|
++p;
|
||||||
continue;
|
continue;
|
||||||
} else if (res == -1) {
|
}
|
||||||
|
if (res == -1) {
|
||||||
// No match
|
// No match
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
while (*p && *p != ' ')
|
// using strchr() for the other instances leads to a performance decrease
|
||||||
++p;
|
if (!(p = strchr(p, ' ')))
|
||||||
|
break;
|
||||||
|
|
||||||
tok = tok->next();
|
tok = tok->next();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue