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:
Oliver Stöneberg 2022-05-31 13:52:34 +02:00 committed by GitHub
parent 3d5d2e8fd8
commit 40bcbd47d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 10 deletions

View File

@ -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,8 +657,9 @@ 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;
} }
// [.. => search for a one-character token.. // [.. => search for a one-character token..
@ -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();
} }