Skip to content

Commit 40bcbd4

Browse files
authored
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()`
1 parent 3d5d2e8 commit 40bcbd4

1 file changed

Lines changed: 12 additions & 10 deletions

File tree

lib/token.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -638,8 +638,11 @@ const char *Token::chrInFirstWord(const char *str, char c)
638638

639639
bool Token::Match(const Token *tok, const char pattern[], nonneg int varid)
640640
{
641+
if (!(*pattern))
642+
return true;
643+
641644
const char *p = pattern;
642-
while (*p) {
645+
while (true) {
643646
// Skip spaces in pattern..
644647
while (*p == ' ')
645648
++p;
@@ -654,8 +657,9 @@ bool Token::Match(const Token *tok, const char pattern[], nonneg int varid)
654657
while (*p && *p != ' ')
655658
++p;
656659
continue;
657-
} else
658-
return false;
660+
}
661+
662+
return false;
659663
}
660664

661665
// [.. => search for a one-character token..
@@ -686,17 +690,13 @@ bool Token::Match(const Token *tok, const char pattern[], nonneg int varid)
686690
return false;
687691

688692
p = temp;
689-
while (*p && *p != ' ')
690-
++p;
691693
}
692694

693695
// Parse "not" options. Token can be anything except the given one
694696
else if (p[0] == '!' && p[1] == '!' && p[2] != '\0') {
695697
p += 2;
696698
if (firstWordEquals(p, tok->str().c_str()))
697699
return false;
698-
while (*p && *p != ' ')
699-
++p;
700700
}
701701

702702
// 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)
707707
while (*p && *p != ' ')
708708
++p;
709709
continue;
710-
} else if (res == -1) {
710+
}
711+
if (res == -1) {
711712
// No match
712713
return false;
713714
}
714715
}
715716

716-
while (*p && *p != ' ')
717-
++p;
717+
// using strchr() for the other instances leads to a performance decrease
718+
if (!(p = strchr(p, ' ')))
719+
break;
718720

719721
tok = tok->next();
720722
}

0 commit comments

Comments
 (0)